routerlist.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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 *
  17. router_pick_directory_server_impl(int requireauth, int preferothers);
  18. static void mark_all_authdirservers_up(void);
  19. static int router_resolve_routerlist(routerlist_t *dir);
  20. /****************************************************************************/
  21. /** List of digests of keys for servers that are trusted directories. */
  22. static smartlist_t *trusted_dir_digests = NULL;
  23. /****
  24. * Functions to manage and access our list of known routers. (Note:
  25. * dirservers maintain a separate, independent list of known router
  26. * descriptors.)
  27. *****/
  28. /** Global list of all of the routers that we, as an OR or OP, know about. */
  29. static routerlist_t *routerlist = NULL;
  30. extern int has_fetched_directory; /**< from main.c */
  31. /** Try to find a running dirserver. If there are no running dirservers
  32. * in our routerlist, set all the authoritative ones as running again,
  33. * and pick one. If there are no dirservers at all in our routerlist,
  34. * reload the routerlist and try one last time. */
  35. routerinfo_t *router_pick_directory_server(int requireauth, int requireothers) {
  36. routerinfo_t *choice;
  37. choice = router_pick_directory_server_impl(requireauth, requireothers);
  38. if(choice)
  39. return choice;
  40. log_fn(LOG_INFO,"No dirservers are reachable. Trying them all again.");
  41. /* mark all authdirservers are up again */
  42. mark_all_authdirservers_up();
  43. /* try again */
  44. choice = router_pick_directory_server_impl(requireauth, requireothers);
  45. if(choice)
  46. return choice;
  47. log_fn(LOG_WARN,"Still no dirservers %s. Reloading and trying again.",
  48. options.FascistFirewall ? "reachable" : "known");
  49. has_fetched_directory=0; /* reset it */
  50. routerlist_clear_trusted_directories();
  51. if(options.RouterFile) {
  52. if(router_load_routerlist_from_file(options.RouterFile, 1) < 0)
  53. return NULL;
  54. } else {
  55. if(config_assign_default_dirservers() < 0)
  56. return NULL;
  57. }
  58. /* give it one last try */
  59. choice = router_pick_directory_server_impl(requireauth, requireothers);
  60. return choice;
  61. }
  62. /** Pick a random running router from our routerlist. If requireauth,
  63. * it has to be a trusted server. If requireothers, it cannot be us.
  64. */
  65. static routerinfo_t *
  66. router_pick_directory_server_impl(int requireauth, int requireothers)
  67. {
  68. int i;
  69. routerinfo_t *router;
  70. smartlist_t *sl;
  71. char buf[16];
  72. if(!routerlist)
  73. return NULL;
  74. /* Find all the running dirservers we know about. */
  75. sl = smartlist_create();
  76. for(i=0;i< smartlist_len(routerlist->routers); i++) {
  77. router = smartlist_get(routerlist->routers, i);
  78. if(!router->is_running || !router->dir_port)
  79. continue;
  80. if(requireauth && !router->is_trusted_dir)
  81. continue;
  82. if(requireothers && router_is_me(router))
  83. continue;
  84. if(options.FascistFirewall) {
  85. sprintf(buf,"%d",router->dir_port);
  86. if (!smartlist_string_isin(options.FirewallPorts, buf))
  87. continue;
  88. }
  89. smartlist_add(sl, router);
  90. }
  91. router = smartlist_choose(sl);
  92. smartlist_free(sl);
  93. return router;
  94. }
  95. /** Go through and mark the auth dirservers as up */
  96. static void mark_all_authdirservers_up(void) {
  97. int i;
  98. routerinfo_t *router;
  99. if(!routerlist)
  100. return;
  101. for(i=0; i < smartlist_len(routerlist->routers); i++) {
  102. router = smartlist_get(routerlist->routers, i);
  103. if(router->is_trusted_dir) {
  104. tor_assert(router->dir_port > 0);
  105. router->is_running = 1;
  106. router->status_set_at = time(NULL);
  107. }
  108. }
  109. }
  110. /** Return 0 if \exists an authoritative dirserver that's currently
  111. * thought to be running, else return 1.
  112. */
  113. int all_directory_servers_down(void) {
  114. int i;
  115. routerinfo_t *router;
  116. if(!routerlist)
  117. return 1; /* if no dirservers, I guess they're all down */
  118. for(i=0;i< smartlist_len(routerlist->routers); i++) {
  119. router = smartlist_get(routerlist->routers, i);
  120. if(router->is_running && router->is_trusted_dir) {
  121. tor_assert(router->dir_port > 0);
  122. return 0;
  123. }
  124. }
  125. return 1;
  126. }
  127. /** Given a comma-and-whitespace separated list of nicknames, see which
  128. * nicknames in <b>list</b> name routers in our routerlist that are
  129. * currently running. Add the routerinfos for those routers to <b>sl</b>.
  130. */
  131. void add_nickname_list_to_smartlist(smartlist_t *sl, const char *list) {
  132. const char *start,*end;
  133. char nick[MAX_HEX_NICKNAME_LEN+1];
  134. routerinfo_t *router;
  135. tor_assert(sl);
  136. tor_assert(list);
  137. while(isspace((int)*list) || *list==',') list++;
  138. start = list;
  139. while(*start) {
  140. end=start; while(*end && !isspace((int)*end) && *end != ',') end++;
  141. if (end-start > MAX_HEX_NICKNAME_LEN) {
  142. log_fn(LOG_WARN,"Nickname too long; skipping");
  143. start = end;
  144. continue;
  145. }
  146. memcpy(nick,start,end-start);
  147. nick[end-start] = 0; /* null terminate it */
  148. router = router_get_by_nickname(nick);
  149. if (router) {
  150. if (router->is_running)
  151. smartlist_add(sl,router);
  152. else
  153. log_fn(LOG_WARN,"Nickname list includes '%s' which is known but down.",nick);
  154. } else
  155. log_fn(has_fetched_directory ? LOG_WARN : LOG_INFO,
  156. "Nickname list includes '%s' which isn't a known router.",nick);
  157. while(isspace((int)*end) || *end==',') end++;
  158. start = end;
  159. }
  160. }
  161. /** Add every router from our routerlist that is currently running to
  162. * <b>sl</b>.
  163. */
  164. void router_add_running_routers_to_smartlist(smartlist_t *sl) {
  165. routerinfo_t *router;
  166. int i;
  167. if(!routerlist)
  168. return;
  169. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  170. router = smartlist_get(routerlist->routers, i);
  171. /* XXX008 for now, only choose verified routers */
  172. if(router->is_running && router->is_verified) {
  173. if(!clique_mode()) {
  174. smartlist_add(sl, router);
  175. } else {
  176. connection_t *conn =
  177. connection_get_by_identity_digest(router->identity_digest,
  178. CONN_TYPE_OR);
  179. if(conn && conn->state == OR_CONN_STATE_OPEN)
  180. smartlist_add(sl, router);
  181. }
  182. }
  183. }
  184. }
  185. /** How many seconds a router must be up before we'll use it for
  186. * reliability-critical node positions.
  187. */
  188. #define ROUTER_REQUIRED_MIN_UPTIME 0
  189. /* XXX008 change this once we parse router->uptime */
  190. static void
  191. routerlist_sl_remove_unreliable_routers(smartlist_t *sl)
  192. {
  193. int i;
  194. routerinfo_t *router;
  195. for (i = 0; i < smartlist_len(sl); ++i) {
  196. router = smartlist_get(sl, i);
  197. if(router->uptime < ROUTER_REQUIRED_MIN_UPTIME) {
  198. log(LOG_DEBUG, "Router %s has insufficient uptime; deleting.",
  199. router->nickname);
  200. smartlist_del(sl, i--);
  201. }
  202. }
  203. }
  204. static routerinfo_t *
  205. routerlist_sl_choose_by_bandwidth(smartlist_t *sl)
  206. {
  207. int i;
  208. routerinfo_t *router;
  209. smartlist_t *bandwidths;
  210. uint32_t this_bw, tmp, total_bw=0, rand_bw;
  211. uint32_t *p;
  212. bandwidths = smartlist_create();
  213. for (i = 0; i < smartlist_len(sl); ++i) {
  214. router = smartlist_get(sl, i);
  215. /* give capacity a default, until 0.0.7 is obsolete */
  216. tmp = (router->bandwidthcapacity == 0) ? 200000 : router->bandwidthcapacity;
  217. this_bw = (tmp < router->bandwidthrate) ? tmp : router->bandwidthrate;
  218. if(this_bw > 800000)
  219. this_bw = 800000; /* if they claim something huge, don't believe it */
  220. p = tor_malloc(sizeof(uint32_t));
  221. *p = this_bw;
  222. smartlist_add(bandwidths, p);
  223. total_bw += this_bw;
  224. // log_fn(LOG_INFO,"Recording bw %d for node %s.", this_bw, router->nickname);
  225. }
  226. if(!total_bw)
  227. return NULL;
  228. rand_bw = crypto_pseudo_rand_int(total_bw);
  229. // log_fn(LOG_INFO,"Total bw %d. Randomly chose %d.", total_bw, rand_bw);
  230. tmp = 0;
  231. for(i=0; ; i++) {
  232. tor_assert(i < smartlist_len(sl));
  233. p = smartlist_get(bandwidths, i);
  234. tmp += *p;
  235. router = smartlist_get(sl, i);
  236. // log_fn(LOG_INFO,"Considering %s. tmp = %d.", router->nickname, tmp);
  237. if(tmp >= rand_bw)
  238. break;
  239. }
  240. SMARTLIST_FOREACH(bandwidths, uint32_t*, p, tor_free(p));
  241. smartlist_free(bandwidths);
  242. router = smartlist_get(sl, i);
  243. log_fn(LOG_INFO,"Picked %s.", router->nickname);
  244. return router;
  245. }
  246. /** Return a random running router from the routerlist. If any node
  247. * named in <b>preferred</b> is available, pick one of those. Never
  248. * pick a node named in <b>excluded</b>, or whose routerinfo is in
  249. * <b>excludedsmartlist</b>, even if they are the only nodes
  250. * available. If <b>strict</b> is true, never pick any node besides
  251. * those in <b>preferred</b>.
  252. */
  253. routerinfo_t *router_choose_random_node(char *preferred, char *excluded,
  254. smartlist_t *excludedsmartlist,
  255. int preferuptime, int preferbandwidth,
  256. int strict)
  257. {
  258. smartlist_t *sl, *excludednodes;
  259. routerinfo_t *choice;
  260. excludednodes = smartlist_create();
  261. add_nickname_list_to_smartlist(excludednodes,excluded);
  262. /* try the preferred nodes first */
  263. sl = smartlist_create();
  264. add_nickname_list_to_smartlist(sl,preferred);
  265. smartlist_subtract(sl,excludednodes);
  266. if(excludedsmartlist)
  267. smartlist_subtract(sl,excludedsmartlist);
  268. if(preferuptime)
  269. routerlist_sl_remove_unreliable_routers(sl);
  270. if(preferbandwidth)
  271. choice = routerlist_sl_choose_by_bandwidth(sl);
  272. else
  273. choice = smartlist_choose(sl);
  274. smartlist_free(sl);
  275. if(!choice && !strict) {
  276. sl = smartlist_create();
  277. router_add_running_routers_to_smartlist(sl);
  278. smartlist_subtract(sl,excludednodes);
  279. if(excludedsmartlist)
  280. smartlist_subtract(sl,excludedsmartlist);
  281. if(preferuptime)
  282. routerlist_sl_remove_unreliable_routers(sl);
  283. if(preferbandwidth)
  284. choice = routerlist_sl_choose_by_bandwidth(sl);
  285. smartlist_free(sl);
  286. }
  287. smartlist_free(excludednodes);
  288. if(!choice)
  289. log_fn(LOG_WARN,"No available nodes when trying to choose node. Failing.");
  290. return choice;
  291. }
  292. /** Return the router in our routerlist whose address is <b>addr</b> and
  293. * whose OR port is <b>port</b>. Return NULL if no such router is known.
  294. */
  295. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  296. int i;
  297. routerinfo_t *router;
  298. tor_assert(routerlist);
  299. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  300. router = smartlist_get(routerlist->routers, i);
  301. if ((router->addr == addr) && (router->or_port == port))
  302. return router;
  303. }
  304. return NULL;
  305. }
  306. /** Return true iff the digest of <b>router</b>'s identity key,
  307. * encoded in hexadecimal, matches <b>hexdigest</b> (which is
  308. * optionally prefixed with a single dollar sign). Return false if
  309. * <b>hexdigest</b> is malformed, or it doesn't match. */
  310. static INLINE int router_hex_digest_matches(routerinfo_t *router,
  311. const char *hexdigest)
  312. {
  313. char digest[DIGEST_LEN];
  314. tor_assert(hexdigest);
  315. if (hexdigest[0] == '$')
  316. ++hexdigest;
  317. if (strlen(hexdigest) != HEX_DIGEST_LEN ||
  318. base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
  319. return 0;
  320. else
  321. return (!memcmp(digest, router->identity_digest, DIGEST_LEN));
  322. }
  323. /* Return true if <b>router</b>'s nickname matches <b>nickname</b>
  324. * (case-insensitive), or if <b>router's</b> identity key digest
  325. * matches a hexadecimal value stored in <b>nickname</b>. Return
  326. * false otherwise.*/
  327. int router_nickname_matches(routerinfo_t *router, const char *nickname)
  328. {
  329. if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
  330. return 1;
  331. else
  332. return router_hex_digest_matches(router, nickname);
  333. }
  334. /** Return the router in our routerlist whose (case-insensitive)
  335. * nickname or (case-sensitive) hexadecimal key digest is
  336. * <b>nickname</b>. Return NULL if no such router is known.
  337. */
  338. routerinfo_t *router_get_by_nickname(const char *nickname)
  339. {
  340. int i, maybedigest;
  341. routerinfo_t *router;
  342. char digest[DIGEST_LEN];
  343. tor_assert(nickname);
  344. if (!routerlist)
  345. return NULL;
  346. if (nickname[0] == '$')
  347. return router_get_by_hexdigest(nickname);
  348. maybedigest = (strlen(nickname) == HEX_DIGEST_LEN) &&
  349. (base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);
  350. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  351. router = smartlist_get(routerlist->routers, i);
  352. if (0 == strcasecmp(router->nickname, nickname) ||
  353. (maybedigest && 0 == memcmp(digest, router->identity_digest,
  354. DIGEST_LEN)))
  355. return router;
  356. }
  357. return NULL;
  358. }
  359. /* XXX008 currently this trusted_dir_digests stuff is not used. */
  360. /** Return true iff <b>digest</b> is the digest of the identity key of
  361. * a trusted directory. */
  362. int router_digest_is_trusted_dir(const char *digest) {
  363. if (!trusted_dir_digests)
  364. return 0;
  365. SMARTLIST_FOREACH(trusted_dir_digests, char *, cp,
  366. if (!memcmp(digest, cp, DIGEST_LEN)) return 1);
  367. return 0;
  368. }
  369. /** Return the router in our routerlist whose hexadecimal key digest
  370. * is <b>hexdigest</b>. Return NULL if no such router is known. */
  371. routerinfo_t *router_get_by_hexdigest(const char *hexdigest) {
  372. char digest[DIGEST_LEN];
  373. tor_assert(hexdigest);
  374. if (!routerlist)
  375. return NULL;
  376. if (hexdigest[0]=='$')
  377. ++hexdigest;
  378. if (strlen(hexdigest) != HEX_DIGEST_LEN ||
  379. base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
  380. return NULL;
  381. return router_get_by_digest(digest);
  382. }
  383. /** Return the router in our routerlist whose 20-byte key digest
  384. * is <b>hexdigest</b>. Return NULL if no such router is known. */
  385. routerinfo_t *router_get_by_digest(const char *digest) {
  386. int i;
  387. routerinfo_t *router;
  388. tor_assert(digest);
  389. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  390. router = smartlist_get(routerlist->routers, i);
  391. if (0 == memcmp(router->identity_digest, digest, DIGEST_LEN))
  392. return router;
  393. }
  394. return NULL;
  395. }
  396. /** Set *<b>prouterlist</b> to the current list of all known routers. */
  397. void router_get_routerlist(routerlist_t **prouterlist) {
  398. *prouterlist = routerlist;
  399. }
  400. /** Free all storage held by <b>router</b>. */
  401. void routerinfo_free(routerinfo_t *router)
  402. {
  403. if (!router)
  404. return;
  405. tor_free(router->address);
  406. tor_free(router->nickname);
  407. tor_free(router->platform);
  408. if (router->onion_pkey)
  409. crypto_free_pk_env(router->onion_pkey);
  410. if (router->identity_pkey)
  411. crypto_free_pk_env(router->identity_pkey);
  412. exit_policy_free(router->exit_policy);
  413. free(router);
  414. }
  415. /** Allocate a fresh copy of <b>router</b> */
  416. routerinfo_t *routerinfo_copy(const routerinfo_t *router)
  417. {
  418. routerinfo_t *r;
  419. struct exit_policy_t **e, *tmp;
  420. r = tor_malloc(sizeof(routerinfo_t));
  421. memcpy(r, router, sizeof(routerinfo_t));
  422. r->address = tor_strdup(r->address);
  423. r->nickname = tor_strdup(r->nickname);
  424. r->platform = tor_strdup(r->platform);
  425. if (r->onion_pkey)
  426. r->onion_pkey = crypto_pk_dup_key(r->onion_pkey);
  427. if (r->identity_pkey)
  428. r->identity_pkey = crypto_pk_dup_key(r->identity_pkey);
  429. e = &r->exit_policy;
  430. while (*e) {
  431. tmp = tor_malloc(sizeof(struct exit_policy_t));
  432. memcpy(tmp,*e,sizeof(struct exit_policy_t));
  433. *e = tmp;
  434. (*e)->string = tor_strdup((*e)->string);
  435. e = & ((*e)->next);
  436. }
  437. return r;
  438. }
  439. /** Free all storage held by a routerlist <b>rl</b> */
  440. void routerlist_free(routerlist_t *rl)
  441. {
  442. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
  443. routerinfo_free(r));
  444. smartlist_free(rl->routers);
  445. tor_free(rl->software_versions);
  446. tor_free(rl);
  447. }
  448. /** Mark the router with ID <b>digest</b> as non-running in our routerlist. */
  449. void router_mark_as_down(const char *digest) {
  450. routerinfo_t *router;
  451. tor_assert(digest);
  452. router = router_get_by_digest(digest);
  453. if(!router) /* we don't seem to know about him in the first place */
  454. return;
  455. log_fn(LOG_DEBUG,"Marking %s as down.",router->nickname);
  456. router->is_running = 0;
  457. router->status_set_at = time(NULL);
  458. }
  459. /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
  460. * older entries (if any) with the same name. Note: Callers should not hold
  461. * their pointers to <b>router</b> after invoking this function; <b>router</b>
  462. * will either be inserted into the routerlist or freed. Returns 0 if the
  463. * router was added; -1 if it was not.
  464. */
  465. int router_add_to_routerlist(routerinfo_t *router) {
  466. int i;
  467. routerinfo_t *r;
  468. char id_digest[DIGEST_LEN];
  469. crypto_pk_get_digest(router->identity_pkey, id_digest);
  470. /* If we have a router with this name, and the identity key is the same,
  471. * choose the newer one. If the identity key has changed, drop the router.
  472. */
  473. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  474. r = smartlist_get(routerlist->routers, i);
  475. if (!crypto_pk_cmp_keys(router->identity_pkey, r->identity_pkey)) {
  476. if (router->published_on > r->published_on) {
  477. log_fn(LOG_DEBUG, "Replacing entry for router '%s/%s' [%s]",
  478. router->nickname, r->nickname, hex_str(id_digest,DIGEST_LEN));
  479. /* Remember whether we trust this router as a dirserver. */
  480. if (r->is_trusted_dir)
  481. router->is_trusted_dir = 1;
  482. /* If the address hasn't changed; no need to re-resolve. */
  483. if (!strcasecmp(r->address, router->address))
  484. router->addr = r->addr;
  485. routerinfo_free(r);
  486. smartlist_set(routerlist->routers, i, router);
  487. return 0;
  488. } else {
  489. log_fn(LOG_DEBUG, "Skipping old entry for router '%s'",
  490. router->nickname);
  491. /* If we now trust 'router', then we trust the one in the routerlist
  492. * too. */
  493. if (router->is_trusted_dir)
  494. r->is_trusted_dir = 1;
  495. /* Update the is_running status to whatever we were told. */
  496. r->is_running = router->is_running;
  497. routerinfo_free(router);
  498. return -1;
  499. }
  500. }
  501. }
  502. /* We haven't seen a router with this name before. Add it to the end of
  503. * the list. */
  504. smartlist_add(routerlist->routers, router);
  505. return 0;
  506. }
  507. /** Remove any routers from the routerlist that are more than <b>age</b>
  508. * seconds old.
  509. *
  510. * (This function is just like dirserv_remove_old_servers. One day we should
  511. * merge them.)
  512. */
  513. void
  514. routerlist_remove_old_routers(int age)
  515. {
  516. int i;
  517. time_t cutoff;
  518. routerinfo_t *router;
  519. if (!routerlist)
  520. return;
  521. cutoff = time(NULL) - age;
  522. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  523. router = smartlist_get(routerlist->routers, i);
  524. if (router->published_on <= cutoff &&
  525. !router->is_trusted_dir) {
  526. /* Too old. Remove it. But never remove dirservers! */
  527. log_fn(LOG_INFO,"Forgetting obsolete routerinfo for node %s.", router->nickname);
  528. routerinfo_free(router);
  529. smartlist_del(routerlist->routers, i--);
  530. }
  531. }
  532. }
  533. /*
  534. * Code to parse router descriptors and directories.
  535. */
  536. /** Update the current router list with the one stored in
  537. * <b>routerfile</b>. If <b>trusted</b> is true, then we'll use
  538. * directory servers from the file. */
  539. int router_load_routerlist_from_file(char *routerfile, int trusted)
  540. {
  541. char *string;
  542. string = read_file_to_str(routerfile);
  543. if(!string) {
  544. log_fn(LOG_WARN,"Failed to load routerfile %s.",routerfile);
  545. return -1;
  546. }
  547. if(router_load_routerlist_from_string(string, trusted) < 0) {
  548. log_fn(LOG_WARN,"The routerfile itself was corrupt.");
  549. free(string);
  550. return -1;
  551. }
  552. /* dump_onion_keys(LOG_NOTICE); */
  553. free(string);
  554. return 0;
  555. }
  556. /** Mark all directories in the routerlist as nontrusted. */
  557. void routerlist_clear_trusted_directories(void)
  558. {
  559. if (routerlist) {
  560. SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
  561. r->is_trusted_dir = 0);
  562. }
  563. if (trusted_dir_digests) {
  564. SMARTLIST_FOREACH(trusted_dir_digests, char *, cp, tor_free(cp));
  565. smartlist_clear(trusted_dir_digests);
  566. }
  567. }
  568. /** Helper function: read routerinfo elements from s, and throw out the
  569. * ones that don't parse and resolve. Add all remaining elements to the
  570. * routerlist. If <b>trusted</b> is true, then we'll use
  571. * directory servers from the string
  572. */
  573. int router_load_routerlist_from_string(const char *s, int trusted)
  574. {
  575. routerlist_t *new_list=NULL;
  576. if (router_parse_list_from_string(&s, &new_list, NULL, 0)) {
  577. log(LOG_WARN, "Error parsing router file");
  578. return -1;
  579. }
  580. if (trusted) {
  581. int i;
  582. if (!trusted_dir_digests)
  583. trusted_dir_digests = smartlist_create();
  584. for (i=0;i<smartlist_len(new_list->routers);++i) {
  585. routerinfo_t *r = smartlist_get(new_list->routers, i);
  586. if (r->dir_port) {
  587. char *b;
  588. log_fn(LOG_DEBUG,"Trusting router %s.", r->nickname);
  589. r->is_trusted_dir = 1;
  590. b = tor_malloc(DIGEST_LEN);
  591. memcpy(b, r->identity_digest, DIGEST_LEN);
  592. smartlist_add(trusted_dir_digests, b);
  593. }
  594. }
  595. }
  596. if (routerlist) {
  597. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  598. router_add_to_routerlist(r));
  599. smartlist_clear(new_list->routers);
  600. routerlist_free(new_list);
  601. } else {
  602. routerlist = new_list;
  603. }
  604. if (router_resolve_routerlist(routerlist)) {
  605. log(LOG_WARN, "Error resolving routerlist");
  606. return -1;
  607. }
  608. /* dump_onion_keys(LOG_NOTICE); */
  609. return 0;
  610. }
  611. /** Add to the current routerlist each router stored in the
  612. * signed directory <b>s</b>. If pkey is provided, check the signature against
  613. * pkey; else check against the pkey of the signing directory server. */
  614. int router_load_routerlist_from_directory(const char *s,
  615. crypto_pk_env_t *pkey)
  616. {
  617. routerlist_t *new_list = NULL;
  618. if (router_parse_routerlist_from_directory(s, &new_list, pkey)) {
  619. log_fn(LOG_WARN, "Couldn't parse directory.");
  620. return -1;
  621. }
  622. if (routerlist) {
  623. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  624. router_add_to_routerlist(r));
  625. smartlist_clear(new_list->routers);
  626. routerlist->published_on = new_list->published_on;
  627. tor_free(routerlist->software_versions);
  628. routerlist->software_versions = new_list->software_versions;
  629. new_list->software_versions = NULL;
  630. routerlist_free(new_list);
  631. } else {
  632. routerlist = new_list;
  633. }
  634. if (router_resolve_routerlist(routerlist)) {
  635. log_fn(LOG_WARN, "Error resolving routerlist");
  636. return -1;
  637. }
  638. if (options.AuthoritativeDir) {
  639. /* Learn about the descriptors in the directory. */
  640. dirserv_load_from_directory_string(s);
  641. } else {
  642. /* Remember the directory. */
  643. dirserv_set_cached_directory(s, routerlist->published_on);
  644. }
  645. return 0;
  646. }
  647. /** Helper function: resolve the hostname for <b>router</b>. */
  648. static int
  649. router_resolve(routerinfo_t *router)
  650. {
  651. if (tor_lookup_hostname(router->address, &router->addr) != 0
  652. || !router->addr) {
  653. log_fn(LOG_WARN,"Could not get address for router %s (%s).",
  654. router->address, router->nickname);
  655. return -1;
  656. }
  657. router->addr = ntohl(router->addr); /* get it back into host order */
  658. return 0;
  659. }
  660. /** Helper function: resolve every router in rl, and ensure that our own
  661. * routerinfo is at the front.
  662. */
  663. static int
  664. router_resolve_routerlist(routerlist_t *rl)
  665. {
  666. int i, remove;
  667. routerinfo_t *r;
  668. if (!rl)
  669. rl = routerlist;
  670. i = 0;
  671. if ((r = router_get_my_routerinfo())) {
  672. smartlist_insert(rl->routers, 0, routerinfo_copy(r));
  673. ++i;
  674. }
  675. for ( ; i < smartlist_len(rl->routers); ++i) {
  676. remove = 0;
  677. r = smartlist_get(rl->routers,i);
  678. if (router_is_me(r)) {
  679. remove = 1;
  680. } else if (r->addr) {
  681. /* already resolved. */
  682. } else if (router_resolve(r)) {
  683. log_fn(LOG_WARN, "Couldn't resolve router %s; not using", r->address);
  684. remove = 1;
  685. }
  686. if (remove) {
  687. routerinfo_free(r);
  688. smartlist_del_keeporder(rl->routers, i--);
  689. }
  690. }
  691. return 0;
  692. }
  693. /** Decide whether a given addr:port is definitely accepted, definitely
  694. * rejected, or neither by a given exit policy. If <b>addr</b> is 0, we
  695. * don't know the IP of the target address.
  696. *
  697. * Returns -1 for "rejected", 0 for "accepted", 1 for "maybe" (since IP is
  698. * unknown).
  699. */
  700. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  701. struct exit_policy_t *policy)
  702. {
  703. int maybe_reject = 0;
  704. int maybe_accept = 0;
  705. int match = 0;
  706. int maybe = 0;
  707. struct in_addr in;
  708. struct exit_policy_t *tmpe;
  709. for(tmpe=policy; tmpe; tmpe=tmpe->next) {
  710. // log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
  711. maybe = 0;
  712. if (!addr) {
  713. /* Address is unknown. */
  714. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  715. /* The port definitely matches. */
  716. if (tmpe->msk == 0) {
  717. match = 1;
  718. } else {
  719. maybe = 1;
  720. }
  721. } else if (!port) {
  722. /* The port maybe matches. */
  723. maybe = 1;
  724. }
  725. } else {
  726. /* Address is known */
  727. if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
  728. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  729. /* Exact match for the policy */
  730. match = 1;
  731. } else if (!port) {
  732. maybe = 1;
  733. }
  734. }
  735. }
  736. if (maybe) {
  737. if (tmpe->policy_type == EXIT_POLICY_REJECT)
  738. maybe_reject = 1;
  739. else
  740. maybe_accept = 1;
  741. }
  742. if (match) {
  743. in.s_addr = htonl(addr);
  744. log_fn(LOG_DEBUG,"Address %s:%d matches exit policy '%s'",
  745. inet_ntoa(in), port, tmpe->string);
  746. if(tmpe->policy_type == EXIT_POLICY_ACCEPT) {
  747. /* If we already hit a clause that might trigger a 'reject', than we
  748. * can't be sure of this certain 'accept'.*/
  749. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  750. } else {
  751. return maybe_accept ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_REJECTED;
  752. }
  753. }
  754. }
  755. /* accept all by default. */
  756. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  757. }
  758. /** Return 1 if all running routers will reject addr:port, return 0 if
  759. * any might accept it. */
  760. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port) {
  761. int i;
  762. routerinfo_t *router;
  763. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  764. router = smartlist_get(routerlist->routers, i);
  765. if (router->is_running && router_compare_addr_to_exit_policy(
  766. addr, port, router->exit_policy) != ADDR_POLICY_REJECTED)
  767. return 0; /* this one could be ok. good enough. */
  768. }
  769. return 1; /* all will reject. */
  770. }
  771. /** Return true iff <b>router</b> does not permit exit streams.
  772. */
  773. int router_exit_policy_rejects_all(routerinfo_t *router) {
  774. return router_compare_addr_to_exit_policy(0, 0, router->exit_policy)
  775. == ADDR_POLICY_REJECTED;
  776. }
  777. /** Release all space held in <b>rr</b>. */
  778. void running_routers_free(running_routers_t *rr)
  779. {
  780. tor_assert(rr);
  781. if (rr->running_routers) {
  782. SMARTLIST_FOREACH(rr->running_routers, char *, s, tor_free(s));
  783. smartlist_free(rr->running_routers);
  784. }
  785. tor_free(rr);
  786. }
  787. /** Update the running/not-running status of every router in <b>list</b>, based
  788. * on the contents of <b>rr</b>. */
  789. /* Note: this function is not yet used, since nobody publishes just
  790. * running-router lists yet. */
  791. void routerlist_update_from_runningrouters(routerlist_t *list,
  792. running_routers_t *rr)
  793. {
  794. int n_routers, i;
  795. routerinfo_t *router, *me = router_get_my_routerinfo();
  796. if (!list)
  797. return;
  798. if (list->published_on >= rr->published_on)
  799. return;
  800. if (list->running_routers_updated_on >= rr->published_on)
  801. return;
  802. if(me) { /* learn if the dirservers think I'm verified */
  803. router_update_status_from_smartlist(me,
  804. rr->published_on,
  805. rr->running_routers);
  806. }
  807. n_routers = smartlist_len(list->routers);
  808. for (i=0; i<n_routers; ++i) {
  809. router = smartlist_get(list->routers, i);
  810. router_update_status_from_smartlist(router,
  811. rr->published_on,
  812. rr->running_routers);
  813. }
  814. list->running_routers_updated_on = rr->published_on;
  815. }
  816. /** Update the is_running and is_verified fields of the router <b>router</b>,
  817. * based in its status in the list of strings stored in <b>running_list</b>.
  818. * All entries in <b>running_list</b> follow one of these formats:
  819. * <ol><li> <b>nickname</b> -- router is running and verified.
  820. * <li> !<b>nickname</b> -- router is not-running and verified.
  821. * <li> $<b>hexdigest</b> -- router is running and unverified.
  822. * <li> !$<b>hexdigest</b> -- router is not-running and unverified.
  823. * </ol>
  824. *
  825. * Return 1 if we found router in running_list, else return 0.
  826. */
  827. int router_update_status_from_smartlist(routerinfo_t *router,
  828. time_t list_time,
  829. smartlist_t *running_list)
  830. {
  831. int n_names, i, running, approved;
  832. const char *name;
  833. #if 1
  834. char *cp;
  835. int n;
  836. n = 0;
  837. for (i=0; i<smartlist_len(running_list); ++i) {
  838. name = smartlist_get(running_list, i);
  839. n += strlen(name) + 1;
  840. }
  841. cp = tor_malloc(n+2);
  842. cp[0] = '\0';
  843. for (i=0; i<smartlist_len(running_list); ++i) {
  844. name = smartlist_get(running_list, i);
  845. strlcat(cp, name, n);
  846. strlcat(cp, " ", n);
  847. }
  848. log_fn(LOG_DEBUG, "Updating status of %s from list \"%s\"",
  849. router->nickname, cp);
  850. tor_free(cp);
  851. #endif
  852. running = approved = 0;
  853. n_names = smartlist_len(running_list);
  854. for (i=0; i<n_names; ++i) {
  855. name = smartlist_get(running_list, i);
  856. if (*name != '!') {
  857. if (router_nickname_matches(router, name)) {
  858. if (router->status_set_at < list_time) {
  859. router->status_set_at = list_time;
  860. router->is_running = 1;
  861. }
  862. router->is_verified = (name[0] != '$');
  863. return 1;
  864. }
  865. } else { /* *name == '!' */
  866. name++;
  867. if (router_nickname_matches(router, name)) {
  868. if (router->status_set_at < list_time) {
  869. router->status_set_at = list_time;
  870. router->is_running = 0;
  871. }
  872. router->is_verified = (name[0] != '$');
  873. return 1;
  874. }
  875. }
  876. }
  877. return 0;
  878. }
  879. /*
  880. Local Variables:
  881. mode:c
  882. indent-tabs-mode:nil
  883. c-basic-offset:2
  884. End:
  885. */