tools: Added helper function to remove table entry in iptables-test
[framework/connectivity/connman.git] / tools / iptables-test.c
index bfe4803..5e7c866 100644 (file)
@@ -236,37 +236,15 @@ static GList *find_chain_tail(struct connman_iptables *table,
                                char *chain_name)
 {
        GList *chain_head, *list;
-       struct connman_iptables_entry *head, *tail;
-       struct ipt_entry *entry;
-       struct xt_entry_target *target;
-       int builtin;
+       struct connman_iptables_entry *tail;
 
-       /* First we look for the head */
-       for (list = table->entries; list; list = list->next) {
-               head = list->data;
-               entry = head->entry;
-
-               /* Buit-in chain */
-               builtin = head->builtin;
-               if (builtin >= 0 && !strcmp(hooknames[builtin], chain_name))
-                       break;
-
-               /* User defined chain */
-               target = ipt_get_target(entry);
-               if (!strcmp(target->u.user.name, IPT_ERROR_TARGET) &&
-                   !strcmp((char *)target->data, chain_name))
-                       break;
-       }
-
-       if (list == NULL)
+       chain_head = find_chain_head(table, chain_name);
+       if (chain_head == NULL)
                return NULL;
 
-       chain_head = list;
-
        /* Then we look for the next chain */
        for (list = chain_head->next; list; list = list->next) {
                tail = list->data;
-               entry = tail->entry;
 
                if (is_chain(table, tail))
                        return list;
@@ -349,6 +327,22 @@ static int connman_add_entry(struct connman_iptables *table,
        return 0;
 }
 
+static int remove_table_entry(struct connman_iptables *table,
+                                       struct connman_iptables_entry *entry)
+{
+       int removed = 0;
+
+       table->num_entries--;
+       table->size -= entry->entry->next_offset;
+       removed = entry->entry->next_offset;
+
+       g_free(entry->entry);
+
+       table->entries = g_list_remove(table->entries, entry);
+
+       return removed;
+}
+
 static int connman_iptables_flush_chain(struct connman_iptables *table,
                                                char *name)
 {
@@ -379,11 +373,7 @@ static int connman_iptables_flush_chain(struct connman_iptables *table,
                entry = list->data;
                next = g_list_next(list);
 
-               table->num_entries--;
-               table->size -= entry->entry->next_offset;
-               removed += entry->entry->next_offset;
-
-               table->entries = g_list_remove(table->entries, list->data);
+               removed += remove_table_entry(table, entry);
 
                list = next;
        }