Add common fw3_address_to_string() helper function
authorJo-Philipp Wich <jow@openwrt.org>
Tue, 30 Apr 2013 17:56:39 +0000 (19:56 +0200)
committerJo-Philipp Wich <jow@openwrt.org>
Tue, 30 Apr 2013 18:51:42 +0000 (20:51 +0200)
ipsets.c
options.c
options.h
utils.c

index 430cf7485555060e5c149514c42c4f33a0090b10..7278c50cd6a3b519db0e6ba87117af0d94404a86 100644 (file)
--- a/ipsets.c
+++ b/ipsets.c
@@ -272,10 +272,8 @@ static void
 create_ipset(struct fw3_ipset *ipset, struct fw3_state *state)
 {
        bool first = true;
-       char s[INET6_ADDRSTRLEN];
 
        struct fw3_ipset_datatype *type;
-       struct fw3_address *a;
 
        const char *methods[] = {
                "(bug)",
@@ -309,27 +307,7 @@ create_ipset(struct fw3_ipset *ipset, struct fw3_state *state)
 
        if (ipset->iprange.set)
        {
-               a = &ipset->iprange;
-
-               if (!a->range)
-               {
-                       inet_ntop(a->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                 &a->address.v6, s, sizeof(s));
-
-                       fw3_pr(" range %s/%u", s, a->mask);
-               }
-               else
-               {
-                       inet_ntop(a->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                 &a->address.v6, s, sizeof(s));
-
-                       fw3_pr(" range %s", s);
-
-                       inet_ntop(a->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                 &a->address2.v6, s, sizeof(s));
-
-                       fw3_pr("-%s", s);
-               }
+               fw3_pr(" range %s", fw3_address_to_string(&ipset->iprange, false));
        }
        else if (ipset->portrange.set)
        {
index 1f561d344132880ac7600728a054147930e4a9e8..f261e7038d6d1e717fed7040d5ca99d3353e6f6e 100644 (file)
--- a/options.c
+++ b/options.c
@@ -888,58 +888,55 @@ fw3_format_in_out(struct fw3_device *in, struct fw3_device *out)
                fw3_pr(" %s-o %s", out->invert ? "! " : "", out->name);
 }
 
-void
-fw3_format_src_dest(struct fw3_address *src, struct fw3_address *dest)
+const char *
+fw3_address_to_string(struct fw3_address *address, bool allow_invert)
 {
-       char s[INET6_ADDRSTRLEN];
+       char *p, ip[INET6_ADDRSTRLEN];
+       static char buf[INET6_ADDRSTRLEN * 2 + 2];
 
-       if ((src && src->range) || (dest && dest->range))
-               fw3_pr(" -m iprange");
+       p = buf;
 
-       if (src && src->set)
-       {
-               if (src->range)
-               {
-                       inet_ntop(src->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                         &src->address.v4, s, sizeof(s));
+       if (address->invert && allow_invert)
+               p += sprintf(p, "!");
 
-                       fw3_pr(" %s--src-range %s", src->invert ? "! " : "", s);
+       inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
+                 &address->address.v4, ip, sizeof(ip));
 
-                       inet_ntop(src->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                         &src->address2.v4, s, sizeof(s));
+       p += sprintf(p, "%s", ip);
 
-                       fw3_pr("-%s", s);
-               }
-               else
-               {
-                       inet_ntop(src->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                         &src->address.v4, s, sizeof(s));
+       if (address->range)
+       {
+               inet_ntop(address->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
+                         &address->address2.v4, ip, sizeof(ip));
 
-                       fw3_pr(" %s-s %s/%u", src->invert ? "! " : "", s, src->mask);
-               }
+               p += sprintf(p, "-%s", ip);
        }
-
-       if (dest && dest->set)
+       else
        {
-               if (dest->range)
-               {
-                       inet_ntop(dest->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                         &dest->address.v4, s, sizeof(s));
+               p += sprintf(p, "/%u", address->mask);
+       }
 
-                       fw3_pr(" %s--dst-range %s", dest->invert ? "! " : "", s);
+       return buf;
+}
 
-                       inet_ntop(dest->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                         &dest->address2.v4, s, sizeof(s));
+void
+fw3_format_src_dest(struct fw3_address *src, struct fw3_address *dest)
+{
+       if ((src && src->range) || (dest && dest->range))
+               fw3_pr(" -m iprange");
 
-                       fw3_pr("-%s", s);
-               }
-               else
-               {
-                       inet_ntop(dest->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                         &dest->address.v4, s, sizeof(s));
+       if (src && src->set)
+       {
+               fw3_pr(" %s%s %s", src->invert ? "! " : "",
+                                  src->range ? "--src-range" : "-s",
+                                  fw3_address_to_string(src, false));
+       }
 
-                       fw3_pr(" %s-d %s/%u", dest->invert ? "! " : "", s, dest->mask);
-               }
+       if (dest && dest->set)
+       {
+               fw3_pr(" %s%s %s", dest->invert ? "! " : "",
+                                  dest->range ? "--dst-range" : "-d",
+                                  fw3_address_to_string(dest, false));
        }
 }
 
index 734d1858ce205be85706e5ebecd359e8127f5af7..e242f67d9a8ec87ba08fe9f481e0cf4a14a033ab 100644 (file)
--- a/options.h
+++ b/options.h
@@ -488,6 +488,9 @@ bool fw3_parse_mark(void *ptr, const char *val, bool is_list);
 void fw3_parse_options(void *s, const struct fw3_option *opts,
                        struct uci_section *section);
 
+const char * fw3_address_to_string(struct fw3_address *address,
+                                   bool allow_invert);
+
 void fw3_format_in_out(struct fw3_device *in, struct fw3_device *out);
 void fw3_format_src_dest(struct fw3_address *src, struct fw3_address *dest);
 void fw3_format_sport_dport(struct fw3_port *sp, struct fw3_port *dp);
diff --git a/utils.c b/utils.c
index 9f57e9f8e03701a33010a0ffb26790680b45e7a0..3ad50e3ff17553022ebd29a55fca9fd7d77f03b5 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -378,9 +378,7 @@ write_zone_uci(struct uci_context *ctx, struct fw3_zone *z,
        struct fw3_address *sub;
        enum fw3_family fam = FW3_FAMILY_ANY;
 
-       char addr[INET6_ADDRSTRLEN];
-       char buf[INET6_ADDRSTRLEN * 2 + 2];
-       char *p;
+       char *p, buf[34];
 
        struct uci_ptr ptr = { .p = dest };
 
@@ -471,29 +469,7 @@ write_zone_uci(struct uci_context *ctx, struct fw3_zone *z,
                if (!sub)
                        continue;
 
-               p = buf;
-
-               if (sub->invert)
-                       p += sprintf(p, "!");
-
-               inet_ntop(sub->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                 &sub->address.v4, addr, sizeof(addr));
-
-               p += sprintf(p, "%s", addr);
-
-               if (sub->range)
-               {
-                       inet_ntop(sub->family == FW3_FAMILY_V4 ? AF_INET : AF_INET6,
-                                 &sub->address2.v4, addr, sizeof(addr));
-
-                       p += sprintf(p, "-%s", addr);
-               }
-               else
-               {
-                       p += sprintf(p, "/%u", sub->mask);
-               }
-
-               ptr.value = buf;
+               ptr.value = fw3_address_to_string(sub, true);
                uci_add_list(ctx, &ptr);
        }