28 #include <ndn-cxx/net/address-converter.hpp> 29 #include <boost/utility/value_init.hpp> 37 const boost::asio::ip::address& maxAddress)
38 : m_minAddress(minAddress)
39 , m_maxAddress(maxAddress)
46 using boost::asio::ip::address_v4;
47 static Network range{address_v4{}, address_v4{0xffffffff}};
54 using boost::asio::ip::address_v6;
55 static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
57 static Network range{address_v6{}, address_v6{maxV6}};
64 auto pos = cidr.find(
'/');
65 if (pos == std::string::npos) {
69 boost::system::error_code invalidIp;
70 boost::asio::ip::address_v4::from_string(cidr.substr(0, pos), invalidIp);
75 auto prefixLenStr = cidr.substr(pos + 1);
76 if (!std::all_of(prefixLenStr.begin(), prefixLenStr.end(), ::isdigit)) {
81 prefixLen = boost::lexical_cast<
int>(prefixLenStr);
83 catch (
const boost::bad_lexical_cast&) {
86 if (prefixLen < 0 || prefixLen > 32) {
96 return os << network.m_minAddress <<
" <-> " << network.m_maxAddress;
102 namespace ip = boost::asio::ip;
104 std::string networkStr;
107 size_t position = networkStr.find(
'/');
108 if (position == std::string::npos) {
109 network.m_minAddress = ndn::ip::addressFromString(networkStr);
110 network.m_maxAddress = ndn::ip::addressFromString(networkStr);
113 ip::address address = ndn::ip::addressFromString(networkStr.substr(0, position));
114 size_t mask = boost::lexical_cast<
size_t>(networkStr.substr(position+1));
116 if (address.is_v4()) {
117 ip::address_v4::bytes_type maskBytes = boost::initialized_value;
118 for (
size_t i = 0; i < mask; i++) {
119 size_t byteId = i / 8;
120 size_t bitIndex = 7 - i % 8;
121 maskBytes[byteId] |= (1 << bitIndex);
124 ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes();
125 ip::address_v4::bytes_type min;
126 ip::address_v4::bytes_type max;
128 for (
size_t i = 0; i < addressBytes.size(); i++) {
129 min[i] = addressBytes[i] & maskBytes[i];
130 max[i] = addressBytes[i] | ~(maskBytes[i]);
133 network.m_minAddress = ip::address_v4(min);
134 network.m_maxAddress = ip::address_v4(max);
137 ip::address_v6::bytes_type maskBytes = boost::initialized_value;
138 for (
size_t i = 0; i < mask; i++) {
139 size_t byteId = i / 8;
140 size_t bitIndex = 7 - i % 8;
141 maskBytes[byteId] |= (1 << bitIndex);
144 ip::address_v6::bytes_type addressBytes = address.to_v6().to_bytes();
145 ip::address_v6::bytes_type min;
146 ip::address_v6::bytes_type max;
148 for (
size_t i = 0; i < addressBytes.size(); i++) {
149 min[i] = addressBytes[i] & maskBytes[i];
150 max[i] = addressBytes[i] | ~(maskBytes[i]);
153 network.m_minAddress = ip::address_v6(min);
154 network.m_maxAddress = ip::address_v6(max);
friend std::ostream & operator<<(std::ostream &os, const Network &network)
static bool isValidCidr(const std::string &cidr)
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
static const Network & getMaxRangeV6()
friend std::istream & operator>>(std::istream &is, Network &network)
static const Network & getMaxRangeV4()