tor-commits
Threads by month
- ----- 2026 -----
- June
- May
- April
- March
- 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
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
[tor/master] Fix bug in get_transport_in_state_by_name() when using strcmpstart().
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit ebc232bb79c8816d17eea75d3a29c1ecb25da0b0
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Sun Sep 11 21:22:37 2011 +0200
Fix bug in get_transport_in_state_by_name() when using strcmpstart().
We now split the state lines into smartlists and compare the token
properly. Not that efficient but it's surely correct.
---
src/or/config.c | 27 ++++++++++++++++++++++++---
1 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/src/or/config.c b/src/or/config.c
index bacdae3..792124c 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -5787,13 +5787,34 @@ get_transport_in_state_by_name(const char *transport)
{
or_state_t *or_state = get_or_state();
config_line_t *line;
+ config_line_t *ret = NULL;
+ smartlist_t *items = NULL;
for (line = or_state->TransportProxies ; line ; line = line->next) {
tor_assert(!strcmp(line->key, "TransportProxy"));
- if (!strcmpstart(line->value, transport))
- return line;
+
+ items = smartlist_create();
+ smartlist_split_string(items, line->value, NULL,
+ SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
+ if (smartlist_len(items) != 2) /* broken state */
+ goto done;
+
+ if (!strcmp(smartlist_get(items, 0), transport)) {
+ ret = line;
+ goto done;
+ }
+
+ SMARTLIST_FOREACH(items, char*, s, tor_free(s));
+ smartlist_free(items);
+ items = NULL;
}
- return NULL;
+
+ done:
+ if (items) {
+ SMARTLIST_FOREACH(items, char*, s, tor_free(s));
+ smartlist_free(items);
+ }
+ return ret;
}
/** Return string containing the address:port part of the
1
0
commit 31361074216534b0a4c6377730c09b261ca4ef4e
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Sun Sep 11 23:33:31 2011 +0200
Trivial fixes around the code.
* C90-fy.
* Remove ASN comments.
* Don't smartlist_clear() before smartlist_free().
* Plug a mem. leak.
---
src/or/circuitbuild.c | 4 ++--
src/or/config.c | 12 ++++++------
src/or/transports.c | 17 ++++++++++++-----
3 files changed, 20 insertions(+), 13 deletions(-)
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index bd06d31..7338e24 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -4722,9 +4722,10 @@ transport_resolve_conflicts(transport_t *t)
int
transport_add(transport_t *t)
{
+ int r;
tor_assert(t);
- int r = transport_resolve_conflicts(t);
+ r = transport_resolve_conflicts(t);
switch (r) {
case 0: /* should register transport */
@@ -5016,7 +5017,6 @@ fetch_bridge_descriptors(or_options_t *options, time_t now)
if (!bridge_list)
return;
- /* ASN Should we move this to launch_direct_bridge_descriptor_fetch() ? */
/* If we still have unconfigured managed proxies, don't go and
connect to a bridge. */
if (pt_proxies_configuration_pending())
diff --git a/src/or/config.c b/src/or/config.c
index 79c1b1e..d36418b 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1226,11 +1226,6 @@ options_act(or_options_t *old_options)
sweep_bridge_list();
}
- /* If we have pluggable transport related options enabled, see if we
- should warn the user about potential configuration problems. */
- if (options->Bridges || options->ClientTransportPlugin)
- validate_pluggable_transports_config();
-
if (running_tor && rend_config_services(options, 0)<0) {
log_warn(LD_BUG,
"Previously validated hidden services line could not be added!");
@@ -1277,6 +1272,11 @@ options_act(or_options_t *old_options)
sweep_transport_list();
sweep_proxy_list();
+ /* If we have pluggable transport related options enabled, see if we
+ should warn the user about potential configuration problems. */
+ if (options->Bridges || options->ClientTransportPlugin)
+ validate_pluggable_transports_config();
+
/* 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. */
if (!running_tor)
@@ -5820,7 +5820,7 @@ 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. */
-const char *
+static const char *
get_transport_bindaddr(const char *line, const char *transport)
{
char *line_tmp = NULL;
diff --git a/src/or/transports.c b/src/or/transports.c
index 91ff518..1b7249f 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -13,7 +13,6 @@
#include "transports.h"
#include "util.h"
-/* ASN TIDY THESE UP*/
static void set_managed_proxy_environment(char ***envp, const managed_proxy_t *mp);
static INLINE int proxy_configuration_finished(const managed_proxy_t *mp);
@@ -372,11 +371,16 @@ register_server_proxy(managed_proxy_t *mp)
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? */
+ save_transport_to_state(t->name, &t->addr, t->port);
smartlist_add(sm_tmp, tor_strdup(t->name));
} SMARTLIST_FOREACH_END(t);
+ /* Since server proxies don't register their transports in the
+ circuitbuild.c subsystem, it's our duty to free them when we
+ switch mp->transports to strings. */
+ SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
smartlist_free(mp->transports);
+
mp->transports = sm_tmp;
}
@@ -438,7 +442,6 @@ managed_proxy_destroy(managed_proxy_t *mp)
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 */
@@ -469,8 +472,12 @@ handle_finished_proxy(managed_proxy_t *mp)
register_proxy(mp); /* register transports */
mp->conf_state = PT_PROTO_COMPLETED; /* mark it as completed. */
break;
+ case PT_PROTO_INFANT:
+ case PT_PROTO_LAUNCHED:
+ case PT_PROTO_ACCEPTING_METHODS:
+ case PT_PROTO_COMPLETED:
default:
- log_warn(LD_CONFIG, "Unfinished managed proxy in "
+ log_warn(LD_CONFIG, "Unexpected managed proxy state in "
"handle_finished_proxy().");
tor_assert(0);
}
@@ -891,6 +898,7 @@ void
pt_kickstart_proxy(const char *transport, char **proxy_argv, int is_server)
{
managed_proxy_t *mp=NULL;
+ transport_t *old_transport = NULL;
mp = get_managed_proxy_by_argv_and_type(proxy_argv, is_server);
@@ -911,7 +919,6 @@ pt_kickstart_proxy(const char *transport, char **proxy_argv, int is_server)
unconfigured_proxies_n++;
}
- transport_t *old_transport = NULL;
old_transport = transport_get_by_name(transport);
if (old_transport)
old_transport->marked_for_removal = 0;
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 a8f21f91cfa916b662d07dd486857fdf848c6f1d
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Mon Jul 18 02:33:31 2011 +0200
Updated #includes etc. to use transports.[ch].
---
src/or/Makefile.am | 4 ++--
src/or/circuitbuild.c | 2 +-
src/or/config.c | 2 +-
src/or/main.c | 2 +-
src/or/transports.c | 4 ++--
src/or/transports.h | 8 ++++----
src/test/test_pt.c | 2 +-
7 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/src/or/Makefile.am b/src/or/Makefile.am
index 4dc5ee5..41e89d7 100644
--- a/src/or/Makefile.am
+++ b/src/or/Makefile.am
@@ -39,7 +39,7 @@ libtor_a_SOURCES = \
networkstatus.c \
nodelist.c \
onion.c \
- pluggable_transports.c \
+ transports.c \
policies.c \
reasons.c \
relay.c \
@@ -105,7 +105,7 @@ noinst_HEADERS = \
ntmain.h \
onion.h \
or.h \
- pluggable_transports.h \
+ transports.h \
policies.h \
reasons.h \
relay.h \
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index 003f579..3dba83b 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -26,7 +26,7 @@
#include "nodelist.h"
#include "onion.h"
#include "policies.h"
-#include "pluggable_transports.h"
+#include "transports.h"
#include "relay.h"
#include "rephist.h"
#include "router.h"
diff --git a/src/or/config.c b/src/or/config.c
index 5f2f11d..da35270 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -35,7 +35,7 @@
#include "router.h"
#include "util.h"
#include "routerlist.h"
-#include "pluggable_transports.h"
+#include "transports.h"
#ifdef MS_WINDOWS
#include <shlobj.h>
#endif
diff --git a/src/or/main.c b/src/or/main.c
index d79d9eb..5294e4a 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -37,7 +37,7 @@
#include "ntmain.h"
#include "onion.h"
#include "policies.h"
-#include "pluggable_transports.h"
+#include "transports.h"
#include "relay.h"
#include "rendclient.h"
#include "rendcommon.h"
diff --git a/src/or/transports.c b/src/or/transports.c
index 49b0e13..aae39cd 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -2,7 +2,7 @@
/* See LICENSE for licensing information */
/**
- * \file pluggable_transports.c
+ * \file transports.c
* \brief Pluggable Transports related code.
**/
@@ -10,7 +10,7 @@
#include "or.h"
#include "config.h"
#include "circuitbuild.h"
-#include "pluggable_transports.h"
+#include "transports.h"
/* ASN TIDY THESE UP*/
static void set_environ(char ***envp, const char *method,
diff --git a/src/or/transports.h b/src/or/transports.h
index 80d5429..17a6803 100644
--- a/src/or/transports.h
+++ b/src/or/transports.h
@@ -4,12 +4,12 @@
/* See LICENSE for licensing information */
/**
- * \file pluggable_transpots.h
- * \brief Headers for pluggable_transpots.c
+ * \file transports.h
+ * \brief Headers for transports.c
**/
-#ifndef TOR_PLUGGABLE_TRANSPORTS_H
-#define TOR_PLUGGABLE_TRANSPORTS_H
+#ifndef TOR_TRANSPORTS_H
+#define TOR_TRANSPORTS_H
int pt_managed_launch_proxy(const char *method,
char **proxy_argv, int is_server);
diff --git a/src/test/test_pt.c b/src/test/test_pt.c
index 02950b8..99fc514 100644
--- a/src/test/test_pt.c
+++ b/src/test/test_pt.c
@@ -6,7 +6,7 @@
#include "orconfig.h"
#define PT_PRIVATE
#include "or.h"
-#include "pluggable_transports.h"
+#include "transports.h"
#include "circuitbuild.h"
#include "test.h"
1
0
07 Oct '11
commit 69271b2a383f6fe53ed7d5ca21cc4bd89c25de17
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Mon Jul 18 17:06:16 2011 +0200
Reuse get_string_from_pipe() in log_from_pipe().
---
src/common/util.c | 91 +++++++++++++++++++++-----------------------------
src/or/transports.c | 2 -
2 files changed, 38 insertions(+), 55 deletions(-)
diff --git a/src/common/util.c b/src/common/util.c
index c2db542..7a7ee19 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -3173,6 +3173,12 @@ get_string_from_pipe(FILE *stream, char *buf_out, size_t count)
if (buf_out[len - 1] == '\n') {
/* Remove the trailing newline */
buf_out[len - 1] = '\0';
+ } else {
+ /* No newline; check whether we overflowed the buffer */
+ if (!feof(stream))
+ log_warn(LD_GENERAL,
+ "Line from stream was truncated: %s", buf_out);
+ /* TODO: What to do with this error? */
}
return IO_STREAM_OKAY;
@@ -3192,65 +3198,44 @@ log_from_pipe(FILE *stream, int severity, const char *executable,
int *child_status)
{
char buf[256];
+ enum stream_status r;
for (;;) {
- char *retval;
- retval = fgets(buf, sizeof(buf), stream);
-
- if (NULL == retval) {
- if (feof(stream)) {
- /* Program has closed stream (probably it exited) */
- /* TODO: check error */
- fclose(stream);
- return 1;
- } else {
- if (EAGAIN == errno) {
- /* Nothing more to read, try again next time */
- return 0;
- } else {
- /* There was a problem, abandon this child process */
- fclose(stream);
- return -1;
- }
- }
- } else {
- /* We have some data, log it and keep asking for more */
- size_t len;
+ r = get_string_from_pipe(stream, buf, sizeof(buf) - 1);
- len = strlen(buf);
- if (buf[len - 1] == '\n') {
- /* Remove the trailing newline */
- buf[len - 1] = '\0';
- } else {
- /* No newline; check whether we overflowed the buffer */
- if (!feof(stream))
- log_warn(LD_GENERAL,
- "Line from port forwarding helper was truncated: %s", buf);
- /* TODO: What to do with this error? */
- }
+ if (r == IO_STREAM_CLOSED) {
+ fclose(stream);
+ return 1;
+ } else if (r == IO_STREAM_EAGAIN) {
+ return 0;
+ } else if (r == IO_STREAM_TERM) {
+ fclose(stream);
+ return -1;
+ }
- /* Check if buf starts with SPAWN_ERROR_MESSAGE */
- if (strcmpstart(buf, SPAWN_ERROR_MESSAGE) == 0) {
- /* Parse error message */
- int retval, child_state, saved_errno;
- retval = tor_sscanf(buf, SPAWN_ERROR_MESSAGE "%x/%x",
- &child_state, &saved_errno);
- if (retval == 2) {
- log_warn(LD_GENERAL,
- "Failed to start child process \"%s\" in state %d: %s",
- executable, child_state, strerror(saved_errno));
- if (child_status)
- *child_status = 1;
- } else {
- /* Failed to parse message from child process, log it as a
- warning */
- log_warn(LD_GENERAL,
- "Unexpected message from port forwarding helper \"%s\": %s",
- executable, buf);
- }
+ tor_assert(r == IO_STREAM_OKAY);
+
+ /* Check if buf starts with SPAWN_ERROR_MESSAGE */
+ if (strcmpstart(buf, SPAWN_ERROR_MESSAGE) == 0) {
+ /* Parse error message */
+ int retval, child_state, saved_errno;
+ retval = tor_sscanf(buf, SPAWN_ERROR_MESSAGE "%x/%x",
+ &child_state, &saved_errno);
+ if (retval == 2) {
+ log_warn(LD_GENERAL,
+ "Failed to start child process \"%s\" in state %d: %s",
+ executable, child_state, strerror(saved_errno));
+ if (child_status)
+ *child_status = 1;
} else {
- log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", buf);
+ /* Failed to parse message from child process, log it as a
+ warning */
+ log_warn(LD_GENERAL,
+ "Unexpected message from port forwarding helper \"%s\": %s",
+ executable, buf);
}
+ } else {
+ log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", buf);
}
}
diff --git a/src/or/transports.c b/src/or/transports.c
index 930cb8c..392c43d 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -157,8 +157,6 @@ configure_proxy(managed_proxy_t *mp)
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);
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