1
/* Copyright 2001-2003 Roger Dingledine, Matej Pfajfar. */
2
/* See LICENSE for licensing information */
3
/* $Id: routerlist.c,v 1.84 2004/06/02 20:00:57 nickm Exp $ */
11
* maintain and access the global list of routerinfos for known
15
/****************************************************************************/
17
extern or_options_t options; /**< command-line and config-file options */
19
/* ********************************************************************** */
21
/* static function prototypes */
22
static routerinfo_t *router_pick_directory_server_impl(void);
23
static int router_resolve_routerlist(routerlist_t *dir);
25
/****************************************************************************/
28
* Functions to manage and access our list of known routers. (Note:
29
* dirservers maintain a separate, independent list of known router
33
/** Global list of all of the routers that we, as an OR or OP, know about. */
34
static routerlist_t *routerlist = NULL;
36
extern int has_fetched_directory; /**< from main.c */
38
/** Try to find a running dirserver. If there are no running dirservers
39
* in our routerlist, reload the routerlist and try again. */
40
routerinfo_t *router_pick_directory_server(void) {
43
choice = router_pick_directory_server_impl();
45
log_fn(LOG_WARN,"No dirservers known. Reloading and trying again.");
46
has_fetched_directory=0; /* reset it */
47
routerlist_clear_trusted_directories();
48
if(options.RouterFile) {
49
if(router_load_routerlist_from_file(options.RouterFile, 1) < 0)
52
if(config_assign_default_dirservers() < 0)
55
/* give it another try */
56
choice = router_pick_directory_server_impl();
61
/** Pick a random running router with a positive dir_port from our
63
static routerinfo_t *router_pick_directory_server_impl(void) {
65
routerinfo_t *router, *dirserver=NULL;
71
/* Find all the running dirservers we know about. */
72
sl = smartlist_create();
73
for(i=0;i< smartlist_len(routerlist->routers); i++) {
74
router = smartlist_get(routerlist->routers, i);
75
if(router->dir_port > 0 && router->is_running && router->is_trusted_dir)
76
smartlist_add(sl, router);
79
router = smartlist_choose(sl);
84
log_fn(LOG_INFO,"No dirservers are reachable. Trying them all again.");
86
/* No running dir servers found? go through and mark them all as up,
87
* so we cycle through the list again. */
88
for(i=0; i < smartlist_len(routerlist->routers); i++) {
89
router = smartlist_get(routerlist->routers, i);
90
if(router->dir_port > 0 && router->is_trusted_dir) {
91
router->is_running = 1;
96
log_fn(LOG_WARN,"No dirservers in directory! Returning NULL.");
100
/** Given a comma-and-whitespace separated list of nicknames, see which
101
* nicknames in <b>list</b> name routers in our routerlist that are
102
* currently running. Add the routerinfos for those routers to <b>sl</b>.
104
void add_nickname_list_to_smartlist(smartlist_t *sl, const char *list) {
105
const char *start,*end;
106
char nick[MAX_NICKNAME_LEN+1];
107
routerinfo_t *router;
112
while(isspace((int)*list) || *list==',') list++;
116
end=start; while(*end && !isspace((int)*end) && *end != ',') end++;
117
memcpy(nick,start,end-start);
118
nick[end-start] = 0; /* null terminate it */
119
router = router_get_by_nickname(nick);
121
if (router->is_running)
122
smartlist_add(sl,router);
124
log_fn(LOG_INFO,"Nickname list includes '%s' which is known but down.",nick);
126
log_fn(has_fetched_directory ? LOG_WARN : LOG_INFO,
127
"Nickname list includes '%s' which isn't a known router.",nick);
128
while(isspace((int)*end) || *end==',') end++;
133
/** Add every router from our routerlist that is currently running to
136
void router_add_running_routers_to_smartlist(smartlist_t *sl) {
137
routerinfo_t *router;
143
for(i=0;i<smartlist_len(routerlist->routers);i++) {
144
router = smartlist_get(routerlist->routers, i);
145
if(router->is_running &&
147
connection_twin_get_by_addr_port(router->addr, router->or_port) ))
148
smartlist_add(sl, router);
152
/** Return a random running router from the routerlist. If any node
153
* named in <b>preferred</b> is available, pick one of those. Never pick a
154
* node named in <b>excluded</b>, or whose routerinfo is in
155
* <b>excludedsmartlist</b>, even if they are the only nodes available.
157
routerinfo_t *router_choose_random_node(char *preferred, char *excluded,
158
smartlist_t *excludedsmartlist)
160
smartlist_t *sl, *excludednodes;
161
routerinfo_t *choice;
163
excludednodes = smartlist_create();
164
add_nickname_list_to_smartlist(excludednodes,excluded);
166
/* try the nodes in RendNodes first */
167
sl = smartlist_create();
168
add_nickname_list_to_smartlist(sl,preferred);
169
smartlist_subtract(sl,excludednodes);
170
if(excludedsmartlist)
171
smartlist_subtract(sl,excludedsmartlist);
172
choice = smartlist_choose(sl);
175
sl = smartlist_create();
176
router_add_running_routers_to_smartlist(sl);
177
smartlist_subtract(sl,excludednodes);
178
if(excludedsmartlist)
179
smartlist_subtract(sl,excludedsmartlist);
180
choice = smartlist_choose(sl);
183
smartlist_free(excludednodes);
185
log_fn(LOG_WARN,"No available nodes when trying to choose node. Failing.");
189
/** Return the router in our routerlist whose address is <b>addr</b> and
190
* whose OR port is <b>port</b>. Return NULL if no such router is known.
192
routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
194
routerinfo_t *router;
196
tor_assert(routerlist);
198
for(i=0;i<smartlist_len(routerlist->routers);i++) {
199
router = smartlist_get(routerlist->routers, i);
200
if ((router->addr == addr) && (router->or_port == port))
206
/** Return the router in our routerlist whose nickname is <b>nickname</b>
207
* (case insensitive). Return NULL if no such router is known.
209
routerinfo_t *router_get_by_nickname(char *nickname)
212
routerinfo_t *router;
214
tor_assert(nickname);
218
for(i=0;i<smartlist_len(routerlist->routers);i++) {
219
router = smartlist_get(routerlist->routers, i);
220
if (0 == strcasecmp(router->nickname, nickname))
227
/** Set *<b>prouterlist</b> to the current list of all known routers. */
228
void router_get_routerlist(routerlist_t **prouterlist) {
229
*prouterlist = routerlist;
232
/** Free all storage held by <b>router</b>. */
233
void routerinfo_free(routerinfo_t *router)
238
tor_free(router->address);
239
tor_free(router->nickname);
240
tor_free(router->platform);
241
if (router->onion_pkey)
242
crypto_free_pk_env(router->onion_pkey);
243
if (router->identity_pkey)
244
crypto_free_pk_env(router->identity_pkey);
245
exit_policy_free(router->exit_policy);
249
/** Allocate a fresh copy of <b>router</b> */
250
routerinfo_t *routerinfo_copy(const routerinfo_t *router)
253
struct exit_policy_t **e, *tmp;
255
r = tor_malloc(sizeof(routerinfo_t));
256
memcpy(r, router, sizeof(routerinfo_t));
258
r->address = tor_strdup(r->address);
259
r->nickname = tor_strdup(r->nickname);
260
r->platform = tor_strdup(r->platform);
262
r->onion_pkey = crypto_pk_dup_key(r->onion_pkey);
263
if (r->identity_pkey)
264
r->identity_pkey = crypto_pk_dup_key(r->identity_pkey);
267
tmp = tor_malloc(sizeof(struct exit_policy_t));
268
memcpy(tmp,*e,sizeof(struct exit_policy_t));
270
(*e)->string = tor_strdup((*e)->string);
276
/** Free all storage held by a routerlist <b>rl</b> */
277
void routerlist_free(routerlist_t *rl)
279
SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
281
smartlist_free(rl->routers);
282
tor_free(rl->software_versions);
286
/** Mark the router named <b>nickname</b> as non-running in our routerlist. */
287
void router_mark_as_down(char *nickname) {
288
routerinfo_t *router;
289
tor_assert(nickname);
290
router = router_get_by_nickname(nickname);
291
if(!router) /* we don't seem to know about him in the first place */
293
log_fn(LOG_DEBUG,"Marking %s as down.",router->nickname);
294
router->is_running = 0;
297
/** Add <b>router</b> to the routerlist, if we don't already have it. Replace
298
* older entries (if any) with the same name. Note: Callers should not hold
299
* their pointers to <b>router</b> after invoking this function; <b>router</b>
300
* will either be inserted into the routerlist or freed. Returns 0 if the
301
* router was added; -1 if it was not.
303
int router_add_to_routerlist(routerinfo_t *router) {
306
/* If we have a router with this name, and the identity key is the same,
307
* choose the newer one. If the identity key has changed, drop the router.
309
for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
310
r = smartlist_get(routerlist->routers, i);
311
if (!strcasecmp(router->nickname, r->nickname)) {
312
if (!crypto_pk_cmp_keys(router->identity_pkey, r->identity_pkey)) {
313
if (router->published_on > r->published_on) {
314
log_fn(LOG_DEBUG, "Replacing entry for router '%s'",
316
/* Remember whether we trust this router as a dirserver. */
317
if (r->is_trusted_dir)
318
router->is_trusted_dir = 1;
319
/* If the address hasn't changed; no need to re-resolve. */
320
if (!strcasecmp(r->address, router->address))
321
router->addr = r->addr;
323
smartlist_set(routerlist->routers, i, router);
326
log_fn(LOG_DEBUG, "Skipping old entry for router '%s'",
328
/* If we now trust 'router', then we trust the one in the routerlist
330
if (router->is_trusted_dir)
331
r->is_trusted_dir = 1;
332
/* Update the is_running status to whatever we were told. */
333
r->is_running = router->is_running;
334
routerinfo_free(router);
338
log_fn(LOG_WARN, "Identity key mismatch for router '%s'",
340
routerinfo_free(router);
345
/* We haven't seen a router with this name before. Add it to the end of
347
smartlist_add(routerlist->routers, router);
351
/** Remove any routers from the routerlist that are more than ROUTER_MAX_AGE
354
* (This function is just like dirserv_remove_old_servers. One day we should
358
routerlist_remove_old_routers(void)
362
routerinfo_t *router;
366
cutoff = time(NULL) - ROUTER_MAX_AGE;
367
for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
368
router = smartlist_get(routerlist->routers, i);
369
if (router->published_on < cutoff &&
371
/* Too old. Remove it. But never remove dirservers! */
372
log_fn(LOG_INFO,"Forgetting obsolete routerinfo for node %s.", router->nickname);
373
routerinfo_free(router);
374
smartlist_del(routerlist->routers, i--);
380
* Code to parse router descriptors and directories.
383
/** Update the current router list with the one stored in
384
* <b>routerfile</b>. If <b>trusted</b> is true, then we'll use
385
* directory servers from the file. */
386
int router_load_routerlist_from_file(char *routerfile, int trusted)
390
string = read_file_to_str(routerfile);
392
log_fn(LOG_WARN,"Failed to load routerfile %s.",routerfile);
396
if(router_load_routerlist_from_string(string, trusted) < 0) {
397
log_fn(LOG_WARN,"The routerfile itself was corrupt.");
401
/* dump_onion_keys(LOG_NOTICE); */
407
/** Mark all directories in the routerlist as nontrusted. */
408
void routerlist_clear_trusted_directories(void)
410
if (!routerlist) return;
411
SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
412
r->is_trusted_dir = 0);
415
/** Helper function: read routerinfo elements from s, and throw out the
416
* ones that don't parse and resolve. Add all remaining elements to the
417
* routerlist. If <b>trusted</b> is true, then we'll use
418
* directory servers from the string
420
int router_load_routerlist_from_string(const char *s, int trusted)
422
routerlist_t *new_list=NULL;
424
if (router_parse_list_from_string(&s, &new_list, -1, NULL)) {
425
log(LOG_WARN, "Error parsing router file");
429
SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
430
if (r->dir_port) r->is_trusted_dir = 1);
433
SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
434
router_add_to_routerlist(r));
435
smartlist_clear(new_list->routers);
436
routerlist_free(new_list);
438
routerlist = new_list;
440
if (router_resolve_routerlist(routerlist)) {
441
log(LOG_WARN, "Error resolving routerlist");
444
/* dump_onion_keys(LOG_NOTICE); */
449
/** Add to the current routerlist each router stored in the
450
* signed directory <b>s</b>. If pkey is provided, check the signature against
451
* pkey; else check against the pkey of the signing directory server. */
452
int router_load_routerlist_from_directory(const char *s,
453
crypto_pk_env_t *pkey)
455
routerlist_t *new_list = NULL;
456
check_software_version_against_directory(s, options.IgnoreVersion);
457
if (router_parse_routerlist_from_directory(s, &new_list, pkey)) {
458
log_fn(LOG_WARN, "Couldn't parse directory.");
462
SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
463
router_add_to_routerlist(r));
464
smartlist_clear(new_list->routers);
465
routerlist->published_on = new_list->published_on;
466
tor_free(routerlist->software_versions);
467
routerlist->software_versions = new_list->software_versions;
468
new_list->software_versions = NULL;
469
routerlist_free(new_list);
471
routerlist = new_list;
473
if (router_resolve_routerlist(routerlist)) {
474
log_fn(LOG_WARN, "Error resolving routerlist");
480
/** Helper function: resolve the hostname for <b>router</b>. */
482
router_resolve(routerinfo_t *router)
484
if (tor_lookup_hostname(router->address, &router->addr) != 0) {
485
log_fn(LOG_WARN,"Could not get address for router %s (%s).",
486
router->address, router->nickname);
489
router->addr = ntohl(router->addr); /* get it back into host order */
494
/** Helper function: resolve every router in rl, and ensure that our own
495
* routerinfo is at the front.
498
router_resolve_routerlist(routerlist_t *rl)
506
if ((r = router_get_my_routerinfo())) {
507
smartlist_insert(rl->routers, 0, routerinfo_copy(r));
511
for ( ; i < smartlist_len(rl->routers); ++i) {
513
r = smartlist_get(rl->routers,i);
514
if (router_is_me(r)) {
516
} else if (r->addr) {
517
/* already resolved. */
518
} else if (router_resolve(r)) {
519
log_fn(LOG_WARN, "Couldn't resolve router %s; not using", r->address);
524
smartlist_del_keeporder(rl->routers, i--);
531
/** Decide whether a given addr:port is definitely accepted, definitely
532
* rejected, or neither by a given exit policy. If <b>addr</b> is 0, we
533
* don't know the IP of the target address.
535
* Returns -1 for "rejected", 0 for "accepted", 1 for "maybe" (since IP is
538
int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
539
struct exit_policy_t *policy)
541
int maybe_reject = 0;
542
int maybe_accept = 0;
546
struct exit_policy_t *tmpe;
548
for(tmpe=policy; tmpe; tmpe=tmpe->next) {
549
// log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
552
/* Address is unknown. */
553
if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
554
/* The port definitely matches. */
555
if (tmpe->msk == 0) {
561
/* The port maybe matches. */
565
/* Address is known */
566
if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
567
if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
568
/* Exact match for the policy */
576
if (tmpe->policy_type == EXIT_POLICY_REJECT)
582
in.s_addr = htonl(addr);
583
log_fn(LOG_DEBUG,"Address %s:%d matches exit policy '%s'",
584
inet_ntoa(in), port, tmpe->string);
585
if(tmpe->policy_type == EXIT_POLICY_ACCEPT) {
586
/* If we already hit a clause that might trigger a 'reject', than we
587
* can't be sure of this certain 'accept'.*/
588
return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
590
return maybe_accept ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_REJECTED;
594
/* accept all by default. */
595
return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
598
/** Return 1 if all running routers will reject addr:port, return 0 if
599
* any might accept it. */
600
int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port) {
602
routerinfo_t *router;
604
for (i=0;i<smartlist_len(routerlist->routers);i++) {
605
router = smartlist_get(routerlist->routers, i);
606
if (router->is_running && router_compare_addr_to_exit_policy(
607
addr, port, router->exit_policy) != ADDR_POLICY_REJECTED)
608
return 0; /* this one could be ok. good enough. */
610
return 1; /* all will reject. */
613
/** Return true iff <b>router</b> does not permit exit streams.
615
int router_exit_policy_rejects_all(routerinfo_t *router) {
616
return router_compare_addr_to_exit_policy(0, 0, router->exit_policy)
617
== ADDR_POLICY_REJECTED;