Tor  0.4.3.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-2020, 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"
20 
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  format_iso_time(published, rs->published_on);
57  digest_to_base64(identity64, rs->identity_digest);
58  digest_to_base64(digest64, rs->descriptor_digest);
59 
61  "r %s %s %s%s%s %s %d %d\n",
62  rs->nickname,
63  identity64,
64  (format==NS_V3_CONSENSUS_MICRODESC)?"":digest64,
65  (format==NS_V3_CONSENSUS_MICRODESC)?"":" ",
66  published,
67  fmt_addr32(rs->addr),
68  (int)rs->or_port,
69  (int)rs->dir_port);
70 
71  /* TODO: Maybe we want to pass in what we need to build the rest of
72  * this here, instead of in the caller. Then we could use the
73  * networkstatus_type_t values, with an additional control port value
74  * added -MP */
75 
76  /* Possible "a" line. At most one for now. */
77  if (!tor_addr_is_null(&rs->ipv6_addr)) {
78  smartlist_add_asprintf(chunks, "a %s\n",
80  }
81 
82  if (format == NS_V3_CONSENSUS || format == NS_V3_CONSENSUS_MICRODESC)
83  goto done;
84 
86  "s%s%s%s%s%s%s%s%s%s%s%s\n",
87  /* These must stay in alphabetical order. */
88  rs->is_authority?" Authority":"",
89  rs->is_bad_exit?" BadExit":"",
90  rs->is_exit?" Exit":"",
91  rs->is_fast?" Fast":"",
92  rs->is_possible_guard?" Guard":"",
93  rs->is_hs_dir?" HSDir":"",
94  rs->is_flagged_running?" Running":"",
95  rs->is_stable?" Stable":"",
96  rs->is_staledesc?" StaleDesc":"",
97  rs->is_v2_dir?" V2Dir":"",
98  rs->is_valid?" Valid":"");
99 
100  /* length of "opt v \n" */
101 #define V_LINE_OVERHEAD 7
102  if (version && strlen(version) < MAX_V_LINE_LEN - V_LINE_OVERHEAD) {
103  smartlist_add_asprintf(chunks, "v %s\n", version);
104  }
105  if (protocols) {
106  smartlist_add_asprintf(chunks, "pr %s\n", protocols);
107  }
108 
109  if (format != NS_V2) {
111  uint32_t bw_kb;
112 
113  if (format != NS_CONTROL_PORT) {
114  /* Blow up more or less nicely if we didn't get anything or not the
115  * thing we expected.
116  */
117  if (!desc) {
118  char id[HEX_DIGEST_LEN+1];
119  char dd[HEX_DIGEST_LEN+1];
120 
121  base16_encode(id, sizeof(id), rs->identity_digest, DIGEST_LEN);
122  base16_encode(dd, sizeof(dd), rs->descriptor_digest, DIGEST_LEN);
123  log_warn(LD_BUG, "Cannot get any descriptor for %s "
124  "(wanted descriptor %s).",
125  id, dd);
126  goto err;
127  }
128 
129  /* This assert could fire for the control port, because
130  * it can request NS documents before all descriptors
131  * have been fetched. Therefore, we only do this test when
132  * format != NS_CONTROL_PORT. */
133  if (tor_memneq(desc->cache_info.signed_descriptor_digest,
134  rs->descriptor_digest,
135  DIGEST_LEN)) {
136  char rl_d[HEX_DIGEST_LEN+1];
137  char rs_d[HEX_DIGEST_LEN+1];
138  char id[HEX_DIGEST_LEN+1];
139 
140  base16_encode(rl_d, sizeof(rl_d),
141  desc->cache_info.signed_descriptor_digest, DIGEST_LEN);
142  base16_encode(rs_d, sizeof(rs_d), rs->descriptor_digest, DIGEST_LEN);
143  base16_encode(id, sizeof(id), rs->identity_digest, DIGEST_LEN);
144  log_err(LD_BUG, "descriptor digest in routerlist does not match "
145  "the one in routerstatus: %s vs %s "
146  "(router %s)\n",
147  rl_d, rs_d, id);
148 
150  rs->descriptor_digest,
151  DIGEST_LEN));
152  }
153  }
154 
155  if (format == NS_CONTROL_PORT && rs->has_bandwidth) {
156  bw_kb = rs->bandwidth_kb;
157  } else {
158  tor_assert(desc);
159  bw_kb = router_get_advertised_bandwidth_capped(desc) / 1000;
160  }
161  smartlist_add_asprintf(chunks,
162  "w Bandwidth=%d", bw_kb);
163 
164  if (format == NS_V3_VOTE && vrs && vrs->has_measured_bw) {
165  smartlist_add_asprintf(chunks,
166  " Measured=%d", vrs->measured_bw_kb);
167  }
168  /* Write down guardfraction information if we have it. */
169  if (format == NS_V3_VOTE && vrs && vrs->status.has_guardfraction) {
170  smartlist_add_asprintf(chunks,
171  " GuardFraction=%d",
173  }
174 
175  smartlist_add_strdup(chunks, "\n");
176 
177  if (desc) {
178  summary = policy_summarize(desc->exit_policy, AF_INET);
179  smartlist_add_asprintf(chunks, "p %s\n", summary);
180  tor_free(summary);
181  }
182 
183  if (format == NS_V3_VOTE && vrs) {
184  if (fast_mem_is_zero((char*)vrs->ed25519_id, ED25519_PUBKEY_LEN)) {
185  smartlist_add_strdup(chunks, "id ed25519 none\n");
186  } else {
187  char ed_b64[BASE64_DIGEST256_LEN+1];
188  digest256_to_base64(ed_b64, (const char*)vrs->ed25519_id);
189  smartlist_add_asprintf(chunks, "id ed25519 %s\n", ed_b64);
190  }
191  }
192  }
193 
194  done:
195  result = smartlist_join_strings(chunks, "", 0, NULL);
196 
197  err:
198  SMARTLIST_FOREACH(chunks, char *, cp, tor_free(cp));
199  smartlist_free(chunks);
200 
201  return result;
202 }
char * routerstatus_format_entry(const routerstatus_t *rs, const char *version, const char *protocols, routerstatus_format_type_t format, const vote_routerstatus_t *vrs)
uint32_t router_get_advertised_bandwidth_capped(const routerinfo_t *router)
Definition: routerlist.c:572
Router descriptor structure.
uint16_t ipv6_orport
Format routerstatus entries for controller, vote, or consensus.
char * policy_summarize(smartlist_t *policy, sa_family_t family)
Definition: policies.c:2636
unsigned int is_hs_dir
void smartlist_add_strdup(struct smartlist_t *sl, const char *string)
char nickname[MAX_NICKNAME_LEN+1]
unsigned int is_fast
unsigned int is_stable
void digest_to_base64(char *d64, const char *digest)
int fast_mem_is_zero(const char *mem, size_t len)
Definition: util_string.c:74
#define tor_assert(expr)
Definition: util_bug.h:102
unsigned int has_bandwidth
#define tor_free(p)
Definition: malloc.h:52
#define ED25519_PUBKEY_LEN
Definition: x25519_sizes.h:27
smartlist_t * smartlist_new(void)
unsigned int is_exit
#define tor_memneq(a, b, sz)
Definition: di_ops.h:21
Header file for policies.c.
char signed_descriptor_digest[DIGEST_LEN]
char descriptor_digest[DIGEST256_LEN]
unsigned int is_bad_exit
Header for crypto_format.c.
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
const char * fmt_addrport(const tor_addr_t *addr, uint16_t port)
Definition: address.c:1169
const routerinfo_t * router_get_by_id_digest(const char *digest)
Definition: routerlist.c:691
#define DIGEST_LEN
Definition: digest_sizes.h:20
unsigned int is_possible_guard
void smartlist_add_asprintf(struct smartlist_t *sl, const char *pattern,...)
Definition: smartlist.c:36
Master header file for Tor-specific functionality.
tor_addr_t ipv6_addr
const char * fmt_addr32(uint32_t addr)
Definition: address.c:1181
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:770
unsigned int has_guardfraction
void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen)
Definition: binascii.c:478
void digest256_to_base64(char *d64, const char *digest)
#define MAX_V_LINE_LEN
#define HEX_DIGEST_LEN
Definition: crypto_digest.h:35
unsigned int is_staledesc
void format_iso_time(char *buf, time_t t)
Definition: time_fmt.c:295
char * smartlist_join_strings(smartlist_t *sl, const char *join, int terminate, size_t *len_out)
Definition: smartlist.c:279
Header file for dirvote.c.
#define SMARTLIST_FOREACH(sl, type, var, cmd)
uint32_t guardfraction_percentage
routerstatus_format_type_t
#define BASE64_DIGEST_LEN
Definition: crypto_digest.h:26
char identity_digest[DIGEST_LEN]
smartlist_t * exit_policy
Definition: routerinfo_st.h:61
uint32_t bandwidth_kb
#define BASE64_DIGEST256_LEN
Definition: crypto_digest.h:29
uint8_t ed25519_id[ED25519_PUBKEY_LEN]
unsigned int is_authority
unsigned int is_valid
#define LD_BUG
Definition: log.h:86
unsigned int is_flagged_running
Header file for routerlist.c.
Routerstatus (vote entry) structure.