tor  0.4.1.0-alpha-dev
fmt_serverstatus.c
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 #include "core/or/or.h"
8 
9 #include "app/config/config.h"
11 #include "feature/dirauth/voteflags.h"// XXXX remove
14 
15 #include "feature/nodelist/node_st.h"
16 #include "feature/nodelist/routerinfo_st.h"
17 
26 static char *
27 list_single_server_status(const routerinfo_t *desc, int is_live)
28 {
29  char buf[MAX_NICKNAME_LEN+HEX_DIGEST_LEN+4]; /* !nickname=$hexdigest\0 */
30  char *cp;
31  const node_t *node;
32 
33  tor_assert(desc);
34 
35  cp = buf;
36  if (!is_live) {
37  *cp++ = '!';
38  }
39  node = node_get_by_id(desc->cache_info.identity_digest);
40  if (node && node->is_valid) {
41  strlcpy(cp, desc->nickname, sizeof(buf)-(cp-buf));
42  cp += strlen(cp);
43  *cp++ = '=';
44  }
45  *cp++ = '$';
46  base16_encode(cp, HEX_DIGEST_LEN+1, desc->cache_info.identity_digest,
47  DIGEST_LEN);
48  return tor_strdup(buf);
49 }
50 
60 int
61 list_server_status_v1(smartlist_t *routers, char **router_status_out,
62  int for_controller)
63 {
64  /* List of entries in a router-status style: An optional !, then an optional
65  * equals-suffixed nickname, then a dollar-prefixed hexdigest. */
66  smartlist_t *rs_entries;
67  time_t now = time(NULL);
68  time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
69  const or_options_t *options = get_options();
70  /* We include v2 dir auths here too, because they need to answer
71  * controllers. Eventually we'll deprecate this whole function;
72  * see also networkstatus_getinfo_by_purpose(). */
73  int authdir = authdir_mode_publishes_statuses(options);
74  tor_assert(router_status_out);
75 
76  rs_entries = smartlist_new();
77 
78  SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
79  const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
80  tor_assert(node);
81  if (authdir) {
82  /* Update router status in routerinfo_t. */
84  }
85  if (for_controller) {
86  char name_buf[MAX_VERBOSE_NICKNAME_LEN+2];
87  char *cp = name_buf;
88  if (!node->is_running)
89  *cp++ = '!';
91  smartlist_add_strdup(rs_entries, name_buf);
92  } else if (ri->cache_info.published_on >= cutoff) {
93  smartlist_add(rs_entries, list_single_server_status(ri,
94  node->is_running));
95  }
96  } SMARTLIST_FOREACH_END(ri);
97 
98  *router_status_out = smartlist_join_strings(rs_entries, " ", 0, NULL);
99 
100  SMARTLIST_FOREACH(rs_entries, char *, cp, tor_free(cp));
101  smartlist_free(rs_entries);
102 
103  return 0;
104 }
Definition: node_st.h:28
Header file for voteflags.c.
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
Header file for nodelist.c.
void smartlist_add(smartlist_t *sl, void *element)
int authdir_mode_publishes_statuses(const or_options_t *options)
Definition: authmode.c:49
#define MAX_NICKNAME_LEN
Definition: or.h:113
Header file for config.c.
#define tor_free(p)
Definition: malloc.h:52
void dirserv_set_router_is_running(routerinfo_t *router, time_t now)
Definition: voteflags.c:475
unsigned int is_valid
Definition: node_st.h:59
Header file for fmt_serverstatus.c.
Header file for directory authority mode.
tor_assert(buffer)
void router_get_verbose_nickname(char *buf, const routerinfo_t *router)
Definition: describe.c:176
unsigned int is_running
Definition: node_st.h:57
#define MAX_VERBOSE_NICKNAME_LEN
Definition: or.h:119
#define DIGEST_LEN
Definition: digest_sizes.h:20
Master header file for Tor-specific functionality.
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:476
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
char identity_digest[DIGEST_LEN]
#define ROUTER_MAX_AGE_TO_PUBLISH
Definition: or.h:162
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
#define SMARTLIST_FOREACH(sl, type, var, cmd)
char * nickname
Definition: routerinfo_st.h:17
int list_server_status_v1(smartlist_t *routers, char **router_status_out, int for_controller)
Header file for routerinfo.c.