routerlist.c 39 KB

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