]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: standard: Add function for converting cidr to network mask.
authorThierry FOURNIER <tfournier@exceliance.fr>
Sat, 14 Dec 2013 14:39:02 +0000 (15:39 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 17 Mar 2014 17:06:07 +0000 (18:06 +0100)
include/common/standard.h
src/standard.c

index cd422cd1462e25394f34146fc799fd570027f7f9..9ed06688f771db2755d4cb620bfe3932fb4b11da 100644 (file)
@@ -237,6 +237,11 @@ struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high, char
  */
 int str2mask(const char *str, struct in_addr *mask);
 
+/* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion
+ * succeeds otherwise non-zero.
+ */
+int cidr2dotted(int cidr, struct in_addr *mask);
+
 /*
  * converts <str> to two struct in_addr* which must be pre-allocated.
  * The format is "addr[/mask]", where "addr" cannot be empty, and mask
index 81df8b194915d3020da71df92e9010b83c63c090..89af08f72a013e3d3d351b4ee6bb694c66a94c23 100644 (file)
@@ -791,6 +791,18 @@ int str2mask(const char *str, struct in_addr *mask)
        return 1;
 }
 
+/* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion
+ * succeeds otherwise zero.
+ */
+int cidr2dotted(int cidr, struct in_addr *mask) {
+
+       if (cidr < 0 || cidr > 32)
+               return 0;
+
+       mask->s_addr = cidr ? htonl(~0UL << (32 - cidr)) : 0;
+       return 1;
+}
+
 /*
  * converts <str> to two struct in_addr* which must be pre-allocated.
  * The format is "addr[/mask]", where "addr" cannot be empty, and mask