routerlist.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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 *
  17. router_pick_directory_server_impl(int requireauth, int requireothers, int fascistfirewall);
  18. static void mark_all_authdirservers_up(void);
  19. static int router_resolve_routerlist(routerlist_t *dir);
  20. /****************************************************************************/
  21. /** List of digests of keys for servers that are trusted directories. */
  22. static smartlist_t *trusted_dir_digests = NULL;
  23. /****
  24. * Functions to manage and access our list of known routers. (Note:
  25. * dirservers maintain a separate, independent list of known router
  26. * descriptors.)
  27. *****/
  28. /** Global list of all of the routers that we, as an OR or OP, know about. */
  29. static routerlist_t *routerlist = NULL;
  30. extern int has_fetched_directory; /**< from main.c */
  31. /**
  32. * Reload the original list of trusted dirservers, and the most recent
  33. * cached directory (if present).
  34. */
  35. int router_reload_router_list(void)
  36. {
  37. char filename[512];
  38. routerlist_clear_trusted_directories();
  39. if (options.RouterFile) {
  40. if (router_load_routerlist_from_file(options.RouterFile, 1) < 0) {
  41. log_fn(LOG_ERR,"Error loading router list.");
  42. return -1;
  43. }
  44. } else {
  45. if (config_assign_default_dirservers() < 0)
  46. return -1;
  47. }
  48. if (get_data_directory(&options)) {
  49. char *s;
  50. sprintf(filename,"%s/cached-directory", get_data_directory(&options));
  51. s = read_file_to_str(filename,0);
  52. if (s) {
  53. log_fn(LOG_INFO, "Loading cached directory from %s", filename);
  54. if (router_load_routerlist_from_string(s, 0) < 0) {
  55. log_fn(LOG_WARN, "Cached directory was unparseable; ignoring.");
  56. }
  57. }
  58. }
  59. return 0;
  60. }
  61. /** Try to find a running dirserver. If there are no running dirservers
  62. * in our routerlist, set all the authoritative ones as running again,
  63. * and pick one. If there are no dirservers at all in our routerlist,
  64. * reload the routerlist and try one last time. */
  65. routerinfo_t *router_pick_directory_server(int requireauth, int requireothers) {
  66. routerinfo_t *choice;
  67. choice = router_pick_directory_server_impl(requireauth, requireothers, options.FascistFirewall);
  68. if(choice)
  69. return choice;
  70. log_fn(LOG_INFO,"No dirservers are reachable. Trying them all again.");
  71. /* mark all authdirservers as up again */
  72. mark_all_authdirservers_up();
  73. /* try again */
  74. choice = router_pick_directory_server_impl(requireauth, requireothers, 0);
  75. if(choice)
  76. return choice;
  77. log_fn(LOG_WARN,"Still no dirservers %s. Reloading and trying again.",
  78. options.FascistFirewall ? "reachable" : "known");
  79. has_fetched_directory=0; /* reset it */
  80. routerlist_clear_trusted_directories();
  81. if(router_reload_router_list()) {
  82. return NULL;
  83. }
  84. /* give it one last try */
  85. choice = router_pick_directory_server_impl(requireauth, requireothers, 0);
  86. return choice;
  87. }
  88. /** Pick a random running router from our routerlist. If requireauth,
  89. * it has to be a trusted server. If requireothers, it cannot be us.
  90. */
  91. static routerinfo_t *
  92. router_pick_directory_server_impl(int requireauth, int requireothers, int fascistfirewall)
  93. {
  94. int i;
  95. routerinfo_t *router;
  96. smartlist_t *sl;
  97. char buf[16];
  98. if(!routerlist)
  99. return NULL;
  100. /* Find all the running dirservers we know about. */
  101. sl = smartlist_create();
  102. for(i=0;i< smartlist_len(routerlist->routers); i++) {
  103. router = smartlist_get(routerlist->routers, i);
  104. if(!router->is_running || !router->dir_port)
  105. continue;
  106. if(requireauth && !router->is_trusted_dir)
  107. continue;
  108. if(requireothers && router_is_me(router))
  109. continue;
  110. if(fascistfirewall) {
  111. sprintf(buf,"%d",router->dir_port);
  112. if (!smartlist_string_isin(options.FirewallPorts, buf))
  113. continue;
  114. }
  115. smartlist_add(sl, router);
  116. }
  117. router = smartlist_choose(sl);
  118. smartlist_free(sl);
  119. return router;
  120. }
  121. /** Go through and mark the auth dirservers as up */
  122. static void mark_all_authdirservers_up(void) {
  123. int i;
  124. routerinfo_t *router;
  125. if(!routerlist)
  126. return;
  127. for(i=0; i < smartlist_len(routerlist->routers); i++) {
  128. router = smartlist_get(routerlist->routers, i);
  129. if(router->is_trusted_dir) {
  130. tor_assert(router->dir_port > 0);
  131. router->is_running = 1;
  132. router->status_set_at = time(NULL);
  133. }
  134. }
  135. }
  136. /** Return 0 if \exists an authoritative dirserver that's currently
  137. * thought to be running, else return 1.
  138. */
  139. int all_directory_servers_down(void) {
  140. int i;
  141. routerinfo_t *router;
  142. if(!routerlist)
  143. return 1; /* if no dirservers, I guess they're all down */
  144. for(i=0;i< smartlist_len(routerlist->routers); i++) {
  145. router = smartlist_get(routerlist->routers, i);
  146. if(router->is_running && router->is_trusted_dir) {
  147. tor_assert(router->dir_port > 0);
  148. return 0;
  149. }
  150. }
  151. return 1;
  152. }
  153. /** Add all the friends of <b>router</b> to the smartlist <b>sl</b>.
  154. */
  155. void routerlist_add_friends(smartlist_t *sl, routerinfo_t *router) {
  156. }
  157. /** Given a comma-and-whitespace separated list of nicknames, see which
  158. * nicknames in <b>list</b> name routers in our routerlist that are
  159. * currently running. Add the routerinfos for those routers to <b>sl</b>.
  160. */
  161. void
  162. add_nickname_list_to_smartlist(smartlist_t *sl, const char *list, int warn_if_down)
  163. {
  164. routerinfo_t *router;
  165. smartlist_t *nickname_list;
  166. tor_assert(sl);
  167. tor_assert(list);
  168. nickname_list = smartlist_create();
  169. smartlist_split_string(nickname_list, list, ",",
  170. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  171. SMARTLIST_FOREACH(nickname_list, const char *, nick, {
  172. if (strlen(nick) > MAX_HEX_NICKNAME_LEN) {
  173. log_fn(LOG_WARN,"Nickname too long; skipping");
  174. continue;
  175. }
  176. router = router_get_by_nickname(nick);
  177. if (router) {
  178. if (router->is_running)
  179. smartlist_add(sl,router);
  180. else
  181. log_fn(warn_if_down ? LOG_WARN : LOG_DEBUG,
  182. "Nickname list includes '%s' which is known but down.",nick);
  183. } else
  184. log_fn(has_fetched_directory ? LOG_WARN : LOG_INFO,
  185. "Nickname list includes '%s' which isn't a known router.",nick);
  186. });
  187. SMARTLIST_FOREACH(nickname_list, char *, nick, tor_free(nick));
  188. smartlist_free(nickname_list);
  189. }
  190. /** Add every router from our routerlist that is currently running to
  191. * <b>sl</b>.
  192. */
  193. static void
  194. router_add_running_routers_to_smartlist(smartlist_t *sl, int allow_unverified,
  195. int preferuptime, int preferbandwidth)
  196. {
  197. routerinfo_t *router;
  198. int i;
  199. if(!routerlist)
  200. return;
  201. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  202. router = smartlist_get(routerlist->routers, i);
  203. if(router->is_running &&
  204. (router->is_verified ||
  205. (allow_unverified &&
  206. !router_is_unreliable_router(router, preferuptime, preferbandwidth)))) {
  207. /* If it's running, and either it's verified or we're ok picking
  208. * unverified routers and this one is suitable.
  209. */
  210. smartlist_add(sl, router);
  211. }
  212. }
  213. }
  214. routerinfo_t *
  215. routerlist_find_my_routerinfo(void) {
  216. routerinfo_t *router;
  217. int i;
  218. if(!routerlist)
  219. return NULL;
  220. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  221. router = smartlist_get(routerlist->routers, i);
  222. if(router_is_me(router))
  223. return router;
  224. }
  225. return NULL;
  226. }
  227. /** How many seconds a router must be up before we'll use it for
  228. * reliability-critical node positions.
  229. */
  230. #define ROUTER_REQUIRED_MIN_UPTIME 3600 /* an hour */
  231. #define ROUTER_REQUIRED_MIN_BANDWIDTH 10000
  232. int
  233. router_is_unreliable_router(routerinfo_t *router, int need_uptime, int need_bw)
  234. {
  235. if(need_uptime && router->uptime < ROUTER_REQUIRED_MIN_UPTIME)
  236. return 1;
  237. if(need_bw && router->bandwidthcapacity < ROUTER_REQUIRED_MIN_BANDWIDTH)
  238. return 1;
  239. return 0;
  240. }
  241. static void
  242. routerlist_sl_remove_unreliable_routers(smartlist_t *sl)
  243. {
  244. int i;
  245. routerinfo_t *router;
  246. for (i = 0; i < smartlist_len(sl); ++i) {
  247. router = smartlist_get(sl, i);
  248. if(router_is_unreliable_router(router, 1, 0)) {
  249. log(LOG_DEBUG, "Router %s has insufficient uptime; deleting.",
  250. router->nickname);
  251. smartlist_del(sl, i--);
  252. }
  253. }
  254. }
  255. routerinfo_t *
  256. routerlist_sl_choose_by_bandwidth(smartlist_t *sl)
  257. {
  258. int i;
  259. routerinfo_t *router;
  260. smartlist_t *bandwidths;
  261. uint32_t this_bw, tmp, total_bw=0, rand_bw;
  262. uint32_t *p;
  263. bandwidths = smartlist_create();
  264. for (i = 0; i < smartlist_len(sl); ++i) {
  265. router = smartlist_get(sl, i);
  266. /* give capacity a default, until 0.0.7 is obsolete */
  267. tmp = (router->bandwidthcapacity == 0) ? 200000 : router->bandwidthcapacity;
  268. this_bw = (tmp < router->bandwidthrate) ? tmp : router->bandwidthrate;
  269. if(this_bw > 800000)
  270. this_bw = 800000; /* if they claim something huge, don't believe it */
  271. p = tor_malloc(sizeof(uint32_t));
  272. *p = this_bw;
  273. smartlist_add(bandwidths, p);
  274. total_bw += this_bw;
  275. // log_fn(LOG_INFO,"Recording bw %d for node %s.", this_bw, router->nickname);
  276. }
  277. if(!total_bw)
  278. return NULL;
  279. rand_bw = crypto_pseudo_rand_int(total_bw);
  280. // log_fn(LOG_INFO,"Total bw %d. Randomly chose %d.", total_bw, rand_bw);
  281. tmp = 0;
  282. for(i=0; ; i++) {
  283. tor_assert(i < smartlist_len(sl));
  284. p = smartlist_get(bandwidths, i);
  285. tmp += *p;
  286. router = smartlist_get(sl, i);
  287. // log_fn(LOG_INFO,"Considering %s. tmp = %d.", router->nickname, tmp);
  288. if(tmp >= rand_bw)
  289. break;
  290. }
  291. SMARTLIST_FOREACH(bandwidths, uint32_t*, p, tor_free(p));
  292. smartlist_free(bandwidths);
  293. router = smartlist_get(sl, i);
  294. // log_fn(LOG_INFO,"Picked %s.", router->nickname);
  295. return router;
  296. }
  297. /** Return a random running router from the routerlist. If any node
  298. * named in <b>preferred</b> is available, pick one of those. Never
  299. * pick a node named in <b>excluded</b>, or whose routerinfo is in
  300. * <b>excludedsmartlist</b>, even if they are the only nodes
  301. * available. If <b>strict</b> is true, never pick any node besides
  302. * those in <b>preferred</b>.
  303. */
  304. routerinfo_t *router_choose_random_node(char *preferred, char *excluded,
  305. smartlist_t *excludedsmartlist,
  306. int preferuptime, int preferbandwidth,
  307. int allow_unverified, int strict)
  308. {
  309. smartlist_t *sl, *excludednodes;
  310. routerinfo_t *choice;
  311. excludednodes = smartlist_create();
  312. add_nickname_list_to_smartlist(excludednodes,excluded,0);
  313. /* try the preferred nodes first */
  314. sl = smartlist_create();
  315. add_nickname_list_to_smartlist(sl,preferred,1);
  316. smartlist_subtract(sl,excludednodes);
  317. if(excludedsmartlist)
  318. smartlist_subtract(sl,excludedsmartlist);
  319. if(preferuptime)
  320. routerlist_sl_remove_unreliable_routers(sl);
  321. if(preferbandwidth)
  322. choice = routerlist_sl_choose_by_bandwidth(sl);
  323. else
  324. choice = smartlist_choose(sl);
  325. smartlist_free(sl);
  326. if(!choice && !strict) {
  327. sl = smartlist_create();
  328. router_add_running_routers_to_smartlist(sl, allow_unverified,
  329. preferuptime, preferbandwidth);
  330. smartlist_subtract(sl,excludednodes);
  331. if(excludedsmartlist)
  332. smartlist_subtract(sl,excludedsmartlist);
  333. if(preferuptime)
  334. routerlist_sl_remove_unreliable_routers(sl);
  335. if(preferbandwidth)
  336. choice = routerlist_sl_choose_by_bandwidth(sl);
  337. else
  338. choice = smartlist_choose(sl);
  339. smartlist_free(sl);
  340. }
  341. smartlist_free(excludednodes);
  342. if(!choice)
  343. log_fn(LOG_WARN,"No available nodes when trying to choose node. Failing.");
  344. return choice;
  345. }
  346. /** Return the router in our routerlist whose address is <b>addr</b> and
  347. * whose OR port is <b>port</b>. Return NULL if no such router is known.
  348. */
  349. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  350. int i;
  351. routerinfo_t *router;
  352. tor_assert(routerlist);
  353. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  354. router = smartlist_get(routerlist->routers, i);
  355. if ((router->addr == addr) && (router->or_port == port))
  356. return router;
  357. }
  358. return NULL;
  359. }
  360. /** Return true iff the digest of <b>router</b>'s identity key,
  361. * encoded in hexadecimal, matches <b>hexdigest</b> (which is
  362. * optionally prefixed with a single dollar sign). Return false if
  363. * <b>hexdigest</b> is malformed, or it doesn't match. */
  364. static INLINE int router_hex_digest_matches(routerinfo_t *router,
  365. const char *hexdigest)
  366. {
  367. char digest[DIGEST_LEN];
  368. tor_assert(hexdigest);
  369. if (hexdigest[0] == '$')
  370. ++hexdigest;
  371. if (strlen(hexdigest) != HEX_DIGEST_LEN ||
  372. base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
  373. return 0;
  374. else
  375. return (!memcmp(digest, router->identity_digest, DIGEST_LEN));
  376. }
  377. /* Return true if <b>router</b>'s nickname matches <b>nickname</b>
  378. * (case-insensitive), or if <b>router's</b> identity key digest
  379. * matches a hexadecimal value stored in <b>nickname</b>. Return
  380. * false otherwise.*/
  381. int router_nickname_matches(routerinfo_t *router, const char *nickname)
  382. {
  383. if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
  384. return 1;
  385. else
  386. return router_hex_digest_matches(router, nickname);
  387. }
  388. /** Return the router in our routerlist whose (case-insensitive)
  389. * nickname or (case-sensitive) hexadecimal key digest is
  390. * <b>nickname</b>. Return NULL if no such router is known.
  391. */
  392. routerinfo_t *router_get_by_nickname(const char *nickname)
  393. {
  394. int i, maybedigest;
  395. routerinfo_t *router;
  396. char digest[DIGEST_LEN];
  397. tor_assert(nickname);
  398. if (!routerlist)
  399. return NULL;
  400. if (nickname[0] == '$')
  401. return router_get_by_hexdigest(nickname);
  402. maybedigest = (strlen(nickname) == HEX_DIGEST_LEN) &&
  403. (base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);
  404. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  405. router = smartlist_get(routerlist->routers, i);
  406. if (0 == strcasecmp(router->nickname, nickname) ||
  407. (maybedigest && 0 == memcmp(digest, router->identity_digest,
  408. DIGEST_LEN)))
  409. return router;
  410. }
  411. return NULL;
  412. }
  413. /* XXX008 currently this trusted_dir_digests stuff is not used. */
  414. /** Return true iff <b>digest</b> is the digest of the identity key of
  415. * a trusted directory. */
  416. int router_digest_is_trusted_dir(const char *digest) {
  417. if (!trusted_dir_digests)
  418. return 0;
  419. SMARTLIST_FOREACH(trusted_dir_digests, char *, cp,
  420. if (!memcmp(digest, cp, DIGEST_LEN)) return 1);
  421. return 0;
  422. }
  423. /** Return the router in our routerlist whose hexadecimal key digest
  424. * is <b>hexdigest</b>. Return NULL if no such router is known. */
  425. routerinfo_t *router_get_by_hexdigest(const char *hexdigest) {
  426. char digest[DIGEST_LEN];
  427. tor_assert(hexdigest);
  428. if (!routerlist)
  429. return NULL;
  430. if (hexdigest[0]=='$')
  431. ++hexdigest;
  432. if (strlen(hexdigest) != HEX_DIGEST_LEN ||
  433. base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
  434. return NULL;
  435. return router_get_by_digest(digest);
  436. }
  437. /** Return the router in our routerlist whose 20-byte key digest
  438. * is <b>hexdigest</b>. Return NULL if no such router is known. */
  439. routerinfo_t *router_get_by_digest(const char *digest) {
  440. int i;
  441. routerinfo_t *router;
  442. tor_assert(digest);
  443. for(i=0;i<smartlist_len(routerlist->routers);i++) {
  444. router = smartlist_get(routerlist->routers, i);
  445. if (0 == memcmp(router->identity_digest, digest, DIGEST_LEN))
  446. return router;
  447. }
  448. return NULL;
  449. }
  450. /** Set *<b>prouterlist</b> to the current list of all known routers. */
  451. void router_get_routerlist(routerlist_t **prouterlist) {
  452. *prouterlist = routerlist;
  453. }
  454. /** Free all storage held by <b>router</b>. */
  455. void routerinfo_free(routerinfo_t *router)
  456. {
  457. if (!router)
  458. return;
  459. tor_free(router->address);
  460. tor_free(router->nickname);
  461. tor_free(router->platform);
  462. if (router->onion_pkey)
  463. crypto_free_pk_env(router->onion_pkey);
  464. if (router->identity_pkey)
  465. crypto_free_pk_env(router->identity_pkey);
  466. exit_policy_free(router->exit_policy);
  467. free(router);
  468. }
  469. /** Allocate a fresh copy of <b>router</b> */
  470. routerinfo_t *routerinfo_copy(const routerinfo_t *router)
  471. {
  472. routerinfo_t *r;
  473. struct exit_policy_t **e, *tmp;
  474. r = tor_malloc(sizeof(routerinfo_t));
  475. memcpy(r, router, sizeof(routerinfo_t));
  476. r->address = tor_strdup(r->address);
  477. r->nickname = tor_strdup(r->nickname);
  478. r->platform = tor_strdup(r->platform);
  479. if (r->onion_pkey)
  480. r->onion_pkey = crypto_pk_dup_key(r->onion_pkey);
  481. if (r->identity_pkey)
  482. r->identity_pkey = crypto_pk_dup_key(r->identity_pkey);
  483. e = &r->exit_policy;
  484. while (*e) {
  485. tmp = tor_malloc(sizeof(struct exit_policy_t));
  486. memcpy(tmp,*e,sizeof(struct exit_policy_t));
  487. *e = tmp;
  488. (*e)->string = tor_strdup((*e)->string);
  489. e = & ((*e)->next);
  490. }
  491. return r;
  492. }
  493. /** Free all storage held by a routerlist <b>rl</b> */
  494. void routerlist_free(routerlist_t *rl)
  495. {
  496. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
  497. routerinfo_free(r));
  498. smartlist_free(rl->routers);
  499. tor_free(rl->software_versions);
  500. tor_free(rl);
  501. }
  502. /** Mark the router with ID <b>digest</b> as non-running in our routerlist. */
  503. void router_mark_as_down(const char *digest) {
  504. routerinfo_t *router;
  505. tor_assert(digest);
  506. router = router_get_by_digest(digest);
  507. if(!router) /* we don't seem to know about him in the first place */
  508. return;
  509. log_fn(LOG_DEBUG,"Marking %s as down.",router->nickname);
  510. router->is_running = 0;
  511. router->status_set_at = time(NULL);
  512. }
  513. /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
  514. * older entries (if any) with the same name. Note: Callers should not hold
  515. * their pointers to <b>router</b> after invoking this function; <b>router</b>
  516. * will either be inserted into the routerlist or freed. Returns 0 if the
  517. * router was added; -1 if it was not.
  518. */
  519. int router_add_to_routerlist(routerinfo_t *router) {
  520. int i;
  521. routerinfo_t *r;
  522. char id_digest[DIGEST_LEN];
  523. crypto_pk_get_digest(router->identity_pkey, id_digest);
  524. /* If we have a router with this name, and the identity key is the same,
  525. * choose the newer one. If the identity key has changed, drop the router.
  526. */
  527. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  528. r = smartlist_get(routerlist->routers, i);
  529. if (!crypto_pk_cmp_keys(router->identity_pkey, r->identity_pkey)) {
  530. if (router->published_on > r->published_on) {
  531. log_fn(LOG_DEBUG, "Replacing entry for router '%s/%s' [%s]",
  532. router->nickname, r->nickname, hex_str(id_digest,DIGEST_LEN));
  533. /* Remember whether we trust this router as a dirserver. */
  534. if (r->is_trusted_dir)
  535. router->is_trusted_dir = 1;
  536. /* If the address hasn't changed; no need to re-resolve. */
  537. if (!strcasecmp(r->address, router->address))
  538. router->addr = r->addr;
  539. routerinfo_free(r);
  540. smartlist_set(routerlist->routers, i, router);
  541. return 0;
  542. } else {
  543. log_fn(LOG_DEBUG, "Skipping old entry for router '%s'",
  544. router->nickname);
  545. /* If we now trust 'router', then we trust the one in the routerlist
  546. * too. */
  547. if (router->is_trusted_dir)
  548. r->is_trusted_dir = 1;
  549. /* Update the is_running status to whatever we were told. */
  550. r->is_running = router->is_running;
  551. routerinfo_free(router);
  552. return -1;
  553. }
  554. } else if (!strcasecmp(router->nickname, r->nickname)) {
  555. /* nicknames match, keys don't. */
  556. if (router->is_verified) {
  557. /* The new verified router replaces the old one; remove the
  558. * old one. And carry on to the end of the list, in case
  559. * there are more old unverifed routers with this nickname
  560. */
  561. /* mark-for-close connections using the old key, so we can
  562. * make new ones with the new key.
  563. */
  564. connection_t *conn;
  565. while((conn = connection_get_by_identity_digest(r->identity_digest,
  566. CONN_TYPE_OR))) {
  567. log_fn(LOG_INFO,"Closing conn to obsolete router '%s'", r->nickname);
  568. connection_mark_for_close(conn);
  569. }
  570. routerinfo_free(r);
  571. smartlist_del_keeporder(routerlist->routers, i--);
  572. } else if (r->is_verified) {
  573. /* Can't replace a verified router with an unverified one. */
  574. log_fn(LOG_DEBUG, "Skipping unverified entry for verified router '%s'",
  575. router->nickname);
  576. routerinfo_free(router);
  577. return -1;
  578. }
  579. }
  580. }
  581. /* We haven't seen a router with this name before. Add it to the end of
  582. * the list. */
  583. smartlist_add(routerlist->routers, router);
  584. return 0;
  585. }
  586. /** Remove any routers from the routerlist that are more than <b>age</b>
  587. * seconds old.
  588. *
  589. * (This function is just like dirserv_remove_old_servers. One day we should
  590. * merge them.)
  591. */
  592. void
  593. routerlist_remove_old_routers(int age)
  594. {
  595. int i;
  596. time_t cutoff;
  597. routerinfo_t *router;
  598. if (!routerlist)
  599. return;
  600. cutoff = time(NULL) - age;
  601. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  602. router = smartlist_get(routerlist->routers, i);
  603. if (router->published_on <= cutoff &&
  604. !router->is_trusted_dir) {
  605. /* Too old. Remove it. But never remove dirservers! */
  606. log_fn(LOG_INFO,"Forgetting obsolete routerinfo for node %s.", router->nickname);
  607. routerinfo_free(router);
  608. smartlist_del(routerlist->routers, i--);
  609. }
  610. }
  611. }
  612. /*
  613. * Code to parse router descriptors and directories.
  614. */
  615. /** Update the current router list with the one stored in
  616. * <b>routerfile</b>. If <b>trusted</b> is true, then we'll use
  617. * directory servers from the file. */
  618. int router_load_routerlist_from_file(char *routerfile, int trusted)
  619. {
  620. char *string;
  621. string = read_file_to_str(routerfile,0);
  622. if(!string) {
  623. log_fn(LOG_WARN,"Failed to load routerfile %s.",routerfile);
  624. return -1;
  625. }
  626. if(router_load_routerlist_from_string(string, trusted) < 0) {
  627. log_fn(LOG_WARN,"The routerfile itself was corrupt.");
  628. free(string);
  629. return -1;
  630. }
  631. /* dump_onion_keys(LOG_NOTICE); */
  632. free(string);
  633. return 0;
  634. }
  635. /** Mark all directories in the routerlist as nontrusted. */
  636. void routerlist_clear_trusted_directories(void)
  637. {
  638. if (routerlist) {
  639. SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, r,
  640. r->is_trusted_dir = 0);
  641. }
  642. if (trusted_dir_digests) {
  643. SMARTLIST_FOREACH(trusted_dir_digests, char *, cp, tor_free(cp));
  644. smartlist_clear(trusted_dir_digests);
  645. }
  646. }
  647. /** Helper function: read routerinfo elements from s, and throw out the
  648. * ones that don't parse and resolve. Add all remaining elements to the
  649. * routerlist. If <b>trusted</b> is true, then we'll use
  650. * directory servers from the string
  651. */
  652. int router_load_routerlist_from_string(const char *s, int trusted)
  653. {
  654. routerlist_t *new_list=NULL;
  655. if (router_parse_list_from_string(&s, &new_list, NULL, 0)) {
  656. log(LOG_WARN, "Error parsing router file");
  657. return -1;
  658. }
  659. if (trusted) {
  660. int i;
  661. if (!trusted_dir_digests)
  662. trusted_dir_digests = smartlist_create();
  663. for (i=0;i<smartlist_len(new_list->routers);++i) {
  664. routerinfo_t *r = smartlist_get(new_list->routers, i);
  665. if (r->dir_port) {
  666. char *b;
  667. log_fn(LOG_DEBUG,"Trusting router %s.", r->nickname);
  668. r->is_trusted_dir = 1;
  669. b = tor_malloc(DIGEST_LEN);
  670. memcpy(b, r->identity_digest, DIGEST_LEN);
  671. smartlist_add(trusted_dir_digests, b);
  672. }
  673. }
  674. }
  675. if (routerlist) {
  676. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  677. router_add_to_routerlist(r));
  678. smartlist_clear(new_list->routers);
  679. routerlist_free(new_list);
  680. } else {
  681. routerlist = new_list;
  682. }
  683. if (router_resolve_routerlist(routerlist)) {
  684. log(LOG_WARN, "Error resolving routerlist");
  685. return -1;
  686. }
  687. /* dump_onion_keys(LOG_NOTICE); */
  688. return 0;
  689. }
  690. /** Add to the current routerlist each router stored in the
  691. * signed directory <b>s</b>. If pkey is provided, check the signature against
  692. * pkey; else check against the pkey of the signing directory server. */
  693. int router_load_routerlist_from_directory(const char *s,
  694. crypto_pk_env_t *pkey)
  695. {
  696. routerlist_t *new_list = NULL;
  697. if (router_parse_routerlist_from_directory(s, &new_list, pkey)) {
  698. log_fn(LOG_WARN, "Couldn't parse directory.");
  699. return -1;
  700. }
  701. if (routerlist) {
  702. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  703. router_add_to_routerlist(r));
  704. smartlist_clear(new_list->routers);
  705. routerlist->published_on = new_list->published_on;
  706. tor_free(routerlist->software_versions);
  707. routerlist->software_versions = new_list->software_versions;
  708. new_list->software_versions = NULL;
  709. routerlist_free(new_list);
  710. } else {
  711. routerlist = new_list;
  712. }
  713. if (router_resolve_routerlist(routerlist)) {
  714. log_fn(LOG_WARN, "Error resolving routerlist");
  715. return -1;
  716. }
  717. if (options.AuthoritativeDir) {
  718. /* Learn about the descriptors in the directory. */
  719. dirserv_load_from_directory_string(s);
  720. } else {
  721. /* Remember the directory. */
  722. dirserv_set_cached_directory(s, routerlist->published_on);
  723. }
  724. return 0;
  725. }
  726. /** Helper function: resolve the hostname for <b>router</b>. */
  727. static int
  728. router_resolve(routerinfo_t *router)
  729. {
  730. if (tor_lookup_hostname(router->address, &router->addr) != 0
  731. || !router->addr) {
  732. log_fn(LOG_WARN,"Could not get address for router %s (%s).",
  733. router->address, router->nickname);
  734. return -1;
  735. }
  736. router->addr = ntohl(router->addr); /* get it back into host order */
  737. return 0;
  738. }
  739. /** Helper function: resolve every router in rl, and ensure that our own
  740. * routerinfo is at the front.
  741. */
  742. static int
  743. router_resolve_routerlist(routerlist_t *rl)
  744. {
  745. int i, remove;
  746. routerinfo_t *r;
  747. if (!rl)
  748. rl = routerlist;
  749. i = 0;
  750. if ((r = router_get_my_routerinfo())) {
  751. smartlist_insert(rl->routers, 0, routerinfo_copy(r));
  752. ++i;
  753. }
  754. for ( ; i < smartlist_len(rl->routers); ++i) {
  755. remove = 0;
  756. r = smartlist_get(rl->routers,i);
  757. if (router_is_me(r)) {
  758. remove = 1;
  759. } else if (r->addr) {
  760. /* already resolved. */
  761. } else if (router_resolve(r)) {
  762. log_fn(LOG_WARN, "Couldn't resolve router %s; not using", r->address);
  763. remove = 1;
  764. }
  765. if (remove) {
  766. routerinfo_free(r);
  767. smartlist_del_keeporder(rl->routers, i--);
  768. }
  769. }
  770. return 0;
  771. }
  772. /** Decide whether a given addr:port is definitely accepted, definitely
  773. * rejected, or neither by a given exit policy. If <b>addr</b> is 0, we
  774. * don't know the IP of the target address.
  775. *
  776. * Returns -1 for "rejected", 0 for "accepted", 1 for "maybe" (since IP is
  777. * unknown).
  778. */
  779. int router_compare_addr_to_exit_policy(uint32_t addr, uint16_t port,
  780. struct exit_policy_t *policy)
  781. {
  782. int maybe_reject = 0;
  783. int maybe_accept = 0;
  784. int match = 0;
  785. int maybe = 0;
  786. struct in_addr in;
  787. struct exit_policy_t *tmpe;
  788. for(tmpe=policy; tmpe; tmpe=tmpe->next) {
  789. // log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
  790. maybe = 0;
  791. if (!addr) {
  792. /* Address is unknown. */
  793. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  794. /* The port definitely matches. */
  795. if (tmpe->msk == 0) {
  796. match = 1;
  797. } else {
  798. maybe = 1;
  799. }
  800. } else if (!port) {
  801. /* The port maybe matches. */
  802. maybe = 1;
  803. }
  804. } else {
  805. /* Address is known */
  806. if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
  807. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  808. /* Exact match for the policy */
  809. match = 1;
  810. } else if (!port) {
  811. maybe = 1;
  812. }
  813. }
  814. }
  815. if (maybe) {
  816. if (tmpe->policy_type == EXIT_POLICY_REJECT)
  817. maybe_reject = 1;
  818. else
  819. maybe_accept = 1;
  820. }
  821. if (match) {
  822. in.s_addr = htonl(addr);
  823. log_fn(LOG_DEBUG,"Address %s:%d matches exit policy '%s'",
  824. inet_ntoa(in), port, tmpe->string);
  825. if(tmpe->policy_type == EXIT_POLICY_ACCEPT) {
  826. /* If we already hit a clause that might trigger a 'reject', than we
  827. * can't be sure of this certain 'accept'.*/
  828. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  829. } else {
  830. return maybe_accept ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_REJECTED;
  831. }
  832. }
  833. }
  834. /* accept all by default. */
  835. return maybe_reject ? ADDR_POLICY_UNKNOWN : ADDR_POLICY_ACCEPTED;
  836. }
  837. /** Return 1 if all running routers will reject addr:port, return 0 if
  838. * any might accept it. */
  839. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port) {
  840. int i;
  841. routerinfo_t *router;
  842. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  843. router = smartlist_get(routerlist->routers, i);
  844. if (router->is_running && router_compare_addr_to_exit_policy(
  845. addr, port, router->exit_policy) != ADDR_POLICY_REJECTED)
  846. return 0; /* this one could be ok. good enough. */
  847. }
  848. return 1; /* all will reject. */
  849. }
  850. /** Return true iff <b>router</b> does not permit exit streams.
  851. */
  852. int router_exit_policy_rejects_all(routerinfo_t *router) {
  853. return router_compare_addr_to_exit_policy(0, 0, router->exit_policy)
  854. == ADDR_POLICY_REJECTED;
  855. }
  856. /** Release all space held in <b>rr</b>. */
  857. void running_routers_free(running_routers_t *rr)
  858. {
  859. tor_assert(rr);
  860. if (rr->running_routers) {
  861. SMARTLIST_FOREACH(rr->running_routers, char *, s, tor_free(s));
  862. smartlist_free(rr->running_routers);
  863. }
  864. tor_free(rr);
  865. }
  866. /** Update the running/not-running status of every router in <b>list</b>, based
  867. * on the contents of <b>rr</b>. */
  868. /* Note: this function is not yet used, since nobody publishes just
  869. * running-router lists yet. */
  870. void routerlist_update_from_runningrouters(routerlist_t *list,
  871. running_routers_t *rr)
  872. {
  873. int n_routers, i;
  874. routerinfo_t *router, *me = router_get_my_routerinfo();
  875. if (!list)
  876. return;
  877. if (list->published_on >= rr->published_on)
  878. return;
  879. if (list->running_routers_updated_on >= rr->published_on)
  880. return;
  881. if(me) { /* learn if the dirservers think I'm verified */
  882. router_update_status_from_smartlist(me,
  883. rr->published_on,
  884. rr->running_routers);
  885. }
  886. n_routers = smartlist_len(list->routers);
  887. for (i=0; i<n_routers; ++i) {
  888. router = smartlist_get(list->routers, i);
  889. router_update_status_from_smartlist(router,
  890. rr->published_on,
  891. rr->running_routers);
  892. }
  893. list->running_routers_updated_on = rr->published_on;
  894. }
  895. /** Update the is_running and is_verified fields of the router <b>router</b>,
  896. * based in its status in the list of strings stored in <b>running_list</b>.
  897. * All entries in <b>running_list</b> follow one of these formats:
  898. * <ol><li> <b>nickname</b> -- router is running and verified.
  899. * <li> !<b>nickname</b> -- router is not-running and verified.
  900. * <li> $<b>hexdigest</b> -- router is running and unverified.
  901. * <li> !$<b>hexdigest</b> -- router is not-running and unverified.
  902. * </ol>
  903. *
  904. * Return 1 if we found router in running_list, else return 0.
  905. */
  906. int router_update_status_from_smartlist(routerinfo_t *router,
  907. time_t list_time,
  908. smartlist_t *running_list)
  909. {
  910. int n_names, i, running, approved;
  911. const char *name;
  912. #if 1
  913. char *cp;
  914. int n;
  915. n = 0;
  916. for (i=0; i<smartlist_len(running_list); ++i) {
  917. name = smartlist_get(running_list, i);
  918. n += strlen(name) + 1;
  919. }
  920. cp = tor_malloc(n+2);
  921. cp[0] = '\0';
  922. for (i=0; i<smartlist_len(running_list); ++i) {
  923. name = smartlist_get(running_list, i);
  924. strlcat(cp, name, n);
  925. strlcat(cp, " ", n);
  926. }
  927. log_fn(LOG_DEBUG, "Updating status of %s from list \"%s\"",
  928. router->nickname, cp);
  929. tor_free(cp);
  930. #endif
  931. running = approved = 0;
  932. n_names = smartlist_len(running_list);
  933. for (i=0; i<n_names; ++i) {
  934. name = smartlist_get(running_list, i);
  935. if (*name != '!') {
  936. if (router_nickname_matches(router, name)) {
  937. if (router->status_set_at < list_time) {
  938. router->status_set_at = list_time;
  939. router->is_running = 1;
  940. }
  941. router->is_verified = (name[0] != '$');
  942. return 1;
  943. }
  944. } else { /* *name == '!' */
  945. name++;
  946. if (router_nickname_matches(router, name)) {
  947. if (router->status_set_at < list_time) {
  948. router->status_set_at = list_time;
  949. router->is_running = 0;
  950. }
  951. router->is_verified = (name[0] != '$');
  952. return 1;
  953. }
  954. }
  955. }
  956. return 0;
  957. }
  958. /*
  959. Local Variables:
  960. mode:c
  961. indent-tabs-mode:nil
  962. c-basic-offset:2
  963. End:
  964. */