#include #include #include #include class CidrBlock4 { uint32_t val, begin, end, mask; uint8_t cidr; void _init(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t cidr) { if (cidr > 32) throw std::runtime_error("Invalid CIDR"); this->cidr = cidr; for (uint8_t i = 0, cnt = cidr; i < 32; i++) { this->mask <<= 1; if (cnt) this->mask |= 1, cnt--; } this->val = ((a << 24) | (b << 16) | (c << 8) | d); this->begin = this->val & this->mask; this->end = this->begin | (~(this->mask)); } public: CidrBlock4(char* const &ip) { uint8_t a, b, c, d, cidr; int ret = sscanf(ip, "%hhu.%hhu.%hhu.%hhu/%hhu", &a, &b, &c, &d, &cidr); if (ret != 5) throw std::runtime_error("Failed parsing, invalid parameters"); _init(a, b, c, d, cidr); } CidrBlock4(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint8_t cidr) { _init(a, b, c, d, cidr); } ~CidrBlock4() {} int dump() { #define _toDotSplitDec(x) (x & 0xff000000u) >> 24, (x & 0x00ff0000u) >> 16, (x & 0x0000ff00u) >> 8, (x & 0x000000ffu) return fprintf(stderr, "Block %u.%u.%u.%u/%hhu, from %u.%u.%u.%u to %u.%u.%u.%u, masked by %u.%u.%u.%u\n", _toDotSplitDec(val), cidr, _toDotSplitDec(begin), _toDotSplitDec(end), _toDotSplitDec(mask) ); #undef _toDotSplitDec } }; int main(int argc, char const *argv[]) { uint8_t a, b, c, d, e; while (scanf("%hhu.%hhu.%hhu.%hhu/%hhu", &a, &b, &c, &d, &e) != EOF) { CidrBlock4(a, b, c, d, e).dump(); } return 0; }