commit d7261fb7f1ac6892b034ea47cdf59577188af93f
Author: George Kadianakis <desnac...@riseup.net>
Date:   Thu Jun 7 00:43:20 2012 +0300

    Implement the new TCP port parsing logic in tor-fw-helper.
---
 src/tools/tor-fw-helper/tor-fw-helper.c |   56 +++++++++++++++++++++++++++++++
 1 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/src/tools/tor-fw-helper/tor-fw-helper.c 
b/src/tools/tor-fw-helper/tor-fw-helper.c
index d23f9ba..9e5fbca 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 <time.h>
 #include <string.h>
 
+#include "container.h"
+
 #ifdef _WIN32
 #include <winsock2.h>
 #endif
@@ -244,6 +246,50 @@ network_init(void)
   return 0;
 }
 
+/** Parse the '-p' argument of tor-fw-helper. Its format is
+ *  [<external port>]:<internal port>, and <external port> is optional.
+ *  Return NULL if <b>arg</b> 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();
 



_______________________________________________
tor-commits mailing list
tor-commits@lists.torproject.org
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits

Reply via email to