Tor  0.4.7.0-alpha-dev
fmt_routerstatus.c
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 /**
7  * \file fmt_routerstatus.h
8  * \brief Format routerstatus entries for controller, vote, or consensus.
9  *
10  * (Because controllers consume this format, we can't make this
11  * code dirauth-only.)
12  **/
13 
14 #include "core/or/or.h"
16 
17 #include "core/or/policies.h"
22 #include "feature/stats/rephist.h"
23 
25 
26 /** Helper: write the router-status information in <b>rs</b> into a newly
27  * allocated character buffer. Use the same format as in network-status
28  * documents. If <b>version</b> is non-NULL, add a "v" line for the platform.
29  *
30  * Return 0 on success, -1 on failure.
31  *
32  * The format argument has one of the following values:
33  * NS_V2 - Output an entry suitable for a V2 NS opinion document
34  * NS_V3_CONSENSUS - Output the first portion of a V3 NS consensus entry
35  * for consensus_method.
36  * NS_V3_CONSENSUS_MICRODESC - Output the first portion of a V3 microdesc
37  * consensus entry for consensus_method.
38  * NS_V3_VOTE - Output a complete V3 NS vote. If <b>vrs</b> is present,
39  * it contains additional information for the vote.
40  * NS_CONTROL_PORT - Output a NS document for the control port.
41  */
42 char *
43 routerstatus_format_entry(const routerstatus_t *rs, const char *version,
44  const char *protocols,
46  const vote_routerstatus_t *vrs)
47 {
48  char *summary;
49  char *result = NULL;
50 
51  char published[ISO_TIME_LEN+1];
52  char identity64[BASE64_DIGEST_LEN+1];
53  char digest64[BASE64_DIGEST_LEN+1];
54  smartlist_t *chunks = smartlist_new();
55 
56  const char *ip_str = fmt_addr(&rs->ipv4_addr);
57  if (ip_str[0] == '\0')
58  goto err;
59 
60  format_iso_time(published, rs->published_on);
61  digest_to_base64(identity64, rs->identity_digest);
62  digest_to_base64(digest64, rs->descriptor_digest);
63 
65  "r %s %s %s%s%s %s %" PRIu16 " %" PRIu16 "\n",
66  rs->nickname,
67  identity64,
68  (format==NS_V3_CONSENSUS_MICRODESC)?"":digest64,
69  (format==NS_V3_CONSENSUS_MICRODESC)?"":" ",
70  published,
71  ip_str,
72  rs->ipv4_orport,
73  rs->ipv4_dirport);
74 
75  /* TODO: Maybe we want to pass in what we need to build the rest of
76  * this here, instead of in the caller. Then we could use the
77  * networkstatus_type_t values, with an additional control port value
78  * added -MP */
79 
80  /* Possible "a" line. At most one for now. */
81  if (!tor_addr_is_null(&rs->ipv6_addr)) {
82  smartlist_add_asprintf(chunks, "a %s\n",
84  }
85 
86  if (format == NS_V3_CONSENSUS || format == NS_V3_CONSENSUS_MICRODESC)
87  goto done;
88 
90  "s%s%s%s%s%s%s%s%s%s%s%s%s\n",
91  /* These must stay in alphabetical order. */
92  rs->is_authority?" Authority":"",
93  rs->is_bad_exit?" BadExit":"",
94  rs->is_exit?" Exit":"",
95  rs->is_fast?" Fast":"",
96  rs->is_possible_guard?" Guard":"",
97  rs->is_hs_dir?" HSDir":"",
98  rs->is_flagged_running?" Running":"",
99  rs->is_stable?" Stable":"",
100  rs->is_staledesc?" StaleDesc":"",
101  rs->is_sybil?" Sybil":"",
102  rs->is_v2_dir?" V2Dir":"",
103  rs->is_valid?" Valid":"");
104 
105  /* length of "opt v \n" */
106 #define V_LINE_OVERHEAD 7
107  if (version && strlen(version) < MAX_V_LINE_LEN - V_LINE_OVERHEAD) {
108  smartlist_add_asprintf(chunks, "v %s\n", version);
109  }
110  if (protocols) {
111  smartlist_add_asprintf(chunks, "pr %s\n", protocols);
112  }
113 
114  if (format != NS_V2) {
116  uint32_t bw_kb;
117 
118  if (format != NS_CONTROL_PORT) {
119  /* Blow up more or less nicely if we didn't get anything or not the
120  * thing we expected.
121  * This should be kept in sync with the function
122  * routerstatus_has_visibly_changed and the struct routerstatus_t
123  */
124  if (!desc) {
125  char id[HEX_DIGEST_LEN+1];
126  char dd[HEX_DIGEST_LEN+1];
127 
128  base16_encode(id, sizeof(id), rs->identity_digest, DIGEST_LEN);
129  base16_encode(dd, sizeof(dd), rs->descriptor_digest, DIGEST_LEN);
130  log_warn(LD_BUG, "Cannot get any descriptor for %s "
131  "(wanted descriptor %s).",
132  id, dd);
133  goto err;
134  }
135 
136  /* This assert could fire for the control port, because
137  * it can request NS documents before all descriptors
138  * have been fetched. Therefore, we only do this test when
139  * format != NS_CONTROL_PORT. */
140  if (tor_memneq(desc->cache_info.signed_descriptor_digest,
141  rs->descriptor_digest,
142  DIGEST_LEN)) {
143  char rl_d[HEX_DIGEST_LEN+1];
144  char rs_d[HEX_DIGEST_LEN+1];
145  char id[HEX_DIGEST_LEN+1];
146 
147  base16_encode(rl_d, sizeof(rl_d),
148  desc->cache_info.signed_descriptor_digest, DIGEST_LEN);
149  base16_encode(rs_d, sizeof(rs_d), rs->descriptor_digest, DIGEST_LEN);
150  base16_encode(id, sizeof(id), rs->identity_digest, DIGEST_LEN);
151  log_err(LD_BUG, "descriptor digest in routerlist does not match "
152  "the one in routerstatus: %s vs %s "
153  "(router %s)\n",
154  rl_d, rs_d, id);
155 
157  rs->descriptor_digest,
158  DIGEST_LEN));
159  }
160  }
161 
162  if (format == NS_CONTROL_PORT && rs->has_bandwidth) {
163  bw_kb = rs->bandwidth_kb;
164  } else {
165  tor_assert(desc);
166  bw_kb = router_get_advertised_bandwidth_capped(desc) / 1000;
167  }
168  smartlist_add_asprintf(chunks,
169  "w Bandwidth=%d", bw_kb);
170 
171  if (format == NS_V3_VOTE && vrs && vrs->has_measured_bw) {
172  smartlist_add_asprintf(chunks,
173  " Measured=%d", vrs->measured_bw_kb);
174  }
175  /* Write down guardfraction information if we have it. */
176  if (format == NS_V3_VOTE && vrs && vrs->status.has_guardfraction) {
177  smartlist_add_asprintf(chunks,
178  " GuardFraction=%d",
180  }
181 
182  smartlist_add_strdup(chunks, "\n");
183 
184  if (desc) {
185  summary = policy_summarize(desc->exit_policy, AF_INET);
186  smartlist_add_asprintf(chunks, "p %s\n", summary);
187  tor_free(summary);
188  }
189 
190  if (format == NS_V3_VOTE && vrs) {
191  if (fast_mem_is_zero((char*)vrs->ed25519_id, ED25519_PUBKEY_LEN)) {
192  smartlist_add_strdup(chunks, "id ed25519 none\n");
193  } else {
194  char ed_b64[BASE64_DIGEST256_LEN+1];
195  digest256_to_base64(ed_b64, (const char*)vrs->ed25519_id);
196  smartlist_add_asprintf(chunks, "id ed25519 %s\n", ed_b64);
197  }
198 
199  /* We'll add a series of statistics to the vote per relays so we are
200  * able to assess what each authorities sees and help our health and
201  * performance work. */
202  time_t now = time(NULL);
203  smartlist_add_asprintf(chunks, "stats wfu=%.6f tk=%lu mtbf=%.0f\n",
207  }
208  }
209 
210  done:
211  result = smartlist_join_strings(chunks, "", 0, NULL);
212 
213  err:
214  SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
215  smartlist_free(chunks);
216 
217  return result;
218 }
const char * fmt_addrport(const tor_addr_t *addr, uint16_t port)
Definition: address.c:1199
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:780
#define fmt_addr(a)
Definition: address.h:239
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
#define BASE64_DIGEST256_LEN
Definition: crypto_digest.h:29
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
#define BASE64_DIGEST_LEN
Definition: crypto_digest.h:26
void digest256_to_base64(char *d64, const char *digest)
void digest_to_base64(char *d64, const char *digest)
Header for crypto_format.c.
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
#define tor_memneq(a, b, sz)
Definition: di_ops.h:21
#define DIGEST_LEN
Definition: digest_sizes.h:20
Header file for dirvote.c.
Format routerstatus entries for controller, vote, or consensus.
routerstatus_format_type_t
@ NS_V3_VOTE
@ NS_CONTROL_PORT
@ NS_V3_CONSENSUS
@ NS_V3_CONSENSUS_MICRODESC
@ NS_V2
#define MAX_V_LINE_LEN
char * routerstatus_format_entry(const routerstatus_t *rs, const char *version, const char *protocols, routerstatus_format_type_t format, const vote_routerstatus_t *vrs)
#define LD_BUG
Definition: log.h:86
#define tor_free(p)
Definition: malloc.h:52
Master header file for Tor-specific functionality.
char * policy_summarize(smartlist_t *policy, sa_family_t family)
Definition: policies.c:2573
Header file for policies.c.
double rep_hist_get_stability(const char *id, time_t when)
Definition: rephist.c:839
double rep_hist_get_weighted_fractional_uptime(const char *id, time_t when)
Definition: rephist.c:851
long rep_hist_get_weighted_time_known(const char *id, time_t when)
Definition: rephist.c:867
Header file for rephist.c.
Router descriptor structure.
const routerinfo_t * router_get_by_id_digest(const char *digest)
Definition: routerlist.c:776
uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router)
Definition: routerlist.c:657
Header file for routerlist.c.
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
smartlist_t * smartlist_new(void)
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
smartlist_t * exit_policy
Definition: routerinfo_st.h:59
unsigned int is_bad_exit
uint16_t ipv6_orport
tor_addr_t ipv6_addr
unsigned int is_sybil
unsigned int is_staledesc
char descriptor_digest[DIGEST256_LEN]
char identity_digest[DIGEST_LEN]
unsigned int is_hs_dir
unsigned int has_guardfraction
unsigned int is_valid
char nickname[MAX_NICKNAME_LEN+1]
unsigned int has_bandwidth
uint16_t ipv4_dirport
unsigned int is_possible_guard
unsigned int is_stable
unsigned int is_flagged_running
unsigned int is_exit
unsigned int is_authority
uint32_t guardfraction_percentage
unsigned int is_fast
uint32_t bandwidth_kb
uint16_t ipv4_orport
char signed_descriptor_digest[DIGEST_LEN]
uint8_t ed25519_id[ED25519_PUBKEY_LEN]
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
#define tor_assert(expr)
Definition: util_bug.h:102
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
Routerstatus (vote entry) structure.
#define ED25519_PUBKEY_LEN
Definition: x25519_sizes.h:27