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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19
20#ifndef IPV4_ADDRESS_H
21#define IPV4_ADDRESS_H
22
23#include "ns3/address.h"
24#include "ns3/attribute-helper.h"
25
26#include <ostream>
27#include <stdint.h>
28
29namespace ns3
30{
31
32class Ipv4Mask;
33
34/**
35 * \ingroup address
36 *
37 * \brief Ipv4 addresses are stored in host order in this class.
38 *
39 * \see attribute_Ipv4Address
40 */
42{
43 public:
45 /**
46 * input address is in host order.
47 * \param address The host order 32-bit address
48 */
49 explicit Ipv4Address(uint32_t address);
50 /**
51 * \brief Constructs an Ipv4Address by parsing a the input C-string
52 *
53 * Input address is in format:
54 * \c hhh.xxx.xxx.lll
55 * where \c h is the high byte and \c l the
56 * low byte
57 * \param address C-string containing the address as described above
58 */
59 Ipv4Address(const char* address);
60 /**
61 * Get the host-order 32-bit IP address
62 * \return the host-order 32-bit IP address
63 */
64 uint32_t Get() const;
65 /**
66 * input address is in host order.
67 * \param address The host order 32-bit address
68 */
69 void Set(uint32_t address);
70 /**
71 * \brief Sets an Ipv4Address by parsing a the input C-string
72 *
73 * Input address is in format:
74 * \c hhh.xxx.xxx.lll
75 * where \c h is the high byte and \c l the
76 * low byte
77 * \param address C-string containing the address as described above
78 */
79 void Set(const char* address);
80 /**
81 * Serialize this address to a 4-byte buffer
82 *
83 * \param buf output buffer to which this address gets overwritten with this
84 * Ipv4Address
85 */
86 void Serialize(uint8_t buf[4]) const;
87 /**
88 * \param buf buffer to read address from
89 * \return an Ipv4Address
90 *
91 * The input address is expected to be in network byte order format.
92 */
93 static Ipv4Address Deserialize(const uint8_t buf[4]);
94 /**
95 * \brief Print this address to the given output stream
96 *
97 * The print format is in the typical "192.168.1.1"
98 * \param os The output stream to which this Ipv4Address is printed
99 */
100 void Print(std::ostream& os) const;
101
102 /**
103 * \return true if address is initialized (i.e., set to something), false otherwise
104 */
105 bool IsInitialized() const;
106 /**
107 * \return true if address is 0.0.0.0; false otherwise
108 */
109 bool IsAny() const;
110 /**
111 * \return true if address is 127.0.0.1; false otherwise
112 */
113 bool IsLocalhost() const;
114 /**
115 * \return true if address is 255.255.255.255; false otherwise
116 */
117 bool IsBroadcast() const;
118 /**
119 * \return true only if address is in the range 224.0.0.0 - 239.255.255.255
120 */
121 bool IsMulticast() const;
122 /**
123 * \return true only if address is in local multicast address scope, 224.0.0.0/24
124 */
125 bool IsLocalMulticast() const;
126 /**
127 * \brief Combine this address with a network mask
128 *
129 * This method returns an IPv4 address that is this address combined
130 * (bitwise and) with a network mask, yielding an IPv4 network
131 * address.
132 *
133 * \param mask a network mask
134 * \returns the address combined with the mask
135 */
136 Ipv4Address CombineMask(const Ipv4Mask& mask) const;
137 /**
138 * \brief Generate subnet-directed broadcast address corresponding to mask
139 *
140 * The subnet-directed broadcast address has the host bits set to all
141 * ones. If this method is called with a mask of 255.255.255.255,
142 * (i.e., the address is a /32 address), the program will assert, since
143 * there is no subnet associated with a /32 address.
144 *
145 * \param mask a network mask
146 * \returns a broadcast address for the subnet.
147 */
149 /**
150 * \brief Generate subnet-directed broadcast address corresponding to mask
151 *
152 * The subnet-directed broadcast address has the host bits set to all
153 * ones. If this method is called with a mask of 255.255.255.255,
154 * (i.e., the address is a /32 address), the program will assert, since
155 * there is no subnet associated with a /32 address.
156 *
157 * \param mask a network mask
158 * \return true if the address, when combined with the input mask, has all
159 * of its host bits set to one
160 */
161 bool IsSubnetDirectedBroadcast(const Ipv4Mask& mask) const;
162 /**
163 * \param address an address to compare type with
164 *
165 * \return true if the type of the address stored internally
166 * is compatible with the type of the input address, false otherwise.
167 */
168 static bool IsMatchingType(const Address& address);
169 /**
170 * Convert an instance of this class to a polymorphic Address instance.
171 *
172 * \return a new Address instance
173 */
174 operator Address() const;
175 /**
176 * \param address a polymorphic address
177 * \return a new Ipv4Address from the polymorphic address
178 *
179 * This function performs a type check and asserts if the
180 * type of the input address is not compatible with an
181 * Ipv4Address.
182 */
183 static Ipv4Address ConvertFrom(const Address& address);
184 /**
185 * \brief Convert to an Address type
186 * \return the Address corresponding to this object.
187 */
188 Address ConvertTo() const;
189
190 /**
191 * \return the 0.0.0.0 address
192 */
193 static Ipv4Address GetZero();
194 /**
195 * \return the 0.0.0.0 address
196 */
197 static Ipv4Address GetAny();
198 /**
199 * \return the 255.255.255.255 address
200 */
201 static Ipv4Address GetBroadcast();
202 /**
203 * \return the 127.0.0.1 address
204 */
205 static Ipv4Address GetLoopback();
206
207 private:
208 /**
209 * \brief Get the underlying address type (automatically assigned).
210 *
211 * \returns the address type
212 */
213 static uint8_t GetType();
214 uint32_t m_address; //!< IPv4 address
215 bool m_initialized; //!< IPv4 address has been explicitly initialized to a valid value.
216
217 /**
218 * \brief Equal to operator.
219 *
220 * \param a the first operand.
221 * \param b the first operand.
222 * \returns true if the operands are equal.
223 */
224 friend bool operator==(const Ipv4Address& a, const Ipv4Address& b);
225
226 /**
227 * \brief Not equal to operator.
228 *
229 * \param a the first operand.
230 * \param b the first operand.
231 * \returns true if the operands are not equal.
232 */
233 friend bool operator!=(const Ipv4Address& a, const Ipv4Address& b);
234
235 /**
236 * \brief Less than to operator.
237 *
238 * \param a the first operand.
239 * \param b the first operand.
240 * \returns true if the first operand is less than the second.
241 */
242 friend bool operator<(const Ipv4Address& a, const Ipv4Address& b);
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 garbage value (0x66666666)
261 */
262 Ipv4Mask();
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 Equal to operator.
321 *
322 * \param a the first operand.
323 * \param b the first operand.
324 * \returns true if the operands are equal.
325 */
326 friend bool operator==(const Ipv4Mask& a, const Ipv4Mask& b);
327
328 /**
329 * \brief Not equal to operator.
330 *
331 * \param a the first operand.
332 * \param b the first operand.
333 * \returns true if the operands are not equal.
334 */
335 friend bool operator!=(const Ipv4Mask& a, const Ipv4Mask& b);
336
337 private:
338 uint32_t m_mask; //!< IP mask
339};
340
343
344/**
345 * \brief Stream insertion operator.
346 *
347 * \param os the stream
348 * \param address the address
349 * \returns a reference to the stream
350 */
351std::ostream& operator<<(std::ostream& os, const Ipv4Address& address);
352/**
353 * \brief Stream insertion operator.
354 *
355 * \param os the stream
356 * \param mask the mask
357 * \returns a reference to the stream
358 */
359std::ostream& operator<<(std::ostream& os, const Ipv4Mask& mask);
360/**
361 * \brief Stream extraction operator.
362 *
363 * \param is the stream
364 * \param address the address
365 * \returns a reference to the stream
366 */
367std::istream& operator>>(std::istream& is, Ipv4Address& address);
368/**
369 * \brief Stream extraction operator.
370 *
371 * \param is the stream
372 * \param mask the mask
373 * \returns a reference to the stream
374 */
375std::istream& operator>>(std::istream& is, Ipv4Mask& mask);
376
377inline bool
379{
380 return a.m_address == b.m_address;
381}
382
383inline bool
385{
386 return a.m_address != b.m_address;
387}
388
389inline bool
390operator<(const Ipv4Address& a, const Ipv4Address& b)
391{
392 return a.m_address < b.m_address;
393}
394
395/**
396 * \ingroup address
397 *
398 * \brief Class providing an hash for IPv4 addresses
399 */
401{
402 public:
403 /**
404 * \brief Returns the hash of an IPv4 address.
405 * \param x the address
406 * \return the hash
407 *
408 * This method uses std::hash rather than class Hash
409 * as speed is more important than cryptographic robustness.
410 */
411 size_t operator()(const Ipv4Address& x) const;
412};
413
414inline bool
415operator==(const Ipv4Mask& a, const Ipv4Mask& b)
416{
417 return a.m_mask == b.m_mask;
418}
419
420inline bool
421operator!=(const Ipv4Mask& a, const Ipv4Mask& b)
422{
423 return a.m_mask != b.m_mask;
424}
425
426} // namespace ns3
427
428#endif /* IPV4_ADDRESS_H */
a polymophic address class
Definition: address.h:101
Class providing an hash for IPv4 addresses.
Definition: ipv4-address.h:401
size_t operator()(const Ipv4Address &x) const
Returns the hash of an IPv4 address.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
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.
Definition: ipv4-address.h:384
friend bool operator<(const Ipv4Address &a, const Ipv4Address &b)
Less than to operator.
Definition: ipv4-address.h:390
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.
Definition: ipv4-address.h:378
bool m_initialized
IPv4 address has been explicitly initialized to a valid value.
Definition: ipv4-address.h:215
uint32_t m_address
IPv4 address.
Definition: ipv4-address.h:214
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
Definition: ipv4-address.h:257
uint32_t m_mask
IP mask.
Definition: ipv4-address.h:338
friend bool operator!=(const Ipv4Mask &a, const Ipv4Mask &b)
Not equal to operator.
Definition: ipv4-address.h:421
static Ipv4Mask GetOnes()
void Set(uint32_t mask)
input mask is in host order.
Definition: ipv4-address.cc:91
Ipv4Mask()
Will initialize to a garbage value (0x66666666)
Definition: ipv4-address.cc:38
friend bool operator==(const Ipv4Mask &a, const Ipv4Mask &b)
Equal to operator.
Definition: ipv4-address.h:415
uint16_t GetPrefixLength() const
uint32_t Get() const
Get the host-order 32-bit IP mask.
Definition: ipv4-address.cc:84
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.
Definition: ipv4-address.cc:98
bool IsMatch(Ipv4Address a, Ipv4Address b) const
Definition: ipv4-address.cc:77
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:680
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170