routerlist.c 36 KB

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