routerlist.c 33 KB

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