statefile.c 20 KB

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