From 471851713af20d84b67b8966471ea758dc8c12b9 Mon Sep 17 00:00:00 2001 From: Tim Duesterhus Date: Thu, 25 Jan 2018 16:24:49 +0100 Subject: [PATCH] MINOR: standard: Add str2mask6 function This new function mirrors the str2mask() function for IPv4 addresses. This commit is in preparation to support ARGT_MSK6. --- include/common/standard.h | 6 ++++++ src/standard.c | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/common/standard.h b/include/common/standard.h index dd89203ae..461044787 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -377,6 +377,12 @@ struct sockaddr_storage *str2sa_range(const char *str, */ int str2mask(const char *str, struct in_addr *mask); +/* converts 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 to struct in_addr . It returns 1 if the conversion * succeeds otherwise non-zero. */ diff --git a/src/standard.c b/src/standard.c index 495bae571..707e2c716 100644 --- a/src/standard.c +++ b/src/standard.c @@ -1028,6 +1028,28 @@ int str2mask(const char *str, struct in_addr *mask) return 1; } +/* converts 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 to struct in_addr . It returns 1 if the conversion * succeeds otherwise zero. */