~ubuntu-branches/ubuntu/hoary/tor/hoary

« back to all changes in this revision

Viewing changes to src/or/routerlist.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Palfrader
  • Date: 2004-06-07 21:46:08 UTC
  • Revision ID: james.westby@ubuntu.com-20040607214608-do51p01di99xdmjr
Tags: upstream-0.0.7
ImportĀ upstreamĀ versionĀ 0.0.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 $ */
 
4
 
 
5
#include "or.h"
 
6
 
 
7
/**
 
8
 * \file routerlist.c
 
9
 *
 
10
 * \brief Code to
 
11
 * maintain and access the global list of routerinfos for known
 
12
 * servers.
 
13
 **/
 
14
 
 
15
/****************************************************************************/
 
16
 
 
17
extern or_options_t options; /**< command-line and config-file options */
 
18
 
 
19
/* ********************************************************************** */
 
20
 
 
21
/* static function prototypes */
 
22
static routerinfo_t *router_pick_directory_server_impl(void);
 
23
static int router_resolve_routerlist(routerlist_t *dir);
 
24
 
 
25
/****************************************************************************/
 
26
 
 
27
/****
 
28
 * Functions to manage and access our list of known routers. (Note:
 
29
 * dirservers maintain a separate, independent list of known router
 
30
 * descriptors.)
 
31
 *****/
 
32
 
 
33
/** Global list of all of the routers that we, as an OR or OP, know about. */
 
34
static routerlist_t *routerlist = NULL;
 
35
 
 
36
extern int has_fetched_directory; /**< from main.c */
 
37
 
 
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) {
 
41
  routerinfo_t *choice;
 
42
 
 
43
  choice = router_pick_directory_server_impl();
 
44
  if(!choice) {
 
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)
 
50
        return NULL;
 
51
    } else {
 
52
      if(config_assign_default_dirservers() < 0)
 
53
        return NULL;
 
54
    }
 
55
    /* give it another try */
 
56
    choice = router_pick_directory_server_impl();
 
57
  }
 
58
  return choice;
 
59
}
 
60
 
 
61
/** Pick a random running router with a positive dir_port from our
 
62
 * routerlist. */
 
63
static routerinfo_t *router_pick_directory_server_impl(void) {
 
64
  int i;
 
65
  routerinfo_t *router, *dirserver=NULL;
 
66
  smartlist_t *sl;
 
67
 
 
68
  if(!routerlist)
 
69
    return NULL;
 
70
 
 
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);
 
77
  }
 
78
 
 
79
  router = smartlist_choose(sl);
 
80
  smartlist_free(sl);
 
81
 
 
82
  if(router)
 
83
    return router;
 
84
  log_fn(LOG_INFO,"No dirservers are reachable. Trying them all again.");
 
85
 
 
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;
 
92
      dirserver = router;
 
93
    }
 
94
  }
 
95
  if(!dirserver)
 
96
    log_fn(LOG_WARN,"No dirservers in directory! Returning NULL.");
 
97
  return dirserver;
 
98
}
 
99
 
 
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>.
 
103
 */
 
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;
 
108
 
 
109
  tor_assert(sl);
 
110
  tor_assert(list);
 
111
 
 
112
  while(isspace((int)*list) || *list==',') list++;
 
113
 
 
114
  start = list;
 
115
  while(*start) {
 
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);
 
120
    if (router) {
 
121
      if (router->is_running)
 
122
        smartlist_add(sl,router);
 
123
      else
 
124
        log_fn(LOG_INFO,"Nickname list includes '%s' which is known but down.",nick);
 
125
    } else
 
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++;
 
129
    start = end;
 
130
  }
 
131
}
 
132
 
 
133
/** Add every router from our routerlist that is currently running to
 
134
 * <b>sl</b>.
 
135
 */
 
136
void router_add_running_routers_to_smartlist(smartlist_t *sl) {
 
137
  routerinfo_t *router;
 
138
  int i;
 
139
 
 
140
  if(!routerlist)
 
141
    return;
 
142
 
 
143
  for(i=0;i<smartlist_len(routerlist->routers);i++) {
 
144
    router = smartlist_get(routerlist->routers, i);
 
145
    if(router->is_running &&
 
146
       (!options.ORPort ||
 
147
        connection_twin_get_by_addr_port(router->addr, router->or_port) ))
 
148
      smartlist_add(sl, router);
 
149
  }
 
150
}
 
