statefile.c 19 KB

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