statefile.c 20 KB

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