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