1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3
* Copyright (c) 2005 INRIA
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License version 2 as
7
* published by the Free Software Foundation;
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
21
#ifndef IPV4_ADDRESS_H
22
#define IPV4_ADDRESS_H
26
#include "ns3/address.h"
27
#include "ns3/attribute-helper.h"
36
* \brief Ipv4 addresses are stored in host order in this class.
42
* input address is in host order.
43
* \param address The host order 32-bit address
45
explicit Ipv4Address (uint32_t address);
47
* \brief Constructs an Ipv4Address by parsing a the input C-string
49
* Input address is in format:
51
* where h is the high byte and l the
53
* \param address C-string containing the address as described above
55
Ipv4Address (char const *address);
57
* Get the host-order 32-bit IP address
58
* \return the host-order 32-bit IP address
60
uint32_t Get (void) const;
62
* input address is in host order.
63
* \param address The host order 32-bit address
65
void Set (uint32_t address);
67
* \brief Sets an Ipv4Address by parsing a the input C-string
69
* Input address is in format:
71
* where h is the high byte and l the
73
* \param address C-string containing the address as described above
75
void Set (char const *address);
77
* \brief Comparison operation between two Ipv4Addresses
78
* \param other address to which to compare this address
79
* \return True if the addresses are equal. False otherwise.
81
bool IsEqual (const Ipv4Address &other) const
83
return m_address == other.m_address;
86
* Serialize this address to a 4-byte buffer
88
* \param buf output buffer to which this address gets overwritten with this
91
void Serialize (uint8_t buf[4]) const;
93
* \param buf buffer to read address from
94
* \return an Ipv4Address
96
* The input address is expected to be in network byte order format.
98
static Ipv4Address Deserialize (const uint8_t buf[4]);
100
* \brief Print this address to the given output stream
102
* The print format is in the typical "192.168.1.1"
103
* \param os The output stream to which this Ipv4Address is printed
105
void Print (std::ostream &os) const;
107
* \return true if address is 255.255.255.255; false otherwise
109
bool IsBroadcast (void) const;
111
* \return true only if address is in the range 224.0.0.0 - 239.255.255.255
113
bool IsMulticast (void) const;
115
* \return true only if address is in local multicast address scope, 224.0.0.0/24
117
bool IsLocalMulticast (void) const;
119
* \brief Combine this address with a network mask
121
* This method returns an IPv4 address that is this address combined
122
* (bitwise and) with a network mask, yielding an IPv4 network
125
* \param mask a network mask
127
Ipv4Address CombineMask (Ipv4Mask const &mask) const;
129
* \brief Generate subnet-directed broadcast address corresponding to mask
131
* The subnet-directed broadcast address has the host bits set to all
132
* ones. If this method is called with a mask of 255.255.255.255,
133
* (i.e., the address is a /32 address), the program will assert, since
134
* there is no subnet associated with a /32 address.
136
* \param mask a network mask
138
Ipv4Address GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
140
* \brief Generate subnet-directed broadcast address corresponding to mask
142
* The subnet-directed broadcast address has the host bits set to all
143
* ones. If this method is called with a mask of 255.255.255.255,
144
* (i.e., the address is a /32 address), the program will assert, since
145
* there is no subnet associated with a /32 address.
147
* \param mask a network mask
148
* \return true if the address, when combined with the input mask, has all
149
* of its host bits set to one
151
bool IsSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
153
* \param address an address to compare type with
155
* \return true if the type of the address stored internally
156
* is compatible with the type of the input address, false otherwise.
158
static bool IsMatchingType (const Address &address);
160
* Convert an instance of this class to a polymorphic Address instance.
162
* \return a new Address instance
164
operator Address () const;
166
* \param address a polymorphic address
167
* \return a new Ipv4Address from the polymorphic address
169
* This function performs a type check and asserts if the
170
* type of the input address is not compatible with an
173
static Ipv4Address ConvertFrom (const Address &address);
175
* \return the 0.0.0.0 address
177
static Ipv4Address GetZero (void);
179
* \return the 0.0.0.0 address
181
static Ipv4Address GetAny (void);
183
* \return the 255.255.255.255 address
185
static Ipv4Address GetBroadcast (void);
187
* \return the 127.0.0.1 address
189
static Ipv4Address GetLoopback (void);
192
Address ConvertTo (void) const;
193
static uint8_t GetType (void);
196
friend bool operator == (Ipv4Address const &a, Ipv4Address const &b);
197
friend bool operator != (Ipv4Address const &a, Ipv4Address const &b);
198
friend bool operator < (Ipv4Address const &addrA, Ipv4Address const &addrB);
204
* \brief a class to represent an Ipv4 address mask
206
* The constructor takes arguments according to a few formats.
207
* Ipv4Mask ("255.255.255.255"), Ipv4Mask ("/32"), and Ipv4Mask (0xffffffff)
208
* are all equivalent.
213
* Will initialize to a garbage value (0x66666666)
217
* \param mask bitwise integer representation of the mask
219
* For example, the integer input 0xffffff00 yields a 24-bit mask
221
Ipv4Mask (uint32_t mask);
223
* \param mask String constant either in "255.255.255.0" or "/24" format
225
Ipv4Mask (char const *mask);
227
* \param a first address to compare
228
* \param b second address to compare
229
* \return true if both addresses are equal in their masked bits,
230
* corresponding to this mask
232
bool IsMatch (Ipv4Address a, Ipv4Address b) const;
234
* \param other a mask to compare
235
* \return true if the mask equals the mask passed as input parameter
237
bool IsEqual (Ipv4Mask other) const;
239
* Get the host-order 32-bit IP mask
240
* \return the host-order 32-bit IP mask
242
uint32_t Get (void) const;
244
* input mask is in host order.
245
* \param mask The host order 32-bit mask
247
void Set (uint32_t mask);
249
* \brief Return the inverse mask in host order.
251
uint32_t GetInverse (void) const;
253
* \brief Print this mask to the given output stream
255
* The print format is in the typical "255.255.255.0"
256
* \param os The output stream to which this Ipv4Address is printed
258
void Print (std::ostream &os) const;
260
* \return the prefix length of mask (the yy in x.x.x.x/yy notation)
262
uint16_t GetPrefixLength (void) const;
264
* \return the 255.0.0.0 mask corresponding to a typical loopback address
266
static Ipv4Mask GetLoopback (void);
268
* \return the 0.0.0.0 mask
270
static Ipv4Mask GetZero (void);
272
* \return the 255.255.255.255 mask
274
static Ipv4Mask GetOnes (void);
281
* \class ns3::Ipv4AddressValue
282
* \brief hold objects of type ns3::Ipv4Address
285
* \class ns3::Ipv4MaskValue
286
* \brief hold objects of type ns3::Ipv4Mask
289
ATTRIBUTE_HELPER_HEADER (Ipv4Address);
290
ATTRIBUTE_HELPER_HEADER (Ipv4Mask);
292
std::ostream& operator<< (std::ostream& os, Ipv4Address const& address);
293
std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask);
294
std::istream & operator >> (std::istream &is, Ipv4Address &address);
295
std::istream & operator >> (std::istream &is, Ipv4Mask &mask);
297
inline bool operator == (const Ipv4Address &a, const Ipv4Address &b)
299
return (a.m_address == b.m_address);
301
inline bool operator != (const Ipv4Address &a, const Ipv4Address &b)
303
return (a.m_address != b.m_address);
305
inline bool operator < (const Ipv4Address &a, const Ipv4Address &b)
307
return (a.m_address < b.m_address);
311
class Ipv4AddressHash : public std::unary_function<Ipv4Address, size_t> {
313
size_t operator() (Ipv4Address const &x) const;
316
bool operator == (Ipv4Mask const &a, Ipv4Mask const &b);
317
bool operator != (Ipv4Mask const &a, Ipv4Mask const &b);
321
#endif /* IPV4_ADDRESS_H */