statefile.c 21 KB

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