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 | /* |
Davide Pesavento | 191a7a2 | 2023-05-17 22:40:43 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 29 | #include <boost/utility/value_init.hpp> |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 30 | |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 31 | #include <algorithm> |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 32 | #include <cctype> |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 33 | #include <istream> |
| 34 | #include <ostream> |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 35 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 36 | namespace nfd { |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 37 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 38 | Network::Network() = default; |
| 39 | |
| 40 | Network::Network(const boost::asio::ip::address& minAddress, |
| 41 | const boost::asio::ip::address& maxAddress) |
| 42 | : m_minAddress(minAddress) |
| 43 | , m_maxAddress(maxAddress) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 44 | { |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 45 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 46 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 47 | const Network& |
| 48 | Network::getMaxRangeV4() |
| 49 | { |
| 50 | using boost::asio::ip::address_v4; |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 51 | static Network range{address_v4{}, address_v4{0xffffffff}}; |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 52 | return range; |
| 53 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 55 | const Network& |
| 56 | Network::getMaxRangeV6() |
| 57 | { |
| 58 | using boost::asio::ip::address_v6; |
| 59 | static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 60 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 61 | static Network range{address_v6{}, address_v6{maxV6}}; |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 62 | return range; |
| 63 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 64 | |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 65 | bool |
Davide Pesavento | 191a7a2 | 2023-05-17 22:40:43 -0400 | [diff] [blame] | 66 | Network::isValidCidr(std::string_view cidr) noexcept |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 67 | { |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 68 | auto pos = cidr.find('/'); |
| 69 | if (pos == std::string::npos) { |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 70 | return false; |
| 71 | } |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 72 | |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 73 | try { |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 74 | boost::lexical_cast<Network>(cidr); |
| 75 | return true; |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 76 | } |
| 77 | catch (const boost::bad_lexical_cast&) { |
| 78 | return false; |
| 79 | } |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 80 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 82 | std::ostream& |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 83 | operator<<(std::ostream& os, const Network& network) |
| 84 | { |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 85 | return os << network.m_minAddress << " <-> " << network.m_maxAddress; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 88 | std::istream& |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 89 | operator>>(std::istream& is, Network& network) |
| 90 | { |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 91 | namespace ip = boost::asio::ip; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 92 | |
| 93 | std::string networkStr; |
| 94 | is >> networkStr; |
| 95 | |
| 96 | size_t position = networkStr.find('/'); |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 97 | if (position == std::string::npos) { |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 98 | try { |
Davide Pesavento | 9c33b90 | 2018-05-20 01:30:29 -0400 | [diff] [blame] | 99 | network.m_minAddress = ip::address::from_string(networkStr); |
| 100 | network.m_maxAddress = ip::address::from_string(networkStr); |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 101 | } |
| 102 | catch (const boost::system::system_error&) { |
| 103 | is.setstate(std::ios::failbit); |
| 104 | return is; |
| 105 | } |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 106 | } |
| 107 | else { |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 108 | boost::system::error_code ec; |
Davide Pesavento | 9c33b90 | 2018-05-20 01:30:29 -0400 | [diff] [blame] | 109 | auto address = ip::address::from_string(networkStr.substr(0, position), ec); |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 110 | if (ec) { |
| 111 | is.setstate(std::ios::failbit); |
| 112 | return is; |
| 113 | } |
| 114 | |
| 115 | auto prefixLenStr = networkStr.substr(position + 1); |
| 116 | if (!std::all_of(prefixLenStr.begin(), prefixLenStr.end(), |
| 117 | [] (unsigned char c) { return std::isdigit(c); })) { |
| 118 | is.setstate(std::ios::failbit); |
| 119 | return is; |
| 120 | } |
| 121 | size_t mask; |
| 122 | try { |
| 123 | mask = boost::lexical_cast<size_t>(prefixLenStr); |
| 124 | } |
| 125 | catch (const boost::bad_lexical_cast&) { |
| 126 | is.setstate(std::ios::failbit); |
| 127 | return is; |
| 128 | } |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 129 | |
| 130 | if (address.is_v4()) { |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 131 | if (mask > 32) { |
| 132 | is.setstate(std::ios::failbit); |
| 133 | return is; |
| 134 | } |
| 135 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 136 | ip::address_v4::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); |
| 141 | } |
| 142 | |
| 143 | ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes(); |
| 144 | ip::address_v4::bytes_type min; |
| 145 | ip::address_v4::bytes_type max; |
| 146 | |
| 147 | for (size_t i = 0; i < addressBytes.size(); i++) { |
| 148 | min[i] = addressBytes[i] & maskBytes[i]; |
| 149 | max[i] = addressBytes[i] | ~(maskBytes[i]); |
| 150 | } |
| 151 | |
| 152 | network.m_minAddress = ip::address_v4(min); |
| 153 | network.m_maxAddress = ip::address_v4(max); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 154 | } |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 155 | else { |
Alexander Afanasyev | 3bdc1de | 2018-04-03 17:33:31 -0400 | [diff] [blame] | 156 | if (mask > 128) { |
| 157 | is.setstate(std::ios::failbit); |
| 158 | return is; |
| 159 | } |
| 160 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 161 | ip::address_v6::bytes_type maskBytes = boost::initialized_value; |
| 162 | for (size_t i = 0; i < mask; i++) { |
| 163 | size_t byteId = i / 8; |
| 164 | size_t bitIndex = 7 - i % 8; |
| 165 | maskBytes[byteId] |= (1 << bitIndex); |
| 166 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 167 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 168 | ip::address_v6::bytes_type addressBytes = address.to_v6().to_bytes(); |
| 169 | ip::address_v6::bytes_type min; |
| 170 | ip::address_v6::bytes_type max; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 171 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 172 | for (size_t i = 0; i < addressBytes.size(); i++) { |
| 173 | min[i] = addressBytes[i] & maskBytes[i]; |
| 174 | max[i] = addressBytes[i] | ~(maskBytes[i]); |
| 175 | } |
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 | network.m_minAddress = ip::address_v6(min); |
| 178 | network.m_maxAddress = ip::address_v6(max); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 179 | } |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 182 | return is; |
| 183 | } |
| 184 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 185 | } // namespace nfd |