NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 namespace nfd {
29 
30 void
31 Network::print(std::ostream& os) const
32 {
33  os << m_minAddress << " <-> " << m_maxAddress;
34 }
35 
36 const Network&
38 {
39  using boost::asio::ip::address_v4;
40  static Network range = Network(address_v4(0), address_v4(0xFFFFFFFF));
41  return range;
42 }
43 
44 const Network&
46 {
47  using boost::asio::ip::address_v6;
48  static address_v6::bytes_type maxV6 = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
49  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
50  static Network range = Network(address_v6(), address_v6(maxV6));
51  return range;
52 }
53 
56 
57 std::ostream&
58 operator<<(std::ostream& os, const Network& network)
59 {
60  network.print(os);
61  return os;
62 }
63 
64 std::istream&
65 operator>>(std::istream& is, Network& network)
66 {
67  using namespace boost::asio;
68 
69  std::string networkStr;
70  is >> networkStr;
71 
72  size_t position = networkStr.find('/');
73  if (position == std::string::npos)
74  {
75  network.m_minAddress = ip::address::from_string(networkStr);
76  network.m_maxAddress = ip::address::from_string(networkStr);
77  }
78  else
79  {
80  ip::address address = ip::address::from_string(networkStr.substr(0, position));
81  size_t mask = boost::lexical_cast<size_t>(networkStr.substr(position+1));
82 
83  if (address.is_v4())
84  {
85  ip::address_v4::bytes_type maskBytes = boost::initialized_value;
86  for (size_t i = 0; i < mask; i++)
87  {
88  size_t byteId = i / 8;
89  size_t bitIndex = 7 - i % 8;
90  maskBytes[byteId] |= (1 << bitIndex);
91  }
92 
93  ip::address_v4::bytes_type addressBytes = address.to_v4().to_bytes();
94  ip::address_v4::bytes_type min;
95  ip::address_v4::bytes_type max;
96 
97  for (size_t i = 0; i < addressBytes.size(); i++)
98  {
99  min[i] = addressBytes[i] & maskBytes[i];
100  max[i] = addressBytes[i] | ~(maskBytes[i]);
101  }
102 
103  network.m_minAddress = ip::address_v4(min);
104  network.m_maxAddress = ip::address_v4(max);
105  }
106  else
107  {
108  ip::address_v6::bytes_type maskBytes = boost::initialized_value;
109  for (size_t i = 0; i < mask; i++)
110  {
111  size_t byteId = i / 8;
112  size_t bitIndex = 7 - i % 8;
113  maskBytes[byteId] |= (1 << bitIndex);
114  }
115 
116  ip::address_v6::bytes_type addressBytes = address.to_v6().to_bytes();
117  ip::address_v6::bytes_type min;
118  ip::address_v6::bytes_type max;
119 
120  for (size_t i = 0; i < addressBytes.size(); i++)
121  {
122  min[i] = addressBytes[i] & maskBytes[i];
123  max[i] = addressBytes[i] | ~(maskBytes[i]);
124  }
125 
126  network.m_minAddress = ip::address_v6(min);
127  network.m_maxAddress = ip::address_v6(max);
128  }
129  }
130  return is;
131 }
132 
133 } // namespace nfd
void print(std::ostream &os) const
Definition: network.cpp:31
std::ostream & operator<<(std::ostream &os, const Face &face)
Definition: ndn-face.hpp:38
static const Network & getMaxRangeV4()
Definition: network.cpp:37
static const Network & getMaxRangeV6()
Definition: network.cpp:45
std::istream & operator>>(std::istream &is, Network &network)
Definition: network.cpp:65