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 
202  : m_address (0x66666666)
203 {
204  NS_LOG_FUNCTION (this);
205 }
207 {
208  NS_LOG_FUNCTION (this << address);
209  m_address = address;
210 }
212 {
213  NS_LOG_FUNCTION (this << address);
215 }
216 
217 uint32_t
218 Ipv4Address::Get (void) const
219 {
220  NS_LOG_FUNCTION (this);
221  return m_address;
222 }
223 void
225 {
226  NS_LOG_FUNCTION (this << address);
227  m_address = address;
228 }
229 void
231 {
232  NS_LOG_FUNCTION (this << address);
234 }
235 
238 {
239  NS_LOG_FUNCTION (this << mask);
240  return Ipv4Address (Get () & mask.Get ());
241 }
242 
245 {
246  NS_LOG_FUNCTION (this << mask);
247  if (mask == Ipv4Mask::GetOnes ())
248  {
249  NS_ASSERT_MSG (false, "Trying to get subnet-directed broadcast address with an all-ones netmask");
250  }
251  return Ipv4Address (Get () | mask.GetInverse ());
252 }
253 
254 bool
256 {
257  NS_LOG_FUNCTION (this << mask);
258  if (mask == Ipv4Mask::GetOnes ())
259  {
260  // If the mask is 255.255.255.255, there is no subnet directed
261  // broadcast for this address.
262  return false;
263  }
264  return ( (Get () | mask.GetInverse ()) == Get () );
265 }
266 
267 bool
268 Ipv4Address::IsAny (void) const
269 {
270  NS_LOG_FUNCTION (this);
271  return (m_address == 0x00000000U);
272 }
273 
274 bool
276 {
277  NS_LOG_FUNCTION (this);
278  return (m_address == 0x7f000001U);
279 }
280 
281 bool
283 {
284  NS_LOG_FUNCTION (this);
285  return (m_address == 0xffffffffU);
286 }
287 
288 bool
290 {
291 //
292 // Multicast addresses are defined as ranging from 224.0.0.0 through
293 // 239.255.255.255 (which is E0000000 through EFFFFFFF in hex).
294 //
295  NS_LOG_FUNCTION (this);
296  return (m_address >= 0xe0000000 && m_address <= 0xefffffff);
297 }
298 
299 bool
301 {
302  NS_LOG_FUNCTION (this);
303  // Link-Local multicast address is 224.0.0.0/24
304  return (m_address & 0xffffff00) == 0xe0000000;
305 }
306 
307 void
308 Ipv4Address::Serialize (uint8_t buf[4]) const
309 {
310  NS_LOG_FUNCTION (this << &buf);
311  buf[0] = (m_address >> 24) & 0xff;
312  buf[1] = (m_address >> 16) & 0xff;
313  buf[2] = (m_address >> 8) & 0xff;
314  buf[3] = (m_address >> 0) & 0xff;
315 }
317 Ipv4Address::Deserialize (const uint8_t buf[4])
318 {
319  NS_LOG_FUNCTION (&buf);
320  Ipv4Address ipv4;
321  ipv4.m_address = 0;
322  ipv4.m_address |= buf[0];
323  ipv4.m_address <<= 8;
324  ipv4.m_address |= buf[1];
325  ipv4.m_address <<= 8;
326  ipv4.m_address |= buf[2];
327  ipv4.m_address <<= 8;
328  ipv4.m_address |= buf[3];
329  return ipv4;
330 }
331 
332 void
333 Ipv4Address::Print (std::ostream &os) const
334 {
335  NS_LOG_FUNCTION (this);
336  os << ((m_address >> 24) & 0xff) << "."
337  << ((m_address >> 16) & 0xff) << "."
338  << ((m_address >> 8) & 0xff) << "."
339  << ((m_address >> 0) & 0xff);
340 }
341 
342 bool
344 {
346  return address.CheckCompatible (GetType (), 4);
347 }
348 Ipv4Address::operator Address () const
349 {
350  return ConvertTo ();
351 }
352 
353 Address
355 {
356  NS_LOG_FUNCTION (this);
357  uint8_t buf[4];
358  Serialize (buf);
359  return Address (GetType (), buf, 4);
360 }
361 
364 {
366  NS_ASSERT (address.CheckCompatible (GetType (), 4));
367  uint8_t buf[4];
368  address.CopyTo (buf);
369  return Deserialize (buf);
370 }
371 
372 uint8_t
374 {
376  static uint8_t type = Address::Register ();
377  return type;
378 }
379 
382 {
384  static Ipv4Address zero ("0.0.0.0");
385  return zero;
386 }
389 {
391  static Ipv4Address any ("0.0.0.0");
392  return any;
393 }
396 {
398  static Ipv4Address broadcast ("255.255.255.255");
399  return broadcast;
400 }
403 {
405  Ipv4Address loopback ("127.0.0.1");
406  return loopback;
407 }
408 
410 {
411  return x.Get ();
412 }
413 
414 std::ostream& operator<< (std::ostream& os, Ipv4Address const& address)
415 {
416  address.Print (os);
417  return os;
418 }
419 std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask)
420 {
421  mask.Print (os);
422  return os;
423 }
424 std::istream & operator >> (std::istream &is, Ipv4Address &address)
425 {
426  std::string str;
427  is >> str;
428  address = Ipv4Address (str.c_str ());
429  return is;
430 }
431 std::istream & operator >> (std::istream &is, Ipv4Mask &mask)
432 {
433  std::string str;
434  is >> str;
435  mask = Ipv4Mask (str.c_str ());
436  return is;
437 }
438 
439 bool operator == (Ipv4Mask const &a, Ipv4Mask const &b)
440 {
441  return a.IsEqual (b);
442 }
443 bool operator != (Ipv4Mask const &a, Ipv4Mask const &b)
444 {
445  return !a.IsEqual (b);
446 }
447 
450 
451 } // namespace ns3
Ipv4Mask()
Will initialize to a garbage value (0x66666666)
Definition: ipv4-address.cc:78
static Ipv4Address Deserialize(const uint8_t buf[4])
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
#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 IsMatch(Ipv4Address a, Ipv4Address b) const
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
bool IsLocalhost(void) const
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
uint32_t m_address
IPv4 address.
Definition: ipv4-address.h:217
#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:202
uint32_t GetInverse(void) const
Return the inverse mask in host order.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
static double zero
a polymophic address class
Definition: address.h:90
bool IsMulticast(void) const
bool IsSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
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)
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
bool operator!=(Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > a, Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > b)
Inequality test.
Definition: callback.h:1471
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:37
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 the 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
uint32_t m_mask
IP mask.
Definition: ipv4-address.h:326
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:40
bool IsAny(void) const
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:135
static uint8_t GetType(void)
Get the underlying address type (automatically assigned).
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.