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 constexpr 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 * A mask must have all the 1 bits contiguous, i.e., "255.255.0.255" is not
255 * valid, in accordance with \RFC{4632}. Note that in \RFC{950} a non-contiguous
256 * mask was not forbidden, but discouraged. Nowadays it's forbidden.
257 *
258 * @see attribute_Ipv4Mask
259 */
261{
262 public:
263 /**
264 * Will initialize to a zero-length mask, which will match any address.
265 */
266 Ipv4Mask() = default;
267 /**
268 * @param mask bitwise integer representation of the mask
269 *
270 * For example, the integer input 0xffffff00 yields a 24-bit mask
271 */
272 Ipv4Mask(uint32_t mask);
273 /**
274 * @param mask String constant either in "255.255.255.0" or "/24" format
275 */
276 Ipv4Mask(const char* mask);
277 /**
278 * @param a first address to compare
279 * @param b second address to compare
280 * @return true if both addresses are equal in their masked bits,
281 * corresponding to this mask
282 */
283 bool IsMatch(Ipv4Address a, Ipv4Address b) const;
284 /**
285 * Get the host-order 32-bit IP mask
286 * @return the host-order 32-bit IP mask
287 */
288 uint32_t Get() const;
289 /**
290 * input mask is in host order.
291 * @param mask The host order 32-bit mask
292 */
293 void Set(uint32_t mask);
294 /**
295 * @brief Return the inverse mask in host order.
296 * @return The inverse mask
297 */
298 uint32_t GetInverse() const;
299 /**
300 * @brief Print this mask to the given output stream
301 *
302 * The print format is in the typical "255.255.255.0"
303 * @param os The output stream to which this Ipv4Address is printed
304 */
305 void Print(std::ostream& os) const;
306 /**
307 * @return the prefix length of mask (the yy in x.x.x.x/yy notation)
308 */
309 uint16_t GetPrefixLength() const;
310 /**
311 * @return the 255.0.0.0 mask corresponding to a typical loopback address
312 */
313 static Ipv4Mask GetLoopback();
314 /**
315 * @return the 0.0.0.0 mask
316 */
317 static Ipv4Mask GetZero();
318 /**
319 * @return the 255.255.255.255 mask
320 */
321 static Ipv4Mask GetOnes();
322
323 /**
324 * @brief Three-way comparison operator.
325 *
326 * @param a the other mask to compare with
327 * @returns comparison result
328 */
329 constexpr std::strong_ordering operator<=>(const Ipv4Mask& a) const = default;
330
331 private:
332 /**
333 * @brief Checks if the mask is valid, i.e., if it's in the form n-1 + m-0.
334 *
335 * This function is used in the constructors, to prevent misuse.
336 *
337 * @returns true if the mask is valid
338 */
339 constexpr bool IsValid() const;
340
341 uint32_t m_mask{0}; //!< IP mask
342};
343
346
347/**
348 * @brief Stream insertion operator.
349 *
350 * @param os the stream
351 * @param address the address
352 * @returns a reference to the stream
353 */
354std::ostream& operator<<(std::ostream& os, const Ipv4Address& address);
355/**
356 * @brief Stream insertion operator.
357 *
358 * @param os the stream
359 * @param mask the mask
360 * @returns a reference to the stream
361 */
362std::ostream& operator<<(std::ostream& os, const Ipv4Mask& mask);
363/**
364 * @brief Stream extraction operator.
365 *
366 * @param is the stream
367 * @param address the address
368 * @returns a reference to the stream
369 */
370std::istream& operator>>(std::istream& is, Ipv4Address& address);
371/**
372 * @brief Stream extraction operator.
373 *
374 * @param is the stream
375 * @param mask the mask
376 * @returns a reference to the stream
377 */
378std::istream& operator>>(std::istream& is, Ipv4Mask& mask);
379
380/**
381 * @ingroup address
382 *
383 * @brief Class providing an hash for IPv4 addresses
384 */
385class NS_DEPRECATED_3_47("Unnecessary thanks to std::hash specialization, remove") Ipv4AddressHash
386{
387 public:
388 /**
389 * @brief Returns the hash of an IPv4 address.
390 * @param x the address
391 * @return the hash
392 *
393 * This method uses std::hash rather than class Hash
394 * as speed is more important than cryptographic robustness.
395 */
396 size_t operator()(const Ipv4Address& x) const;
397};
398
399} // namespace ns3
400
401namespace std
402{
403
404/**
405 * @brief Hash function class for IPv4 addresses.
406 */
407template <>
408struct hash<ns3::Ipv4Address>
409{
410 /**
411 * @brief Returns the hash of an IPv4 address.
412 * @param addr IPv4 address to hash
413 * @returns the hash of the address
414 */
415 size_t operator()(const ns3::Ipv4Address& addr) const
416 {
417 return std::hash<uint32_t>()(addr.Get());
418 }
419};
420} // namespace std
421
422#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
constexpr std::strong_ordering operator<=>(const Ipv4Mask &a) const =default
Three-way comparison operator.
uint32_t m_mask
IP mask.
constexpr bool IsValid() const
Checks if the mask is valid, i.e., if it's in the form n-1 + m-0.
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.
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:98
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.