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 | 689f0e9 | 2014-11-09 12:09:00 -0800 | [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 | * 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 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 28 | namespace nfd { |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 29 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 30 | void |
| 31 | Network::print(std::ostream& os) const |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 32 | { |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 33 | os << m_minAddress << " <-> " << m_maxAddress; |
| 34 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 36 | const Network& |
| 37 | Network::getMaxRangeV4() |
| 38 | { |
| 39 | using boost::asio::ip::address_v4; |
| 40 | static Network range = Network(address_v4(0), address_v4(0xFFFFFFFF)); |
| 41 | return range; |
| 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::getMaxRangeV6() |
| 46 | { |
| 47 | using boost::asio::ip::address_v6; |
| 48 | static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 49 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; |
| 50 | static Network range = Network(address_v6(), address_v6(maxV6)); |
| 51 | return range; |
| 52 | } |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 53 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 54 | ////////////////////////////////////////////////////////////////////// |
| 55 | ////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 57 | std::ostream& |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 58 | operator<<(std::ostream& os, const Network& network) |
| 59 | { |
| 60 | network.print(os); |
| 61 | return os; |
| 62 | } |
| 63 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 64 | std::istream& |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 65 | operator>>(std::istream& is, Network& network) |
| 66 | { |
| 67 | using namespace boost::asio; |
| 68 | |
| 69 | std::string networkStr; |
| 70 | is >> networkStr; |
| 71 | |
| 72 | size_t position = networkStr.find('/'); |
| 73 | if (position == std::string::npos) |
| 74 | { |
| 75 | network.m_minAddress = ip::address::from_string(networkStr); |
| 76 | network.m_maxAddress = ip::address::from_string(networkStr); |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | ip::address address = ip::address::from_string(networkStr.substr(0, position)); |
| 81 | size_t mask = boost::lexical_cast<size_t>(networkStr.substr(position+1)); |
| 82 | |
| 83 | if (address.is_v4()) |
| 84 | { |
Alexander Afanasyev | f4e89b4 | 2014-05-31 15:54:18 +0300 | [diff] [blame] | 85 | ip::address_v4::bytes_type maskBytes = boost::initialized_value; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 86 | for (size_t i = 0; i < mask; i++) |
| 87 | { |
| 88 | size_t byteId = i / 8; |
| 89 | size_t bitIndex = 7 - i % 8; |
| 90 | maskBytes[byteId] |= (1 << bitIndex); |
| 91 | } |
| 92 | |
| 93 | ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes(); |
| 94 | ip::address_v4::bytes_type min; |
| 95 | ip::address_v4::bytes_type max; |
| 96 | |
| 97 | for (size_t i = 0; i < addressBytes.size(); i++) |
| 98 | { |
| 99 | min[i] = addressBytes[i] & maskBytes[i]; |
| 100 | max[i] = addressBytes[i] | ~(maskBytes[i]); |
| 101 | } |
| 102 | |
| 103 | network.m_minAddress = ip::address_v4(min); |
| 104 | network.m_maxAddress = ip::address_v4(max); |
| 105 | } |
| 106 | else |
| 107 | { |
Alexander Afanasyev | f4e89b4 | 2014-05-31 15:54:18 +0300 | [diff] [blame] | 108 | ip::address_v6::bytes_type maskBytes = boost::initialized_value; |
Alexander Afanasyev | 82afa1a | 2014-03-20 16:56:56 -0700 | [diff] [blame] | 109 | for (size_t i = 0; i < mask; i++) |
| 110 | { |
| 111 | size_t byteId = i / 8; |
| 112 | size_t bitIndex = 7 - i % 8; |
| 113 | maskBytes[byteId] |= (1 << bitIndex); |
| 114 | } |
| 115 | |
| 116 | ip::address_v6::bytes_type addressBytes = address.to_v6().to_bytes(); |
| 117 | ip::address_v6::bytes_type min; |
| 118 | ip::address_v6::bytes_type max; |
| 119 | |
| 120 | for (size_t i = 0; i < addressBytes.size(); i++) |
| 121 | { |
| 122 | min[i] = addressBytes[i] & maskBytes[i]; |
| 123 | max[i] = addressBytes[i] | ~(maskBytes[i]); |
| 124 | } |
| 125 | |
| 126 | network.m_minAddress = ip::address_v6(min); |
| 127 | network.m_maxAddress = ip::address_v6(max); |
| 128 | } |
| 129 | } |
| 130 | return is; |
| 131 | } |
| 132 | |
Alexander Afanasyev | 689f0e9 | 2014-11-09 12:09:00 -0800 | [diff] [blame] | 133 | } // namespace nfd |