routerlist.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /* Copyright 2001-2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /**
  6. * \file routerlist.c
  7. *
  8. * \brief Code to
  9. * maintain and access the global list of routerinfos for known
  10. * servers.
  11. **/
  12. /****************************************************************************/
  13. extern or_options_t options; /**< command-line and config-file options */
  14. /* ********************************************************************** */
  15. /* static function prototypes */
  16. static routerinfo_t *router_pick_directory_server_impl(void);
  17. static int router_resolve_routerlist(routerlist_t *dir);
  18. /****************************************************************************/
  19. /****
  20. * Functions to manage and access our list of known routers. (Note:
  21. * dirservers maintain a separate, independent list of known router
  22. * descriptors.)
  23. *****/
  24. /** Global list of all of the routers that we, as an OR or OP, know about. */
  25. static routerlist_t *routerlist = NULL;
  26. extern int has_fetched_directory; /**< from main.c */
  27. /** Try to find a running dirserver. If there are no running dirservers
  28. * in our routerlist, reload the routerlist and try again. */
  29. routerinfo_t *router_pick_directory_server(void) {
  30. routerinfo_t *choice;
  31. choice = router_pick_directory_server_impl();
  32. if(!choice) {
  33. log_fn(LOG_WARN,"No dirservers known. Reloading and trying again.");
  34. has_fetched_directory=0; /* reset it */
  35. routerlist_clear_trusted_directories();
  36. if(options.RouterFile) {
  37. if(router_load_routerlist_from_file(options.RouterFile, 1) < 0)
  38. return NULL;
  39. } else {
  40. if(config_assign_default_dirservers() < 0)
  41. return NULL;
  42. }
  43. /* give it another try */
  44. choice = router_pick_directory_server_impl();
  45. }
  46. return choice;
  47. }
  48. /** Pick a random running router that's a trusted dirserver from our
  49. * routerlist. */
  50. static routerinfo_t *router_pick_directory_server_impl(void) {
  51. int i;
  52. routerinfo_t *router;
  53. smartlist_t *sl;
  54. if(!routerlist)
  55. return NULL;
  56. /* Find all the running dirservers we know about. */
  57. sl = smartlist_create();
  58. for(i=0;i< smartlist_len(routerlist->routers); i++) {
  59. router = smartlist_get(routerlist->routers, i);
  60. if(router->is_running && router->is_trusted_dir) {
  61. tor_assert(router->dir_port > 0);
  62. smartlist_add(sl, router);
  63. }
  64. }
  65. router = smartlist_choose(sl);
  66. smartlist_free(sl);
  67. if(router)
  68. return router;
  69. log_fn(LOG_INFO,"No dirservers are reachable. Trying them all again.");
  70. /* No running dir servers found? go through and mark them all as up,
  71. * so we cycle through the list again. */
  72. sl = smartlist_create();
  73. for(i=0; i < smartlist_len(routerlist->routers); i++) {
  74. router = smartlist_get(routerlist->routers, i);
  75. if(router->is_trusted_dir) {
  76. tor_assert(router->dir_port > 0);
  77. router->is_running = 1;
  78. smartlist_add(sl, router);
  79. }
  80. }
  81. router = smartlist_choose(sl);
  82. smartlist_free(sl);
  83. if(!router)
  84. log_fn(LOG_WARN,"No dirservers in directory! Returning NULL.");
  85. return router;
  86. }
  87. /** Return 0 if \exists an authoritative dirserver that's currently
  88. * thought to be running, else return 1.
  89. */
  90. int all_directory_servers_down(void) {
  91. int i;
  92. routerinfo_t *router;
  93. if(!routerlist)
  94. return 1; /* if no dirservers, I guess they're all down */
  95. for(i=0;i< smartlist_len(routerlist->routers); i++) {
  96. router = smartlist_get(routerlist->routers, i);
  97. if(router->is_running && router->is_trusted_dir) {
  98. tor_assert(router->dir_port > 0);
  99. return 0;
  100. }
  101. }
  102. return 1;
  103. }
  104. /** Given a comma-and-whitespace separated list of nicknames, see which
  105. * nicknames in <b>list</b> name routers in our routerlist that are
  106. * currently running. Add the routerinfos for those routers to <b>sl</b>.
  107. */
  108. void add_nickname_list_to_smartlist(smartlist_t *sl, const char *list) {
  109. const char *start,*end;
  110. char nick[MAX_NICKNAME_LEN+1];
  111. routerinfo_t *router;
  112. tor_assert(sl);
  113. tor_assert(list);
  114. while(isspace((int)*list) || *list==',') list++;
  115. start = list;
  116. while(*start) {
  117. end=start; while(*end && !isspace((int)*end) && *end != ',') end++;
  118. memcpy(nick,start,end-start);
  119. nick[end-start] = 0; /* null terminate it */
  120. router = router_get_by_nickname(nick);
  121. if (router) {
  122. if (router->is_running)
  123. smartlist_add(sl,router);
  124. else
  125. log_fn(LOG_WARN,"Nickname list includes '%s' which is known but down.",nick);
  126. } else
  127. log_fn(has_fetched_directory ? LOG_WARN : LOG_INFO,
  128. "Nickname list includes '%s' which isn't a known router.",nick);
  129. while(isspace((int)*end) || *end==',') end++;
  130. start = end;
  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. if(!routerlist)
  140. return;
  141. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  142. router = smartlist_get(routerlist->routers, i);
  143. if(router->is_running &&
  144. (!options.ORPort ||
  145. connection_get_by_identity_digest(router->identity_digest,
  146. CONN_TYPE_OR)))
  147. smartlist_add(sl, router);
  148. }
  149. }
  150. /** Return a random running router from the routerlist. If any node
  151. * named in <b>preferred</b> is available, pick one of those. Never pick a
  152. * node named in <b>excluded</b>, or whose routerinfo is in
  153. * <b>excludedsmartlist</b>, even if they are the only nodes available.
  154. */
  155. routerinfo_t *router_choose_random_node(char *preferred, char *excluded,
  156. smartlist_t *excludedsmartlist)
  157. {
  158. smartlist_t *sl, *excludednodes;
  159. routerinfo_t *choice;
  160. excludednodes = smartlist_create();
  161. add_nickname_list_to_smartlist(excludednodes,excluded);
  162. /* try the nodes in RendNodes first */
  163. sl = smartlist_create();
  164. add_nickname_list_to_smartlist(sl,preferred);
  165. smartlist_subtract(sl,excludednodes);
  166. if(excludedsmartlist)
  167. smartlist_subtract(sl,excludedsmartlist);
  168. choice = smartlist_choose(sl);
  169. smartlist_free(sl);
  170. if(!choice) {
  171. sl = smartlist_create();
  172. router_add_running_routers_to_smartlist(sl);
  173. smartlist_subtract(sl,excludednodes);
  174. if(excludedsmartlist)
  175. smartlist_subtract(sl,excludedsmartlist);
  176. choice = smartlist_choose(sl);
  177. smartlist_free(sl);
  178. }
  179. smartlist_free(excludednodes);
  180. if(!choice)
  181. log_fn(LOG_WARN,"No available nodes when trying to choose node. Failing.");
  182. return choice;
  183. }
  184. /** Return the router in our routerlist whose address is <b>addr</b> and
  185. * whose OR port is <b>port</b>. Return NULL if no such router is known.
  186. */
  187. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  188. int i;
  189. routerinfo_t *router;
  190. tor_assert(routerlist);
  191. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  192. router = smartlist_get(routerlist->routers, i);
  193. if ((router->addr == addr) && (router->or_port == port))
  194. return router;
  195. }
  196. return NULL;
  197. }
  198. /* DOCDOC */
  199. static INLINE int router_hex_digest_matches(routerinfo_t *router,
  200. const char *hexdigest)
  201. {
  202. char digest[DIGEST_LEN];
  203. tor_assert(hexdigest);
  204. if (hexdigest[0] == '$')
  205. ++hexdigest;
  206. if (base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
  207. return 0;
  208. else
  209. return (!memcmp(digest, router->identity_digest, DIGEST_LEN));
  210. }
  211. /* DOCDOC */
  212. int router_nickname_matches(routerinfo_t *router, const char *nickname)
  213. {
  214. if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
  215. return 1;
  216. else
  217. return router_hex_digest_matches(router, nickname);
  218. }
  219. /** Return the router in our routerlist whose (case-insensitive)
  220. * nickname or (case-sensitive) hexadecimal key digest is
  221. * <b>nickname</b>. Return NULL if no such router is known.
  222. */
  223. routerinfo_t *router_get_by_nickname(const char *nickname)
  224. {
  225. int i, maybedigest;
  226. routerinfo_t *router;
  227. char digest[DIGEST_LEN];
  228. tor_assert(nickname);
  229. if (!routerlist)
  230. return NULL;
  231. if (nickname[0] == '$')
  232. return router_get_by_hexdigest(nickname);
  233. maybedigest = (strlen(nickname) == HEX_DIGEST_LEN) &&
  234. (base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);
  235. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  236. router = smartlist_get(routerlist->routers, i);
  237. if (0 == strcasecmp(router->nickname, nickname) ||
  238. (maybedigest && 0 == memcmp(digest, router->identity_digest,
  239. DIGEST_LEN)))
  240. return router;
  241. }
  242. return NULL;
  243. }
  244. /** Return the router in our routerlist whose hexadecimal key digest
  245. * is <b>hexdigest</b>. Return NULL if no such router is known. */
  246. routerinfo_t *router_get_by_hexdigest(const char *hexdigest) {
  247. char digest[DIGEST_LEN];
  248. tor_assert(hexdigest);
  249. if (!routerlist)
  250. return NULL;
  251. if (hexdigest[0]=='$')
  252. ++hexdigest;
  253. if (strlen(hexdigest) != HEX_DIGEST_LEN ||
  254. base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
  255. return NULL;
  256. return router_get_by_digest(digest);
  257. }
  258. /** Return the router in our routerlist whose 20-byte key digest
  259. * is <b>hexdigest</b>. Return NULL if no such router is known. */
  260. routerinfo_t *router_get_by_digest(const char *digest) {
  261. int i;
  262. routerinfo_t *router;
  263. tor_assert(digest);
  264. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  265. router = smartlist_get(routerlist->routers, i);
  266. if (0 == memcmp(router->identity_digest, digest, DIGEST_LEN))
  267. return router;
  268. }
  269. return NULL;
  270. }
  271. /** Set *<b>prouterlist</b> to the current list of all known routers. */
  272. void router_get_routerlist(routerlist_t **prouterlist) {
  273. *prouterlist = routerlist;
  274. }
  275. /** Free all storage held by <b>router</b>. */
  276. void routerinfo_free(routerinfo_t *router)
  277. {
  278. if (!router)
  279. return;
  280. tor_free(router->address);
  281. tor_free(router->nickname);
  282. tor_free(router->platform);
  283. if (router->onion_pkey)
  284. crypto_free_pk_env(router->onion_pkey);
  285. if (router->identity_pkey)
  286. crypto_free_pk_env(router->identity_pkey);
  287. exit_policy_free(router->exit_policy);
  288. free(router);
  289. }
  290. /** Allocate a fresh copy of <b>router</b> */
  291. routerinfo_t *routerinfo_copy(const routerinfo_t *router)
  292. {
  293. routerinfo_t *r;
  294. struct exit_policy_t **e, *tmp;
  295. r = tor_malloc(sizeof(routerinfo_t));
  296. memcpy(r, router, sizeof(routerinfo_t));
  297. r->address = tor_strdup(r->address);
  298. r->nickname = tor_strdup(r->nickname);
  299. r->platform = tor_strdup(r->platform);
  300. if (r->onion_pkey)
  301. r->onion_pkey = crypto_pk_dup_key(r->onion_pkey);
  302. if (r->identity_pkey)
  303. r->identity_pkey = crypto_pk_dup_key(r->identity_pkey);
  304. e = &r->exit_policy;
  305. while (*e) {
  306. tmp = tor_malloc(sizeof(struct exit_policy_t));
  307. memcpy(tmp,*e,sizeof(struct exit_policy_t));
  308. *e = tmp;
  309. (*e)->string = tor_strdup((*e)->string);
  310. e = & ((*e)->next);
  311. }
  312. return r;
  313. }
  314. /** Free all storage held by a routerlist <b>rl</b> */
  315. void routerlist_free(routerlist_t *rl)
  316. {
  317. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
  318. routerinfo_free(r));
  319. smartlist_free(rl->routers);
  320. tor_free(rl->software_versions);
  321. tor_free(rl);
  322. }
  323. /** Mark the router with ID <b>digest</b> as non-running in our routerlist. */
  324. void router_mark_as_down(const char *digest) {
  325. routerinfo_t *router;
  326. tor_assert(digest);
  327. router = router_get_by_digest(digest);
  328. if(!router) /* we don't seem to know about him in the first place */
  329. return;
  330. log_fn(LOG_DEBUG,"Marking %s as down.",router->nickname);
  331. router->is_running = 0;
  332. }
  333. /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
  334. * older entries (if any) with the same name. Note: Callers should not hold
  335. * their pointers to <b>router</b> after invoking this function; <b>router</b>
  336. * will either be inserted into the routerlist or freed. Returns 0 if the
  337. * router was added; -1 if it was not.
  338. */
  339. int router_add_to_routerlist(routerinfo_t *router) {
  340. int i;
  341. routerinfo_t *r;
  342. /* If we have a router with this name, and the identity key is the same,
  343. * choose the newer one. If the identity key has changed, drop the router.
  344. */
  345. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  346. r = smartlist_get(routerlist->routers, i);
  347. /* XXXX008 should just compare digests instead. */
  348. if (!strcasecmp(router->nickname, r->nickname)) {
  349. if (!crypto_pk_cmp_keys(router->identity_pkey, r->identity_pkey)) {
  350. if (router->published_on > r->published_on) {
  351. log_fn(LOG_DEBUG, "Replacing entry for router '%s'",
  352. router->nickname);
  353. /* Remember whether we trust this router as a dirserver. */
  354. if (r->is_trusted_dir)
  355. router->is_trusted_dir = 1;
  356. /* If the address hasn't changed; no need to re-resolve. */
  357. if (!strcasecmp(r->address, router->address))
  358. router->addr = r->addr;
  359. routerinfo_free(r);
  360. smartlist_set(routerlist->routers, i, router);
  361. return 0;
  362. } else {
  363. log_fn(LOG_DEBUG, "Skipping old entry for router '%s'",
  364. router->nickname);
  365. /* If we now trust 'router', then we trust the one in the routerlist
  366. * too. */
  367. if (router->is_trusted_dir)
  368. r->is_trusted_dir = 1;
  369. /* Update the is_running status to whatever we were told. */
  370. r->is_running = router->is_running;
  371. routerinfo_free(router);
  372. return -1;
  373. }
  374. } else {
  375. /* XXXX008 It's okay to have two keys for a nickname as soon as
  376. * all the 007 clients are dead. */
  377. log_fn(LOG_WARN, "Identity key mismatch for router '%s'",
  378. router->nickname);
  379. routerinfo_free(router);
  380. return -1;
  381. }
  382. }
  383. }
  384. /* We haven't seen a router with this name before. Add it to the end of
  385. * the list. */
  386. smartlist_add(routerlist->routers, router);
  387. return 0;
  388. }
  389. /** Remove any routers from the routerlist that are more than ROUTER_MAX_AGE
  390. * seconds old.
  391. *
  392. * (This function is just like dirserv_remove_old_servers. One day we should
  393. * merge them.)
  394. */
  395. void
  396. routerlist_remove_old_routers(void)
  397. {
  398. int i;
  399. time_t cutoff;
  400. routerinfo_t *router;
  401. if (!routerlist)
  402. return;
  403. cutoff = time(NULL) - ROUTER_MAX_AGE;
  404. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  405. router = smartlist_get(routerlist->routers, i);
  406. if (router->published_on < cutoff &&
  407. !router->dir_port) {
  408. /* Too old. Remove it. But never remove dirservers! */
  409. log_fn(LOG_INFO,"Forgetting obsolete routerinfo for node %s.", router->nickname);
  410. routerinfo_free(router);
  411. smartlist_del(routerlist->routers, i--);
  412. }
  413. }
  414. }
  415. /*
  416. * Code to parse router descriptors and directories.
  417. */
  418. /** Update the current router list with the one stored in
  419. * <b>routerfile</b>. If <b>trusted</b> is true, then we'll use
  420. * directory servers from the file. */
  421. int router_load_routerlist_from_file(char *routerfile, int trusted)
  422. {
  423. char *string;
  424. string = read_file_to_str(routerfile);
  425. if(!string) {
  426. log_fn(LOG_WARN,"Failed to load routerfile %s.",routerfile);
  427. return -1;
  428. }
  429. if(router_load_routerlist_from_string(string, trusted) < 0) {
  430. log_fn(LOG_WARN,"The routerfile itself was corrupt.");
  431. free(string);
  432. return -1;
  433. }
  434. /* dump_onion_keys(LOG_NOTICE); */
  435. free(string);
  436. return 0;
  437. }
  438. /** Mark all directories in the routerlist as nontrusted. */
  439. void routerlist_clear_trusted_directories(void)
  440. {
  441. if (!routerlist) return;
  442. SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
  443. r->is_trusted_dir = 0);
  444. }
  445. /** Helper function: read routerinfo elements from s, and throw out the
  446. * ones that don't parse and resolve. Add all remaining elements to the
  447. * routerlist. If <b>trusted</b> is true, then we'll use
  448. * directory servers from the string
  449. */
  450. int router_load_routerlist_from_string(const char *s, int trusted)
  451. {
  452. routerlist_t *new_list=NULL;
  453. if (router_parse_list_from_string(&s, &new_list, -1, NULL)) {
  454. log(LOG_WARN, "Error parsing router file");
  455. return -1;
  456. }
  457. if (trusted) {
  458. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  459. if (r->dir_port) r->is_trusted_dir = 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_free(new_list);
  466. } else {
  467. routerlist = new_list;
  468. }
  469. if (router_resolve_routerlist(routerlist)) {
  470. log(LOG_WARN, "Error resolving routerlist");
  471. return -1;
  472. }
  473. /* dump_onion_keys(LOG_NOTICE); */
  474. return 0;
  475. }
  476. /** Add to the current routerlist each router stored in the
  477. * signed directory <b>s</b>. If pkey is provided, check the signature against
  478. * pkey; else check against the pkey of the signing directory server. */
  479. int router_load_routerlist_from_directory(const char *s,
  480. crypto_pk_env_t *pkey)
  481. {
  482. routerlist_t *new_list = NULL;
  483. check_software_version_against_directory(s, options.IgnoreVersion);
  484. if (router_parse_routerlist_from_directory(s, &new_list, pkey)) {
  485. log_fn(LOG_WARN, "Couldn't parse directory.");
  486. return -1;
  487. }
  488. if (routerlist) {
  489. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  490. router_add_to_routerlist(r));
  491. smartlist_clear(new_list->routers);
  492. routerlist->published_on = new_list->published_on;
  493. tor_free(routerlist->software_versions);
  494. routerlist->software_versions = new_list->software_versions;
  495. new_list->software_versions = NULL;
  496. routerlist_free(new_list);
  497. } else {
  498. routerlist = new_list;
  499. }
  500. if (router_resolve_routerlist(routerlist)) {
  501. log_fn(LOG_WARN, "Error resolving routerlist");
  502. return -1;
  503. }
  504. if (options.AuthoritativeDir) {
  505. /* Learn about the descriptors in the directory. */
  506. dirserv_load_from_directory_string(s);
  507. } else {
  508. /* Remember the directory. */
  509. dirserv_set_cached_directory(s, routerlist->published_on);
  510. }
  511. return 0;
  512. }
  513. /** Helper function: resolve the hostname for <b>router</b>. */
  514. static int
  515. router_resolve(routerinfo_t *router)
  516. {
  517. if (tor_lookup_hostname(router->address, &router->addr) != 0
  518. || !router->addr) {
  519. log_fn(LOG_WARN,"Could not get address for router %s (%s).",
  520. router->address, router->nickname);
  521. return -1;
  522. }
  523. router->addr = ntohl(router->addr); /* get it back into host order */
  524. return 0;
  525. }
  526. /** Helper function: resolve every router in rl, and ensure that our own
  527. * routerinfo is at the front.
  528. */
  529. static int
  530. router_resolve_routerlist(routerlist_t *rl)
  531. {
  532. int i, remove;
  533. routerinfo_t *r;
  534. if (!rl)
  535. rl = routerlist;
  536. i = 0;
  537. if ((r = router_get_my_routerinfo())) {
  538. smartlist_insert(rl->routers, 0, routerinfo_copy(r));
  539. ++i;
  540. }
  541. for ( ; i < smartlist_len(rl->routers); ++i) {
  542. remove = 0;
  543. r = smartlist_get(rl->routers,i);
  544. if (router_is_me(r)) {
  545. remove = 1;
  546. } else if (r->addr) {
  547. /* already resolved. */
  548. } else if (router_resolve(r)) {
  549. log_fn(LOG_WARN, "Couldn't resolve router %s; not using", r->address);
  550. remove = 1;
  551. }
  552. if (remove) {
  553. routerinfo_free(r);
  554. smartlist_del_keeporder(rl->routers, i--);
  555. }
  556. }
  557. return 0;
  558. }
  559. /** Decide whether a given addr:port is definitely accepted, definitely
  560. * rejected, or neither by a given exit policy. If <b>addr</b> is 0, we
  561. * don't know the IP of the target address.
  562. *
  563. * Returns -1 for "rejected", 0 for "accepted", 1 for "maybe" (since IP is
  564. * unknown).
  565. */
  566. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  567. struct exit_policy_t *policy)
  568. {
  569. int maybe_reject = 0;
  570. int maybe_accept = 0;
  571. int match = 0;
  572. int maybe = 0;
  573. struct in_addr in;
  574. struct exit_policy_t *tmpe;
  575. for(tmpe=policy; tmpe; tmpe=tmpe->next) {
  576. // log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
  577. maybe = 0;
  578. if (!addr) {
  579. /* Address is unknown. */
  580. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  581. /* The port definitely matches. */
  582. if (tmpe->msk == 0) {
  583. match = 1;
  584. } else {
  585. maybe = 1;
  586. }
  587. } else if (!port) {
  588. /* The port maybe matches. */
  589. maybe = 1;
  590. }
  591. } else {
  592. /* Address is known */
  593. if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
  594. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  595. /* Exact match for the policy */
  596. match = 1;
  597. } else if (!port) {
  598. maybe = 1;
  599. }
  600. }
  601. }
  602. if (maybe) {
  603. if (tmpe->policy_type == EXIT_POLICY_REJECT)
  604. maybe_reject = 1;
  605. else
  606. maybe_accept = 1;
  607. }
  608. if (match) {
  609. in.s_addr = htonl(addr);
  610. log_fn(LOG_DEBUG,"Address %s:%d matches exit policy '%s'",
  611. inet_ntoa(in), port, tmpe->string);
  612. if(tmpe->policy_type == EXIT_POLICY_ACCEPT) {
  613. /* If we already hit a clause that might trigger a 'reject', than we
  614. * can't be sure of this certain 'accept'.*/
  615. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  616. } else {
  617. return maybe_accept ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_REJECTED;
  618. }
  619. }
  620. }
  621. /* accept all by default. */
  622. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  623. }
  624. /** Return 1 if all running routers will reject addr:port, return 0 if
  625. * any might accept it. */
  626. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port) {
  627. int i;
  628. routerinfo_t *router;
  629. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  630. router = smartlist_get(routerlist->routers, i);
  631. if (router->is_running && router_compare_addr_to_exit_policy(
  632. addr, port, router->exit_policy) != ADDR_POLICY_REJECTED)
  633. return 0; /* this one could be ok. good enough. */
  634. }
  635. return 1; /* all will reject. */
  636. }
  637. /** Return true iff <b>router</b> does not permit exit streams.
  638. */
  639. int router_exit_policy_rejects_all(routerinfo_t *router) {
  640. return router_compare_addr_to_exit_policy(0, 0, router->exit_policy)
  641. == ADDR_POLICY_REJECTED;
  642. }
  643. /* DODCDOC */
  644. void running_routers_free(running_routers_t *rr)
  645. {
  646. tor_assert(rr);
  647. if (rr->running_routers) {
  648. SMARTLIST_FOREACH(rr->running_routers, char *, s, tor_free(s));
  649. smartlist_free(rr->running_routers);
  650. }
  651. tor_free(rr);
  652. }
  653. /* DOCDOC*/
  654. void routerlist_update_from_runningrouters(routerlist_t *list,
  655. running_routers_t *rr)
  656. {
  657. int n_routers, n_names, i, j, running;
  658. routerinfo_t *router;
  659. const char *name;
  660. if (!list)
  661. return;
  662. if (list->published_on >= rr->published_on)
  663. return;
  664. if (list->running_routers_updated_on >= rr->published_on)
  665. return;
  666. n_routers = smartlist_len(list->routers);
  667. n_names = smartlist_len(rr->running_routers);
  668. for (i=0; i<n_routers; ++i) {
  669. running = 0;
  670. router = smartlist_get(list->routers, i);
  671. for (j=0; j<n_names; ++j) {
  672. name = smartlist_get(rr->running_routers, j);
  673. if (*name != '!') {
  674. if (router_nickname_matches(router, name)) {
  675. router->is_running = 1;
  676. break;
  677. }
  678. } else { /* *name == '!' */
  679. if (router_nickname_matches(router, name)) {
  680. router->is_running = 0;
  681. break;
  682. }
  683. }
  684. }
  685. }
  686. list->running_routers_updated_on = rr->published_on;
  687. }
  688. /*
  689. Local Variables:
  690. mode:c
  691. indent-tabs-mode:nil
  692. c-basic-offset:2
  693. End:
  694. */