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 
34 static uint32_t
35 AsciiToIpv4Host (char const *address)
36 {
37  NS_LOG_FUNCTION (&address);
38  uint32_t host = 0;
39  while (true)
40  {
41  uint8_t byte = 0;
42  while (*address != ASCII_DOT && *address != 0)
43  {
44  byte *= 10;
45  byte += *address - ASCII_ZERO;
46  address++;
47  }
48  host <<= 8;
49  host |= byte;
50  if (*address == 0)
51  {
52  break;
53  }
54  address++;
55  }
56  return host;
57 }
58 
59 } // namespace ns3
60 
61 namespace ns3 {
62 
63 
65  : m_mask (0x66666666)
66 {
67  NS_LOG_FUNCTION (this);
68 }
69 
70 Ipv4Mask::Ipv4Mask (uint32_t mask)
71  : m_mask (mask)
72 {
73  NS_LOG_FUNCTION (this << mask);
74 }
75 
76 Ipv4Mask::Ipv4Mask (char const *mask)
77 {
78  NS_LOG_FUNCTION (this << mask);
79  if (*mask == ASCII_SLASH)
80  {
81  uint32_t plen = static_cast<uint32_t> (std::atoi (++mask));
82  NS_ASSERT (plen <= 32);
83  if (plen > 0)
84  {
85  m_mask = 0xffffffff << (32 - plen);
86  }
87  else
88  {
89  m_mask = 0;
90  }
91  }
92  else
93  {
94  m_mask = AsciiToIpv4Host (mask);
95  }
96 }
97 
98 bool
100 {
101  NS_LOG_FUNCTION (this << other);
102  if (other.m_mask == m_mask) {
103  return true;
104  } else {
105  return false;
106  }
107 }
108 
109 bool
111 {
112  NS_LOG_FUNCTION (this << a << b);
113  if ((a.Get () & m_mask) == (b.Get () & m_mask)) {
114  return true;
115  } else {
116  return false;
117  }
118 }
119 
120 uint32_t
121 Ipv4Mask::Get (void) const
122 {
123  NS_LOG_FUNCTION (this);
124  return m_mask;
125 }
126 void
127 Ipv4Mask::Set (uint32_t mask)
128 {
129  NS_LOG_FUNCTION (this << mask);
130  m_mask = mask;
131 }
132 uint32_t
134 {
135  NS_LOG_FUNCTION (this);
136  return ~m_mask;
137 }
138 
139 void
140 Ipv4Mask::Print (std::ostream &os) const
141 {
142  NS_LOG_FUNCTION (this << &os);
143  os << ((m_mask >> 24) & 0xff) << "."
144  << ((m_mask >> 16) & 0xff) << "."
145  << ((m_mask >> 8) & 0xff) << "."
146  << ((m_mask >> 0) & 0xff);
147 }
148 
149 
150 Ipv4Mask
152 {
154  static Ipv4Mask loopback = Ipv4Mask ("255.0.0.0");
155  return loopback;
156 }
157 Ipv4Mask
159 {
161  static Ipv4Mask zero = Ipv4Mask ("0.0.0.0");
162  return zero;
163 }
164 Ipv4Mask
166 {
168  static Ipv4Mask ones = Ipv4Mask ("255.255.255.255");
169  return ones;
170 }
171 
172 uint16_t
174 {
175  NS_LOG_FUNCTION (this);
176  uint16_t tmp = 0;
177  uint32_t mask = m_mask;
178  while (mask != 0 )
179  {
180  mask = mask << 1;
181  tmp++;
182  }
183  return tmp;
184 }
185 
186 
188  : m_address (0x66666666)
189 {
190  NS_LOG_FUNCTION (this);
191 }
193 {
194  NS_LOG_FUNCTION (this << address);
195  m_address = address;
196 }
198 {
199  NS_LOG_FUNCTION (this << address);
200  m_address = AsciiToIpv4Host (address);
201 }
202 
203 uint32_t
204 Ipv4Address::Get (void) const
205 {
206  NS_LOG_FUNCTION (this);
207  return m_address;
208 }
209 void
211 {
212  NS_LOG_FUNCTION (this << address);
213  m_address = address;
214 }
215 void
217 {
218  NS_LOG_FUNCTION (this << address);
219  m_address = AsciiToIpv4Host (address);
220 }
221 
224 {
225  NS_LOG_FUNCTION (this << mask);
226  return Ipv4Address (Get () & mask.Get ());
227 }
228 
231 {
232  NS_LOG_FUNCTION (this << mask);
233  if (mask == Ipv4Mask::GetOnes ())
234  {
235  NS_ASSERT_MSG (false, "Trying to get subnet-directed broadcast address with an all-ones netmask");
236  }
237  return Ipv4Address (Get () | mask.GetInverse ());
238 }
239 
240 bool
242 {
243  NS_LOG_FUNCTION (this << mask);
244  if (mask == Ipv4Mask::GetOnes ())
245  {
246  // If the mask is 255.255.255.255, there is no subnet directed
247  // broadcast for this address.
248  return false;
249  }
250  return ( (Get () | mask.GetInverse ()) == Get () );
251 }
252 
253 bool
255 {
256  NS_LOG_FUNCTION (this);
257  return (m_address == 0xffffffffU);
258 }
259 
260 bool
262 {
263 //
264 // Multicast addresses are defined as ranging from 224.0.0.0 through
265 // 239.255.255.255 (which is E0000000 through EFFFFFFF in hex).
266 //
267  NS_LOG_FUNCTION (this);
268  return (m_address >= 0xe0000000 && m_address <= 0xefffffff);
269 }
270 
271 bool
273 {
274  NS_LOG_FUNCTION (this);
275  // Link-Local multicast address is 224.0.0.0/24
276  return (m_address & 0xffffff00) == 0xe0000000;
277 }
278 
279 void
280 Ipv4Address::Serialize (uint8_t buf[4]) const
281 {
282  NS_LOG_FUNCTION (this << &buf);
283  buf[0] = (m_address >> 24) & 0xff;
284  buf[1] = (m_address >> 16) & 0xff;
285  buf[2] = (m_address >> 8) & 0xff;
286  buf[3] = (m_address >> 0) & 0xff;
287 }
289 Ipv4Address::Deserialize (const uint8_t buf[4])
290 {
291  NS_LOG_FUNCTION (&buf);
292  Ipv4Address ipv4;
293  ipv4.m_address = 0;
294  ipv4.m_address |= buf[0];
295  ipv4.m_address <<= 8;
296  ipv4.m_address |= buf[1];
297  ipv4.m_address <<= 8;
298  ipv4.m_address |= buf[2];
299  ipv4.m_address <<= 8;
300  ipv4.m_address |= buf[3];
301  return ipv4;
302 }
303 
304 void
305 Ipv4Address::Print (std::ostream &os) const
306 {
307  NS_LOG_FUNCTION (this);
308  os << ((m_address >> 24) & 0xff) << "."
309  << ((m_address >> 16) & 0xff) << "."
310  << ((m_address >> 8) & 0xff) << "."
311  << ((m_address >> 0) & 0xff);
312 }
313 
314 bool
316 {
317  NS_LOG_FUNCTION (&address);
318  return address.CheckCompatible (GetType (), 4);
319 }
320 Ipv4Address::operator Address () const
321 {
322  return ConvertTo ();
323 }
324 
325 Address
327 {
328  NS_LOG_FUNCTION (this);
329  uint8_t buf[4];
330  Serialize (buf);
331  return Address (GetType (), buf, 4);
332 }
333 
336 {
337  NS_LOG_FUNCTION (&address);
338  NS_ASSERT (address.CheckCompatible (GetType (), 4));
339  uint8_t buf[4];
340  address.CopyTo (buf);
341  return Deserialize (buf);
342 }
343 
344 uint8_t
346 {
348  static uint8_t type = Address::Register ();
349  return type;
350 }
351 
354 {
356  static Ipv4Address zero ("0.0.0.0");
357  return zero;
358 }
361 {
363  static Ipv4Address any ("0.0.0.0");
364  return any;
365 }
368 {
370  static Ipv4Address broadcast ("255.255.255.255");
371  return broadcast;
372 }
375 {
377  Ipv4Address loopback ("127.0.0.1");
378  return loopback;
379 }
380 
382 {
383  return x.Get ();
384 }
385 
386 std::ostream& operator<< (std::ostream& os, Ipv4Address const& address)
387 {
388  address.Print (os);
389  return os;
390 }
391 std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask)
392 {
393  mask.Print (os);
394  return os;
395 }
396 std::istream & operator >> (std::istream &is, Ipv4Address &address)
397 {
398  std::string str;
399  is >> str;
400  address = Ipv4Address (str.c_str ());
401  return is;
402 }
403 std::istream & operator >> (std::istream &is, Ipv4Mask &mask)
404 {
405  std::string str;
406  is >> str;
407  mask = Ipv4Mask (str.c_str ());
408  return is;
409 }
410 
411 bool operator == (Ipv4Mask const &a, Ipv4Mask const &b)
412 {
413  return a.IsEqual (b);
414 }
415 bool operator != (Ipv4Mask const &a, Ipv4Mask const &b)
416 {
417  return !a.IsEqual (b);
418 }
419 
420 ATTRIBUTE_HELPER_CPP (Ipv4Address);
421 ATTRIBUTE_HELPER_CPP (Ipv4Mask);
422 
423 } // namespace ns3
Ipv4Mask()
Will initialize to a garbage value (0x66666666)
Definition: ipv4-address.cc:64
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)
Definition: log.h:345
size_t operator()(Ipv4Address const &x) const
bool IsMatch(Ipv4Address a, Ipv4Address b) const
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:210
uint32_t m_address
Definition: ipv4-address.h:194
bool IsLocalMulticast(void) const
#define ASCII_SLASH
Definition: ipv4-address.cc:32
#define NS_ASSERT(condition)
Definition: assert.h:64
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.
Definition: log.h:309
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:1213
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
static uint32_t AsciiToIpv4Host(char const *address)
Definition: ipv4-address.cc:35
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
uint32_t m_mask
Definition: ipv4-address.h:277
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
Definition: address.cc:82
bool IsEqual(Ipv4Mask other) const
Definition: ipv4-address.cc:99
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.cc:89
static uint8_t GetType(void)
NS_LOG_COMPONENT_DEFINE("Ipv4Address")
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