routerlist.c 39 KB

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