transports.c 36 KB

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