routerlist.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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, 1) < 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. int retry_if_no_servers) {
  86. routerinfo_t *choice;
  87. if (!routerlist)
  88. return NULL;
  89. choice = router_pick_directory_server_impl(requireothers, fascistfirewall,
  90. for_runningrouters);
  91. if (choice || !retry_if_no_servers)
  92. return choice;
  93. log_fn(LOG_INFO,"No reachable router entries for dirservers. Trying them all again.");
  94. /* mark all authdirservers as up again */
  95. mark_all_trusteddirservers_up();
  96. /* try again */
  97. choice = router_pick_directory_server_impl(requireothers, fascistfirewall,
  98. for_runningrouters);
  99. if (choice)
  100. return choice;
  101. log_fn(LOG_INFO,"Still no %s router entries. Reloading and trying again.",
  102. get_options()->FascistFirewall ? "reachable" : "known");
  103. has_fetched_directory=0; /* reset it */
  104. if (router_reload_router_list()) {
  105. return NULL;
  106. }
  107. /* give it one last try */
  108. choice = router_pick_directory_server_impl(requireothers, 0,
  109. for_runningrouters);
  110. return choice;
  111. }
  112. trusted_dir_server_t *router_pick_trusteddirserver(int requireothers,
  113. int fascistfirewall,
  114. int retry_if_no_servers) {
  115. trusted_dir_server_t *choice;
  116. choice = router_pick_trusteddirserver_impl(requireothers, fascistfirewall);
  117. if (choice || !retry_if_no_servers)
  118. return choice;
  119. log_fn(LOG_INFO,"No trusted dirservers are reachable. Trying them all again.");
  120. /* mark all authdirservers as up again */
  121. mark_all_trusteddirservers_up();
  122. /* try again */
  123. choice = router_pick_trusteddirserver_impl(requireothers, fascistfirewall);
  124. if (choice)
  125. return choice;
  126. log_fn(LOG_WARN,"Still no dirservers %s. Reloading and trying again.",
  127. get_options()->FascistFirewall ? "reachable" : "known");
  128. has_fetched_directory=0; /* reset it */
  129. if (router_reload_router_list()) {
  130. return NULL;
  131. }
  132. /* give it one last try */
  133. choice = router_pick_trusteddirserver_impl(requireothers, 0);
  134. return choice;
  135. }
  136. /** Pick a random running router from our routerlist. If requireauth,
  137. * it has to be a trusted server. If requireothers, it cannot be us.
  138. */
  139. static routerinfo_t *
  140. router_pick_directory_server_impl(int requireothers, int fascistfirewall,
  141. int for_runningrouters)
  142. {
  143. int i;
  144. routerinfo_t *router;
  145. smartlist_t *sl;
  146. char buf[16];
  147. if (!routerlist)
  148. return NULL;
  149. if (get_options()->HttpProxy)
  150. fascistfirewall = 0;
  151. /* Find all the running dirservers we know about. */
  152. sl = smartlist_create();
  153. for (i=0;i< smartlist_len(routerlist->routers); i++) {
  154. router = smartlist_get(routerlist->routers, i);
  155. if (!router->is_running || !router->dir_port)
  156. continue;
  157. if (requireothers && router_is_me(router))
  158. continue;
  159. if (fascistfirewall) {
  160. tor_snprintf(buf,sizeof(buf),"%d",router->dir_port);
  161. if (!smartlist_string_isin(get_options()->FirewallPorts, buf))
  162. continue;
  163. }
  164. /* before 0.0.9rc5-cvs, only trusted dirservers served status info. */
  165. if (for_runningrouters &&
  166. !(tor_version_as_new_as(router->platform,"0.0.9rc5-cvs") ||
  167. router_digest_is_trusted_dir(router->identity_digest)))
  168. continue;
  169. smartlist_add(sl, router);
  170. }
  171. router = smartlist_choose(sl);
  172. smartlist_free(sl);
  173. return router;
  174. }
  175. static trusted_dir_server_t *
  176. router_pick_trusteddirserver_impl(int requireother, int fascistfirewall)
  177. {
  178. smartlist_t *sl;
  179. routerinfo_t *me;
  180. char buf[16];
  181. trusted_dir_server_t *ds;
  182. sl = smartlist_create();
  183. me = router_get_my_routerinfo();
  184. if (!trusted_dir_servers)
  185. return NULL;
  186. if (get_options()->HttpProxy)
  187. fascistfirewall = 0;
  188. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
  189. {
  190. if (!d->is_running) continue;
  191. if (requireother && me &&
  192. !memcmp(me->identity_digest, d->digest, DIGEST_LEN))
  193. continue;
  194. if (fascistfirewall) {
  195. tor_snprintf(buf,sizeof(buf),"%d",d->dir_port);
  196. if (!smartlist_string_isin(get_options()->FirewallPorts, buf))
  197. continue;
  198. }
  199. smartlist_add(sl, d);
  200. });
  201. ds = smartlist_choose(sl);
  202. smartlist_free(sl);
  203. return ds;
  204. }
  205. /** Go through and mark the auth dirservers as up */
  206. static void mark_all_trusteddirservers_up(void) {
  207. if (routerlist) {
  208. SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
  209. if (router_digest_is_trusted_dir(router->identity_digest)) {
  210. tor_assert(router->dir_port > 0);
  211. router->is_running = 1;
  212. router->status_set_at = time(NULL);
  213. });
  214. }
  215. if (trusted_dir_servers) {
  216. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, dir,
  217. dir->is_running = 1);
  218. }
  219. }
  220. /** Return 0 if \exists an authoritative dirserver that's currently
  221. * thought to be running, else return 1.
  222. */
  223. int all_trusted_directory_servers_down(void) {
  224. if (!trusted_dir_servers)
  225. return 1;
  226. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, dir,
  227. if (dir->is_running) return 0);
  228. return 1;
  229. }
  230. /** Add all the family of <b>router</b> to the smartlist <b>sl</b>.
  231. */
  232. void routerlist_add_family(smartlist_t *sl, routerinfo_t *router) {
  233. routerinfo_t *r;
  234. struct config_line_t *cl;
  235. if (!router->declared_family)
  236. return;
  237. /* Add every r such that router declares familyness with r, and r
  238. * declares familyhood with router. */
  239. SMARTLIST_FOREACH(router->declared_family, const char *, n,
  240. {
  241. if (!(r = router_get_by_nickname(n)))
  242. continue;
  243. if (!r->declared_family)
  244. continue;
  245. SMARTLIST_FOREACH(r->declared_family, const char *, n2,
  246. {
  247. if (router_nickname_matches(router, n2))
  248. smartlist_add(sl, r);
  249. });
  250. });
  251. for (cl = get_options()->NodeFamilies; cl; cl = cl->next) {
  252. if (router_nickname_is_in_list(router, cl->value)) {
  253. add_nickname_list_to_smartlist(sl, cl->value, 0);
  254. }
  255. }
  256. }
  257. /** Given a comma-and-whitespace separated list of nicknames, see which
  258. * nicknames in <b>list</b> name routers in our routerlist that are
  259. * currently running. Add the routerinfos for those routers to <b>sl</b>.
  260. */
  261. void
  262. add_nickname_list_to_smartlist(smartlist_t *sl, const char *list, int warn_if_down)
  263. {
  264. routerinfo_t *router;
  265. smartlist_t *nickname_list;
  266. if (!list)
  267. return; /* nothing to do */
  268. tor_assert(sl);
  269. nickname_list = smartlist_create();
  270. smartlist_split_string(nickname_list, list, ",",
  271. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  272. SMARTLIST_FOREACH(nickname_list, const char *, nick, {
  273. if (!is_legal_nickname_or_hexdigest(nick)) {
  274. log_fn(LOG_WARN,"Nickname %s is misformed; skipping", nick);
  275. continue;
  276. }
  277. router = router_get_by_nickname(nick);
  278. if (router) {
  279. if (router->is_running)
  280. smartlist_add(sl,router);
  281. else
  282. log_fn(warn_if_down ? LOG_WARN : LOG_DEBUG,
  283. "Nickname list includes '%s' which is known but down.",nick);
  284. } else
  285. log_fn(has_fetched_directory ? LOG_WARN : LOG_INFO,
  286. "Nickname list includes '%s' which isn't a known router.",nick);
  287. });
  288. SMARTLIST_FOREACH(nickname_list, char *, nick, tor_free(nick));
  289. smartlist_free(nickname_list);
  290. }
  291. /** Return 1 iff any member of the comma-separated list <b>list</b> is an
  292. * acceptable nickname or hexdigest for <b>router</b>. Else return 0.
  293. */
  294. int
  295. router_nickname_is_in_list(routerinfo_t *router, const char *list)
  296. {
  297. smartlist_t *nickname_list;
  298. int v = 0;
  299. if (!list)
  300. return 0; /* definitely not */
  301. tor_assert(router);
  302. nickname_list = smartlist_create();
  303. smartlist_split_string(nickname_list, list, ",",
  304. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  305. SMARTLIST_FOREACH(nickname_list, const char *, cp,
  306. if (router_nickname_matches(router, cp)) {v=1;break;});
  307. SMARTLIST_FOREACH(nickname_list, char *, cp, tor_free(cp));
  308. smartlist_free(nickname_list);
  309. return v;
  310. }
  311. /** Add every router from our routerlist that is currently running to
  312. * <b>sl</b>.
  313. */
  314. static void
  315. router_add_running_routers_to_smartlist(smartlist_t *sl, int allow_unverified,
  316. int preferuptime, int preferbandwidth)
  317. {
  318. routerinfo_t *router;
  319. int i;
  320. if (!routerlist)
  321. return;
  322. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  323. router = smartlist_get(routerlist->routers, i);
  324. if (router->is_running &&
  325. (router->is_verified ||
  326. (allow_unverified &&
  327. !router_is_unreliable_router(router, preferuptime, preferbandwidth)))) {
  328. /* If it's running, and either it's verified or we're ok picking
  329. * unverified routers and this one is suitable.
  330. */
  331. smartlist_add(sl, router);
  332. }
  333. }
  334. }
  335. routerinfo_t *
  336. routerlist_find_my_routerinfo(void) {
  337. routerinfo_t *router;
  338. int i;
  339. if (!routerlist)
  340. return NULL;
  341. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  342. router = smartlist_get(routerlist->routers, i);
  343. if (router_is_me(router))
  344. return router;
  345. }
  346. return NULL;
  347. }
  348. int
  349. router_is_unreliable_router(routerinfo_t *router, int need_uptime, int need_bw)
  350. {
  351. if (need_uptime && router->uptime < ROUTER_REQUIRED_MIN_UPTIME)
  352. return 1;
  353. if (need_bw && router->bandwidthcapacity < ROUTER_REQUIRED_MIN_BANDWIDTH)
  354. return 1;
  355. return 0;
  356. }
  357. static void
  358. routerlist_sl_remove_unreliable_routers(smartlist_t *sl)
  359. {
  360. int i;
  361. routerinfo_t *router;
  362. for (i = 0; i < smartlist_len(sl); ++i) {
  363. router = smartlist_get(sl, i);
  364. if (router_is_unreliable_router(router, 1, 0)) {
  365. log(LOG_DEBUG, "Router '%s' has insufficient uptime; deleting.",
  366. router->nickname);
  367. smartlist_del(sl, i--);
  368. }
  369. }
  370. }
  371. routerinfo_t *
  372. routerlist_sl_choose_by_bandwidth(smartlist_t *sl)
  373. {
  374. int i;
  375. routerinfo_t *router;
  376. smartlist_t *bandwidths;
  377. uint32_t this_bw, tmp, total_bw=0, rand_bw;
  378. uint32_t *p;
  379. bandwidths = smartlist_create();
  380. for (i = 0; i < smartlist_len(sl); ++i) {
  381. router = smartlist_get(sl, i);
  382. this_bw = (router->bandwidthcapacity < router->bandwidthrate) ?
  383. router->bandwidthcapacity : router->bandwidthrate;
  384. if (this_bw > 800000)
  385. this_bw = 800000; /* if they claim something huge, don't believe it */
  386. p = tor_malloc(sizeof(uint32_t));
  387. *p = this_bw;
  388. smartlist_add(bandwidths, p);
  389. total_bw += this_bw;
  390. // log_fn(LOG_INFO,"Recording bw %d for node %s.", this_bw, router->nickname);
  391. }
  392. if (!total_bw) {
  393. SMARTLIST_FOREACH(bandwidths, uint32_t*, p, tor_free(p));
  394. smartlist_free(bandwidths);
  395. return smartlist_choose(sl);
  396. }
  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. 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(addr_policy_t));
  615. memcpy(tmp,*e,sizeof(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 unverified 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. *
  753. * DOCDOC dir_is_recent/cached
  754. */
  755. int router_load_routerlist_from_directory(const char *s,
  756. crypto_pk_env_t *pkey,
  757. int dir_is_recent,
  758. int dir_is_cached)
  759. {
  760. routerlist_t *new_list = NULL;
  761. if (router_parse_routerlist_from_directory(s, &new_list, pkey,
  762. dir_is_recent,
  763. !dir_is_cached)) {
  764. log_fn(LOG_WARN, "Couldn't parse directory.");
  765. return -1;
  766. }
  767. if (routerlist) {
  768. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  769. router_add_to_routerlist(r));
  770. smartlist_clear(new_list->routers);
  771. routerlist->published_on = new_list->published_on;
  772. tor_free(routerlist->software_versions);
  773. routerlist->software_versions = new_list->software_versions;
  774. new_list->software_versions = NULL;
  775. routerlist_free(new_list);
  776. } else {
  777. routerlist = new_list;
  778. }
  779. if (router_resolve_routerlist(routerlist)) {
  780. log_fn(LOG_WARN, "Error resolving routerlist");
  781. return -1;
  782. }
  783. if (get_options()->AuthoritativeDir) {
  784. /* Learn about the descriptors in the directory. */
  785. dirserv_load_from_directory_string(s);
  786. }
  787. return 0;
  788. }
  789. /** Helper function: resolve the hostname for <b>router</b>. */
  790. static int
  791. router_resolve(routerinfo_t *router)
  792. {
  793. if (tor_lookup_hostname(router->address, &router->addr) != 0
  794. || !router->addr) {
  795. log_fn(LOG_WARN,"Could not resolve address for router '%s' at %s",
  796. router->nickname, router->address);
  797. return -1;
  798. }
  799. router->addr = ntohl(router->addr); /* get it back into host order */
  800. return 0;
  801. }
  802. /** Helper function: resolve every router in rl, and ensure that our own
  803. * routerinfo is at the front.
  804. */
  805. static int
  806. router_resolve_routerlist(routerlist_t *rl)
  807. {
  808. int i, remove;
  809. routerinfo_t *r;
  810. if (!rl)
  811. rl = routerlist;
  812. i = 0;
  813. if ((r = router_get_my_routerinfo())) {
  814. smartlist_insert(rl->routers, 0, routerinfo_copy(r));
  815. ++i;
  816. }
  817. for ( ; i < smartlist_len(rl->routers); ++i) {
  818. remove = 0;
  819. r = smartlist_get(rl->routers,i);
  820. if (router_is_me(r)) {
  821. remove = 1;
  822. } else if (r->addr) {
  823. /* already resolved. */
  824. } else if (router_resolve(r)) {
  825. log_fn(LOG_WARN, "Couldn't resolve router '%s' at '%s'; not using",
  826. r->nickname, r->address);
  827. remove = 1;
  828. }
  829. if (remove) {
  830. routerinfo_free(r);
  831. smartlist_del_keeporder(rl->routers, i--);
  832. }
  833. }
  834. return 0;
  835. }
  836. /** Decide whether a given addr:port is definitely accepted, definitely
  837. * rejected, or neither by a given policy. If <b>addr</b> is 0, we
  838. * don't know the IP of the target address. If <b>port</b> is 0, we
  839. * don't know the port of the target address.
  840. *
  841. * Returns -1 for "rejected", 0 for "accepted", 1 for "maybe" (since IP or
  842. * port is unknown).
  843. */
  844. int router_compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
  845. addr_policy_t *policy)
  846. {
  847. int maybe_reject = 0;
  848. int maybe_accept = 0;
  849. int match = 0;
  850. int maybe = 0;
  851. addr_policy_t *tmpe;
  852. for (tmpe=policy; tmpe; tmpe=tmpe->next) {
  853. // log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
  854. maybe = 0;
  855. if (!addr) {
  856. /* Address is unknown. */
  857. if ((port >= tmpe->prt_min && port <= tmpe->prt_max) ||
  858. (!port && tmpe->prt_min<=1 && tmpe->prt_max>=65535)) {
  859. /* The port definitely matches. */
  860. if (tmpe->msk == 0) {
  861. match = 1;
  862. } else {
  863. maybe = 1;
  864. }
  865. } else if (!port) {
  866. /* The port maybe matches. */
  867. maybe = 1;
  868. }
  869. } else {
  870. /* Address is known */
  871. if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
  872. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  873. /* Exact match for the policy */
  874. match = 1;
  875. } else if (!port) {
  876. maybe = 1;
  877. }
  878. }
  879. }
  880. if (maybe) {
  881. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  882. maybe_reject = 1;
  883. else
  884. maybe_accept = 1;
  885. }
  886. if (match) {
  887. // struct in_addr in;
  888. // in.s_addr = htonl(addr);
  889. // log_fn(LOG_DEBUG,"Address %s:%d matches policy '%s'",
  890. // inet_ntoa(in), port, tmpe->string);
  891. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  892. /* If we already hit a clause that might trigger a 'reject', than we
  893. * can't be sure of this certain 'accept'.*/
  894. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  895. } else {
  896. return maybe_accept ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_REJECTED;
  897. }
  898. }
  899. }
  900. /* accept all by default. */
  901. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  902. }
  903. /** Return 1 if all running routers will reject addr:port, return 0 if
  904. * any might accept it. */
  905. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port) {
  906. int i;
  907. routerinfo_t *router;
  908. if (!routerlist) return 1;
  909. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  910. router = smartlist_get(routerlist->routers, i);
  911. if (router->is_running && router_compare_addr_to_addr_policy(
  912. addr, port, router->exit_policy) != ADDR_POLICY_REJECTED)
  913. return 0; /* this one could be ok. good enough. */
  914. }
  915. return 1; /* all will reject. */
  916. }
  917. /** Return true iff <b>router</b> does not permit exit streams.
  918. */
  919. int router_exit_policy_rejects_all(routerinfo_t *router) {
  920. return router_compare_addr_to_addr_policy(0, 0, router->exit_policy)
  921. == ADDR_POLICY_REJECTED;
  922. }
  923. /** Release all space held in <b>rr</b>. */
  924. void running_routers_free(running_routers_t *rr)
  925. {
  926. tor_assert(rr);
  927. if (rr->running_routers) {
  928. SMARTLIST_FOREACH(rr->running_routers, char *, s, tor_free(s));
  929. smartlist_free(rr->running_routers);
  930. }
  931. tor_free(rr);
  932. }
  933. /** Update the running/not-running status of every router in <b>list</b>, based
  934. * on the contents of <b>rr</b>. */
  935. /* Note: this function is not yet used, since nobody publishes just
  936. * running-router lists yet. */
  937. void routerlist_update_from_runningrouters(routerlist_t *list,
  938. running_routers_t *rr)
  939. {
  940. routerinfo_t *me = router_get_my_routerinfo();
  941. smartlist_t *all_routers;
  942. if (!list)
  943. return;
  944. if (list->published_on >= rr->published_on)
  945. return;
  946. if (list->running_routers_updated_on >= rr->published_on)
  947. return;
  948. all_routers = smartlist_create();
  949. if (me) /* learn if the dirservers think I'm verified */
  950. smartlist_add(all_routers, me);
  951. smartlist_add_all(all_routers,list->routers);
  952. SMARTLIST_FOREACH(rr->running_routers, const char *, cp,
  953. routers_update_status_from_entry(all_routers, rr->published_on,
  954. cp, rr->is_running_routers_format));
  955. smartlist_free(all_routers);
  956. list->running_routers_updated_on = rr->published_on;
  957. }
  958. /** Update the is_running and is_verified fields of the router <b>router</b>,
  959. * based in its status in the list of strings stored in <b>running_list</b>.
  960. * All entries in <b>running_list</b> follow one of these formats:
  961. * <ol><li> <b>nickname</b> -- router is running and verified.
  962. * (running-routers format)
  963. * <li> !<b>nickname</b> -- router is not-running and verified.
  964. * (running-routers format)
  965. * <li> <b>nickname</b>=$<b>hexdigest</b> -- router is running and
  966. * verified. (router-status format)
  967. * (router-status format)
  968. * <li> !<b>nickname</b>=$<b>hexdigest</b> -- router is running and
  969. * verified. (router-status format)
  970. * <li> !<b>nickname</b> -- router is not-running and verified.
  971. * <li> $<b>hexdigest</b> -- router is running and unverified.
  972. * <li> !$<b>hexdigest</b> -- router is not-running and unverified.
  973. * </ol>
  974. *
  975. * Return 1 if we found router in running_list, else return 0.
  976. */
  977. int routers_update_status_from_entry(smartlist_t *routers,
  978. time_t list_time,
  979. const char *s,
  980. int rr_format)
  981. {
  982. int is_running = 1;
  983. int is_verified = 0;
  984. int hex_digest_set = 0;
  985. char nickname[MAX_NICKNAME_LEN+1];
  986. char hexdigest[HEX_DIGEST_LEN+1];
  987. char digest[DIGEST_LEN];
  988. const char *cp, *end;
  989. /* First, parse the entry. */
  990. cp = s;
  991. if (*cp == '!') {
  992. is_running = 0;
  993. ++cp;
  994. }
  995. if (*cp != '$') {
  996. /* It starts with a non-dollar character; that's a nickname. The nickname
  997. * entry will either extend to a NUL (old running-routers format) or to an
  998. * equals sign (new router-status format). */
  999. is_verified = 1;
  1000. end = strchr(cp, '=');
  1001. if (!end)
  1002. end = strchr(cp,'\0');
  1003. tor_assert(end);
  1004. /* 'end' now points on character beyond the end of the nickname */
  1005. if (end == cp || end-cp > MAX_NICKNAME_LEN) {
  1006. log_fn(LOG_WARN, "Bad nickname length (%d) in router status entry (%s)",
  1007. (int)(end-cp), s);
  1008. return -1;
  1009. }
  1010. memcpy(nickname, cp, end-cp);
  1011. nickname[end-cp]='\0';
  1012. if (!is_legal_nickname(nickname)) {
  1013. log_fn(LOG_WARN, "Bad nickname (%s) in router status entry (%s)",
  1014. nickname, s);
  1015. return -1;
  1016. }
  1017. cp = end;
  1018. if (*cp == '=')
  1019. ++cp;
  1020. }
  1021. /* 'end' now points to the start of a hex digest, or EOS. */
  1022. /* Parse the hexdigest portion of the status. */
  1023. if (*cp == '$') {
  1024. hex_digest_set = 1;
  1025. ++cp;
  1026. if (strlen(cp) != HEX_DIGEST_LEN) {
  1027. log_fn(LOG_WARN, "Bad length (%d) on digest in router status entry (%s)",
  1028. (int)strlen(cp), s);
  1029. return -1;
  1030. }
  1031. strlcpy(hexdigest, cp, sizeof(hexdigest));
  1032. if (base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0) {
  1033. log_fn(LOG_WARN, "Invalid digest in router status entry (%s)", s);
  1034. return -1;
  1035. }
  1036. }
  1037. /* Make sure that the entry was in the right format. */
  1038. if (rr_format) {
  1039. if (is_verified == hex_digest_set) {
  1040. log_fn(LOG_WARN, "Invalid syntax for running-routers member (%s)", s);
  1041. return -1;
  1042. }
  1043. } else {
  1044. if (!hex_digest_set) {
  1045. log_fn(LOG_WARN, "Invalid syntax for router-status member (%s)", s);
  1046. return -1;
  1047. }
  1048. }
  1049. /* Okay, we're done parsing. For all routers that match, update their status.
  1050. */
  1051. SMARTLIST_FOREACH(routers, routerinfo_t *, r,
  1052. {
  1053. int nickname_matches = is_verified && !strcasecmp(r->nickname, nickname);
  1054. int digest_matches = !memcmp(digest, r->identity_digest, DIGEST_LEN);
  1055. if (nickname_matches && (digest_matches||rr_format))
  1056. r->is_verified = 1;
  1057. else if (digest_matches)
  1058. r->is_verified = 0;
  1059. if (digest_matches || (nickname_matches&&rr_format))
  1060. if (r->status_set_at < list_time) {
  1061. r->is_running = is_running;
  1062. r->status_set_at = time(NULL);
  1063. }
  1064. });
  1065. return 0;
  1066. }
  1067. /** As router_update_status_from_entry, but consider all entries in
  1068. * running_list. */
  1069. int
  1070. router_update_status_from_smartlist(routerinfo_t *router,
  1071. time_t list_time,
  1072. smartlist_t *running_list,
  1073. int rr_format)
  1074. {
  1075. smartlist_t *rl;
  1076. rl = smartlist_create();
  1077. smartlist_add(rl,router);
  1078. SMARTLIST_FOREACH(running_list, const char *, cp,
  1079. routers_update_status_from_entry(rl,list_time,cp,rr_format));
  1080. smartlist_free(rl);
  1081. return 0;
  1082. }
  1083. /** Add to the list of authorized directory servers one at
  1084. * <b>address</b>:<b>port</b>, with identity key <b>digest</b>. */
  1085. void
  1086. add_trusted_dir_server(const char *address, uint16_t port, const char *digest)
  1087. {
  1088. trusted_dir_server_t *ent;
  1089. uint32_t a;
  1090. if (!trusted_dir_servers)
  1091. trusted_dir_servers = smartlist_create();
  1092. if (tor_lookup_hostname(address, &a)) {
  1093. log_fn(LOG_WARN, "Unable to lookup address for directory server at %s",
  1094. address);
  1095. return;
  1096. }
  1097. ent = tor_malloc(sizeof(trusted_dir_server_t));
  1098. ent->address = tor_strdup(address);
  1099. ent->addr = ntohl(a);
  1100. ent->dir_port = port;
  1101. ent->is_running = 1;
  1102. memcpy(ent->digest, digest, DIGEST_LEN);
  1103. smartlist_add(trusted_dir_servers, ent);
  1104. }
  1105. void clear_trusted_dir_servers(void)
  1106. {
  1107. if (trusted_dir_servers) {
  1108. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
  1109. { tor_free(ent->address); tor_free(ent); });
  1110. smartlist_clear(trusted_dir_servers);
  1111. } else {
  1112. trusted_dir_servers = smartlist_create();
  1113. }
  1114. }