151
 
 
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.
 
156
 */
 
157
routerinfo_t *router_choose_random_node(char *preferred, char *excluded,
 
158
                                        smartlist_t *excludedsmartlist)
 
159
{
 
160
  smartlist_t *sl, *excludednodes;
 
161
  routerinfo_t *choice;
 
162
 
 
163
  excludednodes = smartlist_create();
 
164
  add_nickname_list_to_smartlist(excludednodes,excluded);
 
165
 
 
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);
 
173
  smartlist_free(sl);
 
174
  if(!choice) {
 
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);
 
181
    smartlist_free(sl);
 
182
  }
 
183
  smartlist_free(excludednodes);
 
184
  if(!choice)
 
185
    log_fn(LOG_WARN,"No available nodes when trying to choose node. Failing.");
 
186
  return choice;
 
187
}
 
188
 
 
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.
 
191
 */
 
192
routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
 
193
  int i;
 
194
  routerinfo_t *router;
 
195
 
 
196
  tor_assert(routerlist);
 
197
 
 
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))
 
201
      return router;
 
202
  }
 
203
  return NULL;
 
204
}
 
205
 
 
206
/** Return the router in our routerlist whose nickname is <b>nickname</b>
 
207
 * (case insensitive).  Return NULL if no such router is known.
 
208
 */
 
209
routerinfo_t *router_get_by_nickname(char *nickname)
 
210
{
 
211
  int i;
 
212
  routerinfo_t *router;
 
213
 
 
214
  tor_assert(nickname);
 
215
  if (!routerlist)
 
216
    return NULL;
 
217
 
 
218
  for(i=0;i<smartlist_len(routerlist->routers);i++) {
 
219
    router = smartlist_get(routerlist->routers, i);
 
220
    if (0 == strcasecmp(router->nickname, nickname))
 
221
      return router;
 
222
  }
 
223
 
 
224
  return NULL;
 
225
}
 
226
 
 
227
/** Set *<b>prouterlist</b> to the current list of all known routers. */
 
228
void router_get_routerlist(routerlist_t **prouterlist) {
 
229
  *prouterlist = routerlist;
 
230
}
 
231
 
 
232
/** Free all storage held by <b>router</b>. */
 
233
void routerinfo_free(routerinfo_t *router)
 
234
{
 
235
  if (!router)
 
236
    return;
 
237
 
 
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);
 
246
  free(router);
 
247
}
 
248
 
 
249
/** Allocate a fresh copy of <b>router</b> */
 
250
routerinfo_t *routerinfo_copy(const routerinfo_t *router)
 
251
{
 
252
  routerinfo_t *r;
 
253
  struct exit_policy_t **e, *tmp;
 
254
 
 
255
  r = tor_malloc(sizeof(routerinfo_t));
 
256
  memcpy(r, router, sizeof(routerinfo_t));
 
257
 
 
258
  r->address = tor_strdup(r->address);
 
259
  r->nickname = tor_strdup(r->nickname);
 
260
  r->platform = tor_strdup(r->platform);
 
261
  if (r->onion_pkey)
 
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);
 
265
  e = &r->exit_policy;
 
266
  while (*e) {
 
267
    tmp = tor_malloc(sizeof(struct exit_policy_t));
 
268
    memcpy(tmp,*e,sizeof(struct exit_policy_t));
 
269
    *e = tmp;
 
270
    (*e)->string = tor_strdup((*e)->string);
 
271
    e = & ((*e)->next);
 
272
  }
 
273
  return r;
 
274
}
 
275
 
 
276
/** Free all storage held by a routerlist <b>rl</b> */
 
277
void routerlist_free(routerlist_t *rl)
 
278
{
 
279
  SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
 
280
                    routerinfo_free(r));
 
281
  smartlist_free(rl->routers);
 
282
  tor_free(rl->software_versions);
 
283
  tor_free(rl);
 
284
}
 
285
 
 
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 */
 
