routerlist.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char routerlist_c_id[] = "$Id$";
  7. #include "or.h"
  8. /**
  9. * \file routerlist.c
  10. *
  11. * \brief Code to
  12. * maintain and access the global list of routerinfos for known
  13. * servers.
  14. **/
  15. /****************************************************************************/
  16. static smartlist_t *trusted_dir_servers = NULL;
  17. /* static function prototypes */
  18. static routerinfo_t *
  19. router_pick_directory_server_impl(int requireothers, int fascistfirewall,
  20. int for_runningrouters);
  21. static trusted_dir_server_t *
  22. router_pick_trusteddirserver_impl(int requireother, int fascistfirewall);
  23. static void mark_all_trusteddirservers_up(void);
  24. static int router_resolve(routerinfo_t *router);
  25. static int router_resolve_routerlist(routerlist_t *dir);
  26. /****************************************************************************/
  27. /****
  28. * Functions to manage and access our list of known routers. (Note:
  29. * dirservers maintain a separate, independent list of known router
  30. * descriptors.)
  31. *****/
  32. /** Global list of all of the routers that we, as an OR or OP, know about. */
  33. static routerlist_t *routerlist = NULL;
  34. extern int has_fetched_directory; /**< from main.c */
  35. /**
  36. * Reload the original list of trusted dirservers, and the most recent
  37. * cached directory (if present).
  38. */
  39. int router_reload_router_list(void)
  40. {
  41. char filename[512];
  42. int is_recent;
  43. struct stat st;
  44. char *s;
  45. tor_assert(get_options()->DataDirectory);
  46. tor_snprintf(filename,sizeof(filename),"%s/cached-directory",
  47. get_options()->DataDirectory);
  48. s = read_file_to_str(filename,0);
  49. if (s) {
  50. stat(filename, &st); /* if s is true, stat probably worked */
  51. log_fn(LOG_INFO, "Loading cached directory from %s", filename);
  52. is_recent = st.st_mtime > time(NULL) - 60*15;
  53. if (router_load_routerlist_from_directory(s, NULL, is_recent, 1) < 0) {
  54. log_fn(LOG_WARN, "Cached directory at '%s' was unparseable; ignoring.", filename);
  55. }
  56. if (routerlist &&
  57. ((routerlist->published_on > time(NULL) - MIN_ONION_KEY_LIFETIME/2)
  58. || is_recent)) {
  59. directory_has_arrived(st.st_mtime, NULL); /* do things we've been waiting to do */
  60. }
  61. tor_free(s);
  62. }
  63. return 0;
  64. }
  65. /* Set *<b>outp</b> to a smartlist containing a list of
  66. * trusted_dir_server_t * for all known trusted dirservers. Callers
  67. * must not modify the list or its contents.
  68. */
  69. void router_get_trusted_dir_servers(smartlist_t **outp)
  70. {
  71. if (!trusted_dir_servers)
  72. trusted_dir_servers = smartlist_create();
  73. *outp = trusted_dir_servers;
  74. }
  75. /** Try to find a running dirserver. If there are no running dirservers
  76. * in our routerlist, set all the authoritative ones as running again,
  77. * and pick one. If there are no dirservers at all in our routerlist,
  78. * reload the routerlist and try one last time. If for_runningrouters is
  79. * true, then only pick a dirserver that can answer runningrouters queries
  80. * (that is, a trusted dirserver, or one running 0.0.9rc5-cvs or later).
  81. */
  82. routerinfo_t *router_pick_directory_server(int requireothers,
  83. int fascistfirewall,
  84. int for_runningrouters,
  85. int retry_if_no_servers) {
  86. routerinfo_t *choice;
  87. if (!routerlist)
  88. return NULL;
  89. choice = router_pick_directory_server_impl(requireothers, fascistfirewall,
  90. for_runningrouters);
  91. if (choice || !retry_if_no_servers)
  92. return choice;
  93. log_fn(LOG_INFO,"No reachable router entries for dirservers. Trying them all again.");
  94. /* mark all authdirservers as up again */
  95. mark_all_trusteddirservers_up();
  96. /* try again */
  97. choice = router_pick_directory_server_impl(requireothers, fascistfirewall,
  98. for_runningrouters);
  99. if (choice)
  100. return choice;
  101. log_fn(LOG_INFO,"Still no %s router entries. Reloading and trying again.",
  102. get_options()->FascistFirewall ? "reachable" : "known");
  103. has_fetched_directory=0; /* reset it */
  104. if (router_reload_router_list()) {
  105. return NULL;
  106. }
  107. /* give it one last try */
  108. choice = router_pick_directory_server_impl(requireothers, 0,
  109. for_runningrouters);
  110. return choice;
  111. }
  112. trusted_dir_server_t *router_pick_trusteddirserver(int requireothers,
  113. int fascistfirewall,
  114. int retry_if_no_servers) {
  115. trusted_dir_server_t *choice;
  116. choice = router_pick_trusteddirserver_impl(requireothers, fascistfirewall);
  117. if (choice || !retry_if_no_servers)
  118. return choice;
  119. log_fn(LOG_INFO,"No trusted dirservers are reachable. Trying them all again.");
  120. /* mark all authdirservers as up again */
  121. mark_all_trusteddirservers_up();
  122. /* try again */
  123. choice = router_pick_trusteddirserver_impl(requireothers, fascistfirewall);
  124. if (choice)
  125. return choice;
  126. log_fn(LOG_WARN,"Still no dirservers %s. Reloading and trying again.",
  127. get_options()->FascistFirewall ? "reachable" : "known");
  128. has_fetched_directory=0; /* reset it */
  129. if (router_reload_router_list()) {
  130. return NULL;
  131. }
  132. /* give it one last try */
  133. choice = router_pick_trusteddirserver_impl(requireothers, 0);
  134. return choice;
  135. }
  136. /** Pick a random running router from our routerlist. If requireauth,
  137. * it has to be a trusted server. If requireothers, it cannot be us.
  138. */
  139. static routerinfo_t *
  140. router_pick_directory_server_impl(int requireothers, int fascistfirewall,
  141. int for_runningrouters)
  142. {
  143. int i;
  144. routerinfo_t *router;
  145. smartlist_t *sl;
  146. if (!routerlist)
  147. return NULL;
  148. if (get_options()->HttpProxy)
  149. fascistfirewall = 0;
  150. /* Find all the running dirservers we know about. */
  151. sl = smartlist_create();
  152. for (i=0;i < smartlist_len(routerlist->routers); i++) {
  153. router = smartlist_get(routerlist->routers, i);
  154. if (!router->is_running || !router->dir_port || !router->is_verified)
  155. continue;
  156. if (requireothers && router_is_me(router))
  157. continue;
  158. if (fascistfirewall) {
  159. if (!smartlist_string_num_isin(get_options()->FirewallPorts, router->dir_port))
  160. continue;
  161. }
  162. /* before 0.0.9rc5-cvs, only trusted dirservers served status info. */
  163. if (for_runningrouters &&
  164. !(tor_version_as_new_as(router->platform,"0.0.9rc5-cvs") ||
  165. router_digest_is_trusted_dir(router->identity_digest)))
  166. continue;
  167. smartlist_add(sl, router);
  168. }
  169. router = smartlist_choose(sl);
  170. smartlist_free(sl);
  171. return router;
  172. }
  173. static trusted_dir_server_t *
  174. router_pick_trusteddirserver_impl(int requireother, int fascistfirewall)
  175. {
  176. smartlist_t *sl;
  177. routerinfo_t *me;
  178. trusted_dir_server_t *ds;
  179. sl = smartlist_create();
  180. me = router_get_my_routerinfo();
  181. if (!trusted_dir_servers)
  182. return NULL;
  183. if (get_options()->HttpProxy)
  184. fascistfirewall = 0;
  185. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
  186. {
  187. if (!d->is_running) continue;
  188. if (requireother && me &&
  189. !memcmp(me->identity_digest, d->digest, DIGEST_LEN))
  190. continue;
  191. if (fascistfirewall) {
  192. if (!smartlist_string_num_isin(get_options()->FirewallPorts, d->dir_port))
  193. continue;
  194. }
  195. smartlist_add(sl, d);
  196. });
  197. ds = smartlist_choose(sl);
  198. smartlist_free(sl);
  199. return ds;
  200. }
  201. /** Go through and mark the auth dirservers as up */
  202. static void mark_all_trusteddirservers_up(void) {
  203. if (routerlist) {
  204. SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
  205. if (router_digest_is_trusted_dir(router->identity_digest)) {
  206. tor_assert(router->dir_port > 0);
  207. router->is_running = 1;
  208. router->status_set_at = time(NULL);
  209. });
  210. }
  211. if (trusted_dir_servers) {
  212. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, dir,
  213. dir->is_running = 1);
  214. }
  215. }
  216. /** Return 0 if \exists an authoritative dirserver that's currently
  217. * thought to be running, else return 1.
  218. */
  219. int all_trusted_directory_servers_down(void) {
  220. if (!trusted_dir_servers)
  221. return 1;
  222. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, dir,
  223. if (dir->is_running) return 0);
  224. return 1;
  225. }
  226. /** Add all the family of <b>router</b> to the smartlist <b>sl</b>.
  227. */
  228. void routerlist_add_family(smartlist_t *sl, routerinfo_t *router) {
  229. routerinfo_t *r;
  230. struct config_line_t *cl;
  231. if (!router->declared_family)
  232. return;
  233. /* Add every r such that router declares familyness with r, and r
  234. * declares familyhood with router. */
  235. SMARTLIST_FOREACH(router->declared_family, const char *, n,
  236. {
  237. if (!(r = router_get_by_nickname(n)))
  238. continue;
  239. if (!r->declared_family)
  240. continue;
  241. SMARTLIST_FOREACH(r->declared_family, const char *, n2,
  242. {
  243. if (router_nickname_matches(router, n2))
  244. smartlist_add(sl, r);
  245. });
  246. });
  247. for (cl = get_options()->NodeFamilies; cl; cl = cl->next) {
  248. if (router_nickname_is_in_list(router, cl->value)) {
  249. add_nickname_list_to_smartlist(sl, cl->value, 0);
  250. }
  251. }
  252. }
  253. /** List of string for nicknames we've warned about and haven't yet succeeded.
  254. */
  255. static smartlist_t *warned_nicknames = NULL;
  256. /** Given a comma-and-whitespace separated list of nicknames, see which
  257. * nicknames in <b>list</b> name routers in our routerlist that are
  258. * currently running. Add the routerinfos for those routers to <b>sl</b>.
  259. */
  260. void
  261. add_nickname_list_to_smartlist(smartlist_t *sl, const char *list, int warn_if_down)
  262. {
  263. routerinfo_t *router;
  264. smartlist_t *nickname_list;
  265. if (!list)
  266. return; /* nothing to do */
  267. tor_assert(sl);
  268. nickname_list = smartlist_create();
  269. if (!warned_nicknames)
  270. warned_nicknames = smartlist_create();
  271. smartlist_split_string(nickname_list, list, ",",
  272. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  273. SMARTLIST_FOREACH(nickname_list, const char *, nick, {
  274. int warned;
  275. if (!is_legal_nickname_or_hexdigest(nick)) {
  276. log_fn(LOG_WARN,"Nickname %s is misformed; skipping", nick);
  277. continue;
  278. }
  279. router = router_get_by_nickname(nick);
  280. warned = smartlist_string_isin(warned_nicknames, nick);
  281. if (router) {
  282. if (router->is_running) {
  283. smartlist_add(sl,router);
  284. if (warned)
  285. smartlist_string_remove(warned_nicknames, nick);
  286. } else {
  287. if (!warned) {
  288. log_fn(warn_if_down ? LOG_WARN : LOG_DEBUG,
  289. "Nickname list includes '%s' which is known but down.",nick);
  290. smartlist_add(warned_nicknames, tor_strdup(nick));
  291. }
  292. }
  293. } else {
  294. if (!warned) {
  295. log_fn(has_fetched_directory ? LOG_WARN : LOG_INFO,
  296. "Nickname list includes '%s' which isn't a known router.",nick);
  297. smartlist_add(warned_nicknames, tor_strdup(nick));
  298. }
  299. }
  300. });
  301. SMARTLIST_FOREACH(nickname_list, char *, nick, tor_free(nick));
  302. smartlist_free(nickname_list);
  303. }
  304. /** Return 1 iff any member of the comma-separated list <b>list</b> is an
  305. * acceptable nickname or hexdigest for <b>router</b>. Else return 0.
  306. */
  307. int
  308. router_nickname_is_in_list(routerinfo_t *router, const char *list)
  309. {
  310. smartlist_t *nickname_list;
  311. int v = 0;
  312. if (!list)
  313. return 0; /* definitely not */
  314. tor_assert(router);
  315. nickname_list = smartlist_create();
  316. smartlist_split_string(nickname_list, list, ",",
  317. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  318. SMARTLIST_FOREACH(nickname_list, const char *, cp,
  319. if (router_nickname_matches(router, cp)) {v=1;break;});
  320. SMARTLIST_FOREACH(nickname_list, char *, cp, tor_free(cp));
  321. smartlist_free(nickname_list);
  322. return v;
  323. }
  324. /** Add every router from our routerlist that is currently running to
  325. * <b>sl</b>.
  326. */
  327. static void
  328. router_add_running_routers_to_smartlist(smartlist_t *sl, int allow_unverified,
  329. int need_uptime, int need_capacity)
  330. {
  331. routerinfo_t *router;
  332. int i;
  333. if (!routerlist)
  334. return;
  335. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  336. router = smartlist_get(routerlist->routers, i);
  337. if (router->is_running &&
  338. (router->is_verified ||
  339. (allow_unverified &&
  340. !router_is_unreliable(router, need_uptime, need_capacity)))) {
  341. /* If it's running, and either it's verified or we're ok picking
  342. * unverified routers and this one is suitable.
  343. */
  344. smartlist_add(sl, router);
  345. }
  346. }
  347. }
  348. routerinfo_t *
  349. routerlist_find_my_routerinfo(void) {
  350. routerinfo_t *router;
  351. int i;
  352. if (!routerlist)
  353. return NULL;
  354. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  355. router = smartlist_get(routerlist->routers, i);
  356. if (router_is_me(router))
  357. return router;
  358. }
  359. return NULL;
  360. }
  361. int
  362. router_is_unreliable(routerinfo_t *router, int need_uptime, int need_capacity)
  363. {
  364. if (need_uptime && router->uptime < ROUTER_REQUIRED_MIN_UPTIME)
  365. return 1;
  366. if (need_capacity && router->bandwidthcapacity < ROUTER_REQUIRED_MIN_BANDWIDTH)
  367. return 1;
  368. return 0;
  369. }
  370. static void
  371. routerlist_sl_remove_unreliable_routers(smartlist_t *sl)
  372. {
  373. int i;
  374. routerinfo_t *router;
  375. for (i = 0; i < smartlist_len(sl); ++i) {
  376. router = smartlist_get(sl, i);
  377. if (router_is_unreliable(router, 1, 0)) {
  378. log(LOG_DEBUG, "Router '%s' has insufficient uptime; deleting.",
  379. router->nickname);
  380. smartlist_del(sl, i--);
  381. }
  382. }
  383. }
  384. routerinfo_t *
  385. routerlist_sl_choose_by_bandwidth(smartlist_t *sl)
  386. {
  387. int i;
  388. routerinfo_t *router;
  389. smartlist_t *bandwidths;
  390. uint32_t this_bw, tmp, total_bw=0, rand_bw;
  391. uint32_t *p;
  392. bandwidths = smartlist_create();
  393. for (i = 0; i < smartlist_len(sl); ++i) {
  394. router = smartlist_get(sl, i);
  395. this_bw = (router->bandwidthcapacity < router->bandwidthrate) ?
  396. router->bandwidthcapacity : router->bandwidthrate;
  397. if (this_bw > 2000000)
  398. this_bw = 2000000; /* if they claim something huge, don't believe it */
  399. p = tor_malloc(sizeof(uint32_t));
  400. *p = this_bw;
  401. smartlist_add(bandwidths, p);
  402. total_bw += this_bw;
  403. // log_fn(LOG_INFO,"Recording bw %d for node %s.", this_bw, router->nickname);
  404. }
  405. if (!total_bw) {
  406. SMARTLIST_FOREACH(bandwidths, uint32_t*, p, tor_free(p));
  407. smartlist_free(bandwidths);
  408. return smartlist_choose(sl);
  409. }
  410. rand_bw = crypto_pseudo_rand_int(total_bw);
  411. // log_fn(LOG_INFO,"Total bw %d. Randomly chose %d.", total_bw, rand_bw);
  412. tmp = 0;
  413. for (i=0; ; i++) {
  414. tor_assert(i < smartlist_len(sl));
  415. p = smartlist_get(bandwidths, i);
  416. tmp += *p;
  417. router = smartlist_get(sl, i);
  418. // log_fn(LOG_INFO,"Considering %s. tmp = %d.", router->nickname, tmp);
  419. if (tmp >= rand_bw)
  420. break;
  421. }
  422. SMARTLIST_FOREACH(bandwidths, uint32_t*, p, tor_free(p));
  423. smartlist_free(bandwidths);
  424. router = smartlist_get(sl, i);
  425. // log_fn(LOG_INFO,"Picked %s.", router->nickname);
  426. return router;
  427. }
  428. /** Return a random running router from the routerlist. If any node
  429. * named in <b>preferred</b> is available, pick one of those. Never
  430. * pick a node named in <b>excluded</b>, or whose routerinfo is in
  431. * <b>excludedsmartlist</b>, even if they are the only nodes
  432. * available. If <b>strict</b> is true, never pick any node besides
  433. * those in <b>preferred</b>.
  434. */
  435. routerinfo_t *router_choose_random_node(const char *preferred,
  436. const char *excluded,
  437. smartlist_t *excludedsmartlist,
  438. int need_uptime, int need_capacity,
  439. int allow_unverified, int strict)
  440. {
  441. smartlist_t *sl, *excludednodes;
  442. routerinfo_t *choice;
  443. excludednodes = smartlist_create();
  444. add_nickname_list_to_smartlist(excludednodes,excluded,0);
  445. /* Try the preferred nodes first. Ignore need_uptime and need_capacity,
  446. * since the user explicitly asked for these nodes. */
  447. sl = smartlist_create();
  448. add_nickname_list_to_smartlist(sl,preferred,1);
  449. smartlist_subtract(sl,excludednodes);
  450. if (excludedsmartlist)
  451. smartlist_subtract(sl,excludedsmartlist);
  452. #if 0
  453. if (need_uptime)
  454. routerlist_sl_remove_unreliable_routers(sl);
  455. if (need_capacity)
  456. choice = routerlist_sl_choose_by_bandwidth(sl);
  457. else
  458. #endif
  459. choice = smartlist_choose(sl);
  460. smartlist_free(sl);
  461. if (!choice && !strict) {
  462. /* Then give up on our preferred choices: any node
  463. * will do that has the required attributes. */
  464. sl = smartlist_create();
  465. router_add_running_routers_to_smartlist(sl, allow_unverified,
  466. need_uptime, need_capacity);
  467. smartlist_subtract(sl,excludednodes);
  468. if (excludedsmartlist)
  469. smartlist_subtract(sl,excludedsmartlist);
  470. if (need_uptime)
  471. routerlist_sl_remove_unreliable_routers(sl);
  472. if (need_capacity)
  473. choice = routerlist_sl_choose_by_bandwidth(sl);
  474. else
  475. choice = smartlist_choose(sl);
  476. smartlist_free(sl);
  477. }
  478. smartlist_free(excludednodes);
  479. if (!choice)
  480. log_fn(LOG_WARN,"No available nodes when trying to choose node. Failing.");
  481. return choice;
  482. }
  483. /** Return the router in our routerlist whose address is <b>addr</b> and
  484. * whose OR port is <b>port</b>. Return NULL if no such router is known.
  485. */
  486. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  487. int i;
  488. routerinfo_t *router;
  489. if (!routerlist)
  490. return NULL;
  491. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  492. router = smartlist_get(routerlist->routers, i);
  493. if ((router->addr == addr) && (router->or_port == port))
  494. return router;
  495. }
  496. return NULL;
  497. }
  498. /** Return true iff the digest of <b>router</b>'s identity key,
  499. * encoded in hexadecimal, matches <b>hexdigest</b> (which is
  500. * optionally prefixed with a single dollar sign). Return false if
  501. * <b>hexdigest</b> is malformed, or it doesn't match. */
  502. static INLINE int router_hex_digest_matches(routerinfo_t *router,
  503. const char *hexdigest)
  504. {
  505. char digest[DIGEST_LEN];
  506. tor_assert(hexdigest);
  507. if (hexdigest[0] == '$')
  508. ++hexdigest;
  509. if (strlen(hexdigest) != HEX_DIGEST_LEN ||
  510. base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
  511. return 0;
  512. else
  513. return (!memcmp(digest, router->identity_digest, DIGEST_LEN));
  514. }
  515. /* Return true if <b>router</b>'s nickname matches <b>nickname</b>
  516. * (case-insensitive), or if <b>router's</b> identity key digest
  517. * matches a hexadecimal value stored in <b>nickname</b>. Return
  518. * false otherwise.*/
  519. int router_nickname_matches(routerinfo_t *router, const char *nickname)
  520. {
  521. if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
  522. return 1;
  523. else
  524. return router_hex_digest_matches(router, nickname);
  525. }
  526. /** Return the router in our routerlist whose (case-insensitive)
  527. * nickname or (case-sensitive) hexadecimal key digest is
  528. * <b>nickname</b>. Return NULL if no such router is known.
  529. */
  530. routerinfo_t *router_get_by_nickname(const char *nickname)
  531. {
  532. int i, maybedigest;
  533. routerinfo_t *router;
  534. char digest[DIGEST_LEN];
  535. tor_assert(nickname);
  536. if (!routerlist)
  537. return NULL;
  538. if (nickname[0] == '$')
  539. return router_get_by_hexdigest(nickname);
  540. if (server_mode(get_options()) &&
  541. !strcasecmp(nickname, get_options()->Nickname))
  542. return router_get_my_routerinfo();
  543. maybedigest = (strlen(nickname) == HEX_DIGEST_LEN) &&
  544. (base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);
  545. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  546. router = smartlist_get(routerlist->routers, i);
  547. if (0 == strcasecmp(router->nickname, nickname) ||
  548. (maybedigest && 0 == memcmp(digest, router->identity_digest,
  549. DIGEST_LEN)))
  550. return router;
  551. }
  552. return NULL;
  553. }
  554. /** Return true iff <b>digest</b> is the digest of the identity key of
  555. * a trusted directory. */
  556. int router_digest_is_trusted_dir(const char *digest) {
  557. if (!trusted_dir_servers)
  558. return 0;
  559. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
  560. if (!memcmp(digest, ent->digest, DIGEST_LEN)) return 1);
  561. return 0;
  562. }
  563. /** Return the router in our routerlist whose hexadecimal key digest
  564. * is <b>hexdigest</b>. Return NULL if no such router is known. */
  565. routerinfo_t *router_get_by_hexdigest(const char *hexdigest) {
  566. char digest[DIGEST_LEN];
  567. tor_assert(hexdigest);
  568. if (!routerlist)
  569. return NULL;
  570. if (hexdigest[0]=='$')
  571. ++hexdigest;
  572. if (strlen(hexdigest) != HEX_DIGEST_LEN ||
  573. base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
  574. return NULL;
  575. return router_get_by_digest(digest);
  576. }
  577. /** Return the router in our routerlist whose 20-byte key digest
  578. * is <b>digest</b>. Return NULL if no such router is known. */
  579. routerinfo_t *router_get_by_digest(const char *digest) {
  580. int i;
  581. routerinfo_t *router;
  582. tor_assert(digest);
  583. if (server_mode(get_options()) &&
  584. (router = router_get_my_routerinfo()) &&
  585. !memcmp(digest, router->identity_digest, DIGEST_LEN))
  586. return router;
  587. if (!routerlist) return NULL;
  588. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  589. router = smartlist_get(routerlist->routers, i);
  590. if (0 == memcmp(router->identity_digest, digest, DIGEST_LEN))
  591. return router;
  592. }
  593. return NULL;
  594. }
  595. /** Set *<b>prouterlist</b> to the current list of all known routers. */
  596. void router_get_routerlist(routerlist_t **prouterlist) {
  597. *prouterlist = routerlist;
  598. }
  599. /** Return the publication time on the current routerlist, or 0 if we have no
  600. * routerlist. */
  601. time_t routerlist_get_published_time(void) {
  602. return routerlist ? routerlist->published_on : 0;
  603. }
  604. /** Free all storage held by <b>router</b>. */
  605. void routerinfo_free(routerinfo_t *router)
  606. {
  607. if (!router)
  608. return;
  609. tor_free(router->signed_descriptor);
  610. tor_free(router->address);
  611. tor_free(router->nickname);
  612. tor_free(router->platform);
  613. tor_free(router->contact_info);
  614. if (router->onion_pkey)
  615. crypto_free_pk_env(router->onion_pkey);
  616. if (router->identity_pkey)
  617. crypto_free_pk_env(router->identity_pkey);
  618. if (router->declared_family) {
  619. SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
  620. smartlist_free(router->declared_family);
  621. }
  622. addr_policy_free(router->exit_policy);
  623. tor_free(router);
  624. }
  625. /** Allocate a fresh copy of <b>router</b> */
  626. routerinfo_t *routerinfo_copy(const routerinfo_t *router)
  627. {
  628. routerinfo_t *r;
  629. addr_policy_t **e, *tmp;
  630. r = tor_malloc(sizeof(routerinfo_t));
  631. memcpy(r, router, sizeof(routerinfo_t));
  632. r->address = tor_strdup(r->address);
  633. r->nickname = tor_strdup(r->nickname);
  634. r->platform = tor_strdup(r->platform);
  635. if (r->signed_descriptor)
  636. r->signed_descriptor = tor_strdup(r->signed_descriptor);
  637. if (r->onion_pkey)
  638. r->onion_pkey = crypto_pk_dup_key(r->onion_pkey);
  639. if (r->identity_pkey)
  640. r->identity_pkey = crypto_pk_dup_key(r->identity_pkey);
  641. e = &r->exit_policy;
  642. while (*e) {
  643. tmp = tor_malloc(sizeof(addr_policy_t));
  644. memcpy(tmp,*e,sizeof(addr_policy_t));
  645. *e = tmp;
  646. (*e)->string = tor_strdup((*e)->string);
  647. e = & ((*e)->next);
  648. }
  649. if (r->declared_family) {
  650. r->declared_family = smartlist_create();
  651. SMARTLIST_FOREACH(router->declared_family, const char *, s,
  652. smartlist_add(r->declared_family, tor_strdup(s)));
  653. }
  654. return r;
  655. }
  656. /** Free all storage held by a routerlist <b>rl</b> */
  657. void routerlist_free(routerlist_t *rl)
  658. {
  659. tor_assert(rl);
  660. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
  661. routerinfo_free(r));
  662. smartlist_free(rl->routers);
  663. running_routers_free(rl->running_routers);
  664. tor_free(rl->software_versions);
  665. tor_free(rl);
  666. }
  667. void routerlist_free_current(void)
  668. {
  669. if (routerlist)
  670. routerlist_free(routerlist);
  671. routerlist = NULL;
  672. if (warned_nicknames) {
  673. SMARTLIST_FOREACH(warned_nicknames, char *, cp, tor_free(cp));
  674. smartlist_free(warned_nicknames);
  675. warned_nicknames = NULL;
  676. }
  677. }
  678. void free_trusted_dir_servers(void)
  679. {
  680. if (trusted_dir_servers) {
  681. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
  682. { tor_free(ds->address); tor_free(ds); });
  683. smartlist_free(trusted_dir_servers);
  684. trusted_dir_servers = NULL;
  685. }
  686. }
  687. /** Mark the router with ID <b>digest</b> as non-running in our routerlist. */
  688. void router_mark_as_down(const char *digest) {
  689. routerinfo_t *router;
  690. tor_assert(digest);
  691. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
  692. if (!memcmp(d->digest, digest, DIGEST_LEN))
  693. d->is_running = 0);
  694. router = router_get_by_digest(digest);
  695. if (!router) /* we don't seem to know about him in the first place */
  696. return;
  697. log_fn(LOG_DEBUG,"Marking router '%s' as down.",router->nickname);
  698. if (router_is_me(router))
  699. log_fn(LOG_WARN, "We just marked ourself as down. Are your external addresses reachable?");
  700. router->is_running = 0;
  701. router->status_set_at = time(NULL);
  702. }
  703. /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
  704. * older entries (if any) with the same name. Note: Callers should not hold
  705. * their pointers to <b>router</b> after invoking this function; <b>router</b>
  706. * will either be inserted into the routerlist or freed. Returns 0 if the
  707. * router was added; -1 if it was not.
  708. *
  709. * DOCDOC msg
  710. */
  711. static int
  712. router_add_to_routerlist(routerinfo_t *router, const char **msg) {
  713. int i;
  714. routerinfo_t *r;
  715. char id_digest[DIGEST_LEN];
  716. tor_assert(routerlist);
  717. crypto_pk_get_digest(router->identity_pkey, id_digest);
  718. /* If we have a router with this name, and the identity key is the same,
  719. * choose the newer one. If the identity key has changed, drop the router.
  720. */
  721. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  722. r = smartlist_get(routerlist->routers, i);
  723. if (!crypto_pk_cmp_keys(router->identity_pkey, r->identity_pkey)) {
  724. if (router->published_on > r->published_on) {
  725. log_fn(LOG_DEBUG, "Replacing entry for router '%s/%s' [%s]",
  726. router->nickname, r->nickname, hex_str(id_digest,DIGEST_LEN));
  727. /* Remember whether we trust this router as a dirserver. */
  728. /* If the address hasn't changed; no need to re-resolve. */
  729. if (!strcasecmp(r->address, router->address))
  730. router->addr = r->addr;
  731. routerinfo_free(r);
  732. smartlist_set(routerlist->routers, i, router);
  733. return 0;
  734. } else {
  735. log_fn(LOG_DEBUG, "Skipping not-new descriptor for router '%s'",
  736. router->nickname);
  737. /* Update the is_running status to whatever we were told. */
  738. r->is_running = router->is_running;
  739. routerinfo_free(router);
  740. if (msg) *msg = "Router descriptor was not new.";
  741. return -1;
  742. }
  743. } else if (!strcasecmp(router->nickname, r->nickname)) {
  744. /* nicknames match, keys don't. */
  745. if (router->is_verified) {
  746. /* The new verified router replaces the old one; remove the
  747. * old one. And carry on to the end of the list, in case
  748. * there are more old unverified routers with this nickname
  749. */
  750. /* mark-for-close connections using the old key, so we can
  751. * make new ones with the new key.
  752. */
  753. connection_t *conn;
  754. while ((conn = connection_get_by_identity_digest(r->identity_digest,
  755. CONN_TYPE_OR))) {
  756. log_fn(LOG_INFO,"Closing conn to obsolete router '%s'", r->nickname);
  757. connection_mark_for_close(conn);
  758. }
  759. routerinfo_free(r);
  760. smartlist_del_keeporder(routerlist->routers, i--);
  761. } else if (r->is_verified) {
  762. /* Can't replace a verified router with an unverified one. */
  763. log_fn(LOG_DEBUG, "Skipping unverified entry for verified router '%s'",
  764. router->nickname);
  765. routerinfo_free(router);
  766. if (msg) *msg = "Already have verified router with different key and same nickname";
  767. return -1;
  768. }
  769. }
  770. }
  771. /* We haven't seen a router with this name before. Add it to the end of
  772. * the list. */
  773. smartlist_add(routerlist->routers, router);
  774. return 0;
  775. }
  776. /** Remove any routers from the routerlist that are more than <b>age</b>
  777. * seconds old.
  778. *
  779. * (This function is just like dirserv_remove_old_servers. One day we should
  780. * merge them.)
  781. */
  782. void
  783. routerlist_remove_old_routers(int age)
  784. {
  785. int i;
  786. time_t cutoff;
  787. routerinfo_t *router;
  788. if (!routerlist)
  789. return;
  790. cutoff = time(NULL) - age;
  791. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  792. router = smartlist_get(routerlist->routers, i);
  793. if (router->published_on <= cutoff) {
  794. /* Too old. Remove it. */
  795. log_fn(LOG_INFO,"Forgetting obsolete routerinfo for router '%s'", router->nickname);
  796. routerinfo_free(router);
  797. smartlist_del(routerlist->routers, i--);
  798. }
  799. }
  800. }
  801. /*
  802. * Code to parse a single router descriptors and insert it into the
  803. * directory. Return -1 if the descriptor was ill-formed; 0 if the
  804. * descriptor was well-formed but could not be added; and 1 if the
  805. * descriptor was added.
  806. */
  807. int
  808. router_load_single_router(const char *s, const char **msg)
  809. {
  810. routerinfo_t *ri;
  811. if (!(ri = router_parse_entry_from_string(s, NULL))) {
  812. log_fn(LOG_WARN, "Error parsing router descriptor; dropping.");
  813. if (msg) *msg = "Couldn't parse router descriptor";
  814. return -1;
  815. }
  816. if (routerlist && routerlist->running_routers) {
  817. running_routers_t *rr = routerlist->running_routers;
  818. router_update_status_from_smartlist(ri,
  819. rr->published_on,
  820. rr->running_routers);
  821. }
  822. if (router_add_to_routerlist(ri, msg)<0) {
  823. log_fn(LOG_WARN, "Couldn't add router to list; dropping.");
  824. if (msg && !*msg) *msg = "Couldn't add router to list.";
  825. return 0;
  826. } else {
  827. smartlist_t *changed = smartlist_create();
  828. smartlist_add(changed, ri);
  829. control_event_descriptors_changed(changed);
  830. smartlist_free(changed);
  831. }
  832. if (router_resolve(ri)<0) {
  833. if (msg && !*msg) *msg = "Couldn't resolve router address.";
  834. return 0;
  835. }
  836. log_fn(LOG_DEBUG, "Added router to list");
  837. return 1;
  838. }
  839. /** Add to the current routerlist each router stored in the
  840. * signed directory <b>s</b>. If pkey is provided, check the signature against
  841. * pkey; else check against the pkey of the signing directory server.
  842. *
  843. * If <b>dir_is_recent</b> is non-zero, then examine the
  844. * Recommended-versions line and take appropriate action.
  845. *
  846. * If <b>dir_is_cached</b> is non-zero, then we're reading it
  847. * from the cache so don't bother to re-write it to the cache.
  848. */
  849. int router_load_routerlist_from_directory(const char *s,
  850. crypto_pk_env_t *pkey,
  851. int dir_is_recent,
  852. int dir_is_cached)
  853. {
  854. routerlist_t *new_list = NULL;
  855. if (router_parse_routerlist_from_directory(s, &new_list, pkey,
  856. dir_is_recent,
  857. !dir_is_cached)) {
  858. log_fn(LOG_WARN, "Couldn't parse directory.");
  859. return -1;
  860. }
  861. if (routerlist) {
  862. smartlist_t *changed = smartlist_create();
  863. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  864. {
  865. if (router_add_to_routerlist(r,NULL)==0)
  866. smartlist_add(changed, r);
  867. });
  868. smartlist_clear(new_list->routers);
  869. routerlist->published_on = new_list->published_on;
  870. tor_free(routerlist->software_versions);
  871. routerlist->software_versions = new_list->software_versions;
  872. new_list->software_versions = NULL;
  873. routerlist_free(new_list);
  874. control_event_descriptors_changed(changed);
  875. smartlist_free(changed);
  876. } else {
  877. routerlist = new_list;
  878. control_event_descriptors_changed(routerlist->routers);
  879. }
  880. if (router_resolve_routerlist(routerlist)) {
  881. log_fn(LOG_WARN, "Error resolving routerlist");
  882. return -1;
  883. }
  884. if (get_options()->AuthoritativeDir) {
  885. /* Learn about the descriptors in the directory. */
  886. dirserv_load_from_directory_string(s);
  887. }
  888. return 0;
  889. }
  890. /** Helper function: resolve the hostname for <b>router</b>. */
  891. static int
  892. router_resolve(routerinfo_t *router)
  893. {
  894. if (tor_lookup_hostname(router->address, &router->addr) != 0
  895. || !router->addr) {
  896. log_fn(LOG_WARN,"Could not resolve address for router '%s' at %s",
  897. router->nickname, router->address);
  898. return -1;
  899. }
  900. router->addr = ntohl(router->addr); /* get it back into host order */
  901. return 0;
  902. }
  903. /** Helper function: resolve every router in rl, and ensure that our own
  904. * routerinfo is at the front.
  905. */
  906. static int
  907. router_resolve_routerlist(routerlist_t *rl)
  908. {
  909. int i, remove;
  910. routerinfo_t *r;
  911. if (!rl)
  912. rl = routerlist;
  913. i = 0;
  914. if ((r = router_get_my_routerinfo())) {
  915. smartlist_insert(rl->routers, 0, routerinfo_copy(r));
  916. ++i;
  917. }
  918. for ( ; i < smartlist_len(rl->routers); ++i) {
  919. remove = 0;
  920. r = smartlist_get(rl->routers,i);
  921. if (router_is_me(r)) {
  922. remove = 1;
  923. } else if (r->addr) {
  924. /* already resolved. */
  925. } else if (router_resolve(r)) {
  926. log_fn(LOG_WARN, "Couldn't resolve router '%s' at '%s'; not using",
  927. r->nickname, r->address);
  928. remove = 1;
  929. }
  930. if (remove) {
  931. routerinfo_free(r);
  932. smartlist_del_keeporder(rl->routers, i--);
  933. }
  934. }
  935. return 0;
  936. }
  937. /** Decide whether a given addr:port is definitely accepted,
  938. * definitely rejected, probably accepted, or probably rejected by a
  939. * given policy. If <b>addr</b> is 0, we don't know the IP of the
  940. * target address. If <b>port</b> is 0, we don't know the port of the
  941. * target address.
  942. *
  943. * For now, the algorithm is pretty simple: we look for definite and
  944. * uncertain matches. The first definite match is what we guess; if
  945. * it was proceded by no uncertain matches of the opposite policy,
  946. * then the guess is definite; otherwise it is probable. (If we
  947. * have a known addr and port, all matches are definite; if we have an
  948. * unknown addr/port, any address/port ranges other than "all" are
  949. * uncertain.)
  950. *
  951. * We could do better by assuming that some ranges never match typical
  952. * addresses (127.0.0.1, and so on). But we'll try this for now.
  953. */
  954. addr_policy_result_t
  955. router_compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
  956. addr_policy_t *policy)
  957. {
  958. int maybe_reject = 0;
  959. int maybe_accept = 0;
  960. int match = 0;
  961. int maybe = 0;
  962. addr_policy_t *tmpe;
  963. for (tmpe=policy; tmpe; tmpe=tmpe->next) {
  964. // log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
  965. maybe = 0;
  966. if (!addr) {
  967. /* Address is unknown. */
  968. if ((port >= tmpe->prt_min && port <= tmpe->prt_max) ||
  969. (!port && tmpe->prt_min<=1 && tmpe->prt_max>=65535)) {
  970. /* The port definitely matches. */
  971. if (tmpe->msk == 0) {
  972. match = 1;
  973. } else {
  974. maybe = 1;
  975. }
  976. } else if (!port) {
  977. /* The port maybe matches. */
  978. maybe = 1;
  979. }
  980. } else {
  981. /* Address is known */
  982. if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
  983. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  984. /* Exact match for the policy */
  985. match = 1;
  986. } else if (!port) {
  987. maybe = 1;
  988. }
  989. }
  990. }
  991. if (maybe) {
  992. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  993. maybe_reject = 1;
  994. else
  995. maybe_accept = 1;
  996. }
  997. if (match) {
  998. // struct in_addr in;
  999. // in.s_addr = htonl(addr);
  1000. // log_fn(LOG_DEBUG,"Address %s:%d matches policy '%s'",
  1001. // inet_ntoa(in), port, tmpe->string);
  1002. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  1003. /* If we already hit a clause that might trigger a 'reject', than we
  1004. * can't be sure of this certain 'accept'.*/
  1005. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  1006. } else {
  1007. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED : ADDR_POLICY_REJECTED;
  1008. }
  1009. }
  1010. }
  1011. /* accept all by default. */
  1012. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  1013. }
  1014. /** Return 1 if all running sufficiently-stable routers will reject
  1015. * addr:port, return 0 if any might accept it. */
  1016. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
  1017. int need_uptime) {
  1018. int i;
  1019. routerinfo_t *router;
  1020. addr_policy_result_t r;
  1021. if (!routerlist) return 1;
  1022. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  1023. router = smartlist_get(routerlist->routers, i);
  1024. if (router->is_running &&
  1025. !router_is_unreliable(router, need_uptime, 0)) {
  1026. r = router_compare_addr_to_addr_policy(addr, port, router->exit_policy);
  1027. if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
  1028. return 0; /* this one could be ok. good enough. */
  1029. }
  1030. }
  1031. return 1; /* all will reject. */
  1032. }
  1033. /**
  1034. * If <b>policy</b> implicitly allows connections to any port in the
  1035. * IP set <b>addr</b>/<b>mask</b>, then set *<b>policy_out</b> to the
  1036. * part of the policy that allows it, and return 1. Else return 0.
  1037. *
  1038. * A policy allows an IP:Port combination <em>implicitly</em> if
  1039. * it is included in a *: pattern, or in a fallback pattern.
  1040. */
  1041. static int
  1042. policy_includes_addr_mask_implicitly(addr_policy_t *policy,
  1043. uint32_t addr, uint32_t mask,
  1044. addr_policy_t **policy_out)
  1045. {
  1046. uint32_t addr2;
  1047. tor_assert(policy_out);
  1048. addr &= mask;
  1049. addr2 = addr | ~mask;
  1050. for (; policy; policy=policy->next) {
  1051. /* Does this policy cover all of the address range we're looking at? */
  1052. /* Boolean logic time: range X is contained in range Y if, for
  1053. * each bit B, all possible values of B in X are values of B in Y.
  1054. * In "addr", we have every fixed bit set to its value, and every
  1055. * free bit set to 0. In "addr2", we have every fixed bit set to
  1056. * its value, and every free bit set to 1. So if addr and addr2 are
  1057. * both in the policy, the range is covered by the policy.
  1058. */
  1059. uint32_t p_addr = policy->addr & policy->msk;
  1060. if (p_addr == (addr & policy->msk) &&
  1061. p_addr == (addr2 & policy->msk) &&
  1062. (policy->prt_min <= 1 && policy->prt_max == 65535)) {
  1063. return 0;
  1064. }
  1065. /* Does this policy cover some of the address range we're looking at? */
  1066. /* Boolean logic time: range X and range Y intersect if there is
  1067. * some z such that z & Xmask == Xaddr and z & Ymask == Yaddr.
  1068. * This is FALSE iff there is some bit b where Xmask == yMask == 1
  1069. * and Xaddr != Yaddr. So if X intersects with Y iff at every
  1070. * place where Xmask&Ymask==1, Xaddr == Yaddr, or equivalently,
  1071. * Xaddr&Xmask&Ymask == Yaddr&Xmask&Ymask.
  1072. */
  1073. if ((policy->addr & policy->msk & mask) == (addr & policy->msk) &&
  1074. policy->policy_type == ADDR_POLICY_ACCEPT) {
  1075. *policy_out = policy;
  1076. return 1;
  1077. }
  1078. }
  1079. *policy_out = NULL;
  1080. return 1;
  1081. }
  1082. /** If <b>policy</b> implicitly allows connections to any port on
  1083. * 127.*, 192.168.*, etc, then warn (if <b>warn</b> is set) and return
  1084. * true. Else return false.
  1085. **/
  1086. int
  1087. exit_policy_implicitly_allows_local_networks(addr_policy_t *policy,
  1088. int warn)
  1089. {
  1090. addr_policy_t *p;
  1091. int r=0,i;
  1092. static struct {
  1093. uint32_t addr; uint32_t mask; const char *network;
  1094. } private_networks[] = {
  1095. { 0x7f000000, 0xff000000, "localhost (127.x)" },
  1096. { 0x0a000000, 0xff000000, "addresses in private network 10.x" },
  1097. { 0xa9fe0000, 0xffff0000, "addresses in private network 169.254.x" },
  1098. { 0xac100000, 0xfff00000, "addresses in private network 172.16.x" },
  1099. { 0xc0a80000, 0xffff0000, "addresses in private network 192.168.x" },
  1100. { 0,0,NULL},
  1101. };
  1102. for (i=0; private_networks[i].addr; ++i) {
  1103. p = NULL;
  1104. /* log_fn(LOG_INFO,"Checking network %s", private_networks[i].network); */
  1105. if (policy_includes_addr_mask_implicitly(
  1106. policy, private_networks[i].addr, private_networks[i].mask, &p)) {
  1107. if (warn)
  1108. log_fn(LOG_WARN, "Exit policy %s implicitly accepts %s",
  1109. p?p->string:"(default)",
  1110. private_networks[i].network);
  1111. r = 1;
  1112. }
  1113. }
  1114. return r;
  1115. }
  1116. /** Return true iff <b>router</b> does not permit exit streams.
  1117. */
  1118. int router_exit_policy_rejects_all(routerinfo_t *router) {
  1119. return router_compare_addr_to_addr_policy(0, 0, router->exit_policy)
  1120. == ADDR_POLICY_REJECTED;
  1121. }
  1122. /** Release all space held in <b>rr</b>. */
  1123. void running_routers_free(running_routers_t *rr)
  1124. {
  1125. if (!rr)
  1126. return;
  1127. if (rr->running_routers) {
  1128. SMARTLIST_FOREACH(rr->running_routers, char *, s, tor_free(s));
  1129. smartlist_free(rr->running_routers);
  1130. }
  1131. tor_free(rr);
  1132. }
  1133. /** We've just got a running routers list in <b>rr</b>; update the
  1134. * status of the routers in <b>list</b>, and cache <b>rr</b> */
  1135. void
  1136. routerlist_set_runningrouters(routerlist_t *list, running_routers_t *rr)
  1137. {
  1138. routerlist_update_from_runningrouters(list,rr);
  1139. if (list->running_routers != rr) {
  1140. running_routers_free(list->running_routers);
  1141. list->running_routers = rr;
  1142. }
  1143. }
  1144. /** Update the running/not-running status of every router in <b>list</b>, based
  1145. * on the contents of <b>rr</b>. */
  1146. /* Note: this function is not yet used, since nobody publishes just
  1147. * running-router lists yet. */
  1148. void routerlist_update_from_runningrouters(routerlist_t *list,
  1149. running_routers_t *rr)
  1150. {
  1151. routerinfo_t *me = router_get_my_routerinfo();
  1152. smartlist_t *all_routers;
  1153. if (!list)
  1154. return;
  1155. if (list->published_on >= rr->published_on)
  1156. return;
  1157. if (list->running_routers_updated_on >= rr->published_on)
  1158. return;
  1159. all_routers = smartlist_create();
  1160. if (me) /* learn if the dirservers think I'm verified */
  1161. smartlist_add(all_routers, me);
  1162. smartlist_add_all(all_routers,list->routers);
  1163. SMARTLIST_FOREACH(rr->running_routers, const char *, cp,
  1164. routers_update_status_from_entry(all_routers, rr->published_on,
  1165. cp));
  1166. smartlist_free(all_routers);
  1167. list->running_routers_updated_on = rr->published_on;
  1168. }
  1169. /** Update the is_running and is_verified fields of the router <b>router</b>,
  1170. * based in its status in the list of strings stored in <b>running_list</b>.
  1171. * All entries in <b>running_list</b> follow one of these formats:
  1172. * <ol><li> <b>nickname</b> -- router is running and verified.
  1173. * (running-routers format)
  1174. * <li> !<b>nickname</b> -- router is not-running and verified.
  1175. * (running-routers format)
  1176. * <li> <b>nickname</b>=$<b>hexdigest</b> -- router is running and
  1177. * verified. (router-status format)
  1178. * (router-status format)
  1179. * <li> !<b>nickname</b>=$<b>hexdigest</b> -- router is running and
  1180. * verified. (router-status format)
  1181. * <li> !<b>nickname</b> -- router is not-running and verified.
  1182. * <li> $<b>hexdigest</b> -- router is running and unverified.
  1183. * <li> !$<b>hexdigest</b> -- router is not-running and unverified.
  1184. * </ol>
  1185. *
  1186. * Return 1 if we found router in running_list, else return 0.
  1187. */
  1188. int routers_update_status_from_entry(smartlist_t *routers,
  1189. time_t list_time,
  1190. const char *s)
  1191. {
  1192. int is_running = 1;
  1193. int is_verified = 0;
  1194. int hex_digest_set = 0;
  1195. char nickname[MAX_NICKNAME_LEN+1];
  1196. char hexdigest[HEX_DIGEST_LEN+1];
  1197. char digest[DIGEST_LEN];
  1198. const char *cp, *end;
  1199. /* First, parse the entry. */
  1200. cp = s;
  1201. if (*cp == '!') {
  1202. is_running = 0;
  1203. ++cp;
  1204. }
  1205. if (*cp != '$') {
  1206. /* It starts with a non-dollar character; that's a nickname. The nickname
  1207. * entry will either extend to a NUL (old running-routers format) or to an
  1208. * equals sign (new router-status format). */
  1209. is_verified = 1;
  1210. end = strchr(cp, '=');
  1211. if (!end)
  1212. end = strchr(cp,'\0');
  1213. tor_assert(end);
  1214. /* 'end' now points on character beyond the end of the nickname */
  1215. if (end == cp || end-cp > MAX_NICKNAME_LEN) {
  1216. log_fn(LOG_WARN, "Bad nickname length (%d) in router status entry (%s)",
  1217. (int)(end-cp), s);
  1218. return -1;
  1219. }
  1220. memcpy(nickname, cp, end-cp);
  1221. nickname[end-cp]='\0';
  1222. if (!is_legal_nickname(nickname)) {
  1223. log_fn(LOG_WARN, "Bad nickname (%s) in router status entry (%s)",
  1224. nickname, s);
  1225. return -1;
  1226. }
  1227. cp = end;
  1228. if (*cp == '=')
  1229. ++cp;
  1230. }
  1231. /* 'end' now points to the start of a hex digest, or EOS. */
  1232. /* Parse the hexdigest portion of the status. */
  1233. if (*cp == '$') {
  1234. hex_digest_set = 1;
  1235. ++cp;
  1236. if (strlen(cp) != HEX_DIGEST_LEN) {
  1237. log_fn(LOG_WARN, "Bad length (%d) on digest in router status entry (%s)",
  1238. (int)strlen(cp), s);
  1239. return -1;
  1240. }
  1241. strlcpy(hexdigest, cp, sizeof(hexdigest));
  1242. if (base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0) {
  1243. log_fn(LOG_WARN, "Invalid digest in router status entry (%s)", s);
  1244. return -1;
  1245. }
  1246. }
  1247. /* Make sure that the entry was in the right format. */
  1248. if (!hex_digest_set) {
  1249. log_fn(LOG_WARN, "Invalid syntax for router-status member (%s)", s);
  1250. return -1;
  1251. }
  1252. /* Okay, we're done parsing. For all routers that match, update their status.
  1253. */
  1254. SMARTLIST_FOREACH(routers, routerinfo_t *, r,
  1255. {
  1256. int nickname_matches = is_verified && !strcasecmp(r->nickname, nickname);
  1257. int digest_matches = !memcmp(digest, r->identity_digest, DIGEST_LEN);
  1258. if (nickname_matches && digest_matches)
  1259. r->is_verified = 1;
  1260. else if (digest_matches)
  1261. r->is_verified = 0;
  1262. if (digest_matches)
  1263. if (r->status_set_at < list_time) {
  1264. r->is_running = is_running;
  1265. r->status_set_at = time(NULL);
  1266. }
  1267. });
  1268. return 0;
  1269. }
  1270. /** As router_update_status_from_entry, but consider all entries in
  1271. * running_list. */
  1272. int
  1273. router_update_status_from_smartlist(routerinfo_t *router,
  1274. time_t list_time,
  1275. smartlist_t *running_list)
  1276. {
  1277. smartlist_t *rl;
  1278. rl = smartlist_create();
  1279. smartlist_add(rl,router);
  1280. SMARTLIST_FOREACH(running_list, const char *, cp,
  1281. routers_update_status_from_entry(rl,list_time,cp));
  1282. smartlist_free(rl);
  1283. return 0;
  1284. }
  1285. /** Add to the list of authorized directory servers one at
  1286. * <b>address</b>:<b>port</b>, with identity key <b>digest</b>. */
  1287. void
  1288. add_trusted_dir_server(const char *address, uint16_t port, const char *digest)
  1289. {
  1290. trusted_dir_server_t *ent;
  1291. uint32_t a;
  1292. if (!trusted_dir_servers)
  1293. trusted_dir_servers = smartlist_create();
  1294. if (tor_lookup_hostname(address, &a)) {
  1295. log_fn(LOG_WARN, "Unable to lookup address for directory server at %s",
  1296. address);
  1297. return;
  1298. }
  1299. ent = tor_malloc(sizeof(trusted_dir_server_t));
  1300. ent->address = tor_strdup(address);
  1301. ent->addr = ntohl(a);
  1302. ent->dir_port = port;
  1303. ent->is_running = 1;
  1304. memcpy(ent->digest, digest, DIGEST_LEN);
  1305. smartlist_add(trusted_dir_servers, ent);
  1306. }
  1307. /** Remove all members from the list of trusted dir servers. */
  1308. void clear_trusted_dir_servers(void)
  1309. {
  1310. if (trusted_dir_servers) {
  1311. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
  1312. { tor_free(ent->address); tor_free(ent); });
  1313. smartlist_clear(trusted_dir_servers);
  1314. } else {
  1315. trusted_dir_servers = smartlist_create();
  1316. }
  1317. }