From: Daniel Wagner <daniel.wag...@bmw-carit.de>

Currently, find_chain_tail() returns the element after the chain end.
With returning the real end of chain, the code gets more readable.
---
 src/iptables.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/iptables.c b/src/iptables.c
index e84ba58..6ff4860 100644
--- a/src/iptables.c
+++ b/src/iptables.c
@@ -377,7 +377,7 @@ static GList *find_chain_head(struct connman_iptables 
*table,
 static GList *find_chain_tail(struct connman_iptables *table,
                                const char *chain_name)
 {
-       struct connman_iptables_entry *tail;
+       struct connman_iptables_entry *entry;
        GList *chain_head, *list;
 
        chain_head = find_chain_head(table, chain_name);
@@ -386,10 +386,12 @@ static GList *find_chain_tail(struct connman_iptables 
*table,
 
        /* Then we look for the next chain */
        for (list = chain_head->next; list; list = list->next) {
-               tail = list->data;
+               entry = list->data;
 
-               if (is_chain(table, tail))
-                       return list;
+               if (is_chain(table, entry) == FALSE)
+                       continue;
+
+               return g_list_previous(list);
        }
 
        /* Nothing found, we return the table end */
@@ -540,10 +542,11 @@ static int iptables_flush_chain(struct connman_iptables 
*table,
        else
                list = chain_head->next;
 
-       if (list == chain_tail->prev)
+       /* Is chain already emtpy ?*/
+       if (list == chain_tail)
                return 0;
 
-       while (list != chain_tail->prev) {
+       while (list != chain_tail) {
                entry = list->data;
                next = g_list_next(list);
 
@@ -674,12 +677,13 @@ static int iptables_delete_chain(struct connman_iptables 
*table,
                return -EINVAL;
 
        /* Chain must be flushed */
-       if (chain_head->next != chain_tail->prev)
+       if (chain_head->next != chain_tail)
                return -EINVAL;
 
+       entry = chain_head->data;
        remove_table_entry(table, entry);
 
-       entry = chain_tail->prev->data;
+       entry = chain_tail->data;
        remove_table_entry(table, entry);
 
        update_offsets(table);
@@ -855,7 +859,7 @@ static int iptables_append_rule(struct connman_iptables 
*table,
        if (new_entry == NULL)
                return -EINVAL;
 
-       ret = iptables_add_entry(table, new_entry, chain_tail->prev, builtin);
+       ret = iptables_add_entry(table, new_entry, chain_tail, builtin);
        if (ret < 0)
                g_free(new_entry);
 
@@ -983,7 +987,7 @@ static GList *find_existing_rule(struct connman_iptables 
*table,
        else
                list = chain_head->next;
 
-       for (; list != chain_tail->prev; list = list->next) {
+       for (; list != chain_tail; list = list->next) {
                struct connman_iptables_entry *tmp;
                struct ipt_entry *tmp_e;
 
@@ -1016,7 +1020,7 @@ static GList *find_existing_rule(struct connman_iptables 
*table,
 
        g_free(entry_test);
 
-       if (list != chain_tail->prev)
+       if (list != chain_tail)
                return list;
 
        return NULL;
@@ -1119,7 +1123,7 @@ static int iptables_change_policy(struct connman_iptables 
*table,
        if (chain_tail == NULL)
                return -EINVAL;
 
-       entry = chain_tail->prev->data;
+       entry = chain_tail->data;
        target = ipt_get_target(entry->entry);
 
        t = (struct xt_standard_target *)target;
-- 
1.8.1.3.566.gaa39828

_______________________________________________
connman mailing list
connman@connman.net
http://lists.connman.net/listinfo/connman

Reply via email to