Tor  0.4.3.0-alpha-dev
fmt_serverstatus.c
Go to the documentation of this file.
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2020, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 /**
7  * @file fmt_serverstatus.c
8  * @brief Format relay info for a controller.
9  **/
10 
11 #include "core/or/or.h"
13 
14 #include "app/config/config.h"
16 #include "feature/dirauth/voteflags.h"// XXXX remove
19 
22 
23 /**
24  * Allocate and return a description of the status of the server <b>desc</b>,
25  * for use in a v1-style router-status line. The server is listed
26  * as running iff <b>is_live</b> is true.
27  *
28  * This is deprecated: it's only used for controllers that want outputs in
29  * the old format.
30  */
31 static char *
32 list_single_server_status(const routerinfo_t *desc, int is_live)
33 {
34  char buf[MAX_NICKNAME_LEN+HEX_DIGEST_LEN+4]; /* !nickname=$hexdigest\0 */
35  char *cp;
36  const node_t *node;
37 
38  tor_assert(desc);
39 
40  cp = buf;
41  if (!is_live) {
42  *cp++ = '!';
43  }
44  node = node_get_by_id(desc->cache_info.identity_digest);
45  if (node && node->is_valid) {
46  strlcpy(cp, desc->nickname, sizeof(buf)-(cp-buf));
47  cp += strlen(cp);
48  *cp++ = '=';
49  }
50  *cp++ = '$';
51  base16_encode(cp, HEX_DIGEST_LEN+1, desc->cache_info.identity_digest,
52  DIGEST_LEN);
53  return tor_strdup(buf);
54 }
55 
56 /** Based on the routerinfo_ts in <b>routers</b>, allocate the
57  * contents of a v1-style router-status line, and store it in
58  * *<b>router_status_out</b>. Return 0 on success, -1 on failure.
59  *
60  * If for_controller is true, include the routers with very old descriptors.
61  *
62  * This is deprecated: it's only used for controllers that want outputs in
63  * the old format.
64  */
65 int
66 list_server_status_v1(smartlist_t *routers, char **router_status_out,
67  int for_controller)
68 {
69  /* List of entries in a router-status style: An optional !, then an optional
70  * equals-suffixed nickname, then a dollar-prefixed hexdigest. */
71  smartlist_t *rs_entries;
72  time_t now = time(NULL);
73  time_t cutoff = now - ROUTER_MAX_AGE_TO_PUBLISH;
74  /* We include v2 dir auths here too, because they need to answer
75  * controllers. Eventually we'll deprecate this whole function;
76  * see also networkstatus_getinfo_by_purpose(). */
77  tor_assert(router_status_out);
78 
79  rs_entries = smartlist_new();
80 
81  SMARTLIST_FOREACH_BEGIN(routers, routerinfo_t *, ri) {
82  const node_t *node = node_get_by_id(ri->cache_info.identity_digest);
83  tor_assert(node);
84  if (for_controller) {
85  char name_buf[MAX_VERBOSE_NICKNAME_LEN+2];
86  char *cp = name_buf;
87  if (!node->is_running)
88  *cp++ = '!';
90  smartlist_add_strdup(rs_entries, name_buf);
91  } else if (ri->cache_info.published_on >= cutoff) {
93  node->is_running));
94  }
95  } SMARTLIST_FOREACH_END(ri);
96 
97  *router_status_out = smartlist_join_strings(rs_entries, " ", 0, NULL);
98 
99  SMARTLIST_FOREACH(rs_entries, char *, cp, tor_free(cp));
100  smartlist_free(rs_entries);
101 
102  return 0;
103 }
Router descriptor structure.
unsigned int is_valid
Definition: node_st.h:65
Definition: node_st.h:34
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)
Node information structure.
#define MAX_NICKNAME_LEN
Definition: or.h:112
Header file for config.c.
char identity_digest[DIGEST_LEN]
#define tor_assert(expr)
Definition: util_bug.h:102
static char * list_single_server_status(const routerinfo_t *desc, int is_live)
char * nickname
Definition: routerinfo_st.h:22
#define tor_free(p)
Definition: malloc.h:52
smartlist_t * smartlist_new(void)
Header file for fmt_serverstatus.c.
Header file for directory authority mode.
void router_get_verbose_nickname(char *buf, const routerinfo_t *router)
Definition: describe.c:226
#define MAX_VERBOSE_NICKNAME_LEN
Definition: or.h:118
#define DIGEST_LEN
Definition: digest_sizes.h:20
Master header file for Tor-specific functionality.
const node_t * node_get_by_id(const char *identity_digest)
Definition: nodelist.c:223
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
unsigned int is_running
Definition: node_st.h:63
int list_server_status_v1(smartlist_t *routers, char **router_status_out, int for_controller)
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
#define ROUTER_MAX_AGE_TO_PUBLISH
Definition: or.h:161
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)