routerlist.c 33 KB

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