NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
network.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2018, 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.
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  */
25 
26 #include "network.hpp"
27 
28 #include <ndn-cxx/net/address-converter.hpp>
29 #include <boost/utility/value_init.hpp>
30 #include <algorithm>
31 #include <cctype>
32 
33 namespace nfd {
34 
35 Network::Network() = default;
36 
37 Network::Network(const boost::asio::ip::address& minAddress,
38  const boost::asio::ip::address& maxAddress)
39  : m_minAddress(minAddress)
40  , m_maxAddress(maxAddress)
41 {
42 }
43 
44 const Network&
46 {
47  using boost::asio::ip::address_v4;
48  static Network range{address_v4{}, address_v4{0xffffffff}};
49  return range;
50 }
51 
52 const Network&
54 {
55  using boost::asio::ip::address_v6;
56  static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
58  static Network range{address_v6{}, address_v6{maxV6}};
59  return range;
60 }
61 
62 bool
63 Network::isValidCidr(const std::string& cidr)
64 {
65  auto pos = cidr.find('/');
66  if (pos == std::string::npos) {
67  return false;
68  }
69 
70  try {
71  boost::lexical_cast<Network>(cidr);
72  return true;
73  }
74  catch (const boost::bad_lexical_cast&) {
75  return false;
76  }
77 }
78 
79 std::ostream&
80 operator<<(std::ostream& os, const Network& network)
81 {
82  return os << network.m_minAddress << " <-> " << network.m_maxAddress;
83 }
84 
85 std::istream&
86 operator>>(std::istream& is, Network& network)
87 {
88  namespace ip = boost::asio::ip;
89 
90  std::string networkStr;
91  is >> networkStr;
92 
93  size_t position = networkStr.find('/');
94  if (position == std::string::npos) {
95  try {
96  network.m_minAddress = ndn::ip::addressFromString(networkStr);
97  network.m_maxAddress = ndn::ip::addressFromString(networkStr);
98  }
99  catch (const boost::system::system_error&) {
100  is.setstate(std::ios::failbit);
101  return is;
102  }
103  }
104  else {
105  boost::system::error_code ec;
106  ip::address address = ndn::ip::addressFromString(networkStr.substr(0, position), ec);
107  if (ec) {
108  is.setstate(std::ios::failbit);
109  return is;
110  }
111 
112  auto prefixLenStr = networkStr.substr(position + 1);
113  if (!std::all_of(prefixLenStr.begin(), prefixLenStr.end(),
114  [] (unsigned char c) { return std::isdigit(c); })) {
115  is.setstate(std::ios::failbit);
116  return is;
117  }
118  size_t mask;
119  try {
120  mask = boost::lexical_cast<size_t>(prefixLenStr);
121  }
122  catch (const boost::bad_lexical_cast&) {
123  is.setstate(std::ios::failbit);
124  return is;
125  }
126 
127  if (address.is_v4()) {
128  if (mask > 32) {
129  is.setstate(std::ios::failbit);
130  return is;
131  }
132 
133  ip::address_v4::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  }
139 
140  ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes();
141  ip::address_v4::bytes_type min;
142  ip::address_v4::bytes_type max;
143 
144  for (size_t i = 0; i < addressBytes.size(); i++) {
145  min[i] = addressBytes[i] & maskBytes[i];
146  max[i] = addressBytes[i] | ~(maskBytes[i]);
147  }
148 
149  network.m_minAddress = ip::address_v4(min);
150  network.m_maxAddress = ip::address_v4(max);
151  }
152  else {
153  if (mask > 128) {
154  is.setstate(std::ios::failbit);
155  return is;
156  }
157 
158  ip::address_v6::bytes_type maskBytes = boost::initialized_value;
159  for (size_t i = 0; i < mask; i++) {
160  size_t byteId = i / 8;
161  size_t bitIndex = 7 - i % 8;
162  maskBytes[byteId] |= (1 << bitIndex);
163  }
164 
165  ip::address_v6::bytes_type addressBytes = address.to_v6().to_bytes();
166  ip::address_v6::bytes_type min;
167  ip::address_v6::bytes_type max;
168 
169  for (size_t i = 0; i < addressBytes.size(); i++) {
170  min[i] = addressBytes[i] & maskBytes[i];
171  max[i] = addressBytes[i] | ~(maskBytes[i]);
172  }
173 
174  network.m_minAddress = ip::address_v6(min);
175  network.m_maxAddress = ip::address_v6(max);
176  }
177  }
178 
179  return is;
180 }
181 
182 } // namespace nfd
static const Network & getMaxRangeV4()
Definition: network.cpp:45
std::ostream & operator<<(std::ostream &os, const Network &network)
Definition: network.cpp:80
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
static const Network & getMaxRangeV6()
Definition: network.cpp:53
static bool isValidCidr(const std::string &cidr)
Definition: network.cpp:63
std::istream & operator>>(std::istream &is, Network &network)
Definition: network.cpp:86
boost::asio::ip::address addressFromString(const std::string &address, boost::system::error_code &ec)
parse and convert the input string into an IP address