routerlist.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /* Copyright 2001 Matej Pfajfar.
  2. * Copyright 2001-2004 Roger Dingledine.
  3. * Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char routerlist_c_id[] = "$Id$";
  7. #include "or.h"
  8. /**
  9. * \file routerlist.c
  10. *
  11. * \brief Code to
  12. * maintain and access the global list of routerinfos for known
  13. * servers.
  14. **/
  15. /****************************************************************************/
  16. static smartlist_t *trusted_dir_servers = NULL;
  17. /* static function prototypes */
  18. static routerinfo_t *
  19. router_pick_directory_server_impl(int requireothers, int fascistfirewall,
  20. int for_runningrouters);
  21. static trusted_dir_server_t *
  22. router_pick_trusteddirserver_impl(int requireother, int fascistfirewall);
  23. static void mark_all_trusteddirservers_up(void);
  24. static int router_resolve_routerlist(routerlist_t *dir);
  25. /****************************************************************************/
  26. /****
  27. * Functions to manage and access our list of known routers. (Note:
  28. * dirservers maintain a separate, independent list of known router
  29. * descriptors.)
  30. *****/
  31. /** Global list of all of the routers that we, as an OR or OP, know about. */
  32. static routerlist_t *routerlist = NULL;
  33. extern int has_fetched_directory; /**< from main.c */
  34. /**
  35. * Reload the original list of trusted dirservers, and the most recent
  36. * cached directory (if present).
  37. */
  38. int router_reload_router_list(void)
  39. {
  40. char filename[512];
  41. int is_recent;
  42. struct stat st;
  43. char *s;
  44. tor_assert(get_options()->DataDirectory);
  45. tor_snprintf(filename,sizeof(filename),"%s/cached-directory",
  46. get_options()->DataDirectory);
  47. s = read_file_to_str(filename,0);
  48. if (s) {
  49. stat(filename, &st); /* if s is true, stat probably worked */
  50. log_fn(LOG_INFO, "Loading cached directory from %s", filename);
  51. is_recent = st.st_mtime > time(NULL) - 60*15;
  52. if (router_load_routerlist_from_directory(s, NULL, is_recent, 1) < 0) {
  53. log_fn(LOG_WARN, "Cached directory at '%s' was unparseable; ignoring.", filename);
  54. }
  55. if (routerlist &&
  56. ((routerlist->published_on > time(NULL) - MIN_ONION_KEY_LIFETIME/2)
  57. || is_recent)) {
  58. directory_has_arrived(st.st_mtime, NULL); /* do things we've been waiting to do */
  59. }
  60. tor_free(s);
  61. }
  62. return 0;
  63. }
  64. /* Set *<b>outp</b> to a smartlist containing a list of
  65. * trusted_dir_server_t * for all known trusted dirservers. Callers
  66. * must not modify the list or its contents.
  67. */
  68. void router_get_trusted_dir_servers(smartlist_t **outp)
  69. {
  70. if (!trusted_dir_servers)
  71. trusted_dir_servers = smartlist_create();
  72. *outp = trusted_dir_servers;
  73. }
  74. /** Try to find a running dirserver. If there are no running dirservers
  75. * in our routerlist, set all the authoritative ones as running again,
  76. * and pick one. If there are no dirservers at all in our routerlist,
  77. * reload the routerlist and try one last time. If for_runningrouters is
  78. * true, then only pick a dirserver that can answer runningrouters queries
  79. * (that is, a trusted dirserver, or one running 0.0.9rc5-cvs or later).
  80. */
  81. routerinfo_t *router_pick_directory_server(int requireothers,
  82. int fascistfirewall,
  83. int for_runningrouters,
  84. int retry_if_no_servers) {
  85. routerinfo_t *choice;
  86. if (!routerlist)
  87. return NULL;
  88. choice = router_pick_directory_server_impl(requireothers, fascistfirewall,
  89. for_runningrouters);
  90. if (choice || !retry_if_no_servers)
  91. return choice;
  92. log_fn(LOG_INFO,"No reachable router entries for dirservers. Trying them all again.");
  93. /* mark all authdirservers as up again */
  94. mark_all_trusteddirservers_up();
  95. /* try again */
  96. choice = router_pick_directory_server_impl(requireothers, fascistfirewall,
  97. for_runningrouters);
  98. if (choice)
  99. return choice;
  100. log_fn(LOG_INFO,"Still no %s router entries. Reloading and trying again.",
  101. get_options()->FascistFirewall ? "reachable" : "known");
  102. has_fetched_directory=0; /* reset it */
  103. if (router_reload_router_list()) {
  104. return NULL;
  105. }
  106. /* give it one last try */
  107. choice = router_pick_directory_server_impl(requireothers, 0,
  108. for_runningrouters);
  109. return choice;
  110. }
  111. trusted_dir_server_t *router_pick_trusteddirserver(int requireothers,
  112. int fascistfirewall,
  113. int retry_if_no_servers) {
  114. trusted_dir_server_t *choice;
  115. choice = router_pick_trusteddirserver_impl(requireothers, fascistfirewall);
  116. if (choice || !retry_if_no_servers)
  117. return choice;
  118. log_fn(LOG_INFO,"No trusted dirservers are reachable. Trying them all again.");
  119. /* mark all authdirservers as up again */
  120. mark_all_trusteddirservers_up();
  121. /* try again */
  122. choice = router_pick_trusteddirserver_impl(requireothers, fascistfirewall);
  123. if (choice)
  124. return choice;
  125. log_fn(LOG_WARN,"Still no dirservers %s. Reloading and trying again.",
  126. get_options()->FascistFirewall ? "reachable" : "known");
  127. has_fetched_directory=0; /* reset it */
  128. if (router_reload_router_list()) {
  129. return NULL;
  130. }
  131. /* give it one last try */
  132. choice = router_pick_trusteddirserver_impl(requireothers, 0);
  133. return choice;
  134. }
  135. /** Pick a random running router from our routerlist. If requireauth,
  136. * it has to be a trusted server. If requireothers, it cannot be us.
  137. */
  138. static routerinfo_t *
  139. router_pick_directory_server_impl(int requireothers, int fascistfirewall,
  140. int for_runningrouters)
  141. {
  142. int i;
  143. routerinfo_t *router;
  144. smartlist_t *sl;
  145. if (!routerlist)
  146. return NULL;
  147. if (get_options()->HttpProxy)
  148. fascistfirewall = 0;
  149. /* Find all the running dirservers we know about. */
  150. sl = smartlist_create();
  151. for (i=0;i < smartlist_len(routerlist->routers); i++) {
  152. router = smartlist_get(routerlist->routers, i);
  153. if (!router->is_running || !router->dir_port || !router->is_verified)
  154. continue;
  155. if (requireothers && router_is_me(router))
  156. continue;
  157. if (fascistfirewall) {
  158. if (!smartlist_string_num_isin(get_options()->FirewallPorts, router->dir_port))
  159. continue;
  160. }
  161. /* before 0.0.9rc5-cvs, only trusted dirservers served status info. */
  162. if (for_runningrouters &&
  163. !(tor_version_as_new_as(router->platform,"0.0.9rc5-cvs") ||
  164. router_digest_is_trusted_dir(router->identity_digest)))
  165. continue;
  166. smartlist_add(sl, router);
  167. }
  168. router = smartlist_choose(sl);
  169. smartlist_free(sl);
  170. return router;
  171. }
  172. static trusted_dir_server_t *
  173. router_pick_trusteddirserver_impl(int requireother, int fascistfirewall)
  174. {
  175. smartlist_t *sl;
  176. routerinfo_t *me;
  177. trusted_dir_server_t *ds;
  178. sl = smartlist_create();
  179. me = router_get_my_routerinfo();
  180. if (!trusted_dir_servers)
  181. return NULL;
  182. if (get_options()->HttpProxy)
  183. fascistfirewall = 0;
  184. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
  185. {
  186. if (!d->is_running) continue;
  187. if (requireother && me &&
  188. !memcmp(me->identity_digest, d->digest, DIGEST_LEN))
  189. continue;
  190. if (fascistfirewall) {
  191. if (!smartlist_string_num_isin(get_options()->FirewallPorts, d->dir_port))
  192. continue;
  193. }
  194. smartlist_add(sl, d);
  195. });
  196. ds = smartlist_choose(sl);
  197. smartlist_free(sl);
  198. return ds;
  199. }
  200. /** Go through and mark the auth dirservers as up */
  201. static void mark_all_trusteddirservers_up(void) {
  202. if (routerlist) {
  203. SMARTLIST_FOREACH(routerlist->routers, routerinfo_t *, router,
  204. if (router_digest_is_trusted_dir(router->identity_digest)) {
  205. tor_assert(router->dir_port > 0);
  206. router->is_running = 1;
  207. router->status_set_at = time(NULL);
  208. });
  209. }
  210. if (trusted_dir_servers) {
  211. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, dir,
  212. dir->is_running = 1);
  213. }
  214. }
  215. /** Return 0 if \exists an authoritative dirserver that's currently
  216. * thought to be running, else return 1.
  217. */
  218. int all_trusted_directory_servers_down(void) {
  219. if (!trusted_dir_servers)
  220. return 1;
  221. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, dir,
  222. if (dir->is_running) return 0);
  223. return 1;
  224. }
  225. /** Add all the family of <b>router</b> to the smartlist <b>sl</b>.
  226. */
  227. void routerlist_add_family(smartlist_t *sl, routerinfo_t *router) {
  228. routerinfo_t *r;
  229. struct config_line_t *cl;
  230. if (!router->declared_family)
  231. return;
  232. /* Add every r such that router declares familyness with r, and r
  233. * declares familyhood with router. */
  234. SMARTLIST_FOREACH(router->declared_family, const char *, n,
  235. {
  236. if (!(r = router_get_by_nickname(n)))
  237. continue;
  238. if (!r->declared_family)
  239. continue;
  240. SMARTLIST_FOREACH(r->declared_family, const char *, n2,
  241. {
  242. if (router_nickname_matches(router, n2))
  243. smartlist_add(sl, r);
  244. });
  245. });
  246. for (cl = get_options()->NodeFamilies; cl; cl = cl->next) {
  247. if (router_nickname_is_in_list(router, cl->value)) {
  248. add_nickname_list_to_smartlist(sl, cl->value, 0);
  249. }
  250. }
  251. }
  252. /** Given a comma-and-whitespace separated list of nicknames, see which
  253. * nicknames in <b>list</b> name routers in our routerlist that are
  254. * currently running. Add the routerinfos for those routers to <b>sl</b>.
  255. */
  256. void
  257. add_nickname_list_to_smartlist(smartlist_t *sl, const char *list, int warn_if_down)
  258. {
  259. routerinfo_t *router;
  260. smartlist_t *nickname_list;
  261. if (!list)
  262. return; /* nothing to do */
  263. tor_assert(sl);
  264. nickname_list = smartlist_create();
  265. smartlist_split_string(nickname_list, list, ",",
  266. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  267. SMARTLIST_FOREACH(nickname_list, const char *, nick, {
  268. if (!is_legal_nickname_or_hexdigest(nick)) {
  269. log_fn(LOG_WARN,"Nickname %s is misformed; skipping", nick);
  270. continue;
  271. }
  272. router = router_get_by_nickname(nick);
  273. if (router) {
  274. if (router->is_running)
  275. smartlist_add(sl,router);
  276. else
  277. log_fn(warn_if_down ? LOG_WARN : LOG_DEBUG,
  278. "Nickname list includes '%s' which is known but down.",nick);
  279. } else
  280. log_fn(has_fetched_directory ? LOG_WARN : LOG_INFO,
  281. "Nickname list includes '%s' which isn't a known router.",nick);
  282. });
  283. SMARTLIST_FOREACH(nickname_list, char *, nick, tor_free(nick));
  284. smartlist_free(nickname_list);
  285. }
  286. /** Return 1 iff any member of the comma-separated list <b>list</b> is an
  287. * acceptable nickname or hexdigest for <b>router</b>. Else return 0.
  288. */
  289. int
  290. router_nickname_is_in_list(routerinfo_t *router, const char *list)
  291. {
  292. smartlist_t *nickname_list;
  293. int v = 0;
  294. if (!list)
  295. return 0; /* definitely not */
  296. tor_assert(router);
  297. nickname_list = smartlist_create();
  298. smartlist_split_string(nickname_list, list, ",",
  299. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  300. SMARTLIST_FOREACH(nickname_list, const char *, cp,
  301. if (router_nickname_matches(router, cp)) {v=1;break;});
  302. SMARTLIST_FOREACH(nickname_list, char *, cp, tor_free(cp));
  303. smartlist_free(nickname_list);
  304. return v;
  305. }
  306. /** Add every router from our routerlist that is currently running to
  307. * <b>sl</b>.
  308. */
  309. static void
  310. router_add_running_routers_to_smartlist(smartlist_t *sl, int allow_unverified,
  311. int need_uptime, int need_capacity)
  312. {
  313. routerinfo_t *router;
  314. int i;
  315. if (!routerlist)
  316. return;
  317. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  318. router = smartlist_get(routerlist->routers, i);
  319. if (router->is_running &&
  320. (router->is_verified ||
  321. (allow_unverified &&
  322. !router_is_unreliable(router, need_uptime, need_capacity)))) {
  323. /* If it's running, and either it's verified or we're ok picking
  324. * unverified routers and this one is suitable.
  325. */
  326. smartlist_add(sl, router);
  327. }
  328. }
  329. }
  330. routerinfo_t *
  331. routerlist_find_my_routerinfo(void) {
  332. routerinfo_t *router;
  333. int i;
  334. if (!routerlist)
  335. return NULL;
  336. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  337. router = smartlist_get(routerlist->routers, i);
  338. if (router_is_me(router))
  339. return router;
  340. }
  341. return NULL;
  342. }
  343. int
  344. router_is_unreliable(routerinfo_t *router, int need_uptime, int need_capacity)
  345. {
  346. if (need_uptime && router->uptime < ROUTER_REQUIRED_MIN_UPTIME)
  347. return 1;
  348. if (need_capacity && router->bandwidthcapacity < ROUTER_REQUIRED_MIN_BANDWIDTH)
  349. return 1;
  350. return 0;
  351. }
  352. static void
  353. routerlist_sl_remove_unreliable_routers(smartlist_t *sl)
  354. {
  355. int i;
  356. routerinfo_t *router;
  357. for (i = 0; i < smartlist_len(sl); ++i) {
  358. router = smartlist_get(sl, i);
  359. if (router_is_unreliable(router, 1, 0)) {
  360. log(LOG_DEBUG, "Router '%s' has insufficient uptime; deleting.",
  361. router->nickname);
  362. smartlist_del(sl, i--);
  363. }
  364. }
  365. }
  366. routerinfo_t *
  367. routerlist_sl_choose_by_bandwidth(smartlist_t *sl)
  368. {
  369. int i;
  370. routerinfo_t *router;
  371. smartlist_t *bandwidths;
  372. uint32_t this_bw, tmp, total_bw=0, rand_bw;
  373. uint32_t *p;
  374. bandwidths = smartlist_create();
  375. for (i = 0; i < smartlist_len(sl); ++i) {
  376. router = smartlist_get(sl, i);
  377. this_bw = (router->bandwidthcapacity < router->bandwidthrate) ?
  378. router->bandwidthcapacity : router->bandwidthrate;
  379. if (this_bw > 1000000)
  380. this_bw = 1000000; /* if they claim something huge, don't believe it */
  381. p = tor_malloc(sizeof(uint32_t));
  382. *p = this_bw;
  383. smartlist_add(bandwidths, p);
  384. total_bw += this_bw;
  385. // log_fn(LOG_INFO,"Recording bw %d for node %s.", this_bw, router->nickname);
  386. }
  387. if (!total_bw) {
  388. SMARTLIST_FOREACH(bandwidths, uint32_t*, p, tor_free(p));
  389. smartlist_free(bandwidths);
  390. return smartlist_choose(sl);
  391. }
  392. rand_bw = crypto_pseudo_rand_int(total_bw);
  393. // log_fn(LOG_INFO,"Total bw %d. Randomly chose %d.", total_bw, rand_bw);
  394. tmp = 0;
  395. for (i=0; ; i++) {
  396. tor_assert(i < smartlist_len(sl));
  397. p = smartlist_get(bandwidths, i);
  398. tmp += *p;
  399. router = smartlist_get(sl, i);
  400. // log_fn(LOG_INFO,"Considering %s. tmp = %d.", router->nickname, tmp);
  401. if (tmp >= rand_bw)
  402. break;
  403. }
  404. SMARTLIST_FOREACH(bandwidths, uint32_t*, p, tor_free(p));
  405. smartlist_free(bandwidths);
  406. router = smartlist_get(sl, i);
  407. // log_fn(LOG_INFO,"Picked %s.", router->nickname);
  408. return router;
  409. }
  410. /** Return a random running router from the routerlist. If any node
  411. * named in <b>preferred</b> is available, pick one of those. Never
  412. * pick a node named in <b>excluded</b>, or whose routerinfo is in
  413. * <b>excludedsmartlist</b>, even if they are the only nodes
  414. * available. If <b>strict</b> is true, never pick any node besides
  415. * those in <b>preferred</b>.
  416. */
  417. routerinfo_t *router_choose_random_node(const char *preferred,
  418. const char *excluded,
  419. smartlist_t *excludedsmartlist,
  420. int need_uptime, int need_capacity,
  421. int allow_unverified, int strict)
  422. {
  423. smartlist_t *sl, *excludednodes;
  424. routerinfo_t *choice;
  425. excludednodes = smartlist_create();
  426. add_nickname_list_to_smartlist(excludednodes,excluded,0);
  427. /* try the preferred nodes first */
  428. sl = smartlist_create();
  429. add_nickname_list_to_smartlist(sl,preferred,1);
  430. smartlist_subtract(sl,excludednodes);
  431. if (excludedsmartlist)
  432. smartlist_subtract(sl,excludedsmartlist);
  433. if (need_uptime)
  434. routerlist_sl_remove_unreliable_routers(sl);
  435. if (need_capacity)
  436. choice = routerlist_sl_choose_by_bandwidth(sl);
  437. else
  438. choice = smartlist_choose(sl);
  439. smartlist_free(sl);
  440. if (!choice && !strict) {
  441. sl = smartlist_create();
  442. router_add_running_routers_to_smartlist(sl, allow_unverified,
  443. need_uptime, need_capacity);
  444. smartlist_subtract(sl,excludednodes);
  445. if (excludedsmartlist)
  446. smartlist_subtract(sl,excludedsmartlist);
  447. if (need_uptime)
  448. routerlist_sl_remove_unreliable_routers(sl);
  449. if (need_capacity)
  450. choice = routerlist_sl_choose_by_bandwidth(sl);
  451. else
  452. choice = smartlist_choose(sl);
  453. smartlist_free(sl);
  454. }
  455. smartlist_free(excludednodes);
  456. if (!choice)
  457. log_fn(LOG_WARN,"No available nodes when trying to choose node. Failing.");
  458. return choice;
  459. }
  460. /** Return the router in our routerlist whose address is <b>addr</b> and
  461. * whose OR port is <b>port</b>. Return NULL if no such router is known.
  462. */
  463. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port) {
  464. int i;
  465. routerinfo_t *router;
  466. if (!routerlist)
  467. return NULL;
  468. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  469. router = smartlist_get(routerlist->routers, i);
  470. if ((router->addr == addr) && (router->or_port == port))
  471. return router;
  472. }
  473. return NULL;
  474. }
  475. /** Return true iff the digest of <b>router</b>'s identity key,
  476. * encoded in hexadecimal, matches <b>hexdigest</b> (which is
  477. * optionally prefixed with a single dollar sign). Return false if
  478. * <b>hexdigest</b> is malformed, or it doesn't match. */
  479. static INLINE int router_hex_digest_matches(routerinfo_t *router,
  480. const char *hexdigest)
  481. {
  482. char digest[DIGEST_LEN];
  483. tor_assert(hexdigest);
  484. if (hexdigest[0] == '$')
  485. ++hexdigest;
  486. if (strlen(hexdigest) != HEX_DIGEST_LEN ||
  487. base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0)
  488. return 0;
  489. else
  490. return (!memcmp(digest, router->identity_digest, DIGEST_LEN));
  491. }
  492. /* Return true if <b>router</b>'s nickname matches <b>nickname</b>
  493. * (case-insensitive), or if <b>router's</b> identity key digest
  494. * matches a hexadecimal value stored in <b>nickname</b>. Return
  495. * false otherwise.*/
  496. int router_nickname_matches(routerinfo_t *router, const char *nickname)
  497. {
  498. if (nickname[0]!='$' && !strcasecmp(router->nickname, nickname))
  499. return 1;
  500. else
  501. return router_hex_digest_matches(router, nickname);
  502. }
  503. /** Return the router in our routerlist whose (case-insensitive)
  504. * nickname or (case-sensitive) hexadecimal key digest is
  505. * <b>nickname</b>. Return NULL if no such router is known.
  506. */
  507. routerinfo_t *router_get_by_nickname(const char *nickname)
  508. {
  509. int i, maybedigest;
  510. routerinfo_t *router;
  511. char digest[DIGEST_LEN];
  512. tor_assert(nickname);
  513. if (!routerlist)
  514. return NULL;
  515. if (nickname[0] == '$')
  516. return router_get_by_hexdigest(nickname);
  517. if (server_mode(get_options()) &&
  518. !strcasecmp(nickname, get_options()->Nickname))
  519. return router_get_my_routerinfo();
  520. maybedigest = (strlen(nickname) == HEX_DIGEST_LEN) &&
  521. (base16_decode(digest,DIGEST_LEN,nickname,HEX_DIGEST_LEN) == 0);
  522. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  523. router = smartlist_get(routerlist->routers, i);
  524. if (0 == strcasecmp(router->nickname, nickname) ||
  525. (maybedigest && 0 == memcmp(digest, router->identity_digest,
  526. DIGEST_LEN)))
  527. return router;
  528. }
  529. return NULL;
  530. }
  531. /** Return true iff <b>digest</b> is the digest of the identity key of
  532. * a trusted directory. */
  533. int router_digest_is_trusted_dir(const char *digest) {
  534. if (!trusted_dir_servers)
  535. return 0;
  536. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
  537. if (!memcmp(digest, ent->digest, DIGEST_LEN)) return 1);
  538. return 0;
  539. }
  540. /** Return the router in our routerlist whose hexadecimal key digest
  541. * is <b>hexdigest</b>. Return NULL if no such router is known. */
  542. routerinfo_t *router_get_by_hexdigest(const char *hexdigest) {
  543. char digest[DIGEST_LEN];
  544. tor_assert(hexdigest);
  545. if (!routerlist)
  546. return NULL;
  547. if (hexdigest[0]=='$')
  548. ++hexdigest;
  549. if (strlen(hexdigest) != HEX_DIGEST_LEN ||
  550. base16_decode(digest,DIGEST_LEN,hexdigest,HEX_DIGEST_LEN) < 0)
  551. return NULL;
  552. return router_get_by_digest(digest);
  553. }
  554. /** Return the router in our routerlist whose 20-byte key digest
  555. * is <b>digest</b>. Return NULL if no such router is known. */
  556. routerinfo_t *router_get_by_digest(const char *digest) {
  557. int i;
  558. routerinfo_t *router;
  559. tor_assert(digest);
  560. if (server_mode(get_options()) &&
  561. (router = router_get_my_routerinfo()) &&
  562. !memcmp(digest, router->identity_digest, DIGEST_LEN))
  563. return router;
  564. if (!routerlist) return NULL;
  565. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  566. router = smartlist_get(routerlist->routers, i);
  567. if (0 == memcmp(router->identity_digest, digest, DIGEST_LEN))
  568. return router;
  569. }
  570. return NULL;
  571. }
  572. /** Set *<b>prouterlist</b> to the current list of all known routers. */
  573. void router_get_routerlist(routerlist_t **prouterlist) {
  574. *prouterlist = routerlist;
  575. }
  576. /** Return the publication time on the current routerlist, or 0 if we have no
  577. * routerlist. */
  578. time_t routerlist_get_published_time(void) {
  579. return routerlist ? routerlist->published_on : 0;
  580. }
  581. /** Free all storage held by <b>router</b>. */
  582. void routerinfo_free(routerinfo_t *router)
  583. {
  584. if (!router)
  585. return;
  586. tor_free(router->signed_descriptor);
  587. tor_free(router->address);
  588. tor_free(router->nickname);
  589. tor_free(router->platform);
  590. if (router->onion_pkey)
  591. crypto_free_pk_env(router->onion_pkey);
  592. if (router->identity_pkey)
  593. crypto_free_pk_env(router->identity_pkey);
  594. if (router->declared_family) {
  595. SMARTLIST_FOREACH(router->declared_family, char *, s, tor_free(s));
  596. smartlist_free(router->declared_family);
  597. }
  598. addr_policy_free(router->exit_policy);
  599. tor_free(router);
  600. }
  601. /** Allocate a fresh copy of <b>router</b> */
  602. routerinfo_t *routerinfo_copy(const routerinfo_t *router)
  603. {
  604. routerinfo_t *r;
  605. addr_policy_t **e, *tmp;
  606. r = tor_malloc(sizeof(routerinfo_t));
  607. memcpy(r, router, sizeof(routerinfo_t));
  608. r->address = tor_strdup(r->address);
  609. r->nickname = tor_strdup(r->nickname);
  610. r->platform = tor_strdup(r->platform);
  611. if (r->signed_descriptor)
  612. r->signed_descriptor = tor_strdup(r->signed_descriptor);
  613. if (r->onion_pkey)
  614. r->onion_pkey = crypto_pk_dup_key(r->onion_pkey);
  615. if (r->identity_pkey)
  616. r->identity_pkey = crypto_pk_dup_key(r->identity_pkey);
  617. e = &r->exit_policy;
  618. while (*e) {
  619. tmp = tor_malloc(sizeof(addr_policy_t));
  620. memcpy(tmp,*e,sizeof(addr_policy_t));
  621. *e = tmp;
  622. (*e)->string = tor_strdup((*e)->string);
  623. e = & ((*e)->next);
  624. }
  625. if (r->declared_family) {
  626. r->declared_family = smartlist_create();
  627. SMARTLIST_FOREACH(router->declared_family, const char *, s,
  628. smartlist_add(r->declared_family, tor_strdup(s)));
  629. }
  630. return r;
  631. }
  632. /** Free all storage held by a routerlist <b>rl</b> */
  633. void routerlist_free(routerlist_t *rl)
  634. {
  635. tor_assert(rl);
  636. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, r,
  637. routerinfo_free(r));
  638. smartlist_free(rl->routers);
  639. running_routers_free(rl->running_routers);
  640. tor_free(rl->software_versions);
  641. tor_free(rl);
  642. }
  643. void routerlist_free_current(void)
  644. {
  645. if (routerlist)
  646. routerlist_free(routerlist);
  647. routerlist = NULL;
  648. }
  649. void free_trusted_dir_servers(void)
  650. {
  651. if (trusted_dir_servers) {
  652. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ds,
  653. { tor_free(ds->address); tor_free(ds); });
  654. smartlist_free(trusted_dir_servers);
  655. trusted_dir_servers = NULL;
  656. }
  657. }
  658. /** Mark the router with ID <b>digest</b> as non-running in our routerlist. */
  659. void router_mark_as_down(const char *digest) {
  660. routerinfo_t *router;
  661. tor_assert(digest);
  662. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, d,
  663. if (!memcmp(d->digest, digest, DIGEST_LEN))
  664. d->is_running = 0);
  665. router = router_get_by_digest(digest);
  666. if (!router) /* we don't seem to know about him in the first place */
  667. return;
  668. log_fn(LOG_DEBUG,"Marking router '%s' as down.",router->nickname);
  669. if (router_is_me(router))
  670. log_fn(LOG_WARN, "We just marked ourself as down. Are your external addresses reachable?");
  671. router->is_running = 0;
  672. router->status_set_at = time(NULL);
  673. }
  674. /** Add <b>router</b> to the routerlist, if we don't already have it. Replace
  675. * older entries (if any) with the same name. Note: Callers should not hold
  676. * their pointers to <b>router</b> after invoking this function; <b>router</b>
  677. * will either be inserted into the routerlist or freed. Returns 0 if the
  678. * router was added; -1 if it was not.
  679. */
  680. static int
  681. router_add_to_routerlist(routerinfo_t *router) {
  682. int i;
  683. routerinfo_t *r;
  684. char id_digest[DIGEST_LEN];
  685. tor_assert(routerlist);
  686. crypto_pk_get_digest(router->identity_pkey, id_digest);
  687. /* If we have a router with this name, and the identity key is the same,
  688. * choose the newer one. If the identity key has changed, drop the router.
  689. */
  690. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  691. r = smartlist_get(routerlist->routers, i);
  692. if (!crypto_pk_cmp_keys(router->identity_pkey, r->identity_pkey)) {
  693. if (router->published_on > r->published_on) {
  694. log_fn(LOG_DEBUG, "Replacing entry for router '%s/%s' [%s]",
  695. router->nickname, r->nickname, hex_str(id_digest,DIGEST_LEN));
  696. /* Remember whether we trust this router as a dirserver. */
  697. /* If the address hasn't changed; no need to re-resolve. */
  698. if (!strcasecmp(r->address, router->address))
  699. router->addr = r->addr;
  700. routerinfo_free(r);
  701. smartlist_set(routerlist->routers, i, router);
  702. return 0;
  703. } else {
  704. log_fn(LOG_DEBUG, "Skipping old entry for router '%s'",
  705. router->nickname);
  706. /* Update the is_running status to whatever we were told. */
  707. r->is_running = router->is_running;
  708. routerinfo_free(router);
  709. return -1;
  710. }
  711. } else if (!strcasecmp(router->nickname, r->nickname)) {
  712. /* nicknames match, keys don't. */
  713. if (router->is_verified) {
  714. /* The new verified router replaces the old one; remove the
  715. * old one. And carry on to the end of the list, in case
  716. * there are more old unverified routers with this nickname
  717. */
  718. /* mark-for-close connections using the old key, so we can
  719. * make new ones with the new key.
  720. */
  721. connection_t *conn;
  722. while ((conn = connection_get_by_identity_digest(r->identity_digest,
  723. CONN_TYPE_OR))) {
  724. log_fn(LOG_INFO,"Closing conn to obsolete router '%s'", r->nickname);
  725. connection_mark_for_close(conn);
  726. }
  727. routerinfo_free(r);
  728. smartlist_del_keeporder(routerlist->routers, i--);
  729. } else if (r->is_verified) {
  730. /* Can't replace a verified router with an unverified one. */
  731. log_fn(LOG_DEBUG, "Skipping unverified entry for verified router '%s'",
  732. router->nickname);
  733. routerinfo_free(router);
  734. return -1;
  735. }
  736. }
  737. }
  738. /* We haven't seen a router with this name before. Add it to the end of
  739. * the list. */
  740. smartlist_add(routerlist->routers, router);
  741. return 0;
  742. }
  743. /** Remove any routers from the routerlist that are more than <b>age</b>
  744. * seconds old.
  745. *
  746. * (This function is just like dirserv_remove_old_servers. One day we should
  747. * merge them.)
  748. */
  749. void
  750. routerlist_remove_old_routers(int age)
  751. {
  752. int i;
  753. time_t cutoff;
  754. routerinfo_t *router;
  755. if (!routerlist)
  756. return;
  757. cutoff = time(NULL) - age;
  758. for (i = 0; i < smartlist_len(routerlist->routers); ++i) {
  759. router = smartlist_get(routerlist->routers, i);
  760. if (router->published_on <= cutoff) {
  761. /* Too old. Remove it. */
  762. log_fn(LOG_INFO,"Forgetting obsolete routerinfo for router '%s'", router->nickname);
  763. routerinfo_free(router);
  764. smartlist_del(routerlist->routers, i--);
  765. }
  766. }
  767. }
  768. /*
  769. * Code to parse router descriptors and directories.
  770. */
  771. int
  772. router_load_single_router(const char *s)
  773. {
  774. routerinfo_t *ri;
  775. if (!(ri = router_parse_entry_from_string(s, NULL))) {
  776. log_fn(LOG_WARN, "Error parsing router descriptor; dropping.");
  777. return -1;
  778. }
  779. if (routerlist && routerlist->running_routers) {
  780. running_routers_t *rr = routerlist->running_routers;
  781. router_update_status_from_smartlist(ri,
  782. rr->published_on,
  783. rr->running_routers,
  784. rr->is_running_routers_format);
  785. }
  786. if (router_add_to_routerlist(ri)<0) {
  787. log_fn(LOG_WARN, "Couldn't add router to list; dropping.");
  788. return -1;
  789. } else {
  790. smartlist_t *changed = smartlist_create();
  791. smartlist_add(changed, ri);
  792. control_event_descriptors_changed(changed);
  793. smartlist_free(changed);
  794. }
  795. log_fn(LOG_DEBUG, "Added router to list");
  796. return 0;
  797. }
  798. /** Add to the current routerlist each router stored in the
  799. * signed directory <b>s</b>. If pkey is provided, check the signature against
  800. * pkey; else check against the pkey of the signing directory server.
  801. *
  802. * If <b>dir_is_recent</b> is non-zero, then examine the
  803. * Recommended-versions line and take appropriate action.
  804. *
  805. * If <b>dir_is_cached</b> is non-zero, then we're reading it
  806. * from the cache so don't bother to re-write it to the cache.
  807. */
  808. int router_load_routerlist_from_directory(const char *s,
  809. crypto_pk_env_t *pkey,
  810. int dir_is_recent,
  811. int dir_is_cached)
  812. {
  813. routerlist_t *new_list = NULL;
  814. if (router_parse_routerlist_from_directory(s, &new_list, pkey,
  815. dir_is_recent,
  816. !dir_is_cached)) {
  817. log_fn(LOG_WARN, "Couldn't parse directory.");
  818. return -1;
  819. }
  820. if (routerlist) {
  821. smartlist_t *changed = smartlist_create();
  822. SMARTLIST_FOREACH(new_list->routers, routerinfo_t *, r,
  823. {
  824. if (router_add_to_routerlist(r)==0)
  825. smartlist_add(changed, r);
  826. });
  827. smartlist_clear(new_list->routers);
  828. routerlist->published_on = new_list->published_on;
  829. tor_free(routerlist->software_versions);
  830. routerlist->software_versions = new_list->software_versions;
  831. new_list->software_versions = NULL;
  832. routerlist_free(new_list);
  833. control_event_descriptors_changed(changed);
  834. smartlist_free(changed);
  835. } else {
  836. routerlist = new_list;
  837. control_event_descriptors_changed(routerlist->routers);
  838. }
  839. if (router_resolve_routerlist(routerlist)) {
  840. log_fn(LOG_WARN, "Error resolving routerlist");
  841. return -1;
  842. }
  843. if (get_options()->AuthoritativeDir) {
  844. /* Learn about the descriptors in the directory. */
  845. dirserv_load_from_directory_string(s);
  846. }
  847. return 0;
  848. }
  849. /** Helper function: resolve the hostname for <b>router</b>. */
  850. static int
  851. router_resolve(routerinfo_t *router)
  852. {
  853. if (tor_lookup_hostname(router->address, &router->addr) != 0
  854. || !router->addr) {
  855. log_fn(LOG_WARN,"Could not resolve address for router '%s' at %s",
  856. router->nickname, router->address);
  857. return -1;
  858. }
  859. router->addr = ntohl(router->addr); /* get it back into host order */
  860. return 0;
  861. }
  862. /** Helper function: resolve every router in rl, and ensure that our own
  863. * routerinfo is at the front.
  864. */
  865. static int
  866. router_resolve_routerlist(routerlist_t *rl)
  867. {
  868. int i, remove;
  869. routerinfo_t *r;
  870. if (!rl)
  871. rl = routerlist;
  872. i = 0;
  873. if ((r = router_get_my_routerinfo())) {
  874. smartlist_insert(rl->routers, 0, routerinfo_copy(r));
  875. ++i;
  876. }
  877. for ( ; i < smartlist_len(rl->routers); ++i) {
  878. remove = 0;
  879. r = smartlist_get(rl->routers,i);
  880. if (router_is_me(r)) {
  881. remove = 1;
  882. } else if (r->addr) {
  883. /* already resolved. */
  884. } else if (router_resolve(r)) {
  885. log_fn(LOG_WARN, "Couldn't resolve router '%s' at '%s'; not using",
  886. r->nickname, r->address);
  887. remove = 1;
  888. }
  889. if (remove) {
  890. routerinfo_free(r);
  891. smartlist_del_keeporder(rl->routers, i--);
  892. }
  893. }
  894. return 0;
  895. }
  896. /** Decide whether a given addr:port is definitely accepted,
  897. * definitely rejected, probably accepted, or probably rejected by a
  898. * given policy. If <b>addr</b> is 0, we don't know the IP of the
  899. * target address. If <b>port</b> is 0, we don't know the port of the
  900. * target address.
  901. *
  902. * For now, the algorithm is pretty simple: we look for definite and
  903. * uncertain matches. The first definite match is what we guess; if
  904. * it was proceded by no uncertain matches of the opposite policy,
  905. * then the guess is definite; otherwise it is probable. (If we
  906. * have a known addr and port, all matches are definite; if we have an
  907. * unknown addr/port, any address/port ranges other than "all" are
  908. * uncertain.)
  909. *
  910. * We could do better by assuming that some ranges never match typical
  911. * addresses (127.0.0.1, and so on). But we'll try this for now.
  912. */
  913. addr_policy_result_t
  914. router_compare_addr_to_addr_policy(uint32_t addr, uint16_t port,
  915. addr_policy_t *policy)
  916. {
  917. int maybe_reject = 0;
  918. int maybe_accept = 0;
  919. int match = 0;
  920. int maybe = 0;
  921. addr_policy_t *tmpe;
  922. for (tmpe=policy; tmpe; tmpe=tmpe->next) {
  923. // log_fn(LOG_DEBUG,"Considering exit policy %s", tmpe->string);
  924. maybe = 0;
  925. if (!addr) {
  926. /* Address is unknown. */
  927. if ((port >= tmpe->prt_min && port <= tmpe->prt_max) ||
  928. (!port && tmpe->prt_min<=1 && tmpe->prt_max>=65535)) {
  929. /* The port definitely matches. */
  930. if (tmpe->msk == 0) {
  931. match = 1;
  932. } else {
  933. maybe = 1;
  934. }
  935. } else if (!port) {
  936. /* The port maybe matches. */
  937. maybe = 1;
  938. }
  939. } else {
  940. /* Address is known */
  941. if ((addr & tmpe->msk) == (tmpe->addr & tmpe->msk)) {
  942. if (port >= tmpe->prt_min && port <= tmpe->prt_max) {
  943. /* Exact match for the policy */
  944. match = 1;
  945. } else if (!port) {
  946. maybe = 1;
  947. }
  948. }
  949. }
  950. if (maybe) {
  951. if (tmpe->policy_type == ADDR_POLICY_REJECT)
  952. maybe_reject = 1;
  953. else
  954. maybe_accept = 1;
  955. }
  956. if (match) {
  957. // struct in_addr in;
  958. // in.s_addr = htonl(addr);
  959. // log_fn(LOG_DEBUG,"Address %s:%d matches policy '%s'",
  960. // inet_ntoa(in), port, tmpe->string);
  961. if (tmpe->policy_type == ADDR_POLICY_ACCEPT) {
  962. /* If we already hit a clause that might trigger a 'reject', than we
  963. * can't be sure of this certain 'accept'.*/
  964. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  965. } else {
  966. return maybe_accept ? ADDR_POLICY_PROBABLY_REJECTED : ADDR_POLICY_REJECTED;
  967. }
  968. }
  969. }
  970. /* accept all by default. */
  971. return maybe_reject ? ADDR_POLICY_PROBABLY_ACCEPTED : ADDR_POLICY_ACCEPTED;
  972. }
  973. /** Return 1 if all running sufficiently-stable routers will reject
  974. * addr:port, return 0 if any might accept it. */
  975. int router_exit_policy_all_routers_reject(uint32_t addr, uint16_t port,
  976. int need_uptime) {
  977. int i;
  978. routerinfo_t *router;
  979. addr_policy_result_t r;
  980. if (!routerlist) return 1;
  981. for (i=0;i<smartlist_len(routerlist->routers);i++) {
  982. router = smartlist_get(routerlist->routers, i);
  983. if (router->is_running &&
  984. !router_is_unreliable(router, need_uptime, 0)) {
  985. r = router_compare_addr_to_addr_policy(addr, port, router->exit_policy);
  986. if (r != ADDR_POLICY_REJECTED && r != ADDR_POLICY_PROBABLY_REJECTED)
  987. return 0; /* this one could be ok. good enough. */
  988. }
  989. }
  990. return 1; /* all will reject. */
  991. }
  992. /**
  993. * If <b>policy</b> implicitly allows connections to any port in the
  994. * IP set <b>addr</b>/<b>mask</b>, then set *<b>policy_out</b> to the
  995. * part of the policy that allows it, and return 1. Else return 0.
  996. *
  997. * A policy allows an IP:Port combination <em>implicitly</em> if
  998. * it is included in a *: pattern, or in a fallback pattern.
  999. */
  1000. static int
  1001. policy_includes_addr_mask_implicitly(addr_policy_t *policy,
  1002. uint32_t addr, uint32_t mask,
  1003. addr_policy_t **policy_out)
  1004. {
  1005. uint32_t addr2;
  1006. tor_assert(policy_out);
  1007. addr &= mask;
  1008. addr2 = addr | ~mask;
  1009. for (; policy; policy=policy->next) {
  1010. /* Does this policy cover all of the address range we're looking at? */
  1011. /* Boolean logic time: range X is contained in range Y if, for
  1012. * each bit B, all possible values of B in X are values of B in Y.
  1013. * In "addr", we have every fixed bit set to its value, and every
  1014. * free bit set to 0. In "addr2", we have every fixed bit set to
  1015. * its value, and every free bit set to 1. So if addr and addr2 are
  1016. * both in the policy, the range is covered by the policy.
  1017. */
  1018. if ((policy->addr & policy->msk) == (addr & policy->msk) &&
  1019. (policy->addr & policy->msk) == (addr2 & policy->msk) &&
  1020. (policy->prt_min <= 1 && policy->prt_max == 65535)) {
  1021. return 0;
  1022. }
  1023. /* Does this policy cover some of the address range we're looking at? */
  1024. /* Boolean logic time: range X and range Y intersect if there is
  1025. * some z such that z & Xmask == Xaddr and z & Ymask == Yaddr.
  1026. * This is FALSE iff there is some bit b where Xmask == yMask == 1
  1027. * and Xaddr != Yaddr. So if X intersects with Y iff at every
  1028. * place where Xmask&Ymask==1, Xaddr == Yaddr, or equivalently,
  1029. * Xaddr&Xmask&Ymask == Yaddr&Xmask&Ymask.
  1030. */
  1031. if ((policy->addr & policy->msk & mask) == (addr & policy->msk) &&
  1032. policy->policy_type == ADDR_POLICY_ACCEPT) {
  1033. *policy_out = policy;
  1034. return 1;
  1035. }
  1036. }
  1037. *policy_out = NULL;
  1038. return 1;
  1039. }
  1040. /** If <b>policy</b> implicitly allows connections to any port on
  1041. * 127.*, 192.168.*, etc, then warn (if <b>warn</b> is set) and return
  1042. * true. Else return false.
  1043. **/
  1044. int
  1045. exit_policy_implicitly_allows_local_networks(addr_policy_t *policy,
  1046. int warn)
  1047. {
  1048. addr_policy_t *p;
  1049. int r=0,i;
  1050. static struct {
  1051. uint32_t addr; uint32_t mask; const char *network;
  1052. } private_networks[] = {
  1053. { 0x7f000000, 0xff000000, "localhost (127.x)" },
  1054. { 0x0a000000, 0xff000000, "addresses in private network 10.x" },
  1055. { 0xa9fe0000, 0xffff0000, "addresses in private network 169.254.x" },
  1056. { 0xac100000, 0xfff00000, "addresses in private network 172.16.x" },
  1057. { 0xc0a80000, 0xffff0000, "addresses in private network 192.168.x" },
  1058. { 0,0,NULL},
  1059. };
  1060. for (i=0; private_networks[i].addr; ++i) {
  1061. p = NULL;
  1062. if (policy_includes_addr_mask_implicitly(
  1063. policy, private_networks[i].addr, private_networks[i].mask, &p)) {
  1064. if (warn)
  1065. log_fn(LOG_WARN, "Exit policy %s implicitly accepts %s",
  1066. p?p->string:"(default)",
  1067. private_networks[i].network);
  1068. r = 1;
  1069. }
  1070. }
  1071. return r;
  1072. }
  1073. /** Return true iff <b>router</b> does not permit exit streams.
  1074. */
  1075. int router_exit_policy_rejects_all(routerinfo_t *router) {
  1076. return router_compare_addr_to_addr_policy(0, 0, router->exit_policy)
  1077. == ADDR_POLICY_REJECTED;
  1078. }
  1079. /** Release all space held in <b>rr</b>. */
  1080. void running_routers_free(running_routers_t *rr)
  1081. {
  1082. if (!rr)
  1083. return;
  1084. if (rr->running_routers) {
  1085. SMARTLIST_FOREACH(rr->running_routers, char *, s, tor_free(s));
  1086. smartlist_free(rr->running_routers);
  1087. }
  1088. tor_free(rr);
  1089. }
  1090. /** We've just got a running routers list in <b>rr</b>; update the
  1091. * status of the routers in <b>list</b>, and cache <b>rr</b> */
  1092. void
  1093. routerlist_set_runningrouters(routerlist_t *list, running_routers_t *rr)
  1094. {
  1095. routerlist_update_from_runningrouters(list,rr);
  1096. if (list->running_routers != rr) {
  1097. running_routers_free(list->running_routers);
  1098. list->running_routers = rr;
  1099. }
  1100. }
  1101. /** Update the running/not-running status of every router in <b>list</b>, based
  1102. * on the contents of <b>rr</b>. */
  1103. /* Note: this function is not yet used, since nobody publishes just
  1104. * running-router lists yet. */
  1105. void routerlist_update_from_runningrouters(routerlist_t *list,
  1106. running_routers_t *rr)
  1107. {
  1108. routerinfo_t *me = router_get_my_routerinfo();
  1109. smartlist_t *all_routers;
  1110. if (!list)
  1111. return;
  1112. if (list->published_on >= rr->published_on)
  1113. return;
  1114. if (list->running_routers_updated_on >= rr->published_on)
  1115. return;
  1116. all_routers = smartlist_create();
  1117. if (me) /* learn if the dirservers think I'm verified */
  1118. smartlist_add(all_routers, me);
  1119. smartlist_add_all(all_routers,list->routers);
  1120. SMARTLIST_FOREACH(rr->running_routers, const char *, cp,
  1121. routers_update_status_from_entry(all_routers, rr->published_on,
  1122. cp, rr->is_running_routers_format));
  1123. smartlist_free(all_routers);
  1124. list->running_routers_updated_on = rr->published_on;
  1125. }
  1126. /** Update the is_running and is_verified fields of the router <b>router</b>,
  1127. * based in its status in the list of strings stored in <b>running_list</b>.
  1128. * All entries in <b>running_list</b> follow one of these formats:
  1129. * <ol><li> <b>nickname</b> -- router is running and verified.
  1130. * (running-routers format)
  1131. * <li> !<b>nickname</b> -- router is not-running and verified.
  1132. * (running-routers format)
  1133. * <li> <b>nickname</b>=$<b>hexdigest</b> -- router is running and
  1134. * verified. (router-status format)
  1135. * (router-status format)
  1136. * <li> !<b>nickname</b>=$<b>hexdigest</b> -- router is running and
  1137. * verified. (router-status format)
  1138. * <li> !<b>nickname</b> -- router is not-running and verified.
  1139. * <li> $<b>hexdigest</b> -- router is running and unverified.
  1140. * <li> !$<b>hexdigest</b> -- router is not-running and unverified.
  1141. * </ol>
  1142. *
  1143. * Return 1 if we found router in running_list, else return 0.
  1144. */
  1145. int routers_update_status_from_entry(smartlist_t *routers,
  1146. time_t list_time,
  1147. const char *s,
  1148. int rr_format)
  1149. {
  1150. int is_running = 1;
  1151. int is_verified = 0;
  1152. int hex_digest_set = 0;
  1153. char nickname[MAX_NICKNAME_LEN+1];
  1154. char hexdigest[HEX_DIGEST_LEN+1];
  1155. char digest[DIGEST_LEN];
  1156. const char *cp, *end;
  1157. /* First, parse the entry. */
  1158. cp = s;
  1159. if (*cp == '!') {
  1160. is_running = 0;
  1161. ++cp;
  1162. }
  1163. if (*cp != '$') {
  1164. /* It starts with a non-dollar character; that's a nickname. The nickname
  1165. * entry will either extend to a NUL (old running-routers format) or to an
  1166. * equals sign (new router-status format). */
  1167. is_verified = 1;
  1168. end = strchr(cp, '=');
  1169. if (!end)
  1170. end = strchr(cp,'\0');
  1171. tor_assert(end);
  1172. /* 'end' now points on character beyond the end of the nickname */
  1173. if (end == cp || end-cp > MAX_NICKNAME_LEN) {
  1174. log_fn(LOG_WARN, "Bad nickname length (%d) in router status entry (%s)",
  1175. (int)(end-cp), s);
  1176. return -1;
  1177. }
  1178. memcpy(nickname, cp, end-cp);
  1179. nickname[end-cp]='\0';
  1180. if (!is_legal_nickname(nickname)) {
  1181. log_fn(LOG_WARN, "Bad nickname (%s) in router status entry (%s)",
  1182. nickname, s);
  1183. return -1;
  1184. }
  1185. cp = end;
  1186. if (*cp == '=')
  1187. ++cp;
  1188. }
  1189. /* 'end' now points to the start of a hex digest, or EOS. */
  1190. /* Parse the hexdigest portion of the status. */
  1191. if (*cp == '$') {
  1192. hex_digest_set = 1;
  1193. ++cp;
  1194. if (strlen(cp) != HEX_DIGEST_LEN) {
  1195. log_fn(LOG_WARN, "Bad length (%d) on digest in router status entry (%s)",
  1196. (int)strlen(cp), s);
  1197. return -1;
  1198. }
  1199. strlcpy(hexdigest, cp, sizeof(hexdigest));
  1200. if (base16_decode(digest, DIGEST_LEN, hexdigest, HEX_DIGEST_LEN)<0) {
  1201. log_fn(LOG_WARN, "Invalid digest in router status entry (%s)", s);
  1202. return -1;
  1203. }
  1204. }
  1205. /* Make sure that the entry was in the right format. */
  1206. if (rr_format) {
  1207. if (is_verified == hex_digest_set) {
  1208. log_fn(LOG_WARN, "Invalid syntax for running-routers member (%s)", s);
  1209. return -1;
  1210. }
  1211. } else {
  1212. if (!hex_digest_set) {
  1213. log_fn(LOG_WARN, "Invalid syntax for router-status member (%s)", s);
  1214. return -1;
  1215. }
  1216. }
  1217. /* Okay, we're done parsing. For all routers that match, update their status.
  1218. */
  1219. SMARTLIST_FOREACH(routers, routerinfo_t *, r,
  1220. {
  1221. int nickname_matches = is_verified && !strcasecmp(r->nickname, nickname);
  1222. int digest_matches = !memcmp(digest, r->identity_digest, DIGEST_LEN);
  1223. if (nickname_matches && (digest_matches||rr_format))
  1224. r->is_verified = 1;
  1225. else if (digest_matches)
  1226. r->is_verified = 0;
  1227. if (digest_matches || (nickname_matches&&rr_format))
  1228. if (r->status_set_at < list_time) {
  1229. r->is_running = is_running;
  1230. r->status_set_at = time(NULL);
  1231. }
  1232. });
  1233. return 0;
  1234. }
  1235. /** As router_update_status_from_entry, but consider all entries in
  1236. * running_list. */
  1237. int
  1238. router_update_status_from_smartlist(routerinfo_t *router,
  1239. time_t list_time,
  1240. smartlist_t *running_list,
  1241. int rr_format)
  1242. {
  1243. smartlist_t *rl;
  1244. rl = smartlist_create();
  1245. smartlist_add(rl,router);
  1246. SMARTLIST_FOREACH(running_list, const char *, cp,
  1247. routers_update_status_from_entry(rl,list_time,cp,rr_format));
  1248. smartlist_free(rl);
  1249. return 0;
  1250. }
  1251. /** Add to the list of authorized directory servers one at
  1252. * <b>address</b>:<b>port</b>, with identity key <b>digest</b>. */
  1253. void
  1254. add_trusted_dir_server(const char *address, uint16_t port, const char *digest)
  1255. {
  1256. trusted_dir_server_t *ent;
  1257. uint32_t a;
  1258. if (!trusted_dir_servers)
  1259. trusted_dir_servers = smartlist_create();
  1260. if (tor_lookup_hostname(address, &a)) {
  1261. log_fn(LOG_WARN, "Unable to lookup address for directory server at %s",
  1262. address);
  1263. return;
  1264. }
  1265. ent = tor_malloc(sizeof(trusted_dir_server_t));
  1266. ent->address = tor_strdup(address);
  1267. ent->addr = ntohl(a);
  1268. ent->dir_port = port;
  1269. ent->is_running = 1;
  1270. memcpy(ent->digest, digest, DIGEST_LEN);
  1271. smartlist_add(trusted_dir_servers, ent);
  1272. }
  1273. /** Remove all members from the list of trusted dir servers. */
  1274. void clear_trusted_dir_servers(void)
  1275. {
  1276. if (trusted_dir_servers) {
  1277. SMARTLIST_FOREACH(trusted_dir_servers, trusted_dir_server_t *, ent,
  1278. { tor_free(ent->address); tor_free(ent); });
  1279. smartlist_clear(trusted_dir_servers);
  1280. } else {
  1281. trusted_dir_servers = smartlist_create();
  1282. }
  1283. }