From d7261fb7f1ac6892b034ea47cdf59577188af93f Mon Sep 17 00:00:00 2001 From: George Kadianakis Date: Thu, 7 Jun 2012 00:43:20 +0300 Subject: Implement the new TCP port parsing logic in tor-fw-helper. --- src/tools/tor-fw-helper/tor-fw-helper.c | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'src/tools') diff --git a/src/tools/tor-fw-helper/tor-fw-helper.c b/src/tools/tor-fw-helper/tor-fw-helper.c index d23f9ba90..9e5fbca52 100644 --- a/src/tools/tor-fw-helper/tor-fw-helper.c +++ b/src/tools/tor-fw-helper/tor-fw-helper.c @@ -21,6 +21,8 @@ #include #include +#include "container.h" + #ifdef _WIN32 #include #endif @@ -244,6 +246,50 @@ network_init(void) return 0; } +/** Parse the '-p' argument of tor-fw-helper. Its format is + * []:, and is optional. + * Return NULL if arg was c0rrupted. */ +static port_to_forward_t * +parse_port(const char *arg) +{ + smartlist_t *sl = smartlist_new(); + port_to_forward_t *port_to_forward = NULL; + char *port_str = NULL; + int ok; + int port; + + smartlist_split_string(sl, arg, ":", 0, 0); + if (smartlist_len(sl) != 2) + goto err; + + port_to_forward = tor_malloc(sizeof(port_to_forward_t)); + if (!port_to_forward) + goto err; + + port_str = smartlist_get(sl, 0); /* macroify ? */ + port = (int)tor_parse_long(port_str, 10, 1, 65536, &ok, NULL); + if (!ok && strlen(port_str)) /* ":1555" is valid */ + goto err; + port_to_forward->external_port = port; + + port_str = smartlist_get(sl, 1); + port = (int)tor_parse_long(port_str, 10, 1, 65536, &ok, NULL); + if (!ok) + goto err; + port_to_forward->internal_port = port; + + goto done; + + err: + tor_free(port_to_forward); + + done: + SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp)); + smartlist_free(sl); + + return port_to_forward; +} + int main(int argc, char **argv) { @@ -285,6 +331,16 @@ main(int argc, char **argv) exit(1); } + /* If no external port was given (it's optional), set it to be + * equal with the internal port. */ + if (!port_to_forward->external_port) { + assert(port_to_forward->internal_port); + if (tor_fw_options.verbose) + fprintf(stderr, "V: No external port was given. Setting to %u.\n", + port_to_forward->internal_port); + port_to_forward->external_port = port_to_forward->internal_port; + } + if (!tor_fw_options.ports_to_forward) tor_fw_options.ports_to_forward = smartlist_new(); -- cgit v1.2.3