Move IP address parsing functions to utils.
authorStéphan Kochen <stephan@kochen.nl>
Sun, 17 Jun 2012 15:26:34 +0000 (17:26 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Mon, 18 Jun 2012 21:03:17 +0000 (23:03 +0200)
This is a patch series against the netifd repository.

This first patch moves some previously static functions to utils, so that the
follow up patch may use them from system-linux.c.

Signed-off-by: Stéphan Kochen <stephan@kochen.nl>
proto.c
utils.c
utils.h

diff --git a/proto.c b/proto.c
index ccc5f25..c72a005 100644 (file)
--- a/proto.c
+++ b/proto.c
@@ -70,66 +70,6 @@ static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
        [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
 };
 
        [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
 };
 
-unsigned int
-parse_netmask_string(const char *str, bool v6)
-{
-       struct in_addr addr;
-       unsigned int ret;
-       char *err = NULL;
-
-       if (!strchr(str, '.')) {
-               ret = strtoul(str, &err, 0);
-               if (err && *err)
-                       goto error;
-
-               return ret;
-       }
-
-       if (v6)
-               goto error;
-
-       if (inet_aton(str, &addr) != 1)
-               goto error;
-
-       return 32 - fls(~(ntohl(addr.s_addr)));
-
-error:
-       return ~0;
-}
-
-static bool
-split_netmask(char *str, unsigned int *netmask, bool v6)
-{
-       char *delim = strchr(str, '/');
-
-       if (delim) {
-               *(delim++) = 0;
-
-               *netmask = parse_netmask_string(delim, v6);
-       }
-       return true;
-}
-
-static int
-parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
-{
-       char *astr = alloca(strlen(str) + 1);
-
-       strcpy(astr, str);
-       if (!split_netmask(astr, netmask, af == AF_INET6))
-               return 0;
-
-       if (af == AF_INET6) {
-               if (*netmask > 128)
-                       return 0;
-       } else {
-               if (*netmask > 32)
-                       return 0;
-       }
-
-       return inet_pton(af, astr, addr);
-}
-
 static struct device_addr *
 alloc_device_addr(bool v6, bool ext)
 {
 static struct device_addr *
 alloc_device_addr(bool v6, bool ext)
 {
diff --git a/utils.c b/utils.c
index 65109d7..986cee9 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -15,6 +15,9 @@
 #include <stdlib.h>
 #include "utils.h"
 
 #include <stdlib.h>
 #include "utils.h"
 
+#include <arpa/inet.h>
+#include <netinet/in.h>
+
 void
 __vlist_simple_init(struct vlist_simple_tree *tree, int offset)
 {
 void
 __vlist_simple_init(struct vlist_simple_tree *tree, int offset)
 {
@@ -66,3 +69,63 @@ vlist_simple_flush_all(struct vlist_simple_tree *tree)
        tree->version = -1;
        vlist_simple_flush(tree);
 }
        tree->version = -1;
        vlist_simple_flush(tree);
 }
+
+unsigned int
+parse_netmask_string(const char *str, bool v6)
+{
+       struct in_addr addr;
+       unsigned int ret;
+       char *err = NULL;
+
+       if (!strchr(str, '.')) {
+               ret = strtoul(str, &err, 0);
+               if (err && *err)
+                       goto error;
+
+               return ret;
+       }
+
+       if (v6)
+               goto error;
+
+       if (inet_aton(str, &addr) != 1)
+               goto error;
+
+       return 32 - fls(~(ntohl(addr.s_addr)));
+
+error:
+       return ~0;
+}
+
+bool
+split_netmask(char *str, unsigned int *netmask, bool v6)
+{
+       char *delim = strchr(str, '/');
+
+       if (delim) {
+               *(delim++) = 0;
+
+               *netmask = parse_netmask_string(delim, v6);
+       }
+       return true;
+}
+
+int
+parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask)
+{
+       char *astr = alloca(strlen(str) + 1);
+
+       strcpy(astr, str);
+       if (!split_netmask(astr, netmask, af == AF_INET6))
+               return 0;
+
+       if (af == AF_INET6) {
+               if (*netmask > 128)
+                       return 0;
+       } else {
+               if (*netmask > 32)
+                       return 0;
+       }
+
+       return inet_pton(af, astr, addr);
+}
diff --git a/utils.h b/utils.h
index 319e530..ec22313 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -110,4 +110,8 @@ static inline int fls(int x)
 }
 #endif
 
 }
 #endif
 
+unsigned int parse_netmask_string(const char *str, bool v6);
+bool split_netmask(char *str, unsigned int *netmask, bool v6);
+int parse_ip_and_netmask(int af, const char *str, void *addr, unsigned int *netmask);
+
 #endif
 #endif