blob: b9a6c36fb2962f9c22502c6d54b6c59dc64c3f2f [file] [log] [blame]
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev689f0e92014-11-09 12:09:00 -08003 * 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 Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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 Afanasyev82afa1a2014-03-20 16:56:56 -070025
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080026#include "network.hpp"
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070027
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080028namespace nfd {
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070029
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080030void
31Network::print(std::ostream& os) const
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070032{
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080033 os << m_minAddress << " <-> " << m_maxAddress;
34}
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070035
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080036const Network&
37Network::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 Afanasyev82afa1a2014-03-20 16:56:56 -070043
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080044const Network&
45Network::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 Afanasyev82afa1a2014-03-20 16:56:56 -070053
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080054//////////////////////////////////////////////////////////////////////
55//////////////////////////////////////////////////////////////////////
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070056
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080057std::ostream&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070058operator<<(std::ostream& os, const Network& network)
59{
60 network.print(os);
61 return os;
62}
63
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080064std::istream&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070065operator>>(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 Afanasyevf4e89b42014-05-31 15:54:18 +030085 ip::address_v4::bytes_type maskBytes = boost::initialized_value;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070086 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 Afanasyevf4e89b42014-05-31 15:54:18 +0300108 ip::address_v6::bytes_type maskBytes = boost::initialized_value;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700109 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 Afanasyev689f0e92014-11-09 12:09:00 -0800133} // namespace nfd