routerlist.c 33 KB

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