diff --git a/include/common/standard.h b/include/common/standard.h index cd422cd14..9ed06688f 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -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 to struct in_addr . It returns 1 if the conversion + * succeeds otherwise non-zero. + */ +int cidr2dotted(int cidr, struct in_addr *mask); + /* * converts to two struct in_addr* which must be pre-allocated. * The format is "addr[/mask]", where "addr" cannot be empty, and mask diff --git a/src/standard.c b/src/standard.c index 81df8b194..89af08f72 100644 --- a/src/standard.c +++ b/src/standard.c @@ -791,6 +791,18 @@ int str2mask(const char *str, struct in_addr *mask) return 1; } +/* convert to struct in_addr . 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 to two struct in_addr* which must be pre-allocated. * The format is "addr[/mask]", where "addr" cannot be empty, and mask