return 1;
}
- if ((ipconf.mask.s_addr = inet_addr(args->ipmask)) == INADDR_NONE) {
+ ipconf.mask.s_addr = inet_addr(args->ipmask);
+ if (ipconf.mask.s_addr == INADDR_NONE
+ || netmask(bitcount(ipconf.mask.s_addr)) != ipconf.mask.s_addr) {
fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask);
return 1;
}
time_t time_monotonic();
char *lltostr(long long ll, int base);
+uint32_t bitcount(uint32_t n);
+uint32_t netmask(uint32_t count);
#endif
snprintf(buf, sizeof(buf) - 1, (base == 16 ? "%llx" : (base == 8 ? "%llo" : "%lld")), ll);
return buf;
}
+
+uint32_t bitcount(uint32_t n)
+{
+ uint32_t c;
+ for (c = 0; n; ++c) {
+ n &= n - 1;
+ }
+ return c;
+}
+
+uint32_t netmask(uint32_t count)
+{
+ return htonl(count <= 32 ? 0xffffffff << (32 - count) : 0);
+}