tor  0.4.2.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 
27 #include "core/or/or.h"
28 
29 #include "app/config/config.h"
30 #include "core/or/policies.h"
38 #include "feature/nodelist/routerset.h"
39 #include "feature/relay/router.h"
40 #include "lib/net/resolve.h"
41 
42 #include "feature/dirclient/dir_server_st.h"
43 #include "feature/nodelist/node_st.h"
44 
51 
54 int
56 {
57  int n = 0;
59  return 0;
61  if (ds->type & type)
62  ++n);
63  return n;
64 }
65 
72 {
74  trusted_dir_servers = smartlist_new();
75 
76  return trusted_dir_servers;
77 }
78 
80 router_get_fallback_dir_servers_mutable(void)
81 {
83  fallback_dir_servers = smartlist_new();
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 
102 void
104 {
106 }
107 
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 
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 
156 int
157 router_digest_is_fallback_dir(const char *digest)
158 {
159  return (router_get_fallback_dirserver_by_digest(digest) != NULL);
160 }
161 
167 trusteddirserver_get_by_v3_auth_digest, (const char *digest))
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 
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 
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 
225 static dir_server_t *
226 dir_server_new(int is_authority,
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 
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;
315  tor_addr_t addr;
316  char *hostname=NULL;
317  dir_server_t *result;
318 
319  if (!address) { /* The address is us; we should guess. */
320  if (resolve_my_address(LOG_WARN, get_options(),
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  }
338  tor_addr_from_ipv4h(&addr, a);
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 
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 
365 void
367 {
368  if (!trusted_dir_servers)
369  trusted_dir_servers = smartlist_new();
371  fallback_dir_servers = smartlist_new();
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 
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 
397 void
399 {
400  if (fallback_dir_servers) {
402  dir_server_free(ent));
404  } else {
405  fallback_dir_servers = smartlist_new();
406  }
407  if (trusted_dir_servers) {
409  } else {
410  trusted_dir_servers = smartlist_new();
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)
Definition: node_st.h:28
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
void tor_addr_make_unspec(tor_addr_t *a)
Definition: address.c:225
void router_dir_info_changed(void)
Definition: nodelist.c:2330
int authdir_mode(const or_options_t *options)
Definition: authmode.c:25
static smartlist_t * fallback_dir_servers
Definition: dirlist.c:50
char identity_digest[DIGEST_LEN]
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)
uint16_t dir_port
Definition: dir_server_st.h:24
static void dir_server_free_(dir_server_t *ds)
Definition: dirlist.c:385
char digest[DIGEST_LEN]
Definition: dir_server_st.h:28
Header file for config.c.
int router_digest_is_fallback_dir(const char *digest)
Definition: dirlist.c:157
time_t last_dir_503_at
static uint32_t tor_addr_to_ipv4h(const tor_addr_t *a)
Definition: address.h:152
#define tor_free(p)
Definition: malloc.h:52
char * address
Definition: dir_server_st.h:19
dirinfo_type_t
Definition: or.h:887
unsigned int is_running
Definition: dir_server_st.h:32
dir_server_t * router_get_trusteddirserver_by_digest(const char *digest)
Definition: dirlist.c:112
Header file for directory authority mode.
smartlist_t * router_get_trusted_dir_servers_mutable(void)
Definition: dirlist.c:71
Header file for policies.c.
MOCK_IMPL(dir_server_t *, trusteddirserver_get_by_v3_auth_digest,(const char *digest))
Definition: dirlist.c:166
tor_assert(buffer)
unsigned int is_running
Definition: node_st.h:57
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 ipv6_orport
Definition: dir_server_st.h:26
#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.
routerstatus_t fake_status
Definition: dir_server_st.h:46
static smartlist_t * trusted_dir_servers
Definition: dirlist.c:47
tor_addr_t ipv6_addr
#define LOG_WARN
Definition: log.h:51
char nickname[MAX_NICKNAME_LEN+1]
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
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:2782
dirinfo_type_t type
Definition: dir_server_st.h:41
uint16_t or_port
Definition: dir_server_st.h:25
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
tor_addr_t ipv6_addr
Definition: dir_server_st.h:22
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Header file for router.c.
dir_server_t * router_get_fallback_dirserver_by_digest(const char *digest)
Definition: dirlist.c:133
uint32_t addr
Definition: dir_server_st.h:23
int router_digest_is_me(const char *digest)
Definition: router.c:1592
Definition: or.h:890
char v3_identity_digest[DIGEST_LEN]
Definition: dir_server_st.h:29
Header for resolve.c.
Header file for dirlist.c.
void mark_all_dirservers_up(smartlist_t *server_list)
Definition: dirlist.c:184
uint16_t ipv6_orport
void clear_dir_servers(void)
Definition: dirlist.c:398
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
Definition: address.c:904
Header file for control_events.c.
void smartlist_clear(smartlist_t *sl)
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:84
#define LD_CONFIG
Definition: log.h:66
unsigned int is_authority
Definition: dir_server_st.h:33
Header file for routerlist.c.
static sa_family_t tor_addr_family(const tor_addr_t *a)
Definition: address.h:179
char * tor_dup_ip(uint32_t addr)
Definition: address.c:1948