28 #include <ndn-cxx/net/address-converter.hpp> 29 #include <boost/utility/value_init.hpp> 38 const boost::asio::ip::address& maxAddress)
39 : m_minAddress(minAddress)
40 , m_maxAddress(maxAddress)
47 using boost::asio::ip::address_v4;
48 static Network range{address_v4{}, address_v4{0xffffffff}};
55 using boost::asio::ip::address_v6;
56 static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
58 static Network range{address_v6{}, address_v6{maxV6}};
65 auto pos = cidr.find(
'/');
66 if (pos == std::string::npos) {
71 boost::lexical_cast<
Network>(cidr);
74 catch (
const boost::bad_lexical_cast&) {
82 return os << network.m_minAddress <<
" <-> " << network.m_maxAddress;
88 namespace ip = boost::asio::ip;
90 std::string networkStr;
93 size_t position = networkStr.find(
'/');
94 if (position == std::string::npos) {
99 catch (
const boost::system::system_error&) {
100 is.setstate(std::ios::failbit);
105 boost::system::error_code ec;
108 is.setstate(std::ios::failbit);
112 auto prefixLenStr = networkStr.substr(position + 1);
113 if (!std::all_of(prefixLenStr.begin(), prefixLenStr.end(),
114 [] (
unsigned char c) {
return std::isdigit(c); })) {
115 is.setstate(std::ios::failbit);
120 mask = boost::lexical_cast<
size_t>(prefixLenStr);
122 catch (
const boost::bad_lexical_cast&) {
123 is.setstate(std::ios::failbit);
127 if (address.is_v4()) {
129 is.setstate(std::ios::failbit);
133 ip::address_v4::bytes_type maskBytes = boost::initialized_value;
134 for (
size_t i = 0; i < mask; i++) {
135 size_t byteId = i / 8;
136 size_t bitIndex = 7 - i % 8;
137 maskBytes[byteId] |= (1 << bitIndex);
140 ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes();
141 ip::address_v4::bytes_type min;
142 ip::address_v4::bytes_type max;
144 for (
size_t i = 0; i < addressBytes.size(); i++) {
145 min[i] = addressBytes[i] & maskBytes[i];
146 max[i] = addressBytes[i] | ~(maskBytes[i]);
149 network.m_minAddress = ip::address_v4(min);
150 network.m_maxAddress = ip::address_v4(max);
154 is.setstate(std::ios::failbit);
158 ip::address_v6::bytes_type maskBytes = boost::initialized_value;
159 for (
size_t i = 0; i < mask; i++) {
160 size_t byteId = i / 8;
161 size_t bitIndex = 7 - i % 8;
162 maskBytes[byteId] |= (1 << bitIndex);
165 ip::address_v6::bytes_type addressBytes = address.to_v6().to_bytes();
166 ip::address_v6::bytes_type min;
167 ip::address_v6::bytes_type max;
169 for (
size_t i = 0; i < addressBytes.size(); i++) {
170 min[i] = addressBytes[i] & maskBytes[i];
171 max[i] = addressBytes[i] | ~(maskBytes[i]);
174 network.m_minAddress = ip::address_v6(min);
175 network.m_maxAddress = ip::address_v6(max);
static const Network & getMaxRangeV4()
std::ostream & operator<<(std::ostream &os, const Network &network)
Copyright (c) 2011-2015 Regents of the University of California.
static const Network & getMaxRangeV6()
static bool isValidCidr(const std::string &cidr)
std::istream & operator>>(std::istream &is, Network &network)
boost::asio::ip::address addressFromString(const std::string &address, boost::system::error_code &ec)
parse and convert the input string into an IP address