routerlist.c 38 KB

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