routerlist.c 38 KB

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