A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-address.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
9#include "ipv4-address.h"
10
11#include "ns3/assert.h"
12#include "ns3/log.h"
13
14#include <cstdlib>
15
16#ifdef __WIN32__
17#include <WS2tcpip.h>
18#else
19#include <arpa/inet.h>
20#include <sys/socket.h>
21#endif
22
23namespace ns3
24{
25
26NS_LOG_COMPONENT_DEFINE("Ipv4Address");
27
32
34 : m_mask(mask)
35{
36 NS_LOG_FUNCTION(this << mask);
37}
38
39Ipv4Mask::Ipv4Mask(const char* mask)
40{
41 NS_LOG_FUNCTION(this << mask);
42 if (*mask == '/')
43 {
44 auto plen = static_cast<uint32_t>(std::atoi(++mask));
45 NS_ASSERT(plen <= 32);
46 if (plen > 0)
47 {
48 m_mask = 0xffffffff << (32 - plen);
49 }
50 else
51 {
52 m_mask = 0;
53 }
54 }
55 else
56 {
57 if (inet_pton(AF_INET, mask, &m_mask) <= 0)
58 {
59 NS_ABORT_MSG("Error, can not build an IPv4 mask from an invalid string: " << mask);
60 }
61 m_mask = ntohl(m_mask);
62 }
63}
64
65bool
67{
68 NS_LOG_FUNCTION(this << a << b);
69 return (a.Get() & m_mask) == (b.Get() & m_mask);
70}
71
74{
75 NS_LOG_FUNCTION(this);
76 return m_mask;
77}
78
79void
81{
82 NS_LOG_FUNCTION(this << mask);
83 m_mask = mask;
84}
85
88{
89 NS_LOG_FUNCTION(this);
90 return ~m_mask;
91}
92
93void
94Ipv4Mask::Print(std::ostream& os) const
95{
96 NS_LOG_FUNCTION(this << &os);
97 os << ((m_mask >> 24) & 0xff) << "." << ((m_mask >> 16) & 0xff) << "." << ((m_mask >> 8) & 0xff)
98 << "." << ((m_mask >> 0) & 0xff);
99}
100
103{
105 static Ipv4Mask loopback("255.0.0.0");
106 return loopback;
107}
108
111{
113 static Ipv4Mask zero("0.0.0.0");
114 return zero;
115}
116
119{
121 static Ipv4Mask ones("255.255.255.255");
122 return ones;
123}
124
125uint16_t
127{
128 NS_LOG_FUNCTION(this);
129 uint16_t tmp = 0;
130 uint32_t mask = m_mask;
131 while (mask != 0)
132 {
133 mask = mask << 1;
134 tmp++;
135 }
136 return tmp;
137}
138
139/**
140 * Value of a not-yet-initialized IPv4 address, corresponding to 102.102.102.102.
141 * This is totally arbitrary.
142 */
143static constexpr uint32_t UNINITIALIZED = 0x66666666U;
144
151
153{
154 NS_LOG_FUNCTION(this << address);
155 m_address = address;
156 m_initialized = true;
157}
158
159Ipv4Address::Ipv4Address(const char* address)
160{
161 NS_LOG_FUNCTION(this << address);
162
163 if (inet_pton(AF_INET, address, &m_address) <= 0)
164 {
165 NS_LOG_LOGIC("Error, can not build an IPv4 address from an invalid string: " << address);
166 m_address = 0;
167 m_initialized = false;
168 return;
169 }
170 m_initialized = true;
171 m_address = ntohl(m_address);
172}
173
176{
177 NS_LOG_FUNCTION(this);
178 return m_address;
179}
180
181void
183{
184 NS_LOG_FUNCTION(this << address);
185 m_address = address;
186 m_initialized = true;
187}
188
189void
190Ipv4Address::Set(const char* address)
191{
192 NS_LOG_FUNCTION(this << address);
193 if (inet_pton(AF_INET, address, &m_address) <= 0)
194 {
195 NS_LOG_LOGIC("Error, can not build an IPv4 address from an invalid string: " << address);
196 m_address = 0;
197 m_initialized = false;
198 return;
199 }
200 m_initialized = true;
201 m_address = ntohl(m_address);
202}
203
206{
207 NS_LOG_FUNCTION(this << mask);
208 return Ipv4Address(Get() & mask.Get());
209}
210
213{
214 NS_LOG_FUNCTION(this << mask);
215 if (mask == Ipv4Mask::GetOnes())
216 {
217 NS_ASSERT_MSG(false,
218 "Trying to get subnet-directed broadcast address with an all-ones netmask");
219 }
220 return Ipv4Address(Get() | mask.GetInverse());
221}
222
223bool
225{
226 NS_LOG_FUNCTION(this << mask);
227 if (mask == Ipv4Mask::GetOnes())
228 {
229 // If the mask is 255.255.255.255, there is no subnet directed
230 // broadcast for this address.
231 return false;
232 }
233 return ((Get() | mask.Get()) == Ipv4Address::GetBroadcast().Get());
234}
235
236bool
238{
239 NS_LOG_FUNCTION(this);
240 return m_initialized;
241}
242
243bool
245{
246 NS_LOG_FUNCTION(this);
247 return (m_address == 0x00000000U);
248}
249
250bool
252{
253 NS_LOG_FUNCTION(this);
254 return (m_address == 0x7f000001U);
255}
256
257bool
259{
260 NS_LOG_FUNCTION(this);
261 return (m_address == 0xffffffffU);
262}
263
264bool
266{
267 //
268 // Multicast addresses are defined as ranging from 224.0.0.0 through
269 // 239.255.255.255 (which is E0000000 through EFFFFFFF in hex).
270 //
271 NS_LOG_FUNCTION(this);
272 return (m_address >= 0xe0000000 && m_address <= 0xefffffff);
273}
274
275bool
277{
278 NS_LOG_FUNCTION(this);
279 // Link-Local multicast address is 224.0.0.0/24
280 return (m_address & 0xffffff00) == 0xe0000000;
281}
282
283bool
285{
286 NS_LOG_FUNCTION(this);
287 // Link-Local address is 169.254.0.0/16
288 return (m_address & 0xffff0000) == 0xa9fe0000;
289}
290
291void
292Ipv4Address::Serialize(uint8_t buf[4]) const
293{
294 NS_LOG_FUNCTION(this << &buf);
295 buf[0] = (m_address >> 24) & 0xff;
296 buf[1] = (m_address >> 16) & 0xff;
297 buf[2] = (m_address >> 8) & 0xff;
298 buf[3] = (m_address >> 0) & 0xff;
299}
300
302Ipv4Address::Deserialize(const uint8_t buf[4])
303{
304 NS_LOG_FUNCTION(&buf);
305 Ipv4Address ipv4;
306 ipv4.m_address = 0;
307 ipv4.m_address |= buf[0];
308 ipv4.m_address <<= 8;
309 ipv4.m_address |= buf[1];
310 ipv4.m_address <<= 8;
311 ipv4.m_address |= buf[2];
312 ipv4.m_address <<= 8;
313 ipv4.m_address |= buf[3];
314 ipv4.m_initialized = true;
315
316 return ipv4;
317}
318
319void
320Ipv4Address::Print(std::ostream& os) const
321{
322 NS_LOG_FUNCTION(this);
323 os << ((m_address >> 24) & 0xff) << "." << ((m_address >> 16) & 0xff) << "."
324 << ((m_address >> 8) & 0xff) << "." << ((m_address >> 0) & 0xff);
325}
326
327bool
329{
330 NS_LOG_FUNCTION(&address);
331 return address.CheckCompatible(GetType(), 4);
332}
333
334Ipv4Address::
335operator Address() const
336{
337 return ConvertTo();
338}
339
342{
343 NS_LOG_FUNCTION(this);
344 uint8_t buf[4];
345 Serialize(buf);
346 return Address(GetType(), buf, 4);
347}
348
351{
352 NS_LOG_FUNCTION(&address);
353 NS_ASSERT(address.CheckCompatible(GetType(), 4));
354 uint8_t buf[4];
355 address.CopyTo(buf);
356 return Deserialize(buf);
357}
358
359uint8_t
361{
363 static uint8_t type = Address::Register("IpAddress", 4);
364 return type;
365}
366
369{
371 static Ipv4Address zero("0.0.0.0");
372 return zero;
373}
374
377{
379 static Ipv4Address any("0.0.0.0");
380 return any;
381}
382
385{
387 static Ipv4Address broadcast("255.255.255.255");
388 return broadcast;
389}
390
393{
395 Ipv4Address loopback("127.0.0.1");
396 return loopback;
397}
398
399size_t
401{
402 return std::hash<uint32_t>()(x.Get());
403}
404
405std::ostream&
406operator<<(std::ostream& os, const Ipv4Address& address)
407{
408 address.Print(os);
409 return os;
410}
411
412std::ostream&
413operator<<(std::ostream& os, const Ipv4Mask& mask)
414{
415 mask.Print(os);
416 return os;
417}
418
419std::istream&
420operator>>(std::istream& is, Ipv4Address& address)
421{
422 std::string str;
423 is >> str;
424 address = Ipv4Address(str.c_str());
425 return is;
426}
427
428std::istream&
429operator>>(std::istream& is, Ipv4Mask& mask)
430{
431 std::string str;
432 is >> str;
433 mask = Ipv4Mask(str.c_str());
434 return is;
435}
436
439
440} // namespace ns3
cairo_uint64_t x
_cairo_uint_96by64_32x64_divrem:
a polymophic address class
Definition address.h:111
static uint8_t Register(const std::string &kind, uint8_t length)
Allocate a new type id for a new type of address.
Definition address.cc:156
size_t operator()(const Ipv4Address &x) const
Returns the hash of an IPv4 address.
Ipv4 addresses are stored in host order in this class.
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.
uint32_t m_address
IPv4 address.
Address ConvertTo() const
Convert to an Address type.
bool IsBroadcast() const
static Ipv4Address GetAny()
bool IsInitialized() const
bool IsLocalMulticast() const
bool IsLinkLocal() const
If the IPv4 address is an APIPA address (169.254/16).
a class to represent an Ipv4 address mask
uint32_t m_mask
IP mask.
static Ipv4Mask GetOnes()
void Set(uint32_t mask)
input mask is in host order.
Ipv4Mask()
Will initialize to a zero-length mask, which will match any address.
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()
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#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:75
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition log.h:271
#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 ",...
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:148
std::istream & operator>>(std::istream &is, Angles &a)
Definition angles.cc:172
static constexpr uint32_t UNINITIALIZED
Value of a not-yet-initialized IPv4 address, corresponding to 102.102.102.102.