292
    return;
 
293
  log_fn(LOG_DEBUG,"Marking %s as down.",router->nickname);
 
294
  router->is_running = 0;
 
295
}
 
296
 
 
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.
 
302
 */
 
303
int router_add_to_routerlist(routerinfo_t *router) {
 
304
  int i;
 
305
  routerinfo_t *r;
 
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.
 
308
   */
 
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'",
 
315
                 router->nickname);
 
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;
 
322
          routerinfo_free(r);
 
323
          smartlist_set(routerlist->routers, i, router);
 
324
          return 0;
 
325
        } else {
 
326
          log_fn(LOG_DEBUG, "Skipping old entry for router '%s'",
 
327
                 router->nickname);
 
328
          /* If we now trust 'router', then we trust the one in the routerlist
 
329
           * too. */
 
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);
 
335
          return -1;
 
336
        }
 
337
      } else {
 
338
        log_fn(LOG_WARN, "Identity key mismatch for router '%s'",
 
339
               router->nickname);
 
340
        routerinfo_free(router);
 
341
        return -1;
 
342
      }
 
343
    }
 
344
  }
 
345
  /* We haven't seen a router with this name before.  Add it to the end of
 
346
   * the list. */
 
347
  smartlist_add(routerlist->routers, router);
 
348
  return 0;
 
349
}
 
350
 
 
351
/** Remove any routers from the routerlist that are more than ROUTER_MAX_AGE
 
352
 * seconds old.
 
353
 *
 
354
 * (This function is just like dirserv_remove_old_servers. One day we should
 
355
 * merge them.)
 
356
 */
 
357
void
 
358
routerlist_remove_old_routers(void)
 
359
{
 
360
  int i;
 
361
  time_t cutoff;
 
362
  routerinfo_t *router;
 
363
  if (!routerlist)
 
364
    return;
 
365
 
 
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 &&
 
370
      !router->dir_port) {
 
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--);
 
375
    }
 
376
  }
 
377
}
 
378
 
 
379
/*
 
380
 * Code to parse router descriptors and directories.
 
381
 */
 
382
 
 
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)
 
387
{
 
388
  char *string;
 
389
 
 
390
  string = read_file_to_str(routerfile);
 
391
  if(!string) {
 
392
    log_fn(LOG_WARN,"Failed to load routerfile %s.",routerfile);
 
393
    return -1;
 
394
  }
 
395
 
 
396
  if(router_load_routerlist_from_string(string, trusted) < 0) {
 
397
    log_fn(LOG_WARN,"The routerfile itself was corrupt.");
 
398
    free(string);
 
399
    return -1;
 
400
  }
 
401
  /* dump_onion_keys(LOG_NOTICE); */
 
402
 
 
403
  free(string);
 
404
  return 0;
 
405
}
 
406
 
 
407
/** Mark all directories in the routerlist as nontrusted. */
 
408
void routerlist_clear_trusted_directories(void)
 
409
{
 
410
  if (!routerlist) return;
 
411
  SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
 
412
                    r->is_trusted_dir = 0);
 
413
}
 
414
 
 
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
 
419
 */
 
420
int router_load_routerlist_from_string(const char *s, int trusted)
 
421
{
 
422
  routerlist_t *new_list=NULL;
 
423
 
 
424
  if (router_parse_list_from_string(&s, &new_list, -1, NULL)) {
 
425
    log(LOG_WARN, "Error parsing router file");
 
426
    return -1;
 
427
  }
 
428
  if (trusted) {
 
429
    SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
 
430
                      if (r->dir_port) r->is_trusted_dir = 1);
 
431
  }
 
432
  if (routerlist) {
 
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);
 
437
  } else {
 
438
    routerlist = new_list;
 
439
  }
 
440
  if (router_resolve_routerlist(routerlist)) {
 
441
    log(LOG_WARN, "Error resolving routerlist");
 
442
    return -1;
 
443
  }
 
444
  /* dump_onion_keys(LOG_NOTICE); */
 
445
 
 
446
  return 0;
 
447
}
 
448
 
 
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)
 
454
{
 
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.");
 
459
    return -1;
 
460
  }
 
