A Discrete-Event Network Simulator
API
ipv4-address.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include <cstdlib>
22 #include "ns3/log.h"
23 #include "ipv4-address.h"
24 #include "ns3/assert.h"
25 
26 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("Ipv4Address");
29 
30 #define ASCII_DOT (0x2e)
31 #define ASCII_ZERO (0x30)
32 #define ASCII_SLASH (0x2f)
33 
39 static uint32_t
40 AsciiToIpv4Host (char const *address)
41 {
43  uint32_t host = 0;
44  uint8_t numberOfDots = 0;
45  char const *ptr = address;
46 
47  NS_ASSERT_MSG (*ptr != ASCII_DOT, "Error, can not build an IPv4 address from an invalid string: " << address);
48  while (true)
49  {
50  uint8_t byte = 0;
51  while (*ptr != ASCII_DOT && *ptr != 0)
52  {
53  byte *= 10;
54  byte += *ptr - ASCII_ZERO;
55  ptr++;
56  }
57  host <<= 8;
58  host |= byte;
59  if (*ptr == 0)
60  {
61  break;
62  }
63  ptr++;
64  NS_ASSERT_MSG (*ptr != ASCII_DOT, "Error, can not build an IPv4 address from an invalid string: " << address);
65  numberOfDots ++;
66  }
67  NS_ASSERT_MSG (*(ptr-1) != ASCII_DOT, "Error, can not build an IPv4 address from an invalid string: " << address);
68  NS_ASSERT_MSG (numberOfDots == 3, "Error, can not build an IPv4 address from an invalid string: " << address);
69 
70  return host;
71 }
72 
73 } // namespace ns3
74 
75 namespace ns3 {
76 
77 
79  : m_mask (0x66666666)
80 {
81  NS_LOG_FUNCTION (this);
82 }
83 
84 Ipv4Mask::Ipv4Mask (uint32_t mask)
85  : m_mask (mask)
86 {
87  NS_LOG_FUNCTION (this << mask);
88 }
89 
90 Ipv4Mask::Ipv4Mask (char const *mask)
91 {
92  NS_LOG_FUNCTION (this << mask);
93  if (*mask == ASCII_SLASH)
94  {
95  uint32_t plen = static_cast<uint32_t> (std::atoi (++mask));
96  NS_ASSERT (plen <= 32);
97  if (plen > 0)
98  {
99  m_mask = 0xffffffff << (32 - plen);
100  }
101  else
102  {
103  m_mask = 0;
104  }
105  }
106  else
107  {
108  m_mask = AsciiToIpv4Host (mask);
109  }
110 }
111 
112 bool
114 {
115  NS_LOG_FUNCTION (this << other);
116  if (other.m_mask == m_mask) {
117  return true;
118  } else {
119  return false;
120  }
121 }
122 
123 bool
125 {
126  NS_LOG_FUNCTION (this << a << b);
127  if ((a.Get () & m_mask) == (b.Get () & m_mask)) {
128  return true;
129  } else {
130  return false;
131  }
132 }
133 
134 uint32_t
135 Ipv4Mask::Get (void) const
136 {
137  NS_LOG_FUNCTION (this);
138  return m_mask;
139 }
140 void
141 Ipv4Mask::Set (uint32_t mask)
142 {
143  NS_LOG_FUNCTION (this << mask);
144  m_mask = mask;
145 }
146 uint32_t
148 {
149  NS_LOG_FUNCTION (this);
150  return ~m_mask;
151 }
152 
153 void
154 Ipv4Mask::Print (std::ostream &os) const
155 {
156  NS_LOG_FUNCTION (this << &os);
157  os << ((m_mask >> 24) & 0xff) << "."
158  << ((m_mask >> 16) & 0xff) << "."
159  << ((m_mask >> 8) & 0xff) << "."
160  << ((m_mask >> 0) & 0xff);
161 }
162 
163 
164 Ipv4Mask
166 {
168  static Ipv4Mask loopback = Ipv4Mask ("255.0.0.0");
169  return loopback;
170 }
171 Ipv4Mask
173 {
175  static Ipv4Mask zero = Ipv4Mask ("0.0.0.0");
176  return zero;
177 }
178 Ipv4Mask
180 {
182  static Ipv4Mask ones = Ipv4Mask ("255.255.255.255");
183  return ones;
184 }
185 
186 uint16_t
188 {
189  NS_LOG_FUNCTION (this);
190  uint16_t tmp = 0;
191  uint32_t mask = m_mask;
192  while (mask != 0 )
193  {
194  mask = mask << 1;
195  tmp++;
196  }
197  return tmp;
198 }
199 
200 static constexpr uint32_t UNINITIALIZED = 0x66666666U;
201 
203  : m_address (UNINITIALIZED), m_initialized (false)
204 {
205  NS_LOG_FUNCTION (this);
206 }
208 {
209  NS_LOG_FUNCTION (this << address);
210  m_address = address;
211  m_initialized = true;
212 }
214 {
215  NS_LOG_FUNCTION (this << address);
217  m_initialized = true;
218 }
219 
220 uint32_t
221 Ipv4Address::Get (void) const
222 {
223  NS_LOG_FUNCTION (this);
224  return m_address;
225 }
226 void
228 {
229  NS_LOG_FUNCTION (this << address);
230  m_address = address;
231  m_initialized = true;
232 }
233 void
235 {
236  NS_LOG_FUNCTION (this << address);
238  m_initialized = true;
239 }
240 
243 {
244  NS_LOG_FUNCTION (this << mask);
245  return Ipv4Address (Get () & mask.Get ());
246 }
247 
250 {
251  NS_LOG_FUNCTION (this << mask);
252  if (mask == Ipv4Mask::GetOnes ())
253  {
254  NS_ASSERT_MSG (false, "Trying to get subnet-directed broadcast address with an all-ones netmask");
255  }
256  return Ipv4Address (Get () | mask.GetInverse ());
257 }
258 
259 bool
261 {
262  NS_LOG_FUNCTION (this << mask);
263  if (mask == Ipv4Mask::GetOnes ())
264  {
265  // If the mask is 255.255.255.255, there is no subnet directed
266  // broadcast for this address.
267  return false;
268  }
269  return ( (Get () | mask.GetInverse ()) == Get () );
270 }
271 
272 bool
274 {
275  NS_LOG_FUNCTION (this);
276  return (m_initialized);
277 }
278 
279 bool
280 Ipv4Address::IsAny (void) const
281 {
282  NS_LOG_FUNCTION (this);
283  return (m_address == 0x00000000U);
284 }
285 
286 bool
288 {
289  NS_LOG_FUNCTION (this);
290  return (m_address == 0x7f000001U);
291 }
292 
293 bool
295 {
296  NS_LOG_FUNCTION (this);
297  return (m_address == 0xffffffffU);
298 }
299 
300 bool
302 {
303 //
304 // Multicast addresses are defined as ranging from 224.0.0.0 through
305 // 239.255.255.255 (which is E0000000 through EFFFFFFF in hex).
306 //
307  NS_LOG_FUNCTION (this);
308  return (m_address >= 0xe0000000 && m_address <= 0xefffffff);
309 }
310 
311 bool
313 {
314  NS_LOG_FUNCTION (this);
315  // Link-Local multicast address is 224.0.0.0/24
316  return (m_address & 0xffffff00) == 0xe0000000;
317 }
318 
319 void
320 Ipv4Address::Serialize (uint8_t buf[4]) const
321 {
322  NS_LOG_FUNCTION (this << &buf);
323  buf[0] = (m_address >> 24) & 0xff;
324  buf[1] = (m_address >> 16) & 0xff;
325  buf[2] = (m_address >> 8) & 0xff;
326  buf[3] = (m_address >> 0) & 0xff;
327 }
329 Ipv4Address::Deserialize (const uint8_t buf[4])
330 {
331  NS_LOG_FUNCTION (&buf);
332  Ipv4Address ipv4;
333  ipv4.m_address = 0;
334  ipv4.m_address |= buf[0];
335  ipv4.m_address <<= 8;
336  ipv4.m_address |= buf[1];
337  ipv4.m_address <<= 8;
338  ipv4.m_address |= buf[2];
339  ipv4.m_address <<= 8;
340  ipv4.m_address |= buf[3];
341  ipv4.m_initialized = true;
342 
343  return ipv4;
344 }
345 
346 void
347 Ipv4Address::Print (std::ostream &os) const
348 {
349  NS_LOG_FUNCTION (this);
350  os << ((m_address >> 24) & 0xff) << "."
351  << ((m_address >> 16) & 0xff) << "."
352  << ((m_address >> 8) & 0xff) << "."
353  << ((m_address >> 0) & 0xff);
354 }
355 
356 bool
358 {
360  return address.CheckCompatible (GetType (), 4);
361 }
362 Ipv4Address::operator Address () const
363 {
364  return ConvertTo ();
365 }
366 
367 Address
369 {
370  NS_LOG_FUNCTION (this);
371  uint8_t buf[4];
372  Serialize (buf);
373  return Address (GetType (), buf, 4);
374 }
375 
378 {
380  NS_ASSERT (address.CheckCompatible (GetType (), 4));
381  uint8_t buf[4];
382  address.CopyTo (buf);
383  return Deserialize (buf);
384 }
385 
386 uint8_t
388 {
390  static uint8_t type = Address::Register ();
391  return type;
392 }
393 
396 {
398  static Ipv4Address zero ("0.0.0.0");
399  return zero;
400 }
403 {
405  static Ipv4Address any ("0.0.0.0");
406  return any;
407 }
410 {
412  static Ipv4Address broadcast ("255.255.255.255");
413  return broadcast;
414 }
417 {
419  Ipv4Address loopback ("127.0.0.1");
420  return loopback;
421 }
422 
424 {
425  return std::hash<uint32_t>()(x.Get ());
426 }
427 
428 std::ostream& operator<< (std::ostream& os, Ipv4Address const& address)
429 {
430  address.Print (os);
431  return os;
432 }
433 std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask)
434 {
435  mask.Print (os);
436  return os;
437 }
438 std::istream & operator >> (std::istream &is, Ipv4Address &address)
439 {
440  std::string str;
441  is >> str;
442  address = Ipv4Address (str.c_str ());
443  return is;
444 }
445 std::istream & operator >> (std::istream &is, Ipv4Mask &mask)
446 {
447  std::string str;
448  is >> str;
449  mask = Ipv4Mask (str.c_str ());
450  return is;
451 }
452 
455 
456 } // namespace ns3
Ipv4Mask()
Will initialize to a garbage value (0x66666666)
Definition: ipv4-address.cc:78
static Ipv4Address Deserialize(const uint8_t buf[4])
#define ASCII_ZERO
Definition: ipv4-address.cc:31
static Ipv4Mask GetOnes(void)
static Ipv4Address GetAny(void)
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
bool IsBroadcast(void) const
bool IsInitialized(void) const
bool IsMatch(Ipv4Address a, Ipv4Address b) const
bool IsLocalhost(void) const
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:269
uint32_t m_address
IPv4 address.
Definition: ipv4-address.h:227
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type
#define ASCII_SLASH
Definition: ipv4-address.cc:32
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
uint32_t GetInverse(void) const
Return the inverse mask in host order.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:160
static double zero
a polymophic address class
Definition: address.h:90
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:137
bool IsMulticast(void) const
bool IsSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
NS_DEPRECATED_3_31 bool IsEqual(Ipv4Mask other) const
Ipv4Address CombineMask(Ipv4Mask const &mask) const
Combine this address with a network mask.
static Ipv4Mask GetZero(void)
bool IsLocalMulticast(void) const
static bool IsMatchingType(const Address &address)
static Ipv4Address GetBroadcast(void)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:44
void Set(uint32_t mask)
input mask is in host order.
static Ipv4Address GetZero(void)
static Ipv4Address GetLoopback(void)
Ipv4Address GetSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
size_t operator()(Ipv4Address const &x) const
Returns the hash of an IPv4 address.
static Ipv4Mask GetLoopback(void)
static uint32_t AsciiToIpv4Host(char const *address)
Converts a string representing an IP address into the address.
Definition: ipv4-address.cc:40
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:88
uint32_t m_mask
IP mask.
Definition: ipv4-address.h:360
void Set(uint32_t address)
input address is in host order.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
bool IsAny(void) const
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
static uint8_t GetType(void)
Get the underlying address type (automatically assigned).
static constexpr uint32_t UNINITIALIZED
bool m_initialized
IPv4 address has been explicitly initialized to a valid value.
Definition: ipv4-address.h:228
uint16_t GetPrefixLength(void) const
static Ipv4Address ConvertFrom(const Address &address)
void Print(std::ostream &os) const
Print this address to the given output stream.
uint32_t Get(void) const
Get the host-order 32-bit IP address.
#define ASCII_DOT
Definition: ipv4-address.cc:30
Address ConvertTo(void) const
Convert to an Address type.
void Print(std::ostream &os) const
Print this mask to the given output stream.
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition: address.cc:138
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.