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 Combine this address with a network mask
117 *
118 * This method returns an IPv4 address that is this address combined
119 * (bitwise and) with a network mask, yielding an IPv4 network
120 * address.
121 *
122 * @param mask a network mask
123 * @returns the address combined with the mask
124 */
125 Ipv4Address CombineMask(const Ipv4Mask& mask) const;
126 /**
127 * @brief Generate subnet-directed broadcast address corresponding to mask
128 *
129 * The subnet-directed broadcast address has the host bits set to all
130 * ones. If this method is called with a mask of 255.255.255.255,
131 * (i.e., the address is a /32 address), the program will assert, since
132 * there is no subnet associated with a /32 address.
133 *
134 * @param mask a network mask
135 * @returns a broadcast address for the subnet.
136 */
138 /**
139 * @brief Generate subnet-directed broadcast address corresponding to mask
140 *
141 * The subnet-directed broadcast address has the host bits set to all
142 * ones. If this method is called with a mask of 255.255.255.255,
143 * (i.e., the address is a /32 address), the program will assert, since
144 * there is no subnet associated with a /32 address.
145 *
146 * @param mask a network mask
147 * @return true if the address, when combined with the input mask, has all
148 * of its host bits set to one
149 */
150 bool IsSubnetDirectedBroadcast(const Ipv4Mask& mask) const;
151 /**
152 * @param address an address to compare type with
153 *
154 * @return true if the type of the address stored internally
155 * is compatible with the type of the input address, false otherwise.
156 */
157 static bool IsMatchingType(const Address& address);
158 /**
159 * Convert an instance of this class to a polymorphic Address instance.
160 *
161 * @return a new Address instance
162 */
163 operator Address() const;
164 /**
165 * @param address a polymorphic address
166 * @return a new Ipv4Address from the polymorphic address
167 *
168 * This function performs a type check and asserts if the
169 * type of the input address is not compatible with an
170 * Ipv4Address.
171 */
172 static Ipv4Address ConvertFrom(const Address& address);
173 /**
174 * @brief Convert to an Address type
175 * @return the Address corresponding to this object.
176 */
177 Address ConvertTo() const;
178
179 /**
180 * @return the 0.0.0.0 address
181 */
182 static Ipv4Address GetZero();
183 /**
184 * @return the 0.0.0.0 address
185 */
186 static Ipv4Address GetAny();
187 /**
188 * @return the 255.255.255.255 address
189 */
190 static Ipv4Address GetBroadcast();
191 /**
192 * @return the 127.0.0.1 address
193 */
194 static Ipv4Address GetLoopback();
195
196 private:
197 /**
198 * @brief Get the underlying address type (automatically assigned).
199 *
200 * @returns the address type
201 */
202 static uint8_t GetType();
203 uint32_t m_address; //!< IPv4 address
204 bool m_initialized; //!< IPv4 address has been explicitly initialized to a valid value.
205
206 /**
207 * @brief Equal to operator.
208 *
209 * @param a the first operand.
210 * @param b the first operand.
211 * @returns true if the operands are equal.
212 */
213 friend bool operator==(const Ipv4Address& a, const Ipv4Address& b);
214
215 /**
216 * @brief Not equal to operator.
217 *
218 * @param a the first operand.
219 * @param b the first operand.
220 * @returns true if the operands are not equal.
221 */
222 friend bool operator!=(const Ipv4Address& a, const Ipv4Address& b);
223
224 /**
225 * @brief Less than to operator.
226 *
227 * @param a the first operand.
228 * @param b the first operand.
229 * @returns true if the first operand is less than the second.
230 */
231 friend bool operator<(const Ipv4Address& a, const Ipv4Address& b);
232};
233
234/**
235 * @ingroup address
236 *
237 * @brief a class to represent an Ipv4 address mask
238 *
239 * The constructor takes arguments according to a few formats.
240 * Ipv4Mask ("255.255.255.255"), Ipv4Mask ("/32"), and Ipv4Mask (0xffffffff)
241 * are all equivalent.
242 *
243 * @see attribute_Ipv4Mask
244 */
246{
247 public:
248 /**
249 * Will initialize to a garbage value (0x66666666)
250 */
251 Ipv4Mask();
252 /**
253 * @param mask bitwise integer representation of the mask
254 *
255 * For example, the integer input 0xffffff00 yields a 24-bit mask
256 */
257 Ipv4Mask(uint32_t mask);
258 /**
259 * @param mask String constant either in "255.255.255.0" or "/24" format
260 */
261 Ipv4Mask(const char* mask);
262 /**
263 * @param a first address to compare
264 * @param b second address to compare
265 * @return true if both addresses are equal in their masked bits,
266 * corresponding to this mask
267 */
268 bool IsMatch(Ipv4Address a, Ipv4Address b) const;
269 /**
270 * Get the host-order 32-bit IP mask
271 * @return the host-order 32-bit IP mask
272 */
273 uint32_t Get() const;
274 /**
275 * input mask is in host order.
276 * @param mask The host order 32-bit mask
277 */
278 void Set(uint32_t mask);
279 /**
280 * @brief Return the inverse mask in host order.
281 * @return The inverse mask
282 */
283 uint32_t GetInverse() const;
284 /**
285 * @brief Print this mask to the given output stream
286 *
287 * The print format is in the typical "255.255.255.0"
288 * @param os The output stream to which this Ipv4Address is printed
289 */
290 void Print(std::ostream& os) const;
291 /**
292 * @return the prefix length of mask (the yy in x.x.x.x/yy notation)
293 */
294 uint16_t GetPrefixLength() const;
295 /**
296 * @return the 255.0.0.0 mask corresponding to a typical loopback address
297 */
298 static Ipv4Mask GetLoopback();
299 /**
300 * @return the 0.0.0.0 mask
301 */
302 static Ipv4Mask GetZero();
303 /**
304 * @return the 255.255.255.255 mask
305 */
306 static Ipv4Mask GetOnes();
307
308 /**
309 * @brief Equal to operator.
310 *
311 * @param a the first operand.
312 * @param b the first operand.
313 * @returns true if the operands are equal.
314 */
315 friend bool operator==(const Ipv4Mask& a, const Ipv4Mask& b);
316
317 /**
318 * @brief Not equal to operator.
319 *
320 * @param a the first operand.
321 * @param b the first operand.
322 * @returns true if the operands are not equal.
323 */
324 friend bool operator!=(const Ipv4Mask& a, const Ipv4Mask& b);
325
326 private:
327 uint32_t m_mask; //!< IP mask
328};
329
332
333/**
334 * @brief Stream insertion operator.
335 *
336 * @param os the stream
337 * @param address the address
338 * @returns a reference to the stream
339 */
340std::ostream& operator<<(std::ostream& os, const Ipv4Address& address);
341/**
342 * @brief Stream insertion operator.
343 *
344 * @param os the stream
345 * @param mask the mask
346 * @returns a reference to the stream
347 */
348std::ostream& operator<<(std::ostream& os, const Ipv4Mask& mask);
349/**
350 * @brief Stream extraction operator.
351 *
352 * @param is the stream
353 * @param address the address
354 * @returns a reference to the stream
355 */
356std::istream& operator>>(std::istream& is, Ipv4Address& address);
357/**
358 * @brief Stream extraction operator.
359 *
360 * @param is the stream
361 * @param mask the mask
362 * @returns a reference to the stream
363 */
364std::istream& operator>>(std::istream& is, Ipv4Mask& mask);
365
366inline bool
368{
369 return a.m_address == b.m_address;
370}
371
372inline bool
374{
375 return a.m_address != b.m_address;
376}
377
378inline bool
379operator<(const Ipv4Address& a, const Ipv4Address& b)
380{
381 return a.m_address < b.m_address;
382}
383
384/**
385 * @ingroup address
386 *
387 * @brief Class providing an hash for IPv4 addresses
388 */
390{
391 public:
392 /**
393 * @brief Returns the hash of an IPv4 address.
394 * @param x the address
395 * @return the hash
396 *
397 * This method uses std::hash rather than class Hash
398 * as speed is more important than cryptographic robustness.
399 */
400 size_t operator()(const Ipv4Address& x) const;
401};
402
403inline bool
404operator==(const Ipv4Mask& a, const Ipv4Mask& b)
405{
406 return a.m_mask == b.m_mask;
407}
408
409inline bool
410operator!=(const Ipv4Mask& a, const Ipv4Mask& b)
411{
412 return a.m_mask != b.m_mask;
413}
414
415} // namespace ns3
416
417#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
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