28 #include <boost/utility/value_init.hpp> 36 const boost::asio::ip::address& maxAddress)
37 : m_minAddress(minAddress)
38 , m_maxAddress(maxAddress)
45 using boost::asio::ip::address_v4;
46 static Network range{address_v4{}, address_v4{0xffffffff}};
53 using boost::asio::ip::address_v6;
54 static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
56 static Network range{address_v6{}, address_v6{maxV6}};
63 auto pos = cidr.find(
'/');
64 if (pos == std::string::npos) {
68 boost::system::error_code invalidIp;
69 boost::asio::ip::address_v4::from_string(cidr.substr(0, pos), invalidIp);
74 auto prefixLenStr = cidr.substr(pos + 1);
75 if (!std::all_of(prefixLenStr.begin(), prefixLenStr.end(), ::isdigit)) {
80 prefixLen = boost::lexical_cast<
int>(prefixLenStr);
82 catch (
const boost::bad_lexical_cast&) {
85 if (prefixLen < 0 || prefixLen > 32) {
95 return os << network.m_minAddress <<
" <-> " << network.m_maxAddress;
103 std::string networkStr;
106 size_t position = networkStr.find(
'/');
107 if (position == std::string::npos) {
108 network.m_minAddress = ip::address::from_string(networkStr);
109 network.m_maxAddress = ip::address::from_string(networkStr);
112 ip::address address = ip::address::from_string(networkStr.substr(0, position));
113 size_t mask = boost::lexical_cast<
size_t>(networkStr.substr(position+1));
115 if (address.is_v4()) {
116 ip::address_v4::bytes_type maskBytes = boost::initialized_value;
117 for (
size_t i = 0; i < mask; i++) {
118 size_t byteId = i / 8;
119 size_t bitIndex = 7 - i % 8;
120 maskBytes[byteId] |= (1 << bitIndex);
123 ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes();
124 ip::address_v4::bytes_type min;
125 ip::address_v4::bytes_type max;
127 for (
size_t i = 0; i < addressBytes.size(); i++) {
128 min[i] = addressBytes[i] & maskBytes[i];
129 max[i] = addressBytes[i] | ~(maskBytes[i]);
132 network.m_minAddress = ip::address_v4(min);
133 network.m_maxAddress = ip::address_v4(max);
136 ip::address_v6::bytes_type maskBytes = boost::initialized_value;
137 for (
size_t i = 0; i < mask; i++) {
138 size_t byteId = i / 8;
139 size_t bitIndex = 7 - i % 8;
140 maskBytes[byteId] |= (1 << bitIndex);
143 ip::address_v6::bytes_type addressBytes = address.to_v6().to_bytes();
144 ip::address_v6::bytes_type min;
145 ip::address_v6::bytes_type max;
147 for (
size_t i = 0; i < addressBytes.size(); i++) {
148 min[i] = addressBytes[i] & maskBytes[i];
149 max[i] = addressBytes[i] | ~(maskBytes[i]);
152 network.m_minAddress = ip::address_v6(min);
153 network.m_maxAddress = ip::address_v6(max);
static const Network & getMaxRangeV4()
Copyright (c) 2011-2015 Regents of the University of California.
static const Network & getMaxRangeV6()
static bool isValidCidr(const std::string &cidr)
friend std::istream & operator>>(std::istream &is, Network &network)
friend std::ostream & operator<<(std::ostream &os, const Network &network)