Tor  0.4.3.0-alpha-dev
dirlist.c
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * \file dirlist.c
9  * \brief Code to maintain our lists of directory authorities and
10  * fallback directories.
11  *
12  * For the directory authorities, we have a list containing the public
13  * identity key, and contact points, for each authority. The
14  * authorities receive descriptors from relays, and publish consensuses,
15  * descriptors, and microdescriptors. This list is pre-configured.
16  *
17  * Fallback directories are well-known, stable, but untrusted directory
18  * caches that clients which have not yet bootstrapped can use to get
19  * their first networkstatus consensus, in order to find out where the
20  * Tor network really is. This list is pre-configured in
21  * fallback_dirs.inc. Every authority also serves as a fallback.
22  *
23  * Both fallback directories and directory authorities are are
24  * represented by a dir_server_t.
25  */
26 
27 #include "core/or/or.h"
28 
29 #include "app/config/config.h"
30 #include "core/or/policies.h"
39 #include "feature/relay/router.h"
40 #include "lib/net/resolve.h"
41 
44 
45 /** Global list of a dir_server_t object for each directory
46  * authority. */
48 /** Global list of dir_server_t objects for all directory authorities
49  * and all fallback directory servers. */
51 
52 /** Return the number of directory authorities whose type matches some bit set
53  * in <b>type</b> */
54 int
56 {
57  int n = 0;
59  return 0;
61  if (ds->type & type)
62  ++n);
63  return n;
64 }
65 
66 /** Return a smartlist containing a list of dir_server_t * for all
67  * known trusted dirservers. Callers must not modify the list or its
68  * contents.
69  */
72 {
75 
76  return trusted_dir_servers;
77 }
78 
80 router_get_fallback_dir_servers_mutable(void)
81 {
84 
85  return fallback_dir_servers;
86 }
87 
88 const smartlist_t *
89 router_get_trusted_dir_servers(void)
90 {
92 }
93 
94 const smartlist_t *
95 router_get_fallback_dir_servers(void)
96 {
97  return router_get_fallback_dir_servers_mutable();
98 }
99 
100 /** Reset all internal variables used to count failed downloads of network
101  * status objects. */
102 void
104 {
106 }
107 
108 /** Return the dir_server_t for the directory authority whose identity
109  * key hashes to <b>digest</b>, or NULL if no such authority is known.
110  */
111 dir_server_t *
113 {
114  if (!trusted_dir_servers)
115  return NULL;
116 
118  {
119  if (tor_memeq(ds->digest, digest, DIGEST_LEN))
120  return ds;
121  });
122 
123  return NULL;
124 }
125 
126 /** Return the dir_server_t for the fallback dirserver whose identity
127  * key hashes to <b>digest</b>, or NULL if no such fallback is in the list of
128  * fallback_dir_servers. (fallback_dir_servers is affected by the FallbackDir
129  * and UseDefaultFallbackDirs torrc options.)
130  * The list of fallback directories includes the list of authorities.
131  */
132 dir_server_t *
134 {
136  return NULL;
137 
138  if (!digest)
139  return NULL;
140 
142  {
143  if (tor_memeq(ds->digest, digest, DIGEST_LEN))
144  return ds;
145  });
146 
147  return NULL;
148 }
149 
150 /** Return 1 if any fallback dirserver's identity key hashes to <b>digest</b>,
151  * or 0 if no such fallback is in the list of fallback_dir_servers.
152  * (fallback_dir_servers is affected by the FallbackDir and
153  * UseDefaultFallbackDirs torrc options.)
154  * The list of fallback directories includes the list of authorities.
155  */
156 int
157 router_digest_is_fallback_dir(const char *digest)
158 {
159  return (router_get_fallback_dirserver_by_digest(digest) != NULL);
160 }
161 
162 /** Return the dir_server_t for the directory authority whose
163  * v3 identity key hashes to <b>digest</b>, or NULL if no such authority
164  * is known.
165  */
168 {
169  if (!trusted_dir_servers)
170  return NULL;
171 
173  {
174  if (tor_memeq(ds->v3_identity_digest, digest, DIGEST_LEN) &&
175  (ds->type & V3_DIRINFO))
176  return ds;
177  });
178 
179  return NULL;
180 }
181 
182 /** Mark as running every dir_server_t in <b>server_list</b>. */
183 void
185 {
186  if (server_list) {
187  SMARTLIST_FOREACH_BEGIN(server_list, dir_server_t *, dir) {
188  routerstatus_t *rs;
189  node_t *node;
190  dir->is_running = 1;
191  node = node_get_mutable_by_id(dir->digest);
192  if (node)
193  node->is_running = 1;
195  if (rs) {
196  rs->last_dir_503_at = 0;
198  }
199  } SMARTLIST_FOREACH_END(dir);
200  }
202 }
203 
204 /** Return true iff <b>digest</b> is the digest of the identity key of a
205  * trusted directory matching at least one bit of <b>type</b>. If <b>type</b>
206  * is zero (NO_DIRINFO), or ALL_DIRINFO, any authority is okay. */
207 int
209 {
210  if (!trusted_dir_servers)
211  return 0;
212  if (authdir_mode(get_options()) && router_digest_is_me(digest))
213  return 1;
215  if (tor_memeq(digest, ent->digest, DIGEST_LEN)) {
216  return (!type) || ((type & ent->type) != 0);
217  });
218  return 0;
219 }
220 
221 /** Create a directory server at <b>address</b>:<b>port</b>, with OR identity
222  * key <b>digest</b> which has DIGEST_LEN bytes. If <b>address</b> is NULL,
223  * add ourself. If <b>is_authority</b>, this is a directory authority. Return
224  * the new directory server entry on success or NULL on failure. */
225 static dir_server_t *
227  const char *nickname,
228  const tor_addr_t *addr,
229  const char *hostname,
230  uint16_t dir_port, uint16_t or_port,
231  const tor_addr_port_t *addrport_ipv6,
232  const char *digest, const char *v3_auth_digest,
233  dirinfo_type_t type,
234  double weight)
235 {
236  dir_server_t *ent;
237  uint32_t a;
238  char *hostname_ = NULL;
239 
240  tor_assert(digest);
241 
242  if (weight < 0)
243  return NULL;
244 
245  if (tor_addr_family(addr) == AF_INET)
246  a = tor_addr_to_ipv4h(addr);
247  else
248  return NULL;
249 
250  if (!hostname)
251  hostname_ = tor_addr_to_str_dup(addr);
252  else
253  hostname_ = tor_strdup(hostname);
254 
255  ent = tor_malloc_zero(sizeof(dir_server_t));
256  ent->nickname = nickname ? tor_strdup(nickname) : NULL;
257  ent->address = hostname_;
258  ent->addr = a;
259  ent->dir_port = dir_port;
260  ent->or_port = or_port;
261  ent->is_running = 1;
262  ent->is_authority = is_authority;
263  ent->type = type;
264  ent->weight = weight;
265  if (addrport_ipv6) {
266  if (tor_addr_family(&addrport_ipv6->addr) != AF_INET6) {
267  log_warn(LD_BUG, "Hey, I got a non-ipv6 addr as addrport_ipv6.");
269  } else {
270  tor_addr_copy(&ent->ipv6_addr, &addrport_ipv6->addr);
271  ent->ipv6_orport = addrport_ipv6->port;
272  }
273  } else {
275  }
276 
277  memcpy(ent->digest, digest, DIGEST_LEN);
278  if (v3_auth_digest && (type & V3_DIRINFO))
279  memcpy(ent->v3_identity_digest, v3_auth_digest, DIGEST_LEN);
280 
281  if (nickname)
282  tor_asprintf(&ent->description, "directory server \"%s\" at %s:%d",
283  nickname, hostname_, (int)dir_port);
284  else
285  tor_asprintf(&ent->description, "directory server at %s:%d",
286  hostname_, (int)dir_port);
287 
288  ent->fake_status.addr = ent->addr;
290  memcpy(ent->fake_status.identity_digest, digest, DIGEST_LEN);
291  if (nickname)
292  strlcpy(ent->fake_status.nickname, nickname,
293  sizeof(ent->fake_status.nickname));
294  else
295  ent->fake_status.nickname[0] = '\0';
296  ent->fake_status.dir_port = ent->dir_port;
297  ent->fake_status.or_port = ent->or_port;
298  ent->fake_status.ipv6_orport = ent->ipv6_orport;
299 
300  return ent;
301 }
302 
303 /** Create an authoritative directory server at
304  * <b>address</b>:<b>port</b>, with identity key <b>digest</b>. If
305  * <b>address</b> is NULL, add ourself. Return the new trusted directory
306  * server entry on success or NULL if we couldn't add it. */
307 dir_server_t *
308 trusted_dir_server_new(const char *nickname, const char *address,
309  uint16_t dir_port, uint16_t or_port,
310  const tor_addr_port_t *ipv6_addrport,
311  const char *digest, const char *v3_auth_digest,
312  dirinfo_type_t type, double weight)
313 {
314  uint32_t a;
316  char *hostname=NULL;
317  dir_server_t *result;
318 
319  if (!address) { /* The address is us; we should guess. */
321  &a, NULL, &hostname) < 0) {
322  log_warn(LD_CONFIG,
323  "Couldn't find a suitable address when adding ourself as a "
324  "trusted directory server.");
325  return NULL;
326  }
327  if (!hostname)
328  hostname = tor_dup_ip(a);
329  } else {
330  if (tor_lookup_hostname(address, &a)) {
331  log_warn(LD_CONFIG,
332  "Unable to lookup address for directory server at '%s'",
333  address);
334  return NULL;
335  }
336  hostname = tor_strdup(address);
337  }
339 
340  result = dir_server_new(1, nickname, &addr, hostname,
341  dir_port, or_port,
342  ipv6_addrport,
343  digest,
344  v3_auth_digest, type, weight);
345  tor_free(hostname);
346  return result;
347 }
348 
349 /** Return a new dir_server_t for a fallback directory server at
350  * <b>addr</b>:<b>or_port</b>/<b>dir_port</b>, with identity key digest
351  * <b>id_digest</b> */
352 dir_server_t *
354  uint16_t dir_port, uint16_t or_port,
355  const tor_addr_port_t *addrport_ipv6,
356  const char *id_digest, double weight)
357 {
358  return dir_server_new(0, NULL, addr, NULL, dir_port, or_port,
359  addrport_ipv6,
360  id_digest,
361  NULL, ALL_DIRINFO, weight);
362 }
363 
364 /** Add a directory server to the global list(s). */
365 void
367 {
368  if (!trusted_dir_servers)
372 
373  if (ent->is_authority)
375 
378 }
379 
380 #define dir_server_free(val) \
381  FREE_AND_NULL(dir_server_t, dir_server_free_, (val))
382 
383 /** Free storage held in <b>ds</b>. */
384 static void
386 {
387  if (!ds)
388  return;
389 
390  tor_free(ds->nickname);
391  tor_free(ds->description);
392  tor_free(ds->address);
393  tor_free(ds);
394 }
395 
396 /** Remove all members from the list of dir servers. */
397 void
399 {
400  if (fallback_dir_servers) {
402  dir_server_free(ent));
404  } else {
406  }
407  if (trusted_dir_servers) {
409  } else {
411  }
413 }
414 
415 void
416 dirlist_free_all(void)
417 {
419  smartlist_free(trusted_dir_servers);
420  smartlist_free(fallback_dir_servers);
422 }
void dir_server_add(dir_server_t *ent)
Definition: dirlist.c:366
routerstatus_t * router_get_mutable_consensus_status_by_id(const char *digest)
unsigned int is_running
Definition: dir_server_st.h:37
uint16_t ipv6_orport
dirinfo_type_t type
Definition: dir_server_st.h:46
Definition: node_st.h:33
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
void tor_addr_make_unspec(tor_addr_t *a)
Definition: address.c:225
void router_dir_info_changed(void)
Definition: nodelist.c:2340
int authdir_mode(const or_options_t *options)
Definition: authmode.c:25
static smartlist_t * fallback_dir_servers
Definition: dirlist.c:50
int get_n_authorities(dirinfo_type_t type)
Definition: dirlist.c:55
Header file for nodelist.c.
static dir_server_t * dir_server_new(int is_authority, const char *nickname, const tor_addr_t *addr, const char *hostname, uint16_t dir_port, uint16_t or_port, const tor_addr_port_t *addrport_ipv6, const char *digest, const char *v3_auth_digest, dirinfo_type_t type, double weight)
Definition: dirlist.c:226
Header file for directory.c.
void smartlist_add(smartlist_t *sl, void *element)
char nickname[MAX_NICKNAME_LEN+1]
tor_addr_t ipv6_addr
Definition: dir_server_st.h:27
static void dir_server_free_(dir_server_t *ds)
Definition: dirlist.c:385
Node information structure.
Header file for config.c.
int router_digest_is_fallback_dir(const char *digest)
Definition: dirlist.c:157
const or_options_t * get_options(void)
Definition: config.c:944
#define tor_assert(expr)
Definition: util_bug.h:102
static uint32_t tor_addr_to_ipv4h(const tor_addr_t *a)
Definition: address.h:152
#define tor_free(p)
Definition: malloc.h:52
dirinfo_type_t
Definition: or.h:891
smartlist_t * smartlist_new(void)
uint16_t dir_port
Definition: dir_server_st.h:29
dir_server_t * router_get_trusteddirserver_by_digest(const char *digest)
Definition: dirlist.c:112
char v3_identity_digest[DIGEST_LEN]
Definition: dir_server_st.h:34
Header file for routerset.c.
Header file for directory authority mode.
smartlist_t * router_get_trusted_dir_servers_mutable(void)
Definition: dirlist.c:71
Trusted/fallback directory server structure.
Header file for policies.c.
int router_digest_is_trusted_dir_type(const char *digest, dirinfo_type_t type)
Definition: dirlist.c:208
#define tor_addr_from_ipv4h(dest, v4addr)
Definition: address.h:287
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
uint16_t or_port
Definition: dir_server_st.h:30
#define DIGEST_LEN
Definition: digest_sizes.h:20
dir_server_t * trusted_dir_server_new(const char *nickname, const char *address, uint16_t dir_port, uint16_t or_port, const tor_addr_port_t *ipv6_addrport, const char *digest, const char *v3_auth_digest, dirinfo_type_t type, double weight)
Definition: dirlist.c:308
Master header file for Tor-specific functionality.
static smartlist_t * trusted_dir_servers
Definition: dirlist.c:47
tor_addr_t ipv6_addr
#define LOG_WARN
Definition: log.h:53
unsigned int is_running
Definition: node_st.h:62
dir_server_t * fallback_dir_server_new(const tor_addr_t *addr, uint16_t dir_port, uint16_t or_port, const tor_addr_port_t *addrport_ipv6, const char *id_digest, double weight)
Definition: dirlist.c:353
char digest[DIGEST_LEN]
Definition: dir_server_st.h:33
int resolve_my_address(int warn_severity, const or_options_t *options, uint32_t *addr_out, const char **method_out, char **hostname_out)
Definition: config.c:2812
unsigned int is_authority
Definition: dir_server_st.h:38
void router_reset_status_download_failures(void)
Definition: dirlist.c:103
int control_event_networkstatus_changed_single(const routerstatus_t *rs)
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition: address.c:1134
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Header file for router.c.
time_t last_dir_503_at
dir_server_t * router_get_fallback_dirserver_by_digest(const char *digest)
Definition: dirlist.c:133
char identity_digest[DIGEST_LEN]
uint16_t ipv6_orport
Definition: dir_server_st.h:31
uint32_t addr
Definition: dir_server_st.h:28
int router_digest_is_me(const char *digest)
Definition: router.c:1586
Definition: or.h:894
Header for resolve.c.
Header file for dirlist.c.
dir_server_t * trusteddirserver_get_by_v3_auth_digest(const char *digest)
Definition: dirlist.c:167
void mark_all_dirservers_up(smartlist_t *server_list)
Definition: dirlist.c:184
routerstatus_t fake_status
Definition: dir_server_st.h:51
void clear_dir_servers(void)
Definition: dirlist.c:398
node_t * node_get_mutable_by_id(const char *identity_digest)
Definition: nodelist.c:194
unsigned int is_authority
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:904
char * address
Definition: dir_server_st.h:24
Header file for control_events.c.
void smartlist_clear(smartlist_t *sl)
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:86
#define LD_CONFIG
Definition: log.h:68
Header file for routerlist.c.
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:179
int tor_lookup_hostname(const char *name, uint32_t *addr)
Definition: resolve.c:46
char * tor_dup_ip(uint32_t addr)
Definition: address.c:1948