461
  if (routerlist) {
 
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);
 
470
  } else {
 
471
    routerlist = new_list;
 
472
  }
 
473
  if (router_resolve_routerlist(routerlist)) {
 
474
    log_fn(LOG_WARN, "Error resolving routerlist");
 
475
    return -1;
 
476
  }
 
477
  return 0;
 
478
}
 
479
 
 
480
/** Helper function: resolve the hostname for <b>router</b>. */
 
481
static int
 
482
router_resolve(routerinfo_t *router)
 
483
{
 
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);
 
487
    return -1;
 
488
  }
 
489
  router->addr = ntohl(router->addr); /* get it back into host order */
 
490
 
 
491
  return 0;
 
492
}
 
493
 
 
494
/** Helper function: resolve every router in rl, and ensure that our own
 
495
 * routerinfo is at the front.
 
496
 */
 
497
static int
 
498
router_resolve_routerlist(routerlist_t *rl)
 
499
{
 
500
  int i, remove;
 
501
  routerinfo_t *r;
 
502
  if (!rl)
 
503
    rl = routerlist;
 
504
 
 
505
  i = 0;
 
506
  if ((r = router_get_my_routerinfo())) {
 
507
    smartlist_insert(rl->routers, 0, routerinfo_copy(r));
 
508
    ++i;
 
509
  }
 
510
 
 
511
  for ( ; i < smartlist_len(rl->routers); ++i) {
 
512
    remove = 0;
 
513
    r = smartlist_get(rl->routers,i);
 
514
    if (router_is_me(r)) {
 
515
      remove = 1;
 
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);
 
520
      remove = 1;
 
521
    }
 
522
    if (remove) {
 
523
      routerinfo_free(r);
 
524
      smartlist_del_keeporder(rl->routers, i--);
 
525
    }
 
526
  }
 
527
 
 
528
  return 0;
 
529
}
 
530
 
 
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.
 
534
 *
 
535
 * Returns -1 for "rejected", 0 for "accepted", 1 for "maybe" (since IP is
 
536
 * unknown).
 
537
 */
 
538
int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
 
539
                                       struct exit_policy_t *policy)
 
540
{
 
541
  int maybe_reject = 0;
 
542
  int maybe_accept = 0;
 
543
  int match = 0;
 
544
  int maybe = 0;
 
545
  struct in_addr in;
 
546
  struct exit_policy_t *tmpe;
 
547
 
 
548
  for(tmpe=policy; tmpe; tmpe=tmpe->next) {
 
549
//    log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
 
550
    maybe = 0;
 
551
    if (!addr) {
 
552
      /* Address is unknown. */
 
553
      if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
 
554
        /* The port definitely matches. */
 
555
        if (tmpe->msk == 0) {
 
556
          match = 1;
 
557
        } else {
 
558
          maybe = 1;
 
559
        }
 
560
      } else if (!port) {
 
561
        /* The port maybe matches. */
 
562
        maybe = 1;
 
563
      }
 
564
    } else {
 
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 */
 
569
          match = 1;
 
570
        } else if (!port) {
 
571
          maybe = 1;
 
572
        }
 
573
      }
 
574
    }
 
575
    if (maybe) {
 
576
      if (tmpe->policy_type == EXIT_POLICY_REJECT)
 
577
        maybe_reject = 1;
 
578
      else
 
579
        maybe_accept = 1;
 
580
    }
 
581
    if (match) {
 
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;
 
589
      } else {
 
590
        return maybe_accept ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_REJECTED;
 
591
      }
 
592
    }
 
593
  }
 
594
  /* accept all by default. */
 
595
  return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
 
596
}
 
597
 
 
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) {
 
601
  int i;
 
602
  routerinfo_t *router;
 
603
 
 
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. */
 
609
  }
 
610
  return 1; /* all will reject. */
 
611
}
 
612
 
 
613
/** Return true iff <b>router</b> does not permit exit streams.
 
614
 */
 
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;
 
618
}
 
619
 
 
620
/*
 
621
  Local Variables:
 
622
  mode:c
 
623
  indent-tabs-mode:nil
 
624
  c-basic-offset:2
 
625
  End:
 
626
*/