tor-commits
Threads by month
- ----- 2025 -----
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
October 2011
- 18 participants
- 1256 discussions

07 Oct '11
commit 69271b2a383f6fe53ed7d5ca21cc4bd89c25de17
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Mon Jul 18 17:06:16 2011 +0200
Reuse get_string_from_pipe() in log_from_pipe().
---
src/common/util.c | 91 +++++++++++++++++++++-----------------------------
src/or/transports.c | 2 -
2 files changed, 38 insertions(+), 55 deletions(-)
diff --git a/src/common/util.c b/src/common/util.c
index c2db542..7a7ee19 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -3173,6 +3173,12 @@ get_string_from_pipe(FILE *stream, char *buf_out, size_t count)
if (buf_out[len - 1] == '\n') {
/* Remove the trailing newline */
buf_out[len - 1] = '\0';
+ } else {
+ /* No newline; check whether we overflowed the buffer */
+ if (!feof(stream))
+ log_warn(LD_GENERAL,
+ "Line from stream was truncated: %s", buf_out);
+ /* TODO: What to do with this error? */
}
return IO_STREAM_OKAY;
@@ -3192,65 +3198,44 @@ log_from_pipe(FILE *stream, int severity, const char *executable,
int *child_status)
{
char buf[256];
+ enum stream_status r;
for (;;) {
- char *retval;
- retval = fgets(buf, sizeof(buf), stream);
-
- if (NULL == retval) {
- if (feof(stream)) {
- /* Program has closed stream (probably it exited) */
- /* TODO: check error */
- fclose(stream);
- return 1;
- } else {
- if (EAGAIN == errno) {
- /* Nothing more to read, try again next time */
- return 0;
- } else {
- /* There was a problem, abandon this child process */
- fclose(stream);
- return -1;
- }
- }
- } else {
- /* We have some data, log it and keep asking for more */
- size_t len;
+ r = get_string_from_pipe(stream, buf, sizeof(buf) - 1);
- len = strlen(buf);
- if (buf[len - 1] == '\n') {
- /* Remove the trailing newline */
- buf[len - 1] = '\0';
- } else {
- /* No newline; check whether we overflowed the buffer */
- if (!feof(stream))
- log_warn(LD_GENERAL,
- "Line from port forwarding helper was truncated: %s", buf);
- /* TODO: What to do with this error? */
- }
+ if (r == IO_STREAM_CLOSED) {
+ fclose(stream);
+ return 1;
+ } else if (r == IO_STREAM_EAGAIN) {
+ return 0;
+ } else if (r == IO_STREAM_TERM) {
+ fclose(stream);
+ return -1;
+ }
- /* Check if buf starts with SPAWN_ERROR_MESSAGE */
- if (strcmpstart(buf, SPAWN_ERROR_MESSAGE) == 0) {
- /* Parse error message */
- int retval, child_state, saved_errno;
- retval = tor_sscanf(buf, SPAWN_ERROR_MESSAGE "%x/%x",
- &child_state, &saved_errno);
- if (retval == 2) {
- log_warn(LD_GENERAL,
- "Failed to start child process \"%s\" in state %d: %s",
- executable, child_state, strerror(saved_errno));
- if (child_status)
- *child_status = 1;
- } else {
- /* Failed to parse message from child process, log it as a
- warning */
- log_warn(LD_GENERAL,
- "Unexpected message from port forwarding helper \"%s\": %s",
- executable, buf);
- }
+ tor_assert(r == IO_STREAM_OKAY);
+
+ /* Check if buf starts with SPAWN_ERROR_MESSAGE */
+ if (strcmpstart(buf, SPAWN_ERROR_MESSAGE) == 0) {
+ /* Parse error message */
+ int retval, child_state, saved_errno;
+ retval = tor_sscanf(buf, SPAWN_ERROR_MESSAGE "%x/%x",
+ &child_state, &saved_errno);
+ if (retval == 2) {
+ log_warn(LD_GENERAL,
+ "Failed to start child process \"%s\" in state %d: %s",
+ executable, child_state, strerror(saved_errno));
+ if (child_status)
+ *child_status = 1;
} else {
- log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", buf);
+ /* Failed to parse message from child process, log it as a
+ warning */
+ log_warn(LD_GENERAL,
+ "Unexpected message from port forwarding helper \"%s\": %s",
+ executable, buf);
}
+ } else {
+ log_fn(severity, LD_GENERAL, "Port forwarding helper says: %s", buf);
}
}
diff --git a/src/or/transports.c b/src/or/transports.c
index 930cb8c..392c43d 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -157,8 +157,6 @@ configure_proxy(managed_proxy_t *mp)
char stdout_buf[200];
while (1) {
- memset(stdout_buf, 0, sizeof(stdout_buf));
-
r = get_string_from_pipe(mp->stdout, stdout_buf,
sizeof(stdout_buf) - 1);
1
0

[tor/master] Let's be smarter while parsing {Client, Server}TransportPlugin lines.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit 51cdd30c01c47e3522bd49a23a83a566cf4de5a7
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Mon Jul 18 16:42:31 2011 +0200
Let's be smarter while parsing {Client,Server}TransportPlugin lines.
---
src/or/config.c | 64 +++++++++++++++++++++++---------------------------
src/or/transports.c | 1 -
2 files changed, 29 insertions(+), 36 deletions(-)
diff --git a/src/or/config.c b/src/or/config.c
index da35270..02925f3 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -4702,22 +4702,25 @@ parse_client_transport_line(const char *line, int validate_only)
/* managed proxy options */
int is_managed=0;
char **proxy_argv=NULL;
+ char **tmp=NULL;
+ int proxy_argc,i;
+
+ int line_length;
items = smartlist_create();
smartlist_split_string(items, line, NULL,
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
- if (smartlist_len(items) < 3) {
+ line_length = smartlist_len(items);
+ if (line_length < 3) {
log_warn(LD_CONFIG, "Too few arguments on ClientTransportPlugin line.");
goto err;
}
name = smartlist_get(items, 0);
- smartlist_del_keeporder(items, 0);
/* field2 is either a SOCKS version or "exec" */
- field2 = smartlist_get(items, 0);
- smartlist_del_keeporder(items, 0);
+ field2 = smartlist_get(items, 1);
if (!strcmp(field2,"socks4")) {
socks_ver = PROXY_SOCKS4;
@@ -4735,16 +4738,12 @@ parse_client_transport_line(const char *line, int validate_only)
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));
+ proxy_argc = line_length-2;
+ tor_assert(proxy_argc > 0);
+ proxy_argv = tor_malloc_zero(sizeof(char*)*(proxy_argc+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);
- }
+ for (i=0;i<proxy_argc;i++) /* store arguments */
+ *tmp++ = smartlist_get(items, 2+i);
*tmp = NULL; /*terminated with NUL pointer, just like execve() likes it*/
if (pt_managed_launch_client_proxy(name, proxy_argv) < 0) {
@@ -4754,8 +4753,7 @@ parse_client_transport_line(const char *line, int validate_only)
}
}
} else { /* external */
- addrport = smartlist_get(items, 0);
- smartlist_del_keeporder(items, 0);
+ addrport = smartlist_get(items, 2);
if (tor_addr_port_parse(addrport, &addr, &port)<0) {
log_warn(LD_CONFIG, "Error parsing transport "
@@ -4788,9 +4786,7 @@ 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);
- tor_free(addrport);
+ tor_free(proxy_argv);
return r;
}
@@ -4813,21 +4809,24 @@ parse_server_transport_line(const char *line, int validate_only)
/* managed proxy options */
int is_managed=0;
char **proxy_argv=NULL;
+ char **tmp=NULL;
+ int proxy_argc,i;
+
+ int line_length;
items = smartlist_create();
smartlist_split_string(items, line, NULL,
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
- if (smartlist_len(items) < 3) {
+ line_length = smartlist_len(items);
+ if (line_length < 3) {
log_warn(LD_CONFIG, "Too few arguments on ServerTransportPlugin line.");
goto err;
}
name = smartlist_get(items, 0);
- smartlist_del_keeporder(items, 0);
- type = smartlist_get(items, 0);
- smartlist_del_keeporder(items, 0);
+ type = smartlist_get(items, 1);
if (!strcmp(type, "exec")) {
is_managed=1;
@@ -4840,16 +4839,13 @@ parse_server_transport_line(const char *line, int validate_only)
if (is_managed) { /* managed */
if (!validate_only) {
- char **tmp;
- char *tmp_arg;
- proxy_argv = tor_malloc_zero(sizeof(char*)*(smartlist_len(items)+1));
+ proxy_argc = line_length-2;
+ tor_assert(proxy_argc > 0);
+ proxy_argv = tor_malloc_zero(sizeof(char*)*(proxy_argc+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);
- }
+
+ for (i=0;i<proxy_argc;i++) /* store arguments */
+ *tmp++ = smartlist_get(items, 2+i);
*tmp = NULL; /*terminated with NUL pointer, just like execve() likes it*/
if (pt_managed_launch_server_proxy(name, proxy_argv) < 0) { /* launch it! */
@@ -4859,8 +4855,7 @@ parse_server_transport_line(const char *line, int validate_only)
}
}
} else { /* external */
- addrport = smartlist_get(items, 0);
- smartlist_del_keeporder(items, 0);
+ addrport = smartlist_get(items, 2);
if (tor_addr_port_parse(addrport, &addr, &port)<0) {
log_warn(LD_CONFIG, "Error parsing transport "
@@ -4888,8 +4883,7 @@ parse_server_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(type);
+ tor_free(proxy_argv);
return r;
}
diff --git a/src/or/transports.c b/src/or/transports.c
index 6589a8c..930cb8c 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -106,7 +106,6 @@ pt_managed_launch_proxy(const char *method,
/* 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);
1
0

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

[tor/master] Replaced ST_* enum prefix for stream status with IO_STREAM_*.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit 14c5a24fe74f7ebaf94c69721025f142d42ef1e0
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Mon Jul 18 02:35:29 2011 +0200
Replaced ST_* enum prefix for stream status with IO_STREAM_*.
---
src/common/util.c | 18 +++++++++---------
src/common/util.h | 8 ++++----
src/or/transports.c | 6 +++---
3 files changed, 16 insertions(+), 16 deletions(-)
diff --git a/src/common/util.c b/src/common/util.c
index 5f4472b..c2db542 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -3140,10 +3140,10 @@ tor_spawn_background(const char *const filename, int *stdout_read,
* 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>. */
+ * IO_STREAM_CLOSED: If the stream is closed.
+ * IO_STREAM_EAGAIN: If there is nothing to read and we should check back later.
+ * IO_STREAM_TERM: If something is wrong with the stream.
+ * IO_STREAM_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)
{
@@ -3156,14 +3156,14 @@ get_string_from_pipe(FILE *stream, char *buf_out, size_t count)
if (feof(stream)) {
/* Program has closed stream (probably it exited) */
/* TODO: check error */
- return ST_CLOSED;
+ return IO_STREAM_CLOSED;
} else {
if (EAGAIN == errno) {
/* Nothing more to read, try again next time */
- return ST_EAGAIN;
+ return IO_STREAM_EAGAIN;
} else {
/* There was a problem, abandon this child process */
- return ST_TERM;
+ return IO_STREAM_TERM;
}
}
} else {
@@ -3175,11 +3175,11 @@ get_string_from_pipe(FILE *stream, char *buf_out, size_t count)
buf_out[len - 1] = '\0';
}
- return ST_OKAY;
+ return IO_STREAM_OKAY;
}
/* We should never get here */
- return ST_TERM;
+ return IO_STREAM_TERM;
}
/** Read from stream, and send lines to log at the specified log level.
diff --git a/src/common/util.h b/src/common/util.h
index 1b81fa3..12dc106 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -281,10 +281,10 @@ 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
+ IO_STREAM_OKAY,
+ IO_STREAM_EAGAIN,
+ IO_STREAM_TERM,
+ IO_STREAM_CLOSED
};
enum stream_status get_string_from_pipe(FILE *stream, char *buf, size_t count);
diff --git a/src/or/transports.c b/src/or/transports.c
index aae39cd..6589a8c 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -163,14 +163,14 @@ configure_proxy(managed_proxy_t *mp)
r = get_string_from_pipe(mp->stdout, stdout_buf,
sizeof(stdout_buf) - 1);
- if (r == ST_CLOSED || r == ST_TERM) {
+ if (r == IO_STREAM_CLOSED || r == IO_STREAM_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) {
+ } else if (r == IO_STREAM_EAGAIN) {
return;
} else {
- tor_assert(r == ST_OKAY);
+ tor_assert(r == IO_STREAM_OKAY);
handle_proxy_line(stdout_buf, mp);
}
1
0

[tor/master] Renamed pluggable_transports.[ch] to transports.[ch].
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit d8c04c7ea52bc1ffbeec60d614970eccab3c9b4f
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Mon Jul 18 02:19:38 2011 +0200
Renamed pluggable_transports.[ch] to transports.[ch].
---
src/or/pluggable_transports.c | 613 -----------------------------------------
src/or/pluggable_transports.h | 59 ----
src/or/transports.c | 613 +++++++++++++++++++++++++++++++++++++++++
src/or/transports.h | 59 ++++
4 files changed, 672 insertions(+), 672 deletions(-)
diff --git a/src/or/pluggable_transports.c b/src/or/pluggable_transports.c
deleted file mode 100644
index 49b0e13..0000000
--- a/src/or/pluggable_transports.c
+++ /dev/null
@@ -1,613 +0,0 @@
-/* Copyright (c) 2011, The Tor Project, Inc. */
-/* See LICENSE for licensing information */
-
-/**
- * \file pluggable_transports.c
- * \brief Pluggable Transports related code.
- **/
-
-#define PT_PRIVATE
-#include "or.h"
-#include "config.h"
-#include "circuitbuild.h"
-#include "pluggable_transports.h"
-
-/* ASN TIDY THESE UP*/
-static void set_environ(char ***envp, const char *method,
- int is_server);
-static INLINE int proxy_configuration_finished(managed_proxy_t *mp);
-
-static void managed_proxy_destroy(managed_proxy_t *mp,
- int also_free_transports);
-static void register_proxy_transports(managed_proxy_t *mp);
-static void handle_finished_proxy(managed_proxy_t *mp);
-static void configure_proxy(managed_proxy_t *mp);
-
-static void parse_method_error(char *line, int is_server_method);
-#define parse_server_method_error(l) parse_method_error(l, 1)
-#define parse_client_method_error(l) parse_method_error(l, 0)
-
-static INLINE void free_execve_args(char **arg);
-
-/** Managed proxy protocol strings */
-#define PROTO_ENV_ERROR "ENV-ERROR"
-#define PROTO_NEG_SUCCESS "VERSION"
-#define PROTO_NEG_FAIL "VERSION-ERROR no-version"
-#define PROTO_CMETHOD "CMETHOD"
-#define PROTO_SMETHOD "SMETHOD"
-#define PROTO_CMETHOD_ERROR "CMETHOD-ERROR"
-#define PROTO_SMETHOD_ERROR "SMETHOD-ERROR"
-#define PROTO_CMETHODS_DONE "CMETHODS DONE"
-#define PROTO_SMETHODS_DONE "SMETHODS DONE"
-
-/* The smallest valid managed proxy protocol line that can
- appear. It's the size of "VERSION 1" */
-#define SMALLEST_MANAGED_LINE_SIZE 9
-
-/** Number of environment variables for managed proxy clients/servers. */
-#define ENVIRON_SIZE_CLIENT 5
-#define ENVIRON_SIZE_SERVER 8
-
-/** The first and only supported - at the moment - configuration
- protocol version. */
-#define PROTO_VERSION_ONE 1
-
-/** List of unconfigured managed proxies. */
-static smartlist_t *unconfigured_proxy_list = NULL;
-/** Number of unconfigured managed proxies. */
-static int n_unconfigured_proxies = 0;
-
-/* The main idea is:
-
- A managed proxy is represented by a managed_proxy_t struct and can
- spawn multiple transports.
-
- unconfigured_proxy_list is a list of all the unconfigured managed
- proxies; everytime we spawn a managed proxy we add it in that
- list.
- In every run_scheduled_event() tick, we attempt to configure each
- managed proxy further, using the configuration protocol defined in
- the 180_pluggable_transport.txt proposal.
-
- When a managed proxy is fully configured, we register all its
- transports to the circuitbuild.c subsystem - like we do with
- external proxies - and then free the managed proxy struct
- since it's no longer needed. */
-
-/** Return true if there are still unconfigured managed proxies. */
-int
-pt_proxies_configuration_pending(void)
-{
- return !!n_unconfigured_proxies;
-}
-
-/** Launch a proxy for <b>method</b> using <b>proxy_argv</b> as its
- * arguments. If <b>is_server</b>, launch a server proxy. */
-int
-pt_managed_launch_proxy(const char *method,
- char **proxy_argv, int is_server)
-{
- char **envp=NULL;
- int retval;
- FILE *stdout_read = NULL;
- int stdout_pipe=-1, stderr_pipe=-1;
-
- /* prepare the environment variables for the managed proxy */
- set_environ(&envp, method, is_server);
-
- /* ASN we should probably check if proxy_argv[0] is executable by our user */
- retval = tor_spawn_background(proxy_argv[0], &stdout_pipe,
- &stderr_pipe, (const char **)proxy_argv,
- (const char **)envp);
- if (retval < 0) {
- log_warn(LD_GENERAL, "Spawn failed");
- return -1;
- }
-
- /* free the memory allocated for the execve() */
- free_execve_args(envp);
- free_execve_args(proxy_argv);
-
- /* Set stdout/stderr pipes to be non-blocking */
- fcntl(stdout_pipe, F_SETFL, O_NONBLOCK);
- /* Open the buffered IO streams */
- stdout_read = fdopen(stdout_pipe, "r");
-
- log_warn(LD_CONFIG, "The spawn is alive (%d)!", retval);
-
- /* create a managed proxy */
- managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
- mp->conf_state = PT_PROTO_INFANT;
- mp->stdout = stdout_read;
- mp->transports = smartlist_create();
-
- /* register the managed proxy */
- if (!unconfigured_proxy_list)
- unconfigured_proxy_list = smartlist_create();
- smartlist_add(unconfigured_proxy_list, mp);
-
- n_unconfigured_proxies++; /* ASN should we care about overflows here?
- I say no. */
-
- return 0;
-}
-
-/** Check if any of the managed proxies we are currently trying to
- * configure have anything new to say. This is called from
- * run_scheduled_events(). */
-void
-pt_configure_remaining_proxies(void)
-{
- log_warn(LD_CONFIG, "We start configuring remaining managed proxies!");
- SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
- if (proxy_configuration_finished(mp)) /* finished managed proxies
- shouldn't be here */
- assert(0);
-
- configure_proxy(mp);
-
- } SMARTLIST_FOREACH_END(mp);
-}
-
-/** Receive input from the managed proxy <b>mp</b> to get closer to
- * finally configuring it. */
-static void
-configure_proxy(managed_proxy_t *mp)
-{
- enum stream_status r;
- char stdout_buf[200];
-
- while (1) {
- memset(stdout_buf, 0, sizeof(stdout_buf));
-
- r = get_string_from_pipe(mp->stdout, stdout_buf,
- sizeof(stdout_buf) - 1);
-
- if (r == ST_CLOSED || r == ST_TERM) {
- log_warn(LD_GENERAL, "Managed proxy stream closed. "
- "Most probably application stopped running");
- mp->conf_state = PT_PROTO_BROKEN;
- } else if (r == ST_EAGAIN) {
- return;
- } else {
- tor_assert(r == ST_OKAY);
- handle_proxy_line(stdout_buf, mp);
- }
-
- /* if the proxy finished configuring, exit the loop. */
- if (proxy_configuration_finished(mp)) {
- handle_finished_proxy(mp);
- return;
- }
- }
-}
-
-/** Handle a configured or broken managed proxy <b>mp</b>. */
-static void
-handle_finished_proxy(managed_proxy_t *mp)
-{
- switch (mp->conf_state) {
- case PT_PROTO_BROKEN: /* if broken: */
- managed_proxy_destroy(mp, 1); /* destroy it and all its transports */
- break;
- case PT_PROTO_CONFIGURED: /* if configured correctly: */
- register_proxy_transports(mp); /* register all its transports, */
- mp->conf_state = PT_PROTO_COMPLETED; /* mark it as completed, */
- managed_proxy_destroy(mp, 0); /* destroy the managed proxy struct,
- keeping the transports intact */
- break;
- default:
- log_warn(LD_CONFIG, "Unfinished managed proxy in "
- "handle_finished_proxy().");
- assert(0);
- }
-
- n_unconfigured_proxies--;
- tor_assert(n_unconfigured_proxies >= 0);
-}
-
-/** Register all the transports supported by managed proxy <b>mp</b>. */
-static void
-register_proxy_transports(managed_proxy_t *mp)
-{
- SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
- if (transport_add(t)<0) {
- log_warn(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
- transport_free(t);
- } else {
- log_warn(LD_GENERAL, "Succesfully registered transport %s", t->name);
- }
- } SMARTLIST_FOREACH_END(t);
-}
-
-/** Free memory allocated by managed proxy <b>mp</b>.
- * If <b>also_free_transports</b> is set, also free the transports
- * associated with this managed proxy. */
-static void
-managed_proxy_destroy(managed_proxy_t *mp, int also_free_transports)
-{
- /* transport_free() all its transports */
- if (also_free_transports)
- SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
-
- /* free the transports smartlist */
- smartlist_clear(mp->transports);
- smartlist_free(mp->transports);
-
- /* remove it from the list of managed proxies */
- smartlist_remove(unconfigured_proxy_list, mp);
-
- /* close its stdout stream */
- fclose(mp->stdout);
-
- tor_free(mp);
-}
-
-/** Return true if the configuration of the managed proxy <b>mp</b> is
- finished. */
-static INLINE int
-proxy_configuration_finished(managed_proxy_t *mp)
-{
- return (mp->conf_state == PT_PROTO_CONFIGURED ||
- mp->conf_state == PT_PROTO_BROKEN);
-}
-
-/** Handle a configuration protocol <b>line</b> received from a
- * managed proxy <b>mp</b>. */
-void
-handle_proxy_line(char *line, managed_proxy_t *mp)
-{
- printf("Judging line: %s\n", line);
-
- if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
- log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
- "Discarding");
- goto err;
- }
-
- if (!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR))) {
- if (mp->conf_state != PT_PROTO_INFANT)
- goto err;
-
- parse_env_error(line);
- goto err;
- } else if (!strncmp(line, PROTO_NEG_FAIL, strlen(PROTO_NEG_FAIL))) {
- if (mp->conf_state != PT_PROTO_INFANT)
- goto err;
-
- log_warn(LD_CONFIG, "Managed proxy could not pick a "
- "configuration protocol version.");
- goto err;
- } else if (!strncmp(line, PROTO_NEG_SUCCESS,
- strlen(PROTO_NEG_SUCCESS))) {
- if (mp->conf_state != PT_PROTO_INFANT)
- goto err;
-
- if (parse_version(line,mp) < 0)
- goto err;
-
- tor_assert(mp->conf_protocol != 0);
- mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
- return;
- } else if (!strncmp(line, PROTO_CMETHODS_DONE,
- strlen(PROTO_CMETHODS_DONE))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- log_warn(LD_CONFIG, "Client managed proxy configuration completed!");
- mp->conf_state = PT_PROTO_CONFIGURED;
- return;
- } else if (!strncmp(line, PROTO_SMETHODS_DONE,
- strlen(PROTO_SMETHODS_DONE))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- log_warn(LD_CONFIG, "Server managed proxy configuration completed!");
- mp->conf_state = PT_PROTO_CONFIGURED;
- return;
- } else if (!strncmp(line, PROTO_CMETHOD_ERROR,
- strlen(PROTO_CMETHOD_ERROR))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- parse_client_method_error(line);
- goto err;
- } else if (!strncmp(line, PROTO_SMETHOD_ERROR,
- strlen(PROTO_SMETHOD_ERROR))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- parse_server_method_error(line);
- goto err;
- } else if (!strncmp(line, PROTO_CMETHOD, strlen(PROTO_CMETHOD))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- if (parse_cmethod_line(line, mp) < 0)
- goto err;
-
- return;
- } else if (!strncmp(line, PROTO_SMETHOD, strlen(PROTO_SMETHOD))) {
- if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
- goto err;
-
- if (parse_smethod_line(line, mp) < 0)
- goto err;
-
- return;
- }
-
- log_warn(LD_CONFIG, "Unknown line received by managed proxy. (%s)", line);
-
- err:
- mp->conf_state = PT_PROTO_BROKEN;
- return;
-}
-
-/** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
-void
-parse_env_error(char *line)
-{
- tor_assert(!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR)));
-
- /* (Length of the protocol string) plus (a space) and (the first char of
- the error message) */
- if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
- log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
- "message.", PROTO_ENV_ERROR);
-
- log_warn(LD_CONFIG, "Managed proxy couldn't understand the "
- "pluggable transport environment variables. (%s)",
- line+strlen(PROTO_ENV_ERROR)+1);
-}
-
-/** Handles a VERSION <b>line</b>. Updates the configuration protocol
- * version in <b>mp</b>. */
-int
-parse_version(char *line, managed_proxy_t *mp)
-{
- tor_assert(!strncmp(line, PROTO_NEG_SUCCESS, strlen(PROTO_NEG_SUCCESS)));
-
- if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
- log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
- PROTO_NEG_SUCCESS);
- return -1;
- }
-
- if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) {
- log_warn(LD_CONFIG, "We don't support version '%s'. "
- "We only support version '1'", line+strlen(PROTO_NEG_SUCCESS)+1);
- return -1;
- }
-
- mp->conf_protocol = PROTO_VERSION_ONE; /* temp. till more versions appear */
- return 0;
-}
-
-/** Parses {C,S}METHOD-ERROR <b>line</b> and warns the user
- * accordingly. If <b>is_server</b> it is an SMETHOD-ERROR,
- * otherwise it is a CMETHOD-ERROR. */
-static void
-parse_method_error(char *line, int is_server)
-{
- const char* error = is_server ?
- PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;
-
- /* (Length of the protocol string) plus (a space) and (the first char of
- the error message) */
- if (strlen(line) < (strlen(error) + 2))
- log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
- "message.", error);
-
- log_warn(LD_CONFIG, "%s managed proxy encountered a method error. (%s)",
- is_server ? "Server" : "Client",
- line+strlen(error)+1);
-}
-
-/** Parses an SMETHOD <b>line</b>. */
-int
-parse_smethod_line(char *line, managed_proxy_t *mp)
-{
- int r;
- smartlist_t *items = NULL;
-
- char *method_name=NULL;
-
- char *addrport=NULL;
- tor_addr_t addr;
- uint16_t port = 0;
-
- items = smartlist_create();
- smartlist_split_string(items, line, NULL,
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
- if (smartlist_len(items) < 3) {
- log_warn(LD_CONFIG, "Server managed proxy sent us a SMETHOD line "
- "with too few arguments.");
- goto err;
- }
-
- tor_assert(!strcmp(smartlist_get(items,0),PROTO_SMETHOD));
-
- method_name = smartlist_get(items,1);
-
- addrport = smartlist_get(items, 2);
- if (tor_addr_port_parse(addrport, &addr, &port)<0) {
- log_warn(LD_CONFIG, "Error parsing transport "
- "address '%s'", addrport);
- goto err;
- }
-
- if (!port) {
- log_warn(LD_CONFIG,
- "Transport address '%s' has no port.", addrport);
- goto err;
- }
-
- /* For now, notify the user so that he knows where the server
- transport is listening. */
- log_warn(LD_CONFIG, "Server transport %s at %s:%d.",
- method_name, fmt_addr(&addr), (int)port);
-
- r=0;
- goto done;
-
- err:
- r = -1;
-
- done:
- SMARTLIST_FOREACH(items, char*, s, tor_free(s));
- smartlist_free(items);
- return r;
-}
-
-/** Parses a CMETHOD <b>line</b>, and if well-formed it registers
- * the new transport in <b>mp</b>. */
-int
-parse_cmethod_line(char *line, managed_proxy_t *mp)
-{
- int r;
- smartlist_t *items = NULL;
-
- char *method_name=NULL;
-
- char *socks_ver_str=NULL;
- int socks_ver=PROXY_NONE;
-
- char *addrport=NULL;
- tor_addr_t addr;
- uint16_t port = 0;
-
- transport_t *transport=NULL;
-
- items = smartlist_create();
- smartlist_split_string(items, line, NULL,
- SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
- if (smartlist_len(items) < 4) {
- log_warn(LD_CONFIG, "Client managed proxy sent us a CMETHOD line "
- "with too few arguments.");
- goto err;
- }
-
- tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));
-
- method_name = smartlist_get(items,1);
-
- socks_ver_str = smartlist_get(items,2);
-
- if (!strcmp(socks_ver_str,"socks4")) {
- socks_ver = PROXY_SOCKS4;
- } else if (!strcmp(socks_ver_str,"socks5")) {
- socks_ver = PROXY_SOCKS5;
- } else {
- log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
- "we don't recognize. (%s)", socks_ver_str);
- goto err;
- }
-
- addrport = smartlist_get(items, 3);
- if (tor_addr_port_parse(addrport, &addr, &port)<0) {
- log_warn(LD_CONFIG, "Error parsing transport "
- "address '%s'", addrport);
- goto err;
- }
-
- if (!port) {
- log_warn(LD_CONFIG,
- "Transport address '%s' has no port.", addrport);
- goto err;
- }
-
- transport = transport_create(&addr, port, method_name, socks_ver);
- if (!transport)
- goto err;
-
- smartlist_add(mp->transports, transport);
-
- log_warn(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
- "Attached to managed proxy.",
- method_name, fmt_addr(&addr), (int)port, socks_ver);
-
- r=0;
- goto done;
-
- err:
- r = -1;
-
- done:
- SMARTLIST_FOREACH(items, char*, s, tor_free(s));
- smartlist_free(items);
- return r;
-}
-
-/** Prepares the <b>envp</b> of a pluggable transport managed proxy
- *
- * <b>method</b> is a line with transport methods to be launched.
- * If <b>is_server</b> is set, prepare a server proxy <b>envp</b>. */
-static void
-set_environ(char ***envp, const char *method, int is_server)
-{
- or_options_t *options = get_options();
- char **tmp=NULL;
- char *state_loc=NULL;
-
- int n_envs = is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
-
- /* allocate enough space for our env. vars and a NULL pointer */
- *envp = tor_malloc(sizeof(char*)*(n_envs+1));
- tmp = *envp;
-
- /* these should all be customizable */
- tor_asprintf(tmp++, "HOME=%s", getenv("HOME"));
- tor_asprintf(tmp++, "PATH=%s", getenv("PATH"));
- state_loc = get_datadir_fname("pt_state/");
- tor_asprintf(tmp++, "TOR_PT_STATE_LOCATION=%s", state_loc);
- tor_free(state_loc);
- tor_asprintf(tmp++, "TOR_PT_MANAGED_TRANSPORT_VER=1"); /* temp */
- if (is_server) {
- /* ASN check for ORPort values, should we be here if it's 0? */
- tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort); /* temp */
- tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=127.0.0.1:0");
- tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", method);
- tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200"); /* temp*/
- } else {
- tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", method);
- }
- *tmp = NULL;
-}
-
-/* ASN is this too ugly/stupid? */
-/** Frees the array of pointers in <b>arg</b> used as arguments to
- execve. */
-static INLINE void
-free_execve_args(char **arg)
-{
- char **tmp = arg;
- while (*tmp) /* use the fact that the last element of the array is a
- NULL pointer to know when to stop freeing */
- _tor_free(*tmp++);
-
- tor_free(arg);
-}
-
-/** Release all storage held by the pluggable transports subsystem. */
-void
-pt_free_all(void)
-{
- if (unconfigured_proxy_list) {
- /* If the proxy is in PT_PROTO_COMPLETED, it has registered its
- transports and it's the duty of the circuitbuild.c subsystem to
- free them. Otherwise, it hasn't registered its transports yet
- and we should free them here. */
- SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
- if (mp->conf_state == PT_PROTO_COMPLETED)
- managed_proxy_destroy(mp,0);
- else
- managed_proxy_destroy(mp,1);
- } SMARTLIST_FOREACH_END(mp);
-
- smartlist_clear(unconfigured_proxy_list);
- smartlist_free(unconfigured_proxy_list);
- unconfigured_proxy_list=NULL;
- }
-}
-
diff --git a/src/or/pluggable_transports.h b/src/or/pluggable_transports.h
deleted file mode 100644
index 80d5429..0000000
--- a/src/or/pluggable_transports.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/* Copyright (c) 2003-2004, Roger Dingledine
- * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2011, The Tor Project, Inc. */
-/* See LICENSE for licensing information */
-
-/**
- * \file pluggable_transpots.h
- * \brief Headers for pluggable_transpots.c
- **/
-
-#ifndef TOR_PLUGGABLE_TRANSPORTS_H
-#define TOR_PLUGGABLE_TRANSPORTS_H
-
-int pt_managed_launch_proxy(const char *method,
- char **proxy_argv, int is_server);
-
-#define pt_managed_launch_client_proxy(m, pa) \
- pt_managed_launch_proxy(m, pa, 0)
-#define pt_managed_launch_server_proxy(m, pa) \
- pt_managed_launch_proxy(m, pa, 1)
-
-void pt_configure_remaining_proxies(void);
-
-int pt_proxies_configuration_pending(void);
-
-void pt_free_all(void);
-
-#ifdef PT_PRIVATE
-/** State of the managed proxy configuration protocol. */
-enum pt_proto_state {
- PT_PROTO_INFANT, /* was just born */
- PT_PROTO_ACCEPTING_METHODS, /* accepting methods */
- PT_PROTO_CONFIGURED, /* configured successfully */
- PT_PROTO_COMPLETED, /* configure and registered its transports */
- PT_PROTO_BROKEN
-};
-
-/** Structure containing information of a managed proxy. */
-typedef struct {
- enum pt_proto_state conf_state; /* the current configuration state */
- int conf_protocol; /* the configuration protocol version used */
-
- FILE *stdout; /* a stream to its stdout
- (closed in managed_proxy_destroy()) */
-
- smartlist_t *transports; /* list of transports this proxy spawns */
-} managed_proxy_t;
-
-int parse_cmethod_line(char *line, managed_proxy_t *mp);
-int parse_smethod_line(char *line, managed_proxy_t *mp);
-
-int parse_version(char *line, managed_proxy_t *mp);
-void parse_env_error(char *line);
-void handle_proxy_line(char *line, managed_proxy_t *mp);
-
-#endif
-
-#endif
-
diff --git a/src/or/transports.c b/src/or/transports.c
new file mode 100644
index 0000000..49b0e13
--- /dev/null
+++ b/src/or/transports.c
@@ -0,0 +1,613 @@
+/* Copyright (c) 2011, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file pluggable_transports.c
+ * \brief Pluggable Transports related code.
+ **/
+
+#define PT_PRIVATE
+#include "or.h"
+#include "config.h"
+#include "circuitbuild.h"
+#include "pluggable_transports.h"
+
+/* ASN TIDY THESE UP*/
+static void set_environ(char ***envp, const char *method,
+ int is_server);
+static INLINE int proxy_configuration_finished(managed_proxy_t *mp);
+
+static void managed_proxy_destroy(managed_proxy_t *mp,
+ int also_free_transports);
+static void register_proxy_transports(managed_proxy_t *mp);
+static void handle_finished_proxy(managed_proxy_t *mp);
+static void configure_proxy(managed_proxy_t *mp);
+
+static void parse_method_error(char *line, int is_server_method);
+#define parse_server_method_error(l) parse_method_error(l, 1)
+#define parse_client_method_error(l) parse_method_error(l, 0)
+
+static INLINE void free_execve_args(char **arg);
+
+/** Managed proxy protocol strings */
+#define PROTO_ENV_ERROR "ENV-ERROR"
+#define PROTO_NEG_SUCCESS "VERSION"
+#define PROTO_NEG_FAIL "VERSION-ERROR no-version"
+#define PROTO_CMETHOD "CMETHOD"
+#define PROTO_SMETHOD "SMETHOD"
+#define PROTO_CMETHOD_ERROR "CMETHOD-ERROR"
+#define PROTO_SMETHOD_ERROR "SMETHOD-ERROR"
+#define PROTO_CMETHODS_DONE "CMETHODS DONE"
+#define PROTO_SMETHODS_DONE "SMETHODS DONE"
+
+/* The smallest valid managed proxy protocol line that can
+ appear. It's the size of "VERSION 1" */
+#define SMALLEST_MANAGED_LINE_SIZE 9
+
+/** Number of environment variables for managed proxy clients/servers. */
+#define ENVIRON_SIZE_CLIENT 5
+#define ENVIRON_SIZE_SERVER 8
+
+/** The first and only supported - at the moment - configuration
+ protocol version. */
+#define PROTO_VERSION_ONE 1
+
+/** List of unconfigured managed proxies. */
+static smartlist_t *unconfigured_proxy_list = NULL;
+/** Number of unconfigured managed proxies. */
+static int n_unconfigured_proxies = 0;
+
+/* The main idea is:
+
+ A managed proxy is represented by a managed_proxy_t struct and can
+ spawn multiple transports.
+
+ unconfigured_proxy_list is a list of all the unconfigured managed
+ proxies; everytime we spawn a managed proxy we add it in that
+ list.
+ In every run_scheduled_event() tick, we attempt to configure each
+ managed proxy further, using the configuration protocol defined in
+ the 180_pluggable_transport.txt proposal.
+
+ When a managed proxy is fully configured, we register all its
+ transports to the circuitbuild.c subsystem - like we do with
+ external proxies - and then free the managed proxy struct
+ since it's no longer needed. */
+
+/** Return true if there are still unconfigured managed proxies. */
+int
+pt_proxies_configuration_pending(void)
+{
+ return !!n_unconfigured_proxies;
+}
+
+/** Launch a proxy for <b>method</b> using <b>proxy_argv</b> as its
+ * arguments. If <b>is_server</b>, launch a server proxy. */
+int
+pt_managed_launch_proxy(const char *method,
+ char **proxy_argv, int is_server)
+{
+ char **envp=NULL;
+ int retval;
+ FILE *stdout_read = NULL;
+ int stdout_pipe=-1, stderr_pipe=-1;
+
+ /* prepare the environment variables for the managed proxy */
+ set_environ(&envp, method, is_server);
+
+ /* ASN we should probably check if proxy_argv[0] is executable by our user */
+ retval = tor_spawn_background(proxy_argv[0], &stdout_pipe,
+ &stderr_pipe, (const char **)proxy_argv,
+ (const char **)envp);
+ if (retval < 0) {
+ log_warn(LD_GENERAL, "Spawn failed");
+ return -1;
+ }
+
+ /* free the memory allocated for the execve() */
+ free_execve_args(envp);
+ free_execve_args(proxy_argv);
+
+ /* Set stdout/stderr pipes to be non-blocking */
+ fcntl(stdout_pipe, F_SETFL, O_NONBLOCK);
+ /* Open the buffered IO streams */
+ stdout_read = fdopen(stdout_pipe, "r");
+
+ log_warn(LD_CONFIG, "The spawn is alive (%d)!", retval);
+
+ /* create a managed proxy */
+ managed_proxy_t *mp = tor_malloc(sizeof(managed_proxy_t));
+ mp->conf_state = PT_PROTO_INFANT;
+ mp->stdout = stdout_read;
+ mp->transports = smartlist_create();
+
+ /* register the managed proxy */
+ if (!unconfigured_proxy_list)
+ unconfigured_proxy_list = smartlist_create();
+ smartlist_add(unconfigured_proxy_list, mp);
+
+ n_unconfigured_proxies++; /* ASN should we care about overflows here?
+ I say no. */
+
+ return 0;
+}
+
+/** Check if any of the managed proxies we are currently trying to
+ * configure have anything new to say. This is called from
+ * run_scheduled_events(). */
+void
+pt_configure_remaining_proxies(void)
+{
+ log_warn(LD_CONFIG, "We start configuring remaining managed proxies!");
+ SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
+ if (proxy_configuration_finished(mp)) /* finished managed proxies
+ shouldn't be here */
+ assert(0);
+
+ configure_proxy(mp);
+
+ } SMARTLIST_FOREACH_END(mp);
+}
+
+/** Receive input from the managed proxy <b>mp</b> to get closer to
+ * finally configuring it. */
+static void
+configure_proxy(managed_proxy_t *mp)
+{
+ enum stream_status r;
+ char stdout_buf[200];
+
+ while (1) {
+ memset(stdout_buf, 0, sizeof(stdout_buf));
+
+ r = get_string_from_pipe(mp->stdout, stdout_buf,
+ sizeof(stdout_buf) - 1);
+
+ if (r == ST_CLOSED || r == ST_TERM) {
+ log_warn(LD_GENERAL, "Managed proxy stream closed. "
+ "Most probably application stopped running");
+ mp->conf_state = PT_PROTO_BROKEN;
+ } else if (r == ST_EAGAIN) {
+ return;
+ } else {
+ tor_assert(r == ST_OKAY);
+ handle_proxy_line(stdout_buf, mp);
+ }
+
+ /* if the proxy finished configuring, exit the loop. */
+ if (proxy_configuration_finished(mp)) {
+ handle_finished_proxy(mp);
+ return;
+ }
+ }
+}
+
+/** Handle a configured or broken managed proxy <b>mp</b>. */
+static void
+handle_finished_proxy(managed_proxy_t *mp)
+{
+ switch (mp->conf_state) {
+ case PT_PROTO_BROKEN: /* if broken: */
+ managed_proxy_destroy(mp, 1); /* destroy it and all its transports */
+ break;
+ case PT_PROTO_CONFIGURED: /* if configured correctly: */
+ register_proxy_transports(mp); /* register all its transports, */
+ mp->conf_state = PT_PROTO_COMPLETED; /* mark it as completed, */
+ managed_proxy_destroy(mp, 0); /* destroy the managed proxy struct,
+ keeping the transports intact */
+ break;
+ default:
+ log_warn(LD_CONFIG, "Unfinished managed proxy in "
+ "handle_finished_proxy().");
+ assert(0);
+ }
+
+ n_unconfigured_proxies--;
+ tor_assert(n_unconfigured_proxies >= 0);
+}
+
+/** Register all the transports supported by managed proxy <b>mp</b>. */
+static void
+register_proxy_transports(managed_proxy_t *mp)
+{
+ SMARTLIST_FOREACH_BEGIN(mp->transports, transport_t *, t) {
+ if (transport_add(t)<0) {
+ log_warn(LD_GENERAL, "Could not add transport %s. Skipping.", t->name);
+ transport_free(t);
+ } else {
+ log_warn(LD_GENERAL, "Succesfully registered transport %s", t->name);
+ }
+ } SMARTLIST_FOREACH_END(t);
+}
+
+/** Free memory allocated by managed proxy <b>mp</b>.
+ * If <b>also_free_transports</b> is set, also free the transports
+ * associated with this managed proxy. */
+static void
+managed_proxy_destroy(managed_proxy_t *mp, int also_free_transports)
+{
+ /* transport_free() all its transports */
+ if (also_free_transports)
+ SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
+
+ /* free the transports smartlist */
+ smartlist_clear(mp->transports);
+ smartlist_free(mp->transports);
+
+ /* remove it from the list of managed proxies */
+ smartlist_remove(unconfigured_proxy_list, mp);
+
+ /* close its stdout stream */
+ fclose(mp->stdout);
+
+ tor_free(mp);
+}
+
+/** Return true if the configuration of the managed proxy <b>mp</b> is
+ finished. */
+static INLINE int
+proxy_configuration_finished(managed_proxy_t *mp)
+{
+ return (mp->conf_state == PT_PROTO_CONFIGURED ||
+ mp->conf_state == PT_PROTO_BROKEN);
+}
+
+/** Handle a configuration protocol <b>line</b> received from a
+ * managed proxy <b>mp</b>. */
+void
+handle_proxy_line(char *line, managed_proxy_t *mp)
+{
+ printf("Judging line: %s\n", line);
+
+ if (strlen(line) < SMALLEST_MANAGED_LINE_SIZE) {
+ log_warn(LD_GENERAL, "Managed proxy configuration line is too small. "
+ "Discarding");
+ goto err;
+ }
+
+ if (!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR))) {
+ if (mp->conf_state != PT_PROTO_INFANT)
+ goto err;
+
+ parse_env_error(line);
+ goto err;
+ } else if (!strncmp(line, PROTO_NEG_FAIL, strlen(PROTO_NEG_FAIL))) {
+ if (mp->conf_state != PT_PROTO_INFANT)
+ goto err;
+
+ log_warn(LD_CONFIG, "Managed proxy could not pick a "
+ "configuration protocol version.");
+ goto err;
+ } else if (!strncmp(line, PROTO_NEG_SUCCESS,
+ strlen(PROTO_NEG_SUCCESS))) {
+ if (mp->conf_state != PT_PROTO_INFANT)
+ goto err;
+
+ if (parse_version(line,mp) < 0)
+ goto err;
+
+ tor_assert(mp->conf_protocol != 0);
+ mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
+ return;
+ } else if (!strncmp(line, PROTO_CMETHODS_DONE,
+ strlen(PROTO_CMETHODS_DONE))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ log_warn(LD_CONFIG, "Client managed proxy configuration completed!");
+ mp->conf_state = PT_PROTO_CONFIGURED;
+ return;
+ } else if (!strncmp(line, PROTO_SMETHODS_DONE,
+ strlen(PROTO_SMETHODS_DONE))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ log_warn(LD_CONFIG, "Server managed proxy configuration completed!");
+ mp->conf_state = PT_PROTO_CONFIGURED;
+ return;
+ } else if (!strncmp(line, PROTO_CMETHOD_ERROR,
+ strlen(PROTO_CMETHOD_ERROR))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ parse_client_method_error(line);
+ goto err;
+ } else if (!strncmp(line, PROTO_SMETHOD_ERROR,
+ strlen(PROTO_SMETHOD_ERROR))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ parse_server_method_error(line);
+ goto err;
+ } else if (!strncmp(line, PROTO_CMETHOD, strlen(PROTO_CMETHOD))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ if (parse_cmethod_line(line, mp) < 0)
+ goto err;
+
+ return;
+ } else if (!strncmp(line, PROTO_SMETHOD, strlen(PROTO_SMETHOD))) {
+ if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
+ goto err;
+
+ if (parse_smethod_line(line, mp) < 0)
+ goto err;
+
+ return;
+ }
+
+ log_warn(LD_CONFIG, "Unknown line received by managed proxy. (%s)", line);
+
+ err:
+ mp->conf_state = PT_PROTO_BROKEN;
+ return;
+}
+
+/** Parses an ENV-ERROR <b>line</b> and warns the user accordingly. */
+void
+parse_env_error(char *line)
+{
+ tor_assert(!strncmp(line, PROTO_ENV_ERROR, strlen(PROTO_ENV_ERROR)));
+
+ /* (Length of the protocol string) plus (a space) and (the first char of
+ the error message) */
+ if (strlen(line) < (strlen(PROTO_ENV_ERROR) + 2))
+ log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
+ "message.", PROTO_ENV_ERROR);
+
+ log_warn(LD_CONFIG, "Managed proxy couldn't understand the "
+ "pluggable transport environment variables. (%s)",
+ line+strlen(PROTO_ENV_ERROR)+1);
+}
+
+/** Handles a VERSION <b>line</b>. Updates the configuration protocol
+ * version in <b>mp</b>. */
+int
+parse_version(char *line, managed_proxy_t *mp)
+{
+ tor_assert(!strncmp(line, PROTO_NEG_SUCCESS, strlen(PROTO_NEG_SUCCESS)));
+
+ if (strlen(line) < (strlen(PROTO_NEG_SUCCESS) + 2)) {
+ log_warn(LD_CONFIG, "Managed proxy sent us malformed %s line.",
+ PROTO_NEG_SUCCESS);
+ return -1;
+ }
+
+ if (strcmp("1", line+strlen(PROTO_NEG_SUCCESS)+1)) {
+ log_warn(LD_CONFIG, "We don't support version '%s'. "
+ "We only support version '1'", line+strlen(PROTO_NEG_SUCCESS)+1);
+ return -1;
+ }
+
+ mp->conf_protocol = PROTO_VERSION_ONE; /* temp. till more versions appear */
+ return 0;
+}
+
+/** Parses {C,S}METHOD-ERROR <b>line</b> and warns the user
+ * accordingly. If <b>is_server</b> it is an SMETHOD-ERROR,
+ * otherwise it is a CMETHOD-ERROR. */
+static void
+parse_method_error(char *line, int is_server)
+{
+ const char* error = is_server ?
+ PROTO_SMETHOD_ERROR : PROTO_CMETHOD_ERROR;
+
+ /* (Length of the protocol string) plus (a space) and (the first char of
+ the error message) */
+ if (strlen(line) < (strlen(error) + 2))
+ log_warn(LD_CONFIG, "Managed proxy sent us an %s without an error "
+ "message.", error);
+
+ log_warn(LD_CONFIG, "%s managed proxy encountered a method error. (%s)",
+ is_server ? "Server" : "Client",
+ line+strlen(error)+1);
+}
+
+/** Parses an SMETHOD <b>line</b>. */
+int
+parse_smethod_line(char *line, managed_proxy_t *mp)
+{
+ int r;
+ smartlist_t *items = NULL;
+
+ char *method_name=NULL;
+
+ char *addrport=NULL;
+ tor_addr_t addr;
+ uint16_t port = 0;
+
+ items = smartlist_create();
+ smartlist_split_string(items, line, NULL,
+ SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
+ if (smartlist_len(items) < 3) {
+ log_warn(LD_CONFIG, "Server managed proxy sent us a SMETHOD line "
+ "with too few arguments.");
+ goto err;
+ }
+
+ tor_assert(!strcmp(smartlist_get(items,0),PROTO_SMETHOD));
+
+ method_name = smartlist_get(items,1);
+
+ addrport = smartlist_get(items, 2);
+ if (tor_addr_port_parse(addrport, &addr, &port)<0) {
+ log_warn(LD_CONFIG, "Error parsing transport "
+ "address '%s'", addrport);
+ goto err;
+ }
+
+ if (!port) {
+ log_warn(LD_CONFIG,
+ "Transport address '%s' has no port.", addrport);
+ goto err;
+ }
+
+ /* For now, notify the user so that he knows where the server
+ transport is listening. */
+ log_warn(LD_CONFIG, "Server transport %s at %s:%d.",
+ method_name, fmt_addr(&addr), (int)port);
+
+ r=0;
+ goto done;
+
+ err:
+ r = -1;
+
+ done:
+ SMARTLIST_FOREACH(items, char*, s, tor_free(s));
+ smartlist_free(items);
+ return r;
+}
+
+/** Parses a CMETHOD <b>line</b>, and if well-formed it registers
+ * the new transport in <b>mp</b>. */
+int
+parse_cmethod_line(char *line, managed_proxy_t *mp)
+{
+ int r;
+ smartlist_t *items = NULL;
+
+ char *method_name=NULL;
+
+ char *socks_ver_str=NULL;
+ int socks_ver=PROXY_NONE;
+
+ char *addrport=NULL;
+ tor_addr_t addr;
+ uint16_t port = 0;
+
+ transport_t *transport=NULL;
+
+ items = smartlist_create();
+ smartlist_split_string(items, line, NULL,
+ SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, -1);
+ if (smartlist_len(items) < 4) {
+ log_warn(LD_CONFIG, "Client managed proxy sent us a CMETHOD line "
+ "with too few arguments.");
+ goto err;
+ }
+
+ tor_assert(!strcmp(smartlist_get(items,0),PROTO_CMETHOD));
+
+ method_name = smartlist_get(items,1);
+
+ socks_ver_str = smartlist_get(items,2);
+
+ if (!strcmp(socks_ver_str,"socks4")) {
+ socks_ver = PROXY_SOCKS4;
+ } else if (!strcmp(socks_ver_str,"socks5")) {
+ socks_ver = PROXY_SOCKS5;
+ } else {
+ log_warn(LD_CONFIG, "Client managed proxy sent us a proxy protocol "
+ "we don't recognize. (%s)", socks_ver_str);
+ goto err;
+ }
+
+ addrport = smartlist_get(items, 3);
+ if (tor_addr_port_parse(addrport, &addr, &port)<0) {
+ log_warn(LD_CONFIG, "Error parsing transport "
+ "address '%s'", addrport);
+ goto err;
+ }
+
+ if (!port) {
+ log_warn(LD_CONFIG,
+ "Transport address '%s' has no port.", addrport);
+ goto err;
+ }
+
+ transport = transport_create(&addr, port, method_name, socks_ver);
+ if (!transport)
+ goto err;
+
+ smartlist_add(mp->transports, transport);
+
+ log_warn(LD_CONFIG, "Transport %s at %s:%d with SOCKS %d. "
+ "Attached to managed proxy.",
+ method_name, fmt_addr(&addr), (int)port, socks_ver);
+
+ r=0;
+ goto done;
+
+ err:
+ r = -1;
+
+ done:
+ SMARTLIST_FOREACH(items, char*, s, tor_free(s));
+ smartlist_free(items);
+ return r;
+}
+
+/** Prepares the <b>envp</b> of a pluggable transport managed proxy
+ *
+ * <b>method</b> is a line with transport methods to be launched.
+ * If <b>is_server</b> is set, prepare a server proxy <b>envp</b>. */
+static void
+set_environ(char ***envp, const char *method, int is_server)
+{
+ or_options_t *options = get_options();
+ char **tmp=NULL;
+ char *state_loc=NULL;
+
+ int n_envs = is_server ? ENVIRON_SIZE_SERVER : ENVIRON_SIZE_CLIENT;
+
+ /* allocate enough space for our env. vars and a NULL pointer */
+ *envp = tor_malloc(sizeof(char*)*(n_envs+1));
+ tmp = *envp;
+
+ /* these should all be customizable */
+ tor_asprintf(tmp++, "HOME=%s", getenv("HOME"));
+ tor_asprintf(tmp++, "PATH=%s", getenv("PATH"));
+ state_loc = get_datadir_fname("pt_state/");
+ tor_asprintf(tmp++, "TOR_PT_STATE_LOCATION=%s", state_loc);
+ tor_free(state_loc);
+ tor_asprintf(tmp++, "TOR_PT_MANAGED_TRANSPORT_VER=1"); /* temp */
+ if (is_server) {
+ /* ASN check for ORPort values, should we be here if it's 0? */
+ tor_asprintf(tmp++, "TOR_PT_ORPORT=127.0.0.1:%d", options->ORPort); /* temp */
+ tor_asprintf(tmp++, "TOR_PT_SERVER_BINDADDR=127.0.0.1:0");
+ tor_asprintf(tmp++, "TOR_PT_SERVER_TRANSPORTS=%s", method);
+ tor_asprintf(tmp++, "TOR_PT_EXTENDED_SERVER_PORT=127.0.0.1:4200"); /* temp*/
+ } else {
+ tor_asprintf(tmp++, "TOR_PT_CLIENT_TRANSPORTS=%s", method);
+ }
+ *tmp = NULL;
+}
+
+/* ASN is this too ugly/stupid? */
+/** Frees the array of pointers in <b>arg</b> used as arguments to
+ execve. */
+static INLINE void
+free_execve_args(char **arg)
+{
+ char **tmp = arg;
+ while (*tmp) /* use the fact that the last element of the array is a
+ NULL pointer to know when to stop freeing */
+ _tor_free(*tmp++);
+
+ tor_free(arg);
+}
+
+/** Release all storage held by the pluggable transports subsystem. */
+void
+pt_free_all(void)
+{
+ if (unconfigured_proxy_list) {
+ /* If the proxy is in PT_PROTO_COMPLETED, it has registered its
+ transports and it's the duty of the circuitbuild.c subsystem to
+ free them. Otherwise, it hasn't registered its transports yet
+ and we should free them here. */
+ SMARTLIST_FOREACH_BEGIN(unconfigured_proxy_list, managed_proxy_t *, mp) {
+ if (mp->conf_state == PT_PROTO_COMPLETED)
+ managed_proxy_destroy(mp,0);
+ else
+ managed_proxy_destroy(mp,1);
+ } SMARTLIST_FOREACH_END(mp);
+
+ smartlist_clear(unconfigured_proxy_list);
+ smartlist_free(unconfigured_proxy_list);
+ unconfigured_proxy_list=NULL;
+ }
+}
+
diff --git a/src/or/transports.h b/src/or/transports.h
new file mode 100644
index 0000000..80d5429
--- /dev/null
+++ b/src/or/transports.h
@@ -0,0 +1,59 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2011, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file pluggable_transpots.h
+ * \brief Headers for pluggable_transpots.c
+ **/
+
+#ifndef TOR_PLUGGABLE_TRANSPORTS_H
+#define TOR_PLUGGABLE_TRANSPORTS_H
+
+int pt_managed_launch_proxy(const char *method,
+ char **proxy_argv, int is_server);
+
+#define pt_managed_launch_client_proxy(m, pa) \
+ pt_managed_launch_proxy(m, pa, 0)
+#define pt_managed_launch_server_proxy(m, pa) \
+ pt_managed_launch_proxy(m, pa, 1)
+
+void pt_configure_remaining_proxies(void);
+
+int pt_proxies_configuration_pending(void);
+
+void pt_free_all(void);
+
+#ifdef PT_PRIVATE
+/** State of the managed proxy configuration protocol. */
+enum pt_proto_state {
+ PT_PROTO_INFANT, /* was just born */
+ PT_PROTO_ACCEPTING_METHODS, /* accepting methods */
+ PT_PROTO_CONFIGURED, /* configured successfully */
+ PT_PROTO_COMPLETED, /* configure and registered its transports */
+ PT_PROTO_BROKEN
+};
+
+/** Structure containing information of a managed proxy. */
+typedef struct {
+ enum pt_proto_state conf_state; /* the current configuration state */
+ int conf_protocol; /* the configuration protocol version used */
+
+ FILE *stdout; /* a stream to its stdout
+ (closed in managed_proxy_destroy()) */
+
+ smartlist_t *transports; /* list of transports this proxy spawns */
+} managed_proxy_t;
+
+int parse_cmethod_line(char *line, managed_proxy_t *mp);
+int parse_smethod_line(char *line, managed_proxy_t *mp);
+
+int parse_version(char *line, managed_proxy_t *mp);
+void parse_env_error(char *line);
+void handle_proxy_line(char *line, managed_proxy_t *mp);
+
+#endif
+
+#endif
+
1
0

07 Oct '11
commit a8f21f91cfa916b662d07dd486857fdf848c6f1d
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Mon Jul 18 02:33:31 2011 +0200
Updated #includes etc. to use transports.[ch].
---
src/or/Makefile.am | 4 ++--
src/or/circuitbuild.c | 2 +-
src/or/config.c | 2 +-
src/or/main.c | 2 +-
src/or/transports.c | 4 ++--
src/or/transports.h | 8 ++++----
src/test/test_pt.c | 2 +-
7 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/src/or/Makefile.am b/src/or/Makefile.am
index 4dc5ee5..41e89d7 100644
--- a/src/or/Makefile.am
+++ b/src/or/Makefile.am
@@ -39,7 +39,7 @@ libtor_a_SOURCES = \
networkstatus.c \
nodelist.c \
onion.c \
- pluggable_transports.c \
+ transports.c \
policies.c \
reasons.c \
relay.c \
@@ -105,7 +105,7 @@ noinst_HEADERS = \
ntmain.h \
onion.h \
or.h \
- pluggable_transports.h \
+ transports.h \
policies.h \
reasons.h \
relay.h \
diff --git a/src/or/circuitbuild.c b/src/or/circuitbuild.c
index 003f579..3dba83b 100644
--- a/src/or/circuitbuild.c
+++ b/src/or/circuitbuild.c
@@ -26,7 +26,7 @@
#include "nodelist.h"
#include "onion.h"
#include "policies.h"
-#include "pluggable_transports.h"
+#include "transports.h"
#include "relay.h"
#include "rephist.h"
#include "router.h"
diff --git a/src/or/config.c b/src/or/config.c
index 5f2f11d..da35270 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -35,7 +35,7 @@
#include "router.h"
#include "util.h"
#include "routerlist.h"
-#include "pluggable_transports.h"
+#include "transports.h"
#ifdef MS_WINDOWS
#include <shlobj.h>
#endif
diff --git a/src/or/main.c b/src/or/main.c
index d79d9eb..5294e4a 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -37,7 +37,7 @@
#include "ntmain.h"
#include "onion.h"
#include "policies.h"
-#include "pluggable_transports.h"
+#include "transports.h"
#include "relay.h"
#include "rendclient.h"
#include "rendcommon.h"
diff --git a/src/or/transports.c b/src/or/transports.c
index 49b0e13..aae39cd 100644
--- a/src/or/transports.c
+++ b/src/or/transports.c
@@ -2,7 +2,7 @@
/* See LICENSE for licensing information */
/**
- * \file pluggable_transports.c
+ * \file transports.c
* \brief Pluggable Transports related code.
**/
@@ -10,7 +10,7 @@
#include "or.h"
#include "config.h"
#include "circuitbuild.h"
-#include "pluggable_transports.h"
+#include "transports.h"
/* ASN TIDY THESE UP*/
static void set_environ(char ***envp, const char *method,
diff --git a/src/or/transports.h b/src/or/transports.h
index 80d5429..17a6803 100644
--- a/src/or/transports.h
+++ b/src/or/transports.h
@@ -4,12 +4,12 @@
/* See LICENSE for licensing information */
/**
- * \file pluggable_transpots.h
- * \brief Headers for pluggable_transpots.c
+ * \file transports.h
+ * \brief Headers for transports.c
**/
-#ifndef TOR_PLUGGABLE_TRANSPORTS_H
-#define TOR_PLUGGABLE_TRANSPORTS_H
+#ifndef TOR_TRANSPORTS_H
+#define TOR_TRANSPORTS_H
int pt_managed_launch_proxy(const char *method,
char **proxy_argv, int is_server);
diff --git a/src/test/test_pt.c b/src/test/test_pt.c
index 02950b8..99fc514 100644
--- a/src/test/test_pt.c
+++ b/src/test/test_pt.c
@@ -6,7 +6,7 @@
#include "orconfig.h"
#define PT_PRIVATE
#include "or.h"
-#include "pluggable_transports.h"
+#include "transports.h"
#include "circuitbuild.h"
#include "test.h"
1
0

[tor/master] Reverting the accounting thing introduced in 5492de76 till I think how it should be done properly.
by nickm@torproject.org 07 Oct '11
by nickm@torproject.org 07 Oct '11
07 Oct '11
commit 86b20e0d8ac0cc87cf85e34666d6cd25a9657521
Author: George Kadianakis <desnacked(a)gmail.com>
Date: Thu Jul 14 04:24:10 2011 +0200
Reverting the accounting thing introduced in 5492de76 till I think how it should be done properly.
---
src/or/connection.c | 10 +++-------
1 files changed, 3 insertions(+), 7 deletions(-)
diff --git a/src/or/connection.c b/src/or/connection.c
index 5e8f95f..04dba2b 100644
--- a/src/or/connection.c
+++ b/src/or/connection.c
@@ -2080,8 +2080,6 @@ 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)))
@@ -4158,11 +4156,9 @@ 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;
+ find_transport_by_bridge_addrport(&conn->addr,
+ conn->port,&transport);
+ return transport ? 1 : 0;
}
/** Returns the global proxy type used by tor. */
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] 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