routerlist.c 37 KB

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