MINOR: standard: Add function for converting cidr to network mask.

This commit is contained in:
Thierry FOURNIER 2013-12-14 15:39:02 +01:00 committed by Willy Tarreau
parent 0e9af55700
commit b050463375
2 changed files with 17 additions and 0 deletions

View 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

View 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