routerlist.c 31 KB

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