router.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. const char router_c_id[] =
  7. "$Id$";
  8. #include "or.h"
  9. /**
  10. * \file router.c
  11. * \brief OR functionality, including key maintenance, generating
  12. * and uploading server descriptors, retrying OR connections.
  13. **/
  14. extern long stats_n_seconds_working;
  15. /* Exposed for test.c. */ void get_platform_str(char *platform, size_t len);
  16. /************************************************************/
  17. /*****
  18. * Key management: ORs only.
  19. *****/
  20. /** Private keys for this OR. There is also an SSL key managed by tortls.c.
  21. */
  22. static tor_mutex_t *key_lock=NULL;
  23. static time_t onionkey_set_at=0; /* When was onionkey last changed? */
  24. static crypto_pk_env_t *onionkey=NULL;
  25. static crypto_pk_env_t *lastonionkey=NULL;
  26. static crypto_pk_env_t *identitykey=NULL;
  27. /** Replace the current onion key with <b>k</b>. Does not affect lastonionkey;
  28. * to update onionkey correctly, call rotate_onion_key().
  29. */
  30. void
  31. set_onion_key(crypto_pk_env_t *k)
  32. {
  33. tor_mutex_acquire(key_lock);
  34. onionkey = k;
  35. onionkey_set_at = time(NULL);
  36. tor_mutex_release(key_lock);
  37. mark_my_descriptor_dirty();
  38. }
  39. /** Return the current onion key. Requires that the onion key has been
  40. * loaded or generated. */
  41. crypto_pk_env_t *
  42. get_onion_key(void)
  43. {
  44. tor_assert(onionkey);
  45. return onionkey;
  46. }
  47. /** Store a copy of the current onion key into *<b>key</b>, and a copy
  48. * of the most recent onion key into *<b>last</b>.
  49. */
  50. void
  51. dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last)
  52. {
  53. tor_assert(key);
  54. tor_assert(last);
  55. tor_mutex_acquire(key_lock);
  56. tor_assert(onionkey);
  57. *key = crypto_pk_dup_key(onionkey);
  58. if (lastonionkey)
  59. *last = crypto_pk_dup_key(lastonionkey);
  60. else
  61. *last = NULL;
  62. tor_mutex_release(key_lock);
  63. }
  64. /** Return the time when the onion key was last set. This is either the time
  65. * when the process launched, or the time of the most recent key rotation since
  66. * the process launched.
  67. */
  68. time_t
  69. get_onion_key_set_at(void)
  70. {
  71. return onionkey_set_at;
  72. }
  73. /** Set the current identity key to k.
  74. */
  75. void
  76. set_identity_key(crypto_pk_env_t *k)
  77. {
  78. if (identitykey)
  79. crypto_free_pk_env(identitykey);
  80. identitykey = k;
  81. }
  82. /** Returns the current identity key; requires that the identity key has been
  83. * set.
  84. */
  85. crypto_pk_env_t *
  86. get_identity_key(void)
  87. {
  88. tor_assert(identitykey);
  89. return identitykey;
  90. }
  91. /** Return true iff the identity key has been set. */
  92. int
  93. identity_key_is_set(void)
  94. {
  95. return identitykey != NULL;
  96. }
  97. /** Replace the previous onion key with the current onion key, and generate
  98. * a new previous onion key. Immediately after calling this function,
  99. * the OR should:
  100. * - schedule all previous cpuworkers to shut down _after_ processing
  101. * pending work. (This will cause fresh cpuworkers to be generated.)
  102. * - generate and upload a fresh routerinfo.
  103. */
  104. void
  105. rotate_onion_key(void)
  106. {
  107. char fname[512];
  108. char fname_prev[512];
  109. crypto_pk_env_t *prkey;
  110. tor_snprintf(fname,sizeof(fname),
  111. "%s/keys/secret_onion_key",get_options()->DataDirectory);
  112. tor_snprintf(fname_prev,sizeof(fname_prev),
  113. "%s/keys/secret_onion_key.old",get_options()->DataDirectory);
  114. if (!(prkey = crypto_new_pk_env())) {
  115. log_err(LD_GENERAL,"Error creating crypto environment.");
  116. goto error;
  117. }
  118. if (crypto_pk_generate_key(prkey)) {
  119. log_err(LD_BUG,"Error generating onion key");
  120. goto error;
  121. }
  122. if (file_status(fname) == FN_FILE) {
  123. if (replace_file(fname, fname_prev))
  124. goto error;
  125. }
  126. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  127. log_err(LD_FS,"Couldn't write generated key to \"%s\".", fname);
  128. goto error;
  129. }
  130. log_info(LD_GENERAL, "Rotating onion key");
  131. tor_mutex_acquire(key_lock);
  132. if (lastonionkey)
  133. crypto_free_pk_env(lastonionkey);
  134. lastonionkey = onionkey;
  135. onionkey = prkey;
  136. onionkey_set_at = time(NULL);
  137. tor_mutex_release(key_lock);
  138. mark_my_descriptor_dirty();
  139. return;
  140. error:
  141. log_warn(LD_GENERAL, "Couldn't rotate onion key.");
  142. }
  143. /* Read an RSA secret key key from a file that was once named fname_old,
  144. * but is now named fname_new. Rename the file from old to new as needed.
  145. */
  146. static crypto_pk_env_t *
  147. init_key_from_file_name_changed(const char *fname_old,
  148. const char *fname_new)
  149. {
  150. if (file_status(fname_new) == FN_FILE || file_status(fname_old) != FN_FILE)
  151. /* The new filename is there, or both are, or neither is. */
  152. return init_key_from_file(fname_new);
  153. /* The old filename exists, and the new one doesn't. Rename and load. */
  154. if (rename(fname_old, fname_new) < 0) {
  155. log_warn(LD_FS, "Couldn't rename \"%s\" to \"%s\": %s",
  156. fname_old, fname_new, strerror(errno));
  157. return NULL;
  158. }
  159. return init_key_from_file(fname_new);
  160. }
  161. /** Try to read an RSA key from <b>fname</b>. If <b>fname</b> doesn't exist,
  162. * create a new RSA key and save it in <b>fname</b>. Return the read/created
  163. * key, or NULL on error.
  164. */
  165. crypto_pk_env_t *
  166. init_key_from_file(const char *fname)
  167. {
  168. crypto_pk_env_t *prkey = NULL;
  169. FILE *file = NULL;
  170. if (!(prkey = crypto_new_pk_env())) {
  171. log_err(LD_GENERAL,"Error creating crypto environment.");
  172. goto error;
  173. }
  174. switch (file_status(fname)) {
  175. case FN_DIR:
  176. case FN_ERROR:
  177. log_err(LD_FS,"Can't read key from \"%s\"", fname);
  178. goto error;
  179. case FN_NOENT:
  180. log_info(LD_GENERAL, "No key found in \"%s\"; generating fresh key.",
  181. fname);
  182. if (crypto_pk_generate_key(prkey)) {
  183. log_err(LD_GENERAL,"Error generating onion key");
  184. goto error;
  185. }
  186. if (crypto_pk_check_key(prkey) <= 0) {
  187. log_err(LD_GENERAL,"Generated key seems invalid");
  188. goto error;
  189. }
  190. log_info(LD_GENERAL, "Generated key seems valid");
  191. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  192. log_err(LD_FS,"Couldn't write generated key to \"%s\".", fname);
  193. goto error;
  194. }
  195. return prkey;
  196. case FN_FILE:
  197. if (crypto_pk_read_private_key_from_filename(prkey, fname)) {
  198. log_err(LD_GENERAL,"Error loading private key.");
  199. goto error;
  200. }
  201. return prkey;
  202. default:
  203. tor_assert(0);
  204. }
  205. error:
  206. if (prkey)
  207. crypto_free_pk_env(prkey);
  208. if (file)
  209. fclose(file);
  210. return NULL;
  211. }
  212. /** Initialize all OR private keys, and the TLS context, as necessary.
  213. * On OPs, this only initializes the tls context.
  214. */
  215. int
  216. init_keys(void)
  217. {
  218. /* XXX009 Two problems with how this is called:
  219. * 1. It should be idempotent for servers, so we can call init_keys
  220. * as much as we need to.
  221. */
  222. char keydir[512];
  223. char keydir2[512];
  224. char fingerprint[FINGERPRINT_LEN+1];
  225. /*nickname fp\n\0 */
  226. char fingerprint_line[FINGERPRINT_LEN+MAX_NICKNAME_LEN+3];
  227. char *cp;
  228. const char *mydesc, *datadir;
  229. crypto_pk_env_t *prkey;
  230. char digest[20];
  231. or_options_t *options = get_options();
  232. if (!key_lock)
  233. key_lock = tor_mutex_new();
  234. /* OP's don't need persistent keys; just make up an identity and
  235. * initialize the TLS context. */
  236. if (!server_mode(options)) {
  237. if (!(prkey = crypto_new_pk_env()))
  238. return -1;
  239. if (crypto_pk_generate_key(prkey))
  240. return -1;
  241. set_identity_key(prkey);
  242. /* Create a TLS context; default the client nickname to "client". */
  243. if (tor_tls_context_new(get_identity_key(), 1,
  244. options->Nickname ? options->Nickname : "client",
  245. MAX_SSL_KEY_LIFETIME) < 0) {
  246. log_err(LD_GENERAL,"Error creating TLS context for OP.");
  247. return -1;
  248. }
  249. return 0;
  250. }
  251. /* Make sure DataDirectory exists, and is private. */
  252. datadir = options->DataDirectory;
  253. if (check_private_dir(datadir, CPD_CREATE)) {
  254. return -1;
  255. }
  256. /* Check the key directory. */
  257. tor_snprintf(keydir,sizeof(keydir),"%s/keys", datadir);
  258. if (check_private_dir(keydir, CPD_CREATE)) {
  259. return -1;
  260. }
  261. cp = keydir + strlen(keydir); /* End of string. */
  262. /* 1. Read identity key. Make it if none is found. */
  263. tor_snprintf(keydir,sizeof(keydir),"%s/keys/identity.key",datadir);
  264. tor_snprintf(keydir2,sizeof(keydir2),"%s/keys/secret_id_key",datadir);
  265. log_info(LD_GENERAL,"Reading/making identity key \"%s\"...",keydir2);
  266. prkey = init_key_from_file_name_changed(keydir,keydir2);
  267. if (!prkey) return -1;
  268. set_identity_key(prkey);
  269. /* 2. Read onion key. Make it if none is found. */
  270. tor_snprintf(keydir,sizeof(keydir),"%s/keys/onion.key",datadir);
  271. tor_snprintf(keydir2,sizeof(keydir2),"%s/keys/secret_onion_key",datadir);
  272. log_info(LD_GENERAL,"Reading/making onion key \"%s\"...",keydir2);
  273. prkey = init_key_from_file_name_changed(keydir,keydir2);
  274. if (!prkey) return -1;
  275. set_onion_key(prkey);
  276. tor_snprintf(keydir,sizeof(keydir),"%s/keys/secret_onion_key.old",datadir);
  277. if (file_status(keydir) == FN_FILE) {
  278. prkey = init_key_from_file(keydir);
  279. if (prkey)
  280. lastonionkey = prkey;
  281. }
  282. /* 3. Initialize link key and TLS context. */
  283. if (tor_tls_context_new(get_identity_key(), 1, options->Nickname,
  284. MAX_SSL_KEY_LIFETIME) < 0) {
  285. log_err(LD_GENERAL,"Error initializing TLS context");
  286. return -1;
  287. }
  288. /* 4. Dump router descriptor to 'router.desc' */
  289. /* Must be called after keys are initialized. */
  290. mydesc = router_get_my_descriptor();
  291. if (!mydesc) {
  292. log_err(LD_GENERAL,"Error initializing descriptor.");
  293. return -1;
  294. }
  295. if (authdir_mode(options)) {
  296. const char *m;
  297. /* We need to add our own fingerprint so it gets recognized. */
  298. if (dirserv_add_own_fingerprint(options->Nickname, get_identity_key())) {
  299. log_err(LD_GENERAL,"Error adding own fingerprint to approved set");
  300. return -1;
  301. }
  302. if (dirserv_add_descriptor(mydesc, &m) < 0) {
  303. log_err(LD_GENERAL,"Unable to add own descriptor to directory: %s",
  304. m?m:"<unknown error>");
  305. return -1;
  306. }
  307. }
  308. tor_snprintf(keydir,sizeof(keydir),"%s/router.desc", datadir);
  309. log_info(LD_GENERAL,"Dumping descriptor to \"%s\"...",keydir);
  310. if (write_str_to_file(keydir, mydesc,0)) {
  311. return -1;
  312. }
  313. /* 5. Dump fingerprint to 'fingerprint' */
  314. tor_snprintf(keydir,sizeof(keydir),"%s/fingerprint", datadir);
  315. log_info(LD_GENERAL,"Dumping fingerprint to \"%s\"...",keydir);
  316. if (crypto_pk_get_fingerprint(get_identity_key(), fingerprint, 1)<0) {
  317. log_err(LD_GENERAL,"Error computing fingerprint");
  318. return -1;
  319. }
  320. tor_assert(strlen(options->Nickname) <= MAX_NICKNAME_LEN);
  321. if (tor_snprintf(fingerprint_line, sizeof(fingerprint_line),
  322. "%s %s\n",options->Nickname, fingerprint) < 0) {
  323. log_err(LD_GENERAL,"Error writing fingerprint line");
  324. return -1;
  325. }
  326. if (write_str_to_file(keydir, fingerprint_line, 0)) {
  327. log_err(LD_FS, "Error writing fingerprint line to file");
  328. return -1;
  329. }
  330. if (!authdir_mode(options))
  331. return 0;
  332. /* 6. [authdirserver only] load approved-routers file */
  333. tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", datadir);
  334. log_info(LD_DIRSERV,"Loading approved fingerprints from \"%s\"...",keydir);
  335. if (dirserv_parse_fingerprint_file(keydir) < 0) {
  336. log_err(LD_GENERAL,"Error loading fingerprints");
  337. return -1;
  338. }
  339. /* 6b. [authdirserver only] add own key to approved directories. */
  340. crypto_pk_get_digest(get_identity_key(), digest);
  341. if (!router_digest_is_trusted_dir(digest)) {
  342. add_trusted_dir_server(options->Nickname, NULL,
  343. (uint16_t)options->DirPort, digest,
  344. options->V1AuthoritativeDir);
  345. }
  346. /* success */
  347. return 0;
  348. }
  349. /* Keep track of whether we should upload our server descriptor,
  350. * and what type of server we are.
  351. */
  352. /** Whether we can reach our ORPort from the outside. */
  353. static int can_reach_or_port = 0;
  354. /** Whether we can reach our DirPort from the outside. */
  355. static int can_reach_dir_port = 0;
  356. /** Return 1 if or port is known reachable; else return 0. */
  357. int
  358. check_whether_orport_reachable(void)
  359. {
  360. or_options_t *options = get_options();
  361. return clique_mode(options) ||
  362. options->AssumeReachable ||
  363. can_reach_or_port;
  364. }
  365. /** Return 1 if we don't have a dirport configured, or if it's reachable. */
  366. int
  367. check_whether_dirport_reachable(void)
  368. {
  369. or_options_t *options = get_options();
  370. return !options->DirPort ||
  371. options->AssumeReachable ||
  372. we_are_hibernating() ||
  373. can_reach_dir_port;
  374. }
  375. /** Look at a variety of factors, and return 0 if we don't want to
  376. * advertise the fact that we have a DirPort open. Else return the
  377. * DirPort we want to advertise. */
  378. static int
  379. decide_to_advertise_dirport(or_options_t *options, routerinfo_t *router)
  380. {
  381. if (!router->dir_port) /* short circuit the rest of the function */
  382. return 0;
  383. if (authdir_mode(options)) /* always publish */
  384. return router->dir_port;
  385. if (we_are_hibernating())
  386. return 0;
  387. if (!check_whether_dirport_reachable())
  388. return 0;
  389. if (router->bandwidthcapacity >= router->bandwidthrate) {
  390. /* check if we might potentially hibernate. */
  391. if (options->AccountingMax != 0)
  392. return 0;
  393. /* also check if we're advertising a small amount, and have
  394. a "boring" DirPort. */
  395. if (router->bandwidthrate < 50000 && router->dir_port > 1024)
  396. return 0;
  397. }
  398. /* Sounds like a great idea. Let's publish it. */
  399. return router->dir_port;
  400. }
  401. /** Some time has passed, or we just got new directory information.
  402. * See if we currently believe our ORPort or DirPort to be
  403. * unreachable. If so, launch a new test for it.
  404. *
  405. * For ORPort, we simply try making a circuit that ends at ourselves.
  406. * Success is noticed in onionskin_answer().
  407. *
  408. * For DirPort, we make a connection via Tor to our DirPort and ask
  409. * for our own server descriptor.
  410. * Success is noticed in connection_dir_client_reached_eof().
  411. */
  412. void
  413. consider_testing_reachability(void)
  414. {
  415. routerinfo_t *me = router_get_my_routerinfo();
  416. if (!me) {
  417. log_warn(LD_BUG,
  418. "Bug: router_get_my_routerinfo() did not find my routerinfo?");
  419. return;
  420. }
  421. if (!check_whether_orport_reachable()) {
  422. circuit_launch_by_router(CIRCUIT_PURPOSE_TESTING, me, 0, 1, 1);
  423. }
  424. if (!check_whether_dirport_reachable()) {
  425. /* ask myself, via tor, for my server descriptor. */
  426. directory_initiate_command_router(me, DIR_PURPOSE_FETCH_SERVERDESC,
  427. 1, "authority", NULL, 0);
  428. }
  429. }
  430. /** Annotate that we found our ORPort reachable. */
  431. void
  432. router_orport_found_reachable(void)
  433. {
  434. if (!can_reach_or_port) {
  435. if (!clique_mode(get_options()))
  436. log_notice(LD_OR,"Self-testing indicates your ORPort is reachable from "
  437. "the outside. Excellent.%s",
  438. get_options()->PublishServerDescriptor ?
  439. " Publishing server descriptor." : "");
  440. can_reach_or_port = 1;
  441. mark_my_descriptor_dirty();
  442. consider_publishable_server(time(NULL), 1);
  443. }
  444. }
  445. /** Annotate that we found our DirPort reachable. */
  446. void
  447. router_dirport_found_reachable(void)
  448. {
  449. if (!can_reach_dir_port) {
  450. log_notice(LD_DIRSERV,"Self-testing indicates your DirPort is reachable "
  451. "from the outside. Excellent.");
  452. can_reach_dir_port = 1;
  453. }
  454. }
  455. /** Our router has just moved to a new IP. Reset stats. */
  456. void
  457. server_has_changed_ip(void)
  458. {
  459. stats_n_seconds_working = 0;
  460. can_reach_or_port = 0;
  461. can_reach_dir_port = 0;
  462. mark_my_descriptor_dirty();
  463. }
  464. /** Return true iff we believe ourselves to be an authoritative
  465. * directory server.
  466. */
  467. int
  468. authdir_mode(or_options_t *options)
  469. {
  470. return options->AuthoritativeDir != 0;
  471. }
  472. /** Return true iff we try to stay connected to all ORs at once.
  473. */
  474. int
  475. clique_mode(or_options_t *options)
  476. {
  477. return authdir_mode(options);
  478. }
  479. /** Return true iff we are trying to be a server.
  480. */
  481. int
  482. server_mode(or_options_t *options)
  483. {
  484. if (options->ClientOnly) return 0;
  485. return (options->ORPort != 0 || options->ORListenAddress);
  486. }
  487. /** Remember if we've advertised ourselves to the dirservers. */
  488. static int server_is_advertised=0;
  489. /** Return true iff we have published our descriptor lately.
  490. */
  491. int
  492. advertised_server_mode(void)
  493. {
  494. return server_is_advertised;
  495. }
  496. /**
  497. * Called with a boolean: set whether we have recently published our
  498. * descriptor.
  499. */
  500. static void
  501. set_server_advertised(int s)
  502. {
  503. server_is_advertised = s;
  504. }
  505. /** Return true iff we are trying to be a socks proxy. */
  506. int
  507. proxy_mode(or_options_t *options)
  508. {
  509. return (options->SocksPort != 0 || options->SocksListenAddress);
  510. }
  511. /** Decide if we're a publishable server. We are a publishable server if:
  512. * - We don't have the ClientOnly option set
  513. * and
  514. * - We have the PublishServerDescriptor option set
  515. * and
  516. * - We have ORPort set
  517. * and
  518. * - We believe we are reachable from the outside; or
  519. * - We have the AuthoritativeDirectory option set.
  520. */
  521. static int
  522. decide_if_publishable_server(time_t now)
  523. {
  524. or_options_t *options = get_options();
  525. if (options->ClientOnly)
  526. return 0;
  527. if (!options->PublishServerDescriptor)
  528. return 0;
  529. if (!server_mode(options))
  530. return 0;
  531. if (options->AuthoritativeDir)
  532. return 1;
  533. return check_whether_orport_reachable();
  534. }
  535. /** Initiate server descriptor upload as reasonable (if server is publishable,
  536. * etc). <b>force</b> is as for router_upload_dir_desc_to_dirservers.
  537. *
  538. * We need to rebuild the descriptor if it's dirty even if we're not
  539. * uploading, because our reachability testing *uses* our descriptor to
  540. * determine what IP address and ports to test.
  541. */
  542. void
  543. consider_publishable_server(time_t now, int force)
  544. {
  545. int rebuilt;
  546. if (!server_mode(get_options()))
  547. return;
  548. rebuilt = router_rebuild_descriptor(0);
  549. if (decide_if_publishable_server(now)) {
  550. set_server_advertised(1);
  551. if (rebuilt == 0)
  552. router_upload_dir_desc_to_dirservers(force);
  553. } else {
  554. set_server_advertised(0);
  555. }
  556. }
  557. /*
  558. * Clique maintenance
  559. */
  560. /** OR only: if in clique mode, try to open connections to all of the
  561. * other ORs we know about. Otherwise, open connections to those we
  562. * think are in clique mode.
  563. *
  564. * If <b>testing_reachability</b> is 0, try to open the connections
  565. * but only if we don't already have one. If it's 1, then we're an
  566. * auth dir server, and we should try to connect regardless of
  567. * whether we already have a connection open -- but if <b>try_all</b>
  568. * is 0, we want to load balance such that we only try a few connections
  569. * per call.
  570. *
  571. * The load balancing is such that if we get called once every ten
  572. * seconds, we will cycle through all the tests in 1280 seconds (a
  573. * bit over 20 minutes).
  574. */
  575. void
  576. router_retry_connections(int testing_reachability, int try_all)
  577. {
  578. time_t now = time(NULL);
  579. routerlist_t *rl = router_get_routerlist();
  580. or_options_t *options = get_options();
  581. static char ctr = 0;
  582. tor_assert(server_mode(options));
  583. SMARTLIST_FOREACH(rl->routers, routerinfo_t *, router, {
  584. const char *id_digest = router->cache_info.identity_digest;
  585. if (router_is_me(router))
  586. continue;
  587. if (!clique_mode(options) && !router_is_clique_mode(router))
  588. continue;
  589. if ((testing_reachability &&
  590. (try_all || (((uint8_t)id_digest[0]) % 128) == ctr)) ||
  591. (!testing_reachability &&
  592. !connection_or_get_by_identity_digest(id_digest))) {
  593. log_debug(LD_OR,"%sconnecting to %s at %s:%u.",
  594. clique_mode(options) ? "(forced) " : "",
  595. router->nickname, router->address, router->or_port);
  596. /* Remember when we started trying to determine reachability */
  597. if (!router->testing_since)
  598. router->testing_since = now;
  599. connection_or_connect(router->addr, router->or_port,
  600. id_digest);
  601. }
  602. });
  603. if (testing_reachability && !try_all) /* increment ctr */
  604. ctr = (ctr + 1) % 128;
  605. }
  606. /** Return true iff this OR should try to keep connections open to all
  607. * other ORs. */
  608. int
  609. router_is_clique_mode(routerinfo_t *router)
  610. {
  611. if (router_digest_is_trusted_dir(router->cache_info.identity_digest))
  612. return 1;
  613. return 0;
  614. }
  615. /*
  616. * OR descriptor generation.
  617. */
  618. /** My routerinfo. */
  619. static routerinfo_t *desc_routerinfo = NULL;
  620. /** Since when has our descriptor been "clean"? 0 if we need to regenerate it
  621. * now. */
  622. static time_t desc_clean_since = 0;
  623. /** Boolean: do we need to regenerate the above? */
  624. static int desc_needs_upload = 0;
  625. /** OR only: If <b>force</b> is true, or we haven't uploaded this
  626. * descriptor successfully yet, try to upload our signed descriptor to
  627. * all the directory servers we know about.
  628. */
  629. void
  630. router_upload_dir_desc_to_dirservers(int force)
  631. {
  632. const char *s;
  633. s = router_get_my_descriptor();
  634. if (!s) {
  635. log_warn(LD_GENERAL, "No descriptor; skipping upload");
  636. return;
  637. }
  638. if (!get_options()->PublishServerDescriptor)
  639. return;
  640. if (!force && !desc_needs_upload)
  641. return;
  642. desc_needs_upload = 0;
  643. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_DIR, s, strlen(s));
  644. }
  645. /** OR only: Check whether my exit policy says to allow connection to
  646. * conn. Return 0 if we accept; non-0 if we reject.
  647. */
  648. int
  649. router_compare_to_my_exit_policy(connection_t *conn)
  650. {
  651. tor_assert(desc_routerinfo);
  652. /* make sure it's resolved to something. this way we can't get a
  653. 'maybe' below. */
  654. if (!conn->addr)
  655. return -1;
  656. return router_compare_addr_to_addr_policy(conn->addr, conn->port,
  657. desc_routerinfo->exit_policy) != ADDR_POLICY_ACCEPTED;
  658. }
  659. /** Return true iff I'm a server and <b>digest</b> is equal to
  660. * my identity digest. */
  661. int
  662. router_digest_is_me(const char *digest)
  663. {
  664. routerinfo_t *me = router_get_my_routerinfo();
  665. if (!me || memcmp(me->cache_info.identity_digest, digest, DIGEST_LEN))
  666. return 0;
  667. return 1;
  668. }
  669. /** A wrapper around router_digest_is_me(). */
  670. int
  671. router_is_me(routerinfo_t *router)
  672. {
  673. return router_digest_is_me(router->cache_info.identity_digest);
  674. }
  675. /** Return true iff <b>fp</b> is a hex fingerprint of my identity digest. */
  676. int
  677. router_fingerprint_is_me(const char *fp)
  678. {
  679. char digest[DIGEST_LEN];
  680. if (strlen(fp) == HEX_DIGEST_LEN &&
  681. base16_decode(digest, sizeof(digest), fp, HEX_DIGEST_LEN) == 0)
  682. return router_digest_is_me(digest);
  683. return 0;
  684. }
  685. /** Return a routerinfo for this OR, rebuilding a fresh one if
  686. * necessary. Return NULL on error, or if called on an OP. */
  687. routerinfo_t *
  688. router_get_my_routerinfo(void)
  689. {
  690. if (!server_mode(get_options()))
  691. return NULL;
  692. if (!desc_routerinfo) {
  693. if (router_rebuild_descriptor(1))
  694. return NULL;
  695. }
  696. return desc_routerinfo;
  697. }
  698. /** OR only: Return a signed server descriptor for this OR, rebuilding a fresh
  699. * one if necessary. Return NULL on error.
  700. */
  701. const char *
  702. router_get_my_descriptor(void)
  703. {
  704. const char *body;
  705. if (!desc_routerinfo) {
  706. if (router_rebuild_descriptor(1))
  707. return NULL;
  708. }
  709. body = signed_descriptor_get_body(&desc_routerinfo->cache_info);
  710. log_debug(LD_GENERAL,"my desc is '%s'", body);
  711. return body;
  712. }
  713. /*DOCDOC*/
  714. static smartlist_t *warned_nonexistent_family = NULL;
  715. /** If <b>force</b> is true, or our descriptor is out-of-date, rebuild
  716. * a fresh routerinfo and signed server descriptor for this OR.
  717. * Return 0 on success, -1 on error.
  718. */
  719. int
  720. router_rebuild_descriptor(int force)
  721. {
  722. routerinfo_t *ri;
  723. uint32_t addr;
  724. char platform[256];
  725. int hibernating = we_are_hibernating();
  726. or_options_t *options = get_options();
  727. if (desc_clean_since && !force)
  728. return 0;
  729. if (resolve_my_address(options, &addr, NULL) < 0) {
  730. log_warn(LD_CONFIG,"options->Address didn't resolve into an IP.");
  731. return -1;
  732. }
  733. ri = tor_malloc_zero(sizeof(routerinfo_t));
  734. ri->address = tor_dup_addr(addr);
  735. ri->nickname = tor_strdup(options->Nickname);
  736. ri->addr = addr;
  737. ri->or_port = options->ORPort;
  738. ri->dir_port = options->DirPort;
  739. ri->cache_info.published_on = time(NULL);
  740. ri->onion_pkey = crypto_pk_dup_key(get_onion_key()); /* must invoke from
  741. * main thread */
  742. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  743. if (crypto_pk_get_digest(ri->identity_pkey,
  744. ri->cache_info.identity_digest)<0) {
  745. routerinfo_free(ri);
  746. return -1;
  747. }
  748. get_platform_str(platform, sizeof(platform));
  749. ri->platform = tor_strdup(platform);
  750. ri->bandwidthrate = (int)options->BandwidthRate;
  751. ri->bandwidthburst = (int)options->BandwidthBurst;
  752. ri->bandwidthcapacity = hibernating ? 0 : rep_hist_bandwidth_assess();
  753. if (options->BandwidthRate > options->MaxAdvertisedBandwidth)
  754. ri->bandwidthrate = (int)options->MaxAdvertisedBandwidth;
  755. config_parse_exit_policy(options->ExitPolicy, &ri->exit_policy,
  756. options->ExitPolicyRejectPrivate);
  757. if (desc_routerinfo) { /* inherit values */
  758. ri->is_verified = desc_routerinfo->is_verified;
  759. ri->is_running = desc_routerinfo->is_running;
  760. ri->is_named = desc_routerinfo->is_named;
  761. }
  762. if (authdir_mode(options))
  763. ri->is_verified = ri->is_named = 1; /* believe in yourself */
  764. if (options->MyFamily) {
  765. smartlist_t *family;
  766. if (!warned_nonexistent_family)
  767. warned_nonexistent_family = smartlist_create();
  768. family = smartlist_create();
  769. ri->declared_family = smartlist_create();
  770. smartlist_split_string(family, options->MyFamily, ",",
  771. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
  772. SMARTLIST_FOREACH(family, char *, name,
  773. {
  774. routerinfo_t *member;
  775. if (!strcasecmp(name, options->Nickname))
  776. member = ri;
  777. else
  778. member = router_get_by_nickname(name, 1);
  779. if (!member) {
  780. if (!smartlist_string_isin(warned_nonexistent_family, name)) {
  781. log_warn(LD_CONFIG,
  782. "I have no descriptor for the router named \"%s\" "
  783. "in my declared family; I'll use the nickname as is, but "
  784. "this may confuse clients.", name);
  785. smartlist_add(warned_nonexistent_family, tor_strdup(name));
  786. }
  787. smartlist_add(ri->declared_family, name);
  788. name = NULL;
  789. } else {
  790. char *fp = tor_malloc(HEX_DIGEST_LEN+2);
  791. fp[0] = '$';
  792. base16_encode(fp+1,HEX_DIGEST_LEN+1,
  793. member->cache_info.identity_digest, DIGEST_LEN);
  794. smartlist_add(ri->declared_family, fp);
  795. if (smartlist_string_isin(warned_nonexistent_family, name))
  796. smartlist_string_remove(warned_nonexistent_family, name);
  797. }
  798. tor_free(name);
  799. });
  800. smartlist_free(family);
  801. }
  802. ri->cache_info.signed_descriptor_body = tor_malloc(8192);
  803. if (router_dump_router_to_string(ri->cache_info.signed_descriptor_body, 8192,
  804. ri, get_identity_key())<0) {
  805. log_warn(LD_BUG, "Couldn't allocate string for descriptor.");
  806. return -1;
  807. }
  808. ri->cache_info.signed_descriptor_len =
  809. strlen(ri->cache_info.signed_descriptor_body);
  810. crypto_digest(ri->cache_info.signed_descriptor_digest,
  811. ri->cache_info.signed_descriptor_body,
  812. ri->cache_info.signed_descriptor_len);
  813. if (desc_routerinfo)
  814. routerinfo_free(desc_routerinfo);
  815. desc_routerinfo = ri;
  816. desc_clean_since = time(NULL);
  817. desc_needs_upload = 1;
  818. return 0;
  819. }
  820. /** Mark descriptor out of date if it's older than <b>when</b> */
  821. void
  822. mark_my_descriptor_dirty_if_older_than(time_t when)
  823. {
  824. if (desc_clean_since < when)
  825. mark_my_descriptor_dirty();
  826. }
  827. /** Call when the current descriptor is out of date. */
  828. void
  829. mark_my_descriptor_dirty(void)
  830. {
  831. desc_clean_since = 0;
  832. }
  833. #define MAX_BANDWIDTH_CHANGE_FREQ 20*60
  834. /** Check whether bandwidth has changed a lot since the last time we announced
  835. * bandwidth. If so, mark our descriptor dirty. */
  836. void
  837. check_descriptor_bandwidth_changed(time_t now)
  838. {
  839. static time_t last_changed = 0;
  840. uint64_t prev, cur;
  841. if (!desc_routerinfo)
  842. return;
  843. prev = desc_routerinfo->bandwidthcapacity;
  844. cur = we_are_hibernating() ? 0 : rep_hist_bandwidth_assess();
  845. if ((prev != cur && (!prev || !cur)) ||
  846. cur > prev*2 ||
  847. cur < prev/2) {
  848. if (last_changed+MAX_BANDWIDTH_CHANGE_FREQ < now) {
  849. log_info(LD_GENERAL,
  850. "Measured bandwidth has changed; rebuilding descriptor.");
  851. mark_my_descriptor_dirty();
  852. last_changed = now;
  853. }
  854. }
  855. }
  856. /** Check whether our own address as defined by the Address configuration
  857. * has changed. This is for routers that get their address from a service
  858. * like dyndns. If our address has changed, mark our descriptor dirty. */
  859. void
  860. check_descriptor_ipaddress_changed(time_t now)
  861. {
  862. uint32_t prev, cur;
  863. or_options_t *options = get_options();
  864. if (!desc_routerinfo)
  865. return;
  866. prev = desc_routerinfo->addr;
  867. if (resolve_my_address(options, &cur, NULL) < 0) {
  868. log_warn(LD_CONFIG,"options->Address didn't resolve into an IP.");
  869. return;
  870. }
  871. if (prev != cur) {
  872. char addrbuf_prev[INET_NTOA_BUF_LEN];
  873. char addrbuf_cur[INET_NTOA_BUF_LEN];
  874. struct in_addr in_prev;
  875. struct in_addr in_cur;
  876. in_prev.s_addr = htonl(prev);
  877. tor_inet_ntoa(&in_prev, addrbuf_prev, sizeof(addrbuf_prev));
  878. in_cur.s_addr = htonl(cur);
  879. tor_inet_ntoa(&in_cur, addrbuf_cur, sizeof(addrbuf_cur));
  880. log_info(LD_GENERAL,
  881. "Our IP Address has changed from %s to %s; "
  882. "rebuilding descriptor.",
  883. addrbuf_prev, addrbuf_cur);
  884. mark_my_descriptor_dirty();
  885. }
  886. }
  887. /** Set <b>platform</b> (max length <b>len</b>) to a NUL-terminated short
  888. * string describing the version of Tor and the operating system we're
  889. * currently running on.
  890. */
  891. void
  892. get_platform_str(char *platform, size_t len)
  893. {
  894. tor_snprintf(platform, len, "Tor %s on %s",
  895. VERSION, get_uname());
  896. return;
  897. }
  898. /* XXX need to audit this thing and count fenceposts. maybe
  899. * refactor so we don't have to keep asking if we're
  900. * near the end of maxlen?
  901. */
  902. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  903. /** OR only: Given a routerinfo for this router, and an identity key to sign
  904. * with, encode the routerinfo as a signed server descriptor and write the
  905. * result into <b>s</b>, using at most <b>maxlen</b> bytes. Return -1 on
  906. * failure, and the number of bytes used on success.
  907. */
  908. int
  909. router_dump_router_to_string(char *s, size_t maxlen, routerinfo_t *router,
  910. crypto_pk_env_t *ident_key)
  911. {
  912. char *onion_pkey; /* Onion key, PEM-encoded. */
  913. char *identity_pkey; /* Identity key, PEM-encoded. */
  914. char digest[DIGEST_LEN];
  915. char published[ISO_TIME_LEN+1];
  916. char fingerprint[FINGERPRINT_LEN+1];
  917. struct in_addr in;
  918. char addrbuf[INET_NTOA_BUF_LEN];
  919. size_t onion_pkeylen, identity_pkeylen;
  920. size_t written;
  921. int result=0;
  922. addr_policy_t *tmpe;
  923. char *bandwidth_usage;
  924. char *family_line;
  925. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  926. char *s_tmp, *s_dup;
  927. const char *cp;
  928. routerinfo_t *ri_tmp;
  929. #endif
  930. or_options_t *options = get_options();
  931. /* Make sure the identity key matches the one in the routerinfo. */
  932. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  933. log_warn(LD_BUG,"Tried to sign a router with a private key that didn't "
  934. "match router's public key!");
  935. return -1;
  936. }
  937. /* record our fingerprint, so we can include it in the descriptor */
  938. if (crypto_pk_get_fingerprint(router->identity_pkey, fingerprint, 1)<0) {
  939. log_err(LD_BUG,"Error computing fingerprint");
  940. return -1;
  941. }
  942. /* PEM-encode the onion key */
  943. if (crypto_pk_write_public_key_to_string(router->onion_pkey,
  944. &onion_pkey,&onion_pkeylen)<0) {
  945. log_warn(LD_BUG,"write onion_pkey to string failed!");
  946. return -1;
  947. }
  948. /* PEM-encode the identity key key */
  949. if (crypto_pk_write_public_key_to_string(router->identity_pkey,
  950. &identity_pkey,&identity_pkeylen)<0) {
  951. log_warn(LD_BUG,"write identity_pkey to string failed!");
  952. tor_free(onion_pkey);
  953. return -1;
  954. }
  955. /* Encode the publication time. */
  956. format_iso_time(published, router->cache_info.published_on);
  957. /* How busy have we been? */
  958. bandwidth_usage = rep_hist_get_bandwidth_lines();
  959. if (router->declared_family && smartlist_len(router->declared_family)) {
  960. size_t n;
  961. char *s = smartlist_join_strings(router->declared_family, " ", 0, &n);
  962. n += strlen("family ") + 2; /* 1 for \n, 1 for \0. */
  963. family_line = tor_malloc(n);
  964. tor_snprintf(family_line, n, "family %s\n", s);
  965. tor_free(s);
  966. } else {
  967. family_line = tor_strdup("");
  968. }
  969. /* Generate the easy portion of the router descriptor. */
  970. result = tor_snprintf(s, maxlen,
  971. "router %s %s %d 0 %d\n"
  972. "platform %s\n"
  973. "published %s\n"
  974. "opt fingerprint %s\n"
  975. "uptime %ld\n"
  976. "bandwidth %d %d %d\n"
  977. "onion-key\n%s"
  978. "signing-key\n%s%s%s%s",
  979. router->nickname,
  980. router->address,
  981. router->or_port,
  982. decide_to_advertise_dirport(options, router),
  983. router->platform,
  984. published,
  985. fingerprint,
  986. stats_n_seconds_working,
  987. (int) router->bandwidthrate,
  988. (int) router->bandwidthburst,
  989. (int) router->bandwidthcapacity,
  990. onion_pkey, identity_pkey,
  991. family_line, bandwidth_usage,
  992. we_are_hibernating() ? "opt hibernating 1\n" : "");
  993. tor_free(family_line);
  994. tor_free(onion_pkey);
  995. tor_free(identity_pkey);
  996. tor_free(bandwidth_usage);
  997. if (result < 0)
  998. return -1;
  999. /* From now on, we use 'written' to remember the current length of 's'. */
  1000. written = result;
  1001. if (options->ContactInfo && strlen(options->ContactInfo)) {
  1002. result = tor_snprintf(s+written,maxlen-written, "contact %s\n",
  1003. options->ContactInfo);
  1004. if (result<0)
  1005. return -1;
  1006. written += result;
  1007. }
  1008. /* Write the exit policy to the end of 's'. */
  1009. for (tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  1010. /* Write: "accept 1.2.3.4" */
  1011. in.s_addr = htonl(tmpe->addr);
  1012. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  1013. result = tor_snprintf(s+written, maxlen-written, "%s %s",
  1014. tmpe->policy_type == ADDR_POLICY_ACCEPT ? "accept" : "reject",
  1015. tmpe->msk == 0 ? "*" : addrbuf);
  1016. if (result < 0)
  1017. return -1;
  1018. written += result;
  1019. if (tmpe->msk != 0xFFFFFFFFu && tmpe->msk != 0) {
  1020. int n_bits = addr_mask_get_bits(tmpe->msk);
  1021. if (n_bits >= 0) {
  1022. if (tor_snprintf(s+written, maxlen-written, "/%d", n_bits)<0)
  1023. return -1;
  1024. } else {
  1025. /* Write "/255.255.0.0" */
  1026. in.s_addr = htonl(tmpe->msk);
  1027. tor_inet_ntoa(&in, addrbuf, sizeof(addrbuf));
  1028. if (tor_snprintf(s+written, maxlen-written, "/%s", addrbuf)<0)
  1029. return -1;
  1030. }
  1031. written += strlen(s+written);
  1032. }
  1033. if (tmpe->prt_min <= 1 && tmpe->prt_max == 65535) {
  1034. /* There is no port set; write ":*" */
  1035. if (written+4 > maxlen)
  1036. return -1;
  1037. strlcat(s+written, ":*\n", maxlen-written);
  1038. written += 3;
  1039. } else if (tmpe->prt_min == tmpe->prt_max) {
  1040. /* There is only one port; write ":80". */
  1041. result = tor_snprintf(s+written, maxlen-written, ":%d\n", tmpe->prt_min);
  1042. if (result<0)
  1043. return -1;
  1044. written += result;
  1045. } else {
  1046. /* There is a range of ports; write ":79-80". */
  1047. result = tor_snprintf(s+written, maxlen-written, ":%d-%d\n",
  1048. tmpe->prt_min, tmpe->prt_max);
  1049. if (result<0)
  1050. return -1;
  1051. written += result;
  1052. }
  1053. if (tmpe->msk == 0 && tmpe->prt_min <= 1 && tmpe->prt_max == 65535)
  1054. /* This was a catch-all rule, so future rules are irrelevant. */
  1055. break;
  1056. } /* end for */
  1057. if (written+256 > maxlen) /* Not enough room for signature. */
  1058. return -1;
  1059. /* Sign the directory */
  1060. strlcat(s+written, "router-signature\n", maxlen-written);
  1061. written += strlen(s+written);
  1062. s[written] = '\0';
  1063. if (router_get_router_hash(s, digest) < 0)
  1064. return -1;
  1065. if (router_append_dirobj_signature(s+written,maxlen-written,
  1066. digest,ident_key)<0) {
  1067. log_warn(LD_BUG, "Couldn't sign router descriptor");
  1068. return -1;
  1069. }
  1070. written += strlen(s+written);
  1071. if (written+2 > maxlen)
  1072. return -1;
  1073. /* include a last '\n' */
  1074. s[written] = '\n';
  1075. s[written+1] = 0;
  1076. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  1077. cp = s_tmp = s_dup = tor_strdup(s);
  1078. ri_tmp = router_parse_entry_from_string(cp, NULL);
  1079. if (!ri_tmp) {
  1080. log_err(LD_BUG,
  1081. "We just generated a router descriptor we can't parse: <<%s>>",
  1082. s);
  1083. return -1;
  1084. }
  1085. tor_free(s_dup);
  1086. routerinfo_free(ri_tmp);
  1087. #endif
  1088. return written+1;
  1089. }
  1090. /** Return true iff <b>s</b> is a legally valid server nickname. */
  1091. int
  1092. is_legal_nickname(const char *s)
  1093. {
  1094. size_t len;
  1095. tor_assert(s);
  1096. len = strlen(s);
  1097. return len > 0 && len <= MAX_NICKNAME_LEN &&
  1098. strspn(s,LEGAL_NICKNAME_CHARACTERS) == len;
  1099. }
  1100. /** Return true iff <b>s</b> is a legally valid server nickname or
  1101. * hex-encoded identity-key digest. */
  1102. int
  1103. is_legal_nickname_or_hexdigest(const char *s)
  1104. {
  1105. size_t len;
  1106. tor_assert(s);
  1107. if (*s!='$')
  1108. return is_legal_nickname(s);
  1109. len = strlen(s);
  1110. return len == HEX_DIGEST_LEN+1 && strspn(s+1,HEX_CHARACTERS)==len-1;
  1111. }
  1112. /** Forget that we have issued any router-related warnings, so that we'll
  1113. * warn again if we see the same errors. */
  1114. void
  1115. router_reset_warnings(void)
  1116. {
  1117. if (warned_nonexistent_family) {
  1118. SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
  1119. smartlist_clear(warned_nonexistent_family);
  1120. }
  1121. }
  1122. /** Release all static resources held in router.c */
  1123. void
  1124. router_free_all(void)
  1125. {
  1126. if (onionkey)
  1127. crypto_free_pk_env(onionkey);
  1128. if (lastonionkey)
  1129. crypto_free_pk_env(lastonionkey);
  1130. if (identitykey)
  1131. crypto_free_pk_env(identitykey);
  1132. if (key_lock)
  1133. tor_mutex_free(key_lock);
  1134. if (desc_routerinfo)
  1135. routerinfo_free(desc_routerinfo);
  1136. if (warned_nonexistent_family) {
  1137. SMARTLIST_FOREACH(warned_nonexistent_family, char *, cp, tor_free(cp));
  1138. smartlist_free(warned_nonexistent_family);
  1139. }
  1140. }