tor-commits
Threads by month
- ----- 2026 -----
- 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
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
[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
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
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] Server transport proxies should bind on the same port each time, if possible.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit 941709ee50654b9ef59836fadbd8c4e7029c9fc1
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Sun Aug 7 18:05:40 2011 +0200
Server transport proxies should bind on the same port each time, if possible.
---
src/or/circuitbuild.c | 2 +-
src/or/config.c | 235 +++++++++++++++++++++++++++++++++++++++++++-----
src/or/config.h | 4 +
src/or/or.h | 2 +
src/or/transports.c | 78 ++++++++++++-----
src/or/transports.h | 4 +-
6 files changed, 276 insertions(+), 49 deletions(-)
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index 3dba83b..fe57070 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -4610,7 +4610,7 @@ transport_get_by_name(const char *name)
if (!transport_list)
return NULL;
-
+
SMARTLIST_FOREACH_BEGIN(transport_list, const transport_t *, transport) {
if (!strcmp(transport->name, name))
return transport;
diff --git a/src/or/config.c b/src/or/config.c
index 02925f3..dced47c 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -473,6 +473,9 @@ static config_var_t _state_vars[] = {
VAR("EntryGuardAddedBy", LINELIST_S, EntryGuards, NULL),
V(EntryGuards, LINELIST_V, NULL),
+ VAR("TransportProxy", LINELIST_S, TransportProxies, NULL),
+ V(TransportProxies, LINELIST_V, NULL),
+
V(BWHistoryReadEnds, ISOTIME, NULL),
V(BWHistoryReadInterval, UINT, "900"),
V(BWHistoryReadValues, CSV, ""),
@@ -499,7 +502,6 @@ static config_var_t _state_vars[] = {
V(CircuitBuildAbandonedCount, UINT, "0"),
VAR("CircuitBuildTimeBin", LINELIST_S, BuildtimeHistogram, NULL),
VAR("BuildtimeHistogram", LINELIST_V, BuildtimeHistogram, NULL),
-
{ NULL, CONFIG_TYPE_OBSOLETE, 0, NULL }
};
@@ -1212,29 +1214,6 @@ options_act(or_options_t *old_options)
if (consider_adding_dir_authorities(options, old_options) < 0)
return -1;
- clear_transport_list();
- if (options->ClientTransportPlugin) {
- for (cl = options->ClientTransportPlugin; cl; cl = cl->next) {
- if (parse_client_transport_line(cl->value, 0)<0) {
- log_warn(LD_BUG,
- "Previously validated ClientTransportPlugin line "
- "could not be added!");
- return -1;
- }
- }
- }
-
- if (options->ServerTransportPlugin) {
- for (cl = options->ServerTransportPlugin; cl; cl = cl->next) {
- if (parse_server_transport_line(cl->value, 0)<0) {
- log_warn(LD_BUG,
- "Previously validated ServerTransportPlugin line "
- "could not be added!");
- return -1;
- }
- }
- }
-
if (options->Bridges) {
mark_bridge_list();
for (cl = options->Bridges; cl; cl = cl->next) {
@@ -1271,6 +1250,30 @@ options_act(or_options_t *old_options)
rep_hist_load_mtbf_data(time(NULL));
}
+
+ clear_transport_list();
+ if (options->ClientTransportPlugin) {
+ for (cl = options->ClientTransportPlugin; cl; cl = cl->next) {
+ if (parse_client_transport_line(cl->value, 0)<0) {
+ log_warn(LD_BUG,
+ "Previously validated ClientTransportPlugin line "
+ "could not be added!");
+ return -1;
+ }
+ }
+ }
+
+ if (options->ServerTransportPlugin) {
+ for (cl = options->ServerTransportPlugin; cl; cl = cl->next) {
+ if (parse_server_transport_line(cl->value, 0)<0) {
+ log_warn(LD_BUG,
+ "Previously validated ServerTransportPlugin line "
+ "could not be added!");
+ return -1;
+ }
+ }
+ }
+
/* 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)
@@ -5465,6 +5468,74 @@ options_get_datadir_fname2_suffix(or_options_t *options,
return fname;
}
+/** Return true if <b>line</b> is a valid state TransportProxy line.
+ * Return false otherwise. */
+static int
+state_transport_line_is_valid(char *line)
+{
+ smartlist_t *items = NULL;
+ char *addrport=NULL;
+ tor_addr_t addr;
+ uint16_t port = 0;
+ int r;
+
+ items = smartlist_create();
+ smartlist_split_string(items, line, NULL,
+ SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
+
+ if (smartlist_len(items) != 2) {
+ log_warn(LD_CONFIG, "state: Not enough arguments in TransportProxy line.");
+ goto err;
+ }
+
+ addrport = smartlist_get(items, 1);
+ if (tor_addr_port_parse(addrport, &addr, &port) < 0) {
+ log_warn(LD_CONFIG, "state: Could not parse addrport.");
+ goto err;
+ }
+
+ if (!port) {
+ log_warn(LD_CONFIG, "state: Transport line did not contain port.");
+ goto err;
+ }
+
+ r = 1;
+ goto done;
+
+ err:
+ r = 0;
+
+ done:
+ SMARTLIST_FOREACH(items, char*, s, tor_free(s));
+ smartlist_free(items);
+ return r;
+}
+
+/** Return 0 if all TransportProxy lines in <b>state</b> are well
+ * formed. Otherwise, return -1. */
+static int
+validate_transports_in_state(or_state_t *state)
+{
+ int broken = 0;
+
+ 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)
+ broken = 1;
+ }
+
+ if (broken)
+ log_warn(LD_CONFIG, "state: State file seems to be broken.");
+
+ return 0;
+}
+
/** Return 0 if every setting in <b>state</b> is reasonable, and a
* permissible transition from <b>old_state</b>. Else warn and return -1.
* Should have no side effects, except for normalizing the contents of
@@ -5483,6 +5554,9 @@ or_state_validate(or_state_t *old_state, or_state_t *state,
if (entry_guards_parse_state(state, 0, msg)<0)
return -1;
+ if (validate_transports_in_state(state)<0)
+ return -1;
+
return 0;
}
@@ -5715,6 +5789,118 @@ or_state_save(time_t now)
return 0;
}
+/** Return the config line for transport <b>transport</b> in the current state.
+ * Return NULL if there is no config line for <b>transport</b>. */
+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;
+
+ search = search->next;
+ }
+ return NULL;
+}
+
+/** 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 *
+get_transport_bindaddr(const char *line, const char *transport)
+{
+ if (strlen(line) < strlen(transport) + 2)
+ return NULL;
+ else
+ return (line+strlen(transport)+1);
+}
+
+/** Return a string containing the address:port that <b>transport</b>
+ * should use. */
+const char *
+get_bindaddr_for_transport(const char *transport)
+{
+ static const char default_addrport[] = "127.0.0.1:0";
+ const char *bindaddr = NULL;
+
+ config_line_t *line = get_transport_in_state_by_name(transport);
+ if (!line)
+ return default_addrport;
+
+ bindaddr = get_transport_bindaddr(line->value, transport);
+
+ return bindaddr ? bindaddr : default_addrport;
+}
+
+/** Save <b>transport</b> listening at <b>addr</b>:<b>port</b> to
+ * state */
+void
+save_transport_to_state(const char *transport,
+ tor_addr_t *addr, uint16_t port)
+{
+ or_state_t *state = get_or_state();
+
+ char *transport_addrport=NULL;
+
+ /** find where to write on the state */
+ config_line_t **next, *line;
+
+ /* see if this transport is already stored in state */
+ config_line_t *transport_line =
+ get_transport_in_state_by_name(transport);
+
+ if (transport_line) { /* if transport_exists_in_state() */
+ const char *prev_bindaddr = /* get addrport stored in state */
+ get_transport_bindaddr(transport_line->value, transport);
+ tor_asprintf(&transport_addrport, "%s:%d", fmt_addr(addr), (int)port);
+
+ /* if transport in state has the same address as this one, life is good */
+ if (!strcmp(prev_bindaddr, transport_addrport)) {
+ log_warn(LD_CONFIG, "Transport seems to have spawned on its usual address:port.");
+ goto done;
+ } else { /* addrport in state is different than the one we got */
+ log_warn(LD_CONFIG, "Transport seems to have spawned on different address:port."
+ "Let's update the state file with the new address:port");
+ tor_free(transport_line->value); /* free the old line */
+ tor_asprintf(&transport_line->value, "%s %s:%d", transport,
+ fmt_addr(addr),
+ (int) port); /* replace old addrport line with new line */
+ }
+ } else { /* never seen this one before; save it in state for next time */
+ log_warn(LD_CONFIG, "It's the first time we see this transport. "
+ "Let's save its address:port");
+ next = &state->TransportProxies;
+ /* find the last TransportProxy line in the state and point 'next'
+ right after it */
+ line = state->TransportProxies;
+ while (line) {
+ next = &(line->next);
+ line = line->next;
+ }
+
+ /* allocate space for the new line and fill it in */
+ *next = line = tor_malloc_zero(sizeof(config_line_t));
+ line->key = tor_strdup("TransportProxy");
+ tor_asprintf(&line->value, "%s %s:%d", transport,
+ fmt_addr(addr), (int) port);
+
+ next = &(line->next);
+ }
+
+ if (!get_options()->AvoidDiskWrites)
+ or_state_mark_dirty(state, 0);
+
+ done:
+ tor_free(transport_addrport);
+}
+
/** Given a file name check to see whether the file exists but has not been
* modified for a very long time. If so, remove it. */
void
@@ -5782,4 +5968,3 @@ getinfo_helper_config(control_connection_t *conn,
}
return 0;
}
-
diff --git a/src/or/config.h b/src/or/config.h
index 49f7e25..dc3a828 100644
--- a/src/or/config.h
+++ b/src/or/config.h
@@ -63,6 +63,10 @@ or_state_t *get_or_state(void);
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 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,
const char *question, char **answer,
diff --git a/src/or/or.h b/src/or/or.h
index 8bcfc82..d07422f 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -3139,6 +3139,8 @@ typedef struct {
/** A list of Entry Guard-related configuration lines. */
config_line_t *EntryGuards;
+ config_line_t *TransportProxies;
+
/** These fields hold information on the history of bandwidth usage for
* servers. The "Ends" fields hold the time when we last updated the
* bandwidth usage. The "Interval" fields hold the granularity, in seconds,
diff --git a/src/or/transports.c b/src/or/transports.c
index 21d76f8..b96792f 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -19,10 +19,11 @@ 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 register_server_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)
@@ -119,6 +120,7 @@ pt_managed_launch_proxy(const char *method,
mp->conf_state = PT_PROTO_INFANT;
mp->stdout = stdout_read;
mp->transports = smartlist_create();
+ mp->is_server = is_server;
/* register the managed proxy */
if (!unconfigured_proxy_list)
@@ -179,6 +181,42 @@ 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)
+{
+ 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);
+ }
+}
+
+/** 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)
+{
+ 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);
+}
+
+/** Register the transports of managed proxy <b>mp</b>. */
+static INLINE void
+register_proxy(managed_proxy_t *mp)
+{
+ if (mp->is_server)
+ register_server_proxy(mp);
+ else
+ register_client_proxy(mp);
+}
+
/** Handle a configured or broken managed proxy <b>mp</b>. */
static void
handle_finished_proxy(managed_proxy_t *mp)
@@ -188,7 +226,7 @@ handle_finished_proxy(managed_proxy_t *mp)
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, */
+ 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 */
@@ -203,20 +241,6 @@ handle_finished_proxy(managed_proxy_t *mp)
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. */
@@ -254,8 +278,6 @@ proxy_configuration_finished(managed_proxy_t *mp)
void
handle_proxy_line(char *line, managed_proxy_t *mp)
{
- log_debug(LD_CONFIG, "Judging line: %s\n", line);
-
if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
"Discarding");
@@ -401,7 +423,8 @@ parse_method_error(char *line, int is_server)
line+strlen(error)+1);
}
-/** Parses an SMETHOD <b>line</b>. */
+/** 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)
{
@@ -414,6 +437,8 @@ parse_smethod_line(char *line, managed_proxy_t *mp)
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);
@@ -440,6 +465,12 @@ parse_smethod_line(char *line, managed_proxy_t *mp)
goto err;
}
+ transport = transport_create(&addr, port, method_name, PROXY_NONE);
+ if (!transport)
+ goto err;
+
+ smartlist_add(mp->transports, transport);
+
/* 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.",
@@ -553,23 +584,26 @@ set_environ(char ***envp, const char *method, int is_server)
*envp = tor_malloc(sizeof(char*)*(n_envs+1));
tmp = *envp;
+ state_loc = get_datadir_fname("pt_state/");
+
/* 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_BINDADDR=%s",
+ get_bindaddr_for_transport(method));
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;
+
+ tor_free(state_loc);
}
/* ASN is this too ugly/stupid? */
diff --git a/src/or/transports.h b/src/or/transports.h
index 17a6803..8bd79fe 100644
--- a/src/or/transports.h
+++ b/src/or/transports.h
@@ -40,10 +40,12 @@ typedef struct {
enum pt_proto_state conf_state; /* the current configuration state */
int conf_protocol; /* the configuration protocol version used */
+ int is_server; /* is it a server proxy? */
+
FILE *stdout; /* a stream to its stdout
(closed in managed_proxy_destroy()) */
- smartlist_t *transports; /* list of transports this proxy spawns */
+ smartlist_t *transports; /* list of transport_t this proxy spawns */
} managed_proxy_t;
int parse_cmethod_line(char *line, managed_proxy_t *mp);
1
0
commit cfb473ed348063e1f1abd709ac313f14d33cadf5
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Mon Jul 18 17:08:55 2011 +0200
Changed a printf() to a log_debug().
---
src/or/transports.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/src/or/transports.c b/src/or/transports.c
index 392c43d..21d76f8 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -254,7 +254,7 @@ proxy_configuration_finished(managed_proxy_t *mp)
void
handle_proxy_line(char *line, managed_proxy_t *mp)
{
- printf("Judging line: %s\n", line);
+ log_debug(LD_CONFIG, "Judging line: %s\n", line);
if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
1
0