chiark / gitweb /
net-util: add inet address/family parsing
authorTom Gundersen <teg@jklm.no>
Sat, 9 Nov 2013 21:19:42 +0000 (22:19 +0100)
committerTom Gundersen <teg@jklm.no>
Sat, 9 Nov 2013 22:41:17 +0000 (23:41 +0100)
src/shared/net-util.c
src/shared/net-util.h

index f853add1ec159cdf480b1f8e7133a6e33849c40d..2734d119ce89c6fde6d862ba47993f980c7ddb30 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <netinet/ether.h>
 #include <net/if.h>
+#include <arpa/inet.h>
 
 #include "net-util.h"
 #include "log.h"
@@ -163,3 +164,30 @@ int config_parse_hwaddr(const char *unit,
 
         return 0;
 }
+
+int net_parse_inaddr(const char *address, unsigned char *family, void *dst) {
+        int r;
+
+        assert(address);
+        assert(family);
+        assert(dst);
+
+        /* IPv4 */
+        r = inet_pton(AF_INET, address, dst);
+        if (r > 0)
+                *family = AF_INET; /* successfully parsed IPv4 address */
+        else  if (r < 0)
+                return -errno;
+        else {
+                /* not an IPv4 address, so let's try IPv6 */
+                r = inet_pton(AF_INET6, address, dst);
+                if (r > 0)
+                        *family = AF_INET6; /* successfully parsed IPv6 address */
+                else if (r < 0)
+                        return -errno;
+                else
+                        return -EINVAL;
+        }
+
+        return 0;
+}
index 54981725b0cf596b3d16701bcc3fc274891fd19c..eac394bffa24fc4a1ca854d4fc4a02939b28127d 100644 (file)
@@ -42,3 +42,5 @@ int config_parse_hwaddr(const char *unit, const char *filename, unsigned line,
 int config_parse_ifname(const char *unit, const char *filename, unsigned line,
                         const char *section, const char *lvalue, int ltype,
                         const char *rvalue, void *data, void *userdata);
+
+int net_parse_inaddr(const char *address, unsigned char *family, void *dst);