routerlist.c 39 KB

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