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