tor-commits
Threads by month
- ----- 2026 -----
- July
- 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
- 1 participants
- 215413 discussions
[tor/master] Replaced some leftover assert()s with tor_assert()s.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit c852760b80c4b7e06d775bafdb1f386d0e8ced63
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Sat Sep 10 00:45:28 2011 +0200
Replaced some leftover assert()s with tor_assert()s.
---
src/or/circuitbuild.c | 2 +-
src/or/transports.c | 5 ++---
2 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index fe57070..382016e 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -4640,7 +4640,7 @@ transport_create(const tor_addr_t *addr, uint16_t port,
int
transport_add(transport_t *t)
{
- assert(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 "
diff --git a/src/or/transports.c b/src/or/transports.c
index 21a3022..6255a56 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -174,8 +174,7 @@ 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. */
- if (proxy_configuration_finished(mp))
- assert(0);
+ tor_assert(!proxy_configuration_finished(mp));
configure_proxy(mp);
@@ -307,7 +306,7 @@ handle_finished_proxy(managed_proxy_t *mp)
default:
log_warn(LD_CONFIG, "Unfinished managed proxy in "
"handle_finished_proxy().");
- assert(0);
+ tor_assert(0);
}
tor_assert(smartlist_len(unconfigured_proxy_list) >= 0);
1
0
[tor/master] Spawn multiple protocols using a single managed proxy.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit ea3e9416c6c579735ff2b844a88605ae5e1cbbd2
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Fri Aug 12 21:33:05 2011 +0200
Spawn multiple protocols using a single managed proxy.
If multiple torrc transport lines have the same argv, tor instructs a
single managed proxy to launch multiple protocols.
---
src/or/config.c | 42 +++++------
src/or/config.h | 2 +-
src/or/transports.c | 196 ++++++++++++++++++++++++++++++++++++++++-----------
src/or/transports.h | 17 +++--
4 files changed, 184 insertions(+), 73 deletions(-)
diff --git a/src/or/config.c b/src/or/config.c
index dced47c..41b6bba 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1228,7 +1228,7 @@ options_act(or_options_t *old_options)
/* If we have pluggable transport related options enabled, see if we
should warn the user about potential configuration problems. */
- if (options->Bridges || options->ClientTransportPlugin)
+ if (options->Bridges || options->ClientTransportPlugin || options->ServerTransportPlugin)
validate_pluggable_transports_config();
if (running_tor && rend_config_services(options, 0)<0) {
@@ -4745,15 +4745,14 @@ parse_client_transport_line(const char *line, int validate_only)
tor_assert(proxy_argc > 0);
proxy_argv = tor_malloc_zero(sizeof(char*)*(proxy_argc+1));
tmp = proxy_argv;
- for (i=0;i<proxy_argc;i++) /* store arguments */
- *tmp++ = smartlist_get(items, 2+i);
+ for (i=0;i<proxy_argc;i++) { /* store arguments */
+ *tmp++ = smartlist_get(items, 2);
+ smartlist_del_keeporder(items, 2);
+ }
*tmp = NULL; /*terminated with NUL pointer, just like execve() likes it*/
- if (pt_managed_launch_client_proxy(name, proxy_argv) < 0) {
- log_warn(LD_CONFIG, "Error while launching managed proxy at '%s'",
- proxy_argv[0]);
- goto err;
- }
+ /* kickstart the thing */
+ pt_kickstart_client_proxy(name, proxy_argv);
}
} else { /* external */
addrport = smartlist_get(items, 2);
@@ -4789,7 +4788,6 @@ parse_client_transport_line(const char *line, int validate_only)
done:
SMARTLIST_FOREACH(items, char*, s, tor_free(s));
smartlist_free(items);
- tor_free(proxy_argv);
return r;
}
@@ -4847,15 +4845,14 @@ parse_server_transport_line(const char *line, int validate_only)
proxy_argv = tor_malloc_zero(sizeof(char*)*(proxy_argc+1));
tmp = proxy_argv;
- for (i=0;i<proxy_argc;i++) /* store arguments */
- *tmp++ = smartlist_get(items, 2+i);
+ for (i=0;i<proxy_argc;i++) { /* store arguments */
+ *tmp++ = smartlist_get(items, 2);
+ smartlist_del_keeporder(items, 2);
+ }
*tmp = NULL; /*terminated with NUL pointer, just like execve() likes it*/
- if (pt_managed_launch_server_proxy(name, proxy_argv) < 0) { /* launch it! */
- log_warn(LD_CONFIG, "Error while launching managed proxy at '%s'",
- proxy_argv[0]);
- goto err;
- }
+ /* kickstart the thing */
+ pt_kickstart_server_proxy(name, proxy_argv);
}
} else { /* external */
addrport = smartlist_get(items, 2);
@@ -4886,7 +4883,6 @@ parse_server_transport_line(const char *line, int validate_only)
done:
SMARTLIST_FOREACH(items, char*, s, tor_free(s));
smartlist_free(items);
- tor_free(proxy_argv);
return r;
}
@@ -5811,8 +5807,8 @@ get_transport_in_state_by_name(const char *transport)
}
/** Return string containing the address:port part of the
- * TransportProxy <b>line</b> for transport <b>transport</b>. If the
- * line is corrupted, return NULL. */
+ * TransportProxy <b>line</b> for transport <b>transport</b>.
+ * If the line is corrupted, return NULL. */
const char *
get_transport_bindaddr(const char *line, const char *transport)
{
@@ -5822,8 +5818,8 @@ get_transport_bindaddr(const char *line, const char *transport)
return (line+strlen(transport)+1);
}
-/** Return a string containing the address:port that <b>transport</b>
- * should use. */
+/** Return a static string containing the address:port a proxy
+ * transport should bind on. */
const char *
get_bindaddr_for_transport(const char *transport)
{
@@ -5839,8 +5835,8 @@ get_bindaddr_for_transport(const char *transport)
return bindaddr ? bindaddr : default_addrport;
}
-/** Save <b>transport</b> listening at <b>addr</b>:<b>port</b> to
- * state */
+/** Save <b>transport</b> listening on <b>addr</b>:<b>port</b> to
+ state */
void
save_transport_to_state(const char *transport,
tor_addr_t *addr, uint16_t port)
diff --git a/src/or/config.h b/src/or/config.h
index dc3a828..d09d904 100644
--- a/src/or/config.h
+++ b/src/or/config.h
@@ -65,7 +65,7 @@ int or_state_save(time_t now);
void save_transport_to_state(const char *transport_name,
tor_addr_t *addr, uint16_t port);
-const char * get_bindaddr_for_transport(const char *transport);
+const char *get_bindaddr_for_transport(const char *transport);
int options_need_geoip_info(or_options_t *options, const char **reason_out);
int getinfo_helper_config(control_connection_t *conn,
diff --git a/src/or/transports.c b/src/or/transports.c
index b96792f..c414920 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -13,8 +13,7 @@
#include "transports.h"
/* ASN TIDY THESE UP*/
-static void set_environ(char ***envp, const char *method,
- int is_server);
+static void set_environ(char ***envp, managed_proxy_t *mp);
static INLINE int proxy_configuration_finished(managed_proxy_t *mp);
static void managed_proxy_destroy(managed_proxy_t *mp,
@@ -82,11 +81,55 @@ pt_proxies_configuration_pending(void)
return !!n_unconfigured_proxies;
}
-/** Launch a proxy for <b>method</b> using <b>proxy_argv</b> as its
- * arguments. If <b>is_server</b>, launch a server proxy. */
-int
-pt_managed_launch_proxy(const char *method,
- char **proxy_argv, int is_server)
+/** Return true if <b>mp</b> has the same argv as <b>proxy_argv</b> */
+static int
+managed_proxy_has_argv(managed_proxy_t *mp, char **proxy_argv)
+{
+ char **tmp1=proxy_argv;
+ char **tmp2=mp->argv;
+
+ tor_assert(tmp1);
+ tor_assert(tmp2);
+
+ while (*tmp1 && *tmp2) {
+ if (strcmp(*tmp1++, *tmp2++))
+ return 0;
+ }
+
+ if (!*tmp1 && !*tmp2)
+ return 1;
+
+ return 0;
+}
+
+/** Return a managed proxy with the same argv as <b>proxy_argv</b>.
+ * If no such managed proxy exists, return NULL. */
+static managed_proxy_t *
+get_managed_proxy_by_argv(char **proxy_argv)
+{
+ if (!unconfigured_proxy_list)
+ return NULL;
+
+ SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
+ if (managed_proxy_has_argv(mp, proxy_argv))
+ return mp;
+ } SMARTLIST_FOREACH_END(mp);
+
+ return NULL;
+}
+
+/** Add <b>transport</b> to managed proxy <b>mp</b>. */
+static void
+add_transport_to_proxy(char *transport, managed_proxy_t *mp)
+{
+ tor_assert(mp->transports_to_launch);
+ if (!smartlist_string_isin(mp->transports_to_launch, transport))
+ smartlist_add(mp->transports_to_launch, tor_strdup(transport));
+}
+
+/** Launch managed proxy <b>mp</b>. */
+static int
+launch_managed_proxy(managed_proxy_t *mp)
{
char **envp=NULL;
int retval;
@@ -94,11 +137,16 @@ pt_managed_launch_proxy(const char *method,
int stdout_pipe=-1, stderr_pipe=-1;
/* prepare the environment variables for the managed proxy */
- set_environ(&envp, method, is_server);
+ set_environ(&envp, mp);
+
+ char **tmp = envp;
+ printf("PRINTING ENVP\n");
+ while (*tmp)
+ printf("%s\n", *tmp++);
/* ASN we should probably check if proxy_argv[0] is executable by our user */
- retval = tor_spawn_background(proxy_argv[0], &stdout_pipe,
- &stderr_pipe, (const char **)proxy_argv,
+ retval = tor_spawn_background(mp->argv[0], &stdout_pipe,
+ &stderr_pipe, (const char **)mp->argv,
(const char **)envp);
if (retval < 0) {
log_warn(LD_GENERAL, "Spawn failed");
@@ -115,20 +163,8 @@ pt_managed_launch_proxy(const char *method,
log_warn(LD_CONFIG, "The spawn is alive (%d)!", retval);
- /* create a managed proxy */
- managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
- mp->conf_state = PT_PROTO_INFANT;
+ mp->conf_state = PT_PROTO_LAUNCHED;
mp->stdout = stdout_read;
- mp->transports = smartlist_create();
- mp->is_server = is_server;
-
- /* register the managed proxy */
- if (!unconfigured_proxy_list)
- unconfigured_proxy_list = smartlist_create();
- smartlist_add(unconfigured_proxy_list, mp);
-
- n_unconfigured_proxies++; /* ASN should we care about overflows here?
- I say no. */
return 0;
}
@@ -150,14 +186,21 @@ pt_configure_remaining_proxies(void)
} SMARTLIST_FOREACH_END(mp);
}
-/** Receive input from the managed proxy <b>mp</b> to get closer to
- * finally configuring it. */
+/** Attempt to continue configuring managed proxy <b>mp</b>. */
static void
configure_proxy(managed_proxy_t *mp)
{
enum stream_status r;
char stdout_buf[200];
+ /* if we haven't launched the proxy yet, do it now */
+ if (mp->conf_state == PT_PROTO_INFANT) {
+ launch_managed_proxy(mp);
+ return;
+ }
+
+ tor_assert(mp->conf_state != PT_PROTO_INFANT);
+
while (1) {
r = get_string_from_pipe(mp->stdout, stdout_buf,
sizeof(stdout_buf) - 1);
@@ -255,12 +298,21 @@ managed_proxy_destroy(managed_proxy_t *mp, int also_free_transports)
smartlist_clear(mp->transports);
smartlist_free(mp->transports);
+ SMARTLIST_FOREACH(mp->transports_to_launch, char *, t, tor_free(t));
+
+ /* free the transports smartlist */
+ smartlist_clear(mp->transports_to_launch);
+ smartlist_free(mp->transports_to_launch);
+
/* remove it from the list of managed proxies */
smartlist_remove(unconfigured_proxy_list, mp);
/* close its stdout stream */
fclose(mp->stdout);
+ /* free the argv */
+ free_execve_args(mp->argv);
+
tor_free(mp);
}
@@ -278,6 +330,8 @@ proxy_configuration_finished(managed_proxy_t *mp)
void
handle_proxy_line(char *line, managed_proxy_t *mp)
{
+ printf("Judging line: %s\n", line);
+
if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
"Discarding");
@@ -285,13 +339,13 @@ handle_proxy_line(char *line, managed_proxy_t *mp)
}
if (!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR))) {
- if (mp->conf_state != PT_PROTO_INFANT)
+ if (mp->conf_state != PT_PROTO_LAUNCHED)
goto err;
parse_env_error(line);
goto err;
} else if (!strncmp(line, PROTO_NEG_FAIL, strlen(PROTO_NEG_FAIL))) {
- if (mp->conf_state != PT_PROTO_INFANT)
+ if (mp->conf_state != PT_PROTO_LAUNCHED)
goto err;
log_warn(LD_CONFIG, "Managed proxy could not pick a "
@@ -299,7 +353,7 @@ handle_proxy_line(char *line, managed_proxy_t *mp)
goto err;
} else if (!strncmp(line, PROTO_NEG_SUCCESS,
strlen(PROTO_NEG_SUCCESS))) {
- if (mp->conf_state != PT_PROTO_INFANT)
+ if (mp->conf_state != PT_PROTO_LAUNCHED)
goto err;
if (parse_version(line,mp) < 0)
@@ -567,46 +621,104 @@ parse_cmethod_line(char *line, managed_proxy_t *mp)
return r;
}
-/** Prepares the <b>envp</b> of a pluggable transport managed proxy
- *
- * <b>method</b> is a line with transport methods to be launched.
- * If <b>is_server</b> is set, prepare a server proxy <b>envp</b>. */
+/** Return a string containing the address:port that <b>transport</b>
+ * should use. */
+static char *
+get_bindaddr_for_proxy(managed_proxy_t *mp)
+{
+ char *bindaddr = NULL;
+ smartlist_t *string_tmp = smartlist_create();
+
+ tor_assert(mp->is_server);
+
+ SMARTLIST_FOREACH_BEGIN(mp->transports_to_launch, char *, t) {
+ tor_asprintf(&bindaddr, "%s-%s", t, get_bindaddr_for_transport(t));
+ smartlist_add(string_tmp, bindaddr);
+ } SMARTLIST_FOREACH_END(t);
+
+ bindaddr = smartlist_join_strings(string_tmp, ",", 0, NULL);
+
+ SMARTLIST_FOREACH(string_tmp, char *, t, tor_free(t));
+ smartlist_free(string_tmp);
+
+ return bindaddr;
+}
+
+/** Prepare the <b>envp</b> of managed proxy <b>mp</b> */
static void
-set_environ(char ***envp, const char *method, int is_server)
+set_environ(char ***envp, managed_proxy_t *mp)
{
or_options_t *options = get_options();
char **tmp=NULL;
char *state_loc=NULL;
+ char *transports_to_launch=NULL;
+ char *bindaddr=NULL;
- int n_envs = is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
+ int n_envs = mp->is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
/* allocate enough space for our env. vars and a NULL pointer */
*envp = tor_malloc(sizeof(char*)*(n_envs+1));
tmp = *envp;
state_loc = get_datadir_fname("pt_state/");
+ transports_to_launch =
+ smartlist_join_strings(mp->transports_to_launch, ",", 0, NULL);
- /* these should all be customizable */
tor_asprintf(tmp++, "HOME=%s", getenv("HOME"));
tor_asprintf(tmp++, "PATH=%s", getenv("PATH"));
tor_asprintf(tmp++, "TOR_PT_STATE_LOCATION=%s", state_loc);
tor_asprintf(tmp++, "TOR_PT_MANAGED_TRANSPORT_VER=1"); /* temp */
- if (is_server) {
- /* ASN check for ORPort values, should we be here if it's 0? */
+ if (mp->is_server) {
+ bindaddr = get_bindaddr_for_proxy(mp);
+
tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort); /* temp */
- tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=%s",
- get_bindaddr_for_transport(method));
- tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", method);
+ tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=%s", bindaddr);
+ tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", transports_to_launch);
tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200"); /* temp*/
} else {
- tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", method);
+ tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", transports_to_launch);
}
*tmp = NULL;
tor_free(state_loc);
+ tor_free(transports_to_launch);
+ tor_free(bindaddr);
+}
+
+/** Register <b>transport</b> using proxy with <b>proxy_argv</b> to
+ * the managed proxy subsystem.
+ * If <b>is_server</b> is true, then the proxy is a server proxy. */
+void
+pt_kickstart_proxy(char *transport, char **proxy_argv, int is_server)
+{
+ managed_proxy_t *mp=NULL;
+
+ mp = get_managed_proxy_by_argv(proxy_argv);
+
+ if (!mp) { /* we haven't seen this proxy before */
+ /* create a managed proxy */
+ managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
+ mp->conf_state = PT_PROTO_INFANT;
+ mp->is_server = is_server;
+ mp->argv = proxy_argv;
+ mp->transports = smartlist_create();
+
+ mp->transports_to_launch = smartlist_create();
+ add_transport_to_proxy(transport, mp);
+
+ /* register the managed proxy */
+ if (!unconfigured_proxy_list)
+ unconfigured_proxy_list = smartlist_create();
+ smartlist_add(unconfigured_proxy_list, mp);
+
+ n_unconfigured_proxies++; /* ASN should we care about overflows here?
+ I say no. */
+ } else { /* known proxy. just add transport to its transport list */
+ add_transport_to_proxy(transport, mp);
+ free_execve_args(proxy_argv);
+ }
}
-/* ASN is this too ugly/stupid? */
/** Frees the array of pointers in <b>arg</b> used as arguments to
execve. */
static INLINE void
diff --git a/src/or/transports.h b/src/or/transports.h
index 8bd79fe..c75797d 100644
--- a/src/or/transports.h
+++ b/src/or/transports.h
@@ -11,13 +11,13 @@
#ifndef TOR_TRANSPORTS_H
#define TOR_TRANSPORTS_H
-int pt_managed_launch_proxy(const char *method,
- char **proxy_argv, int is_server);
+void pt_kickstart_proxy(char *method, char **proxy_argv,
+ int is_server);
-#define pt_managed_launch_client_proxy(m, pa) \
- pt_managed_launch_proxy(m, pa, 0)
-#define pt_managed_launch_server_proxy(m, pa) \
- pt_managed_launch_proxy(m, pa, 1)
+#define pt_kickstart_client_proxy(m, pa) \
+ pt_kickstart_proxy(m, pa, 0)
+#define pt_kickstart_server_proxy(m, pa) \
+ pt_kickstart_proxy(m, pa, 1)
void pt_configure_remaining_proxies(void);
@@ -29,6 +29,7 @@ void pt_free_all(void);
/** State of the managed proxy configuration protocol. */
enum pt_proto_state {
PT_PROTO_INFANT, /* was just born */
+ PT_PROTO_LAUNCHED, /* was just launched */
PT_PROTO_ACCEPTING_METHODS, /* accepting methods */
PT_PROTO_CONFIGURED, /* configured successfully */
PT_PROTO_COMPLETED, /* configure and registered its transports */
@@ -38,6 +39,7 @@ enum pt_proto_state {
/** Structure containing information of a managed proxy. */
typedef struct {
enum pt_proto_state conf_state; /* the current configuration state */
+ char **argv; /* the cli arguments of this proxy */
int conf_protocol; /* the configuration protocol version used */
int is_server; /* is it a server proxy? */
@@ -45,7 +47,8 @@ typedef struct {
FILE *stdout; /* a stream to its stdout
(closed in managed_proxy_destroy()) */
- smartlist_t *transports; /* list of transport_t this proxy spawns */
+ smartlist_t *transports_to_launch; /* transports to-be-launched by this proxy */
+ smartlist_t *transports; /* list of transport_t this proxy spawned */
} managed_proxy_t;
int parse_cmethod_line(char *line, managed_proxy_t *mp);
1
0
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
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
[tor/master] Add support for managed {Client, Server}TransportPlugin parsing.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit 73a1e98cb971f7d1105f2b6b2399ae2eeec36a96
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Wed Jul 13 18:58:11 2011 +0200
Add support for managed {Client,Server}TransportPlugin parsing.
---
src/or/config.c | 219 ++++++++++++++++++++++++++++++++++++++++++++++++-------
src/or/or.h | 3 +
2 files changed, 194 insertions(+), 28 deletions(-)
diff --git a/src/or/config.c b/src/or/config.c
index 0082ff9..111b28b 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -33,7 +33,9 @@
#include "rendservice.h"
#include "rephist.h"
#include "router.h"
+#include "util.h"
#include "routerlist.h"
+#include "pluggable_transports.h"
#ifdef MS_WINDOWS
#include <shlobj.h>
#endif
@@ -298,6 +300,7 @@ static config_var_t _option_vars[] = {
V(HTTPProxyAuthenticator, STRING, NULL),
V(HTTPSProxy, STRING, NULL),
V(HTTPSProxyAuthenticator, STRING, NULL),
+ VAR("ServerTransportPlugin", LINELIST, ServerTransportPlugin, NULL),
V(Socks4Proxy, STRING, NULL),
V(Socks5Proxy, STRING, NULL),
V(Socks5ProxyUsername, STRING, NULL),
@@ -572,6 +575,8 @@ static void config_register_addressmaps(or_options_t *options);
static int parse_bridge_line(const char *line, int validate_only);
static int parse_client_transport_line(const char *line, int validate_only);
+
+static int parse_server_transport_line(const char *line, int validate_only);
static int parse_dir_server_line(const char *line,
dirinfo_type_t required_type,
int validate_only);
@@ -1219,6 +1224,18 @@ options_act(or_options_t *old_options)
}
}
+ clear_transport_list();
+ 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) {
@@ -3686,14 +3703,19 @@ options_validate(or_options_t *old_options, or_options_t *options,
if (options->UseBridges && !options->TunnelDirConns)
REJECT("TunnelDirConns set to 0 only works with UseBridges set to 0");
+ for (cl = options->Bridges; cl; cl = cl->next) {
+ if (parse_bridge_line(cl->value, 1)<0)
+ REJECT("Bridge line did not parse. See logs for details.");
+ }
+
for (cl = options->ClientTransportPlugin; cl; cl = cl->next) {
if (parse_client_transport_line(cl->value, 1)<0)
REJECT("Transport line did not parse. See logs for details.");
}
- for (cl = options->Bridges; cl; cl = cl->next) {
- if (parse_bridge_line(cl->value, 1)<0)
- REJECT("Bridge line did not parse. See logs for details.");
+ for (cl = options->ServerTransportPlugin; cl; cl = cl->next) {
+ if (parse_server_transport_line(cl->value, 1)<0)
+ REJECT("Server transport line did not parse. See logs for details.");
}
if (options->ConstrainedSockets) {
@@ -4652,28 +4674,35 @@ parse_bridge_line(const char *line, int validate_only)
SMARTLIST_FOREACH(items, char*, s, tor_free(s));
smartlist_free(items);
tor_free(addrport);
- tor_free(fingerprint);
tor_free(transport_name);
+ tor_free(fingerprint);
return r;
}
/** Read the contents of a ClientTransportPlugin line from
* <b>line</b>. Return 0 if the line is well-formed, and -1 if it
- * isn't. If <b>validate_only</b> is 0, and the line is well-formed,
- * then add the transport described in the line to our internal
- * transport list.
-*/
+ * isn't.
+ *
+ * If <b>validate_only</b> is 0, and the line is well-formed:
+ * - If it's an external proxy line, add the transport described in the line to
+ * our internal transport list.
+ * - If it's a managed proxy line, launch the managed proxy. */
static int
parse_client_transport_line(const char *line, int validate_only)
{
smartlist_t *items = NULL;
int r;
- char *socks_ver_str=NULL;
+ char *field2=NULL;
+
char *name=NULL;
char *addrport=NULL;
- int socks_ver;
tor_addr_t addr;
uint16_t port = 0;
+ int socks_ver=PROXY_NONE;
+
+ /* managed proxy options */
+ int is_managed=0;
+ char **proxy_argv=NULL;
items = smartlist_create();
smartlist_split_string(items, line, NULL,
@@ -4685,39 +4714,171 @@ parse_client_transport_line(const char *line, int validate_only)
}
name = smartlist_get(items, 0);
+ smartlist_del_keeporder(items, 0);
- socks_ver_str = smartlist_get(items, 1);
+ /* field2 is either a SOCKS version or "exec" */
+ field2 = smartlist_get(items, 0);
+ smartlist_del_keeporder(items, 0);
- if (!strcmp(socks_ver_str,"socks4"))
+ if (!strcmp(field2,"socks4")) {
socks_ver = PROXY_SOCKS4;
- else if (!strcmp(socks_ver_str,"socks5"))
+ } else if (!strcmp(field2,"socks5")) {
socks_ver = PROXY_SOCKS5;
- else {
- log_warn(LD_CONFIG, "Strange ClientTransportPlugin proxy type '%s'.",
- socks_ver_str);
+ } else if (!strcmp(field2,"exec")) {
+ is_managed=1;
+ } else {
+ log_warn(LD_CONFIG, "Strange ClientTransportPlugin field '%s'.",
+ field2);
goto err;
}
- addrport = smartlist_get(items, 2);
+ if (!is_managed) {
+ addrport = smartlist_get(items, 0);
+ smartlist_del_keeporder(items, 0);
- if (tor_addr_port_parse(addrport, &addr, &port)<0) {
- log_warn(LD_CONFIG, "Error parsing transport "
- "address '%s'", addrport);
- goto err;
+ 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;
+ }
}
- if (!port) {
- log_warn(LD_CONFIG,
- "Transport address '%s' has no port.", addrport);
+ if (!validate_only) {
+ if (is_managed) { /* if it's managed, and we are planning on
+ launching the proxy, use the rest of the line
+ as the argv. */
+ char **tmp;
+ char *tmp_arg;
+ proxy_argv = tor_malloc_zero(sizeof(char*)*(smartlist_len(items)+1));
+ tmp = proxy_argv;
+ while (smartlist_len(items)) {
+ tmp_arg = smartlist_get(items, 0);
+ smartlist_del_keeporder(items, 0);
+ *tmp++ = tor_strdup(tmp_arg);
+ tor_free(tmp_arg);
+ }
+ *tmp = NULL; /*terminated with NUL pointer, just like execve() likes it*/
+
+ if (pt_managed_launch_client_proxy(name, proxy_argv) < 0) {
+ log_warn(LD_CONFIG, "Error while launching managed proxy at '%s'",
+ proxy_argv[0]);
+ goto err;
+ }
+ } else { /* external */
+ if (transport_add_from_config(&addr, port, name,
+ socks_ver) < 0) {
+ goto err;
+ }
+ log_debug(LD_DIR, "Transport %s found at %s:%d", 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);
+ tor_free(name);
+ tor_free(field2);
+ tor_free(addrport);
+ return r;
+}
+
+/** Read the contents of a ServerTransportPlugin line from
+ * <b>line</b>. Return 0 if the line is well-formed, and -1 if it
+ * isn't.
+ * If <b>validate_only</b> is 0, the line is well-formed, and it's a
+ * managed proxy line, launch the managed proxy. */
+static int
+parse_server_transport_line(const char *line, int validate_only)
+{
+ smartlist_t *items = NULL;
+ int r;
+ char *name=NULL;
+ char *field2=NULL;
+ char *addrport=NULL;
+ tor_addr_t addr;
+ uint16_t port = 0;
+
+ /* managed proxy options */
+ int is_managed=0;
+ char **proxy_argv=NULL;
+
+ 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, "Too few arguments on ServerTransportPlugin line.");
goto err;
}
- if (!validate_only) {
- log_debug(LD_DIR, "Transport %s found at %s:%d", name,
- fmt_addr(&addr), (int)port);
+ name = smartlist_get(items, 0);
+ smartlist_del_keeporder(items, 0);
+
+ /* field2 is either <addr:port> or "exec" */
+ field2 = smartlist_get(items, 0);
+ smartlist_del_keeporder(items, 0);
- if (transport_add_from_config(&addr, port, name, socks_ver) < 0)
+ if (!(strstr(field2, ".") || strstr(field2, ":"))) { /* managed proxy */
+ if (strcmp(field2, "exec")) {
+ log_warn(LD_CONFIG, "Unrecognizable field '%s' in "
+ "ServerTransportPlugin line", field2);
+ goto err;
+ }
+ is_managed=1;
+ }
+
+ if (!is_managed) {
+ addrport = field2;
+
+ 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;
+ }
+ }
+
+ if (!validate_only) {
+ if (is_managed) { /* if it's managed, and we are planning on
+ launching the proxy, use the rest of the line
+ as the argv. */
+ char **tmp;
+ char *tmp_arg;
+ proxy_argv = tor_malloc_zero(sizeof(char*)*(smartlist_len(items)+1));
+ tmp = proxy_argv;
+ while (smartlist_len(items)) {
+ tmp_arg = smartlist_get(items, 0);
+ smartlist_del_keeporder(items, 0);
+ *tmp++ = tor_strdup(tmp_arg);
+ tor_free(tmp_arg);
+ }
+ *tmp = NULL; /*terminated with NUL pointer, just like execve() likes it*/
+
+ if (pt_managed_launch_server_proxy(name, proxy_argv) < 0) {
+ log_warn(LD_CONFIG, "Error while launching managed proxy at '%s'",
+ proxy_argv[0]);
+ goto err;
+ }
+ } else {
+ log_warn(LD_DIR, "Transport %s at %s:%d", name,
+ fmt_addr(&addr), (int)port);
+ }
}
r = 0;
@@ -4729,6 +4890,8 @@ parse_client_transport_line(const char *line, int validate_only)
done:
SMARTLIST_FOREACH(items, char*, s, tor_free(s));
smartlist_free(items);
+ tor_free(name);
+ tor_free(field2);
return r;
}
diff --git a/src/or/or.h b/src/or/or.h
index d1817d4..8bcfc82 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -2679,6 +2679,9 @@ typedef struct {
config_line_t *ClientTransportPlugin; /**< List of client
transport plugins. */
+ config_line_t *ServerTransportPlugin; /**< List of client
+ transport plugins. */
+
int BridgeRelay; /**< Boolean: are we acting as a bridge relay? We make
* this explicit so we can change how we behave in the
* future. */
1
0
commit 810a7a5fa0973451881a874a08594937a8274429
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Wed Jul 13 18:59:52 2011 +0200
Make some utility functions.
* Create a function that will get input from a stream, so that we can
communicate with the managed proxy.
* Hackish change to tor_spawn_background() so that we can specify an
environ for our spawn.
---
src/common/util.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++--
src/common/util.h | 16 +++++++++++-
src/test/test_util.c | 3 +-
3 files changed, 73 insertions(+), 6 deletions(-)
diff --git a/src/common/util.c b/src/common/util.c
index a5a6ea3..5f4472b 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -2958,7 +2958,7 @@ format_helper_exit_status(unsigned char child_state, int saved_errno,
*/
int
tor_spawn_background(const char *const filename, int *stdout_read,
- int *stderr_read, const char **argv)
+ int *stderr_read, const char **argv, const char **envp)
{
#ifdef MS_WINDOWS
(void) filename; (void) stdout_read; (void) stderr_read; (void) argv;
@@ -3068,7 +3068,10 @@ tor_spawn_background(const char *const filename, int *stdout_read,
/* Call the requested program. We need the cast because
execvp doesn't define argv as const, even though it
does not modify the arguments */
- execvp(filename, (char *const *) argv);
+ if (envp)
+ execve(filename, (char *const *) argv, (char*const*)envp);
+ else
+ execvp(filename, (char *const *) argv);
/* If we got here, the exec or open(/dev/null) failed */
@@ -3128,6 +3131,57 @@ tor_spawn_background(const char *const filename, int *stdout_read,
#endif
}
+/** Reads from <b>stream</b> and stores input in <b>buf_out</b> making
+ * sure it's below <b>count</b> bytes.
+ * If the string has a trailing newline, we strip it off.
+ *
+ * This function is specifically created to handle input from managed
+ * proxies, according to the pluggable transports spec. Make sure it
+ * fits your needs before using it.
+ *
+ * Returns:
+ * ST_CLOSED: If the stream is closed.
+ * ST_EAGAIN: If there is nothing to read and we should check back later.
+ * ST_TERM: If something is wrong with the stream.
+ * ST_OKAY: If everything went okay and we got a string in <b>buf_out</b>. */
+enum stream_status
+get_string_from_pipe(FILE *stream, char *buf_out, size_t count)
+{
+ char *retval;
+ size_t len;
+
+ retval = fgets(buf_out, count, stream);
+
+ if (!retval) {
+ if (feof(stream)) {
+ /* Program has closed stream (probably it exited) */
+ /* TODO: check error */
+ return ST_CLOSED;
+ } else {
+ if (EAGAIN == errno) {
+ /* Nothing more to read, try again next time */
+ return ST_EAGAIN;
+ } else {
+ /* There was a problem, abandon this child process */
+ return ST_TERM;
+ }
+ }
+ } else {
+ len = strlen(buf_out);
+ tor_assert(len>0);
+
+ if (buf_out[len - 1] == '\n') {
+ /* Remove the trailing newline */
+ buf_out[len - 1] = '\0';
+ }
+
+ return ST_OKAY;
+ }
+
+ /* We should never get here */
+ return ST_TERM;
+}
+
/** Read from stream, and send lines to log at the specified log level.
* Returns 1 if stream is closed normally, -1 if there is a error reading, and
* 0 otherwise. Handles lines from tor-fw-helper and
@@ -3254,7 +3308,7 @@ tor_check_port_forwarding(const char *filename, int dir_port, int or_port,
/* Assume tor-fw-helper will succeed, start it later*/
time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_SUCCESS;
- child_pid = tor_spawn_background(filename, &fd_out, &fd_err, argv);
+ child_pid = tor_spawn_background(filename, &fd_out, &fd_err, argv, NULL);
if (child_pid < 0) {
log_warn(LD_GENERAL, "Failed to start port forwarding helper %s",
filename);
diff --git a/src/common/util.h b/src/common/util.h
index 2974ab7..1b81fa3 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -279,6 +279,16 @@ char *rate_limit_log(ratelim_t *lim, time_t now);
ssize_t write_all(tor_socket_t fd, const char *buf, size_t count,int isSocket);
ssize_t read_all(tor_socket_t fd, char *buf, size_t count, int isSocket);
+/** Status of an I/O stream. */
+enum stream_status {
+ ST_OKAY,
+ ST_EAGAIN,
+ ST_TERM,
+ ST_CLOSED
+};
+
+enum stream_status get_string_from_pipe(FILE *stream, char *buf, size_t count);
+
/** Return values from file_status(); see that function's documentation
* for details. */
typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR } file_status_t;
@@ -340,14 +350,16 @@ void write_pidfile(char *filename);
void tor_check_port_forwarding(const char *filename,
int dir_port, int or_port, time_t now);
+int tor_spawn_background(const char *const filename, int *stdout_read,
+ int *stderr_read, const char **argv,
+ const char **envp);
+
#ifdef MS_WINDOWS
HANDLE load_windows_system_library(const TCHAR *library_name);
#endif
#ifdef UTIL_PRIVATE
/* Prototypes for private functions only used by util.c (and unit tests) */
-int tor_spawn_background(const char *const filename, int *stdout_read,
- int *stderr_read, const char **argv);
void format_helper_exit_status(unsigned char child_state,
int saved_errno, char *hex_errno);
diff --git a/src/test/test_util.c b/src/test/test_util.c
index c4769e6..c778faa 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -1389,7 +1389,8 @@ run_util_spawn_background(const char *argv[], const char *expected_out,
char stdout_buf[100], stderr_buf[100];
/* Start the program */
- retval = tor_spawn_background(argv[0], &stdout_pipe, &stderr_pipe, argv);
+ retval = tor_spawn_background(argv[0], &stdout_pipe, &stderr_pipe,
+ argv, NULL);
tt_int_op(retval, >, 0);
tt_int_op(stdout_pipe, >, 0);
tt_int_op(stderr_pipe, >, 0);
1
0
[tor/master] Create the pluggable_transports.[ch] source files responsible for talking the 180 talk.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit 9ba2d0e439e53f8307c808fe26e37b53892a604c
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Wed Jul 13 19:00:28 2011 +0200
Create the pluggable_transports.[ch] source files responsible for talking the 180 talk.
---
src/or/Makefile.am | 2 +
src/or/pluggable_transports.c | 613 +++++++++++++++++++++++++++++++++++++++++
src/or/pluggable_transports.h | 59 ++++
3 files changed, 674 insertions(+), 0 deletions(-)
diff --git a/src/or/Makefile.am b/src/or/Makefile.am
index a12d56b..4dc5ee5 100644
--- a/src/or/Makefile.am
+++ b/src/or/Makefile.am
@@ -39,6 +39,7 @@ libtor_a_SOURCES = \
networkstatus.c \
nodelist.c \
onion.c \
+ pluggable_transports.c \
policies.c \
reasons.c \
relay.c \
@@ -104,6 +105,7 @@ noinst_HEADERS = \
ntmain.h \
onion.h \
or.h \
+ pluggable_transports.h \
policies.h \
reasons.h \
relay.h \
diff --git a/src/or/pluggable_transports.c b/src/or/pluggable_transports.c
new file mode 100644
index 0000000..5448c40
--- /dev/null
+++ b/src/or/pluggable_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=%d", options->ORPort);
+ 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=4200");
+ } 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
new file mode 100644
index 0000000..80d5429
--- /dev/null
+++ b/src/or/pluggable_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
commit 5492de76dde34cb56c5658b6311772281c08c200
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Wed Jul 13 19:06:07 2011 +0200
Put some last missing pieces together.
* Add some utility transport functions in circuitbuild.[ch] so that we
can use them from pt.c.
* Make the accounting system consider traffic coming from proxies.
* Make sure that we only fetch bridge descriptors when all the
transports are configured.
---
src/or/circuitbuild.c | 65 +++++++++++++++++++++++++++++++++++-------------
src/or/circuitbuild.h | 5 ++++
src/or/connection.c | 24 ++++++++++++++++--
src/or/connection.h | 1 +
src/or/main.c | 11 +++++++-
5 files changed, 83 insertions(+), 23 deletions(-)
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index aa0e996..f9b5c38 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -26,6 +26,7 @@
#include "nodelist.h"
#include "onion.h"
#include "policies.h"
+#include "pluggable_transports.h"
#include "relay.h"
#include "rephist.h"
#include "router.h"
@@ -124,7 +125,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 transport_free(transport_t *transport);
static void bridge_free(bridge_info_t *bridge);
/**
@@ -4591,7 +4591,7 @@ clear_transport_list(void)
}
/** Free the pluggable transport struct <b>transport</b>. */
-static void
+void
transport_free(transport_t *transport)
{
if (!transport)
@@ -4619,29 +4619,35 @@ transport_get_by_name(const char *name)
return NULL;
}
-/** Remember a new pluggable transport proxy at <b>addr</b>:<b>port</b>.
- * <b>name</b> is set to the name of the protocol this proxy uses.
- * <b>socks_ver</b> is set to the SOCKS version of the proxy.
- *
- * Returns 0 on success, -1 on fail. */
-int
-transport_add_from_config(const tor_addr_t *addr, uint16_t port,
- const char *name, int socks_ver)
+/** Returns a transport_t struct for a transport proxy supporting the
+ protocol <b>name</b> listening at <b>addr</b>:<b>port</b> using
+ SOCKS version <b>socks_ver</b>. */
+transport_t *
+transport_create(const tor_addr_t *addr, uint16_t port,
+ const char *name, int socks_ver)
{
- transport_t *t;
+ transport_t *t = tor_malloc_zero(sizeof(transport_t));
- if (transport_get_by_name(name)) { /* check for duplicate names */
- log_warn(LD_CONFIG, "More than one transport has '%s' as "
- "its name.", name);
- return -1;
- }
-
- t = tor_malloc_zero(sizeof(transport_t));
tor_addr_copy(&t->addr, addr);
t->port = port;
t->name = tor_strdup(name);
t->socks_version = socks_ver;
+ return t;
+}
+
+/** Adds transport <b>t</b> to the internal list of pluggable transports. */
+int
+transport_add(transport_t *t)
+{
+ 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;
+ }
+
if (!transport_list)
transport_list = smartlist_create();
@@ -4649,6 +4655,23 @@ transport_add_from_config(const tor_addr_t *addr, uint16_t port,
return 0;
}
+/** Remember a new pluggable transport proxy at <b>addr</b>:<b>port</b>.
+ * <b>name</b> is set to the name of the protocol this proxy uses.
+ * <b>socks_ver</b> is set to the SOCKS version of the proxy. */
+int
+transport_add_from_config(const tor_addr_t *addr, uint16_t port,
+ const char *name, int socks_ver)
+{
+ transport_t *t = transport_create(addr, port, name, socks_ver);
+
+ if (transport_add(t) < 0) {
+ transport_free(t);
+ return -1;
+ } else {
+ return 0;
+ }
+}
+
/** Warns the user of possible pluggable transport misconfiguration. */
void
validate_pluggable_transports_config(void)
@@ -4897,6 +4920,12 @@ 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())
+ return;
+
SMARTLIST_FOREACH_BEGIN(bridge_list, bridge_info_t *, bridge)
{
if (!download_status_is_ready(&bridge->fetch_status, now,
diff --git a/src/or/circuitbuild.h b/src/or/circuitbuild.h
index 74715b7..92449b4 100644
--- a/src/or/circuitbuild.h
+++ b/src/or/circuitbuild.h
@@ -142,6 +142,11 @@ int circuit_build_times_get_bw_scale(networkstatus_t *ns);
void clear_transport_list(void);
int transport_add_from_config(const tor_addr_t *addr, uint16_t port,
const char *name, int socks_ver);
+int transport_add(transport_t *t);
+void transport_free(transport_t *transport);
+transport_t *transport_create(const tor_addr_t *addr, uint16_t port,
+ const char *name, int socks_ver);
+
int find_transport_by_bridge_addrport(const tor_addr_t *addr, uint16_t port,
const transport_t **transport);
void validate_pluggable_transports_config(void);
diff --git a/src/or/connection.c b/src/or/connection.c
index 00f25e6..5e8f95f 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -2068,15 +2068,20 @@ retry_all_listeners(smartlist_t *replaced_conns,
return retval;
}
-/** Return 1 if we should apply rate limiting to <b>conn</b>,
- * and 0 otherwise. Right now this just checks if it's an internal
- * IP address or an internal connection. */
+/** Return 1 if we should apply rate limiting to <b>conn</b>, and 0
+ * otherwise.
+ * Right now this just checks if it's an internal IP address or an
+ * internal connection. We also check if the connection uses pluggable
+ * transports, since we should then limit it even if it comes from an
+ * internal IP address. */
static int
connection_is_rate_limited(connection_t *conn)
{
or_options_t *options = get_options();
if (conn->linked)
return 0; /* Internal connection */
+ else if (connection_uses_transport(conn)) /* pluggable transport proxy */
+ return 1;
else if (! options->CountPrivateBandwidth &&
(tor_addr_family(&conn->addr) == AF_UNSPEC || /* no address */
tor_addr_is_internal(&conn->addr, 0)))
@@ -4147,6 +4152,19 @@ get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
return 0;
}
+/** Returns true if connection <b>conn</b> is using a pluggable
+ * transports proxy server. */
+int
+connection_uses_transport(connection_t *conn)
+{
+ const transport_t *transport=NULL;
+ if (find_transport_by_bridge_addrport(&conn->addr,
+ conn->port,&transport) == 0)
+ return 1;
+ else
+ return 0;
+}
+
/** Returns the global proxy type used by tor. */
static int
get_proxy_type(void)
diff --git a/src/or/connection.h b/src/or/connection.h
index be3de88..51cf2a8 100644
--- a/src/or/connection.h
+++ b/src/or/connection.h
@@ -60,6 +60,7 @@ int connection_read_proxy_handshake(connection_t *conn);
void log_failed_proxy_connection(connection_t *conn);
int get_proxy_addrport(tor_addr_t *addr, uint16_t *port, int *proxy_type,
const connection_t *conn);
+int connection_uses_transport(connection_t *conn);
int retry_all_listeners(smartlist_t *replaced_conns,
smartlist_t *new_conns);
diff --git a/src/or/main.c b/src/or/main.c
index 4d43267..d79d9eb 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -37,6 +37,7 @@
#include "ntmain.h"
#include "onion.h"
#include "policies.h"
+#include "pluggable_transports.h"
#include "relay.h"
#include "rendclient.h"
#include "rendcommon.h"
@@ -1447,7 +1448,7 @@ run_scheduled_events(time_t now)
}
}
- /** 10b. write bridge networkstatus file to disk */
+ /** 10. write bridge networkstatus file to disk */
if (options->BridgeAuthoritativeDir &&
time_to_write_bridge_status_file < now) {
networkstatus_dump_bridge_status_to_file(now);
@@ -1455,6 +1456,7 @@ run_scheduled_events(time_t now)
time_to_write_bridge_status_file = now+BRIDGE_STATUSFILE_INTERVAL;
}
+ /** 11. check the port forwarding app */
if (time_to_check_port_forwarding < now &&
options->PortForwarding &&
is_server) {
@@ -1466,7 +1468,11 @@ run_scheduled_events(time_t now)
time_to_check_port_forwarding = now+PORT_FORWARDING_CHECK_INTERVAL;
}
- /** 11. write the heartbeat message */
+ /** 11b. check pending unconfigured managed proxies */
+ if (pt_proxies_configuration_pending())
+ pt_configure_remaining_proxies();
+
+ /** 12. write the heartbeat message */
if (options->HeartbeatPeriod &&
time_to_next_heartbeat < now) {
log_heartbeat(now);
@@ -2258,6 +2264,7 @@ tor_free_all(int postfork)
clear_pending_onions();
circuit_free_all();
entry_guards_free_all();
+ pt_free_all();
connection_free_all();
buf_shrink_freelists(1);
memarea_clear_freelist();
1
0
commit ce419a78c56e38fedcb3bce66fc08283648aed9f
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Wed Jul 13 19:06:14 2011 +0200
Add some unit tests.
---
src/test/Makefile.am | 1 +
src/test/test.c | 2 +
src/test/test_pt.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 152 insertions(+), 0 deletions(-)
diff --git a/src/test/Makefile.am b/src/test/Makefile.am
index a4f93d1..0597b65 100644
--- a/src/test/Makefile.am
+++ b/src/test/Makefile.am
@@ -19,6 +19,7 @@ test_SOURCES = \
test_data.c \
test_dir.c \
test_microdesc.c \
+ test_pt.c \
test_util.c \
tinytest.c
diff --git a/src/test/test.c b/src/test/test.c
index e2f8b11..6aba200 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1261,6 +1261,7 @@ extern struct testcase_t container_tests[];
extern struct testcase_t util_tests[];
extern struct testcase_t dir_tests[];
extern struct testcase_t microdesc_tests[];
+extern struct testcase_t pt_tests[];
static struct testgroup_t testgroups[] = {
{ "", test_array },
@@ -1270,6 +1271,7 @@ static struct testgroup_t testgroups[] = {
{ "util/", util_tests },
{ "dir/", dir_tests },
{ "dir/md/", microdesc_tests },
+ { "pt/", pt_tests },
END_OF_GROUPS
};
diff --git a/src/test/test_pt.c b/src/test/test_pt.c
new file mode 100644
index 0000000..02950b8
--- /dev/null
+++ b/src/test/test_pt.c
@@ -0,0 +1,149 @@
+/* Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2011, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#define PT_PRIVATE
+#include "or.h"
+#include "pluggable_transports.h"
+#include "circuitbuild.h"
+#include "test.h"
+
+static void
+reset_mp(managed_proxy_t *mp)
+{
+ mp->conf_state = PT_PROTO_INFANT;
+ SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
+ smartlist_clear(mp->transports);
+ smartlist_free(mp->transports);
+ mp->transports = smartlist_create();
+}
+
+static void
+test_pt_parsing(void)
+{
+ char line[200];
+
+ managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
+ mp->conf_state = PT_PROTO_INFANT;
+ mp->transports = smartlist_create();
+
+ /* incomplete cmethod */
+ strcpy(line,"CMETHOD trebuchet");
+ test_assert(parse_cmethod_line(line, mp) < 0);
+
+ reset_mp(mp);
+
+ /* wrong proxy type */
+ strcpy(line,"CMETHOD trebuchet dog 127.0.0.1:1999");
+ test_assert(parse_cmethod_line(line, mp) < 0);
+
+ reset_mp(mp);
+
+ /* wrong addrport */
+ strcpy(line,"CMETHOD trebuchet socks4 abcd");
+ test_assert(parse_cmethod_line(line, mp) < 0);
+
+ reset_mp(mp);
+
+ /* correct line */
+ strcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999");
+ test_assert(parse_cmethod_line(line, mp) == 0);
+ test_assert(smartlist_len(mp->transports));
+
+ reset_mp(mp);
+
+ /* incomplete smethod */
+ strcpy(line,"SMETHOD trebuchet");
+ test_assert(parse_smethod_line(line, mp) < 0);
+
+ reset_mp(mp);
+
+ /* wrong addr type */
+ strcpy(line,"SMETHOD trebuchet abcd");
+ test_assert(parse_smethod_line(line, mp) < 0);
+
+ reset_mp(mp);
+
+ /* cowwect */
+ strcpy(line,"SMETHOD trebuchy 127.0.0.1:1999");
+ test_assert(parse_smethod_line(line, mp) == 0);
+
+ reset_mp(mp);
+
+ /* unsupported version */
+ strcpy(line,"VERSION 666");
+ test_assert(parse_version(line, mp) < 0);
+
+ /* incomplete VERSION */
+ strcpy(line,"VERSION ");
+ test_assert(parse_version(line, mp) < 0);
+
+ /* correct VERSION */
+ strcpy(line,"VERSION 1");
+ test_assert(parse_version(line, mp) == 0);
+
+ done:
+ tor_free(mp);
+}
+
+static void
+test_pt_protocol(void)
+{
+ char line[200];
+
+ managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
+ mp->conf_state = PT_PROTO_INFANT;
+ mp->transports = smartlist_create();
+
+ /* various wrong protocol runs: */
+
+ strcpy(line, "TEST TEST");
+ handle_proxy_line(line, mp);
+ test_assert(mp->conf_state == PT_PROTO_BROKEN);
+
+ reset_mp(mp);
+
+ strcpy(line,"VERSION 1");
+ handle_proxy_line(line, mp);
+ test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
+
+ strcpy(line,"VERSION 1");
+ handle_proxy_line(line, mp);
+ test_assert(mp->conf_state == PT_PROTO_BROKEN);
+
+ reset_mp(mp);
+
+ strcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999");
+ handle_proxy_line(line, mp);
+ test_assert(mp->conf_state == PT_PROTO_BROKEN);
+
+ reset_mp(mp);
+
+ /* correct protocol run: */
+ strcpy(line,"VERSION 1");
+ handle_proxy_line(line, mp);
+ test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
+
+ strcpy(line,"CMETHOD trebuchet socks5 127.0.0.1:1999");
+ handle_proxy_line(line, mp);
+ test_assert(mp->conf_state == PT_PROTO_ACCEPTING_METHODS);
+
+ strcpy(line,"CMETHODS DONE");
+ handle_proxy_line(line, mp);
+ test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
+
+ done:
+ tor_free(mp);
+}
+
+#define PT_LEGACY(name) \
+ { #name, legacy_test_helper, 0, &legacy_setup, test_pt_ ## name }
+
+struct testcase_t pt_tests[] = {
+ PT_LEGACY(parsing),
+ PT_LEGACY(protocol),
+ END_OF_TESTCASES
+};
+
1
0
[tor/master] Changed a couple of 180 spec stuff according to #3578.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit 684aca7faffea93a8e3dcb2a582aa10797f71e7d
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Thu Jul 14 01:03:35 2011 +0200
Changed a couple of 180 spec stuff according to #3578.
* Restored "proxy" in external ServerTransportPlugin lines.
* Changed the extended OR port and ORPort env. vars to addr:port.
---
src/or/circuitbuild.c | 2 +-
src/or/config.c | 108 ++++++++++++++++++++---------------------
src/or/pluggable_transports.c | 4 +-
3 files changed, 56 insertions(+), 58 deletions(-)
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index f9b5c38..003f579 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 111b28b..5f2f11d 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1224,7 +1224,6 @@ options_act(or_options_t *old_options)
}
}
- clear_transport_list();
if (options->ServerTransportPlugin) {
for (cl = options->ServerTransportPlugin; cl; cl = cl->next) {
if (parse_server_transport_line(cl->value, 0)<0) {
@@ -4732,26 +4731,10 @@ parse_client_transport_line(const char *line, int validate_only)
goto err;
}
- if (!is_managed) {
- addrport = smartlist_get(items, 0);
- smartlist_del_keeporder(items, 0);
-
- 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;
- }
- }
-
- if (!validate_only) {
- if (is_managed) { /* if it's managed, and we are planning on
- launching the proxy, use the rest of the line
- as the argv. */
+ if (is_managed) { /* managed */
+ if (!validate_only) { /* if we are not just validating, use the
+ rest of the line as the argv of the proxy
+ to be launched */
char **tmp;
char *tmp_arg;
proxy_argv = tor_malloc_zero(sizeof(char*)*(smartlist_len(items)+1));
@@ -4769,12 +4752,29 @@ parse_client_transport_line(const char *line, int validate_only)
proxy_argv[0]);
goto err;
}
- } else { /* external */
+ }
+ } else { /* external */
+ addrport = smartlist_get(items, 0);
+ smartlist_del_keeporder(items, 0);
+
+ 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;
+ }
+
+ if (!validate_only) {
if (transport_add_from_config(&addr, port, name,
socks_ver) < 0) {
goto err;
}
- log_debug(LD_DIR, "Transport %s found at %s:%d", name,
+
+ log_debug(LD_DIR, "Transport '%s' found at %s:%d", name,
fmt_addr(&addr), (int)port);
}
}
@@ -4805,7 +4805,7 @@ parse_server_transport_line(const char *line, int validate_only)
smartlist_t *items = NULL;
int r;
char *name=NULL;
- char *field2=NULL;
+ char *type=NULL;
char *addrport=NULL;
tor_addr_t addr;
uint16_t port = 0;
@@ -4826,38 +4826,20 @@ parse_server_transport_line(const char *line, int validate_only)
name = smartlist_get(items, 0);
smartlist_del_keeporder(items, 0);
- /* field2 is either <addr:port> or "exec" */
- field2 = smartlist_get(items, 0);
+ type = smartlist_get(items, 0);
smartlist_del_keeporder(items, 0);
- if (!(strstr(field2, ".") || strstr(field2, ":"))) { /* managed proxy */
- if (strcmp(field2, "exec")) {
- log_warn(LD_CONFIG, "Unrecognizable field '%s' in "
- "ServerTransportPlugin line", field2);
- goto err;
- }
+ if (!strcmp(type, "exec")) {
is_managed=1;
+ } else if (!strcmp(type, "proxy")) {
+ is_managed=0;
+ } else {
+ log_warn(LD_CONFIG, "Strange ServerTransportPlugin type '%s'", type);
+ goto err;
}
- if (!is_managed) {
- addrport = field2;
-
- 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;
- }
- }
-
- if (!validate_only) {
- if (is_managed) { /* if it's managed, and we are planning on
- launching the proxy, use the rest of the line
- as the argv. */
+ if (is_managed) { /* managed */
+ if (!validate_only) {
char **tmp;
char *tmp_arg;
proxy_argv = tor_malloc_zero(sizeof(char*)*(smartlist_len(items)+1));
@@ -4870,13 +4852,29 @@ parse_server_transport_line(const char *line, int validate_only)
}
*tmp = NULL; /*terminated with NUL pointer, just like execve() likes it*/
- if (pt_managed_launch_server_proxy(name, proxy_argv) < 0) {
+ if (pt_managed_launch_server_proxy(name, proxy_argv) < 0) { /* launch it! */
log_warn(LD_CONFIG, "Error while launching managed proxy at '%s'",
proxy_argv[0]);
goto err;
}
- } else {
- log_warn(LD_DIR, "Transport %s at %s:%d", name,
+ }
+ } else { /* external */
+ addrport = smartlist_get(items, 0);
+ smartlist_del_keeporder(items, 0);
+
+ 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;
+ }
+
+ if (!validate_only) {
+ log_warn(LD_DIR, "Transport '%s' at %s:%d.", name,
fmt_addr(&addr), (int)port);
}
}
@@ -4891,7 +4889,7 @@ parse_server_transport_line(const char *line, int validate_only)
SMARTLIST_FOREACH(items, char*, s, tor_free(s));
smartlist_free(items);
tor_free(name);
- tor_free(field2);
+ tor_free(type);
return r;
}
diff --git a/src/or/pluggable_transports.c b/src/or/pluggable_transports.c
index 5448c40..49b0e13 100644
--- a/src/or/pluggable_transports.c
+++ b/src/or/pluggable_transports.c
@@ -565,10 +565,10 @@ set_environ(char ***envp, const char *method, int is_server)
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=%d", options->ORPort);
+ 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=4200");
+ tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200"); /* temp*/
} else {
tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", method);
}
1
0