routerlist.c 47 KB

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