iptables-test: Merge rule adding routines
[framework/connectivity/connman.git] / tools / iptables-test.c
index 1c46ad7..a3de513 100644 (file)
@@ -313,20 +313,22 @@ err:
 }
 
 static struct ipt_entry *
-new_builtin_rule(char *target_name, struct xtables_match *xt_m)
+new_rule(char *target_name, struct xtables_target *xt_t,
+               char *match_name, struct xtables_match *xt_m)
 {
        struct ipt_entry *new_entry;
        size_t match_size, target_size;
-       struct xt_entry_match *entry_match;
-       struct xt_standard_target *target;
-
+       int is_builtin = is_builtin_target(target_name);
 
        if (xt_m)
                match_size = xt_m->m->u.match_size;
        else
                match_size = 0;
 
-       target_size = ALIGN(sizeof(struct xt_standard_target));
+       if (xt_t)
+               target_size = ALIGN(xt_t->t->u.target_size);
+       else
+               target_size = 0;
 
        new_entry = g_try_malloc0(sizeof(struct ipt_entry) + target_size +
                                                                match_size);
@@ -336,37 +338,27 @@ new_builtin_rule(char *target_name, struct xtables_match *xt_m)
        new_entry->target_offset = sizeof(struct ipt_entry) + match_size;
        new_entry->next_offset = sizeof(struct ipt_entry) + target_size +
                                                                match_size;
-
        if (xt_m) {
+               struct xt_entry_match *entry_match;
+
                entry_match = (struct xt_entry_match *)new_entry->elems;
                memcpy(entry_match, xt_m->m, match_size);
        }
 
-       target = (struct xt_standard_target *)(new_entry->elems + match_size);
-       strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
-       target->target.u.user.target_size =
-                               ALIGN(sizeof(struct ipt_standard_target));
-       target->verdict = target_to_verdict(target_name);
+       if (xt_t) {
+               struct xt_entry_target *entry_target;
 
-       return new_entry;
-}
-
-static struct ipt_entry *
-new_custom_rule(struct xtables_target *xt_t, struct xtables_match *xt_m)
-{
-       return NULL;
-}
+               if (is_builtin) {
+                       struct xt_standard_target *target;
 
-static struct ipt_entry *
-new_rule(char *target_name, struct xtables_target *xt_t,
-               char *match_name, struct xtables_match *xt_m)
-{
-       struct ipt_entry *new_entry;
+                       target = (struct xt_standard_target *)(xt_t->t);
+                       strcpy(target->target.u.user.name, IPT_STANDARD_TARGET);
+                       target->verdict = target_to_verdict(target_name);
+               }
 
-       if (is_builtin_target(target_name))
-               new_entry = new_builtin_rule(target_name, xt_m);
-       else
-               new_entry = new_custom_rule(xt_t, xt_m);
+               entry_target = ipt_get_target(new_entry);
+               memcpy(entry_target, xt_t->t, target_size);
+       }
 
        return new_entry;
 }
@@ -483,14 +475,17 @@ static void dump_target(struct connman_iptables *table,
                if(xt_t->print != NULL)
                        xt_t->print(NULL, target, 1);
        } else {
-               printf("\ttarget %s\n", target->u.user.name);
-
                xt_t = xtables_find_target(target->u.user.name, XTF_TRY_LOAD);
-               if (xt_t == NULL)
+               if (xt_t == NULL) {
+                       printf("\ttarget %s\n", target->u.user.name);
                        return;
+               }
 
-               if(xt_t->print != NULL)
+               if(xt_t->print != NULL) {
+                       printf("\ttarget ");
                        xt_t->print(NULL, target, 1);
+                       printf("\n");
+               }
        }
 }
 
@@ -499,6 +494,9 @@ static void dump_match(struct connman_iptables *table, struct ipt_entry *entry)
        struct xtables_match *xt_m;
        struct xt_entry_match *match;
 
+       if (entry->elems == (unsigned char *)entry + entry->target_offset)
+               return;
+
        match = (struct xt_entry_match *) entry->elems;
 
        if (!strlen(match->u.user.name))