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