transports.c 38 KB

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