NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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; -*- */
26 #include "network.hpp"
27 
28 #include <boost/utility/value_init.hpp>
29 #include <algorithm>
30 
31 namespace nfd {
32 
33 Network::Network() = default;
34 
35 Network::Network(const boost::asio::ip::address& minAddress,
36  const boost::asio::ip::address& maxAddress)
37  : m_minAddress(minAddress)
38  , m_maxAddress(maxAddress)
39 {
40 }
41 
42 const Network&
44 {
45  using boost::asio::ip::address_v4;
46  static Network range{address_v4{}, address_v4{0xffffffff}};
47  return range;
48 }
49 
50 const Network&
52 {
53  using boost::asio::ip::address_v6;
54  static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
56  static Network range{address_v6{}, address_v6{maxV6}};
57  return range;
58 }
59 
60 bool
61 Network::isValidCidr(const std::string& cidr)
62 {
63  auto pos = cidr.find('/');
64  if (pos == std::string::npos) {
65  return false;
66  }
67 
68  boost::system::error_code invalidIp;
69  boost::asio::ip::address_v4::from_string(cidr.substr(0, pos), invalidIp);
70  if (invalidIp) {
71  return false;
72  }
73 
74  auto prefixLenStr = cidr.substr(pos + 1);
75  if (!std::all_of(prefixLenStr.begin(), prefixLenStr.end(), ::isdigit)) {
76  return false;
77  }
78  int prefixLen = -1;
79  try {
80  prefixLen = boost::lexical_cast<int>(prefixLenStr);
81  }
82  catch (const boost::bad_lexical_cast&) {
83  return false;
84  }
85  if (prefixLen < 0 || prefixLen > 32) {
86  return false;
87  }
88 
89  return true;
90 }
91 
92 std::ostream&
93 operator<<(std::ostream& os, const Network& network)
94 {
95  return os << network.m_minAddress << " <-> " << network.m_maxAddress;
96 }
97 
98 std::istream&
99 operator>>(std::istream& is, Network& network)
100 {
101  namespace ip = boost::asio::ip;
102 
103  std::string networkStr;
104  is >> networkStr;
105 
106  size_t position = networkStr.find('/');
107  if (position == std::string::npos) {
108  network.m_minAddress = ip::address::from_string(networkStr);
109  network.m_maxAddress = ip::address::from_string(networkStr);
110  }
111  else {
112  ip::address address = ip::address::from_string(networkStr.substr(0, position));
113  size_t mask = boost::lexical_cast<size_t>(networkStr.substr(position+1));
114 
115  if (address.is_v4()) {
116  ip::address_v4::bytes_type maskBytes = boost::initialized_value;
117  for (size_t i = 0; i < mask; i++) {
118  size_t byteId = i / 8;
119  size_t bitIndex = 7 - i % 8;
120  maskBytes[byteId] |= (1 << bitIndex);
121  }
122 
123  ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes();
124  ip::address_v4::bytes_type min;
125  ip::address_v4::bytes_type max;
126 
127  for (size_t i = 0; i < addressBytes.size(); i++) {
128  min[i] = addressBytes[i] & maskBytes[i];
129  max[i] = addressBytes[i] | ~(maskBytes[i]);
130  }
131 
132  network.m_minAddress = ip::address_v4(min);
133  network.m_maxAddress = ip::address_v4(max);
134  }
135  else {
136  ip::address_v6::bytes_type maskBytes = boost::initialized_value;
137  for (size_t i = 0; i < mask; i++) {
138  size_t byteId = i / 8;
139  size_t bitIndex = 7 - i % 8;
140  maskBytes[byteId] |= (1 << bitIndex);
141  }
142 
143  ip::address_v6::bytes_type addressBytes = address.to_v6().to_bytes();
144  ip::address_v6::bytes_type min;
145  ip::address_v6::bytes_type max;
146 
147  for (size_t i = 0; i < addressBytes.size(); i++) {
148  min[i] = addressBytes[i] & maskBytes[i];
149  max[i] = addressBytes[i] | ~(maskBytes[i]);
150  }
151 
152  network.m_minAddress = ip::address_v6(min);
153  network.m_maxAddress = ip::address_v6(max);
154  }
155  }
156 
157  return is;
158 }
159 
160 } // namespace nfd
static const Network & getMaxRangeV4()
Definition: network.cpp:43
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-common.hpp:40
static const Network & getMaxRangeV6()
Definition: network.cpp:51
static bool isValidCidr(const std::string &cidr)
Definition: network.cpp:61
friend std::istream & operator>>(std::istream &is, Network &network)
Definition: network.cpp:99
friend std::ostream & operator<<(std::ostream &os, const Network &network)
Definition: network.cpp:93