routerlist.c 38 KB

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