routerlist.c 37 KB

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