transports.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /* Copyright (c) 2011, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file transports.c
  5. * \brief Pluggable Transports related code.
  6. **/
  7. #define PT_PRIVATE
  8. #include "or.h"
  9. #include "config.h"
  10. #include "circuitbuild.h"
  11. #include "transports.h"
  12. #include "util.h"
  13. #ifdef MS_WINDOWS
  14. static void set_managed_proxy_environment(LPVOID *envp,
  15. const managed_proxy_t *mp);
  16. #else
  17. static int set_managed_proxy_environment(char ***envp,
  18. const managed_proxy_t *mp);
  19. #endif
  20. static INLINE int proxy_configuration_finished(const managed_proxy_t *mp);
  21. static void managed_proxy_destroy(managed_proxy_t *mp,
  22. int also_terminate_process);
  23. static void handle_finished_proxy(managed_proxy_t *mp);
  24. static void configure_proxy(managed_proxy_t *mp);
  25. static void parse_method_error(const char *line, int is_server_method);
  26. #define parse_server_method_error(l) parse_method_error(l, 1)
  27. #define parse_client_method_error(l) parse_method_error(l, 0)
  28. static INLINE void free_execve_args(char **arg);
  29. /** Managed proxy protocol strings */
  30. #define PROTO_ENV_ERROR "ENV-ERROR"
  31. #define PROTO_NEG_SUCCESS "VERSION"
  32. #define PROTO_NEG_FAIL "VERSION-ERROR no-version"
  33. #define PROTO_CMETHOD "CMETHOD"
  34. #define PROTO_SMETHOD "SMETHOD"
  35. #define PROTO_CMETHOD_ERROR "CMETHOD-ERROR"
  36. #define PROTO_SMETHOD_ERROR "SMETHOD-ERROR"
  37. #define PROTO_CMETHODS_DONE "CMETHODS DONE"
  38. #define PROTO_SMETHODS_DONE "SMETHODS DONE"
  39. /* The smallest valid managed proxy protocol line that can
  40. appear. It's the size of "VERSION 1" */
  41. #define SMALLEST_MANAGED_LINE_SIZE 9
  42. /** Number of environment variables for managed proxy clients/servers. */
  43. #define ENVIRON_SIZE_CLIENT 5
  44. #define ENVIRON_SIZE_SERVER 8
  45. /** The first and only supported - at the moment - configuration
  46. protocol version. */
  47. #define PROTO_VERSION_ONE 1
  48. /** List of unconfigured managed proxies. */
  49. static smartlist_t *managed_proxy_list = NULL;
  50. /** Number of still unconfigured proxies. */
  51. static int unconfigured_proxies_n = 0;
  52. /** "The main idea is:"
  53. Each managed proxy is represented by a 'managed_proxy_t'.
  54. Each managed proxy can support multiple transports.
  55. Each managed proxy gets configured through a multistep process.
  56. 'managed_proxy_list' contains all the managed proxies this tor
  57. instance is supporting.
  58. In the 'managed_proxy_list' there are 'unconfigured_proxies_n'
  59. managed proxies that are still unconfigured.
  60. In every run_scheduled_event() tick, we attempt to launch and then
  61. configure the unconfiged managed proxies, using the configuration
  62. protocol defined in the 180_pluggable_transport.txt proposal. A
  63. managed proxy might need several ticks to get fully configured.
  64. When a managed proxy is fully configured, we register all its
  65. transports to the circuitbuild.c subsystem. At that point the
  66. transports are owned by the circuitbuild.c subsystem.
  67. When a managed proxy fails to follow the 180 configuration
  68. protocol, it gets marked as broken and gets destroyed.
  69. "In a little more technical detail:"
  70. While we are serially parsing torrc, we store all the transports
  71. that a proxy should spawn in its 'transports_to_launch' element.
  72. When we finish reading the torrc, we spawn the managed proxy and
  73. expect {S,C}METHOD lines from its output. We add transports
  74. described by METHOD lines to its 'transports' element, as
  75. 'transport_t' structs.
  76. When the managed proxy stops spitting METHOD lines (signified by a
  77. '{S,C}METHODS DONE' message) we register all the transports
  78. collected to the circuitbuild.c subsystem. At this point, the
  79. 'transport_t's can be transformed into dangling pointers at any
  80. point by the circuitbuild.c subsystem, and so we replace all
  81. 'transport_t's with strings describing the transport names. We
  82. can still go from a transport name to a 'transport_t' using the
  83. fact that transport names uniquely identify 'transport_t's.
  84. "In even more technical detail I shall describe what happens when
  85. the SIGHUP bell tolls:"
  86. We immediately destroy all unconfigured proxies (We shouldn't have
  87. unconfigured proxies in the first place, except when SIGHUP rings
  88. immediately after tor is launched.).
  89. We mark all managed proxies and transports to signify that they
  90. must be removed if they don't contribute by the new torrc
  91. (marked_for_removal).
  92. We also mark all managed proxies to signify that they might need
  93. to be restarted so that they end up supporting all the transports
  94. the new torrc wants them to support (got_hup).
  95. We also clear their 'transports_to_launch' list so that we can put
  96. there the transports we need to launch according to the new torrc.
  97. We then start parsing torrc again.
  98. Everytime we encounter a transport line using a known pre-SIGHUP
  99. managed proxy, we cleanse that proxy from the removal mark.
  100. We also mark it as unconfigured so that on the next scheduled
  101. events tick, we investigate whether we need to restart the proxy
  102. so that it also spawns the new transports.
  103. If the post-SIGHUP 'transports_to_launch' list is identical to the
  104. pre-SIGHUP one, it means that no changes were introduced to this
  105. proxy during the SIGHUP and no restart has to take place.
  106. During the post-SIGHUP torrc parsing, we unmark all transports
  107. spawned by managed proxies that we find in our torrc.
  108. We do that so that if we don't need to restart a managed proxy, we
  109. can continue using its old transports normally.
  110. If we end up restarting the proxy, we destroy and unregister all
  111. old transports from the circuitbuild.c subsystem.
  112. */
  113. /** Return true if there are still unconfigured managed proxies. */
  114. int
  115. pt_proxies_configuration_pending(void)
  116. {
  117. return !! unconfigured_proxies_n;
  118. }
  119. /** Return true if <b>mp</b> has the same argv as <b>proxy_argv</b> */
  120. static int
  121. managed_proxy_has_argv(const managed_proxy_t *mp, char **proxy_argv)
  122. {
  123. char **tmp1=proxy_argv;
  124. char **tmp2=mp->argv;
  125. tor_assert(tmp1);
  126. tor_assert(tmp2);
  127. while (*tmp1 && *tmp2) {
  128. if (strcmp(*tmp1++, *tmp2++))
  129. return 0;
  130. }
  131. if (!*tmp1 && !*tmp2)
  132. return 1;
  133. return 0;
  134. }
  135. /** Return a managed proxy with the same argv as <b>proxy_argv</b>.
  136. * If no such managed proxy exists, return NULL. */
  137. static managed_proxy_t *
  138. get_managed_proxy_by_argv_and_type(char **proxy_argv, int is_server)
  139. {
  140. if (!managed_proxy_list)
  141. return NULL;
  142. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  143. if (managed_proxy_has_argv(mp, proxy_argv) &&
  144. mp->is_server == is_server)
  145. return mp;
  146. } SMARTLIST_FOREACH_END(mp);
  147. return NULL;
  148. }
  149. /** Add <b>transport</b> to managed proxy <b>mp</b>. */
  150. static void
  151. add_transport_to_proxy(const char *transport, managed_proxy_t *mp)
  152. {
  153. tor_assert(mp->transports_to_launch);
  154. if (!smartlist_string_isin(mp->transports_to_launch, transport))
  155. smartlist_add(mp->transports_to_launch, tor_strdup(transport));
  156. }
  157. /** Called when a SIGHUP occurs. Returns true if managed proxy
  158. * <b>mp</b> needs to be restarted after the SIGHUP, based on the new
  159. * torrc. */
  160. static int
  161. proxy_needs_restart(const managed_proxy_t *mp)
  162. {
  163. /* mp->transport_to_launch is populated with the names of the
  164. transports that must be launched *after* the SIGHUP.
  165. mp->transports is populated with the names of the transports that
  166. were launched *before* the SIGHUP.
  167. If the two lists contain the same strings, we don't need to
  168. restart the proxy, since it already does what we want. */
  169. tor_assert(smartlist_len(mp->transports_to_launch) > 0);
  170. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  171. if (smartlist_len(mp->transports_to_launch) != smartlist_len(mp->transports))
  172. goto needs_restart;
  173. SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t_t_l) {
  174. if (!smartlist_string_isin(mp->transports, t_t_l))
  175. goto needs_restart;
  176. } SMARTLIST_FOREACH_END(t_t_l);
  177. return 0;
  178. needs_restart:
  179. return 1;
  180. }
  181. /** Managed proxy <b>mp</b> must be restarted. Do all the necessary
  182. * preparations and then flag its state so that it will be relaunched
  183. * in the next tick. */
  184. static void
  185. proxy_prepare_for_restart(managed_proxy_t *mp)
  186. {
  187. transport_t *t_tmp = NULL;
  188. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  189. /* destroy the process handle and terminate the process. */
  190. tor_process_handle_destroy(mp->process_handle, 1);
  191. mp->process_handle = NULL;
  192. /* destroy all its old transports. we no longer use them. */
  193. SMARTLIST_FOREACH_BEGIN(mp->transports, const char *, t_name) {
  194. t_tmp = transport_get_by_name(t_name);
  195. if (t_tmp)
  196. t_tmp->marked_for_removal = 1;
  197. } SMARTLIST_FOREACH_END(t_name);
  198. sweep_transport_list();
  199. /* free the transport names in mp->transports */
  200. SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));
  201. smartlist_clear(mp->transports);
  202. /* flag it as an infant proxy so that it gets launched on next tick */
  203. mp->conf_state = PT_PROTO_INFANT;
  204. }
  205. /** Launch managed proxy <b>mp</b>. */
  206. static int
  207. launch_managed_proxy(managed_proxy_t *mp)
  208. {
  209. int retval;
  210. #ifdef MS_WINDOWS
  211. LPVOID envp=NULL;
  212. set_managed_proxy_environment(&envp, mp);
  213. tor_assert(envp);
  214. /* Passing NULL as lpApplicationName makes Windows search for the .exe */
  215. retval = tor_spawn_background(NULL, (const char **)mp->argv, envp,
  216. &mp->process_handle);
  217. tor_free(envp);
  218. #else
  219. char **envp=NULL;
  220. /* prepare the environment variables for the managed proxy */
  221. if (set_managed_proxy_environment(&envp, mp) < 0) {
  222. log_warn(LD_GENERAL, "Could not setup the environment of "
  223. "the managed proxy at '%s'.", mp->argv[0]);
  224. free_execve_args(envp);
  225. return -1;
  226. }
  227. retval = tor_spawn_background(mp->argv[0], (const char **)mp->argv,
  228. (const char **)envp, &mp->process_handle);
  229. /* free the memory allocated by set_managed_proxy_environment(). */
  230. free_execve_args(envp);
  231. #endif
  232. if (retval == PROCESS_STATUS_ERROR) {
  233. log_warn(LD_GENERAL, "Managed proxy at '%s' failed at launch.",
  234. mp->argv[0]);
  235. return -1;
  236. }
  237. log_info(LD_CONFIG, "Managed proxy at '%s' has spawned with PID '%d'.",
  238. mp->argv[0], tor_process_get_pid(mp->process_handle));
  239. mp->conf_state = PT_PROTO_LAUNCHED;
  240. return 0;
  241. }
  242. /** Check if any of the managed proxies we are currently trying to
  243. * configure have anything new to say. This is called from
  244. * run_scheduled_events(). */
  245. void
  246. pt_configure_remaining_proxies(void)
  247. {
  248. log_debug(LD_CONFIG, "Configuring remaining managed proxies (%d)!",
  249. unconfigured_proxies_n);
  250. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  251. tor_assert(mp->conf_state != PT_PROTO_BROKEN ||
  252. mp->conf_state != PT_PROTO_FAILED_LAUNCH);
  253. if (mp->got_hup) {
  254. mp->got_hup = 0;
  255. /* This proxy is marked by a SIGHUP. Check whether we need to
  256. restart it. */
  257. if (proxy_needs_restart(mp)) {
  258. log_info(LD_GENERAL, "Preparing managed proxy for restart.");
  259. proxy_prepare_for_restart(mp);
  260. continue;
  261. } else { /* it doesn't need to be restarted. */
  262. log_info(LD_GENERAL, "Nothing changed for managed proxy after HUP: "
  263. "not restarting.");
  264. unconfigured_proxies_n--;
  265. tor_assert(unconfigured_proxies_n >= 0);
  266. }
  267. continue;
  268. }
  269. /* If the proxy is not fully configured, try to configure it
  270. futher. */
  271. if (!proxy_configuration_finished(mp))
  272. configure_proxy(mp);
  273. } SMARTLIST_FOREACH_END(mp);
  274. }
  275. #ifdef MS_WINDOWS
  276. /** Attempt to continue configuring managed proxy <b>mp</b>. */
  277. static void
  278. configure_proxy(managed_proxy_t *mp)
  279. {
  280. int pos;
  281. char stdout_buf[200];
  282. smartlist_t *lines = NULL;
  283. /* if we haven't launched the proxy yet, do it now */
  284. if (mp->conf_state == PT_PROTO_INFANT) {
  285. if (launch_managed_proxy(mp) < 0) { /* launch fail */
  286. mp->conf_state = PT_PROTO_FAILED_LAUNCH;
  287. handle_finished_proxy(mp);
  288. }
  289. return;
  290. }
  291. tor_assert(mp->conf_state != PT_PROTO_INFANT);
  292. tor_assert(mp->process_handle);
  293. pos = tor_read_all_handle(tor_process_get_stdout_pipe(mp->process_handle),
  294. stdout_buf, sizeof(stdout_buf) - 1, NULL);
  295. if (pos < 0) {
  296. log_notice(LD_GENERAL, "Failed to read data from managed proxy");
  297. mp->conf_state = PT_PROTO_BROKEN;
  298. goto done;
  299. }
  300. if (pos == 0) /* proxy has nothing interesting to say. */
  301. return;
  302. /* End with a null even if there isn't a \r\n at the end */
  303. /* TODO: What if this is a partial line? */
  304. stdout_buf[pos] = '\0';
  305. /* Split up the buffer */
  306. lines = smartlist_create();
  307. tor_split_lines(lines, stdout_buf, pos);
  308. /* Handle lines. */
  309. SMARTLIST_FOREACH_BEGIN(lines, const char *, line) {
  310. handle_proxy_line(line, mp);
  311. if (proxy_configuration_finished(mp))
  312. goto done;
  313. } SMARTLIST_FOREACH_END(line);
  314. done:
  315. /* if the proxy finished configuring, exit the loop. */
  316. if (proxy_configuration_finished(mp))
  317. handle_finished_proxy(mp);
  318. if (lines)
  319. smartlist_free(lines);
  320. }
  321. #else /* MS_WINDOWS */
  322. /** Attempt to continue configuring managed proxy <b>mp</b>. */
  323. static void
  324. configure_proxy(managed_proxy_t *mp)
  325. {
  326. enum stream_status r;
  327. char stdout_buf[200];
  328. /* if we haven't launched the proxy yet, do it now */
  329. if (mp->conf_state == PT_PROTO_INFANT) {
  330. if (launch_managed_proxy(mp) < 0) { /* launch fail */
  331. mp->conf_state = PT_PROTO_FAILED_LAUNCH;
  332. handle_finished_proxy(mp);
  333. }
  334. return;
  335. }
  336. tor_assert(mp->conf_state != PT_PROTO_INFANT);
  337. tor_assert(mp->process_handle);
  338. while (1) {
  339. r = get_string_from_pipe(tor_process_get_stdout_pipe(mp->process_handle),
  340. stdout_buf, sizeof(stdout_buf) - 1);
  341. if (r == IO_STREAM_OKAY) { /* got a line; handle it! */
  342. handle_proxy_line((const char *)stdout_buf, mp);
  343. } else if (r == IO_STREAM_EAGAIN) { /* check back later */
  344. return;
  345. } else if (r == IO_STREAM_CLOSED || r == IO_STREAM_TERM) { /* snap! */
  346. log_notice(LD_GENERAL, "Managed proxy stream closed. "
  347. "Most probably application stopped running");
  348. mp->conf_state = PT_PROTO_BROKEN;
  349. } else { /* unknown stream status */
  350. log_notice(LD_GENERAL, "Unknown stream status while configuring proxy.");
  351. }
  352. /* if the proxy finished configuring, exit the loop. */
  353. if (proxy_configuration_finished(mp)) {
  354. handle_finished_proxy(mp);
  355. return;
  356. }
  357. }
  358. }
  359. #endif /* MS_WINDOWS */
  360. /** Register server managed proxy <b>mp</b> transports to state */
  361. static void
  362. register_server_proxy(managed_proxy_t *mp)
  363. {
  364. /* After we register this proxy's transports, we switch its
  365. mp->transports to a list containing strings of its transport
  366. names. (See transports.h) */
  367. smartlist_t *sm_tmp = smartlist_create();
  368. tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
  369. SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
  370. save_transport_to_state(t->name, &t->addr, t->port);
  371. /* LOG_WARN so that the bridge operator can easily find the
  372. transport's port in the log file and send it to the users. */
  373. log_warn(LD_GENERAL, "Registered server transport '%s' at '%s:%d'",
  374. t->name, fmt_addr(&t->addr), (int)t->port);
  375. smartlist_add(sm_tmp, tor_strdup(t->name));
  376. } SMARTLIST_FOREACH_END(t);
  377. /* Since server proxies don't register their transports in the
  378. circuitbuild.c subsystem, it's our duty to free them when we
  379. switch mp->transports to strings. */
  380. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  381. smartlist_free(mp->transports);
  382. mp->transports = sm_tmp;
  383. }
  384. /** Register all the transports supported by client managed proxy
  385. * <b>mp</b> to the bridge subsystem. */
  386. static void
  387. register_client_proxy(managed_proxy_t *mp)
  388. {
  389. int r;
  390. /* After we register this proxy's transports, we switch its
  391. mp->transports to a list containing strings of its transport
  392. names. (See transports.h) */
  393. smartlist_t *sm_tmp = smartlist_create();
  394. tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
  395. SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
  396. r = transport_add(t);
  397. switch (r) {
  398. case -1:
  399. log_notice(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
  400. transport_free(t);
  401. break;
  402. case 0:
  403. log_info(LD_GENERAL, "Succesfully registered transport %s", t->name);
  404. smartlist_add(sm_tmp, tor_strdup(t->name));
  405. break;
  406. case 1:
  407. log_info(LD_GENERAL, "Succesfully registered transport %s", t->name);
  408. smartlist_add(sm_tmp, tor_strdup(t->name));
  409. transport_free(t);
  410. break;
  411. }
  412. } SMARTLIST_FOREACH_END(t);
  413. smartlist_free(mp->transports);
  414. mp->transports = sm_tmp;
  415. }
  416. /** Register the transports of managed proxy <b>mp</b>. */
  417. static INLINE void
  418. register_proxy(managed_proxy_t *mp)
  419. {
  420. if (mp->is_server)
  421. register_server_proxy(mp);
  422. else
  423. register_client_proxy(mp);
  424. }
  425. /** Free memory allocated by managed proxy <b>mp</b>. */
  426. static void
  427. managed_proxy_destroy(managed_proxy_t *mp,
  428. int also_terminate_process)
  429. {
  430. if (mp->conf_state != PT_PROTO_COMPLETED)
  431. SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
  432. else
  433. SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));
  434. /* free the transports smartlist */
  435. smartlist_free(mp->transports);
  436. /* free the transports_to_launch smartlist */
  437. SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  438. smartlist_free(mp->transports_to_launch);
  439. /* remove it from the list of managed proxies */
  440. smartlist_remove(managed_proxy_list, mp);
  441. /* free the argv */
  442. free_execve_args(mp->argv);
  443. tor_process_handle_destroy(mp->process_handle, also_terminate_process);
  444. mp->process_handle = NULL;
  445. tor_free(mp);
  446. }
  447. /** Handle a configured or broken managed proxy <b>mp</b>. */
  448. static void
  449. handle_finished_proxy(managed_proxy_t *mp)
  450. {
  451. switch (mp->conf_state) {
  452. case PT_PROTO_BROKEN: /* if broken: */
  453. managed_proxy_destroy(mp, 1); /* annihilate it. */
  454. break;
  455. case PT_PROTO_FAILED_LAUNCH: /* if it failed before launching: */
  456. managed_proxy_destroy(mp, 0); /* destroy it but don't terminate */
  457. break;
  458. case PT_PROTO_CONFIGURED: /* if configured correctly: */
  459. register_proxy(mp); /* register its transports */
  460. mp->conf_state = PT_PROTO_COMPLETED; /* and mark it as completed. */
  461. break;
  462. case PT_PROTO_INFANT:
  463. case PT_PROTO_LAUNCHED:
  464. case PT_PROTO_ACCEPTING_METHODS:
  465. case PT_PROTO_COMPLETED:
  466. default:
  467. log_warn(LD_CONFIG, "Unexpected managed proxy state in "
  468. "handle_finished_proxy().");
  469. tor_assert(0);
  470. }
  471. unconfigured_proxies_n--;
  472. tor_assert(unconfigured_proxies_n >= 0);
  473. }
  474. /** Return true if the configuration of the managed proxy <b>mp</b> is
  475. finished. */
  476. static INLINE int
  477. proxy_configuration_finished(const managed_proxy_t *mp)
  478. {
  479. return (mp->conf_state == PT_PROTO_CONFIGURED ||
  480. mp->conf_state == PT_PROTO_BROKEN ||
  481. mp->conf_state == PT_PROTO_FAILED_LAUNCH);
  482. }
  483. /** This function is called when a proxy sends an {S,C}METHODS DONE message. */
  484. static void
  485. handle_methods_done(const managed_proxy_t *mp)
  486. {
  487. tor_assert(mp->transports);
  488. if (smartlist_len(mp->transports) == 0)
  489. log_notice(LD_GENERAL, "Proxy was spawned successfully, "
  490. "but it didn't laucn any pluggable transport listeners!");
  491. log_info(LD_CONFIG, "%s managed proxy configuration completed!",
  492. mp->is_server ? "Server" : "Client");
  493. }
  494. /** Handle a configuration protocol <b>line</b> received from a
  495. * managed proxy <b>mp</b>. */
  496. void
  497. handle_proxy_line(const char *line, managed_proxy_t *mp)
  498. {
  499. log_debug(LD_GENERAL, "Got a line from managed proxy: %s", line);
  500. if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
  501. log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
  502. "Discarding");
  503. goto err;
  504. }
  505. if (!strcmpstart(line, PROTO_ENV_ERROR)) {
  506. if (mp->conf_state != PT_PROTO_LAUNCHED)
  507. goto err;
  508. parse_env_error(line);
  509. goto err;
  510. } else if (!strcmpstart(line, PROTO_NEG_FAIL)) {
  511. if (mp->conf_state != PT_PROTO_LAUNCHED)
  512. goto err;
  513. log_warn(LD_CONFIG, "Managed proxy could not pick a "
  514. "configuration protocol version.");
  515. goto err;
  516. } else if (!strcmpstart(line, PROTO_NEG_SUCCESS)) {
  517. if (mp->conf_state != PT_PROTO_LAUNCHED)
  518. goto err;
  519. if (parse_version(line,mp) < 0)
  520. goto err;
  521. tor_assert(mp->conf_protocol != 0);
  522. mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
  523. return;
  524. } else if (!strcmpstart(line, PROTO_CMETHODS_DONE)) {
  525. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  526. goto err;
  527. handle_methods_done(mp);
  528. mp->conf_state = PT_PROTO_CONFIGURED;
  529. return;
  530. } else if (!strcmpstart(line, PROTO_SMETHODS_DONE)) {
  531. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  532. goto err;
  533. handle_methods_done(mp);
  534. mp->conf_state = PT_PROTO_CONFIGURED;
  535. return;
  536. } else if (!strcmpstart(line, PROTO_CMETHOD_ERROR)) {
  537. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  538. goto err;
  539. parse_client_method_error(line);
  540. goto err;
  541. } else if (!strcmpstart(line, PROTO_SMETHOD_ERROR)) {
  542. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  543. goto err;
  544. parse_server_method_error(line);
  545. goto err;
  546. } else if (!strcmpstart(line, PROTO_CMETHOD)) {
  547. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  548. goto err;
  549. if (parse_cmethod_line(line, mp) < 0)
  550. goto err;
  551. return;
  552. } else if (!strcmpstart(line, PROTO_SMETHOD)) {
  553. if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
  554. goto err;
  555. if (parse_smethod_line(line, mp) < 0)
  556. goto err;
  557. return;
  558. } else if (!strcmpstart(line, SPAWN_ERROR_MESSAGE)) {
  559. log_warn(LD_GENERAL, "Could not launch managed proxy executable!");
  560. mp->conf_state = PT_PROTO_FAILED_LAUNCH;
  561. return;
  562. }
  563. log_warn(LD_CONFIG, "Unknown line received by managed proxy. (%s)", line);
  564. err:
  565. mp->conf_state = PT_PROTO_BROKEN;
  566. log_warn(LD_CONFIG, "Managed proxy at '%s' failed the configuration protocol"
  567. " and will be destroyed.", mp->argv ? mp->argv[0] : "");
  568. }
  569. /** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
  570. void
  571. parse_env_error(const char *line)
  572. {
  573. /* (Length of the protocol string) plus (a space) and (the first char of
  574. the error message) */
  575. if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
  576. log_notice(LD_CONFIG, "Managed proxy sent us an %s without an error "
  577. "message.", PROTO_ENV_ERROR);
  578. log_warn(LD_CONFIG, "Managed proxy couldn't understand the "
  579. "pluggable transport environment variables. (%s)",
  580. line+strlen(PROTO_ENV_ERROR)+1);
  581. }
  582. /** Handles a VERSION <b>line</b>. Updates the configuration protocol
  583. * version in <b>mp</b>. */
  584. int
  585. parse_version(const char *line, managed_proxy_t *mp)
  586. {
  587. if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
  588. log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
  589. PROTO_NEG_SUCCESS);
  590. return -1;
  591. }
  592. if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) { /* hardcoded temp */
  593. log_warn(LD_CONFIG, "Managed proxy tried to negotiate on version '%s'. "
  594. "We only support version '1'", line+strlen(PROTO_NEG_SUCCESS)+1);
  595. return -1;
  596. }
  597. mp->conf_protocol = PROTO_VERSION_ONE; /* temp. till more versions appear */
  598. return 0;
  599. }
  600. /** Parses {C,S}METHOD-ERROR <b>line</b> and warns the user
  601. * accordingly. If <b>is_server</b> it is an SMETHOD-ERROR,
  602. * otherwise it is a CMETHOD-ERROR. */
  603. static void
  604. parse_method_error(const char *line, int is_server)
  605. {
  606. const char* error = is_server ?
  607. PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;
  608. /* (Length of the protocol string) plus (a space) and (the first char of
  609. the error message) */
  610. if (strlen(line) < (strlen(error) + 2))
  611. log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
  612. "message.", error);
  613. log_warn(LD_CONFIG, "%s managed proxy encountered a method error. (%s)",
  614. is_server ? "Server" : "Client",
  615. line+strlen(error)+1);
  616. }
  617. /** Parses an SMETHOD <b>line</b> and if well-formed it registers the
  618. * new transport in <b>mp</b>. */
  619. int
  620. parse_smethod_line(const char *line, managed_proxy_t *mp)
  621. {
  622. int r;
  623. smartlist_t *items = NULL;
  624. char *method_name=NULL;
  625. char *addrport=NULL;
  626. tor_addr_t addr;
  627. uint16_t port = 0;
  628. transport_t *transport=NULL;
  629. items = smartlist_create();
  630. smartlist_split_string(items, line, NULL,
  631. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  632. if (smartlist_len(items) < 3) {
  633. log_warn(LD_CONFIG, "Server managed proxy sent us a SMETHOD line "
  634. "with too few arguments.");
  635. goto err;
  636. }
  637. tor_assert(!strcmp(smartlist_get(items,0),PROTO_SMETHOD));
  638. method_name = smartlist_get(items,1);
  639. if (!string_is_C_identifier(method_name)) {
  640. log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
  641. method_name);
  642. goto err;
  643. }
  644. addrport = smartlist_get(items, 2);
  645. if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
  646. log_warn(LD_CONFIG, "Error parsing transport "
  647. "address '%s'", addrport);
  648. goto err;
  649. }
  650. if (!port) {
  651. log_warn(LD_CONFIG,
  652. "Transport address '%s' has no port.", addrport);
  653. goto err;
  654. }
  655. transport = transport_create(&addr, port, method_name, PROXY_NONE);
  656. if (!transport)
  657. goto err;
  658. smartlist_add(mp->transports, transport);
  659. /* For now, notify the user so that he knows where the server
  660. transport is listening. */
  661. log_info(LD_CONFIG, "Server transport %s at %s:%d.",
  662. method_name, fmt_addr(&addr), (int)port);
  663. r=0;
  664. goto done;
  665. err:
  666. r = -1;
  667. done:
  668. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  669. smartlist_free(items);
  670. return r;
  671. }
  672. /** Parses a CMETHOD <b>line</b>, and if well-formed it registers
  673. * the new transport in <b>mp</b>. */
  674. int
  675. parse_cmethod_line(const char *line, managed_proxy_t *mp)
  676. {
  677. int r;
  678. smartlist_t *items = NULL;
  679. char *method_name=NULL;
  680. char *socks_ver_str=NULL;
  681. int socks_ver=PROXY_NONE;
  682. char *addrport=NULL;
  683. tor_addr_t addr;
  684. uint16_t port = 0;
  685. transport_t *transport=NULL;
  686. items = smartlist_create();
  687. smartlist_split_string(items, line, NULL,
  688. SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
  689. if (smartlist_len(items) < 4) {
  690. log_warn(LD_CONFIG, "Client managed proxy sent us a CMETHOD line "
  691. "with too few arguments.");
  692. goto err;
  693. }
  694. tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));
  695. method_name = smartlist_get(items,1);
  696. if (!string_is_C_identifier(method_name)) {
  697. log_warn(LD_CONFIG, "Transport name is not a C identifier (%s).",
  698. method_name);
  699. goto err;
  700. }
  701. socks_ver_str = smartlist_get(items,2);
  702. if (!strcmp(socks_ver_str,"socks4")) {
  703. socks_ver = PROXY_SOCKS4;
  704. } else if (!strcmp(socks_ver_str,"socks5")) {
  705. socks_ver = PROXY_SOCKS5;
  706. } else {
  707. log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
  708. "we don't recognize. (%s)", socks_ver_str);
  709. goto err;
  710. }
  711. addrport = smartlist_get(items, 3);
  712. if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
  713. log_warn(LD_CONFIG, "Error parsing transport "
  714. "address '%s'", addrport);
  715. goto err;
  716. }
  717. if (!port) {
  718. log_warn(LD_CONFIG,
  719. "Transport address '%s' has no port.", addrport);
  720. goto err;
  721. }
  722. transport = transport_create(&addr, port, method_name, socks_ver);
  723. if (!transport)
  724. goto err;
  725. smartlist_add(mp->transports, transport);
  726. log_info(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
  727. "Attached to managed proxy.",
  728. method_name, fmt_addr(&addr), (int)port, socks_ver);
  729. r=0;
  730. goto done;
  731. err:
  732. r = -1;
  733. done:
  734. SMARTLIST_FOREACH(items, char*, s, tor_free(s));
  735. smartlist_free(items);
  736. return r;
  737. }
  738. /** Return a string containing the address:port that <b>transport</b>
  739. * should use. It's the responsibility of the caller to free() the
  740. * received string. */
  741. static char *
  742. get_bindaddr_for_proxy(const managed_proxy_t *mp)
  743. {
  744. char *bindaddr = NULL;
  745. smartlist_t *string_tmp = smartlist_create();
  746. tor_assert(mp->is_server);
  747. SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
  748. tor_asprintf(&bindaddr, "%s-%s", t, get_bindaddr_for_transport(t));
  749. smartlist_add(string_tmp, bindaddr);
  750. } SMARTLIST_FOREACH_END(t);
  751. bindaddr = smartlist_join_strings(string_tmp, ",", 0, NULL);
  752. SMARTLIST_FOREACH(string_tmp, char *, t, tor_free(t));
  753. smartlist_free(string_tmp);
  754. return bindaddr;
  755. }
  756. #ifdef MS_WINDOWS
  757. /** Prepare the environment <b>envp</b> of managed proxy <b>mp</b>.
  758. * <b>envp</b> is allocated on the heap and should be freed by the
  759. * caller after its use. */
  760. static void
  761. set_managed_proxy_environment(LPVOID *envp, const managed_proxy_t *mp)
  762. {
  763. const or_options_t *options = get_options();
  764. extern char **environ;
  765. LPVOID tmp=NULL;
  766. char *state_tmp=NULL;
  767. char *state_env=NULL;
  768. char *transports_to_launch=NULL;
  769. char *transports_env=NULL;
  770. char *bindaddr_tmp=NULL;
  771. char *bindaddr_env=NULL;
  772. char *orport_env=NULL;
  773. char version_env[31]; /* XXX temp */
  774. char extended_env[43]; /* XXX temp */
  775. int env_size = 0;
  776. /* A smartlist carrying all the env. variables that the managed
  777. proxy should inherit. */
  778. smartlist_t *envs = smartlist_create();
  779. /* Copy the whole environment of the Tor process.
  780. It should also copy PATH and HOME of the Tor process.*/
  781. char **environ_tmp = environ;
  782. while (*environ_tmp)
  783. smartlist_add(envs, *environ_tmp++);
  784. /* Create the TOR_PT_* environment variables. */
  785. state_tmp = get_datadir_fname("pt_state/"); /* XXX temp */
  786. tor_asprintf(&state_env, "TOR_PT_STATE_LOCATION=%s", state_tmp);
  787. strcpy(version_env, "TOR_PT_MANAGED_TRANSPORT_VER=1");
  788. transports_to_launch =
  789. smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
  790. tor_asprintf(&transports_env,
  791. mp->is_server ?
  792. "TOR_PT_SERVER_TRANSPORTS=%s" : "TOR_PT_CLIENT_TRANSPORTS=%s",
  793. transports_to_launch);
  794. smartlist_add(envs, state_env);
  795. smartlist_add(envs, version_env);
  796. smartlist_add(envs, transports_env);
  797. if (mp->is_server) {
  798. tor_asprintf(&orport_env, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort);
  799. bindaddr_tmp = get_bindaddr_for_proxy(mp);
  800. tor_asprintf(&bindaddr_env, "TOR_PT_SERVER_BINDADDR=%s", bindaddr_tmp);
  801. strcpy(extended_env, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200");
  802. smartlist_add(envs, orport_env);
  803. smartlist_add(envs, extended_env);
  804. smartlist_add(envs, bindaddr_env);
  805. }
  806. /* It seems like some versions of Windows need a sorted lpEnvironment
  807. block. */
  808. smartlist_sort_strings(envs);
  809. /* An environment block consists of a null-terminated block of
  810. null-terminated strings: */
  811. /* Calculate the block's size. */
  812. SMARTLIST_FOREACH(envs, const char *, s,
  813. env_size += strlen(s) + 1);
  814. env_size += 1; /* space for last NUL */
  815. *envp = tor_malloc(env_size);
  816. tmp = *envp;
  817. /* Create the block. */
  818. SMARTLIST_FOREACH_BEGIN(envs, const char *, s) {
  819. memcpy(tmp, s, strlen(s)); /* copy the env. variable string */
  820. tmp += strlen(s);
  821. memset(tmp, '\0', 1); /* append NUL at the end of the string */
  822. tmp += 1;
  823. } SMARTLIST_FOREACH_END(s);
  824. memset(tmp, '\0', 1); /* last NUL */
  825. /* Finally, free the whole mess. */
  826. tor_free(state_tmp);
  827. tor_free(state_env);
  828. tor_free(transports_to_launch);
  829. tor_free(transports_env);
  830. tor_free(bindaddr_tmp);
  831. tor_free(bindaddr_env);
  832. tor_free(orport_env);
  833. smartlist_free(envs);
  834. }
  835. #else /* MS_WINDOWS */
  836. /** Prepare the environment <b>envp</b> of managed proxy <b>mp</b>.
  837. * <b>envp</b> is allocated on the heap and should be freed by the
  838. * caller after its use. */
  839. static int
  840. set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp)
  841. {
  842. const or_options_t *options = get_options();
  843. char **tmp=NULL;
  844. char *state_loc=NULL;
  845. char *transports_to_launch=NULL;
  846. char *bindaddr=NULL;
  847. char *home_env=NULL;
  848. char *path_env=NULL;
  849. int r = -1;
  850. int n_envs = mp->is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
  851. /* allocate enough space for our env. vars and a NULL pointer */
  852. *envp = tor_malloc(sizeof(char*)*(n_envs+1));
  853. tmp = *envp;
  854. state_loc = get_datadir_fname("pt_state/"); /* XXX temp */
  855. transports_to_launch =
  856. smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
  857. home_env = getenv("HOME");
  858. path_env = getenv("PATH");
  859. if (!home_env || !path_env)
  860. goto done;
  861. tor_asprintf(tmp++, "HOME=%s", home_env);
  862. tor_asprintf(tmp++, "PATH=%s", path_env);
  863. tor_asprintf(tmp++, "TOR_PT_STATE_LOCATION=%s", state_loc);
  864. tor_asprintf(tmp++, "TOR_PT_MANAGED_TRANSPORT_VER=1"); /* temp */
  865. if (mp->is_server) {
  866. bindaddr = get_bindaddr_for_proxy(mp);
  867. /* XXX temp */
  868. tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort);
  869. tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=%s", bindaddr);
  870. tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", transports_to_launch);
  871. /* XXX temp*/
  872. tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200");
  873. } else {
  874. tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", transports_to_launch);
  875. }
  876. *tmp = NULL;
  877. r = 0;
  878. done:
  879. tor_free(state_loc);
  880. tor_free(transports_to_launch);
  881. tor_free(bindaddr);
  882. return r;
  883. }
  884. #endif /* MS_WINDOWS */
  885. /** Create and return a new managed proxy for <b>transport</b> using
  886. * <b>proxy_argv</b>. If <b>is_server</b> is true, it's a server
  887. * managed proxy. */
  888. static managed_proxy_t *
  889. managed_proxy_create(const smartlist_t *transport_list,
  890. char **proxy_argv, int is_server)
  891. {
  892. managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
  893. mp->conf_state = PT_PROTO_INFANT;
  894. mp->is_server = is_server;
  895. mp->argv = proxy_argv;
  896. mp->transports = smartlist_create();
  897. mp->transports_to_launch = smartlist_create();
  898. SMARTLIST_FOREACH(transport_list, const char *, transport,
  899. add_transport_to_proxy(transport, mp));
  900. /* register the managed proxy */
  901. if (!managed_proxy_list)
  902. managed_proxy_list = smartlist_create();
  903. smartlist_add(managed_proxy_list, mp);
  904. unconfigured_proxies_n++;
  905. return mp;
  906. }
  907. /** Register <b>transport</b> using proxy with <b>proxy_argv</b> to
  908. * the managed proxy subsystem.
  909. * If <b>is_server</b> is true, then the proxy is a server proxy. */
  910. void
  911. pt_kickstart_proxy(const smartlist_t *transport_list,
  912. char **proxy_argv, int is_server)
  913. {
  914. managed_proxy_t *mp=NULL;
  915. transport_t *old_transport = NULL;
  916. mp = get_managed_proxy_by_argv_and_type(proxy_argv, is_server);
  917. if (!mp) { /* we haven't seen this proxy before */
  918. managed_proxy_create(transport_list, proxy_argv, is_server);
  919. } else { /* known proxy. add its transport to its transport list */
  920. if (mp->got_hup) {
  921. /* If the managed proxy we found is marked by a SIGHUP, it means
  922. that it's not useless and should be kept. If it's marked for
  923. removal, unmark it and increase the unconfigured proxies so
  924. that we try to restart it if we need to. Afterwards, check if
  925. a transport_t for 'transport' used to exist before the SIGHUP
  926. and make sure it doesn't get deleted because we might reuse
  927. it. */
  928. if (mp->marked_for_removal) {
  929. mp->marked_for_removal = 0;
  930. unconfigured_proxies_n++;
  931. }
  932. SMARTLIST_FOREACH_BEGIN(transport_list, const char *, transport) {
  933. old_transport = transport_get_by_name(transport);
  934. if (old_transport)
  935. old_transport->marked_for_removal = 0;
  936. } SMARTLIST_FOREACH_END(transport);
  937. }
  938. SMARTLIST_FOREACH(transport_list, const char *, transport,
  939. add_transport_to_proxy(transport, mp));
  940. free_execve_args(proxy_argv);
  941. }
  942. }
  943. /** Frees the array of pointers in <b>arg</b> used as arguments to
  944. execve(2). */
  945. static INLINE void
  946. free_execve_args(char **arg)
  947. {
  948. char **tmp = arg;
  949. while (*tmp) /* use the fact that the last element of the array is a
  950. NULL pointer to know when to stop freeing */
  951. _tor_free(*tmp++);
  952. tor_free(arg);
  953. }
  954. /** Tor will read its config.
  955. * Prepare the managed proxy list so that proxies not used in the new
  956. * config will shutdown, and proxies that need to spawn different
  957. * transports will do so. */
  958. void
  959. pt_prepare_proxy_list_for_config_read(void)
  960. {
  961. if (!managed_proxy_list)
  962. return;
  963. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  964. /* Destroy unconfigured proxies. */
  965. if (mp->conf_state != PT_PROTO_COMPLETED) {
  966. managed_proxy_destroy(mp, 1);
  967. unconfigured_proxies_n--;
  968. continue;
  969. }
  970. tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
  971. mp->marked_for_removal = 1;
  972. mp->got_hup = 1;
  973. SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
  974. smartlist_clear(mp->transports_to_launch);
  975. } SMARTLIST_FOREACH_END(mp);
  976. tor_assert(unconfigured_proxies_n == 0);
  977. }
  978. /** The tor config was read.
  979. * Destroy all managed proxies that were marked by a previous call to
  980. * prepare_proxy_list_for_config_read() and are not used by the new
  981. * config. */
  982. void
  983. sweep_proxy_list(void)
  984. {
  985. if (!managed_proxy_list)
  986. return;
  987. SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
  988. if (mp->marked_for_removal) {
  989. SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
  990. managed_proxy_destroy(mp, 1);
  991. }
  992. } SMARTLIST_FOREACH_END(mp);
  993. }
  994. /** Release all storage held by the pluggable transports subsystem. */
  995. void
  996. pt_free_all(void)
  997. {
  998. if (managed_proxy_list) {
  999. /* If the proxy is in PT_PROTO_COMPLETED, it has registered its
  1000. transports and it's the duty of the circuitbuild.c subsystem to
  1001. free them. Otherwise, it hasn't registered its transports yet
  1002. and we should free them here. */
  1003. SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp,
  1004. managed_proxy_destroy(mp, 1));
  1005. smartlist_free(managed_proxy_list);
  1006. managed_proxy_list=NULL;
  1007. }
  1008. }