statefile.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2017, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file statefile.c
  8. *
  9. * \brief Handles parsing and encoding the persistent 'state' file that carries
  10. * miscellaneous persistent state between Tor invocations.
  11. *
  12. * This 'state' file is a typed key-value store that allows multiple
  13. * entries for the same key. It follows the same metaformat as described
  14. * in confparse.c, and uses the same code to read and write itself.
  15. *
  16. * The state file is most suitable for small values that don't change too
  17. * frequently. For values that become very large, we typically use a separate
  18. * file -- for example, see how we handle microdescriptors, by storing them in
  19. * a separate file with a journal.
  20. *
  21. * The current state is accessed via get_or_state(), which returns a singleton
  22. * or_state_t object. Functions that change it should call
  23. * or_state_mark_dirty() to ensure that it will get written to disk.
  24. *
  25. * The or_state_save() function additionally calls various functioens
  26. * throughout Tor that might want to flush more state to the the disk,
  27. * including some in rephist.c, entrynodes.c, circuitstats.c, hibernate.c.
  28. */
  29. #define STATEFILE_PRIVATE
  30. #include "or.h"
  31. #include "circuitstats.h"
  32. #include "config.h"
  33. #include "confparse.h"
  34. #include "connection.h"
  35. #include "entrynodes.h"
  36. #include "hibernate.h"
  37. #include "rephist.h"
  38. #include "router.h"
  39. #include "sandbox.h"
  40. #include "statefile.h"
  41. /** A list of state-file "abbreviations," for compatibility. */
  42. static config_abbrev_t state_abbrevs_[] = {
  43. { "AccountingBytesReadInterval", "AccountingBytesReadInInterval", 0, 0 },
  44. { "HelperNode", "EntryGuard", 0, 0 },
  45. { "HelperNodeDownSince", "EntryGuardDownSince", 0, 0 },
  46. { "HelperNodeUnlistedSince", "EntryGuardUnlistedSince", 0, 0 },
  47. { "EntryNode", "EntryGuard", 0, 0 },
  48. { "EntryNodeDownSince", "EntryGuardDownSince", 0, 0 },
  49. { "EntryNodeUnlistedSince", "EntryGuardUnlistedSince", 0, 0 },
  50. { NULL, NULL, 0, 0},
  51. };
  52. /*XXXX these next two are duplicates or near-duplicates from config.c */
  53. #define VAR(name,conftype,member,initvalue) \
  54. { name, CONFIG_TYPE_ ## conftype, STRUCT_OFFSET(or_state_t, member), \
  55. initvalue }
  56. /** As VAR, but the option name and member name are the same. */
  57. #define V(member,conftype,initvalue) \
  58. VAR(#member, conftype, member, initvalue)
  59. /** Array of "state" variables saved to the ~/.tor/state file. */
  60. static config_var_t state_vars_[] = {
  61. /* Remember to document these in state-contents.txt ! */
  62. V(AccountingBytesReadInInterval, MEMUNIT, NULL),
  63. V(AccountingBytesWrittenInInterval, MEMUNIT, NULL),
  64. V(AccountingExpectedUsage, MEMUNIT, NULL),
  65. V(AccountingIntervalStart, ISOTIME, NULL),
  66. V(AccountingSecondsActive, INTERVAL, NULL),
  67. V(AccountingSecondsToReachSoftLimit,INTERVAL, NULL),
  68. V(AccountingSoftLimitHitAt, ISOTIME, NULL),
  69. V(AccountingBytesAtSoftLimit, MEMUNIT, NULL),
  70. VAR("EntryGuard", LINELIST_S, EntryGuards, NULL),
  71. VAR("EntryGuardDownSince", LINELIST_S, EntryGuards, NULL),
  72. VAR("EntryGuardUnlistedSince", LINELIST_S, EntryGuards, NULL),
  73. VAR("EntryGuardAddedBy", LINELIST_S, EntryGuards, NULL),
  74. VAR("EntryGuardPathBias", LINELIST_S, EntryGuards, NULL),
  75. VAR("EntryGuardPathUseBias", LINELIST_S, EntryGuards, NULL),
  76. V(EntryGuards, LINELIST_V, NULL),
  77. VAR("TransportProxy", LINELIST_S, TransportProxies, NULL),
  78. V(TransportProxies, LINELIST_V, NULL),
  79. V(BWHistoryReadEnds, ISOTIME, NULL),
  80. V(BWHistoryReadInterval, UINT, "900"),
  81. V(BWHistoryReadValues, CSV, ""),
  82. V(BWHistoryReadMaxima, CSV, ""),
  83. V(BWHistoryWriteEnds, ISOTIME, NULL),
  84. V(BWHistoryWriteInterval, UINT, "900"),
  85. V(BWHistoryWriteValues, CSV, ""),
  86. V(BWHistoryWriteMaxima, CSV, ""),
  87. V(BWHistoryDirReadEnds, ISOTIME, NULL),
  88. V(BWHistoryDirReadInterval, UINT, "900"),
  89. V(BWHistoryDirReadValues, CSV, ""),
  90. V(BWHistoryDirReadMaxima, CSV, ""),
  91. V(BWHistoryDirWriteEnds, ISOTIME, NULL),
  92. V(BWHistoryDirWriteInterval, UINT, "900"),
  93. V(BWHistoryDirWriteValues, CSV, ""),
  94. V(BWHistoryDirWriteMaxima, CSV, ""),
  95. V(Guard, LINELIST, NULL),
  96. V(TorVersion, STRING, NULL),
  97. V(LastRotatedOnionKey, ISOTIME, NULL),
  98. V(LastWritten, ISOTIME, NULL),
  99. V(TotalBuildTimes, UINT, NULL),
  100. V(CircuitBuildAbandonedCount, UINT, "0"),
  101. VAR("CircuitBuildTimeBin", LINELIST_S, BuildtimeHistogram, NULL),
  102. VAR("BuildtimeHistogram", LINELIST_V, BuildtimeHistogram, NULL),
  103. { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL }
  104. };
  105. #undef VAR
  106. #undef V
  107. static int or_state_validate(or_state_t *state, char **msg);
  108. static int or_state_validate_cb(void *old_options, void *options,
  109. void *default_options,
  110. int from_setconf, char **msg);
  111. /** Magic value for or_state_t. */
  112. #define OR_STATE_MAGIC 0x57A73f57
  113. /** "Extra" variable in the state that receives lines we can't parse. This
  114. * lets us preserve options from versions of Tor newer than us. */
  115. static config_var_t state_extra_var = {
  116. "__extra", CONFIG_TYPE_LINELIST, STRUCT_OFFSET(or_state_t, ExtraLines), NULL
  117. };
  118. /** Configuration format for or_state_t. */
  119. static const config_format_t state_format = {
  120. sizeof(or_state_t),
  121. OR_STATE_MAGIC,
  122. STRUCT_OFFSET(or_state_t, magic_),
  123. state_abbrevs_,
  124. NULL,
  125. state_vars_,
  126. or_state_validate_cb,
  127. &state_extra_var,
  128. };
  129. /** Persistent serialized state. */
  130. static or_state_t *global_state = NULL;
  131. /** Return the persistent state struct for this Tor. */
  132. MOCK_IMPL(or_state_t *,
  133. get_or_state, (void))
  134. {
  135. tor_assert(global_state);
  136. return global_state;
  137. }
  138. /** Return true iff we have loaded the global state for this Tor */
  139. int
  140. or_state_loaded(void)
  141. {
  142. return global_state != NULL;
  143. }
  144. /** Return true if <b>line</b> is a valid state TransportProxy line.
  145. * Return false otherwise. */
  146. static int
  147. state_transport_line_is_valid(const char *line)
  148. {
  149. smartlist_t *items = NULL;
  150. char *addrport=NULL;
  151. tor_addr_t addr;
  152. uint16_t port = 0;
  153. int r;
  154. items = smartlist_new();
  155. smartlist_split_string(items, line, NULL,
  156. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  157. if (smartlist_len(items) != 2) {
  158. log_warn(LD_CONFIG, "state: Not enough arguments in TransportProxy line.");
  159. goto err;
  160. }
  161. addrport = smartlist_get(items, 1);
  162. if (tor_addr_port_lookup(addrport, &addr, &port) < 0) {
  163. log_warn(LD_CONFIG, "state: Could not parse addrport.");
  164. goto err;
  165. }
  166. if (!port) {
  167. log_warn(LD_CONFIG, "state: Transport line did not contain port.");
  168. goto err;
  169. }
  170. r = 1;
  171. goto done;
  172. err:
  173. r = 0;
  174. done:
  175. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  176. smartlist_free(items);
  177. return r;
  178. }
  179. /** Return 0 if all TransportProxy lines in <b>state</b> are well
  180. * formed. Otherwise, return -1. */
  181. static int
  182. validate_transports_in_state(or_state_t *state)
  183. {
  184. int broken = 0;
  185. config_line_t *line;
  186. for (line = state->TransportProxies ; line ; line = line->next) {
  187. tor_assert(!strcmp(line->key, "TransportProxy"));
  188. if (!state_transport_line_is_valid(line->value))
  189. broken = 1;
  190. }
  191. if (broken)
  192. log_warn(LD_CONFIG, "state: State file seems to be broken.");
  193. return 0;
  194. }
  195. static int
  196. or_state_validate_cb(void *old_state, void *state, void *default_state,
  197. int from_setconf, char **msg)
  198. {
  199. /* We don't use these; only options do. Still, we need to match that
  200. * signature. */
  201. (void) from_setconf;
  202. (void) default_state;
  203. (void) old_state;
  204. return or_state_validate(state, msg);
  205. }
  206. /** Return 0 if every setting in <b>state</b> is reasonable, and a
  207. * permissible transition from <b>old_state</b>. Else warn and return -1.
  208. * Should have no side effects, except for normalizing the contents of
  209. * <b>state</b>.
  210. */
  211. static int
  212. or_state_validate(or_state_t *state, char **msg)
  213. {
  214. if (entry_guards_parse_state(state, 0, msg)<0)
  215. return -1;
  216. if (validate_transports_in_state(state)<0)
  217. return -1;
  218. return 0;
  219. }
  220. /** Replace the current persistent state with <b>new_state</b> */
  221. static int
  222. or_state_set(or_state_t *new_state)
  223. {
  224. char *err = NULL;
  225. int ret = 0;
  226. tor_assert(new_state);
  227. config_free(&state_format, global_state);
  228. global_state = new_state;
  229. if (entry_guards_parse_state(global_state, 1, &err)<0) {
  230. log_warn(LD_GENERAL,"%s",err);
  231. tor_free(err);
  232. ret = -1;
  233. }
  234. if (rep_hist_load_state(global_state, &err)<0) {
  235. log_warn(LD_GENERAL,"Unparseable bandwidth history state: %s",err);
  236. tor_free(err);
  237. ret = -1;
  238. }
  239. if (circuit_build_times_parse_state(
  240. get_circuit_build_times_mutable(),global_state) < 0) {
  241. ret = -1;
  242. }
  243. return ret;
  244. }
  245. /**
  246. * Save a broken state file to a backup location.
  247. */
  248. static void
  249. or_state_save_broken(char *fname)
  250. {
  251. int i, res;
  252. file_status_t status;
  253. char *fname2 = NULL;
  254. for (i = 0; i < 100; ++i) {
  255. tor_asprintf(&fname2, "%s.%d", fname, i);
  256. status = file_status(fname2);
  257. if (status == FN_NOENT)
  258. break;
  259. tor_free(fname2);
  260. }
  261. if (i == 100) {
  262. log_warn(LD_BUG, "Unable to parse state in \"%s\"; too many saved bad "
  263. "state files to move aside. Discarding the old state file.",
  264. fname);
  265. res = unlink(fname);
  266. if (res != 0) {
  267. log_warn(LD_FS,
  268. "Also couldn't discard old state file \"%s\" because "
  269. "unlink() failed: %s",
  270. fname, strerror(errno));
  271. }
  272. } else {
  273. log_warn(LD_BUG, "Unable to parse state in \"%s\". Moving it aside "
  274. "to \"%s\". This could be a bug in Tor; please tell "
  275. "the developers.", fname, fname2);
  276. if (tor_rename(fname, fname2) < 0) {//XXXX sandbox prohibits
  277. log_warn(LD_BUG, "Weirdly, I couldn't even move the state aside. The "
  278. "OS gave an error of %s", strerror(errno));
  279. }
  280. }
  281. tor_free(fname2);
  282. }
  283. STATIC or_state_t *
  284. or_state_new(void)
  285. {
  286. or_state_t *new_state = tor_malloc_zero(sizeof(or_state_t));
  287. new_state->magic_ = OR_STATE_MAGIC;
  288. config_init(&state_format, new_state);
  289. return new_state;
  290. }
  291. /** Reload the persistent state from disk, generating a new state as needed.
  292. * Return 0 on success, less than 0 on failure.
  293. */
  294. int
  295. or_state_load(void)
  296. {
  297. or_state_t *new_state = NULL;
  298. char *contents = NULL, *fname;
  299. char *errmsg = NULL;
  300. int r = -1, badstate = 0;
  301. fname = get_datadir_fname("state");
  302. switch (file_status(fname)) {
  303. case FN_FILE:
  304. if (!(contents = read_file_to_str(fname, 0, NULL))) {
  305. log_warn(LD_FS, "Unable to read state file \"%s\"", fname);
  306. goto done;
  307. }
  308. break;
  309. /* treat empty state files as if the file doesn't exist, and generate
  310. * a new state file, overwriting the empty file in or_state_save() */
  311. case FN_NOENT:
  312. case FN_EMPTY:
  313. break;
  314. case FN_ERROR:
  315. case FN_DIR:
  316. default:
  317. log_warn(LD_GENERAL,"State file \"%s\" is not a file? Failing.", fname);
  318. goto done;
  319. }
  320. new_state = or_state_new();
  321. if (contents) {
  322. config_line_t *lines=NULL;
  323. int assign_retval;
  324. if (config_get_lines(contents, &lines, 0)<0)
  325. goto done;
  326. assign_retval = config_assign(&state_format, new_state,
  327. lines, 0, &errmsg);
  328. config_free_lines(lines);
  329. if (assign_retval<0)
  330. badstate = 1;
  331. if (errmsg) {
  332. log_warn(LD_GENERAL, "%s", errmsg);
  333. tor_free(errmsg);
  334. }
  335. }
  336. if (!badstate && or_state_validate(new_state, &errmsg) < 0)
  337. badstate = 1;
  338. if (errmsg) {
  339. log_warn(LD_GENERAL, "%s", errmsg);
  340. tor_free(errmsg);
  341. }
  342. if (badstate && !contents) {
  343. log_warn(LD_BUG, "Uh oh. We couldn't even validate our own default state."
  344. " This is a bug in Tor.");
  345. goto done;
  346. } else if (badstate && contents) {
  347. or_state_save_broken(fname);
  348. tor_free(contents);
  349. config_free(&state_format, new_state);
  350. new_state = or_state_new();
  351. } else if (contents) {
  352. log_info(LD_GENERAL, "Loaded state from \"%s\"", fname);
  353. /* Warn the user if their clock has been set backwards,
  354. * they could be tricked into using old consensuses */
  355. time_t apparent_skew = new_state->LastWritten - time(NULL);
  356. if (apparent_skew > 0)
  357. clock_skew_warning(NULL, (long)apparent_skew, 1, LD_GENERAL,
  358. "local state file", fname);
  359. } else {
  360. log_info(LD_GENERAL, "Initialized state");
  361. }
  362. if (or_state_set(new_state) == -1) {
  363. or_state_save_broken(fname);
  364. }
  365. new_state = NULL;
  366. if (!contents) {
  367. global_state->next_write = 0;
  368. or_state_save(time(NULL));
  369. }
  370. r = 0;
  371. done:
  372. tor_free(fname);
  373. tor_free(contents);
  374. if (new_state)
  375. config_free(&state_format, new_state);
  376. return r;
  377. }
  378. /** Did the last time we tried to write the state file fail? If so, we
  379. * should consider disabling such features as preemptive circuit generation
  380. * to compute circuit-build-time. */
  381. static int last_state_file_write_failed = 0;
  382. /** Return whether the state file failed to write last time we tried. */
  383. int
  384. did_last_state_file_write_fail(void)
  385. {
  386. return last_state_file_write_failed;
  387. }
  388. /** If writing the state to disk fails, try again after this many seconds. */
  389. #define STATE_WRITE_RETRY_INTERVAL 3600
  390. /** If we're a relay, how often should we checkpoint our state file even
  391. * if nothing else dirties it? This will checkpoint ongoing stats like
  392. * bandwidth used, per-country user stats, etc. */
  393. #define STATE_RELAY_CHECKPOINT_INTERVAL (12*60*60)
  394. /** Write the persistent state to disk. Return 0 for success, <0 on failure. */
  395. int
  396. or_state_save(time_t now)
  397. {
  398. char *state, *contents;
  399. char tbuf[ISO_TIME_LEN+1];
  400. char *fname;
  401. tor_assert(global_state);
  402. if (global_state->next_write > now)
  403. return 0;
  404. /* Call everything else that might dirty the state even more, in order
  405. * to avoid redundant writes. */
  406. entry_guards_update_state(global_state);
  407. rep_hist_update_state(global_state);
  408. circuit_build_times_update_state(get_circuit_build_times(), global_state);
  409. if (accounting_is_enabled(get_options()))
  410. accounting_run_housekeeping(now);
  411. global_state->LastWritten = now;
  412. tor_free(global_state->TorVersion);
  413. tor_asprintf(&global_state->TorVersion, "Tor %s", get_version());
  414. state = config_dump(&state_format, NULL, global_state, 1, 0);
  415. format_local_iso_time(tbuf, now);
  416. tor_asprintf(&contents,
  417. "# Tor state file last generated on %s local time\n"
  418. "# Other times below are in UTC\n"
  419. "# You *do not* need to edit this file.\n\n%s",
  420. tbuf, state);
  421. tor_free(state);
  422. fname = get_datadir_fname("state");
  423. if (write_str_to_file(fname, contents, 0)<0) {
  424. log_warn(LD_FS, "Unable to write state to file \"%s\"; "
  425. "will try again later", fname);
  426. last_state_file_write_failed = 1;
  427. tor_free(fname);
  428. tor_free(contents);
  429. /* Try again after STATE_WRITE_RETRY_INTERVAL (or sooner, if the state
  430. * changes sooner). */
  431. global_state->next_write = now + STATE_WRITE_RETRY_INTERVAL;
  432. return -1;
  433. }
  434. last_state_file_write_failed = 0;
  435. log_info(LD_GENERAL, "Saved state to \"%s\"", fname);
  436. tor_free(fname);
  437. tor_free(contents);
  438. if (server_mode(get_options()))
  439. global_state->next_write = now + STATE_RELAY_CHECKPOINT_INTERVAL;
  440. else
  441. global_state->next_write = TIME_MAX;
  442. return 0;
  443. }
  444. /** Return the config line for transport <b>transport</b> in the current state.
  445. * Return NULL if there is no config line for <b>transport</b>. */
  446. STATIC config_line_t *
  447. get_transport_in_state_by_name(const char *transport)
  448. {
  449. or_state_t *or_state = get_or_state();
  450. config_line_t *line;
  451. config_line_t *ret = NULL;
  452. smartlist_t *items = NULL;
  453. for (line = or_state->TransportProxies ; line ; line = line->next) {
  454. tor_assert(!strcmp(line->key, "TransportProxy"));
  455. items = smartlist_new();
  456. smartlist_split_string(items, line->value, NULL,
  457. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  458. if (smartlist_len(items) != 2) /* broken state */
  459. goto done;
  460. if (!strcmp(smartlist_get(items, 0), transport)) {
  461. ret = line;
  462. goto done;
  463. }
  464. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  465. smartlist_free(items);
  466. items = NULL;
  467. }
  468. done:
  469. if (items) {
  470. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  471. smartlist_free(items);
  472. }
  473. return ret;
  474. }
  475. /** Return string containing the address:port part of the
  476. * TransportProxy <b>line</b> for transport <b>transport</b>.
  477. * If the line is corrupted, return NULL. */
  478. static const char *
  479. get_transport_bindaddr(const char *line, const char *transport)
  480. {
  481. char *line_tmp = NULL;
  482. if (strlen(line) < strlen(transport) + 2) {
  483. goto broken_state;
  484. } else {
  485. /* line should start with the name of the transport and a space.
  486. (for example, "obfs2 127.0.0.1:47245") */
  487. tor_asprintf(&line_tmp, "%s ", transport);
  488. if (strcmpstart(line, line_tmp))
  489. goto broken_state;
  490. tor_free(line_tmp);
  491. return (line+strlen(transport)+1);
  492. }
  493. broken_state:
  494. tor_free(line_tmp);
  495. return NULL;
  496. }
  497. /** Return a string containing the address:port that a proxy transport
  498. * should bind on. The string is stored on the heap and must be freed
  499. * by the caller of this function. */
  500. char *
  501. get_stored_bindaddr_for_server_transport(const char *transport)
  502. {
  503. char *default_addrport = NULL;
  504. const char *stored_bindaddr = NULL;
  505. config_line_t *line = NULL;
  506. {
  507. /* See if the user explicitly asked for a specific listening
  508. address for this transport. */
  509. char *conf_bindaddr = get_transport_bindaddr_from_config(transport);
  510. if (conf_bindaddr)
  511. return conf_bindaddr;
  512. }
  513. line = get_transport_in_state_by_name(transport);
  514. if (!line) /* Found no references in state for this transport. */
  515. goto no_bindaddr_found;
  516. stored_bindaddr = get_transport_bindaddr(line->value, transport);
  517. if (stored_bindaddr) /* found stored bindaddr in state file. */
  518. return tor_strdup(stored_bindaddr);
  519. no_bindaddr_found:
  520. /** If we didn't find references for this pluggable transport in the
  521. state file, we should instruct the pluggable transport proxy to
  522. listen on INADDR_ANY on a random ephemeral port. */
  523. tor_asprintf(&default_addrport, "%s:%s", fmt_addr32(INADDR_ANY), "0");
  524. return default_addrport;
  525. }
  526. /** Save <b>transport</b> listening on <b>addr</b>:<b>port</b> to
  527. state */
  528. void
  529. save_transport_to_state(const char *transport,
  530. const tor_addr_t *addr, uint16_t port)
  531. {
  532. or_state_t *state = get_or_state();
  533. char *transport_addrport=NULL;
  534. /** find where to write on the state */
  535. config_line_t **next, *line;
  536. /* see if this transport is already stored in state */
  537. config_line_t *transport_line =
  538. get_transport_in_state_by_name(transport);
  539. if (transport_line) { /* if transport already exists in state... */
  540. const char *prev_bindaddr = /* get its addrport... */
  541. get_transport_bindaddr(transport_line->value, transport);
  542. transport_addrport = tor_strdup(fmt_addrport(addr, port));
  543. /* if transport in state has the same address as this one, life is good */
  544. if (!strcmp(prev_bindaddr, transport_addrport)) {
  545. log_info(LD_CONFIG, "Transport seems to have spawned on its usual "
  546. "address:port.");
  547. goto done;
  548. } else { /* if addrport in state is different than the one we got */
  549. log_info(LD_CONFIG, "Transport seems to have spawned on different "
  550. "address:port. Let's update the state file with the new "
  551. "address:port");
  552. tor_free(transport_line->value); /* free the old line */
  553. /* replace old addrport line with new line */
  554. tor_asprintf(&transport_line->value, "%s %s", transport,
  555. fmt_addrport(addr, port));
  556. }
  557. } else { /* never seen this one before; save it in state for next time */
  558. log_info(LD_CONFIG, "It's the first time we see this transport. "
  559. "Let's save its address:port");
  560. next = &state->TransportProxies;
  561. /* find the last TransportProxy line in the state and point 'next'
  562. right after it */
  563. line = state->TransportProxies;
  564. while (line) {
  565. next = &(line->next);
  566. line = line->next;
  567. }
  568. /* allocate space for the new line and fill it in */
  569. *next = line = tor_malloc_zero(sizeof(config_line_t));
  570. line->key = tor_strdup("TransportProxy");
  571. tor_asprintf(&line->value, "%s %s", transport, fmt_addrport(addr, port));
  572. next = &(line->next);
  573. }
  574. if (!get_options()->AvoidDiskWrites)
  575. or_state_mark_dirty(state, 0);
  576. done:
  577. tor_free(transport_addrport);
  578. }
  579. STATIC void
  580. or_state_free(or_state_t *state)
  581. {
  582. if (!state)
  583. return;
  584. config_free(&state_format, state);
  585. }
  586. void
  587. or_state_free_all(void)
  588. {
  589. or_state_free(global_state);
  590. global_state = NULL;
  591. }