Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014 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 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 24 | |
| 25 | #ifndef NFD_TOOLS_NETWORK_HPP |
| 26 | #define NFD_TOOLS_NETWORK_HPP |
| 27 | |
| 28 | #include <boost/asio.hpp> |
| 29 | |
| 30 | class Network |
| 31 | { |
| 32 | public: |
| 33 | Network() |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | Network(const boost::asio::ip::address& minAddress, |
| 38 | const boost::asio::ip::address& maxAddress) |
| 39 | : m_minAddress(minAddress) |
| 40 | , m_maxAddress(maxAddress) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | void |
| 45 | print(std::ostream& os) const |
| 46 | { |
| 47 | os << m_minAddress << " <-> " << m_maxAddress; |
| 48 | } |
| 49 | |
| 50 | bool |
| 51 | doesContain(const boost::asio::ip::address& address) const |
| 52 | { |
| 53 | return (m_minAddress <= address && address <= m_maxAddress); |
| 54 | } |
| 55 | |
| 56 | static const Network& |
| 57 | getMaxRangeV4() |
| 58 | { |
| 59 | using boost::asio::ip::address_v4; |
| 60 | static Network range = Network(address_v4(0), address_v4(0xFFFFFFFF)); |
| 61 | return range; |
| 62 | } |
| 63 | |
| 64 | static const Network& |
| 65 | getMaxRangeV6() |
| 66 | { |
| 67 | using boost::asio::ip::address_v6; |
| 68 | static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 69 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; |
| 70 | static Network range = Network(address_v6(), address_v6(maxV6)); |
| 71 | return range; |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | boost::asio::ip::address m_minAddress; |
| 76 | boost::asio::ip::address m_maxAddress; |
| 77 | |
| 78 | friend std::istream& |
| 79 | operator>>(std::istream& is, Network& network); |
| 80 | |
| 81 | friend std::ostream& |
| 82 | operator<<(std::ostream& os, const Network& network); |
| 83 | }; |
| 84 | |
| 85 | inline std::ostream& |
| 86 | operator<<(std::ostream& os, const Network& network) |
| 87 | { |
| 88 | network.print(os); |
| 89 | return os; |
| 90 | } |
| 91 | |
| 92 | inline std::istream& |
| 93 | operator>>(std::istream& is, Network& network) |
| 94 | { |
| 95 | using namespace boost::asio; |
| 96 | |
| 97 | std::string networkStr; |
| 98 | is >> networkStr; |
| 99 | |
| 100 | size_t position = networkStr.find('/'); |
| 101 | if (position == std::string::npos) |
| 102 | { |
| 103 | network.m_minAddress = ip::address::from_string(networkStr); |
| 104 | network.m_maxAddress = ip::address::from_string(networkStr); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | ip::address address = ip::address::from_string(networkStr.substr(0, position)); |
| 109 | size_t mask = boost::lexical_cast<size_t>(networkStr.substr(position+1)); |
| 110 | |
| 111 | if (address.is_v4()) |
| 112 | { |
| 113 | ip::address_v4::bytes_type maskBytes = {}; |
| 114 | for (size_t i = 0; i < mask; i++) |
| 115 | { |
| 116 | size_t byteId = i / 8; |
| 117 | size_t bitIndex = 7 - i % 8; |
| 118 | maskBytes[byteId] |= (1 << bitIndex); |
| 119 | } |
| 120 | |
| 121 | ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes(); |
| 122 | ip::address_v4::bytes_type min; |
| 123 | ip::address_v4::bytes_type max; |
| 124 | |
| 125 | for (size_t i = 0; i < addressBytes.size(); i++) |
| 126 | { |
| 127 | min[i] = addressBytes[i] & maskBytes[i]; |
| 128 | max[i] = addressBytes[i] | ~(maskBytes[i]); |
| 129 | } |
| 130 | |
| 131 | network.m_minAddress = ip::address_v4(min); |
| 132 | network.m_maxAddress = ip::address_v4(max); |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | ip::address_v6::bytes_type maskBytes = {}; |
| 137 | for (size_t i = 0; i < mask; i++) |
| 138 | { |
| 139 | size_t byteId = i / 8; |
| 140 | size_t bitIndex = 7 - i % 8; |
| 141 | maskBytes[byteId] |= (1 << bitIndex); |
| 142 | } |
| 143 | |
| 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; |
| 147 | |
| 148 | for (size_t i = 0; i < addressBytes.size(); i++) |
| 149 | { |
| 150 | min[i] = addressBytes[i] & maskBytes[i]; |
| 151 | max[i] = addressBytes[i] | ~(maskBytes[i]); |
| 152 | } |
| 153 | |
| 154 | network.m_minAddress = ip::address_v6(min); |
| 155 | network.m_maxAddress = ip::address_v6(max); |
| 156 | } |
| 157 | } |
| 158 | return is; |
| 159 | } |
| 160 | |
| 161 | #endif // NFD_TOOLS_NETWORK_HPP |