A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("Ipv4Address");
27 
28 namespace ns3 {
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 {
42  NS_LOG_FUNCTION (&address);
43  uint32_t host = 0;
44  while (true)
45  {
46  uint8_t byte = 0;
47  while (*address != ASCII_DOT && *address != 0)
48  {
49  byte *= 10;
50  byte += *address - ASCII_ZERO;
51  address++;
52  }
53  host <<= 8;
54  host |= byte;
55  if (*address == 0)
56  {
57  break;
58  }
59  address++;
60  }
61  return host;
62 }
63 
64 } // namespace ns3
65 
66 namespace ns3 {
67 
68 
70  : m_mask (0x66666666)
71 {
72  NS_LOG_FUNCTION (this);
73 }
74 
75 Ipv4Mask::Ipv4Mask (uint32_t mask)
76  : m_mask (mask)
77 {
78  NS_LOG_FUNCTION (this << mask);
79 }
80 
81 Ipv4Mask::Ipv4Mask (char const *mask)
82 {
83  NS_LOG_FUNCTION (this << mask);
84  if (*mask == ASCII_SLASH)
85  {
86  uint32_t plen = static_cast<uint32_t> (std::atoi (++mask));
87  NS_ASSERT (plen <= 32);
88  if (plen > 0)
89  {
90  m_mask = 0xffffffff << (32 - plen);
91  }
92  else
93  {
94  m_mask = 0;
95  }
96  }
97  else
98  {
99  m_mask = AsciiToIpv4Host (mask);
100  }
101 }
102 
103 bool
105 {
106  NS_LOG_FUNCTION (this << other);
107  if (other.m_mask == m_mask) {
108  return true;
109  } else {
110  return false;
111  }
112 }
113 
114 bool
116 {
117  NS_LOG_FUNCTION (this << a << b);
118  if ((a.Get () & m_mask) == (b.Get () & m_mask)) {
119  return true;
120  } else {
121  return false;
122  }
123 }
124 
125 uint32_t
126 Ipv4Mask::Get (void) const
127 {
128  NS_LOG_FUNCTION (this);
129  return m_mask;
130 }
131 void
132 Ipv4Mask::Set (uint32_t mask)
133 {
134  NS_LOG_FUNCTION (this << mask);
135  m_mask = mask;
136 }
137 uint32_t
139 {
140  NS_LOG_FUNCTION (this);
141  return ~m_mask;
142 }
143 
144 void
145 Ipv4Mask::Print (std::ostream &os) const
146 {
147  NS_LOG_FUNCTION (this << &os);
148  os << ((m_mask >> 24) & 0xff) << "."
149  << ((m_mask >> 16) & 0xff) << "."
150  << ((m_mask >> 8) & 0xff) << "."
151  << ((m_mask >> 0) & 0xff);
152 }
153 
154 
155 Ipv4Mask
157 {
159  static Ipv4Mask loopback = Ipv4Mask ("255.0.0.0");
160  return loopback;
161 }
162 Ipv4Mask
164 {
166  static Ipv4Mask zero = Ipv4Mask ("0.0.0.0");
167  return zero;
168 }
169 Ipv4Mask
171 {
173  static Ipv4Mask ones = Ipv4Mask ("255.255.255.255");
174  return ones;
175 }
176 
177 uint16_t
179 {
180  NS_LOG_FUNCTION (this);
181  uint16_t tmp = 0;
182  uint32_t mask = m_mask;
183  while (mask != 0 )
184  {
185  mask = mask << 1;
186  tmp++;
187  }
188  return tmp;
189 }
190 
191 
193  : m_address (0x66666666)
194 {
195  NS_LOG_FUNCTION (this);
196 }
198 {
199  NS_LOG_FUNCTION (this << address);
200  m_address = address;
201 }
203 {
204  NS_LOG_FUNCTION (this << address);
205  m_address = AsciiToIpv4Host (address);
206 }
207 
208 uint32_t
209 Ipv4Address::Get (void) const
210 {
211  NS_LOG_FUNCTION (this);
212  return m_address;
213 }
214 void
216 {
217  NS_LOG_FUNCTION (this << address);
218  m_address = address;
219 }
220 void
222 {
223  NS_LOG_FUNCTION (this << address);
224  m_address = AsciiToIpv4Host (address);
225 }
226 
229 {
230  NS_LOG_FUNCTION (this << mask);
231  return Ipv4Address (Get () & mask.Get ());
232 }
233 
236 {
237  NS_LOG_FUNCTION (this << mask);
238  if (mask == Ipv4Mask::GetOnes ())
239  {
240  NS_ASSERT_MSG (false, "Trying to get subnet-directed broadcast address with an all-ones netmask");
241  }
242  return Ipv4Address (Get () | mask.GetInverse ());
243 }
244 
245 bool
247 {
248  NS_LOG_FUNCTION (this << mask);
249  if (mask == Ipv4Mask::GetOnes ())
250  {
251  // If the mask is 255.255.255.255, there is no subnet directed
252  // broadcast for this address.
253  return false;
254  }
255  return ( (Get () | mask.GetInverse ()) == Get () );
256 }
257 
258 bool
260 {
261  NS_LOG_FUNCTION (this);
262  return (m_address == 0xffffffffU);
263 }
264 
265 bool
267 {
268 //
269 // Multicast addresses are defined as ranging from 224.0.0.0 through
270 // 239.255.255.255 (which is E0000000 through EFFFFFFF in hex).
271 //
272  NS_LOG_FUNCTION (this);
273  return (m_address >= 0xe0000000 && m_address <= 0xefffffff);
274 }
275 
276 bool
278 {
279  NS_LOG_FUNCTION (this);
280  // Link-Local multicast address is 224.0.0.0/24
281  return (m_address & 0xffffff00) == 0xe0000000;
282 }
283 
284 void
285 Ipv4Address::Serialize (uint8_t buf[4]) const
286 {
287  NS_LOG_FUNCTION (this << &buf);
288  buf[0] = (m_address >> 24) & 0xff;
289  buf[1] = (m_address >> 16) & 0xff;
290  buf[2] = (m_address >> 8) & 0xff;
291  buf[3] = (m_address >> 0) & 0xff;
292 }
294 Ipv4Address::Deserialize (const uint8_t buf[4])
295 {
296  NS_LOG_FUNCTION (&buf);
297  Ipv4Address ipv4;
298  ipv4.m_address = 0;
299  ipv4.m_address |= buf[0];
300  ipv4.m_address <<= 8;
301  ipv4.m_address |= buf[1];
302  ipv4.m_address <<= 8;
303  ipv4.m_address |= buf[2];
304  ipv4.m_address <<= 8;
305  ipv4.m_address |= buf[3];
306  return ipv4;
307 }
308 
309 void
310 Ipv4Address::Print (std::ostream &os) const
311 {
312  NS_LOG_FUNCTION (this);
313  os << ((m_address >> 24) & 0xff) << "."
314  << ((m_address >> 16) & 0xff) << "."
315  << ((m_address >> 8) & 0xff) << "."
316  << ((m_address >> 0) & 0xff);
317 }
318 
319 bool
321 {
322  NS_LOG_FUNCTION (&address);
323  return address.CheckCompatible (GetType (), 4);
324 }
325 Ipv4Address::operator Address () const
326 {
327  return ConvertTo ();
328 }
329 
330 Address
332 {
333  NS_LOG_FUNCTION (this);
334  uint8_t buf[4];
335  Serialize (buf);
336  return Address (GetType (), buf, 4);
337 }
338 
341 {
342  NS_LOG_FUNCTION (&address);
343  NS_ASSERT (address.CheckCompatible (GetType (), 4));
344  uint8_t buf[4];
345  address.CopyTo (buf);
346  return Deserialize (buf);
347 }
348 
349 uint8_t
351 {
353  static uint8_t type = Address::Register ();
354  return type;
355 }
356 
359 {
361  static Ipv4Address zero ("0.0.0.0");
362  return zero;
363 }
366 {
368  static Ipv4Address any ("0.0.0.0");
369  return any;
370 }
373 {
375  static Ipv4Address broadcast ("255.255.255.255");
376  return broadcast;
377 }
380 {
382  Ipv4Address loopback ("127.0.0.1");
383  return loopback;
384 }
385 
387 {
388  return x.Get ();
389 }
390 
391 std::ostream& operator<< (std::ostream& os, Ipv4Address const& address)
392 {
393  address.Print (os);
394  return os;
395 }
396 std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask)
397 {
398  mask.Print (os);
399  return os;
400 }
401 std::istream & operator >> (std::istream &is, Ipv4Address &address)
402 {
403  std::string str;
404  is >> str;
405  address = Ipv4Address (str.c_str ());
406  return is;
407 }
408 std::istream & operator >> (std::istream &is, Ipv4Mask &mask)
409 {
410  std::string str;
411  is >> str;
412  mask = Ipv4Mask (str.c_str ());
413  return is;
414 }
415 
416 bool operator == (Ipv4Mask const &a, Ipv4Mask const &b)
417 {
418  return a.IsEqual (b);
419 }
420 bool operator != (Ipv4Mask const &a, Ipv4Mask const &b)
421 {
422  return !a.IsEqual (b);
423 }
424 
425 ATTRIBUTE_HELPER_CPP (Ipv4Address);
426 ATTRIBUTE_HELPER_CPP (Ipv4Mask);
427 
428 } // namespace ns3
Ipv4Mask()
Will initialize to a garbage value (0x66666666)
Definition: ipv4-address.cc:69
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:49
#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 "...
size_t operator()(Ipv4Address const &x) const
Returns the hash of the address.
bool IsMatch(Ipv4Address a, Ipv4Address b) const
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:222
uint32_t m_address
IPv4 address.
Definition: ipv4-address.h:206
bool IsLocalMulticast(void) const
#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:61
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
bool IsMulticast(void) const
Ipv4Address CombineMask(Ipv4Mask const &mask) const
Combine this address with a network mask.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
static double zero
Ipv4Address GetSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
a polymophic address class
Definition: address.h:86
bool CheckCompatible(uint8_t type, uint8_t len) const
Definition: address.cc:122
bool IsSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
uint32_t Get(void) const
Get the host-order 32-bit IP address.
static Ipv4Mask GetZero(void)
bool IsBroadcast(void) const
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
void Print(std::ostream &os) const
Print this address to the given output stream.
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:43
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:1217
void Set(uint32_t mask)
input mask is in host order.
static Ipv4Address GetZero(void)
static Ipv4Address GetLoopback(void)
void Print(std::ostream &os) const
Print this mask to the given output stream.
static Ipv4Mask GetLoopback(void)
Address ConvertTo(void) const
Convert to an Address type.
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:84
uint32_t m_mask
IP mask.
Definition: ipv4-address.h:289
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:38
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition: address.cc:82
bool IsEqual(Ipv4Mask other) const
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.cc:89
static uint8_t GetType(void)
Get the underlying address type (automatically assigned).
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
static Ipv4Address ConvertFrom(const Address &address)
tuple address
Definition: first.py:37
ATTRIBUTE_HELPER_CPP(ObjectFactory)
#define ASCII_DOT
Definition: ipv4-address.cc:30
uint32_t GetInverse(void) const
Return the inverse mask in host order.
static uint8_t Register(void)
Allocate a new type id for a new type of address.
Definition: address.cc:138
uint16_t GetPrefixLength(void) const