Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yanbiao Li | afb2059 | 2017-08-03 16:20:46 -0700 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame^] | 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Yanbiao Li | afb2059 | 2017-08-03 16:20:46 -0700 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 25 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 26 | #include "network.hpp" |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 27 | |
Yanbiao Li | afb2059 | 2017-08-03 16:20:46 -0700 | [diff] [blame] | 28 | #include <ndn-cxx/net/address-converter.hpp> |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 29 | #include <boost/utility/value_init.hpp> |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 30 | #include <algorithm> |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame^] | 31 | #include <cctype> |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 32 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 33 | namespace nfd { |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 34 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 35 | Network::Network() = default; |
| 36 | |
| 37 | Network::Network(const boost::asio::ip::address& minAddress, |
| 38 | const boost::asio::ip::address& maxAddress) |
| 39 | : m_minAddress(minAddress) |
| 40 | , m_maxAddress(maxAddress) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 41 | { |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 42 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 43 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 44 | const Network& |
| 45 | Network::getMaxRangeV4() |
| 46 | { |
| 47 | using boost::asio::ip::address_v4; |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 48 | static Network range{address_v4{}, address_v4{0xffffffff}}; |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 49 | return range; |
| 50 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 52 | const Network& |
| 53 | Network::getMaxRangeV6() |
| 54 | { |
| 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}}; |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 58 | static Network range{address_v6{}, address_v6{maxV6}}; |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 59 | return range; |
| 60 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 61 | |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 62 | bool |
| 63 | Network::isValidCidr(const std::string& cidr) |
| 64 | { |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 65 | auto pos = cidr.find('/'); |
| 66 | if (pos == std::string::npos) { |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 67 | return false; |
| 68 | } |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 69 | |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 70 | try { |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame^] | 71 | boost::lexical_cast<Network>(cidr); |
| 72 | return true; |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 73 | } |
| 74 | catch (const boost::bad_lexical_cast&) { |
| 75 | return false; |
| 76 | } |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 77 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 79 | std::ostream& |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 80 | operator<<(std::ostream& os, const Network& network) |
| 81 | { |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 82 | return os << network.m_minAddress << " <-> " << network.m_maxAddress; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 85 | std::istream& |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 86 | operator>>(std::istream& is, Network& network) |
| 87 | { |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 88 | namespace ip = boost::asio::ip; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 89 | |
| 90 | std::string networkStr; |
| 91 | is >> networkStr; |
| 92 | |
| 93 | size_t position = networkStr.find('/'); |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 94 | if (position == std::string::npos) { |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame^] | 95 | try { |
| 96 | network.m_minAddress = ndn::ip::addressFromString(networkStr); |
| 97 | network.m_maxAddress = ndn::ip::addressFromString(networkStr); |
| 98 | } |
| 99 | catch (const boost::system::system_error&) { |
| 100 | is.setstate(std::ios::failbit); |
| 101 | return is; |
| 102 | } |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 103 | } |
| 104 | else { |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame^] | 105 | boost::system::error_code ec; |
| 106 | ip::address address = ndn::ip::addressFromString(networkStr.substr(0, position), ec); |
| 107 | if (ec) { |
| 108 | is.setstate(std::ios::failbit); |
| 109 | return is; |
| 110 | } |
| 111 | |
| 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); |
| 116 | return is; |
| 117 | } |
| 118 | size_t mask; |
| 119 | try { |
| 120 | mask = boost::lexical_cast<size_t>(prefixLenStr); |
| 121 | } |
| 122 | catch (const boost::bad_lexical_cast&) { |
| 123 | is.setstate(std::ios::failbit); |
| 124 | return is; |
| 125 | } |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 126 | |
| 127 | if (address.is_v4()) { |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame^] | 128 | if (mask > 32) { |
| 129 | is.setstate(std::ios::failbit); |
| 130 | return is; |
| 131 | } |
| 132 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 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); |
| 138 | } |
| 139 | |
| 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; |
| 143 | |
| 144 | for (size_t i = 0; i < addressBytes.size(); i++) { |
| 145 | min[i] = addressBytes[i] & maskBytes[i]; |
| 146 | max[i] = addressBytes[i] | ~(maskBytes[i]); |
| 147 | } |
| 148 | |
| 149 | network.m_minAddress = ip::address_v4(min); |
| 150 | network.m_maxAddress = ip::address_v4(max); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 151 | } |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 152 | else { |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame^] | 153 | if (mask > 128) { |
| 154 | is.setstate(std::ios::failbit); |
| 155 | return is; |
| 156 | } |
| 157 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 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); |
| 163 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 164 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 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; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 168 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 169 | for (size_t i = 0; i < addressBytes.size(); i++) { |
| 170 | min[i] = addressBytes[i] & maskBytes[i]; |
| 171 | max[i] = addressBytes[i] | ~(maskBytes[i]); |
| 172 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 173 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 174 | network.m_minAddress = ip::address_v6(min); |
| 175 | network.m_maxAddress = ip::address_v6(max); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 176 | } |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 179 | return is; |
| 180 | } |
| 181 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 182 | } // namespace nfd |