Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 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, |
| 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/>. |
| 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 | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 28 | #include <boost/utility/value_init.hpp> |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 29 | #include <algorithm> |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 30 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 31 | namespace nfd { |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 32 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 33 | Network::Network() = default; |
| 34 | |
| 35 | Network::Network(const boost::asio::ip::address& minAddress, |
| 36 | const boost::asio::ip::address& maxAddress) |
| 37 | : m_minAddress(minAddress) |
| 38 | , m_maxAddress(maxAddress) |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 39 | { |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 40 | } |
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 | const Network& |
| 43 | Network::getMaxRangeV4() |
| 44 | { |
| 45 | using boost::asio::ip::address_v4; |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 46 | static Network range{address_v4{}, address_v4{0xffffffff}}; |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 47 | return range; |
| 48 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 49 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 50 | const Network& |
| 51 | Network::getMaxRangeV6() |
| 52 | { |
| 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}}; |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 56 | static Network range{address_v6{}, address_v6{maxV6}}; |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 57 | return range; |
| 58 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 59 | |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 60 | bool |
| 61 | Network::isValidCidr(const std::string& cidr) |
| 62 | { |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 63 | auto pos = cidr.find('/'); |
| 64 | if (pos == std::string::npos) { |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 65 | return false; |
| 66 | } |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 67 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 68 | boost::system::error_code invalidIp; |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 69 | boost::asio::ip::address_v4::from_string(cidr.substr(0, pos), invalidIp); |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 70 | if (invalidIp) { |
| 71 | return false; |
| 72 | } |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 73 | |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 74 | auto prefixLenStr = cidr.substr(pos + 1); |
| 75 | if (!std::all_of(prefixLenStr.begin(), prefixLenStr.end(), ::isdigit)) { |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 76 | return false; |
| 77 | } |
Davide Pesavento | ac767aa | 2016-11-19 18:17:35 +0100 | [diff] [blame] | 78 | int prefixLen = -1; |
| 79 | try { |
| 80 | prefixLen = boost::lexical_cast<int>(prefixLenStr); |
| 81 | } |
| 82 | catch (const boost::bad_lexical_cast&) { |
| 83 | return false; |
| 84 | } |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 85 | if (prefixLen < 0 || prefixLen > 32) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | return true; |
susmit | 91e1d7c | 2016-10-03 16:16:57 -0600 | [diff] [blame] | 90 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 92 | std::ostream& |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 93 | operator<<(std::ostream& os, const Network& network) |
| 94 | { |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 95 | return os << network.m_minAddress << " <-> " << network.m_maxAddress; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 98 | std::istream& |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 99 | operator>>(std::istream& is, Network& network) |
| 100 | { |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 101 | namespace ip = boost::asio::ip; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 102 | |
| 103 | std::string networkStr; |
| 104 | is >> networkStr; |
| 105 | |
| 106 | size_t position = networkStr.find('/'); |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 107 | if (position == std::string::npos) { |
| 108 | network.m_minAddress = ip::address::from_string(networkStr); |
| 109 | network.m_maxAddress = ip::address::from_string(networkStr); |
| 110 | } |
| 111 | else { |
| 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)); |
| 114 | |
| 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); |
| 121 | } |
| 122 | |
| 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; |
| 126 | |
| 127 | for (size_t i = 0; i < addressBytes.size(); i++) { |
| 128 | min[i] = addressBytes[i] & maskBytes[i]; |
| 129 | max[i] = addressBytes[i] | ~(maskBytes[i]); |
| 130 | } |
| 131 | |
| 132 | network.m_minAddress = ip::address_v4(min); |
| 133 | network.m_maxAddress = ip::address_v4(max); |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 134 | } |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 135 | else { |
| 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); |
| 141 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 142 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 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; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 146 | |
Davide Pesavento | 89567d3 | 2016-11-19 16:39:45 +0100 | [diff] [blame] | 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 | } |
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 | network.m_minAddress = ip::address_v6(min); |
| 153 | network.m_maxAddress = ip::address_v6(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 | } |
| 156 | |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 157 | return is; |
| 158 | } |
| 159 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 160 | } // namespace nfd |