Tor  0.4.5.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-2020, 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"
31 #include "core/or/policies.h"
40 #include "feature/relay/router.h"
41 #include "lib/net/resolve.h"
42 
45 
46 /** Global list of a dir_server_t object for each directory
47  * authority. */
49 /** Global list of dir_server_t objects for all directory authorities
50  * and all fallback directory servers. */
52 
53 /** Helper: From a given trusted directory entry, add the v4 or/and v6 address
54  * to the nodelist address set. */
55 static void
57 {
58  tor_assert(dir);
60 
61  /* Add IPv4 and then IPv6 if applicable. */
63  if (!tor_addr_is_null(&dir->ipv6_addr)) {
65  }
66 }
67 
68 /** Go over the trusted directory server list and add their address(es) to the
69  * nodelist address set. This is called everytime a new consensus is set. */
70 MOCK_IMPL(void,
72 {
73  if (!trusted_dir_servers) {
74  return;
75  }
76 
78  if (ent->is_authority) {
80  }
81  } SMARTLIST_FOREACH_END(ent);
82 }
83 
84 /** Return the number of directory authorities whose type matches some bit set
85  * in <b>type</b> */
86 int
88 {
89  int n = 0;
91  return 0;
93  if (ds->type & type)
94  ++n);
95  return n;
96 }
97 
98 /** Return a smartlist containing a list of dir_server_t * for all
99  * known trusted dirservers. Callers must not modify the list or its
100  * contents.
101  */
102 smartlist_t *
104 {
105  if (!trusted_dir_servers)
107 
108  return trusted_dir_servers;
109 }
110 
111 smartlist_t *
112 router_get_fallback_dir_servers_mutable(void)
113 {
116 
117  return fallback_dir_servers;
118 }
119 
120 const smartlist_t *
121 router_get_trusted_dir_servers(void)
122 {
124 }
125 
126 const smartlist_t *
127 router_get_fallback_dir_servers(void)
128 {
129  return router_get_fallback_dir_servers_mutable();
130 }
131 
132 /** Reset all internal variables used to count failed downloads of network
133  * status objects. */
134 void
136 {
138 }
139 
140 /** Return the dir_server_t for the directory authority whose identity
141  * key hashes to <b>digest</b>, or NULL if no such authority is known.
142  */
143 dir_server_t *
145 {
146  if (!trusted_dir_servers)
147  return NULL;
148 
150  {
151  if (tor_memeq(ds->digest, digest, DIGEST_LEN))
152  return ds;
153  });
154 
155  return NULL;
156 }
157 
158 /** Return the dir_server_t for the fallback dirserver whose identity
159  * key hashes to <b>digest</b>, or NULL if no such fallback is in the list of
160  * fallback_dir_servers. (fallback_dir_servers is affected by the FallbackDir
161  * and UseDefaultFallbackDirs torrc options.)
162  * The list of fallback directories includes the list of authorities.
163  */
164 dir_server_t *
166 {
168  return NULL;
169 
170  if (!digest)
171  return NULL;
172 
174  {
175  if (tor_memeq(ds->digest, digest, DIGEST_LEN))
176  return ds;
177  });
178 
179  return NULL;
180 }
181 
182 /** Return 1 if any fallback dirserver's identity key hashes to <b>digest</b>,
183  * or 0 if no such fallback is in the list of fallback_dir_servers.
184  * (fallback_dir_servers is affected by the FallbackDir and
185  * UseDefaultFallbackDirs torrc options.)
186  * The list of fallback directories includes the list of authorities.
187  */
188 int
189 router_digest_is_fallback_dir(const char *digest)
190 {
191  return (router_get_fallback_dirserver_by_digest(digest) != NULL);
192 }
193 
194 /** Return the dir_server_t for the directory authority whose
195  * v3 identity key hashes to <b>digest</b>, or NULL if no such authority
196  * is known.
197  */
200 {
201  if (!trusted_dir_servers)
202  return NULL;
203 
205  {
206  if (tor_memeq(ds->v3_identity_digest, digest, DIGEST_LEN) &&
207  (ds->type & V3_DIRINFO))
208  return ds;
209  });
210 
211  return NULL;
212 }
213 
214 /** Mark as running every dir_server_t in <b>server_list</b>. */
215 void
217 {
218  if (server_list) {
219  SMARTLIST_FOREACH_BEGIN(server_list, dir_server_t *, dir) {
220  routerstatus_t *rs;
221  node_t *node;
222  dir->is_running = 1;
223  node = node_get_mutable_by_id(dir->digest);
224  if (node)
225  node->is_running = 1;
227  if (rs) {
228  rs->last_dir_503_at = 0;
230  }
231  } SMARTLIST_FOREACH_END(dir);
232  }
234 }
235 
236 /** Return true iff <b>digest</b> is the digest of the identity key of a
237  * trusted directory matching at least one bit of <b>type</b>. If <b>type</b>
238  * is zero (NO_DIRINFO), or ALL_DIRINFO, any authority is okay. */
239 int
241 {
242  if (!trusted_dir_servers)
243  return 0;
244  if (authdir_mode(get_options()) && router_digest_is_me(digest))
245  return 1;
247  if (tor_memeq(digest, ent->digest, DIGEST_LEN)) {
248  return (!type) || ((type & ent->type) != 0);
249  });
250  return 0;
251 }
252 
253 /** Create a directory server at <b>address</b>:<b>port</b>, with OR identity
254  * key <b>digest</b> which has DIGEST_LEN bytes. If <b>address</b> is NULL,
255  * add ourself. If <b>is_authority</b>, this is a directory authority. Return
256  * the new directory server entry on success or NULL on failure. */
257 static dir_server_t *
259  const char *nickname,
260  const tor_addr_t *addr,
261  const char *hostname,
262  uint16_t dir_port, uint16_t or_port,
263  const tor_addr_port_t *addrport_ipv6,
264  const char *digest, const char *v3_auth_digest,
265  dirinfo_type_t type,
266  double weight)
267 {
268  dir_server_t *ent;
269  uint32_t a;
270  char *hostname_ = NULL;
271 
272  tor_assert(digest);
273 
274  if (weight < 0)
275  return NULL;
276 
277  if (tor_addr_family(addr) == AF_INET)
278  a = tor_addr_to_ipv4h(addr);
279  else
280  return NULL;
281 
282  if (!hostname)
283  hostname_ = tor_addr_to_str_dup(addr);
284  else
285  hostname_ = tor_strdup(hostname);
286 
287  ent = tor_malloc_zero(sizeof(dir_server_t));
288  ent->nickname = nickname ? tor_strdup(nickname) : NULL;
289  ent->address = hostname_;
290  ent->addr = a;
291  ent->dir_port = dir_port;
292  ent->or_port = or_port;
293  ent->is_running = 1;
294  ent->is_authority = is_authority;
295  ent->type = type;
296  ent->weight = weight;
297  if (addrport_ipv6 && tor_addr_port_is_valid_ap(addrport_ipv6, 0)) {
298  if (tor_addr_family(&addrport_ipv6->addr) != AF_INET6) {
299  log_warn(LD_BUG, "Hey, I got a non-ipv6 addr as addrport_ipv6.");
301  } else {
302  tor_addr_copy(&ent->ipv6_addr, &addrport_ipv6->addr);
303  ent->ipv6_orport = addrport_ipv6->port;
304  }
305  } else {
307  }
308 
309  memcpy(ent->digest, digest, DIGEST_LEN);
310  if (v3_auth_digest && (type & V3_DIRINFO))
311  memcpy(ent->v3_identity_digest, v3_auth_digest, DIGEST_LEN);
312 
313  if (nickname)
314  tor_asprintf(&ent->description, "directory server \"%s\" at %s:%d",
315  nickname, hostname_, (int)dir_port);
316  else
317  tor_asprintf(&ent->description, "directory server at %s:%d",
318  hostname_, (int)dir_port);
319 
320  ent->fake_status.addr = ent->addr;
322  memcpy(ent->fake_status.identity_digest, digest, DIGEST_LEN);
323  if (nickname)
324  strlcpy(ent->fake_status.nickname, nickname,
325  sizeof(ent->fake_status.nickname));
326  else
327  ent->fake_status.nickname[0] = '\0';
328  ent->fake_status.dir_port = ent->dir_port;
329  ent->fake_status.or_port = ent->or_port;
330  ent->fake_status.ipv6_orport = ent->ipv6_orport;
331 
332  return ent;
333 }
334 
335 /** Create an authoritative directory server at
336  * <b>address</b>:<b>port</b>, with identity key <b>digest</b>. If
337  * <b>address</b> is NULL, add ourself. Return the new trusted directory
338  * server entry on success or NULL if we couldn't add it. */
339 dir_server_t *
340 trusted_dir_server_new(const char *nickname, const char *address,
341  uint16_t dir_port, uint16_t or_port,
342  const tor_addr_port_t *ipv6_addrport,
343  const char *digest, const char *v3_auth_digest,
344  dirinfo_type_t type, double weight)
345 {
347  char *hostname=NULL;
348  dir_server_t *result;
349 
350  if (!address) { /* The address is us; we should guess. */
351  if (!find_my_address(get_options(), AF_INET, LOG_WARN, &addr,
352  NULL, &hostname)) {
353  log_warn(LD_CONFIG,
354  "Couldn't find a suitable address when adding ourself as a "
355  "trusted directory server.");
356  return NULL;
357  }
358  if (!hostname)
359  hostname = tor_addr_to_str_dup(&addr);
360 
361  if (!hostname)
362  return NULL;
363  } else {
364  uint32_t a;
365  if (tor_lookup_hostname(address, &a)) {
366  log_warn(LD_CONFIG,
367  "Unable to lookup address for directory server at '%s'",
368  address);
369  return NULL;
370  }
371  hostname = tor_strdup(address);
373  }
374 
375  result = dir_server_new(1, nickname, &addr, hostname,
376  dir_port, or_port,
377  ipv6_addrport,
378  digest,
379  v3_auth_digest, type, weight);
380  tor_free(hostname);
381  return result;
382 }
383 
384 /** Return a new dir_server_t for a fallback directory server at
385  * <b>addr</b>:<b>or_port</b>/<b>dir_port</b>, with identity key digest
386  * <b>id_digest</b> */
387 dir_server_t *
389  uint16_t dir_port, uint16_t or_port,
390  const tor_addr_port_t *addrport_ipv6,
391  const char *id_digest, double weight)
392 {
393  return dir_server_new(0, NULL, addr, NULL, dir_port, or_port,
394  addrport_ipv6,
395  id_digest,
396  NULL, ALL_DIRINFO, weight);
397 }
398 
399 /** Add a directory server to the global list(s). */
400 void
402 {
403  if (!trusted_dir_servers)
407 
408  if (ent->is_authority)
410 
413 }
414 
415 #define dir_server_free(val) \
416  FREE_AND_NULL(dir_server_t, dir_server_free_, (val))
417 
418 /** Free storage held in <b>ds</b>. */
419 static void
421 {
422  if (!ds)
423  return;
424 
425  tor_free(ds->nickname);
426  tor_free(ds->description);
427  tor_free(ds->address);
428  tor_free(ds);
429 }
430 
431 /** Remove all members from the list of dir servers. */
432 void
434 {
435  if (fallback_dir_servers) {
437  dir_server_free(ent));
439  } else {
441  }
442  if (trusted_dir_servers) {
444  } else {
446  }
448 }
449 
450 void
451 dirlist_free_all(void)
452 {
454  smartlist_free(trusted_dir_servers);
455  smartlist_free(fallback_dir_servers);
457 }
tor_free
#define tor_free(p)
Definition: malloc.h:52
dir_server_st.h
Trusted/fallback directory server structure.
routerstatus_t::is_authority
unsigned int is_authority
Definition: routerstatus_st.h:37
tor_addr_family
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:187
tor_addr_t
Definition: address.h:68
MOCK_IMPL
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
tor_assert
#define tor_assert(expr)
Definition: util_bug.h:102
LD_BUG
#define LD_BUG
Definition: log.h:86
router.h
Header file for router.c.
fallback_dir_servers
static smartlist_t * fallback_dir_servers
Definition: dirlist.c:51
router_digest_is_me
int router_digest_is_me(const char *digest)
Definition: router.c:1703
dir_server_t::v3_identity_digest
char v3_identity_digest[DIGEST_LEN]
Definition: dir_server_st.h:34
smartlist_add
void smartlist_add(smartlist_t *sl, void *element)
Definition: smartlist_core.c:117
router_digest_is_fallback_dir
int router_digest_is_fallback_dir(const char *digest)
Definition: dirlist.c:189
nodelist_add_addr6_to_address_set
void nodelist_add_addr6_to_address_set(const tor_addr_t *addr)
Definition: nodelist.c:486
routerstatus_t
Definition: routerstatus_st.h:19
routerstatus_t::nickname
char nickname[MAX_NICKNAME_LEN+1]
Definition: routerstatus_st.h:25
smartlist_new
smartlist_t * smartlist_new(void)
Definition: smartlist_core.c:26
tor_addr_make_unspec
void tor_addr_make_unspec(tor_addr_t *a)
Definition: address.c:225
SMARTLIST_FOREACH
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Definition: smartlist_foreach.h:112
dir_server_t::or_port
uint16_t or_port
Definition: dir_server_st.h:30
router_get_trusteddirserver_by_digest
dir_server_t * router_get_trusteddirserver_by_digest(const char *digest)
Definition: dirlist.c:144
networkstatus.h
Header file for networkstatus.c.
tor_memeq
int tor_memeq(const void *a, const void *b, size_t sz)
Definition: di_ops.c:107
dirlist_add_trusted_dir_addresses
void dirlist_add_trusted_dir_addresses(void)
Definition: dirlist.c:71
dirlist.h
Header file for dirlist.c.
get_n_authorities
int get_n_authorities(dirinfo_type_t type)
Definition: dirlist.c:87
dir_server_new
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:258
DIGEST_LEN
#define DIGEST_LEN
Definition: digest_sizes.h:20
dir_server_free_
static void dir_server_free_(dir_server_t *ds)
Definition: dirlist.c:420
node_get_mutable_by_id
node_t * node_get_mutable_by_id(const char *identity_digest)
Definition: nodelist.c:194
resolve.h
Header for resolve.c.
dir_server_t::dir_port
uint16_t dir_port
Definition: dir_server_st.h:29
tor_addr_from_ipv4h
#define tor_addr_from_ipv4h(dest, v4addr)
Definition: address.h:326
tor_addr_port_t
Definition: address.h:80
tor_addr_to_ipv4h
static uint32_t tor_addr_to_ipv4h(const tor_addr_t *a)
Definition: address.h:160
node_t
Definition: node_st.h:34
dir_server_t::is_authority
unsigned int is_authority
Definition: dir_server_st.h:38
routerstatus_t::addr
uint32_t addr
Definition: routerstatus_st.h:32
fallback_dir_server_new
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:388
directory.h
Header file for directory.c.
routerstatus_t::or_port
uint16_t or_port
Definition: routerstatus_st.h:33
trusted_dir_servers
static smartlist_t * trusted_dir_servers
Definition: dirlist.c:48
router_digest_is_trusted_dir_type
int router_digest_is_trusted_dir_type(const char *digest, dirinfo_type_t type)
Definition: dirlist.c:240
authmode.h
Header file for directory authority mode.
nodelist.h
Header file for nodelist.c.
routerlist.h
Header file for routerlist.c.
dir_server_t::fake_status
routerstatus_t fake_status
Definition: dir_server_st.h:51
routerset.h
Header file for routerset.c.
router_get_trusted_dir_servers_mutable
smartlist_t * router_get_trusted_dir_servers_mutable(void)
Definition: dirlist.c:103
LD_CONFIG
#define LD_CONFIG
Definition: log.h:68
dir_server_t
Definition: dir_server_st.h:21
tor_addr_to_str_dup
char * tor_addr_to_str_dup(const tor_addr_t *addr)
Definition: address.c:1155
dir_server_t::digest
char digest[DIGEST_LEN]
Definition: dir_server_st.h:33
trusted_dir_server_new
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:340
find_my_address
bool find_my_address(const or_options_t *options, int family, int warn_severity, tor_addr_t *addr_out, const char **method_out, char **hostname_out)
Attempt to find our IP address that can be used as our external reachable address.
Definition: resolve_addr.c:582
control_events.h
Header file for control_events.c.
get_options
const or_options_t * get_options(void)
Definition: config.c:926
routerstatus_t::last_dir_503_at
time_t last_dir_503_at
Definition: routerstatus_st.h:84
smartlist_clear
void smartlist_clear(smartlist_t *sl)
Definition: smartlist_core.c:50
resolve_addr.h
Header file for resolve_addr.c.
dir_server_t::is_running
unsigned int is_running
Definition: dir_server_st.h:37
trusteddirserver_get_by_v3_auth_digest
dir_server_t * trusteddirserver_get_by_v3_auth_digest(const char *digest)
Definition: dirlist.c:199
router_get_fallback_dirserver_by_digest
dir_server_t * router_get_fallback_dirserver_by_digest(const char *digest)
Definition: dirlist.c:165
V3_DIRINFO
@ V3_DIRINFO
Definition: or.h:908
nodelist_add_addr4_to_address_set
void nodelist_add_addr4_to_address_set(const uint32_t addr)
Definition: nodelist.c:476
router_reset_status_download_failures
void router_reset_status_download_failures(void)
Definition: dirlist.c:135
tor_addr_is_null
int tor_addr_is_null(const tor_addr_t *addr)
Definition: address.c:771
dir_server_t::addr
uint32_t addr
Definition: dir_server_st.h:28
clear_dir_servers
void clear_dir_servers(void)
Definition: dirlist.c:433
dirinfo_type_t
dirinfo_type_t
Definition: or.h:905
dir_server_t::type
dirinfo_type_t type
Definition: dir_server_st.h:46
tor_asprintf
int tor_asprintf(char **strp, const char *fmt,...)
Definition: printf.c:75
SMARTLIST_FOREACH_BEGIN
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
Definition: smartlist_foreach.h:78
LOG_WARN
#define LOG_WARN
Definition: log.h:53
node_st.h
Node information structure.
routerstatus_t::ipv6_addr
tor_addr_t ipv6_addr
Definition: routerstatus_st.h:35
add_trusted_dir_to_nodelist_addr_set
static void add_trusted_dir_to_nodelist_addr_set(const dir_server_t *dir)
Definition: dirlist.c:56
policies.h
Header file for policies.c.
dir_server_t::ipv6_orport
uint16_t ipv6_orport
Definition: dir_server_st.h:31
config.h
Header file for config.c.
routerstatus_t::dir_port
uint16_t dir_port
Definition: routerstatus_st.h:34
tor_lookup_hostname
int tor_lookup_hostname(const char *name, uint32_t *addr)
Definition: resolve.c:46
routerstatus_t::ipv6_orport
uint16_t ipv6_orport
Definition: routerstatus_st.h:36
router_get_mutable_consensus_status_by_id
routerstatus_t * router_get_mutable_consensus_status_by_id(const char *digest)
Definition: networkstatus.c:830
mark_all_dirservers_up
void mark_all_dirservers_up(smartlist_t *server_list)
Definition: dirlist.c:216
control_event_networkstatus_changed_single
int control_event_networkstatus_changed_single(const routerstatus_t *rs)
Definition: control_events.c:1642
routerstatus_t::identity_digest
char identity_digest[DIGEST_LEN]
Definition: routerstatus_st.h:27
router_dir_info_changed
void router_dir_info_changed(void)
Definition: nodelist.c:2417
node_t::is_running
unsigned int is_running
Definition: node_st.h:63
smartlist_t
Definition: smartlist_core.h:26
authdir_mode
int authdir_mode(const or_options_t *options)
Definition: authmode.c:25
dir_server_add
void dir_server_add(dir_server_t *ent)
Definition: dirlist.c:401
tor_addr_copy
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:924
dir_server_t::ipv6_addr
tor_addr_t ipv6_addr
Definition: dir_server_st.h:27
dir_server_t::address
char * address
Definition: dir_server_st.h:24
or.h
Master header file for Tor-specific functionality.