routerlist.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /* Copyright 2001-2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /**
  6. * \file routerlist.c
  7. *
  8. * \brief Code to
  9. * maintain and access the global list of routerinfos for known
  10. * servers.
  11. **/
  12. /****************************************************************************/
  13. extern or_options_t options; /**< command-line and config-file options */
  14. /* ********************************************************************** */
  15. /* static function prototypes */
  16. static routerinfo_t *router_pick_directory_server_impl(void);
  17. static int router_resolve_routerlist(routerlist_t *dir);
  18. /****************************************************************************/
  19. /****
  20. * Functions to manage and access our list of known routers. (Note:
  21. * dirservers maintain a separate, independent list of known router
  22. * descriptors.)
  23. *****/
  24. /** Global list of all of the routers that we, as an OR or OP, know about. */
  25. static routerlist_t *routerlist = NULL;
  26. extern int has_fetched_directory; /**< from main.c */
  27. /** Try to find a running dirserver. If there are no running dirservers
  28. * in our routerlist, reload the routerlist and try again. */
  29. routerinfo_t *router_pick_directory_server(void) {
  30. routerinfo_t *choice;
  31. choice = router_pick_directory_server_impl();
  32. if(!choice) {
  33. log_fn(LOG_WARN,"No dirservers known. Reloading and trying again.");
  34. has_fetched_directory=0; /* reset it */
  35. routerlist_clear_trusted_directories();
  36. if(options.RouterFile) {
  37. if(router_load_routerlist_from_file(options.RouterFile, 1) < 0)
  38. return NULL;
  39. } else {
  40. if(config_assign_default_dirservers() < 0)
  41. return NULL;
  42. }
  43. /* give it another try */
  44. choice = router_pick_directory_server_impl();
  45. }
  46. return choice;
  47. }
  48. /** Pick a random running router that's a trusted dirserver from our
  49. * routerlist. */
  50. static routerinfo_t *router_pick_directory_server_impl(void) {
  51. int i;
  52. routerinfo_t *router, *dirserver=NULL;
  53. smartlist_t *sl;
  54. if(!routerlist)
  55. return NULL;
  56. /* Find all the running dirservers we know about. */
  57. sl = smartlist_create();
  58. for(i=0;i< smartlist_len(routerlist->routers); i++) {
  59. router = smartlist_get(routerlist->routers, i);
  60. if(router->is_running && router->is_trusted_dir) {
  61. tor_assert(router->dir_port > 0);
  62. smartlist_add(sl, router);
  63. }
  64. }
  65. router = smartlist_choose(sl);
  66. smartlist_free(sl);
  67. if(router)
  68. return router;
  69. log_fn(LOG_INFO,"No dirservers are reachable. Trying them all again.");
  70. /* No running dir servers found? go through and mark them all as up,
  71. * so we cycle through the list again. */
  72. for(i=0; i < smartlist_len(routerlist->routers); i++) {
  73. router = smartlist_get(routerlist->routers, i);
  74. if(router->is_trusted_dir) {
  75. tor_assert(router->dir_port > 0);
  76. router->is_running = 1;
  77. dirserver = router;
  78. }
  79. }
  80. if(!dirserver)
  81. log_fn(LOG_WARN,"No dirservers in directory! Returning NULL.");
  82. return dirserver;
  83. }
  84. /** Given a comma-and-whitespace separated list of nicknames, see which
  85. * nicknames in <b>list</b> name routers in our routerlist that are
  86. * currently running. Add the routerinfos for those routers to <b>sl</b>.
  87. */
  88. void add_nickname_list_to_smartlist(smartlist_t *sl, const char *list) {
  89. const char *start,*end;
  90. char nick[MAX_NICKNAME_LEN+1];
  91. routerinfo_t *router;
  92. tor_assert(sl);
  93. tor_assert(list);
  94. while(isspace((int)*list) || *list==',') list++;
  95. start = list;
  96. while(*start) {
  97. end=start; while(*end && !isspace((int)*end) && *end != ',') end++;
  98. memcpy(nick,start,end-start);
  99. nick[end-start] = 0; /* null terminate it */
  100. router = router_get_by_nickname(nick);
  101. if (router) {
  102. if (router->is_running)
  103. smartlist_add(sl,router);
  104. else
  105. log_fn(LOG_INFO,"Nickname list includes '%s' which is known but down.",nick);
  106. } else
  107. log_fn(has_fetched_directory ? LOG_WARN : LOG_INFO,
  108. "Nickname list includes '%s' which isn't a known router.",nick);
  109. while(isspace((int)*end) || *end==',') end++;
  110. start = end;
  111. }
  112. }
  113. /** Add every router from our routerlist that is currently running to
  114. * <b>sl</b>.
  115. */
  116. void router_add_running_routers_to_smartlist(smartlist_t *sl) {
  117. routerinfo_t *router;
  118. int i;
  119. if(!routerlist)
  120. return;
  121. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  122. router = smartlist_get(routerlist->routers, i);
  123. if(router->is_running &&
  124. (!options.ORPort ||
  125. connection_twin_get_by_addr_port(router->addr, router->or_port) ))
  126. smartlist_add(sl, router);
  127. }
  128. }
  129. /** Return a random running router from the routerlist. If any node
  130. * named in <b>preferred</b> is available, pick one of those. Never pick a
  131. * node named in <b>excluded</b>, or whose routerinfo is in
  132. * <b>excludedsmartlist</b>, even if they are the only nodes available.
  133. */
  134. routerinfo_t *router_choose_random_node(char *preferred, char *excluded,
  135. smartlist_t *excludedsmartlist)
  136. {
  137. smartlist_t *sl, *excludednodes;
  138. routerinfo_t *choice;
  139. excludednodes = smartlist_create();
  140. add_nickname_list_to_smartlist(excludednodes,excluded);
  141. /* try the nodes in RendNodes first */
  142. sl = smartlist_create();
  143. add_nickname_list_to_smartlist(sl,preferred);
  144. smartlist_subtract(sl,excludednodes);
  145. if(excludedsmartlist)
  146. smartlist_subtract(sl,excludedsmartlist);
  147. choice = smartlist_choose(sl);
  148. smartlist_free(sl);
  149. if(!choice) {
  150. sl = smartlist_create();
  151. router_add_running_routers_to_smartlist(sl);
  152. smartlist_subtract(sl,excludednodes);
  153. if(excludedsmartlist)
  154. smartlist_subtract(sl,excludedsmartlist);
  155. choice = smartlist_choose(sl);
  156. smartlist_free(sl);
  157. }
  158. smartlist_free(excludednodes);
  159. if(!choice)
  160. log_fn(LOG_WARN,"No available nodes when trying to choose node. Failing.");
  161. return choice;
  162. }
  163. /** Return the router in our routerlist whose address is <b>addr</b> and
  164. * whose OR port is <b>port</b>. Return NULL if no such router is known.
  165. */
  166. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  167. int i;
  168. routerinfo_t *router;
  169. tor_assert(routerlist);
  170. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  171. router = smartlist_get(routerlist->routers, i);
  172. if ((router->addr == addr) && (router->or_port == port))
  173. return router;
  174. }
  175. return NULL;
  176. }
  177. /** Return the router in our routerlist whose nickname is <b>nickname</b>
  178. * (case insensitive). Return NULL if no such router is known.
  179. */
  180. routerinfo_t *router_get_by_nickname(char *nickname)
  181. {
  182. int i;
  183. routerinfo_t *router;
  184. tor_assert(nickname);
  185. if (!routerlist)
  186. return NULL;
  187. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  188. router = smartlist_get(routerlist->routers, i);
  189. if (0 == strcasecmp(router->nickname, nickname))
  190. return router;
  191. }
  192. return NULL;
  193. }
  194. /** Set *<b>prouterlist</b> to the current list of all known routers. */
  195. void router_get_routerlist(routerlist_t **prouterlist) {
  196. *prouterlist = routerlist;
  197. }
  198. /** Free all storage held by <b>router</b>. */
  199. void routerinfo_free(routerinfo_t *router)
  200. {
  201. if (!router)
  202. return;
  203. tor_free(router->address);
  204. tor_free(router->nickname);
  205. tor_free(router->platform);
  206. if (router->onion_pkey)
  207. crypto_free_pk_env(router->onion_pkey);
  208. if (router->identity_pkey)
  209. crypto_free_pk_env(router->identity_pkey);
  210. exit_policy_free(router->exit_policy);
  211. free(router);
  212. }
  213. /** Allocate a fresh copy of <b>router</b> */
  214. routerinfo_t *routerinfo_copy(const routerinfo_t *router)
  215. {
  216. routerinfo_t *r;
  217. struct exit_policy_t **e, *tmp;
  218. r = tor_malloc(sizeof(routerinfo_t));
  219. memcpy(r, router, sizeof(routerinfo_t));
  220. r->address = tor_strdup(r->address);
  221. r->nickname = tor_strdup(r->nickname);
  222. r->platform = tor_strdup(r->platform);
  223. if (r->onion_pkey)
  224. r->onion_pkey = crypto_pk_dup_key(r->onion_pkey);
  225. if (r->identity_pkey)
  226. r->identity_pkey = crypto_pk_dup_key(r->identity_pkey);
  227. e = &r->exit_policy;
  228. while (*e) {
  229. tmp = tor_malloc(sizeof(struct exit_policy_t));
  230. memcpy(tmp,*e,sizeof(struct exit_policy_t));
  231. *e = tmp;
  232. (*e)->string = tor_strdup((*e)->string);
  233. e = & ((*e)->next);
  234. }
  235. return r;
  236. }
  237. /** Free all storage held by a routerlist <b>rl</b> */
  238. void routerlist_free(routerlist_t *rl)
  239. {
  240. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
  241. routerinfo_free(r));
  242. smartlist_free(rl->routers);
  243. tor_free(rl->software_versions);
  244. tor_free(rl);
  245. }
  246. /** Mark the router named <b>nickname</b> as non-running in our routerlist. */
  247. void router_mark_as_down(char *nickname) {
  248. routerinfo_t *router;
  249. tor_assert(nickname);
  250. router = router_get_by_nickname(nickname);
  251. if(!router) /* we don't seem to know about him in the first place */
  252. return;
  253. log_fn(LOG_DEBUG,"Marking %s as down.",router->nickname);
  254. router->is_running = 0;
  255. }
  256. /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
  257. * older entries (if any) with the same name. Note: Callers should not hold
  258. * their pointers to <b>router</b> after invoking this function; <b>router</b>
  259. * will either be inserted into the routerlist or freed. Returns 0 if the
  260. * router was added; -1 if it was not.
  261. */
  262. int router_add_to_routerlist(routerinfo_t *router) {
  263. int i;
  264. routerinfo_t *r;
  265. /* If we have a router with this name, and the identity key is the same,
  266. * choose the newer one. If the identity key has changed, drop the router.
  267. */
  268. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  269. r = smartlist_get(routerlist->routers, i);
  270. if (!strcasecmp(router->nickname, r->nickname)) {
  271. if (!crypto_pk_cmp_keys(router->identity_pkey, r->identity_pkey)) {
  272. if (router->published_on > r->published_on) {
  273. log_fn(LOG_DEBUG, "Replacing entry for router '%s'",
  274. router->nickname);
  275. /* Remember whether we trust this router as a dirserver. */
  276. if (r->is_trusted_dir)
  277. router->is_trusted_dir = 1;
  278. /* If the address hasn't changed; no need to re-resolve. */
  279. if (!strcasecmp(r->address, router->address))
  280. router->addr = r->addr;
  281. routerinfo_free(r);
  282. smartlist_set(routerlist->routers, i, router);
  283. return 0;
  284. } else {
  285. log_fn(LOG_DEBUG, "Skipping old entry for router '%s'",
  286. router->nickname);
  287. /* If we now trust 'router', then we trust the one in the routerlist
  288. * too. */
  289. if (router->is_trusted_dir)
  290. r->is_trusted_dir = 1;
  291. /* Update the is_running status to whatever we were told. */
  292. r->is_running = router->is_running;
  293. routerinfo_free(router);
  294. return -1;
  295. }
  296. } else {
  297. log_fn(LOG_WARN, "Identity key mismatch for router '%s'",
  298. router->nickname);
  299. routerinfo_free(router);
  300. return -1;
  301. }
  302. }
  303. }
  304. /* We haven't seen a router with this name before. Add it to the end of
  305. * the list. */
  306. smartlist_add(routerlist->routers, router);
  307. return 0;
  308. }
  309. /** Remove any routers from the routerlist that are more than ROUTER_MAX_AGE
  310. * seconds old.
  311. *
  312. * (This function is just like dirserv_remove_old_servers. One day we should
  313. * merge them.)
  314. */
  315. void
  316. routerlist_remove_old_routers(void)
  317. {
  318. int i;
  319. time_t cutoff;
  320. routerinfo_t *router;
  321. if (!routerlist)
  322. return;
  323. cutoff = time(NULL) - ROUTER_MAX_AGE;
  324. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  325. router = smartlist_get(routerlist->routers, i);
  326. if (router->published_on < cutoff &&
  327. !router->dir_port) {
  328. /* Too old. Remove it. But never remove dirservers! */
  329. log_fn(LOG_INFO,"Forgetting obsolete routerinfo for node %s.", router->nickname);
  330. routerinfo_free(router);
  331. smartlist_del(routerlist->routers, i--);
  332. }
  333. }
  334. }
  335. /*
  336. * Code to parse router descriptors and directories.
  337. */
  338. /** Update the current router list with the one stored in
  339. * <b>routerfile</b>. If <b>trusted</b> is true, then we'll use
  340. * directory servers from the file. */
  341. int router_load_routerlist_from_file(char *routerfile, int trusted)
  342. {
  343. char *string;
  344. string = read_file_to_str(routerfile);
  345. if(!string) {
  346. log_fn(LOG_WARN,"Failed to load routerfile %s.",routerfile);
  347. return -1;
  348. }
  349. if(router_load_routerlist_from_string(string, trusted) < 0) {
  350. log_fn(LOG_WARN,"The routerfile itself was corrupt.");
  351. free(string);
  352. return -1;
  353. }
  354. /* dump_onion_keys(LOG_NOTICE); */
  355. free(string);
  356. return 0;
  357. }
  358. /** Mark all directories in the routerlist as nontrusted. */
  359. void routerlist_clear_trusted_directories(void)
  360. {
  361. if (!routerlist) return;
  362. SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
  363. r->is_trusted_dir = 0);
  364. }
  365. /** Helper function: read routerinfo elements from s, and throw out the
  366. * ones that don't parse and resolve. Add all remaining elements to the
  367. * routerlist. If <b>trusted</b> is true, then we'll use
  368. * directory servers from the string
  369. */
  370. int router_load_routerlist_from_string(const char *s, int trusted)
  371. {
  372. routerlist_t *new_list=NULL;
  373. if (router_parse_list_from_string(&s, &new_list, -1, NULL)) {
  374. log(LOG_WARN, "Error parsing router file");
  375. return -1;
  376. }
  377. if (trusted) {
  378. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  379. if (r->dir_port) r->is_trusted_dir = 1);
  380. }
  381. if (routerlist) {
  382. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  383. router_add_to_routerlist(r));
  384. smartlist_clear(new_list->routers);
  385. routerlist_free(new_list);
  386. } else {
  387. routerlist = new_list;
  388. }
  389. if (router_resolve_routerlist(routerlist)) {
  390. log(LOG_WARN, "Error resolving routerlist");
  391. return -1;
  392. }
  393. /* dump_onion_keys(LOG_NOTICE); */
  394. return 0;
  395. }
  396. /** Add to the current routerlist each router stored in the
  397. * signed directory <b>s</b>. If pkey is provided, check the signature against
  398. * pkey; else check against the pkey of the signing directory server. */
  399. int router_load_routerlist_from_directory(const char *s,
  400. crypto_pk_env_t *pkey)
  401. {
  402. routerlist_t *new_list = NULL;
  403. check_software_version_against_directory(s, options.IgnoreVersion);
  404. if (router_parse_routerlist_from_directory(s, &new_list, pkey)) {
  405. log_fn(LOG_WARN, "Couldn't parse directory.");
  406. return -1;
  407. }
  408. if (routerlist) {
  409. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  410. router_add_to_routerlist(r));
  411. smartlist_clear(new_list->routers);
  412. routerlist->published_on = new_list->published_on;
  413. tor_free(routerlist->software_versions);
  414. routerlist->software_versions = new_list->software_versions;
  415. new_list->software_versions = NULL;
  416. routerlist_free(new_list);
  417. } else {
  418. routerlist = new_list;
  419. }
  420. if (router_resolve_routerlist(routerlist)) {
  421. log_fn(LOG_WARN, "Error resolving routerlist");
  422. return -1;
  423. }
  424. /* Remember the directory, if we're nonauthoritative.*/
  425. dirserv_set_cached_directory(s, routerlist->published_on);
  426. /* Learn about the descriptors in the directory, if we're authoritative */
  427. if (options.AuthoritativeDir)
  428. dirserv_load_from_directory_string(s);
  429. return 0;
  430. }
  431. /** Helper function: resolve the hostname for <b>router</b>. */
  432. static int
  433. router_resolve(routerinfo_t *router)
  434. {
  435. if (tor_lookup_hostname(router->address, &router->addr) != 0) {
  436. log_fn(LOG_WARN,"Could not get address for router %s (%s).",
  437. router->address, router->nickname);
  438. return -1;
  439. }
  440. router->addr = ntohl(router->addr); /* get it back into host order */
  441. return 0;
  442. }
  443. /** Helper function: resolve every router in rl, and ensure that our own
  444. * routerinfo is at the front.
  445. */
  446. static int
  447. router_resolve_routerlist(routerlist_t *rl)
  448. {
  449. int i, remove;
  450. routerinfo_t *r;
  451. if (!rl)
  452. rl = routerlist;
  453. i = 0;
  454. if ((r = router_get_my_routerinfo())) {
  455. smartlist_insert(rl->routers, 0, routerinfo_copy(r));
  456. ++i;
  457. }
  458. for ( ; i < smartlist_len(rl->routers); ++i) {
  459. remove = 0;
  460. r = smartlist_get(rl->routers,i);
  461. if (router_is_me(r)) {
  462. remove = 1;
  463. } else if (r->addr) {
  464. /* already resolved. */
  465. } else if (router_resolve(r)) {
  466. log_fn(LOG_WARN, "Couldn't resolve router %s; not using", r->address);
  467. remove = 1;
  468. }
  469. if (remove) {
  470. routerinfo_free(r);
  471. smartlist_del_keeporder(rl->routers, i--);
  472. }
  473. }
  474. return 0;
  475. }
  476. /** Decide whether a given addr:port is definitely accepted, definitely
  477. * rejected, or neither by a given exit policy. If <b>addr</b> is 0, we
  478. * don't know the IP of the target address.
  479. *
  480. * Returns -1 for "rejected", 0 for "accepted", 1 for "maybe" (since IP is
  481. * unknown).
  482. */
  483. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  484. struct exit_policy_t *policy)
  485. {
  486. int maybe_reject = 0;
  487. int maybe_accept = 0;
  488. int match = 0;
  489. int maybe = 0;
  490. struct in_addr in;
  491. struct exit_policy_t *tmpe;
  492. for(tmpe=policy; tmpe; tmpe=tmpe->next) {
  493. // log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
  494. maybe = 0;
  495. if (!addr) {
  496. /* Address is unknown. */
  497. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  498. /* The port definitely matches. */
  499. if (tmpe->msk == 0) {
  500. match = 1;
  501. } else {
  502. maybe = 1;
  503. }
  504. } else if (!port) {
  505. /* The port maybe matches. */
  506. maybe = 1;
  507. }
  508. } else {
  509. /* Address is known */
  510. if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
  511. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  512. /* Exact match for the policy */
  513. match = 1;
  514. } else if (!port) {
  515. maybe = 1;
  516. }
  517. }
  518. }
  519. if (maybe) {
  520. if (tmpe->policy_type == EXIT_POLICY_REJECT)
  521. maybe_reject = 1;
  522. else
  523. maybe_accept = 1;
  524. }
  525. if (match) {
  526. in.s_addr = htonl(addr);
  527. log_fn(LOG_DEBUG,"Address %s:%d matches exit policy '%s'",
  528. inet_ntoa(in), port, tmpe->string);
  529. if(tmpe->policy_type == EXIT_POLICY_ACCEPT) {
  530. /* If we already hit a clause that might trigger a 'reject', than we
  531. * can't be sure of this certain 'accept'.*/
  532. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  533. } else {
  534. return maybe_accept ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_REJECTED;
  535. }
  536. }
  537. }
  538. /* accept all by default. */
  539. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  540. }
  541. /** Return 1 if all running routers will reject addr:port, return 0 if
  542. * any might accept it. */
  543. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port) {
  544. int i;
  545. routerinfo_t *router;
  546. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  547. router = smartlist_get(routerlist->routers, i);
  548. if (router->is_running && router_compare_addr_to_exit_policy(
  549. addr, port, router->exit_policy) != ADDR_POLICY_REJECTED)
  550. return 0; /* this one could be ok. good enough. */
  551. }
  552. return 1; /* all will reject. */
  553. }
  554. /** Return true iff <b>router</b> does not permit exit streams.
  555. */
  556. int router_exit_policy_rejects_all(routerinfo_t *router) {
  557. return router_compare_addr_to_exit_policy(0, 0, router->exit_policy)
  558. == ADDR_POLICY_REJECTED;
  559. }
  560. /* DODCDOC */
  561. void running_routers_free(running_routers_t *rr)
  562. {
  563. tor_assert(rr);
  564. if (rr->running_routers) {
  565. SMARTLIST_FOREACH(rr->running_routers, char *, s, tor_free(s));
  566. smartlist_free(rr->running_routers);
  567. }
  568. tor_free(rr);
  569. }
  570. /* DOCDOC*/
  571. void routerlist_update_from_runningrouters(routerlist_t *list,
  572. running_routers_t *rr)
  573. {
  574. int n_routers, n_names, i, j, running;
  575. routerinfo_t *router;
  576. const char *name;
  577. if (!routerlist)
  578. return;
  579. if (routerlist->published_on >= rr->published_on)
  580. return;
  581. if (routerlist->running_routers_updated_on >= rr->published_on)
  582. return;
  583. n_routers = smartlist_len(list->routers);
  584. n_names = smartlist_len(rr->running_routers);
  585. for (i=0; i<n_routers; ++i) {
  586. running = 0;
  587. router = smartlist_get(list->routers, i);
  588. for (j=0; j<n_names; ++j) {
  589. name = smartlist_get(rr->running_routers, j);
  590. if (!strcmp(name, router->nickname)) {
  591. running=1;
  592. break;
  593. }
  594. }
  595. router->is_running = 1; /* arma: is this correct? */
  596. }
  597. routerlist->running_routers_updated_on = rr->published_on;
  598. /* XXXX008 Should there also be a list of which are down, so that we
  599. * don't mark merely unknown routers as down? */
  600. }
  601. /*
  602. Local Variables:
  603. mode:c
  604. indent-tabs-mode:nil
  605. c-basic-offset:2
  606. End:
  607. */