[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r9866: Remove some duplicated code in control.c: unify the "parse s (in tor/trunk: . src/or)



Author: nickm
Date: 2007-03-17 17:34:41 -0400 (Sat, 17 Mar 2007)
New Revision: 9866

Modified:
   tor/trunk/
   tor/trunk/src/or/control.c
Log:
 r12230@catbus:  nickm | 2007-03-17 17:34:32 -0400
 Remove some duplicated code in control.c: unify the "parse space-separated arguments and warn if there are too few" logic into a single helper function.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r12230] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/src/or/control.c
===================================================================
--- tor/trunk/src/or/control.c	2007-03-17 21:34:37 UTC (rev 9865)
+++ tor/trunk/src/or/control.c	2007-03-17 21:34:41 UTC (rev 9866)
@@ -1705,6 +1705,32 @@
   return 0;
 }
 
+/** Return a newly allocated smartlist containing the arguments to the command
+ * waiting in <b>body</b>. If there are fewer than <b>min_args</b> arguments,
+ * or if <b>max_args</b> is nonnegative and there are more than
+ * <b>max_args</b> arguments, send a 512 error to the controller, using
+ * <b>command</b> as the command name in the error message. */
+static smartlist_t *
+getargs_helper(const char *command, control_connection_t *conn,
+               const char *body, int min_args, int max_args)
+{
+  smartlist_t *args = smartlist_create();
+  smartlist_split_string(args, body, " ",
+                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
+  if (smartlist_len(args) < min_args) {
+    connection_printf_to_buf(conn, "512 Missing argument to %s\r\n",command);
+    goto err;
+  } else if (max_args >= 0 && smartlist_len(args) > max_args) {
+    connection_printf_to_buf(conn, "512 Too many arguments to %s\r\n",command);
+    goto err;
+  }
+  return args;
+ err:
+  SMARTLIST_FOREACH(args, char *, s, tor_free(s));
+  smartlist_free(args);
+  return NULL;
+}
+
 /** Called when we get an EXTENDCIRCUIT message.  Try to extend the listed
  * circuit, and report success or failure. */
 static int
@@ -1720,16 +1746,9 @@
 
   router_nicknames = smartlist_create();
 
-  args = smartlist_create();
-  smartlist_split_string(args, body, " ",
-                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
-  if (smartlist_len(args)<2) {
-    connection_printf_to_buf(conn,
-                             "512 Missing argument to EXTENDCIRCUIT\r\n");
-    SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
-    smartlist_free(args);
+  args = getargs_helper("EXTENDCIRCUIT", conn, body, 2, -1);
+  if (!args)
     goto done;
-  }
 
   zero_circ = !strcmp("0", (char*)smartlist_get(args,0));
   if (!zero_circ && !(circ = get_circ(smartlist_get(args,0)))) {
@@ -1825,16 +1844,14 @@
   origin_circuit_t *circ = NULL;
   routerinfo_t *ri = NULL;
   uint8_t new_purpose;
-  smartlist_t *args = smartlist_create();
+  smartlist_t *args;
+  const char *command =
+    for_circuits ? "SETCIRCUITPURPOSE" : "SETROUTERPURPOSE";
   (void) len; /* body is nul-terminated, so it's safe to ignore the length. */
-  smartlist_split_string(args, body, " ",
-                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
-  if (smartlist_len(args)<2) {
-    connection_printf_to_buf(conn,
-                             "512 Missing argument to SET%sPURPOSE\r\n",
-                             for_circuits ? "CIRCUIT" : "ROUTER");
+
+  args = getargs_helper(command, conn, body, 2, -1);
+  if (!args)
     goto done;
-  }
 
   if (for_circuits) {
     if (!(circ = get_circ(smartlist_get(args,0)))) {
@@ -1865,8 +1882,10 @@
   connection_write_str_to_buf("250 OK\r\n", conn);
 
 done:
-  SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
-  smartlist_free(args);
+  if (args) {
+    SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
+    smartlist_free(args);
+  }
   return 0;
 }
 
@@ -1882,16 +1901,9 @@
   smartlist_t *args;
   (void) len;
 
-  args = smartlist_create();
-  smartlist_split_string(args, body, " ",
-                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
-  if (smartlist_len(args)<2) {
-    connection_printf_to_buf(conn,
-                             "512 Missing argument to ATTACHSTREAM\r\n");
-    SMARTLIST_FOREACH(args, char *, cp, tor_free(cp));
-    smartlist_free(args);
+  args = getargs_helper("ATTACHSTREAM", conn, body, 2, -1);
+  if (!args)
     return 0;
-  }
 
   zero_circ = !strcmp("0", (char*)smartlist_get(args,1));
 
@@ -1962,6 +1974,7 @@
   smartlist_t *args = smartlist_create();
   tor_assert(cp);
   *cp++ = '\0';
+
   smartlist_split_string(args, body, " ",
                          SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
   if (smartlist_len(args)) {
@@ -2008,13 +2021,11 @@
   smartlist_t *args;
   (void) len;
 
-  args = smartlist_create();
-  smartlist_split_string(args, body, " ",
-                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
-  if (smartlist_len(args) < 2)
-    connection_printf_to_buf(conn,
-                             "512 Missing argument to REDIRECTSTREAM\r\n");
-  else if (!(ap_conn = get_stream(smartlist_get(args, 0)))
+  args = getargs_helper("REDIRECTSTREAM", conn, body, 2, -1);
+  if (!args)
+    return 0;
+
+  if (!(ap_conn = get_stream(smartlist_get(args, 0)))
            || !ap_conn->socks_request) {
     connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n",
                              (char*)smartlist_get(args, 0));
@@ -2058,12 +2069,10 @@
   int ok;
   (void) len;
 
-  args = smartlist_create();
-  smartlist_split_string(args, body, " ",
-                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
-  if (smartlist_len(args)<2)
-    connection_printf_to_buf(conn,
-                             "512 Missing argument to CLOSESTREAM\r\n");
+  args = getargs_helper("CLOSESTREAM", conn, body, 2, -1);
+  if (!args)
+    return 0;
+
   else if (!(ap_conn = get_stream(smartlist_get(args, 0))))
     connection_printf_to_buf(conn, "552 Unknown stream \"%s\"\r\n",
                              (char*)smartlist_get(args, 0));
@@ -2097,13 +2106,11 @@
   smartlist_t *args;
   (void) len;
 
-  args = smartlist_create();
-  smartlist_split_string(args, body, " ",
-                         SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
-  if (smartlist_len(args)<1)
-    connection_printf_to_buf(conn,
-                             "512 Missing argument to CLOSECIRCUIT\r\n");
-  else if (!(circ=get_circ(smartlist_get(args, 0))))
+  args = getargs_helper("CLOSECIRCUIT", conn, body, 1, -1);
+  if (!args)
+    return 0;
+
+  if (!(circ=get_circ(smartlist_get(args, 0))))
     connection_printf_to_buf(conn, "552 Unknown circuit \"%s\"\r\n",
                              (char*)smartlist_get(args, 0));
   else {