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
140{
141 NS_LOG_FUNCTION(this << address);
142 m_address = address;
143}
144
145Ipv4Address::Ipv4Address(const char* address)
146{
147 NS_LOG_FUNCTION(this << address);
148
149 if (inet_pton(AF_INET, address, &m_address) <= 0)
150 {
151 NS_ABORT_MSG("Error, can not build an IPv4 address from an invalid string: " << address);
152 return;
153 }
154 m_address = ntohl(m_address);
155}
156
157bool
158Ipv4Address::CheckCompatible(const std::string& addressStr)
159{
160 NS_LOG_FUNCTION(addressStr);
161
162 uint32_t buffer;
163
164 if (inet_pton(AF_INET, addressStr.c_str(), &buffer) <= 0)
165 {
166 NS_LOG_WARN("Error, can not build an IPv4 address from an invalid string: " << addressStr);
167 return false;
168 }
169 return true;
170}
171
174{
175 NS_LOG_FUNCTION(this);
176 return m_address;
177}
178
179void
181{
182 NS_LOG_FUNCTION(this << address);
183 m_address = address;
184}
185
186void
187Ipv4Address::Set(const char* address)
188{
189 NS_LOG_FUNCTION(this << address);
190 if (inet_pton(AF_INET, address, &m_address) <= 0)
191 {
192 NS_ABORT_MSG("Error, can not build an IPv4 address from an invalid string: " << address);
193 return;
194 }
195 m_address = ntohl(m_address);
196}
197
200{
201 NS_LOG_FUNCTION(this << mask);
202 return Ipv4Address(Get() & mask.Get());
203}
204
207{
208 NS_LOG_FUNCTION(this << mask);
209 if (mask == Ipv4Mask::GetOnes())
210 {
211 NS_ASSERT_MSG(false,
212 "Trying to get subnet-directed broadcast address with an all-ones netmask");
213 }
214 return Ipv4Address(Get() | mask.GetInverse());
215}
216
217bool
219{
220 NS_LOG_FUNCTION(this << mask);
221 if (mask == Ipv4Mask::GetOnes())
222 {
223 // If the mask is 255.255.255.255, there is no subnet directed
224 // broadcast for this address.
225 return false;
226 }
227 return ((Get() | mask.Get()) == Ipv4Address::GetBroadcast().Get());
228}
229
230bool
232{
233 NS_LOG_FUNCTION(this);
234 return true;
235}
236
237bool
239{
240 NS_LOG_FUNCTION(this);
241 return (m_address == 0x00000000U);
242}
243
244bool
246{
247 NS_LOG_FUNCTION(this);
248 return (m_address == 0x7f000001U);
249}
250
251bool
253{
254 NS_LOG_FUNCTION(this);
255 return (m_address == 0xffffffffU);
256}
257
258bool
260{
261 //
262 // Multicast addresses are defined as ranging from 224.0.0.0 through
263 // 239.255.255.255 (which is E0000000 through EFFFFFFF in hex).
264 //
265 NS_LOG_FUNCTION(this);
266 return (m_address >= 0xe0000000 && m_address <= 0xefffffff);
267}
268
269bool
271{
272 NS_LOG_FUNCTION(this);
273 // Link-Local multicast address is 224.0.0.0/24
274 return (m_address & 0xffffff00) == 0xe0000000;
275}
276
277bool
279{
280 NS_LOG_FUNCTION(this);
281 // Link-Local address is 169.254.0.0/16
282 return (m_address & 0xffff0000) == 0xa9fe0000;
283}
284
285void
286Ipv4Address::Serialize(uint8_t buf[4]) const
287{
288 NS_LOG_FUNCTION(this << &buf);
289 buf[0] = (m_address >> 24) & 0xff;
290 buf[1] = (m_address >> 16) & 0xff;
291 buf[2] = (m_address >> 8) & 0xff;
292 buf[3] = (m_address >> 0) & 0xff;
293}
294
296Ipv4Address::Deserialize(const uint8_t buf[4])
297{
298 NS_LOG_FUNCTION(&buf);
299 Ipv4Address ipv4;
300 ipv4.m_address = 0;
301 ipv4.m_address |= buf[0];
302 ipv4.m_address <<= 8;
303 ipv4.m_address |= buf[1];
304 ipv4.m_address <<= 8;
305 ipv4.m_address |= buf[2];
306 ipv4.m_address <<= 8;
307 ipv4.m_address |= buf[3];
308
309 return ipv4;
310}
311
312void
313Ipv4Address::Print(std::ostream& os) const
314{
315 NS_LOG_FUNCTION(this);
316 os << ((m_address >> 24) & 0xff) << "." << ((m_address >> 16) & 0xff) << "."
317 << ((m_address >> 8) & 0xff) << "." << ((m_address >> 0) & 0xff);
318}
319
320bool
322{
323 NS_LOG_FUNCTION(&address);
324 return address.CheckCompatible(GetType(), 4);
325}
326
327Ipv4Address::
328operator Address() const
329{
330 return ConvertTo();
331}
332
335{
336 NS_LOG_FUNCTION(this);
337 uint8_t buf[4];
338 Serialize(buf);
339 return Address(GetType(), buf, 4);
340}
341
344{
345 NS_LOG_FUNCTION(&address);
346 NS_ASSERT(address.CheckCompatible(GetType(), 4));
347 uint8_t buf[4];
348 address.CopyTo(buf);
349 return Deserialize(buf);
350}
351
352uint8_t
354{
356 static uint8_t type = Address::Register("IpAddress", 4);
357 return type;
358}
359
362{
364 static Ipv4Address zero("0.0.0.0");
365 return zero;
366}
367
370{
372 static Ipv4Address any("0.0.0.0");
373 return any;
374}
375
378{
380 static Ipv4Address broadcast("255.255.255.255");
381 return broadcast;
382}
383
386{
388 Ipv4Address loopback("127.0.0.1");
389 return loopback;
390}
391
392size_t
394{
395 return std::hash<uint32_t>()(x.Get());
396}
397
398std::ostream&
399operator<<(std::ostream& os, const Ipv4Address& address)
400{
401 address.Print(os);
402 return os;
403}
404
405std::ostream&
406operator<<(std::ostream& os, const Ipv4Mask& mask)
407{
408 mask.Print(os);
409 return os;
410}
411
412std::istream&
413operator>>(std::istream& is, Ipv4Address& address)
414{
415 std::string str;
416 is >> str;
417 address = Ipv4Address(str.c_str());
418 return is;
419}
420
421std::istream&
422operator>>(std::istream& is, Ipv4Mask& mask)
423{
424 std::string str;
425 is >> str;
426 mask = Ipv4Mask(str.c_str());
427 return is;
428}
429
432
433} // namespace ns3
cairo_uint64_t x
_cairo_uint_96by64_32x64_divrem:
a polymophic address class
Definition address.h:114
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:130
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.
Ipv4Address()=default
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).
uint32_t m_address
IPv4 address.
Address ConvertTo() const
Convert to an Address type.
bool IsBroadcast() const
static Ipv4Address GetAny()
static bool CheckCompatible(const std::string &addressStr)
Checks if the string contains an Ipv4Address.
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:194
#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 NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:253
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