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