]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: standard: add an IPv6 parsing function (str62net)
authorWilly Tarreau <w@1wt.eu>
Fri, 27 Apr 2012 20:49:47 +0000 (22:49 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 8 May 2012 18:57:21 +0000 (20:57 +0200)
str62net returns an address and a netmask in number of bits.

include/common/standard.h
src/standard.c

index 5dcb6c7e9ed60a432898581ef16258fbd3923a3b..83b1fc042a4a493399588829c10da90c17c7106f 100644 (file)
@@ -252,6 +252,14 @@ int str2mask(const char *str, struct in_addr *mask);
  */
 int str2net(const char *str, struct in_addr *addr, struct in_addr *mask);
 
+/*
+ * converts <str> to two struct in6_addr* which must be pre-allocated.
+ * The format is "addr[/mask]", where "addr" cannot be empty, and mask
+ * is an optionnal number of bits (128 being the default).
+ * Returns 1 if OK, 0 if error.
+ */
+int str62net(const char *str, struct in6_addr *addr, unsigned char *mask);
+
 /*
  * Parse IP address found in url.
  */
index ea06a87dadf757bdf8d42bdb1f6b548f7cf66dac..0af7c9595ea6fea49371b9280c76e4db188a617e 100644 (file)
@@ -781,6 +781,47 @@ int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
 }
 
 
+/*
+ * converts <str> to two struct in6_addr* which must be pre-allocated.
+ * The format is "addr[/mask]", where "addr" cannot be empty, and mask
+ * is an optionnal number of bits (128 being the default).
+ * Returns 1 if OK, 0 if error.
+ */
+int str62net(const char *str, struct in6_addr *addr, unsigned char *mask)
+{
+       char *c, *s;
+       int ret_val = 0;
+       char *err;
+       unsigned long len = 128;
+
+       s = strdup(str);
+       if (!s)
+               return 0;
+
+       memset(mask, 0, sizeof(*mask));
+       memset(addr, 0, sizeof(*addr));
+
+       if ((c = strrchr(s, '/')) != NULL) {
+               *c++ = '\0'; /* c points to the mask */
+               if (!*c)
+                       goto out_free;
+
+               len = strtoul(c, &err, 10);
+               if ((err && *err) || (unsigned)len > 128)
+                       goto out_free;
+       }
+       *mask = len; /* OK we have a valid mask in <len> */
+
+       if (!inet_pton(AF_INET6, s, addr))
+               goto out_free;
+
+       ret_val = 1;
+ out_free:
+       free(s);
+       return ret_val;
+}
+
+
 /*
  * Parse IPv4 address found in url.
  */