blob: be7d9c67a09c3e18dc3485084a8a401d17ee55d6 [file] [log] [blame]
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
susmit91e1d7c2016-10-03 16:16:57 -06003 * Copyright (c) 2014-2016, 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
Davide Pesavento89567d32016-11-19 16:39:45 +010028#include <boost/algorithm/string/classification.hpp>
29#include <boost/algorithm/string/split.hpp>
30#include <boost/utility/value_init.hpp>
31
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 Pesavento89567d32016-11-19 16:39:45 +010064 std::vector<std::string> splitCidr;
65 boost::split(splitCidr, cidr, boost::is_any_of("/"));
66 if (splitCidr.size() != 2) {
67 return false;
68 }
susmit91e1d7c2016-10-03 16:16:57 -060069
Davide Pesavento89567d32016-11-19 16:39:45 +010070 boost::system::error_code invalidIp;
71 boost::asio::ip::address_v4::from_string(splitCidr[0], invalidIp);
72 if (invalidIp) {
73 return false;
74 }
susmit91e1d7c2016-10-03 16:16:57 -060075
Davide Pesavento89567d32016-11-19 16:39:45 +010076 auto prefixLenStr = splitCidr[1];
77 if (prefixLenStr.length() <= 0 ||
78 !std::all_of(prefixLenStr.begin(), prefixLenStr.end(), ::isdigit)) {
79 return false;
80 }
81 int prefixLen = boost::lexical_cast<int>(prefixLenStr);
82 if (prefixLen < 0 || prefixLen > 32) {
83 return false;
84 }
85
86 return true;
susmit91e1d7c2016-10-03 16:16:57 -060087}
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070088
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080089std::ostream&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070090operator<<(std::ostream& os, const Network& network)
91{
Davide Pesavento89567d32016-11-19 16:39:45 +010092 return os << network.m_minAddress << " <-> " << network.m_maxAddress;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070093}
94
Alexander Afanasyev689f0e92014-11-09 12:09:00 -080095std::istream&
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070096operator>>(std::istream& is, Network& network)
97{
Davide Pesavento89567d32016-11-19 16:39:45 +010098 namespace ip = boost::asio::ip;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -070099
100 std::string networkStr;
101 is >> networkStr;
102
103 size_t position = networkStr.find('/');
Davide Pesavento89567d32016-11-19 16:39:45 +0100104 if (position == std::string::npos) {
105 network.m_minAddress = ip::address::from_string(networkStr);
106 network.m_maxAddress = ip::address::from_string(networkStr);
107 }
108 else {
109 ip::address address = ip::address::from_string(networkStr.substr(0, position));
110 size_t mask = boost::lexical_cast<size_t>(networkStr.substr(position+1));
111
112 if (address.is_v4()) {
113 ip::address_v4::bytes_type maskBytes = boost::initialized_value;
114 for (size_t i = 0; i < mask; i++) {
115 size_t byteId = i / 8;
116 size_t bitIndex = 7 - i % 8;
117 maskBytes[byteId] |= (1 << bitIndex);
118 }
119
120 ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes();
121 ip::address_v4::bytes_type min;
122 ip::address_v4::bytes_type max;
123
124 for (size_t i = 0; i < addressBytes.size(); i++) {
125 min[i] = addressBytes[i] & maskBytes[i];
126 max[i] = addressBytes[i] | ~(maskBytes[i]);
127 }
128
129 network.m_minAddress = ip::address_v4(min);
130 network.m_maxAddress = ip::address_v4(max);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700131 }
Davide Pesavento89567d32016-11-19 16:39:45 +0100132 else {
133 ip::address_v6::bytes_type maskBytes = boost::initialized_value;
134 for (size_t i = 0; i < mask; i++) {
135 size_t byteId = i / 8;
136 size_t bitIndex = 7 - i % 8;
137 maskBytes[byteId] |= (1 << bitIndex);
138 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700139
Davide Pesavento89567d32016-11-19 16:39:45 +0100140 ip::address_v6::bytes_type addressBytes = address.to_v6().to_bytes();
141 ip::address_v6::bytes_type min;
142 ip::address_v6::bytes_type max;
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700143
Davide Pesavento89567d32016-11-19 16:39:45 +0100144 for (size_t i = 0; i < addressBytes.size(); i++) {
145 min[i] = addressBytes[i] & maskBytes[i];
146 max[i] = addressBytes[i] | ~(maskBytes[i]);
147 }
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700148
Davide Pesavento89567d32016-11-19 16:39:45 +0100149 network.m_minAddress = ip::address_v6(min);
150 network.m_maxAddress = ip::address_v6(max);
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700151 }
Davide Pesavento89567d32016-11-19 16:39:45 +0100152 }
153
Alexander Afanasyev82afa1a2014-03-20 16:56:56 -0700154 return is;
155}
156
Alexander Afanasyev689f0e92014-11-09 12:09:00 -0800157} // namespace nfd