routerlist.c 25 KB

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