routerlist.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. int
  347. router_is_unreliable_router(routerinfo_t *router, int need_uptime, int need_bw)
  348. {
  349. if (need_uptime && router->uptime < ROUTER_REQUIRED_MIN_UPTIME)
  350. return 1;
  351. if (need_bw && router->bandwidthcapacity < ROUTER_REQUIRED_MIN_BANDWIDTH)
  352. return 1;
  353. return 0;
  354. }
  355. static void
  356. routerlist_sl_remove_unreliable_routers(smartlist_t *sl)
  357. {
  358. int i;
  359. routerinfo_t *router;
  360. for (i = 0; i < smartlist_len(sl); ++i) {
  361. router = smartlist_get(sl, i);
  362. if (router_is_unreliable_router(router, 1, 0)) {
  363. log(LOG_DEBUG, "Router '%s' has insufficient uptime; deleting.",
  364. router->nickname);
  365. smartlist_del(sl, i--);
  366. }
  367. }
  368. }
  369. routerinfo_t *
  370. routerlist_sl_choose_by_bandwidth(smartlist_t *sl)
  371. {
  372. int i;
  373. routerinfo_t *router;
  374. smartlist_t *bandwidths;
  375. uint32_t this_bw, tmp, total_bw=0, rand_bw;
  376. uint32_t *p;
  377. bandwidths = smartlist_create();
  378. for (i = 0; i < smartlist_len(sl); ++i) {
  379. router = smartlist_get(sl, i);
  380. this_bw = (router->bandwidthcapacity < router->bandwidthrate) ?
  381. router->bandwidthcapacity : router->bandwidthrate;
  382. if (this_bw > 800000)
  383. this_bw = 800000; /* if they claim something huge, don't believe it */
  384. p = tor_malloc(sizeof(uint32_t));
  385. *p = this_bw;
  386. smartlist_add(bandwidths, p);
  387. total_bw += this_bw;
  388. // log_fn(LOG_INFO,"Recording bw %d for node %s.", this_bw, router->nickname);
  389. }
  390. if (!total_bw) {
  391. SMARTLIST_FOREACH(bandwidths, uint32_t*, p, tor_free(p));
  392. smartlist_free(bandwidths);
  393. return smartlist_choose(sl);
  394. }
  395. rand_bw = crypto_pseudo_rand_int(total_bw);
  396. // log_fn(LOG_INFO,"Total bw %d. Randomly chose %d.", total_bw, rand_bw);
  397. tmp = 0;
  398. for (i=0; ; i++) {
  399. tor_assert(i < smartlist_len(sl));
  400. p = smartlist_get(bandwidths, i);
  401. tmp += *p;
  402. router = smartlist_get(sl, i);
  403. // log_fn(LOG_INFO,"Considering %s. tmp = %d.", router->nickname, tmp);
  404. if (tmp >= rand_bw)
  405. break;
  406. }
  407. SMARTLIST_FOREACH(bandwidths, uint32_t*, p, tor_free(p));
  408. smartlist_free(bandwidths);
  409. router = smartlist_get(sl, i);
  410. // log_fn(LOG_INFO,"Picked %s.", router->nickname);
  411. return router;
  412. }
  413. /** Return a random running router from the routerlist. If any node
  414. * named in <b>preferred</b> is available, pick one of those. Never
  415. * pick a node named in <b>excluded</b>, or whose routerinfo is in
  416. * <b>excludedsmartlist</b>, even if they are the only nodes
  417. * available. If <b>strict</b> is true, never pick any node besides
  418. * those in <b>preferred</b>.
  419. */
  420. routerinfo_t *router_choose_random_node(const char *preferred,
  421. const char *excluded,
  422. smartlist_t *excludedsmartlist,
  423. int preferuptime, int preferbandwidth,
  424. int allow_unverified, int strict)
  425. {
  426. smartlist_t *sl, *excludednodes;
  427. routerinfo_t *choice;
  428. excludednodes = smartlist_create();
  429. add_nickname_list_to_smartlist(excludednodes,excluded,0);
  430. /* try the preferred nodes first */
  431. sl = smartlist_create();
  432. add_nickname_list_to_smartlist(sl,preferred,1);
  433. smartlist_subtract(sl,excludednodes);
  434. if (excludedsmartlist)
  435. smartlist_subtract(sl,excludedsmartlist);
  436. if (preferuptime)
  437. routerlist_sl_remove_unreliable_routers(sl);
  438. if (preferbandwidth)
  439. choice = routerlist_sl_choose_by_bandwidth(sl);
  440. else
  441. choice = smartlist_choose(sl);
  442. smartlist_free(sl);
  443. if (!choice && !strict) {
  444. sl = smartlist_create();
  445. router_add_running_routers_to_smartlist(sl, allow_unverified,
  446. preferuptime, preferbandwidth);
  447. smartlist_subtract(sl,excludednodes);
  448. if (excludedsmartlist)
  449. smartlist_subtract(sl,excludedsmartlist);
  450. if (preferuptime)
  451. routerlist_sl_remove_unreliable_routers(sl);
  452. if (preferbandwidth)
  453. choice = routerlist_sl_choose_by_bandwidth(sl);
  454. else
  455. choice = smartlist_choose(sl);
  456. smartlist_free(sl);
  457. }
  458. smartlist_free(excludednodes);
  459. if (!choice)
  460. log_fn(LOG_WARN,"No available nodes when trying to choose node. Failing.");
  461. return choice;
  462. }
  463. /** Return the router in our routerlist whose address is <b>addr</b> and
  464. * whose OR port is <b>port</b>. Return NULL if no such router is known.
  465. */
  466. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  467. int i;
  468. routerinfo_t *router;
  469. if (!routerlist)
  470. return NULL;
  471. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  472. router = smartlist_get(routerlist->routers, i);
  473. if ((router->addr == addr) && (router->or_port == port))
  474. return router;
  475. }
  476. return NULL;
  477. }
  478. /** Return true iff the digest of <b>router</b>'s identity key,
  479. * encoded in hexadecimal, matches <b>hexdigest</b> (which is
  480. * optionally prefixed with a single dollar sign). Return false if
  481. * <b>hexdigest</b> is malformed, or it doesn't match. */
  482. static INLINE int router_hex_digest_matches(routerinfo_t *router,
  483. const char *hexdigest)
  484. {
  485. char digest[DIGEST_LEN];
  486. tor_assert(hexdigest);
  487. if (hexdigest[0] == '$')
  488. ++hexdigest;
  489. if (strlen(hexdigest) != HEX_DIGEST_LEN ||
  490. base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
  491. return 0;
  492. else
  493. return (!memcmp(digest, router->identity_digest, DIGEST_LEN));
  494. }
  495. /* Return true if <b>router</b>'s nickname matches <b>nickname</b>
  496. * (case-insensitive), or if <b>router's</b> identity key digest
  497. * matches a hexadecimal value stored in <b>nickname</b>. Return
  498. * false otherwise.*/
  499. int router_nickname_matches(routerinfo_t *router, const char *nickname)
  500. {
  501. if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
  502. return 1;
  503. else
  504. return router_hex_digest_matches(router, nickname);
  505. }
  506. /** Return the router in our routerlist whose (case-insensitive)
  507. * nickname or (case-sensitive) hexadecimal key digest is
  508. * <b>nickname</b>. Return NULL if no such router is known.
  509. */
  510. routerinfo_t *router_get_by_nickname(const char *nickname)
  511. {
  512. int i, maybedigest;
  513. routerinfo_t *router;
  514. char digest[DIGEST_LEN];
  515. tor_assert(nickname);
  516. if (!routerlist)
  517. return NULL;
  518. if (nickname[0] == '$')
  519. return router_get_by_hexdigest(nickname);
  520. maybedigest = (strlen(nickname) == HEX_DIGEST_LEN) &&
  521. (base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);
  522. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  523. router = smartlist_get(routerlist->routers, i);
  524. if (0 == strcasecmp(router->nickname, nickname) ||
  525. (maybedigest && 0 == memcmp(digest, router->identity_digest,
  526. DIGEST_LEN)))
  527. return router;
  528. }
  529. return NULL;
  530. }
  531. /** Return true iff <b>digest</b> is the digest of the identity key of
  532. * a trusted directory. */
  533. int router_digest_is_trusted_dir(const char *digest) {
  534. if (!trusted_dir_servers)
  535. return 0;
  536. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
  537. if (!memcmp(digest, ent->digest, DIGEST_LEN)) return 1);
  538. return 0;
  539. }
  540. /** Return the router in our routerlist whose hexadecimal key digest
  541. * is <b>hexdigest</b>. Return NULL if no such router is known. */
  542. routerinfo_t *router_get_by_hexdigest(const char *hexdigest) {
  543. char digest[DIGEST_LEN];
  544. tor_assert(hexdigest);
  545. if (!routerlist)
  546. return NULL;
  547. if (hexdigest[0]=='$')
  548. ++hexdigest;
  549. if (strlen(hexdigest) != HEX_DIGEST_LEN ||
  550. base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
  551. return NULL;
  552. return router_get_by_digest(digest);
  553. }
  554. /** Return the router in our routerlist whose 20-byte key digest
  555. * is <b>hexdigest</b>. Return NULL if no such router is known. */
  556. routerinfo_t *router_get_by_digest(const char *digest) {
  557. int i;
  558. routerinfo_t *router;
  559. tor_assert(digest);
  560. if (!routerlist) return NULL;
  561. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  562. router = smartlist_get(routerlist->routers, i);
  563. if (0 == memcmp(router->identity_digest, digest, DIGEST_LEN))
  564. return router;
  565. }
  566. return NULL;
  567. }
  568. /** Set *<b>prouterlist</b> to the current list of all known routers. */
  569. void router_get_routerlist(routerlist_t **prouterlist) {
  570. *prouterlist = routerlist;
  571. }
  572. /** Return the publication time on the current routerlist, or 0 if we have no
  573. * routerlist. */
  574. time_t routerlist_get_published_time(void) {
  575. return routerlist ? routerlist->published_on : 0;
  576. }
  577. /** Free all storage held by <b>router</b>. */
  578. void routerinfo_free(routerinfo_t *router)
  579. {
  580. if (!router)
  581. return;
  582. tor_free(router->address);
  583. tor_free(router->nickname);
  584. tor_free(router->platform);
  585. if (router->onion_pkey)
  586. crypto_free_pk_env(router->onion_pkey);
  587. if (router->identity_pkey)
  588. crypto_free_pk_env(router->identity_pkey);
  589. if (router->declared_family) {
  590. SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
  591. smartlist_free(router->declared_family);
  592. }
  593. addr_policy_free(router->exit_policy);
  594. tor_free(router);
  595. }
  596. /** Allocate a fresh copy of <b>router</b> */
  597. routerinfo_t *routerinfo_copy(const routerinfo_t *router)
  598. {
  599. routerinfo_t *r;
  600. addr_policy_t **e, *tmp;
  601. r = tor_malloc(sizeof(routerinfo_t));
  602. memcpy(r, router, sizeof(routerinfo_t));
  603. r->address = tor_strdup(r->address);
  604. r->nickname = tor_strdup(r->nickname);
  605. r->platform = tor_strdup(r->platform);
  606. if (r->onion_pkey)
  607. r->onion_pkey = crypto_pk_dup_key(r->onion_pkey);
  608. if (r->identity_pkey)
  609. r->identity_pkey = crypto_pk_dup_key(r->identity_pkey);
  610. e = &r->exit_policy;
  611. while (*e) {
  612. tmp = tor_malloc(sizeof(addr_policy_t));
  613. memcpy(tmp,*e,sizeof(addr_policy_t));
  614. *e = tmp;
  615. (*e)->string = tor_strdup((*e)->string);
  616. e = & ((*e)->next);
  617. }
  618. if (r->declared_family) {
  619. r->declared_family = smartlist_create();
  620. SMARTLIST_FOREACH(router->declared_family, const char *, s,
  621. smartlist_add(r->declared_family, tor_strdup(s)));
  622. }
  623. return r;
  624. }
  625. /** Free all storage held by a routerlist <b>rl</b> */
  626. void routerlist_free(routerlist_t *rl)
  627. {
  628. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
  629. routerinfo_free(r));
  630. smartlist_free(rl->routers);
  631. tor_free(rl->software_versions);
  632. tor_free(rl);
  633. }
  634. /** Mark the router with ID <b>digest</b> as non-running in our routerlist. */
  635. void router_mark_as_down(const char *digest) {
  636. routerinfo_t *router;
  637. tor_assert(digest);
  638. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
  639. if (!memcmp(d->digest, digest, DIGEST_LEN))
  640. d->is_running = 0);
  641. router = router_get_by_digest(digest);
  642. if (!router) /* we don't seem to know about him in the first place */
  643. return;
  644. log_fn(LOG_DEBUG,"Marking router '%s' as down.",router->nickname);
  645. if (router_is_me(router))
  646. log_fn(LOG_WARN, "We just marked ourself as down. Are your external addresses reachable?");
  647. router->is_running = 0;
  648. router->status_set_at = time(NULL);
  649. }
  650. /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
  651. * older entries (if any) with the same name. Note: Callers should not hold
  652. * their pointers to <b>router</b> after invoking this function; <b>router</b>
  653. * will either be inserted into the routerlist or freed. Returns 0 if the
  654. * router was added; -1 if it was not.
  655. */
  656. static int
  657. router_add_to_routerlist(routerinfo_t *router) {
  658. int i;
  659. routerinfo_t *r;
  660. char id_digest[DIGEST_LEN];
  661. tor_assert(routerlist);
  662. crypto_pk_get_digest(router->identity_pkey, id_digest);
  663. /* If we have a router with this name, and the identity key is the same,
  664. * choose the newer one. If the identity key has changed, drop the router.
  665. */
  666. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  667. r = smartlist_get(routerlist->routers, i);
  668. if (!crypto_pk_cmp_keys(router->identity_pkey, r->identity_pkey)) {
  669. if (router->published_on > r->published_on) {
  670. log_fn(LOG_DEBUG, "Replacing entry for router '%s/%s' [%s]",
  671. router->nickname, r->nickname, hex_str(id_digest,DIGEST_LEN));
  672. /* Remember whether we trust this router as a dirserver. */
  673. /* If the address hasn't changed; no need to re-resolve. */
  674. if (!strcasecmp(r->address, router->address))
  675. router->addr = r->addr;
  676. routerinfo_free(r);
  677. smartlist_set(routerlist->routers, i, router);
  678. return 0;
  679. } else {
  680. log_fn(LOG_DEBUG, "Skipping old entry for router '%s'",
  681. router->nickname);
  682. /* Update the is_running status to whatever we were told. */
  683. r->is_running = router->is_running;
  684. routerinfo_free(router);
  685. return -1;
  686. }
  687. } else if (!strcasecmp(router->nickname, r->nickname)) {
  688. /* nicknames match, keys don't. */
  689. if (router->is_verified) {
  690. /* The new verified router replaces the old one; remove the
  691. * old one. And carry on to the end of the list, in case
  692. * there are more old unverified routers with this nickname
  693. */
  694. /* mark-for-close connections using the old key, so we can
  695. * make new ones with the new key.
  696. */
  697. connection_t *conn;
  698. while ((conn = connection_get_by_identity_digest(r->identity_digest,
  699. CONN_TYPE_OR))) {
  700. log_fn(LOG_INFO,"Closing conn to obsolete router '%s'", r->nickname);
  701. connection_mark_for_close(conn);
  702. }
  703. routerinfo_free(r);
  704. smartlist_del_keeporder(routerlist->routers, i--);
  705. } else if (r->is_verified) {
  706. /* Can't replace a verified router with an unverified one. */
  707. log_fn(LOG_DEBUG, "Skipping unverified entry for verified router '%s'",
  708. router->nickname);
  709. routerinfo_free(router);
  710. return -1;
  711. }
  712. }
  713. }
  714. /* We haven't seen a router with this name before. Add it to the end of
  715. * the list. */
  716. smartlist_add(routerlist->routers, router);
  717. return 0;
  718. }
  719. /** Remove any routers from the routerlist that are more than <b>age</b>
  720. * seconds old.
  721. *
  722. * (This function is just like dirserv_remove_old_servers. One day we should
  723. * merge them.)
  724. */
  725. void
  726. routerlist_remove_old_routers(int age)
  727. {
  728. int i;
  729. time_t cutoff;
  730. routerinfo_t *router;
  731. if (!routerlist)
  732. return;
  733. cutoff = time(NULL) - age;
  734. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  735. router = smartlist_get(routerlist->routers, i);
  736. if (router->published_on <= cutoff) {
  737. /* Too old. Remove it. */
  738. log_fn(LOG_INFO,"Forgetting obsolete routerinfo for router '%s'", router->nickname);
  739. routerinfo_free(router);
  740. smartlist_del(routerlist->routers, i--);
  741. }
  742. }
  743. }
  744. /*
  745. * Code to parse router descriptors and directories.
  746. */
  747. /** Add to the current routerlist each router stored in the
  748. * signed directory <b>s</b>. If pkey is provided, check the signature against
  749. * pkey; else check against the pkey of the signing directory server. */
  750. int router_load_routerlist_from_directory(const char *s,
  751. crypto_pk_env_t *pkey,
  752. int dir_is_recent)
  753. {
  754. routerlist_t *new_list = NULL;
  755. if (router_parse_routerlist_from_directory(s, &new_list, pkey,
  756. dir_is_recent)) {
  757. log_fn(LOG_WARN, "Couldn't parse directory.");
  758. return -1;
  759. }
  760. if (routerlist) {
  761. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  762. router_add_to_routerlist(r));
  763. smartlist_clear(new_list->routers);
  764. routerlist->published_on = new_list->published_on;
  765. tor_free(routerlist->software_versions);
  766. routerlist->software_versions = new_list->software_versions;
  767. new_list->software_versions = NULL;
  768. routerlist_free(new_list);
  769. } else {
  770. routerlist = new_list;
  771. }
  772. if (router_resolve_routerlist(routerlist)) {
  773. log_fn(LOG_WARN, "Error resolving routerlist");
  774. return -1;
  775. }
  776. if (get_options()->AuthoritativeDir) {
  777. /* Learn about the descriptors in the directory. */
  778. dirserv_load_from_directory_string(s);
  779. }
  780. return 0;
  781. }
  782. /** Helper function: resolve the hostname for <b>router</b>. */
  783. static int
  784. router_resolve(routerinfo_t *router)
  785. {
  786. if (tor_lookup_hostname(router->address, &router->addr) != 0
  787. || !router->addr) {
  788. log_fn(LOG_WARN,"Could not resolve address for router '%s' at %s",
  789. router->nickname, router->address);
  790. return -1;
  791. }
  792. router->addr = ntohl(router->addr); /* get it back into host order */
  793. return 0;
  794. }
  795. /** Helper function: resolve every router in rl, and ensure that our own
  796. * routerinfo is at the front.
  797. */
  798. static int
  799. router_resolve_routerlist(routerlist_t *rl)
  800. {
  801. int i, remove;
  802. routerinfo_t *r;
  803. if (!rl)
  804. rl = routerlist;
  805. i = 0;
  806. if ((r = router_get_my_routerinfo())) {
  807. smartlist_insert(rl->routers, 0, routerinfo_copy(r));
  808. ++i;
  809. }
  810. for ( ; i < smartlist_len(rl->routers); ++i) {
  811. remove = 0;
  812. r = smartlist_get(rl->routers,i);
  813. if (router_is_me(r)) {
  814. remove = 1;
  815. } else if (r->addr) {
  816. /* already resolved. */
  817. } else if (router_resolve(r)) {
  818. log_fn(LOG_WARN, "Couldn't resolve router '%s' at '%s'; not using",
  819. r->nickname, r->address);
  820. remove = 1;
  821. }
  822. if (remove) {
  823. routerinfo_free(r);
  824. smartlist_del_keeporder(rl->routers, i--);
  825. }
  826. }
  827. return 0;
  828. }
  829. /** Decide whether a given addr:port is definitely accepted, definitely
  830. * rejected, or neither by a given policy. If <b>addr</b> is 0, we
  831. * don't know the IP of the target address. If <b>port</b> is 0, we
  832. * don't know the port of the target address.
  833. *
  834. * Returns -1 for "rejected", 0 for "accepted", 1 for "maybe" (since IP or
  835. * port is unknown).
  836. */
  837. int router_compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
  838. addr_policy_t *policy)
  839. {
  840. int maybe_reject = 0;
  841. int maybe_accept = 0;
  842. int match = 0;
  843. int maybe = 0;
  844. addr_policy_t *tmpe;
  845. for (tmpe=policy; tmpe; tmpe=tmpe->next) {
  846. // log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
  847. maybe = 0;
  848. if (!addr) {
  849. /* Address is unknown. */
  850. if ((port >= tmpe->prt_min && port <= tmpe->prt_max) ||
  851. (!port && tmpe->prt_min<=1 && tmpe->prt_max>=65535)) {
  852. /* The port definitely matches. */
  853. if (tmpe->msk == 0) {
  854. match = 1;
  855. } else {
  856. maybe = 1;
  857. }
  858. } else if (!port) {
  859. /* The port maybe matches. */
  860. maybe = 1;
  861. }
  862. } else {
  863. /* Address is known */
  864. if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
  865. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  866. /* Exact match for the policy */
  867. match = 1;
  868. } else if (!port) {
  869. maybe = 1;
  870. }
  871. }
  872. }
  873. if (maybe) {
  874. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  875. maybe_reject = 1;
  876. else
  877. maybe_accept = 1;
  878. }
  879. if (match) {
  880. // struct in_addr in;
  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. }