tor-commits
Threads by month
- ----- 2026 -----
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
October 2011
- 18 participants
- 1256 discussions
[tor/master] Update the transports.c documentation based on the new data.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit a002f0e7c09dc664148a018fc69ac416789330dd
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Sun Sep 11 20:29:52 2011 +0200
Update the transports.c documentation based on the new data.
---
src/or/transports.c | 82 +++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 70 insertions(+), 12 deletions(-)
diff --git a/src/or/transports.c b/src/or/transports.c
index c4391c5..7d483a3 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -56,23 +56,81 @@ static smartlist_t *managed_proxy_list = NULL;
/** Number of still unconfigured proxies. */
static int unconfigured_proxies_n = 0;
-/* The main idea here is:
+/** "The main idea is:"
- A managed proxy is represented by a managed_proxy_t struct and can
- spawn multiple transports.
+ Each managed proxy is represented by a 'managed_proxy_t'.
+ Each managed proxy can support multiple transports.
+ Each managed proxy gets configured through a multistep process.
+
+ 'managed_proxy_list' contains all the managed proxies this tor
+ instance is supporting.
+ In the 'managed_proxy_list' there are 'unconfigured_proxies_n'
+ managed proxies that are still unconfigured.
- managed_proxy_list is a list of all the unconfigured managed
- proxies; everytime we find a managed proxy in torrc we add it in
- that list.
In every run_scheduled_event() tick, we attempt to launch and then
- configure each managed proxy, using the configuration protocol
- defined in the 180_pluggable_transport.txt proposal. A managed
- proxy might need several ticks to get fully configured.
+ configure the unconfiged managed proxies, using the configuration
+ protocol defined in the 180_pluggable_transport.txt proposal. A
+ managed proxy might need several ticks to get fully configured.
When a managed proxy is fully configured, we register all its
- transports to the circuitbuild.c subsystem - like we do with
- external proxies - and then free the managed proxy struct
- since it's no longer needed. */
+ transports to the circuitbuild.c subsystem. At that point the
+ transports are owned by the circuitbuild.c subsystem.
+
+ When a managed proxy fails to follow the 180 configuration
+ protocol, it gets marked as broken and gets destroyed.
+
+ "In a little more technical detail:"
+
+ While we are serially parsing torrc, we store all the transports
+ that a proxy should spawn in its 'transports_to_launch' element.
+
+ When we finish reading the torrc, we spawn the managed proxy and
+ expect {S,C}METHOD lines from its output. We add transports
+ described by METHOD lines to its 'transports' element, as
+ 'transport_t' structs.
+
+ When the managed proxy stops spitting METHOD lines (signified by a
+ '{S,C}METHODS DONE' message) we register all the transports
+ collected to the circuitbuild.c subsystem. At this point, the
+ 'transport_t's can be transformed into dangling pointers at any
+ point by the circuitbuild.c subsystem, and so we replace all
+ 'transport_t's with strings describing the transport names. We
+ can still go from a transport name to a 'transport_t' using the
+ fact that transport names uniquely identify 'transport_t's.
+
+ "In even more technical detail I shall describe what happens when
+ the SIGHUP bell tolls:"
+
+ We immediately destroy all unconfigured proxies (We shouldn't have
+ unconfigured proxies in the first place, except when SIGHUP rings
+ immediately after tor is launched.).
+
+ We mark all managed proxies and transports to signify that they
+ must be removed if they don't contribute by the new torrc. We also
+ mark all managed proxies to signify that they might need to be
+ restarted so that they end up supporting all the transports the
+ new torrc wants them to support.
+ We also clear the 'transports_to_launch' list so that we can put
+ there the transports we need to launch on each proxy according to
+ the new torrc.
+
+ We then start parsing torrc again, everytime we encounter a
+ transport line using a known pre-SIGHUP managed proxy, we cleanse
+ that proxy from the removal mark.
+
+ We also mark it as unconfigured so that on the next scheduled
+ events tick, we investigate whether we need to restart the proxy
+ so that it spawns the new 'transport_to_launch' list. Of course,
+ if the post-SIGHUP 'transports_to_launch' list is identical to the
+ pre-SIGHUP one, it means that no changes were introduced to this
+ proxy during the SIGHUP and no restart has to take place.
+
+ During the post-SIGHUP torrc parsing, we unmark all transports we
+ encounter. This happens in the case that no restart is needed, we
+ can continue using the old transports normally. If we end up
+ restarting the proxy, we destroy and unregister all old transports
+ from the circuitbuild.c subsystem since they become useless.
+*/
/** Return true if there are still unconfigured managed proxies. */
int
1
0
[tor/master] Remove connection_uses_transport() since it was unused.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit 0dcf327248d9e971b2bad9fcd498c3b18f2f70c0
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Sun Sep 11 20:33:27 2011 +0200
Remove connection_uses_transport() since it was unused.
---
src/or/connection.c | 11 -----------
src/or/connection.h | 1 -
2 files changed, 0 insertions(+), 12 deletions(-)
diff --git a/src/or/connection.c b/src/or/connection.c
index 04dba2b..05688e5 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -4150,17 +4150,6 @@ get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
return 0;
}
-/** Returns true if connection <b>conn</b> is using a pluggable
- * transports proxy server. */
-int
-connection_uses_transport(connection_t *conn)
-{
- const transport_t *transport=NULL;
- find_transport_by_bridge_addrport(&conn->addr,
- conn->port,&transport);
- return transport ? 1 : 0;
-}
-
/** Returns the global proxy type used by tor. */
static int
get_proxy_type(void)
diff --git a/src/or/connection.h b/src/or/connection.h
index 51cf2a8..be3de88 100644
--- a/src/or/connection.h
+++ b/src/or/connection.h
@@ -60,7 +60,6 @@ int connection_read_proxy_handshake(connection_t *conn);
void log_failed_proxy_connection(connection_t *conn);
int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
const connection_t *conn);
-int connection_uses_transport(connection_t *conn);
int retry_all_listeners(smartlist_t *replaced_conns,
smartlist_t *new_conns);
1
0
07 Oct '11
commit 9a42ec685751b5c5fd21f33cd788810d997f0d6e
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Sun Sep 11 21:33:02 2011 +0200
Be more defensive in get_transport_bindaddr().
Make sure that lines in get_transport_bindaddr() begin with the name
of the transport and a space.
---
src/or/config.c | 20 +++++++++++++++++---
1 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/src/or/config.c b/src/or/config.c
index 792124c..79c1b1e 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -5823,10 +5823,24 @@ get_transport_in_state_by_name(const char *transport)
const char *
get_transport_bindaddr(const char *line, const char *transport)
{
- if (strlen(line) < strlen(transport) + 2)
- return NULL;
- else
+ char *line_tmp = NULL;
+
+ if (strlen(line) < strlen(transport) + 2) {
+ goto broken_state;
+ } else {
+ /* line should start with the name of the transport and a space.
+ (for example, "obfs2 127.0.0.1:47245") */
+ tor_asprintf(&line_tmp, "%s ", transport);
+ if (strcmpstart(line, line_tmp))
+ goto broken_state;
+
+ tor_free(line_tmp);
return (line+strlen(transport)+1);
+ }
+
+ broken_state:
+ tor_free(line_tmp);
+ return NULL;
}
/** Return a static string containing the address:port a proxy
1
0
commit 2703e41d8b6ffcad653af68a8261c22b5a1ed26f
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Sun Sep 11 20:57:01 2011 +0200
Improve how we access or_state_t.
* Use get_or_state()->VirtualOption instead of relying on
config_find_option(), STRUCT_VAR_P and voodoo.
---
src/or/config.c | 30 ++++++++++--------------------
1 files changed, 10 insertions(+), 20 deletions(-)
diff --git a/src/or/config.c b/src/or/config.c
index 829d5ff..bacdae3 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -5513,16 +5513,11 @@ static int
validate_transports_in_state(or_state_t *state)
{
int broken = 0;
+ config_line_t *line;
- config_var_t *var = config_find_option(&state_format,"TransportProxies");
- if (!var)
- return 0;
-
- config_line_t **value = STRUCT_VAR_P(state, var->var_offset);
- config_line_t *search = NULL;
-
- for (search = *value ; search ; search = search->next) {
- if (!state_transport_line_is_valid(search->value)<0)
+ for (line = state->TransportProxies ; line ; line = line->next) {
+ tor_assert(!strcmp(line->key, "TransportProxy"));
+ if (!state_transport_line_is_valid(line->value)<0)
broken = 1;
}
@@ -5790,18 +5785,13 @@ or_state_save(time_t now)
static config_line_t *
get_transport_in_state_by_name(const char *transport)
{
- config_var_t *var = config_find_option(&state_format,"TransportProxies");
- if (!var)
- return NULL;
-
- config_line_t **value = STRUCT_VAR_P(get_or_state(), var->var_offset);
- config_line_t *search = *value;
-
- while (search) {
- if (!strcmpstart(search->value, transport))
- return search;
+ or_state_t *or_state = get_or_state();
+ config_line_t *line;
- search = search->next;
+ for (line = or_state->TransportProxies ; line ; line = line->next) {
+ tor_assert(!strcmp(line->key, "TransportProxy"));
+ if (!strcmpstart(line->value, transport))
+ return line;
}
return NULL;
}
1
0
[tor/master] Prepare circuitbuild.[ch] and config.[ch] for SIGHUPs.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit fa514fb207f23cb6f0ade95bbd830834ea14811f
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Sun Sep 11 20:28:47 2011 +0200
Prepare circuitbuild.[ch] and config.[ch] for SIGHUPs.
* Create mark/sweep functions for transports.
* Create a transport_resolve_conflicts() function that tries to
resolve conflicts when registering transports.
---
src/or/circuitbuild.c | 126 +++++++++++++++++++++++++++++++++++++++++++------
src/or/circuitbuild.h | 8 +++
src/or/config.c | 10 ++--
3 files changed, 124 insertions(+), 20 deletions(-)
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index 382016e..bd06d31 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -124,7 +124,6 @@ static int onion_append_hop(crypt_path_t **head_ptr, extend_info_t *choice);
static void entry_guards_changed(void);
-static const transport_t *transport_get_by_name(const char *name);
static void bridge_free(bridge_info_t *bridge);
/**
@@ -4579,6 +4578,32 @@ bridge_free(bridge_info_t *bridge)
/** A list of pluggable transports found in torrc. */
static smartlist_t *transport_list = NULL;
+/** Mark every entry of the transport list to be removed on our next call to
+ * sweep_transport_list unless it has first been un-marked. */
+void
+mark_transport_list(void)
+{
+ if (!transport_list)
+ transport_list = smartlist_create();
+ SMARTLIST_FOREACH(transport_list, transport_t *, t,
+ t->marked_for_removal = 1);
+}
+
+/** Remove every entry of the transport list that was marked with
+ * mark_transport_list if it has not subsequently been un-marked. */
+void
+sweep_transport_list(void)
+{
+ if (!transport_list)
+ transport_list = smartlist_create();
+ SMARTLIST_FOREACH_BEGIN(transport_list, transport_t *, t) {
+ if (t->marked_for_removal) {
+ SMARTLIST_DEL_CURRENT(transport_list, t);
+ transport_free(t);
+ }
+ } SMARTLIST_FOREACH_END(t);
+}
+
/** Initialize the pluggable transports list to empty, creating it if
* needed. */
void
@@ -4603,7 +4628,7 @@ transport_free(transport_t *transport)
/** Returns the transport in our transport list that has the name <b>name</b>.
* Else returns NULL. */
-static const transport_t *
+transport_t *
transport_get_by_name(const char *name)
{
tor_assert(name);
@@ -4611,7 +4636,7 @@ transport_get_by_name(const char *name)
if (!transport_list)
return NULL;
- SMARTLIST_FOREACH_BEGIN(transport_list, const transport_t *, transport) {
+ SMARTLIST_FOREACH_BEGIN(transport_list, transport_t *, transport) {
if (!strcmp(transport->name, name))
return transport;
} SMARTLIST_FOREACH_END(transport);
@@ -4636,23 +4661,81 @@ transport_create(const tor_addr_t *addr, uint16_t port,
return t;
}
-/** Adds transport <b>t</b> to the internal list of pluggable transports. */
+/** Resolve any conflicts that the insertion of transport <b>t</b>
+ * might cause.
+ * Return 0 if <b>t</b> is OK and should be registered, 1 if there is
+ * a transport identical to <b>t</b> already registered and -1 if
+ * <b>t</b> cannot be added due to conflicts. */
+static int
+transport_resolve_conflicts(transport_t *t)
+{
+ /* This is how we resolve transport conflicts:
+
+ If there is already a transport with the same name and addrport,
+ we either have duplicate torrc lines OR we are here post-HUP and
+ this transport was here pre-HUP as well. In any case, mark the
+ old transport so that it doesn't get removed and ignore the new
+ one.
+
+ If there is already a transport with the same name but different
+ addrport:
+ * if it's marked for removal, it means that it either has a lower
+ priority than 't' in torrc (otherwise the mark would have been
+ cleared by the paragraph above), or it doesn't exist at all in
+ the post-HUP torrc. We destroy the old transport and register 't'.
+ * if it's *not* marked for removal, it means that it was newly
+ added in the post-HUP torrc or that it's of higher priority, in
+ this case we ignore 't'. */
+ transport_t *t_tmp = transport_get_by_name(t->name);
+ if (t_tmp) { /* same name */
+ if (tor_addr_eq(&t->addr, &t_tmp->addr) && (t->port == t_tmp->port)) {
+ /* same name *and* addrport */
+ t_tmp->marked_for_removal = 0;
+ return 1;
+ } else { /* same name but different addrport */
+ if (t_tmp->marked_for_removal) { /* marked for removal */
+ log_warn(LD_GENERAL, "You tried to add transport '%s' at '%s:%u' but "
+ "there was already a transport marked for deletion at '%s:%u'."
+ "We deleted the old transport and registered the new one.",
+ t->name, fmt_addr(&t->addr), t->port,
+ fmt_addr(&t_tmp->addr), t_tmp->port);
+ smartlist_remove(transport_list, t_tmp);
+ transport_free(t_tmp);
+ } else { /* *not* marked for removal */
+ log_warn(LD_GENERAL, "You tried to add transport '%s' at '%s:%u' which "
+ "already exists at '%s:%u'. Skipping.", t->name,
+ fmt_addr(&t->addr), t->port,
+ fmt_addr(&t_tmp->addr), t_tmp->port);
+ return -1;
+ }
+ }
+ }
+
+ return 0;
+}
+
+/** Add transport <b>t</b> to the internal list of pluggable
+ * transports.
+ * Returns 0 if the transport was added correctly, 1 if the same
+ * transport was already registered (in this case the caller must
+ * free the transport) and -1 if there was an error. */
int
transport_add(transport_t *t)
{
tor_assert(t);
- if (transport_get_by_name(t->name)) { /* check for duplicate names */
- log_notice(LD_CONFIG, "More than one transports have '%s' as "
- "their name.", t->name);
- return -1;
- }
+ int r = transport_resolve_conflicts(t);
- if (!transport_list)
- transport_list = smartlist_create();
+ switch (r) {
+ case 0: /* should register transport */
+ if (!transport_list)
+ transport_list = smartlist_create();
- smartlist_add(transport_list, t);
- return 0;
+ smartlist_add(transport_list, t);
+ return 0;
+ default: /* should let the caller know the return code */
+ return r;
+ }
}
/** Remember a new pluggable transport proxy at <b>addr</b>:<b>port</b>.
@@ -4664,10 +4747,23 @@ transport_add_from_config(const tor_addr_t *addr, uint16_t port,
{
transport_t *t = transport_create(addr, port, name, socks_ver);
- if (transport_add(t) < 0) {
+ int r = transport_add(t);
+
+ switch (r) {
+ case -1:
+ default:
+ log_warn(LD_GENERAL, "Could not add transport %s at %s:%u. Skipping.",
+ t->name, fmt_addr(&t->addr), t->port);
transport_free(t);
return -1;
- } else {
+ case 1:
+ log_warn(LD_GENERAL, "Succesfully registered transport %s at %s:%u.",
+ t->name, fmt_addr(&t->addr), t->port);
+ transport_free(t); /* falling */
+ return 0;
+ case 0:
+ log_warn(LD_GENERAL, "Succesfully registered transport %s at %s:%u.",
+ t->name, fmt_addr(&t->addr), t->port);
return 0;
}
}
diff --git a/src/or/circuitbuild.h b/src/or/circuitbuild.h
index 92449b4..10e7287 100644
--- a/src/or/circuitbuild.h
+++ b/src/or/circuitbuild.h
@@ -22,6 +22,9 @@ typedef struct {
tor_addr_t addr;
/** Port of proxy */
uint16_t port;
+ /** Boolean: We are re-parsing our transport list, and we are going to remove
+ * this one if we don't find it in the list of configured transports. */
+ unsigned marked_for_removal : 1;
} transport_t;
char *circuit_list_path(origin_circuit_t *circ, int verbose);
@@ -77,6 +80,9 @@ int getinfo_helper_entry_guards(control_connection_t *conn,
void mark_bridge_list(void);
void sweep_bridge_list(void);
+void mark_transport_list(void);
+void sweep_transport_list(void);
+
int routerinfo_is_a_configured_bridge(const routerinfo_t *ri);
int node_is_a_configured_bridge(const node_t *node);
void learned_router_identity(const tor_addr_t *addr, uint16_t port,
@@ -149,6 +155,8 @@ transport_t *transport_create(const tor_addr_t *addr, uint16_t port,
int find_transport_by_bridge_addrport(const tor_addr_t *addr, uint16_t port,
const transport_t **transport);
+transport_t *transport_get_by_name(const char *name);
+
void validate_pluggable_transports_config(void);
#endif
diff --git a/src/or/config.c b/src/or/config.c
index 0b84f92..829d5ff 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1251,7 +1251,8 @@ options_act(or_options_t *old_options)
}
- clear_transport_list();
+ mark_transport_list();
+ pt_prepare_proxy_list_for_config_read();
if (options->ClientTransportPlugin) {
for (cl = options->ClientTransportPlugin; cl; cl = cl->next) {
if (parse_client_transport_line(cl->value, 0)<0) {
@@ -1273,6 +1274,8 @@ options_act(or_options_t *old_options)
}
}
}
+ sweep_transport_list();
+ sweep_proxy_list();
/* Bail out at this point if we're not going to be a client or server:
* we want to not fork, and to log stuff to stderr. */
@@ -4769,10 +4772,7 @@ parse_client_transport_line(const char *line, int validate_only)
}
if (!validate_only) {
- if (transport_add_from_config(&addr, port, name,
- socks_ver) < 0) {
- goto err;
- }
+ transport_add_from_config(&addr, port, name, socks_ver);
log_debug(LD_DIR, "Transport '%s' found at %s:%d", name,
fmt_addr(&addr), (int)port);
1
0
07 Oct '11
commit 1e92b24889bd64ccdd568366aaf989714d130f31
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Sun Sep 11 20:29:12 2011 +0200
Update transports.[ch] to support SIGHUPs.
---
src/or/transports.c | 323 +++++++++++++++++++++++++++++++++++++++------------
src/or/transports.h | 42 +++++++-
2 files changed, 292 insertions(+), 73 deletions(-)
diff --git a/src/or/transports.c b/src/or/transports.c
index 6255a56..c4391c5 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -17,10 +17,7 @@
static void set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp);
static INLINE int proxy_configuration_finished(const managed_proxy_t *mp);
-static void managed_proxy_destroy_impl(managed_proxy_t *mp,
- int also_free_transports);
-#define managed_proxy_destroy(mp) managed_proxy_destroy_impl(mp, 0)
-#define managed_proxy_destroy_with_transports(mp) managed_proxy_destroy_impl(mp, 1)
+static void managed_proxy_destroy(managed_proxy_t *mp);
static void handle_finished_proxy(managed_proxy_t *mp);
static void configure_proxy(managed_proxy_t *mp);
@@ -55,14 +52,16 @@ static INLINE void free_execve_args(char **arg);
#define PROTO_VERSION_ONE 1
/** List of unconfigured managed proxies. */
-static smartlist_t *unconfigured_proxy_list = NULL;
+static smartlist_t *managed_proxy_list = NULL;
+/** Number of still unconfigured proxies. */
+static int unconfigured_proxies_n = 0;
/* The main idea here is:
A managed proxy is represented by a managed_proxy_t struct and can
spawn multiple transports.
- unconfigured_proxy_list is a list of all the unconfigured managed
+ managed_proxy_list is a list of all the unconfigured managed
proxies; everytime we find a managed proxy in torrc we add it in
that list.
In every run_scheduled_event() tick, we attempt to launch and then
@@ -79,8 +78,7 @@ static smartlist_t *unconfigured_proxy_list = NULL;
int
pt_proxies_configuration_pending(void)
{
- if (!unconfigured_proxy_list) return 0;
- return !!smartlist_len(unconfigured_proxy_list);
+ return !! unconfigured_proxies_n;
}
/** Return true if <b>mp</b> has the same argv as <b>proxy_argv</b> */
@@ -109,10 +107,10 @@ managed_proxy_has_argv(managed_proxy_t *mp, char **proxy_argv)
static managed_proxy_t *
get_managed_proxy_by_argv(char **proxy_argv)
{
- if (!unconfigured_proxy_list)
+ if (!managed_proxy_list)
return NULL;
- SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
+ SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
if (managed_proxy_has_argv(mp, proxy_argv))
return mp;
} SMARTLIST_FOREACH_END(mp);
@@ -129,22 +127,88 @@ add_transport_to_proxy(const char *transport, managed_proxy_t *mp)
smartlist_add(mp->transports_to_launch, tor_strdup(transport));
}
+/** Called when a SIGHUP occurs.
+ * Returns true if managed proxy <b>mp</b> needs to be restarted
+ * after the SIGHUP based on the new torrc. */
+static int
+proxy_needs_restart(managed_proxy_t *mp)
+{
+ /* mp->transport_to_launch is populated with the names of the
+ transports that must be launched *after* the SIGHUP.
+
+ Since only PT_PROTO_COMPLETED proxies reach this function,
+ mp->transports is populated with strings of the *names of the
+ transports* that were launched *before* the SIGHUP.
+
+ If the two lists contain the same strings, we don't need to
+ restart the proxy, since it already does what we want. */
+
+ tor_assert(smartlist_len(mp->transports_to_launch) > 0);
+ tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
+
+ if (smartlist_len(mp->transports_to_launch) != smartlist_len(mp->transports))
+ goto needs_restart;
+
+ SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t_t_l) {
+ if (!smartlist_string_isin(mp->transports, t_t_l))
+ goto needs_restart;
+
+ } SMARTLIST_FOREACH_END(t_t_l);
+
+ return 0;
+
+ needs_restart:
+ return 1;
+}
+
+/** Managed proxy <b>mp</b> must be restarted. Do all the necessary
+ * preparations and then flag its state so that it will be launched
+ * in the next tick. */
+static void
+proxy_prepare_for_restart(managed_proxy_t *mp)
+{
+ transport_t *t_tmp = NULL;
+
+ tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
+ tor_assert(mp->pid);
+
+ /* kill the old obfsproxy process */
+ tor_terminate_process(mp->pid);
+ mp->pid = 0;
+ fclose(mp->stdout);
+
+ /* destroy all its old transports. we no longer use them. */
+ SMARTLIST_FOREACH_BEGIN(mp->transports, const char *, t_name) {
+ t_tmp = transport_get_by_name(t_name);
+ if (t_tmp)
+ t_tmp->marked_for_removal = 1;
+ } SMARTLIST_FOREACH_END(t_name);
+ sweep_transport_list();
+
+ /* free the transport names in mp->transports */
+ SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));
+ smartlist_clear(mp->transports);
+
+ /* flag it as an infant proxy so that it gets launched on next tick */
+ mp->conf_state = PT_PROTO_INFANT;
+}
+
/** Launch managed proxy <b>mp</b>. */
static int
launch_managed_proxy(managed_proxy_t *mp)
{
char **envp=NULL;
- int retval;
+ int pid;
FILE *stdout_read = NULL;
int stdout_pipe=-1, stderr_pipe=-1;
/* prepare the environment variables for the managed proxy */
set_managed_proxy_environment(&envp, mp);
- retval = tor_spawn_background(mp->argv[0], &stdout_pipe,
- &stderr_pipe, (const char **)mp->argv,
- (const char **)envp);
- if (retval < 0) {
+ pid = tor_spawn_background(mp->argv[0], &stdout_pipe,
+ &stderr_pipe, (const char **)mp->argv,
+ (const char **)envp);
+ if (pid < 0) {
log_warn(LD_GENERAL, "Spawn failed");
return -1;
}
@@ -157,10 +221,11 @@ launch_managed_proxy(managed_proxy_t *mp)
/* Open the buffered IO streams */
stdout_read = fdopen(stdout_pipe, "r");
- log_warn(LD_CONFIG, "The spawn is alive (%d)!", retval);
+ log_warn(LD_CONFIG, "The spawn is alive (%d)!", pid);
mp->conf_state = PT_PROTO_LAUNCHED;
mp->stdout = stdout_read;
+ mp->pid = pid;
return 0;
}
@@ -171,12 +236,32 @@ launch_managed_proxy(managed_proxy_t *mp)
void
pt_configure_remaining_proxies(void)
{
- log_warn(LD_CONFIG, "We start configuring remaining managed proxies!");
- SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
- /* configured proxies shouldn't be in unconfigured_proxy_list. */
- tor_assert(!proxy_configuration_finished(mp));
+ log_warn(LD_CONFIG, "We start configuring remaining managed proxies (%d)!",
+ unconfigured_proxies_n);
+ SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
+ tor_assert(mp->conf_state != PT_PROTO_BROKEN);
+
+ if (mp->got_hup) {
+ mp->got_hup = 0;
+
+ /* This proxy is marked by a SIGHUP. Check whether we need to
+ restart it. */
+ if (proxy_needs_restart(mp)) {
+ proxy_prepare_for_restart(mp);
+ continue;
+ } else { /* it doesn't need to be restarted. */
+ printf("No need for restart; status quo\n");
+ unconfigured_proxies_n--;
+ tor_assert(unconfigured_proxies_n >= 0);
+ }
+
+ continue;
+ }
- configure_proxy(mp);
+ /* If the proxy is not fully configured, try to configure it
+ futher. */
+ if (!proxy_configuration_finished(mp))
+ configure_proxy(mp);
} SMARTLIST_FOREACH_END(mp);
}
@@ -222,33 +307,55 @@ configure_proxy(managed_proxy_t *mp)
/** Register server managed proxy <b>mp</b> transports to state */
static void
-register_server_proxy(const managed_proxy_t *mp)
+register_server_proxy(managed_proxy_t *mp)
{
- if (mp->is_server) {
- SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
- save_transport_to_state(t->name,&t->addr,t->port); /* pass tor_addr_t? */
- } SMARTLIST_FOREACH_END(t);
- }
+ smartlist_t *sm_tmp = smartlist_create();
+
+ tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
+ SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
+ save_transport_to_state(t->name,&t->addr,t->port); /* pass tor_addr_t? */
+ smartlist_add(sm_tmp, tor_strdup(t->name));
+ } SMARTLIST_FOREACH_END(t);
+
+ smartlist_free(mp->transports);
+ mp->transports = sm_tmp;
}
/** Register all the transports supported by client managed proxy
* <b>mp</b> to the bridge subsystem. */
static void
-register_client_proxy(const managed_proxy_t *mp)
+register_client_proxy(managed_proxy_t *mp)
{
+ int r;
+ smartlist_t *sm_tmp = smartlist_create();
+
+ tor_assert(mp->conf_state != PT_PROTO_COMPLETED);
SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
- if (transport_add(t)<0) {
+ r = transport_add(t);
+ switch (r) {
+ case -1:
log_warn(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
transport_free(t);
- } else {
+ break;
+ case 0:
log_warn(LD_GENERAL, "Succesfully registered transport %s", t->name);
+ smartlist_add(sm_tmp, tor_strdup(t->name));
+ break;
+ case 1:
+ log_warn(LD_GENERAL, "Succesfully registered transport %s", t->name);
+ smartlist_add(sm_tmp, tor_strdup(t->name));
+ transport_free(t);
+ break;
}
} SMARTLIST_FOREACH_END(t);
+
+ smartlist_free(mp->transports);
+ mp->transports = sm_tmp;
}
/** Register the transports of managed proxy <b>mp</b>. */
static INLINE void
-register_proxy(const managed_proxy_t *mp)
+register_proxy(managed_proxy_t *mp)
{
if (mp->is_server)
register_server_proxy(mp);
@@ -256,18 +363,17 @@ register_proxy(const managed_proxy_t *mp)
register_client_proxy(mp);
}
-/** Free memory allocated by managed proxy <b>mp</b>.
- * If <b>also_free_transports</b> is set, also free the transports
- * associated with this managed proxy. */
+/** Free memory allocated by managed proxy <b>mp</b>. */
static void
-managed_proxy_destroy_impl(managed_proxy_t *mp, int also_free_transports)
+managed_proxy_destroy(managed_proxy_t *mp)
{
- /* transport_free() all its transports */
- if (also_free_transports)
+ printf("Destroying mp %p\n", mp);
+ if (mp->conf_state != PT_PROTO_COMPLETED)
SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
+ else
+ SMARTLIST_FOREACH(mp->transports, char *, t_name, tor_free(t_name));
/* free the transports smartlist */
- smartlist_clear(mp->transports);
smartlist_free(mp->transports);
SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
@@ -277,31 +383,32 @@ managed_proxy_destroy_impl(managed_proxy_t *mp, int also_free_transports)
smartlist_free(mp->transports_to_launch);
/* remove it from the list of managed proxies */
- smartlist_remove(unconfigured_proxy_list, mp);
+ smartlist_remove(managed_proxy_list, mp);
/* close its stdout stream */
- fclose(mp->stdout);
+ if (mp->stdout)
+ fclose(mp->stdout);
/* free the argv */
free_execve_args(mp->argv);
+ if (mp->pid)
+ tor_terminate_process(mp->pid);
+
tor_free(mp);
}
-
/** Handle a configured or broken managed proxy <b>mp</b>. */
static void
handle_finished_proxy(managed_proxy_t *mp)
{
switch (mp->conf_state) {
case PT_PROTO_BROKEN: /* if broken: */
- managed_proxy_destroy_with_transports(mp); /* destroy it and all its transports */
+ managed_proxy_destroy(mp); /* annihilate it. */
break;
case PT_PROTO_CONFIGURED: /* if configured correctly: */
register_proxy(mp); /* register transports */
- mp->conf_state = PT_PROTO_COMPLETED; /* mark it as completed, */
- managed_proxy_destroy(mp); /* destroy the managed proxy struct,
- keeping the transports intact */
+ mp->conf_state = PT_PROTO_COMPLETED; /* mark it as completed. */
break;
default:
log_warn(LD_CONFIG, "Unfinished managed proxy in "
@@ -309,7 +416,8 @@ handle_finished_proxy(managed_proxy_t *mp)
tor_assert(0);
}
- tor_assert(smartlist_len(unconfigured_proxy_list) >= 0);
+ unconfigured_proxies_n--;
+ tor_assert(unconfigured_proxies_n >= 0);
}
/** Return true if the configuration of the managed proxy <b>mp</b> is
@@ -322,8 +430,7 @@ proxy_configuration_finished(const managed_proxy_t *mp)
}
-/** This function is called when a proxy sends an {S,C}METHODS DONE message,
- */
+/** This function is called when a proxy sends an {S,C}METHODS DONE message. */
static void
handle_methods_done(const managed_proxy_t *mp)
{
@@ -694,6 +801,30 @@ set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp)
tor_free(bindaddr);
}
+/** Create and return a new managed proxy for <b>transport</b> using
+ * <b>proxy_argv</b>. If <b>is_server</b> is true, it's a server
+ * managed proxy. */
+static managed_proxy_t *
+managed_proxy_create(const char *transport, char **proxy_argv, int is_server)
+{
+ managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
+ mp->conf_state = PT_PROTO_INFANT;
+ mp->is_server = is_server;
+ mp->argv = proxy_argv;
+ mp->transports = smartlist_create();
+
+ mp->transports_to_launch = smartlist_create();
+ add_transport_to_proxy(transport, mp);
+
+ /* register the managed proxy */
+ if (!managed_proxy_list)
+ managed_proxy_list = smartlist_create();
+ smartlist_add(managed_proxy_list, mp);
+ unconfigured_proxies_n++;
+
+ return mp;
+}
+
/** Register <b>transport</b> using proxy with <b>proxy_argv</b> to
* the managed proxy subsystem.
* If <b>is_server</b> is true, then the proxy is a server proxy. */
@@ -705,21 +836,28 @@ pt_kickstart_proxy(const char *transport, char **proxy_argv, int is_server)
mp = get_managed_proxy_by_argv(proxy_argv);
if (!mp) { /* we haven't seen this proxy before */
- /* create a managed proxy */
- managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
- mp->conf_state = PT_PROTO_INFANT;
- mp->is_server = is_server;
- mp->argv = proxy_argv;
- mp->transports = smartlist_create();
-
- mp->transports_to_launch = smartlist_create();
- add_transport_to_proxy(transport, mp);
+ managed_proxy_create(transport, proxy_argv, is_server);
+
+ } else { /* known proxy. add its transport to its transport list */
+ if (mp->got_hup) {
+ /* If the managed proxy we found is marked by a SIGHUP, it means
+ that it's not useless and should be kept. If it's marked for
+ removal, unmark it and increase the unconfigured proxies so
+ that we try to restart it if we need to. Afterwards, check if
+ a transport_t for 'transport' used to exist before the SIGHUP
+ and make sure it doesn't get deleted because we might reuse
+ it. */
+ if (mp->marked_for_removal) {
+ mp->marked_for_removal = 0;
+ unconfigured_proxies_n++;
+ }
+
+ transport_t *old_transport = NULL;
+ old_transport = transport_get_by_name(transport);
+ if (old_transport)
+ old_transport->marked_for_removal = 0;
+ }
- /* register the managed proxy */
- if (!unconfigured_proxy_list)
- unconfigured_proxy_list = smartlist_create();
- smartlist_add(unconfigured_proxy_list, mp);
- } else { /* known proxy. just add transport to its transport list */
add_transport_to_proxy(transport, mp);
free_execve_args(proxy_argv);
}
@@ -738,25 +876,66 @@ free_execve_args(char **arg)
tor_free(arg);
}
+
+/** Tor will read its config, prepare the managed proxy list so that
+ * proxies that are not used in the new config will shutdown, and
+ * proxies that need to spawn more transports will do so. */
+void
+pt_prepare_proxy_list_for_config_read(void)
+{
+ if (!managed_proxy_list)
+ return;
+
+ SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
+ /* Destroy unconfigured proxies. */
+ if (mp->conf_state != PT_PROTO_COMPLETED) {
+ managed_proxy_destroy(mp);
+ unconfigured_proxies_n--;
+ continue;
+ }
+
+ tor_assert(mp->conf_state == PT_PROTO_COMPLETED);
+
+ mp->marked_for_removal = 1;
+ mp->got_hup = 1;
+ SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
+ smartlist_clear(mp->transports_to_launch);
+ } SMARTLIST_FOREACH_END(mp);
+
+ tor_assert(unconfigured_proxies_n == 0);
+}
+
+/** The tor config was read, destroy all managed proxies that were
+ * marked by a previous call to prepare_proxy_list_for_config_read()
+ * and are not used by the new config. */
+void
+sweep_proxy_list(void)
+{
+ if (!managed_proxy_list)
+ return;
+
+ SMARTLIST_FOREACH_BEGIN(managed_proxy_list, managed_proxy_t *, mp) {
+ if (mp->marked_for_removal) {
+ SMARTLIST_DEL_CURRENT(managed_proxy_list, mp);
+ managed_proxy_destroy(mp);
+ }
+ } SMARTLIST_FOREACH_END(mp);
+}
+
/** Release all storage held by the pluggable transports subsystem. */
void
pt_free_all(void)
{
- if (unconfigured_proxy_list) {
+ if (managed_proxy_list) {
/* If the proxy is in PT_PROTO_COMPLETED, it has registered its
transports and it's the duty of the circuitbuild.c subsystem to
free them. Otherwise, it hasn't registered its transports yet
and we should free them here. */
- SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
- if (mp->conf_state == PT_PROTO_COMPLETED)
- managed_proxy_destroy(mp);
- else
- managed_proxy_destroy_with_transports(mp);
- } SMARTLIST_FOREACH_END(mp);
+ SMARTLIST_FOREACH(managed_proxy_list, managed_proxy_t *, mp,
+ managed_proxy_destroy(mp));
- smartlist_clear(unconfigured_proxy_list);
- smartlist_free(unconfigured_proxy_list);
- unconfigured_proxy_list=NULL;
+ smartlist_free(managed_proxy_list);
+ managed_proxy_list=NULL;
}
}
diff --git a/src/or/transports.h b/src/or/transports.h
index 6fec1dc..48b7839 100644
--- a/src/or/transports.h
+++ b/src/or/transports.h
@@ -25,6 +25,9 @@ int pt_proxies_configuration_pending(void);
void pt_free_all(void);
+void pt_prepare_proxy_list_for_config_read(void);
+void sweep_proxy_list(void);
+
#ifdef PT_PRIVATE
/** State of the managed proxy configuration protocol. */
enum pt_proto_state {
@@ -47,8 +50,45 @@ typedef struct {
FILE *stdout; /* a stream to its stdout
(closed in managed_proxy_destroy()) */
+ int pid; /* The Process ID this managed proxy is using. */
+
+ /** Boolean: We are re-parsing our config, and we are going to
+ * remove this managed proxy if we don't find it any transport
+ * plugins that use it. */
+ unsigned int marked_for_removal : 1;
+
+ /** Boolean: We got a SIGHUP while this proxy was running. We use
+ * this flag to signify that this proxy might need to be restarted
+ * so that it can listen for other transports according to the new
+ * torrc. */
+ unsigned int got_hup : 1;
+
smartlist_t *transports_to_launch; /* transports to-be-launched by this proxy */
- smartlist_t *transports; /* list of transport_t this proxy spawned */
+
+ /* The 'transports' list contains all the transports this proxy has
+ launched.
+
+ Before a managed_proxy_t reaches the PT_PROTO_COMPLETED phase,
+ this smartlist contains a 'transport_t' for every transport it
+ has launched.
+
+ When the managed_proxy_t reaches the PT_PROTO_COMPLETED phase, it
+ registers all its transports to the circuitbuild.c subsystem. At
+ that point the 'transport_t's are owned by the circuitbuild.c
+ subsystem.
+
+ To avoid carrying dangling 'transport_t's in this smartlist,
+ right before the managed_proxy_t reaches the PT_PROTO_COMPLETED
+ phase we replace all 'transport_t's with strings of their
+ transport names.
+
+ So, tl;dr:
+ When (conf_state != PT_PROTO_COMPLETED) this list carries
+ (transport_t *).
+ When (conf_state == PT_PROTO_COMPLETED) this list carries
+ (char *).
+ */
+ smartlist_t *transports;
} managed_proxy_t;
int parse_cmethod_line(const char *line, managed_proxy_t *mp);
1
0
[tor/master] Renamed pluggable_transports.[ch] to transports.[ch].
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit d8c04c7ea52bc1ffbeec60d614970eccab3c9b4f
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Mon Jul 18 02:19:38 2011 +0200
Renamed pluggable_transports.[ch] to transports.[ch].
---
src/or/pluggable_transports.c | 613 -----------------------------------------
src/or/pluggable_transports.h | 59 ----
src/or/transports.c | 613 +++++++++++++++++++++++++++++++++++++++++
src/or/transports.h | 59 ++++
4 files changed, 672 insertions(+), 672 deletions(-)
diff --git a/src/or/pluggable_transports.c b/src/or/pluggable_transports.c
deleted file mode 100644
index 49b0e13..0000000
--- a/src/or/pluggable_transports.c
+++ /dev/null
@@ -1,613 +0,0 @@
-/* Copyright (c) 2011, The Tor Project, Inc. */
-/* See LICENSE for licensing information */
-
-/**
- * \file pluggable_transports.c
- * \brief Pluggable Transports related code.
- **/
-
-#define PT_PRIVATE
-#include "or.h"
-#include "config.h"
-#include "circuitbuild.h"
-#include "pluggable_transports.h"
-
-/* ASN TIDY THESE UP*/
-static void set_environ(char ***envp, const char *method,
- int is_server);
-static INLINE int proxy_configuration_finished(managed_proxy_t *mp);
-
-static void managed_proxy_destroy(managed_proxy_t *mp,
- int also_free_transports);
-static void register_proxy_transports(managed_proxy_t *mp);
-static void handle_finished_proxy(managed_proxy_t *mp);
-static void configure_proxy(managed_proxy_t *mp);
-
-static void parse_method_error(char *line, int is_server_method);
-#define parse_server_method_error(l) parse_method_error(l, 1)
-#define parse_client_method_error(l) parse_method_error(l, 0)
-
-static INLINE void free_execve_args(char **arg);
-
-/** Managed proxy protocol strings */
-#define PROTO_ENV_ERROR "ENV-ERROR"
-#define PROTO_NEG_SUCCESS "VERSION"
-#define PROTO_NEG_FAIL "VERSION-ERROR no-version"
-#define PROTO_CMETHOD "CMETHOD"
-#define PROTO_SMETHOD "SMETHOD"
-#define PROTO_CMETHOD_ERROR "CMETHOD-ERROR"
-#define PROTO_SMETHOD_ERROR "SMETHOD-ERROR"
-#define PROTO_CMETHODS_DONE "CMETHODS DONE"
-#define PROTO_SMETHODS_DONE "SMETHODS DONE"
-
-/* The smallest valid managed proxy protocol line that can
- appear. It's the size of "VERSION 1" */
-#define SMALLEST_MANAGED_LINE_SIZE 9
-
-/** Number of environment variables for managed proxy clients/servers. */
-#define ENVIRON_SIZE_CLIENT 5
-#define ENVIRON_SIZE_SERVER 8
-
-/** The first and only supported - at the moment - configuration
- protocol version. */
-#define PROTO_VERSION_ONE 1
-
-/** List of unconfigured managed proxies. */
-static smartlist_t *unconfigured_proxy_list = NULL;
-/** Number of unconfigured managed proxies. */
-static int n_unconfigured_proxies = 0;
-
-/* The main idea is:
-
- A managed proxy is represented by a managed_proxy_t struct and can
- spawn multiple transports.
-
- unconfigured_proxy_list is a list of all the unconfigured managed
- proxies; everytime we spawn a managed proxy we add it in that
- list.
- In every run_scheduled_event() tick, we attempt to configure each
- managed proxy further, using the configuration protocol defined in
- the 180_pluggable_transport.txt proposal.
-
- When a managed proxy is fully configured, we register all its
- transports to the circuitbuild.c subsystem - like we do with
- external proxies - and then free the managed proxy struct
- since it's no longer needed. */
-
-/** Return true if there are still unconfigured managed proxies. */
-int
-pt_proxies_configuration_pending(void)
-{
- return !!n_unconfigured_proxies;
-}
-
-/** Launch a proxy for <b>method</b> using <b>proxy_argv</b> as its
- * arguments. If <b>is_server</b>, launch a server proxy. */
-int
-pt_managed_launch_proxy(const char *method,
- char **proxy_argv, int is_server)
-{
- char **envp=NULL;
- int retval;
- FILE *stdout_read = NULL;
- int stdout_pipe=-1, stderr_pipe=-1;
-
- /* prepare the environment variables for the managed proxy */
- set_environ(&envp, method, is_server);
-
- /* ASN we should probably check if proxy_argv[0] is executable by our user */
- retval = tor_spawn_background(proxy_argv[0], &stdout_pipe,
- &stderr_pipe, (const char **)proxy_argv,
- (const char **)envp);
- if (retval < 0) {
- log_warn(LD_GENERAL, "Spawn failed");
- return -1;
- }
-
- /* free the memory allocated for the execve() */
- free_execve_args(envp);
- free_execve_args(proxy_argv);
-
- /* Set stdout/stderr pipes to be non-blocking */
- fcntl(stdout_pipe, F_SETFL, O_NONBLOCK);
- /* Open the buffered IO streams */
- stdout_read = fdopen(stdout_pipe, "r");
-
- log_warn(LD_CONFIG, "The spawn is alive (%d)!", retval);
-
- /* create a managed proxy */
- managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
- mp->conf_state = PT_PROTO_INFANT;
- mp->stdout = stdout_read;
- mp->transports = smartlist_create();
-
- /* register the managed proxy */
- if (!unconfigured_proxy_list)
- unconfigured_proxy_list = smartlist_create();
- smartlist_add(unconfigured_proxy_list, mp);
-
- n_unconfigured_proxies++; /* ASN should we care about overflows here?
- I say no. */
-
- return 0;
-}
-
-/** Check if any of the managed proxies we are currently trying to
- * configure have anything new to say. This is called from
- * run_scheduled_events(). */
-void
-pt_configure_remaining_proxies(void)
-{
- log_warn(LD_CONFIG, "We start configuring remaining managed proxies!");
- SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
- if (proxy_configuration_finished(mp)) /* finished managed proxies
- shouldn't be here */
- assert(0);
-
- configure_proxy(mp);
-
- } SMARTLIST_FOREACH_END(mp);
-}
-
-/** Receive input from the managed proxy <b>mp</b> to get closer to
- * finally configuring it. */
-static void
-configure_proxy(managed_proxy_t *mp)
-{
- enum stream_status r;
- char stdout_buf[200];
-
- while (1) {
- memset(stdout_buf, 0, sizeof(stdout_buf));
-
- r = get_string_from_pipe(mp->stdout, stdout_buf,
- sizeof(stdout_buf) - 1);
-
- if (r == ST_CLOSED || r == ST_TERM) {
- log_warn(LD_GENERAL, "Managed proxy stream closed. "
- "Most probably application stopped running");
- mp->conf_state = PT_PROTO_BROKEN;
- } else if (r == ST_EAGAIN) {
- return;
- } else {
- tor_assert(r == ST_OKAY);
- handle_proxy_line(stdout_buf, mp);
- }
-
- /* if the proxy finished configuring, exit the loop. */
- if (proxy_configuration_finished(mp)) {
- handle_finished_proxy(mp);
- return;
- }
- }
-}
-
-/** Handle a configured or broken managed proxy <b>mp</b>. */
-static void
-handle_finished_proxy(managed_proxy_t *mp)
-{
- switch (mp->conf_state) {
- case PT_PROTO_BROKEN: /* if broken: */
- managed_proxy_destroy(mp, 1); /* destroy it and all its transports */
- break;
- case PT_PROTO_CONFIGURED: /* if configured correctly: */
- register_proxy_transports(mp); /* register all its transports, */
- mp->conf_state = PT_PROTO_COMPLETED; /* mark it as completed, */
- managed_proxy_destroy(mp, 0); /* destroy the managed proxy struct,
- keeping the transports intact */
- break;
- default:
- log_warn(LD_CONFIG, "Unfinished managed proxy in "
- "handle_finished_proxy().");
- assert(0);
- }
-
- n_unconfigured_proxies--;
- tor_assert(n_unconfigured_proxies >= 0);
-}
-
-/** Register all the transports supported by managed proxy <b>mp</b>. */
-static void
-register_proxy_transports(managed_proxy_t *mp)
-{
- SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
- if (transport_add(t)<0) {
- log_warn(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
- transport_free(t);
- } else {
- log_warn(LD_GENERAL, "Succesfully registered transport %s", t->name);
- }
- } SMARTLIST_FOREACH_END(t);
-}
-
-/** Free memory allocated by managed proxy <b>mp</b>.
- * If <b>also_free_transports</b> is set, also free the transports
- * associated with this managed proxy. */
-static void
-managed_proxy_destroy(managed_proxy_t *mp, int also_free_transports)
-{
- /* transport_free() all its transports */
- if (also_free_transports)
- SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
-
- /* free the transports smartlist */
- smartlist_clear(mp->transports);
- smartlist_free(mp->transports);
-
- /* remove it from the list of managed proxies */
- smartlist_remove(unconfigured_proxy_list, mp);
-
- /* close its stdout stream */
- fclose(mp->stdout);
-
- tor_free(mp);
-}
-
-/** Return true if the configuration of the managed proxy <b>mp</b> is
- finished. */
-static INLINE int
-proxy_configuration_finished(managed_proxy_t *mp)
-{
- return (mp->conf_state == PT_PROTO_CONFIGURED ||
- mp->conf_state == PT_PROTO_BROKEN);
-}
-
-/** Handle a configuration protocol <b>line</b> received from a
- * managed proxy <b>mp</b>. */
-void
-handle_proxy_line(char *line, managed_proxy_t *mp)
-{
- printf("Judging line: %s\n", line);
-
- if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
- log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
- "Discarding");
- goto err;
- }
-
- if (!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR))) {
- if (mp->conf_state != PT_PROTO_INFANT)
- goto err;
-
- parse_env_error(line);
- goto err;
- } else if (!strncmp(line, PROTO_NEG_FAIL, strlen(PROTO_NEG_FAIL))) {
- if (mp->conf_state != PT_PROTO_INFANT)
- goto err;
-
- log_warn(LD_CONFIG, "Managed proxy could not pick a "
- "configuration protocol version.");
- goto err;
- } else if (!strncmp(line, PROTO_NEG_SUCCESS,
- strlen(PROTO_NEG_SUCCESS))) {
- if (mp->conf_state != PT_PROTO_INFANT)
- goto err;
-
- if (parse_version(line,mp) < 0)
- goto err;
-
- tor_assert(mp->conf_protocol != 0);
- mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
- return;
- } else if (!strncmp(line, PROTO_CMETHODS_DONE,
- strlen(PROTO_CMETHODS_DONE))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- log_warn(LD_CONFIG, "Client managed proxy configuration completed!");
- mp->conf_state = PT_PROTO_CONFIGURED;
- return;
- } else if (!strncmp(line, PROTO_SMETHODS_DONE,
- strlen(PROTO_SMETHODS_DONE))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- log_warn(LD_CONFIG, "Server managed proxy configuration completed!");
- mp->conf_state = PT_PROTO_CONFIGURED;
- return;
- } else if (!strncmp(line, PROTO_CMETHOD_ERROR,
- strlen(PROTO_CMETHOD_ERROR))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- parse_client_method_error(line);
- goto err;
- } else if (!strncmp(line, PROTO_SMETHOD_ERROR,
- strlen(PROTO_SMETHOD_ERROR))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- parse_server_method_error(line);
- goto err;
- } else if (!strncmp(line, PROTO_CMETHOD, strlen(PROTO_CMETHOD))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- if (parse_cmethod_line(line, mp) < 0)
- goto err;
-
- return;
- } else if (!strncmp(line, PROTO_SMETHOD, strlen(PROTO_SMETHOD))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- if (parse_smethod_line(line, mp) < 0)
- goto err;
-
- return;
- }
-
- log_warn(LD_CONFIG, "Unknown line received by managed proxy. (%s)", line);
-
- err:
- mp->conf_state = PT_PROTO_BROKEN;
- return;
-}
-
-/** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
-void
-parse_env_error(char *line)
-{
- tor_assert(!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR)));
-
- /* (Length of the protocol string) plus (a space) and (the first char of
- the error message) */
- if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
- log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
- "message.", PROTO_ENV_ERROR);
-
- log_warn(LD_CONFIG, "Managed proxy couldn't understand the "
- "pluggable transport environment variables. (%s)",
- line+strlen(PROTO_ENV_ERROR)+1);
-}
-
-/** Handles a VERSION <b>line</b>. Updates the configuration protocol
- * version in <b>mp</b>. */
-int
-parse_version(char *line, managed_proxy_t *mp)
-{
- tor_assert(!strncmp(line, PROTO_NEG_SUCCESS, strlen(PROTO_NEG_SUCCESS)));
-
- if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
- log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
- PROTO_NEG_SUCCESS);
- return -1;
- }
-
- if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) {
- log_warn(LD_CONFIG, "We don't support version '%s'. "
- "We only support version '1'", line+strlen(PROTO_NEG_SUCCESS)+1);
- return -1;
- }
-
- mp->conf_protocol = PROTO_VERSION_ONE; /* temp. till more versions appear */
- return 0;
-}
-
-/** Parses {C,S}METHOD-ERROR <b>line</b> and warns the user
- * accordingly. If <b>is_server</b> it is an SMETHOD-ERROR,
- * otherwise it is a CMETHOD-ERROR. */
-static void
-parse_method_error(char *line, int is_server)
-{
- const char* error = is_server ?
- PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;
-
- /* (Length of the protocol string) plus (a space) and (the first char of
- the error message) */
- if (strlen(line) < (strlen(error) + 2))
- log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
- "message.", error);
-
- log_warn(LD_CONFIG, "%s managed proxy encountered a method error. (%s)",
- is_server ? "Server" : "Client",
- line+strlen(error)+1);
-}
-
-/** Parses an SMETHOD <b>line</b>. */
-int
-parse_smethod_line(char *line, managed_proxy_t *mp)
-{
- int r;
- smartlist_t *items = NULL;
-
- char *method_name=NULL;
-
- char *addrport=NULL;
- tor_addr_t addr;
- uint16_t port = 0;
-
- items = smartlist_create();
- smartlist_split_string(items, line, NULL,
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
- if (smartlist_len(items) < 3) {
- log_warn(LD_CONFIG, "Server managed proxy sent us a SMETHOD line "
- "with too few arguments.");
- goto err;
- }
-
- tor_assert(!strcmp(smartlist_get(items,0),PROTO_SMETHOD));
-
- method_name = smartlist_get(items,1);
-
- addrport = smartlist_get(items, 2);
- if (tor_addr_port_parse(addrport, &addr, &port)<0) {
- log_warn(LD_CONFIG, "Error parsing transport "
- "address '%s'", addrport);
- goto err;
- }
-
- if (!port) {
- log_warn(LD_CONFIG,
- "Transport address '%s' has no port.", addrport);
- goto err;
- }
-
- /* For now, notify the user so that he knows where the server
- transport is listening. */
- log_warn(LD_CONFIG, "Server transport %s at %s:%d.",
- method_name, fmt_addr(&addr), (int)port);
-
- r=0;
- goto done;
-
- err:
- r = -1;
-
- done:
- SMARTLIST_FOREACH(items, char*, s, tor_free(s));
- smartlist_free(items);
- return r;
-}
-
-/** Parses a CMETHOD <b>line</b>, and if well-formed it registers
- * the new transport in <b>mp</b>. */
-int
-parse_cmethod_line(char *line, managed_proxy_t *mp)
-{
- int r;
- smartlist_t *items = NULL;
-
- char *method_name=NULL;
-
- char *socks_ver_str=NULL;
- int socks_ver=PROXY_NONE;
-
- char *addrport=NULL;
- tor_addr_t addr;
- uint16_t port = 0;
-
- transport_t *transport=NULL;
-
- items = smartlist_create();
- smartlist_split_string(items, line, NULL,
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
- if (smartlist_len(items) < 4) {
- log_warn(LD_CONFIG, "Client managed proxy sent us a CMETHOD line "
- "with too few arguments.");
- goto err;
- }
-
- tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));
-
- method_name = smartlist_get(items,1);
-
- socks_ver_str = smartlist_get(items,2);
-
- if (!strcmp(socks_ver_str,"socks4")) {
- socks_ver = PROXY_SOCKS4;
- } else if (!strcmp(socks_ver_str,"socks5")) {
- socks_ver = PROXY_SOCKS5;
- } else {
- log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
- "we don't recognize. (%s)", socks_ver_str);
- goto err;
- }
-
- addrport = smartlist_get(items, 3);
- if (tor_addr_port_parse(addrport, &addr, &port)<0) {
- log_warn(LD_CONFIG, "Error parsing transport "
- "address '%s'", addrport);
- goto err;
- }
-
- if (!port) {
- log_warn(LD_CONFIG,
- "Transport address '%s' has no port.", addrport);
- goto err;
- }
-
- transport = transport_create(&addr, port, method_name, socks_ver);
- if (!transport)
- goto err;
-
- smartlist_add(mp->transports, transport);
-
- log_warn(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
- "Attached to managed proxy.",
- method_name, fmt_addr(&addr), (int)port, socks_ver);
-
- r=0;
- goto done;
-
- err:
- r = -1;
-
- done:
- SMARTLIST_FOREACH(items, char*, s, tor_free(s));
- smartlist_free(items);
- return r;
-}
-
-/** Prepares the <b>envp</b> of a pluggable transport managed proxy
- *
- * <b>method</b> is a line with transport methods to be launched.
- * If <b>is_server</b> is set, prepare a server proxy <b>envp</b>. */
-static void
-set_environ(char ***envp, const char *method, int is_server)
-{
- or_options_t *options = get_options();
- char **tmp=NULL;
- char *state_loc=NULL;
-
- int n_envs = is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
-
- /* allocate enough space for our env. vars and a NULL pointer */
- *envp = tor_malloc(sizeof(char*)*(n_envs+1));
- tmp = *envp;
-
- /* these should all be customizable */
- tor_asprintf(tmp++, "HOME=%s", getenv("HOME"));
- tor_asprintf(tmp++, "PATH=%s", getenv("PATH"));
- state_loc = get_datadir_fname("pt_state/");
- tor_asprintf(tmp++, "TOR_PT_STATE_LOCATION=%s", state_loc);
- tor_free(state_loc);
- tor_asprintf(tmp++, "TOR_PT_MANAGED_TRANSPORT_VER=1"); /* temp */
- if (is_server) {
- /* ASN check for ORPort values, should we be here if it's 0? */
- tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort); /* temp */
- tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=127.0.0.1:0");
- tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", method);
- tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200"); /* temp*/
- } else {
- tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", method);
- }
- *tmp = NULL;
-}
-
-/* ASN is this too ugly/stupid? */
-/** Frees the array of pointers in <b>arg</b> used as arguments to
- execve. */
-static INLINE void
-free_execve_args(char **arg)
-{
- char **tmp = arg;
- while (*tmp) /* use the fact that the last element of the array is a
- NULL pointer to know when to stop freeing */
- _tor_free(*tmp++);
-
- tor_free(arg);
-}
-
-/** Release all storage held by the pluggable transports subsystem. */
-void
-pt_free_all(void)
-{
- if (unconfigured_proxy_list) {
- /* If the proxy is in PT_PROTO_COMPLETED, it has registered its
- transports and it's the duty of the circuitbuild.c subsystem to
- free them. Otherwise, it hasn't registered its transports yet
- and we should free them here. */
- SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
- if (mp->conf_state == PT_PROTO_COMPLETED)
- managed_proxy_destroy(mp,0);
- else
- managed_proxy_destroy(mp,1);
- } SMARTLIST_FOREACH_END(mp);
-
- smartlist_clear(unconfigured_proxy_list);
- smartlist_free(unconfigured_proxy_list);
- unconfigured_proxy_list=NULL;
- }
-}
-
diff --git a/src/or/pluggable_transports.h b/src/or/pluggable_transports.h
deleted file mode 100644
index 80d5429..0000000
--- a/src/or/pluggable_transports.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/* Copyright (c) 2003-2004, Roger Dingledine
- * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2011, The Tor Project, Inc. */
-/* See LICENSE for licensing information */
-
-/**
- * \file pluggable_transpots.h
- * \brief Headers for pluggable_transpots.c
- **/
-
-#ifndef TOR_PLUGGABLE_TRANSPORTS_H
-#define TOR_PLUGGABLE_TRANSPORTS_H
-
-int pt_managed_launch_proxy(const char *method,
- char **proxy_argv, int is_server);
-
-#define pt_managed_launch_client_proxy(m, pa) \
- pt_managed_launch_proxy(m, pa, 0)
-#define pt_managed_launch_server_proxy(m, pa) \
- pt_managed_launch_proxy(m, pa, 1)
-
-void pt_configure_remaining_proxies(void);
-
-int pt_proxies_configuration_pending(void);
-
-void pt_free_all(void);
-
-#ifdef PT_PRIVATE
-/** State of the managed proxy configuration protocol. */
-enum pt_proto_state {
- PT_PROTO_INFANT, /* was just born */
- PT_PROTO_ACCEPTING_METHODS, /* accepting methods */
- PT_PROTO_CONFIGURED, /* configured successfully */
- PT_PROTO_COMPLETED, /* configure and registered its transports */
- PT_PROTO_BROKEN
-};
-
-/** Structure containing information of a managed proxy. */
-typedef struct {
- enum pt_proto_state conf_state; /* the current configuration state */
- int conf_protocol; /* the configuration protocol version used */
-
- FILE *stdout; /* a stream to its stdout
- (closed in managed_proxy_destroy()) */
-
- smartlist_t *transports; /* list of transports this proxy spawns */
-} managed_proxy_t;
-
-int parse_cmethod_line(char *line, managed_proxy_t *mp);
-int parse_smethod_line(char *line, managed_proxy_t *mp);
-
-int parse_version(char *line, managed_proxy_t *mp);
-void parse_env_error(char *line);
-void handle_proxy_line(char *line, managed_proxy_t *mp);
-
-#endif
-
-#endif
-
diff --git a/src/or/transports.c b/src/or/transports.c
new file mode 100644
index 0000000..49b0e13
--- /dev/null
+++ b/src/or/transports.c
@@ -0,0 +1,613 @@
+/* Copyright (c) 2011, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file pluggable_transports.c
+ * \brief Pluggable Transports related code.
+ **/
+
+#define PT_PRIVATE
+#include "or.h"
+#include "config.h"
+#include "circuitbuild.h"
+#include "pluggable_transports.h"
+
+/* ASN TIDY THESE UP*/
+static void set_environ(char ***envp, const char *method,
+ int is_server);
+static INLINE int proxy_configuration_finished(managed_proxy_t *mp);
+
+static void managed_proxy_destroy(managed_proxy_t *mp,
+ int also_free_transports);
+static void register_proxy_transports(managed_proxy_t *mp);
+static void handle_finished_proxy(managed_proxy_t *mp);
+static void configure_proxy(managed_proxy_t *mp);
+
+static void parse_method_error(char *line, int is_server_method);
+#define parse_server_method_error(l) parse_method_error(l, 1)
+#define parse_client_method_error(l) parse_method_error(l, 0)
+
+static INLINE void free_execve_args(char **arg);
+
+/** Managed proxy protocol strings */
+#define PROTO_ENV_ERROR "ENV-ERROR"
+#define PROTO_NEG_SUCCESS "VERSION"
+#define PROTO_NEG_FAIL "VERSION-ERROR no-version"
+#define PROTO_CMETHOD "CMETHOD"
+#define PROTO_SMETHOD "SMETHOD"
+#define PROTO_CMETHOD_ERROR "CMETHOD-ERROR"
+#define PROTO_SMETHOD_ERROR "SMETHOD-ERROR"
+#define PROTO_CMETHODS_DONE "CMETHODS DONE"
+#define PROTO_SMETHODS_DONE "SMETHODS DONE"
+
+/* The smallest valid managed proxy protocol line that can
+ appear. It's the size of "VERSION 1" */
+#define SMALLEST_MANAGED_LINE_SIZE 9
+
+/** Number of environment variables for managed proxy clients/servers. */
+#define ENVIRON_SIZE_CLIENT 5
+#define ENVIRON_SIZE_SERVER 8
+
+/** The first and only supported - at the moment - configuration
+ protocol version. */
+#define PROTO_VERSION_ONE 1
+
+/** List of unconfigured managed proxies. */
+static smartlist_t *unconfigured_proxy_list = NULL;
+/** Number of unconfigured managed proxies. */
+static int n_unconfigured_proxies = 0;
+
+/* The main idea is:
+
+ A managed proxy is represented by a managed_proxy_t struct and can
+ spawn multiple transports.
+
+ unconfigured_proxy_list is a list of all the unconfigured managed
+ proxies; everytime we spawn a managed proxy we add it in that
+ list.
+ In every run_scheduled_event() tick, we attempt to configure each
+ managed proxy further, using the configuration protocol defined in
+ the 180_pluggable_transport.txt proposal.
+
+ When a managed proxy is fully configured, we register all its
+ transports to the circuitbuild.c subsystem - like we do with
+ external proxies - and then free the managed proxy struct
+ since it's no longer needed. */
+
+/** Return true if there are still unconfigured managed proxies. */
+int
+pt_proxies_configuration_pending(void)
+{
+ return !!n_unconfigured_proxies;
+}
+
+/** Launch a proxy for <b>method</b> using <b>proxy_argv</b> as its
+ * arguments. If <b>is_server</b>, launch a server proxy. */
+int
+pt_managed_launch_proxy(const char *method,
+ char **proxy_argv, int is_server)
+{
+ char **envp=NULL;
+ int retval;
+ FILE *stdout_read = NULL;
+ int stdout_pipe=-1, stderr_pipe=-1;
+
+ /* prepare the environment variables for the managed proxy */
+ set_environ(&envp, method, is_server);
+
+ /* ASN we should probably check if proxy_argv[0] is executable by our user */
+ retval = tor_spawn_background(proxy_argv[0], &stdout_pipe,
+ &stderr_pipe, (const char **)proxy_argv,
+ (const char **)envp);
+ if (retval < 0) {
+ log_warn(LD_GENERAL, "Spawn failed");
+ return -1;
+ }
+
+ /* free the memory allocated for the execve() */
+ free_execve_args(envp);
+ free_execve_args(proxy_argv);
+
+ /* Set stdout/stderr pipes to be non-blocking */
+ fcntl(stdout_pipe, F_SETFL, O_NONBLOCK);
+ /* Open the buffered IO streams */
+ stdout_read = fdopen(stdout_pipe, "r");
+
+ log_warn(LD_CONFIG, "The spawn is alive (%d)!", retval);
+
+ /* create a managed proxy */
+ managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
+ mp->conf_state = PT_PROTO_INFANT;
+ mp->stdout = stdout_read;
+ mp->transports = smartlist_create();
+
+ /* register the managed proxy */
+ if (!unconfigured_proxy_list)
+ unconfigured_proxy_list = smartlist_create();
+ smartlist_add(unconfigured_proxy_list, mp);
+
+ n_unconfigured_proxies++; /* ASN should we care about overflows here?
+ I say no. */
+
+ return 0;
+}
+
+/** Check if any of the managed proxies we are currently trying to
+ * configure have anything new to say. This is called from
+ * run_scheduled_events(). */
+void
+pt_configure_remaining_proxies(void)
+{
+ log_warn(LD_CONFIG, "We start configuring remaining managed proxies!");
+ SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
+ if (proxy_configuration_finished(mp)) /* finished managed proxies
+ shouldn't be here */
+ assert(0);
+
+ configure_proxy(mp);
+
+ } SMARTLIST_FOREACH_END(mp);
+}
+
+/** Receive input from the managed proxy <b>mp</b> to get closer to
+ * finally configuring it. */
+static void
+configure_proxy(managed_proxy_t *mp)
+{
+ enum stream_status r;
+ char stdout_buf[200];
+
+ while (1) {
+ memset(stdout_buf, 0, sizeof(stdout_buf));
+
+ r = get_string_from_pipe(mp->stdout, stdout_buf,
+ sizeof(stdout_buf) - 1);
+
+ if (r == ST_CLOSED || r == ST_TERM) {
+ log_warn(LD_GENERAL, "Managed proxy stream closed. "
+ "Most probably application stopped running");
+ mp->conf_state = PT_PROTO_BROKEN;
+ } else if (r == ST_EAGAIN) {
+ return;
+ } else {
+ tor_assert(r == ST_OKAY);
+ handle_proxy_line(stdout_buf, mp);
+ }
+
+ /* if the proxy finished configuring, exit the loop. */
+ if (proxy_configuration_finished(mp)) {
+ handle_finished_proxy(mp);
+ return;
+ }
+ }
+}
+
+/** Handle a configured or broken managed proxy <b>mp</b>. */
+static void
+handle_finished_proxy(managed_proxy_t *mp)
+{
+ switch (mp->conf_state) {
+ case PT_PROTO_BROKEN: /* if broken: */
+ managed_proxy_destroy(mp, 1); /* destroy it and all its transports */
+ break;
+ case PT_PROTO_CONFIGURED: /* if configured correctly: */
+ register_proxy_transports(mp); /* register all its transports, */
+ mp->conf_state = PT_PROTO_COMPLETED; /* mark it as completed, */
+ managed_proxy_destroy(mp, 0); /* destroy the managed proxy struct,
+ keeping the transports intact */
+ break;
+ default:
+ log_warn(LD_CONFIG, "Unfinished managed proxy in "
+ "handle_finished_proxy().");
+ assert(0);
+ }
+
+ n_unconfigured_proxies--;
+ tor_assert(n_unconfigured_proxies >= 0);
+}
+
+/** Register all the transports supported by managed proxy <b>mp</b>. */
+static void
+register_proxy_transports(managed_proxy_t *mp)
+{
+ SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
+ if (transport_add(t)<0) {
+ log_warn(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
+ transport_free(t);
+ } else {
+ log_warn(LD_GENERAL, "Succesfully registered transport %s", t->name);
+ }
+ } SMARTLIST_FOREACH_END(t);
+}
+
+/** Free memory allocated by managed proxy <b>mp</b>.
+ * If <b>also_free_transports</b> is set, also free the transports
+ * associated with this managed proxy. */
+static void
+managed_proxy_destroy(managed_proxy_t *mp, int also_free_transports)
+{
+ /* transport_free() all its transports */
+ if (also_free_transports)
+ SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
+
+ /* free the transports smartlist */
+ smartlist_clear(mp->transports);
+ smartlist_free(mp->transports);
+
+ /* remove it from the list of managed proxies */
+ smartlist_remove(unconfigured_proxy_list, mp);
+
+ /* close its stdout stream */
+ fclose(mp->stdout);
+
+ tor_free(mp);
+}
+
+/** Return true if the configuration of the managed proxy <b>mp</b> is
+ finished. */
+static INLINE int
+proxy_configuration_finished(managed_proxy_t *mp)
+{
+ return (mp->conf_state == PT_PROTO_CONFIGURED ||
+ mp->conf_state == PT_PROTO_BROKEN);
+}
+
+/** Handle a configuration protocol <b>line</b> received from a
+ * managed proxy <b>mp</b>. */
+void
+handle_proxy_line(char *line, managed_proxy_t *mp)
+{
+ printf("Judging line: %s\n", line);
+
+ if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
+ log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
+ "Discarding");
+ goto err;
+ }
+
+ if (!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR))) {
+ if (mp->conf_state != PT_PROTO_INFANT)
+ goto err;
+
+ parse_env_error(line);
+ goto err;
+ } else if (!strncmp(line, PROTO_NEG_FAIL, strlen(PROTO_NEG_FAIL))) {
+ if (mp->conf_state != PT_PROTO_INFANT)
+ goto err;
+
+ log_warn(LD_CONFIG, "Managed proxy could not pick a "
+ "configuration protocol version.");
+ goto err;
+ } else if (!strncmp(line, PROTO_NEG_SUCCESS,
+ strlen(PROTO_NEG_SUCCESS))) {
+ if (mp->conf_state != PT_PROTO_INFANT)
+ goto err;
+
+ if (parse_version(line,mp) < 0)
+ goto err;
+
+ tor_assert(mp->conf_protocol != 0);
+ mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
+ return;
+ } else if (!strncmp(line, PROTO_CMETHODS_DONE,
+ strlen(PROTO_CMETHODS_DONE))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ log_warn(LD_CONFIG, "Client managed proxy configuration completed!");
+ mp->conf_state = PT_PROTO_CONFIGURED;
+ return;
+ } else if (!strncmp(line, PROTO_SMETHODS_DONE,
+ strlen(PROTO_SMETHODS_DONE))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ log_warn(LD_CONFIG, "Server managed proxy configuration completed!");
+ mp->conf_state = PT_PROTO_CONFIGURED;
+ return;
+ } else if (!strncmp(line, PROTO_CMETHOD_ERROR,
+ strlen(PROTO_CMETHOD_ERROR))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ parse_client_method_error(line);
+ goto err;
+ } else if (!strncmp(line, PROTO_SMETHOD_ERROR,
+ strlen(PROTO_SMETHOD_ERROR))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ parse_server_method_error(line);
+ goto err;
+ } else if (!strncmp(line, PROTO_CMETHOD, strlen(PROTO_CMETHOD))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ if (parse_cmethod_line(line, mp) < 0)
+ goto err;
+
+ return;
+ } else if (!strncmp(line, PROTO_SMETHOD, strlen(PROTO_SMETHOD))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ if (parse_smethod_line(line, mp) < 0)
+ goto err;
+
+ return;
+ }
+
+ log_warn(LD_CONFIG, "Unknown line received by managed proxy. (%s)", line);
+
+ err:
+ mp->conf_state = PT_PROTO_BROKEN;
+ return;
+}
+
+/** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
+void
+parse_env_error(char *line)
+{
+ tor_assert(!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR)));
+
+ /* (Length of the protocol string) plus (a space) and (the first char of
+ the error message) */
+ if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
+ log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
+ "message.", PROTO_ENV_ERROR);
+
+ log_warn(LD_CONFIG, "Managed proxy couldn't understand the "
+ "pluggable transport environment variables. (%s)",
+ line+strlen(PROTO_ENV_ERROR)+1);
+}
+
+/** Handles a VERSION <b>line</b>. Updates the configuration protocol
+ * version in <b>mp</b>. */
+int
+parse_version(char *line, managed_proxy_t *mp)
+{
+ tor_assert(!strncmp(line, PROTO_NEG_SUCCESS, strlen(PROTO_NEG_SUCCESS)));
+
+ if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
+ log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
+ PROTO_NEG_SUCCESS);
+ return -1;
+ }
+
+ if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) {
+ log_warn(LD_CONFIG, "We don't support version '%s'. "
+ "We only support version '1'", line+strlen(PROTO_NEG_SUCCESS)+1);
+ return -1;
+ }
+
+ mp->conf_protocol = PROTO_VERSION_ONE; /* temp. till more versions appear */
+ return 0;
+}
+
+/** Parses {C,S}METHOD-ERROR <b>line</b> and warns the user
+ * accordingly. If <b>is_server</b> it is an SMETHOD-ERROR,
+ * otherwise it is a CMETHOD-ERROR. */
+static void
+parse_method_error(char *line, int is_server)
+{
+ const char* error = is_server ?
+ PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;
+
+ /* (Length of the protocol string) plus (a space) and (the first char of
+ the error message) */
+ if (strlen(line) < (strlen(error) + 2))
+ log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
+ "message.", error);
+
+ log_warn(LD_CONFIG, "%s managed proxy encountered a method error. (%s)",
+ is_server ? "Server" : "Client",
+ line+strlen(error)+1);
+}
+
+/** Parses an SMETHOD <b>line</b>. */
+int
+parse_smethod_line(char *line, managed_proxy_t *mp)
+{
+ int r;
+ smartlist_t *items = NULL;
+
+ char *method_name=NULL;
+
+ char *addrport=NULL;
+ tor_addr_t addr;
+ uint16_t port = 0;
+
+ items = smartlist_create();
+ smartlist_split_string(items, line, NULL,
+ SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
+ if (smartlist_len(items) < 3) {
+ log_warn(LD_CONFIG, "Server managed proxy sent us a SMETHOD line "
+ "with too few arguments.");
+ goto err;
+ }
+
+ tor_assert(!strcmp(smartlist_get(items,0),PROTO_SMETHOD));
+
+ method_name = smartlist_get(items,1);
+
+ addrport = smartlist_get(items, 2);
+ if (tor_addr_port_parse(addrport, &addr, &port)<0) {
+ log_warn(LD_CONFIG, "Error parsing transport "
+ "address '%s'", addrport);
+ goto err;
+ }
+
+ if (!port) {
+ log_warn(LD_CONFIG,
+ "Transport address '%s' has no port.", addrport);
+ goto err;
+ }
+
+ /* For now, notify the user so that he knows where the server
+ transport is listening. */
+ log_warn(LD_CONFIG, "Server transport %s at %s:%d.",
+ method_name, fmt_addr(&addr), (int)port);
+
+ r=0;
+ goto done;
+
+ err:
+ r = -1;
+
+ done:
+ SMARTLIST_FOREACH(items, char*, s, tor_free(s));
+ smartlist_free(items);
+ return r;
+}
+
+/** Parses a CMETHOD <b>line</b>, and if well-formed it registers
+ * the new transport in <b>mp</b>. */
+int
+parse_cmethod_line(char *line, managed_proxy_t *mp)
+{
+ int r;
+ smartlist_t *items = NULL;
+
+ char *method_name=NULL;
+
+ char *socks_ver_str=NULL;
+ int socks_ver=PROXY_NONE;
+
+ char *addrport=NULL;
+ tor_addr_t addr;
+ uint16_t port = 0;
+
+ transport_t *transport=NULL;
+
+ items = smartlist_create();
+ smartlist_split_string(items, line, NULL,
+ SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
+ if (smartlist_len(items) < 4) {
+ log_warn(LD_CONFIG, "Client managed proxy sent us a CMETHOD line "
+ "with too few arguments.");
+ goto err;
+ }
+
+ tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));
+
+ method_name = smartlist_get(items,1);
+
+ socks_ver_str = smartlist_get(items,2);
+
+ if (!strcmp(socks_ver_str,"socks4")) {
+ socks_ver = PROXY_SOCKS4;
+ } else if (!strcmp(socks_ver_str,"socks5")) {
+ socks_ver = PROXY_SOCKS5;
+ } else {
+ log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
+ "we don't recognize. (%s)", socks_ver_str);
+ goto err;
+ }
+
+ addrport = smartlist_get(items, 3);
+ if (tor_addr_port_parse(addrport, &addr, &port)<0) {
+ log_warn(LD_CONFIG, "Error parsing transport "
+ "address '%s'", addrport);
+ goto err;
+ }
+
+ if (!port) {
+ log_warn(LD_CONFIG,
+ "Transport address '%s' has no port.", addrport);
+ goto err;
+ }
+
+ transport = transport_create(&addr, port, method_name, socks_ver);
+ if (!transport)
+ goto err;
+
+ smartlist_add(mp->transports, transport);
+
+ log_warn(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
+ "Attached to managed proxy.",
+ method_name, fmt_addr(&addr), (int)port, socks_ver);
+
+ r=0;
+ goto done;
+
+ err:
+ r = -1;
+
+ done:
+ SMARTLIST_FOREACH(items, char*, s, tor_free(s));
+ smartlist_free(items);
+ return r;
+}
+
+/** Prepares the <b>envp</b> of a pluggable transport managed proxy
+ *
+ * <b>method</b> is a line with transport methods to be launched.
+ * If <b>is_server</b> is set, prepare a server proxy <b>envp</b>. */
+static void
+set_environ(char ***envp, const char *method, int is_server)
+{
+ or_options_t *options = get_options();
+ char **tmp=NULL;
+ char *state_loc=NULL;
+
+ int n_envs = is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
+
+ /* allocate enough space for our env. vars and a NULL pointer */
+ *envp = tor_malloc(sizeof(char*)*(n_envs+1));
+ tmp = *envp;
+
+ /* these should all be customizable */
+ tor_asprintf(tmp++, "HOME=%s", getenv("HOME"));
+ tor_asprintf(tmp++, "PATH=%s", getenv("PATH"));
+ state_loc = get_datadir_fname("pt_state/");
+ tor_asprintf(tmp++, "TOR_PT_STATE_LOCATION=%s", state_loc);
+ tor_free(state_loc);
+ tor_asprintf(tmp++, "TOR_PT_MANAGED_TRANSPORT_VER=1"); /* temp */
+ if (is_server) {
+ /* ASN check for ORPort values, should we be here if it's 0? */
+ tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort); /* temp */
+ tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=127.0.0.1:0");
+ tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", method);
+ tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200"); /* temp*/
+ } else {
+ tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", method);
+ }
+ *tmp = NULL;
+}
+
+/* ASN is this too ugly/stupid? */
+/** Frees the array of pointers in <b>arg</b> used as arguments to
+ execve. */
+static INLINE void
+free_execve_args(char **arg)
+{
+ char **tmp = arg;
+ while (*tmp) /* use the fact that the last element of the array is a
+ NULL pointer to know when to stop freeing */
+ _tor_free(*tmp++);
+
+ tor_free(arg);
+}
+
+/** Release all storage held by the pluggable transports subsystem. */
+void
+pt_free_all(void)
+{
+ if (unconfigured_proxy_list) {
+ /* If the proxy is in PT_PROTO_COMPLETED, it has registered its
+ transports and it's the duty of the circuitbuild.c subsystem to
+ free them. Otherwise, it hasn't registered its transports yet
+ and we should free them here. */
+ SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
+ if (mp->conf_state == PT_PROTO_COMPLETED)
+ managed_proxy_destroy(mp,0);
+ else
+ managed_proxy_destroy(mp,1);
+ } SMARTLIST_FOREACH_END(mp);
+
+ smartlist_clear(unconfigured_proxy_list);
+ smartlist_free(unconfigured_proxy_list);
+ unconfigured_proxy_list=NULL;
+ }
+}
+
diff --git a/src/or/transports.h b/src/or/transports.h
new file mode 100644
index 0000000..80d5429
--- /dev/null
+++ b/src/or/transports.h
@@ -0,0 +1,59 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2011, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file pluggable_transpots.h
+ * \brief Headers for pluggable_transpots.c
+ **/
+
+#ifndef TOR_PLUGGABLE_TRANSPORTS_H
+#define TOR_PLUGGABLE_TRANSPORTS_H
+
+int pt_managed_launch_proxy(const char *method,
+ char **proxy_argv, int is_server);
+
+#define pt_managed_launch_client_proxy(m, pa) \
+ pt_managed_launch_proxy(m, pa, 0)
+#define pt_managed_launch_server_proxy(m, pa) \
+ pt_managed_launch_proxy(m, pa, 1)
+
+void pt_configure_remaining_proxies(void);
+
+int pt_proxies_configuration_pending(void);
+
+void pt_free_all(void);
+
+#ifdef PT_PRIVATE
+/** State of the managed proxy configuration protocol. */
+enum pt_proto_state {
+ PT_PROTO_INFANT, /* was just born */
+ PT_PROTO_ACCEPTING_METHODS, /* accepting methods */
+ PT_PROTO_CONFIGURED, /* configured successfully */
+ PT_PROTO_COMPLETED, /* configure and registered its transports */
+ PT_PROTO_BROKEN
+};
+
+/** Structure containing information of a managed proxy. */
+typedef struct {
+ enum pt_proto_state conf_state; /* the current configuration state */
+ int conf_protocol; /* the configuration protocol version used */
+
+ FILE *stdout; /* a stream to its stdout
+ (closed in managed_proxy_destroy()) */
+
+ smartlist_t *transports; /* list of transports this proxy spawns */
+} managed_proxy_t;
+
+int parse_cmethod_line(char *line, managed_proxy_t *mp);
+int parse_smethod_line(char *line, managed_proxy_t *mp);
+
+int parse_version(char *line, managed_proxy_t *mp);
+void parse_env_error(char *line);
+void handle_proxy_line(char *line, managed_proxy_t *mp);
+
+#endif
+
+#endif
+
1
0
07 Oct '11
commit 782810a8bf1fbc3feaca851a011a8124891d39fc
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Sun Sep 11 20:26:01 2011 +0200
Introduce tor_terminate_process() function.
---
src/common/util.c | 26 ++++++++++++++++++++++++++
src/common/util.h | 1 +
2 files changed, 27 insertions(+), 0 deletions(-)
diff --git a/src/common/util.c b/src/common/util.c
index 0632c67..63172c3 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -28,6 +28,7 @@
#include <direct.h>
#include <process.h>
#include <tchar.h>
+#include <Winbase.h>
#else
#include <dirent.h>
#include <pwd.h>
@@ -43,6 +44,7 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
+#include <signal.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
@@ -2930,6 +2932,30 @@ format_helper_exit_status(unsigned char child_state, int saved_errno,
/* Maximum number of file descriptors, if we cannot get it via sysconf() */
#define DEFAULT_MAX_FD 256
+/** Terminate process running at PID <b>pid</b>.
+ * Code borrowed from Python's os.kill. */
+int
+tor_terminate_process(pid_t pid)
+{
+#ifdef MS_WINDOWS
+ DWORD pid_win = pid;
+ DWORD err;
+ HANDLE handle;
+ /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
+ attempt to open and terminate the process. */
+ handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
+ if (handle == NULL)
+ return -1;
+
+ if (TerminateProcess(handle, sig) == 0)
+ return -1;
+ else
+ return 0;
+#else /* *nix */
+ return kill(pid, SIGTERM);
+#endif
+}
+
#define CHILD_STATE_INIT 0
#define CHILD_STATE_PIPE 1
#define CHILD_STATE_MAXFD 2
diff --git a/src/common/util.h b/src/common/util.h
index 8bf4f7b..7e889b1 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -350,6 +350,7 @@ void write_pidfile(char *filename);
void tor_check_port_forwarding(const char *filename,
int dir_port, int or_port, time_t now);
+int tor_terminate_process(pid_t pid);
int tor_spawn_background(const char *const filename, int *stdout_read,
int *stderr_read, const char **argv,
const char **envp);
1
0
commit db4cde38109c677db14316ac77c240d7d61db386
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Mon Aug 15 17:26:03 2011 +0200
Improve the code a tad.
* Use strcmpstart() instead of strcmp(x,y,strlen(y)).
* Warn the user if the managed proxy failed to launch.
* Improve function documentation.
* Use smartlist_len() instead of n_unconfigured_proxies.
* Split managed_proxy_destroy() to managed_proxy_destroy()
and managed_proxy_destroy_with_transports().
* Constification.
---
src/common/util.c | 2 -
src/common/util.h | 1 +
src/or/config.c | 8 +-
src/or/config.h | 2 +-
src/or/transports.c | 207 ++++++++++++++++++++++++++-------------------------
src/or/transports.h | 12 ++--
6 files changed, 119 insertions(+), 113 deletions(-)
diff --git a/src/common/util.c b/src/common/util.c
index 7a7ee19..0632c67 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -2941,8 +2941,6 @@ format_helper_exit_status(unsigned char child_state, int saved_errno,
#define CHILD_STATE_EXEC 8
#define CHILD_STATE_FAILEXEC 9
-#define SPAWN_ERROR_MESSAGE "ERR: Failed to spawn background process - code "
-
/** Start a program in the background. If <b>filename</b> contains a '/',
* then it will be treated as an absolute or relative path. Otherwise the
* system path will be searched for <b>filename</b>. The strings in
diff --git a/src/common/util.h b/src/common/util.h
index 12dc106..8bf4f7b 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -353,6 +353,7 @@ void tor_check_port_forwarding(const char *filename,
int tor_spawn_background(const char *const filename, int *stdout_read,
int *stderr_read, const char **argv,
const char **envp);
+#define SPAWN_ERROR_MESSAGE "ERR: Failed to spawn background process - code "
#ifdef MS_WINDOWS
HANDLE load_windows_system_library(const TCHAR *library_name);
diff --git a/src/or/config.c b/src/or/config.c
index 41b6bba..0b84f92 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1228,7 +1228,7 @@ options_act(or_options_t *old_options)
/* If we have pluggable transport related options enabled, see if we
should warn the user about potential configuration problems. */
- if (options->Bridges || options->ClientTransportPlugin || options->ServerTransportPlugin)
+ if (options->Bridges || options->ClientTransportPlugin)
validate_pluggable_transports_config();
if (running_tor && rend_config_services(options, 0)<0) {
@@ -4696,7 +4696,7 @@ parse_client_transport_line(const char *line, int validate_only)
int r;
char *field2=NULL;
- char *name=NULL;
+ const char *name=NULL;
char *addrport=NULL;
tor_addr_t addr;
uint16_t port = 0;
@@ -4801,7 +4801,7 @@ parse_server_transport_line(const char *line, int validate_only)
{
smartlist_t *items = NULL;
int r;
- char *name=NULL;
+ const char *name=NULL;
char *type=NULL;
char *addrport=NULL;
tor_addr_t addr;
@@ -5839,7 +5839,7 @@ get_bindaddr_for_transport(const char *transport)
state */
void
save_transport_to_state(const char *transport,
- tor_addr_t *addr, uint16_t port)
+ const tor_addr_t *addr, uint16_t port)
{
or_state_t *state = get_or_state();
diff --git a/src/or/config.h b/src/or/config.h
index d09d904..45baf45 100644
--- a/src/or/config.h
+++ b/src/or/config.h
@@ -64,7 +64,7 @@ int did_last_state_file_write_fail(void);
int or_state_save(time_t now);
void save_transport_to_state(const char *transport_name,
- tor_addr_t *addr, uint16_t port);
+ const tor_addr_t *addr, uint16_t port);
const char *get_bindaddr_for_transport(const char *transport);
int options_need_geoip_info(or_options_t *options, const char **reason_out);
diff --git a/src/or/transports.c b/src/or/transports.c
index c414920..21a3022 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -11,19 +11,21 @@
#include "config.h"
#include "circuitbuild.h"
#include "transports.h"
+#include "util.h"
/* ASN TIDY THESE UP*/
-static void set_environ(char ***envp, managed_proxy_t *mp);
-static INLINE int proxy_configuration_finished(managed_proxy_t *mp);
+static void set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp);
+static INLINE int proxy_configuration_finished(const managed_proxy_t *mp);
+
+static void managed_proxy_destroy_impl(managed_proxy_t *mp,
+ int also_free_transports);
+#define managed_proxy_destroy(mp) managed_proxy_destroy_impl(mp, 0)
+#define managed_proxy_destroy_with_transports(mp) managed_proxy_destroy_impl(mp, 1)
-static void managed_proxy_destroy(managed_proxy_t *mp,
- int also_free_transports);
static void handle_finished_proxy(managed_proxy_t *mp);
static void configure_proxy(managed_proxy_t *mp);
-static void register_server_proxy(managed_proxy_t *mp);
-
-static void parse_method_error(char *line, int is_server_method);
+static void parse_method_error(const char *line, int is_server_method);
#define parse_server_method_error(l) parse_method_error(l, 1)
#define parse_client_method_error(l) parse_method_error(l, 0)
@@ -54,20 +56,19 @@ static INLINE void free_execve_args(char **arg);
/** List of unconfigured managed proxies. */
static smartlist_t *unconfigured_proxy_list = NULL;
-/** Number of unconfigured managed proxies. */
-static int n_unconfigured_proxies = 0;
-/* The main idea is:
+/* The main idea here is:
A managed proxy is represented by a managed_proxy_t struct and can
spawn multiple transports.
unconfigured_proxy_list is a list of all the unconfigured managed
- proxies; everytime we spawn a managed proxy we add it in that
- list.
- In every run_scheduled_event() tick, we attempt to configure each
- managed proxy further, using the configuration protocol defined in
- the 180_pluggable_transport.txt proposal.
+ proxies; everytime we find a managed proxy in torrc we add it in
+ that list.
+ In every run_scheduled_event() tick, we attempt to launch and then
+ configure each managed proxy, using the configuration protocol
+ defined in the 180_pluggable_transport.txt proposal. A managed
+ proxy might need several ticks to get fully configured.
When a managed proxy is fully configured, we register all its
transports to the circuitbuild.c subsystem - like we do with
@@ -78,7 +79,8 @@ static int n_unconfigured_proxies = 0;
int
pt_proxies_configuration_pending(void)
{
- return !!n_unconfigured_proxies;
+ if (!unconfigured_proxy_list) return 0;
+ return !!smartlist_len(unconfigured_proxy_list);
}
/** Return true if <b>mp</b> has the same argv as <b>proxy_argv</b> */
@@ -120,7 +122,7 @@ get_managed_proxy_by_argv(char **proxy_argv)
/** Add <b>transport</b> to managed proxy <b>mp</b>. */
static void
-add_transport_to_proxy(char *transport, managed_proxy_t *mp)
+add_transport_to_proxy(const char *transport, managed_proxy_t *mp)
{
tor_assert(mp->transports_to_launch);
if (!smartlist_string_isin(mp->transports_to_launch, transport))
@@ -137,14 +139,8 @@ launch_managed_proxy(managed_proxy_t *mp)
int stdout_pipe=-1, stderr_pipe=-1;
/* prepare the environment variables for the managed proxy */
- set_environ(&envp, mp);
+ set_managed_proxy_environment(&envp, mp);
- char **tmp = envp;
- printf("PRINTING ENVP\n");
- while (*tmp)
- printf("%s\n", *tmp++);
-
- /* ASN we should probably check if proxy_argv[0] is executable by our user */
retval = tor_spawn_background(mp->argv[0], &stdout_pipe,
&stderr_pipe, (const char **)mp->argv,
(const char **)envp);
@@ -153,7 +149,7 @@ launch_managed_proxy(managed_proxy_t *mp)
return -1;
}
- /* free the memory allocated for the execve() */
+ /* free the memory allocated by set_managed_proxy_environment(). */
free_execve_args(envp);
/* Set stdout/stderr pipes to be non-blocking */
@@ -177,8 +173,8 @@ pt_configure_remaining_proxies(void)
{
log_warn(LD_CONFIG, "We start configuring remaining managed proxies!");
SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
- if (proxy_configuration_finished(mp)) /* finished managed proxies
- shouldn't be here */
+ /* configured proxies shouldn't be in unconfigured_proxy_list. */
+ if (proxy_configuration_finished(mp))
assert(0);
configure_proxy(mp);
@@ -205,15 +201,16 @@ configure_proxy(managed_proxy_t *mp)
r = get_string_from_pipe(mp->stdout, stdout_buf,
sizeof(stdout_buf) - 1);
- if (r == IO_STREAM_CLOSED || r == IO_STREAM_TERM) {
+ if (r == IO_STREAM_OKAY) { /* got a line; handle it! */
+ handle_proxy_line((const char *)stdout_buf, mp);
+ } else if (r == IO_STREAM_EAGAIN) { /* check back later */
+ return;
+ } else if (r == IO_STREAM_CLOSED || r == IO_STREAM_TERM) { /* snap! */
log_warn(LD_GENERAL, "Managed proxy stream closed. "
"Most probably application stopped running");
mp->conf_state = PT_PROTO_BROKEN;
- } else if (r == IO_STREAM_EAGAIN) {
- return;
- } else {
- tor_assert(r == IO_STREAM_OKAY);
- handle_proxy_line(stdout_buf, mp);
+ } else { /* unknown stream status */
+ log_warn(LD_GENERAL, "Unknown stream status while configuring proxy.");
}
/* if the proxy finished configuring, exit the loop. */
@@ -226,7 +223,7 @@ configure_proxy(managed_proxy_t *mp)
/** Register server managed proxy <b>mp</b> transports to state */
static void
-register_server_proxy(managed_proxy_t *mp)
+register_server_proxy(const managed_proxy_t *mp)
{
if (mp->is_server) {
SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
@@ -238,7 +235,7 @@ register_server_proxy(managed_proxy_t *mp)
/** Register all the transports supported by client managed proxy
* <b>mp</b> to the bridge subsystem. */
static void
-register_client_proxy(managed_proxy_t *mp)
+register_client_proxy(const managed_proxy_t *mp)
{
SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
if (transport_add(t)<0) {
@@ -252,7 +249,7 @@ register_client_proxy(managed_proxy_t *mp)
/** Register the transports of managed proxy <b>mp</b>. */
static INLINE void
-register_proxy(managed_proxy_t *mp)
+register_proxy(const managed_proxy_t *mp)
{
if (mp->is_server)
register_server_proxy(mp);
@@ -260,35 +257,11 @@ register_proxy(managed_proxy_t *mp)
register_client_proxy(mp);
}
-/** Handle a configured or broken managed proxy <b>mp</b>. */
-static void
-handle_finished_proxy(managed_proxy_t *mp)
-{
- switch (mp->conf_state) {
- case PT_PROTO_BROKEN: /* if broken: */
- managed_proxy_destroy(mp, 1); /* destroy it and all its transports */
- break;
- case PT_PROTO_CONFIGURED: /* if configured correctly: */
- register_proxy(mp); /* register transports */
- mp->conf_state = PT_PROTO_COMPLETED; /* mark it as completed, */
- managed_proxy_destroy(mp, 0); /* destroy the managed proxy struct,
- keeping the transports intact */
- break;
- default:
- log_warn(LD_CONFIG, "Unfinished managed proxy in "
- "handle_finished_proxy().");
- assert(0);
- }
-
- n_unconfigured_proxies--;
- tor_assert(n_unconfigured_proxies >= 0);
-}
-
/** Free memory allocated by managed proxy <b>mp</b>.
* If <b>also_free_transports</b> is set, also free the transports
* associated with this managed proxy. */
static void
-managed_proxy_destroy(managed_proxy_t *mp, int also_free_transports)
+managed_proxy_destroy_impl(managed_proxy_t *mp, int also_free_transports)
{
/* transport_free() all its transports */
if (also_free_transports)
@@ -316,19 +289,59 @@ managed_proxy_destroy(managed_proxy_t *mp, int also_free_transports)
tor_free(mp);
}
+
+/** Handle a configured or broken managed proxy <b>mp</b>. */
+static void
+handle_finished_proxy(managed_proxy_t *mp)
+{
+ switch (mp->conf_state) {
+ case PT_PROTO_BROKEN: /* if broken: */
+ managed_proxy_destroy_with_transports(mp); /* destroy it and all its transports */
+ break;
+ case PT_PROTO_CONFIGURED: /* if configured correctly: */
+ register_proxy(mp); /* register transports */
+ mp->conf_state = PT_PROTO_COMPLETED; /* mark it as completed, */
+ managed_proxy_destroy(mp); /* destroy the managed proxy struct,
+ keeping the transports intact */
+ break;
+ default:
+ log_warn(LD_CONFIG, "Unfinished managed proxy in "
+ "handle_finished_proxy().");
+ assert(0);
+ }
+
+ tor_assert(smartlist_len(unconfigured_proxy_list) >= 0);
+}
+
/** Return true if the configuration of the managed proxy <b>mp</b> is
finished. */
static INLINE int
-proxy_configuration_finished(managed_proxy_t *mp)
+proxy_configuration_finished(const managed_proxy_t *mp)
{
return (mp->conf_state == PT_PROTO_CONFIGURED ||
mp->conf_state == PT_PROTO_BROKEN);
}
+
+/** This function is called when a proxy sends an {S,C}METHODS DONE message,
+ */
+static void
+handle_methods_done(const managed_proxy_t *mp)
+{
+ tor_assert(mp->transports);
+
+ if (smartlist_len(mp->transports) == 0)
+ log_warn(LD_GENERAL, "Proxy was spawned successfully, "
+ "but it didn't laucn any pluggable transport listeners!");
+
+ log_warn(LD_CONFIG, "%s managed proxy configuration completed!",
+ mp->is_server ? "Server" : "Client");
+}
+
/** Handle a configuration protocol <b>line</b> received from a
* managed proxy <b>mp</b>. */
void
-handle_proxy_line(char *line, managed_proxy_t *mp)
+handle_proxy_line(const char *line, managed_proxy_t *mp)
{
printf("Judging line: %s\n", line);
@@ -338,21 +351,20 @@ handle_proxy_line(char *line, managed_proxy_t *mp)
goto err;
}
- if (!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR))) {
+ if (!strcmpstart(line, PROTO_ENV_ERROR)) {
if (mp->conf_state != PT_PROTO_LAUNCHED)
goto err;
parse_env_error(line);
goto err;
- } else if (!strncmp(line, PROTO_NEG_FAIL, strlen(PROTO_NEG_FAIL))) {
+ } else if (!strcmpstart(line, PROTO_NEG_FAIL)) {
if (mp->conf_state != PT_PROTO_LAUNCHED)
goto err;
log_warn(LD_CONFIG, "Managed proxy could not pick a "
"configuration protocol version.");
goto err;
- } else if (!strncmp(line, PROTO_NEG_SUCCESS,
- strlen(PROTO_NEG_SUCCESS))) {
+ } else if (!strcmpstart(line, PROTO_NEG_SUCCESS)) {
if (mp->conf_state != PT_PROTO_LAUNCHED)
goto err;
@@ -362,37 +374,35 @@ handle_proxy_line(char *line, managed_proxy_t *mp)
tor_assert(mp->conf_protocol != 0);
mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
return;
- } else if (!strncmp(line, PROTO_CMETHODS_DONE,
- strlen(PROTO_CMETHODS_DONE))) {
+ } else if (!strcmpstart(line, PROTO_CMETHODS_DONE)) {
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
goto err;
- log_warn(LD_CONFIG, "Client managed proxy configuration completed!");
+ handle_methods_done(mp);
+
mp->conf_state = PT_PROTO_CONFIGURED;
return;
- } else if (!strncmp(line, PROTO_SMETHODS_DONE,
- strlen(PROTO_SMETHODS_DONE))) {
+ } else if (!strcmpstart(line, PROTO_SMETHODS_DONE)) {
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
goto err;
- log_warn(LD_CONFIG, "Server managed proxy configuration completed!");
+ handle_methods_done(mp);
+
mp->conf_state = PT_PROTO_CONFIGURED;
return;
- } else if (!strncmp(line, PROTO_CMETHOD_ERROR,
- strlen(PROTO_CMETHOD_ERROR))) {
+ } else if (!strcmpstart(line, PROTO_CMETHOD_ERROR)) {
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
goto err;
parse_client_method_error(line);
goto err;
- } else if (!strncmp(line, PROTO_SMETHOD_ERROR,
- strlen(PROTO_SMETHOD_ERROR))) {
+ } else if (!strcmpstart(line, PROTO_SMETHOD_ERROR)) {
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
goto err;
parse_server_method_error(line);
goto err;
- } else if (!strncmp(line, PROTO_CMETHOD, strlen(PROTO_CMETHOD))) {
+ } else if (!strcmpstart(line, PROTO_CMETHOD)) {
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
goto err;
@@ -400,7 +410,7 @@ handle_proxy_line(char *line, managed_proxy_t *mp)
goto err;
return;
- } else if (!strncmp(line, PROTO_SMETHOD, strlen(PROTO_SMETHOD))) {
+ } else if (!strcmpstart(line, PROTO_SMETHOD)) {
if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
goto err;
@@ -408,6 +418,9 @@ handle_proxy_line(char *line, managed_proxy_t *mp)
goto err;
return;
+ } else if (!strcmpstart(line, SPAWN_ERROR_MESSAGE)) {
+ log_warn(LD_GENERAL, "Could not launch managed proxy executable!");
+ goto err;
}
log_warn(LD_CONFIG, "Unknown line received by managed proxy. (%s)", line);
@@ -419,10 +432,8 @@ handle_proxy_line(char *line, managed_proxy_t *mp)
/** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
void
-parse_env_error(char *line)
+parse_env_error(const char *line)
{
- tor_assert(!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR)));
-
/* (Length of the protocol string) plus (a space) and (the first char of
the error message) */
if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
@@ -437,10 +448,8 @@ parse_env_error(char *line)
/** Handles a VERSION <b>line</b>. Updates the configuration protocol
* version in <b>mp</b>. */
int
-parse_version(char *line, managed_proxy_t *mp)
+parse_version(const char *line, managed_proxy_t *mp)
{
- tor_assert(!strncmp(line, PROTO_NEG_SUCCESS, strlen(PROTO_NEG_SUCCESS)));
-
if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
PROTO_NEG_SUCCESS);
@@ -461,7 +470,7 @@ parse_version(char *line, managed_proxy_t *mp)
* accordingly. If <b>is_server</b> it is an SMETHOD-ERROR,
* otherwise it is a CMETHOD-ERROR. */
static void
-parse_method_error(char *line, int is_server)
+parse_method_error(const char *line, int is_server)
{
const char* error = is_server ?
PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;
@@ -480,7 +489,7 @@ parse_method_error(char *line, int is_server)
/** Parses an SMETHOD <b>line</b> and if well-formed it registers the
* new transport in <b>mp</b>. */
int
-parse_smethod_line(char *line, managed_proxy_t *mp)
+parse_smethod_line(const char *line, managed_proxy_t *mp)
{
int r;
smartlist_t *items = NULL;
@@ -545,7 +554,7 @@ parse_smethod_line(char *line, managed_proxy_t *mp)
/** Parses a CMETHOD <b>line</b>, and if well-formed it registers
* the new transport in <b>mp</b>. */
int
-parse_cmethod_line(char *line, managed_proxy_t *mp)
+parse_cmethod_line(const char *line, managed_proxy_t *mp)
{
int r;
smartlist_t *items = NULL;
@@ -622,9 +631,10 @@ parse_cmethod_line(char *line, managed_proxy_t *mp)
}
/** Return a string containing the address:port that <b>transport</b>
- * should use. */
+ * should use. It's the responsibility of the caller to free() the
+ * received string. */
static char *
-get_bindaddr_for_proxy(managed_proxy_t *mp)
+get_bindaddr_for_proxy(const managed_proxy_t *mp)
{
char *bindaddr = NULL;
smartlist_t *string_tmp = smartlist_create();
@@ -646,7 +656,7 @@ get_bindaddr_for_proxy(managed_proxy_t *mp)
/** Prepare the <b>envp</b> of managed proxy <b>mp</b> */
static void
-set_environ(char ***envp, managed_proxy_t *mp)
+set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp)
{
or_options_t *options = get_options();
char **tmp=NULL;
@@ -660,7 +670,7 @@ set_environ(char ***envp, managed_proxy_t *mp)
*envp = tor_malloc(sizeof(char*)*(n_envs+1));
tmp = *envp;
- state_loc = get_datadir_fname("pt_state/");
+ state_loc = get_datadir_fname("pt_state/"); /* XXX temp */
transports_to_launch =
smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
@@ -671,10 +681,10 @@ set_environ(char ***envp, managed_proxy_t *mp)
if (mp->is_server) {
bindaddr = get_bindaddr_for_proxy(mp);
- tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort); /* temp */
+ tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort); /* XXX temp */
tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=%s", bindaddr);
tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", transports_to_launch);
- tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200"); /* temp*/
+ tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200"); /* XXX temp*/
} else {
tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", transports_to_launch);
}
@@ -689,7 +699,7 @@ set_environ(char ***envp, managed_proxy_t *mp)
* the managed proxy subsystem.
* If <b>is_server</b> is true, then the proxy is a server proxy. */
void
-pt_kickstart_proxy(char *transport, char **proxy_argv, int is_server)
+pt_kickstart_proxy(const char *transport, char **proxy_argv, int is_server)
{
managed_proxy_t *mp=NULL;
@@ -710,9 +720,6 @@ pt_kickstart_proxy(char *transport, char **proxy_argv, int is_server)
if (!unconfigured_proxy_list)
unconfigured_proxy_list = smartlist_create();
smartlist_add(unconfigured_proxy_list, mp);
-
- n_unconfigured_proxies++; /* ASN should we care about overflows here?
- I say no. */
} else { /* known proxy. just add transport to its transport list */
add_transport_to_proxy(transport, mp);
free_execve_args(proxy_argv);
@@ -743,9 +750,9 @@ pt_free_all(void)
and we should free them here. */
SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
if (mp->conf_state == PT_PROTO_COMPLETED)
- managed_proxy_destroy(mp,0);
+ managed_proxy_destroy(mp);
else
- managed_proxy_destroy(mp,1);
+ managed_proxy_destroy_with_transports(mp);
} SMARTLIST_FOREACH_END(mp);
smartlist_clear(unconfigured_proxy_list);
diff --git a/src/or/transports.h b/src/or/transports.h
index c75797d..6fec1dc 100644
--- a/src/or/transports.h
+++ b/src/or/transports.h
@@ -11,7 +11,7 @@
#ifndef TOR_TRANSPORTS_H
#define TOR_TRANSPORTS_H
-void pt_kickstart_proxy(char *method, char **proxy_argv,
+void pt_kickstart_proxy(const char *method, char **proxy_argv,
int is_server);
#define pt_kickstart_client_proxy(m, pa) \
@@ -51,12 +51,12 @@ typedef struct {
smartlist_t *transports; /* list of transport_t this proxy spawned */
} managed_proxy_t;
-int parse_cmethod_line(char *line, managed_proxy_t *mp);
-int parse_smethod_line(char *line, managed_proxy_t *mp);
+int parse_cmethod_line(const char *line, managed_proxy_t *mp);
+int parse_smethod_line(const char *line, managed_proxy_t *mp);
-int parse_version(char *line, managed_proxy_t *mp);
-void parse_env_error(char *line);
-void handle_proxy_line(char *line, managed_proxy_t *mp);
+int parse_version(const char *line, managed_proxy_t *mp);
+void parse_env_error(const char *line);
+void handle_proxy_line(const char *line, managed_proxy_t *mp);
#endif
1
0
[tor/master] Spawn multiple protocols using a single managed proxy.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit ea3e9416c6c579735ff2b844a88605ae5e1cbbd2
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Fri Aug 12 21:33:05 2011 +0200
Spawn multiple protocols using a single managed proxy.
If multiple torrc transport lines have the same argv, tor instructs a
single managed proxy to launch multiple protocols.
---
src/or/config.c | 42 +++++------
src/or/config.h | 2 +-
src/or/transports.c | 196 ++++++++++++++++++++++++++++++++++++++++-----------
src/or/transports.h | 17 +++--
4 files changed, 184 insertions(+), 73 deletions(-)
diff --git a/src/or/config.c b/src/or/config.c
index dced47c..41b6bba 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1228,7 +1228,7 @@ options_act(or_options_t *old_options)
/* If we have pluggable transport related options enabled, see if we
should warn the user about potential configuration problems. */
- if (options->Bridges || options->ClientTransportPlugin)
+ if (options->Bridges || options->ClientTransportPlugin || options->ServerTransportPlugin)
validate_pluggable_transports_config();
if (running_tor && rend_config_services(options, 0)<0) {
@@ -4745,15 +4745,14 @@ parse_client_transport_line(const char *line, int validate_only)
tor_assert(proxy_argc > 0);
proxy_argv = tor_malloc_zero(sizeof(char*)*(proxy_argc+1));
tmp = proxy_argv;
- for (i=0;i<proxy_argc;i++) /* store arguments */
- *tmp++ = smartlist_get(items, 2+i);
+ for (i=0;i<proxy_argc;i++) { /* store arguments */
+ *tmp++ = smartlist_get(items, 2);
+ smartlist_del_keeporder(items, 2);
+ }
*tmp = NULL; /*terminated with NUL pointer, just like execve() likes it*/
- if (pt_managed_launch_client_proxy(name, proxy_argv) < 0) {
- log_warn(LD_CONFIG, "Error while launching managed proxy at '%s'",
- proxy_argv[0]);
- goto err;
- }
+ /* kickstart the thing */
+ pt_kickstart_client_proxy(name, proxy_argv);
}
} else { /* external */
addrport = smartlist_get(items, 2);
@@ -4789,7 +4788,6 @@ parse_client_transport_line(const char *line, int validate_only)
done:
SMARTLIST_FOREACH(items, char*, s, tor_free(s));
smartlist_free(items);
- tor_free(proxy_argv);
return r;
}
@@ -4847,15 +4845,14 @@ parse_server_transport_line(const char *line, int validate_only)
proxy_argv = tor_malloc_zero(sizeof(char*)*(proxy_argc+1));
tmp = proxy_argv;
- for (i=0;i<proxy_argc;i++) /* store arguments */
- *tmp++ = smartlist_get(items, 2+i);
+ for (i=0;i<proxy_argc;i++) { /* store arguments */
+ *tmp++ = smartlist_get(items, 2);
+ smartlist_del_keeporder(items, 2);
+ }
*tmp = NULL; /*terminated with NUL pointer, just like execve() likes it*/
- if (pt_managed_launch_server_proxy(name, proxy_argv) < 0) { /* launch it! */
- log_warn(LD_CONFIG, "Error while launching managed proxy at '%s'",
- proxy_argv[0]);
- goto err;
- }
+ /* kickstart the thing */
+ pt_kickstart_server_proxy(name, proxy_argv);
}
} else { /* external */
addrport = smartlist_get(items, 2);
@@ -4886,7 +4883,6 @@ parse_server_transport_line(const char *line, int validate_only)
done:
SMARTLIST_FOREACH(items, char*, s, tor_free(s));
smartlist_free(items);
- tor_free(proxy_argv);
return r;
}
@@ -5811,8 +5807,8 @@ get_transport_in_state_by_name(const char *transport)
}
/** Return string containing the address:port part of the
- * TransportProxy <b>line</b> for transport <b>transport</b>. If the
- * line is corrupted, return NULL. */
+ * TransportProxy <b>line</b> for transport <b>transport</b>.
+ * If the line is corrupted, return NULL. */
const char *
get_transport_bindaddr(const char *line, const char *transport)
{
@@ -5822,8 +5818,8 @@ get_transport_bindaddr(const char *line, const char *transport)
return (line+strlen(transport)+1);
}
-/** Return a string containing the address:port that <b>transport</b>
- * should use. */
+/** Return a static string containing the address:port a proxy
+ * transport should bind on. */
const char *
get_bindaddr_for_transport(const char *transport)
{
@@ -5839,8 +5835,8 @@ get_bindaddr_for_transport(const char *transport)
return bindaddr ? bindaddr : default_addrport;
}
-/** Save <b>transport</b> listening at <b>addr</b>:<b>port</b> to
- * state */
+/** Save <b>transport</b> listening on <b>addr</b>:<b>port</b> to
+ state */
void
save_transport_to_state(const char *transport,
tor_addr_t *addr, uint16_t port)
diff --git a/src/or/config.h b/src/or/config.h
index dc3a828..d09d904 100644
--- a/src/or/config.h
+++ b/src/or/config.h
@@ -65,7 +65,7 @@ int or_state_save(time_t now);
void save_transport_to_state(const char *transport_name,
tor_addr_t *addr, uint16_t port);
-const char * get_bindaddr_for_transport(const char *transport);
+const char *get_bindaddr_for_transport(const char *transport);
int options_need_geoip_info(or_options_t *options, const char **reason_out);
int getinfo_helper_config(control_connection_t *conn,
diff --git a/src/or/transports.c b/src/or/transports.c
index b96792f..c414920 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -13,8 +13,7 @@
#include "transports.h"
/* ASN TIDY THESE UP*/
-static void set_environ(char ***envp, const char *method,
- int is_server);
+static void set_environ(char ***envp, managed_proxy_t *mp);
static INLINE int proxy_configuration_finished(managed_proxy_t *mp);
static void managed_proxy_destroy(managed_proxy_t *mp,
@@ -82,11 +81,55 @@ pt_proxies_configuration_pending(void)
return !!n_unconfigured_proxies;
}
-/** Launch a proxy for <b>method</b> using <b>proxy_argv</b> as its
- * arguments. If <b>is_server</b>, launch a server proxy. */
-int
-pt_managed_launch_proxy(const char *method,
- char **proxy_argv, int is_server)
+/** Return true if <b>mp</b> has the same argv as <b>proxy_argv</b> */
+static int
+managed_proxy_has_argv(managed_proxy_t *mp, char **proxy_argv)
+{
+ char **tmp1=proxy_argv;
+ char **tmp2=mp->argv;
+
+ tor_assert(tmp1);
+ tor_assert(tmp2);
+
+ while (*tmp1 && *tmp2) {
+ if (strcmp(*tmp1++, *tmp2++))
+ return 0;
+ }
+
+ if (!*tmp1 && !*tmp2)
+ return 1;
+
+ return 0;
+}
+
+/** Return a managed proxy with the same argv as <b>proxy_argv</b>.
+ * If no such managed proxy exists, return NULL. */
+static managed_proxy_t *
+get_managed_proxy_by_argv(char **proxy_argv)
+{
+ if (!unconfigured_proxy_list)
+ return NULL;
+
+ SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
+ if (managed_proxy_has_argv(mp, proxy_argv))
+ return mp;
+ } SMARTLIST_FOREACH_END(mp);
+
+ return NULL;
+}
+
+/** Add <b>transport</b> to managed proxy <b>mp</b>. */
+static void
+add_transport_to_proxy(char *transport, managed_proxy_t *mp)
+{
+ tor_assert(mp->transports_to_launch);
+ if (!smartlist_string_isin(mp->transports_to_launch, transport))
+ smartlist_add(mp->transports_to_launch, tor_strdup(transport));
+}
+
+/** Launch managed proxy <b>mp</b>. */
+static int
+launch_managed_proxy(managed_proxy_t *mp)
{
char **envp=NULL;
int retval;
@@ -94,11 +137,16 @@ pt_managed_launch_proxy(const char *method,
int stdout_pipe=-1, stderr_pipe=-1;
/* prepare the environment variables for the managed proxy */
- set_environ(&envp, method, is_server);
+ set_environ(&envp, mp);
+
+ char **tmp = envp;
+ printf("PRINTING ENVP\n");
+ while (*tmp)
+ printf("%s\n", *tmp++);
/* ASN we should probably check if proxy_argv[0] is executable by our user */
- retval = tor_spawn_background(proxy_argv[0], &stdout_pipe,
- &stderr_pipe, (const char **)proxy_argv,
+ retval = tor_spawn_background(mp->argv[0], &stdout_pipe,
+ &stderr_pipe, (const char **)mp->argv,
(const char **)envp);
if (retval < 0) {
log_warn(LD_GENERAL, "Spawn failed");
@@ -115,20 +163,8 @@ pt_managed_launch_proxy(const char *method,
log_warn(LD_CONFIG, "The spawn is alive (%d)!", retval);
- /* create a managed proxy */
- managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
- mp->conf_state = PT_PROTO_INFANT;
+ mp->conf_state = PT_PROTO_LAUNCHED;
mp->stdout = stdout_read;
- mp->transports = smartlist_create();
- mp->is_server = is_server;
-
- /* register the managed proxy */
- if (!unconfigured_proxy_list)
- unconfigured_proxy_list = smartlist_create();
- smartlist_add(unconfigured_proxy_list, mp);
-
- n_unconfigured_proxies++; /* ASN should we care about overflows here?
- I say no. */
return 0;
}
@@ -150,14 +186,21 @@ pt_configure_remaining_proxies(void)
} SMARTLIST_FOREACH_END(mp);
}
-/** Receive input from the managed proxy <b>mp</b> to get closer to
- * finally configuring it. */
+/** Attempt to continue configuring managed proxy <b>mp</b>. */
static void
configure_proxy(managed_proxy_t *mp)
{
enum stream_status r;
char stdout_buf[200];
+ /* if we haven't launched the proxy yet, do it now */
+ if (mp->conf_state == PT_PROTO_INFANT) {
+ launch_managed_proxy(mp);
+ return;
+ }
+
+ tor_assert(mp->conf_state != PT_PROTO_INFANT);
+
while (1) {
r = get_string_from_pipe(mp->stdout, stdout_buf,
sizeof(stdout_buf) - 1);
@@ -255,12 +298,21 @@ managed_proxy_destroy(managed_proxy_t *mp, int also_free_transports)
smartlist_clear(mp->transports);
smartlist_free(mp->transports);
+ SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
+
+ /* free the transports smartlist */
+ smartlist_clear(mp->transports_to_launch);
+ smartlist_free(mp->transports_to_launch);
+
/* remove it from the list of managed proxies */
smartlist_remove(unconfigured_proxy_list, mp);
/* close its stdout stream */
fclose(mp->stdout);
+ /* free the argv */
+ free_execve_args(mp->argv);
+
tor_free(mp);
}
@@ -278,6 +330,8 @@ proxy_configuration_finished(managed_proxy_t *mp)
void
handle_proxy_line(char *line, managed_proxy_t *mp)
{
+ printf("Judging line: %s\n", line);
+
if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
"Discarding");
@@ -285,13 +339,13 @@ handle_proxy_line(char *line, managed_proxy_t *mp)
}
if (!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR))) {
- if (mp->conf_state != PT_PROTO_INFANT)
+ if (mp->conf_state != PT_PROTO_LAUNCHED)
goto err;
parse_env_error(line);
goto err;
} else if (!strncmp(line, PROTO_NEG_FAIL, strlen(PROTO_NEG_FAIL))) {
- if (mp->conf_state != PT_PROTO_INFANT)
+ if (mp->conf_state != PT_PROTO_LAUNCHED)
goto err;
log_warn(LD_CONFIG, "Managed proxy could not pick a "
@@ -299,7 +353,7 @@ handle_proxy_line(char *line, managed_proxy_t *mp)
goto err;
} else if (!strncmp(line, PROTO_NEG_SUCCESS,
strlen(PROTO_NEG_SUCCESS))) {
- if (mp->conf_state != PT_PROTO_INFANT)
+ if (mp->conf_state != PT_PROTO_LAUNCHED)
goto err;
if (parse_version(line,mp) < 0)
@@ -567,46 +621,104 @@ parse_cmethod_line(char *line, managed_proxy_t *mp)
return r;
}
-/** Prepares the <b>envp</b> of a pluggable transport managed proxy
- *
- * <b>method</b> is a line with transport methods to be launched.
- * If <b>is_server</b> is set, prepare a server proxy <b>envp</b>. */
+/** Return a string containing the address:port that <b>transport</b>
+ * should use. */
+static char *
+get_bindaddr_for_proxy(managed_proxy_t *mp)
+{
+ char *bindaddr = NULL;
+ smartlist_t *string_tmp = smartlist_create();
+
+ tor_assert(mp->is_server);
+
+ SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
+ tor_asprintf(&bindaddr, "%s-%s", t, get_bindaddr_for_transport(t));
+ smartlist_add(string_tmp, bindaddr);
+ } SMARTLIST_FOREACH_END(t);
+
+ bindaddr = smartlist_join_strings(string_tmp, ",", 0, NULL);
+
+ SMARTLIST_FOREACH(string_tmp, char *, t, tor_free(t));
+ smartlist_free(string_tmp);
+
+ return bindaddr;
+}
+
+/** Prepare the <b>envp</b> of managed proxy <b>mp</b> */
static void
-set_environ(char ***envp, const char *method, int is_server)
+set_environ(char ***envp, managed_proxy_t *mp)
{
or_options_t *options = get_options();
char **tmp=NULL;
char *state_loc=NULL;
+ char *transports_to_launch=NULL;
+ char *bindaddr=NULL;
- int n_envs = is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
+ int n_envs = mp->is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
/* allocate enough space for our env. vars and a NULL pointer */
*envp = tor_malloc(sizeof(char*)*(n_envs+1));
tmp = *envp;
state_loc = get_datadir_fname("pt_state/");
+ transports_to_launch =
+ smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
- /* these should all be customizable */
tor_asprintf(tmp++, "HOME=%s", getenv("HOME"));
tor_asprintf(tmp++, "PATH=%s", getenv("PATH"));
tor_asprintf(tmp++, "TOR_PT_STATE_LOCATION=%s", state_loc);
tor_asprintf(tmp++, "TOR_PT_MANAGED_TRANSPORT_VER=1"); /* temp */
- if (is_server) {
- /* ASN check for ORPort values, should we be here if it's 0? */
+ if (mp->is_server) {
+ bindaddr = get_bindaddr_for_proxy(mp);
+
tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort); /* temp */
- tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=%s",
- get_bindaddr_for_transport(method));
- tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", method);
+ tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=%s", bindaddr);
+ tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", transports_to_launch);
tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200"); /* temp*/
} else {
- tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", method);
+ tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", transports_to_launch);
}
*tmp = NULL;
tor_free(state_loc);
+ tor_free(transports_to_launch);
+ tor_free(bindaddr);
+}
+
+/** Register <b>transport</b> using proxy with <b>proxy_argv</b> to
+ * the managed proxy subsystem.
+ * If <b>is_server</b> is true, then the proxy is a server proxy. */
+void
+pt_kickstart_proxy(char *transport, char **proxy_argv, int is_server)
+{
+ managed_proxy_t *mp=NULL;
+
+ mp = get_managed_proxy_by_argv(proxy_argv);
+
+ if (!mp) { /* we haven't seen this proxy before */
+ /* create a managed proxy */
+ managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
+ mp->conf_state = PT_PROTO_INFANT;
+ mp->is_server = is_server;
+ mp->argv = proxy_argv;
+ mp->transports = smartlist_create();
+
+ mp->transports_to_launch = smartlist_create();
+ add_transport_to_proxy(transport, mp);
+
+ /* register the managed proxy */
+ if (!unconfigured_proxy_list)
+ unconfigured_proxy_list = smartlist_create();
+ smartlist_add(unconfigured_proxy_list, mp);
+
+ n_unconfigured_proxies++; /* ASN should we care about overflows here?
+ I say no. */
+ } else { /* known proxy. just add transport to its transport list */
+ add_transport_to_proxy(transport, mp);
+ free_execve_args(proxy_argv);
+ }
}
-/* ASN is this too ugly/stupid? */
/** Frees the array of pointers in <b>arg</b> used as arguments to
execve. */
static INLINE void
diff --git a/src/or/transports.h b/src/or/transports.h
index 8bd79fe..c75797d 100644
--- a/src/or/transports.h
+++ b/src/or/transports.h
@@ -11,13 +11,13 @@
#ifndef TOR_TRANSPORTS_H
#define TOR_TRANSPORTS_H
-int pt_managed_launch_proxy(const char *method,
- char **proxy_argv, int is_server);
+void pt_kickstart_proxy(char *method, char **proxy_argv,
+ int is_server);
-#define pt_managed_launch_client_proxy(m, pa) \
- pt_managed_launch_proxy(m, pa, 0)
-#define pt_managed_launch_server_proxy(m, pa) \
- pt_managed_launch_proxy(m, pa, 1)
+#define pt_kickstart_client_proxy(m, pa) \
+ pt_kickstart_proxy(m, pa, 0)
+#define pt_kickstart_server_proxy(m, pa) \
+ pt_kickstart_proxy(m, pa, 1)
void pt_configure_remaining_proxies(void);
@@ -29,6 +29,7 @@ void pt_free_all(void);
/** State of the managed proxy configuration protocol. */
enum pt_proto_state {
PT_PROTO_INFANT, /* was just born */
+ PT_PROTO_LAUNCHED, /* was just launched */
PT_PROTO_ACCEPTING_METHODS, /* accepting methods */
PT_PROTO_CONFIGURED, /* configured successfully */
PT_PROTO_COMPLETED, /* configure and registered its transports */
@@ -38,6 +39,7 @@ enum pt_proto_state {
/** Structure containing information of a managed proxy. */
typedef struct {
enum pt_proto_state conf_state; /* the current configuration state */
+ char **argv; /* the cli arguments of this proxy */
int conf_protocol; /* the configuration protocol version used */
int is_server; /* is it a server proxy? */
@@ -45,7 +47,8 @@ typedef struct {
FILE *stdout; /* a stream to its stdout
(closed in managed_proxy_destroy()) */
- smartlist_t *transports; /* list of transport_t this proxy spawns */
+ smartlist_t *transports_to_launch; /* transports to-be-launched by this proxy */
+ smartlist_t *transports; /* list of transport_t this proxy spawned */
} managed_proxy_t;
int parse_cmethod_line(char *line, managed_proxy_t *mp);
1
0