]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: standard: Add str2mask6 function
authorTim Duesterhus <tim@bastelstu.be>
Thu, 25 Jan 2018 15:24:49 +0000 (16:24 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 25 Jan 2018 21:25:40 +0000 (22:25 +0100)
This new function mirrors the str2mask() function for IPv4 addresses.

This commit is in preparation to support ARGT_MSK6.

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

index dd89203ae8e96f0eed7befac6dfe33a02b09f0c6..46104478729de16e699e2f59eddb631f7cb00d68 100644 (file)
@@ -377,6 +377,12 @@ struct sockaddr_storage *str2sa_range(const char *str,
  */
 int str2mask(const char *str, struct in_addr *mask);
 
+/* converts <str> to a struct in6_addr containing a network mask. It can be
+ * passed in quadruplet form (ffff::ffff::) or in CIDR form (64). It returns 1
+ * if the conversion succeeds otherwise zero.
+ */
+int str2mask6(const char *str, struct in6_addr *mask);
+
 /* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion
  * succeeds otherwise non-zero.
  */
index 495bae571c1b7e605e907f677fedcf29d8fe1cd3..707e2c716664eed6021d58a6f2d0c7f465340568 100644 (file)
@@ -1028,6 +1028,28 @@ int str2mask(const char *str, struct in_addr *mask)
        return 1;
 }
 
+/* converts <str> to a struct in6_addr containing a network mask. It can be
+ * passed in quadruplet form (ffff::ffff::) or in CIDR form (64). It returns 1
+ * if the conversion succeeds otherwise zero.
+ */
+int str2mask6(const char *str, struct in6_addr *mask)
+{
+       if (strchr(str, ':') != NULL) {     /* quadruplet notation */
+               if (!inet_pton(AF_INET6, str, mask))
+                       return 0;
+       }
+       else { /* mask length */
+               char *err;
+               unsigned long len = strtol(str, &err, 10);
+
+               if (!*str || (err && *err) || (unsigned)len > 128)
+                       return 0;
+
+               len2mask6(len, mask);
+       }
+       return 1;
+}
+
 /* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion
  * succeeds otherwise zero.
  */