A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ipv4-address.h
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#ifndef IPV4_ADDRESS_H
10#define IPV4_ADDRESS_H
11
12#include "ns3/address.h"
13#include "ns3/attribute-helper.h"
14#include "ns3/deprecated.h"
15
16#include <ostream>
17#include <stdint.h>
18
19namespace ns3
20{
21
22class Ipv4Mask;
23
24/**
25 * @ingroup address
26 *
27 * @brief Ipv4 addresses are stored in host order in this class.
28 *
29 * @see attribute_Ipv4Address
30 */
32{
33 public:
34 Ipv4Address() = default;
35 /**
36 * input address is in host order.
37 * @param address The host order 32-bit address
38 */
39 explicit Ipv4Address(uint32_t address);
40 /**
41 * @brief Constructs an Ipv4Address by parsing a the input C-string
42 *
43 * Input address is in format:
44 * \c hhh.xxx.xxx.lll
45 * where \c h is the high byte and \c l the
46 * low byte
47 *
48 * @param address C-string containing the address as described above
49 */
50 Ipv4Address(const char* address);
51
52 /**
53 * @brief Checks if the string contains an Ipv4Address
54 *
55 * Input address is in format:
56 * \c hhh.xxx.xxx.lll
57 * where \c h is the high byte and \c l the
58 * low byte
59 *
60 * Note: the function uses ``inet_pton`` internally.
61 *
62 * @see Address::CheckCompatible hich has a similar name but which
63 * instead checks the underlying type and length embedded in the Address.
64 *
65 * @param addressStr string containing the address as described above
66 * @return true if the string can be parsed as an IPv4 address
67 */
68 static bool CheckCompatible(const std::string& addressStr);
69 /**
70 * Get the host-order 32-bit IP address
71 * @return the host-order 32-bit IP address
72 */
73 uint32_t Get() const;
74 /**
75 * input address is in host order.
76 * @param address The host order 32-bit address
77 */
78 void Set(uint32_t address);
79 /**
80 * @brief Sets an Ipv4Address by parsing a the input C-string
81 *
82 * Input address is in format:
83 * \c hhh.xxx.xxx.lll
84 * where \c h is the high byte and \c l the
85 * low byte
86 * @param address C-string containing the address as described above
87 */
88 void Set(const char* address);
89 /**
90 * Serialize this address to a 4-byte buffer
91 *
92 * @param buf output buffer to which this address gets overwritten with this
93 * Ipv4Address
94 */
95 void Serialize(uint8_t buf[4]) const;
96 /**
97 * @param buf buffer to read address from
98 * @return an Ipv4Address
99 *
100 * The input address is expected to be in network byte order format.
101 */
102 static Ipv4Address Deserialize(const uint8_t buf[4]);
103 /**
104 * @brief Print this address to the given output stream
105 *
106 * The print format is in the typical "192.168.1.1"
107 * @param os The output stream to which this Ipv4Address is printed
108 */
109 void Print(std::ostream& os) const;
110
111 /**
112 * @return true if address is initialized (i.e., set to something), false otherwise
113 */
114 NS_DEPRECATED_3_47("Use IsAny or std::optional")
115 bool IsInitialized() const;
116 /**
117 * @return true if address is 0.0.0.0; false otherwise
118 */
119 bool IsAny() const;
120 /**
121 * @return true if address is 127.0.0.1; false otherwise
122 */
123 bool IsLocalhost() const;
124 /**
125 * @return true if address is 255.255.255.255; false otherwise
126 */
127 bool IsBroadcast() const;
128 /**
129 * @return true only if address is in the range 224.0.0.0 - 239.255.255.255
130 */
131 bool IsMulticast() const;
132 /**
133 * @return true only if address is in local multicast address scope, 224.0.0.0/24
134 */
135 bool IsLocalMulticast() const;
136 /**
137 * @brief If the IPv4 address is an APIPA address (169.254/16).
138 *
139 * The Automatic Private IP Address is described in \RFC{3927}
140 *
141 * @return true if the address is link-local, false otherwise
142 */
143 bool IsLinkLocal() const;
144 /**
145 * @brief Combine this address with a network mask
146 *
147 * This method returns an IPv4 address that is this address combined
148 * (bitwise and) with a network mask, yielding an IPv4 network
149 * address.
150 *
151 * @param mask a network mask
152 * @returns the address combined with the mask
153 */
154 Ipv4Address CombineMask(const Ipv4Mask& mask) const;
155 /**
156 * @brief Generate subnet-directed broadcast address corresponding to mask
157 *
158 * The subnet-directed broadcast address has the host bits set to all
159 * ones. If this method is called with a mask of 255.255.255.255,
160 * (i.e., the address is a /32 address), the program will assert, since
161 * there is no subnet associated with a /32 address.
162 *
163 * @param mask a network mask
164 * @returns a broadcast address for the subnet.
165 */
167 /**
168 * @brief Generate subnet-directed broadcast address corresponding to mask
169 *
170 * The subnet-directed broadcast address has the host bits set to all
171 * ones. If this method is called with a mask of 255.255.255.255,
172 * (i.e., the address is a /32 address), the program will assert, since
173 * there is no subnet associated with a /32 address.
174 *
175 * @param mask a network mask
176 * @return true if the address, when combined with the input mask, has all
177 * of its host bits set to one
178 */
179 bool IsSubnetDirectedBroadcast(const Ipv4Mask& mask) const;
180 /**
181 * @param address an address to compare type with
182 *
183 * @return true if the type of the address stored internally
184 * is compatible with the type of the input address, false otherwise.
185 */
186 static bool IsMatchingType(const Address& address);
187 /**
188 * Convert an instance of this class to a polymorphic Address instance.
189 *
190 * @return a new Address instance
191 */
192 operator Address() const;
193 /**
194 * @param address a polymorphic address
195 * @return a new Ipv4Address from the polymorphic address
196 *
197 * This function performs a type check and asserts if the
198 * type of the input address is not compatible with an
199 * Ipv4Address.
200 */
201 static Ipv4Address ConvertFrom(const Address& address);
202 /**
203 * @brief Convert to an Address type
204 * @return the Address corresponding to this object.
205 */
206 Address ConvertTo() const;
207
208 /**
209 * @return the 0.0.0.0 address
210 */
211 static Ipv4Address GetZero();
212 /**
213 * @return the 0.0.0.0 address
214 */
215 static Ipv4Address GetAny();
216 /**
217 * @return the 255.255.255.255 address
218 */
219 static Ipv4Address GetBroadcast();
220 /**
221 * @return the 127.0.0.1 address
222 */
223 static Ipv4Address GetLoopback();
224
225 private:
226 /**
227 * @brief Get the underlying address type (automatically assigned).
228 *
229 * @returns the address type
230 */
231 static uint8_t GetType();
232 uint32_t m_address{0}; //!< IPv4 address
233
234 /**
235 * @brief Equal to operator.
236 *
237 * @param a the first operand.
238 * @param b the first operand.
239 * @returns true if the operands are equal.
240 */
241 friend bool operator==(const Ipv4Address& a, const Ipv4Address& b);
242
243 /**
244 * @brief Not equal to operator.
245 *
246 * @param a the first operand.
247 * @param b the first operand.
248 * @returns true if the operands are not equal.
249 */
250 friend bool operator!=(const Ipv4Address& a, const Ipv4Address& b);
251
252 /**
253 * @brief Less than to operator.
254 *
255 * @param a the first operand.
256 * @param b the first operand.
257 * @returns true if the first operand is less than the second.
258 */
259 friend bool operator<(const Ipv4Address& a, const Ipv4Address& b);
260};
261
262/**
263 * @ingroup address
264 *
265 * @brief a class to represent an Ipv4 address mask
266 *
267 * The constructor takes arguments according to a few formats.
268 * Ipv4Mask ("255.255.255.255"), Ipv4Mask ("/32"), and Ipv4Mask (0xffffffff)
269 * are all equivalent.
270 *
271 * @see attribute_Ipv4Mask
272 */
274{
275 public:
276 /**
277 * Will initialize to a zero-length mask, which will match any address.
278 */
279 Ipv4Mask();
280 /**
281 * @param mask bitwise integer representation of the mask
282 *
283 * For example, the integer input 0xffffff00 yields a 24-bit mask
284 */
285 Ipv4Mask(uint32_t mask);
286 /**
287 * @param mask String constant either in "255.255.255.0" or "/24" format
288 */
289 Ipv4Mask(const char* mask);
290 /**
291 * @param a first address to compare
292 * @param b second address to compare
293 * @return true if both addresses are equal in their masked bits,
294 * corresponding to this mask
295 */
296 bool IsMatch(Ipv4Address a, Ipv4Address b) const;
297 /**
298 * Get the host-order 32-bit IP mask
299 * @return the host-order 32-bit IP mask
300 */
301 uint32_t Get() const;
302 /**
303 * input mask is in host order.
304 * @param mask The host order 32-bit mask
305 */
306 void Set(uint32_t mask);
307 /**
308 * @brief Return the inverse mask in host order.
309 * @return The inverse mask
310 */
311 uint32_t GetInverse() const;
312 /**
313 * @brief Print this mask to the given output stream
314 *
315 * The print format is in the typical "255.255.255.0"
316 * @param os The output stream to which this Ipv4Address is printed
317 */
318 void Print(std::ostream& os) const;
319 /**
320 * @return the prefix length of mask (the yy in x.x.x.x/yy notation)
321 */
322 uint16_t GetPrefixLength() const;
323 /**
324 * @return the 255.0.0.0 mask corresponding to a typical loopback address
325 */
326 static Ipv4Mask GetLoopback();
327 /**
328 * @return the 0.0.0.0 mask
329 */
330 static Ipv4Mask GetZero();
331 /**
332 * @return the 255.255.255.255 mask
333 */
334 static Ipv4Mask GetOnes();
335
336 /**
337 * @brief Equal to operator.
338 *
339 * @param a the first operand.
340 * @param b the first operand.
341 * @returns true if the operands are equal.
342 */
343 friend bool operator==(const Ipv4Mask& a, const Ipv4Mask& b);
344
345 /**
346 * @brief Not equal to operator.
347 *
348 * @param a the first operand.
349 * @param b the first operand.
350 * @returns true if the operands are not equal.
351 */
352 friend bool operator!=(const Ipv4Mask& a, const Ipv4Mask& b);
353
354 private:
355 uint32_t m_mask{0}; //!< IP mask
356};
357
360
361/**
362 * @brief Stream insertion operator.
363 *
364 * @param os the stream
365 * @param address the address
366 * @returns a reference to the stream
367 */
368std::ostream& operator<<(std::ostream& os, const Ipv4Address& address);
369/**
370 * @brief Stream insertion operator.
371 *
372 * @param os the stream
373 * @param mask the mask
374 * @returns a reference to the stream
375 */
376std::ostream& operator<<(std::ostream& os, const Ipv4Mask& mask);
377/**
378 * @brief Stream extraction operator.
379 *
380 * @param is the stream
381 * @param address the address
382 * @returns a reference to the stream
383 */
384std::istream& operator>>(std::istream& is, Ipv4Address& address);
385/**
386 * @brief Stream extraction operator.
387 *
388 * @param is the stream
389 * @param mask the mask
390 * @returns a reference to the stream
391 */
392std::istream& operator>>(std::istream& is, Ipv4Mask& mask);
393
394inline bool
396{
397 return a.m_address == b.m_address;
398}
399
400inline bool
402{
403 return a.m_address != b.m_address;
404}
405
406inline bool
407operator<(const Ipv4Address& a, const Ipv4Address& b)
408{
409 return a.m_address < b.m_address;
410}
411
412/**
413 * @ingroup address
414 *
415 * @brief Class providing an hash for IPv4 addresses
416 */
418{
419 public:
420 /**
421 * @brief Returns the hash of an IPv4 address.
422 * @param x the address
423 * @return the hash
424 *
425 * This method uses std::hash rather than class Hash
426 * as speed is more important than cryptographic robustness.
427 */
428 size_t operator()(const Ipv4Address& x) const;
429};
430
431inline bool
432operator==(const Ipv4Mask& a, const Ipv4Mask& b)
433{
434 return a.m_mask == b.m_mask;
435}
436
437inline bool
438operator!=(const Ipv4Mask& a, const Ipv4Mask& b)
439{
440 return a.m_mask != b.m_mask;
441}
442
443} // namespace ns3
444
445#endif /* IPV4_ADDRESS_H */
cairo_uint64_t x
_cairo_uint_96by64_32x64_divrem:
a polymophic address class
Definition address.h:111
Class providing an hash for IPv4 addresses.
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
friend bool operator!=(const Ipv4Address &a, const Ipv4Address &b)
Not equal to operator.
friend bool operator<(const Ipv4Address &a, const Ipv4Address &b)
Less than to operator.
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).
friend bool operator==(const Ipv4Address &a, const Ipv4Address &b)
Equal to operator.
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.
friend bool operator!=(const Ipv4Mask &a, const Ipv4Mask &b)
Not equal to operator.
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.
friend bool operator==(const Ipv4Mask &a, const Ipv4Mask &b)
Equal to operator.
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 ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type.
#define NS_DEPRECATED_3_47(msg)
Tag for things deprecated in version ns-3.47.
Definition deprecated.h:91
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition callback.h:658
bool operator==(const EventId &a, const EventId &b)
Definition event-id.h:146
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
bool operator<(const EventId &a, const EventId &b)
Definition event-id.h:159
#define private