1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
4 #include "iptraf-ng-compat.h"
9 * Returns a binary subnet mask based on the number of mask bits. The
10 * dotted-decimal notation may be obtained with inet_ntoa.
12 unsigned long cidr_get_mask(unsigned int maskbits
)
19 inet_aton("255.255.255.255", &mask
);
20 mask
.s_addr
= htonl(mask
.s_addr
<< (32 - maskbits
));
26 * Returns a subnet mask in dotted-decimal notation given the number of
29 char *cidr_get_quad_mask(unsigned int maskbits
)
33 addr
.s_addr
= cidr_get_mask(maskbits
);
34 return inet_ntoa(addr
);
38 * Returns the number of 1-bits in the given binary subnet mask in
41 unsigned int cidr_get_maskbits(unsigned long mask
)
49 while (mask
% 2 == 0) {
58 * Parse and cut off mask from CIDR-style address/mask string. In case of
59 * absent or invalid input in the mask, 255 is returned in *maskbits
60 * (255 is invalid for an IPv4 address).
62 void cidr_split_address(char *cidr_addr
, unsigned int *maskbits
)
64 char *slashptr
= strchr(cidr_addr
, '/');
65 if (slashptr
== NULL
) {
71 * Cut out the mask part and move past the slash
76 if (*slashptr
!= '\0') {
80 val
= strtoul(slashptr
, &endptr
, 10);
83 *maskbits
= (unsigned int)val
;