blob: eafc3888388066f8075e7238092d8bfeb129694f [file] [log] [blame]
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yanbiao Liafb20592017-08-03 16:20:46 -07002/*
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -04003 * Copyright (c) 2014-2018, Regents of the University of California,
susmit91e1d7c2016-10-03 16:16:57 -06004 * 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 Afanasyev3bdc1de2018-04-03 17:33:31 -04009 * 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/>.
Yanbiao Liafb20592017-08-03 16:20:46 -070024 */
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
Davide Pesavento89567d32016-11-19 16:39:45 +010028#include <boost/utility/value_init.hpp>
Davide Pesaventoac767aa2016-11-19 18:17:35 +010029#include <algorithm>
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -040030#include <cctype>
Davide Pesavento89567d32016-11-19 16:39:45 +010031
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080032namespace nfd {
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070033
Davide Pesavento89567d32016-11-19 16:39:45 +010034Network::Network() = default;
35
36Network::Network(const boost::asio::ip::address& minAddress,
37 const boost::asio::ip::address& maxAddress)
38 : m_minAddress(minAddress)
39 , m_maxAddress(maxAddress)
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070040{
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080041}
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070042
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080043const Network&
44Network::getMaxRangeV4()
45{
46 using boost::asio::ip::address_v4;
Davide Pesavento89567d32016-11-19 16:39:45 +010047 static Network range{address_v4{}, address_v4{0xffffffff}};
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080048 return range;
49}
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070050
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080051const Network&
52Network::getMaxRangeV6()
53{
54 using boost::asio::ip::address_v6;
55 static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
Davide Pesavento89567d32016-11-19 16:39:45 +010057 static Network range{address_v6{}, address_v6{maxV6}};
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080058 return range;
59}
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070060
susmit91e1d7c2016-10-03 16:16:57 -060061bool
62Network::isValidCidr(const std::string& cidr)
63{
Davide Pesaventoac767aa2016-11-19 18:17:35 +010064 auto pos = cidr.find('/');
65 if (pos == std::string::npos) {
Davide Pesavento89567d32016-11-19 16:39:45 +010066 return false;
67 }
susmit91e1d7c2016-10-03 16:16:57 -060068
Davide Pesaventoac767aa2016-11-19 18:17:35 +010069 try {
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -040070 boost::lexical_cast<Network>(cidr);
71 return true;
Davide Pesaventoac767aa2016-11-19 18:17:35 +010072 }
73 catch (const boost::bad_lexical_cast&) {
74 return false;
75 }
susmit91e1d7c2016-10-03 16:16:57 -060076}
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070077
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080078std::ostream&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070079operator<<(std::ostream& os, const Network& network)
80{
Davide Pesavento89567d32016-11-19 16:39:45 +010081 return os << network.m_minAddress << " <-> " << network.m_maxAddress;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070082}
83
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080084std::istream&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070085operator>>(std::istream& is, Network& network)
86{
Davide Pesavento89567d32016-11-19 16:39:45 +010087 namespace ip = boost::asio::ip;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070088
89 std::string networkStr;
90 is >> networkStr;
91
92 size_t position = networkStr.find('/');
Davide Pesavento89567d32016-11-19 16:39:45 +010093 if (position == std::string::npos) {
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -040094 try {
Davide Pesavento9c33b902018-05-20 01:30:29 -040095 network.m_minAddress = ip::address::from_string(networkStr);
96 network.m_maxAddress = ip::address::from_string(networkStr);
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -040097 }
98 catch (const boost::system::system_error&) {
99 is.setstate(std::ios::failbit);
100 return is;
101 }
Davide Pesavento89567d32016-11-19 16:39:45 +0100102 }
103 else {
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -0400104 boost::system::error_code ec;
Davide Pesavento9c33b902018-05-20 01:30:29 -0400105 auto address = ip::address::from_string(networkStr.substr(0, position), ec);
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -0400106 if (ec) {
107 is.setstate(std::ios::failbit);
108 return is;
109 }
110
111 auto prefixLenStr = networkStr.substr(position + 1);
112 if (!std::all_of(prefixLenStr.begin(), prefixLenStr.end(),
113 [] (unsigned char c) { return std::isdigit(c); })) {
114 is.setstate(std::ios::failbit);
115 return is;
116 }
117 size_t mask;
118 try {
119 mask = boost::lexical_cast<size_t>(prefixLenStr);
120 }
121 catch (const boost::bad_lexical_cast&) {
122 is.setstate(std::ios::failbit);
123 return is;
124 }
Davide Pesavento89567d32016-11-19 16:39:45 +0100125
126 if (address.is_v4()) {
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -0400127 if (mask > 32) {
128 is.setstate(std::ios::failbit);
129 return is;
130 }
131
Davide Pesavento89567d32016-11-19 16:39:45 +0100132 ip::address_v4::bytes_type maskBytes = boost::initialized_value;
133 for (size_t i = 0; i < mask; i++) {
134 size_t byteId = i / 8;
135 size_t bitIndex = 7 - i % 8;
136 maskBytes[byteId] |= (1 << bitIndex);
137 }
138
139 ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes();
140 ip::address_v4::bytes_type min;
141 ip::address_v4::bytes_type max;
142
143 for (size_t i = 0; i < addressBytes.size(); i++) {
144 min[i] = addressBytes[i] & maskBytes[i];
145 max[i] = addressBytes[i] | ~(maskBytes[i]);
146 }
147
148 network.m_minAddress = ip::address_v4(min);
149 network.m_maxAddress = ip::address_v4(max);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700150 }
Davide Pesavento89567d32016-11-19 16:39:45 +0100151 else {
Alexander Afanasyev3bdc1de2018-04-03 17:33:31 -0400152 if (mask > 128) {
153 is.setstate(std::ios::failbit);
154 return is;
155 }
156
Davide Pesavento89567d32016-11-19 16:39:45 +0100157 ip::address_v6::bytes_type maskBytes = boost::initialized_value;
158 for (size_t i = 0; i < mask; i++) {
159 size_t byteId = i / 8;
160 size_t bitIndex = 7 - i % 8;
161 maskBytes[byteId] |= (1 << bitIndex);
162 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700163
Davide Pesavento89567d32016-11-19 16:39:45 +0100164 ip::address_v6::bytes_type addressBytes = address.to_v6().to_bytes();
165 ip::address_v6::bytes_type min;
166 ip::address_v6::bytes_type max;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700167
Davide Pesavento89567d32016-11-19 16:39:45 +0100168 for (size_t i = 0; i < addressBytes.size(); i++) {
169 min[i] = addressBytes[i] & maskBytes[i];
170 max[i] = addressBytes[i] | ~(maskBytes[i]);
171 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700172
Davide Pesavento89567d32016-11-19 16:39:45 +0100173 network.m_minAddress = ip::address_v6(min);
174 network.m_maxAddress = ip::address_v6(max);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700175 }
Davide Pesavento89567d32016-11-19 16:39:45 +0100176 }
177
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700178 return is;
179}
180
Alexander Afanasyev689f0e92014-11-09 12:09:00 -0800181} // namespace nfd