blob: 19d6f5045bb53454c9205d4ff60a89eec58f6fe0 [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/*
3 * Copyright (c) 2014-2017, 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,
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/>.
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
Yanbiao Liafb20592017-08-03 16:20:46 -070028#include <ndn-cxx/net/address-converter.hpp>
Davide Pesavento89567d32016-11-19 16:39:45 +010029#include <boost/utility/value_init.hpp>
Davide Pesaventoac767aa2016-11-19 18:17:35 +010030#include <algorithm>
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 Pesavento89567d32016-11-19 16:39:45 +010069 boost::system::error_code invalidIp;
Davide Pesaventoac767aa2016-11-19 18:17:35 +010070 boost::asio::ip::address_v4::from_string(cidr.substr(0, pos), invalidIp);
Davide Pesavento89567d32016-11-19 16:39:45 +010071 if (invalidIp) {
72 return false;
73 }
susmit91e1d7c2016-10-03 16:16:57 -060074
Davide Pesaventoac767aa2016-11-19 18:17:35 +010075 auto prefixLenStr = cidr.substr(pos + 1);
76 if (!std::all_of(prefixLenStr.begin(), prefixLenStr.end(), ::isdigit)) {
Davide Pesavento89567d32016-11-19 16:39:45 +010077 return false;
78 }
Davide Pesaventoac767aa2016-11-19 18:17:35 +010079 int prefixLen = -1;
80 try {
81 prefixLen = boost::lexical_cast<int>(prefixLenStr);
82 }
83 catch (const boost::bad_lexical_cast&) {
84 return false;
85 }
Davide Pesavento89567d32016-11-19 16:39:45 +010086 if (prefixLen < 0 || prefixLen > 32) {
87 return false;
88 }
89
90 return true;
susmit91e1d7c2016-10-03 16:16:57 -060091}
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070092
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080093std::ostream&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070094operator<<(std::ostream& os, const Network& network)
95{
Davide Pesavento89567d32016-11-19 16:39:45 +010096 return os << network.m_minAddress << " <-> " << network.m_maxAddress;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070097}
98
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080099std::istream&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700100operator>>(std::istream& is, Network& network)
101{
Davide Pesavento89567d32016-11-19 16:39:45 +0100102 namespace ip = boost::asio::ip;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700103
104 std::string networkStr;
105 is >> networkStr;
106
107 size_t position = networkStr.find('/');
Davide Pesavento89567d32016-11-19 16:39:45 +0100108 if (position == std::string::npos) {
Yanbiao Liafb20592017-08-03 16:20:46 -0700109 network.m_minAddress = ndn::ip::addressFromString(networkStr);
110 network.m_maxAddress = ndn::ip::addressFromString(networkStr);
Davide Pesavento89567d32016-11-19 16:39:45 +0100111 }
112 else {
Yanbiao Liafb20592017-08-03 16:20:46 -0700113 ip::address address = ndn::ip::addressFromString(networkStr.substr(0, position));
Davide Pesavento89567d32016-11-19 16:39:45 +0100114 size_t mask = boost::lexical_cast<size_t>(networkStr.substr(position+1));
115
116 if (address.is_v4()) {
117 ip::address_v4::bytes_type maskBytes = boost::initialized_value;
118 for (size_t i = 0; i < mask; i++) {
119 size_t byteId = i / 8;
120 size_t bitIndex = 7 - i % 8;
121 maskBytes[byteId] |= (1 << bitIndex);
122 }
123
124 ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes();
125 ip::address_v4::bytes_type min;
126 ip::address_v4::bytes_type max;
127
128 for (size_t i = 0; i < addressBytes.size(); i++) {
129 min[i] = addressBytes[i] & maskBytes[i];
130 max[i] = addressBytes[i] | ~(maskBytes[i]);
131 }
132
133 network.m_minAddress = ip::address_v4(min);
134 network.m_maxAddress = ip::address_v4(max);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700135 }
Davide Pesavento89567d32016-11-19 16:39:45 +0100136 else {
137 ip::address_v6::bytes_type maskBytes = boost::initialized_value;
138 for (size_t i = 0; i < mask; i++) {
139 size_t byteId = i / 8;
140 size_t bitIndex = 7 - i % 8;
141 maskBytes[byteId] |= (1 << bitIndex);
142 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700143
Davide Pesavento89567d32016-11-19 16:39:45 +0100144 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;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700147
Davide Pesavento89567d32016-11-19 16:39:45 +0100148 for (size_t i = 0; i < addressBytes.size(); i++) {
149 min[i] = addressBytes[i] & maskBytes[i];
150 max[i] = addressBytes[i] | ~(maskBytes[i]);
151 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700152
Davide Pesavento89567d32016-11-19 16:39:45 +0100153 network.m_minAddress = ip::address_v6(min);
154 network.m_maxAddress = ip::address_v6(max);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700155 }
Davide Pesavento89567d32016-11-19 16:39:45 +0100156 }
157
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700158 return is;
159}
160
Alexander Afanasyev689f0e92014-11-09 12:09:00 -0800161} // namespace nfd