]> xenbits.xensource.com Git - libvirt.git/commitdiff
Include the iptables command and chain name in the saved rules files
authorMark McLoughlin <markmc@redhat.com>
Thu, 10 Jan 2008 13:51:55 +0000 (13:51 +0000)
committerMark McLoughlin <markmc@redhat.com>
Thu, 10 Jan 2008 13:51:55 +0000 (13:51 +0000)
ChangeLog
src/iptables.c

index 6b9294fefb77fcd40f0446f5c5afabab51678fe4..3add06a58efe7ca90e0e4c5fa00b4c39bb269822 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Thu Jan 10 13:51:00 GMT 2008 Mark McLoughlin <markmc@redhat.com>
+
+       * src/iptables.c: Include the iptables command and chain
+       name in the saved rules files
+
 Thu Jan 10 13:50:11 GMT 2008 Mark McLoughlin <markmc@redhat.com>
 
        * src/iptables.c: Re-name the "flipflop" variable to "command_idx"
index c2877bef5289549673b60aad5896eefe9e9f1ed4..e66bc6495a2a61c95d63c989f831f0c9ccd47971 100644 (file)
@@ -335,37 +335,55 @@ iptablesAddRemoveChain(iptRules *rules, int action)
     return retval;
 }
 
+static char *
+argvToString(char **argv)
+{
+    int len, i;
+    char *ret, *p;
+
+    for (len = 1, i = 0; argv[i]; i++)
+        len += strlen(argv[i]) + 1;
+
+    if (!(p = ret = (char *)malloc(len)))
+        return NULL;
+
+    for (i = 0; argv[i]; i++) {
+        if (i != 0)
+            *(p++) = ' ';
+
+        strcpy(p, argv[i]);
+        p += strlen(argv[i]);
+    }
+
+    *p = '\0';
+
+    return ret;
+}
+
 static int
 iptablesAddRemoveRule(iptRules *rules, int action, const char *arg, ...)
 {
     va_list args;
     int retval = ENOMEM;
     char **argv;
-    char *rule = NULL, *p;
+    char *rule = NULL;
     const char *s;
-    int n, rulelen, command_idx;
+    int n, command_idx;
 
     n = 1 + /* /sbin/iptables  */
         2 + /*   --table foo   */
         2 + /*   --insert bar  */
         1;  /*   arg           */
 
-    rulelen = strlen(arg) + 1;
-
     va_start(args, arg);
-    while ((s = va_arg(args, const char *))) {
+    while ((s = va_arg(args, const char *)))
         n++;
-        rulelen += strlen(s) + 1;
-    }
 
     va_end(args);
 
     if (!(argv = calloc(n + 1, sizeof(*argv))))
         goto error;
 
-    if (!(rule = (char *)malloc(rulelen)))
-        goto error;
-
     n = 0;
 
     if (!(argv[n++] = strdup(IPTABLES_PATH)))
@@ -379,7 +397,7 @@ iptablesAddRemoveRule(iptRules *rules, int action, const char *arg, ...)
 
     command_idx = n;
 
-    if (!(argv[n++] = strdup(action == ADD ? "--insert" : "--delete")))
+    if (!(argv[n++] = strdup("--insert")))
         goto error;
 
     if (!(argv[n++] = strdup(rules->chain)))
@@ -388,23 +406,22 @@ iptablesAddRemoveRule(iptRules *rules, int action, const char *arg, ...)
     if (!(argv[n++] = strdup(arg)))
         goto error;
 
-    p = strcpy(rule, arg);
-    p += strlen(arg);
-
     va_start(args, arg);
 
-    while ((s = va_arg(args, const char *))) {
+    while ((s = va_arg(args, const char *)))
         if (!(argv[n++] = strdup(s)))
             goto error;
 
-        *(p++) = ' ';
-        strcpy(p, s);
-        p += strlen(s);
-    }
-
     va_end(args);
 
-    *p = '\0';
+    if (!(rule = argvToString(&argv[command_idx])))
+        goto error;
+
+    if (action == REMOVE) {
+        free(argv[command_idx]);
+        if (!(argv[command_idx] = strdup("--delete")))
+            goto error;
+    }
 
     if (action == ADD &&
         (retval = iptablesAddRemoveChain(rules, action)))