routerlist.c 47 KB

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