tor  0.4.2.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  /* We include v2 dir auths here too, because they need to answer
70  * controllers. Eventually we'll deprecate this whole function;
71  * see also networkstatus_getinfo_by_purpose(). */
72  tor_assert(router_status_out);
73 
74  rs_entries = smartlist_new();
75 
76  SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
77  const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
78  tor_assert(node);
79  if (for_controller) {
80  char name_buf[MAX_VERBOSE_NICKNAME_LEN+2];
81  char *cp = name_buf;
82  if (!node->is_running)
83  *cp++ = '!';
85  smartlist_add_strdup(rs_entries, name_buf);
86  } else if (ri->cache_info.published_on >= cutoff) {
87  smartlist_add(rs_entries, list_single_server_status(ri,
88  node->is_running));
89  }
90  } SMARTLIST_FOREACH_END(ri);
91 
92  *router_status_out = smartlist_join_strings(rs_entries, " ", 0, NULL);
93 
94  SMARTLIST_FOREACH(rs_entries, char *, cp, tor_free(cp));
95  smartlist_free(rs_entries);
96 
97  return 0;
98 }
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 describe.c.
Header file for nodelist.c.
void smartlist_add(smartlist_t *sl, void *element)
#define MAX_NICKNAME_LEN
Definition: or.h:113
Header file for config.c.
#define tor_free(p)
Definition: malloc.h:52
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:226
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:478
#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)