A Discrete-Event Network Simulator
API
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 
29 namespace ns3
30 {
31 
32 class Ipv4Mask;
33 
42 {
43  public:
44  Ipv4Address();
49  explicit Ipv4Address(uint32_t address);
59  Ipv4Address(const char* address);
64  uint32_t Get() const;
69  void Set(uint32_t address);
79  void Set(const char* address);
86  void Serialize(uint8_t buf[4]) const;
93  static Ipv4Address Deserialize(const uint8_t buf[4]);
100  void Print(std::ostream& os) const;
101 
105  bool IsInitialized() const;
109  bool IsAny() const;
113  bool IsLocalhost() const;
117  bool IsBroadcast() const;
121  bool IsMulticast() const;
125  bool IsLocalMulticast() const;
136  Ipv4Address CombineMask(const Ipv4Mask& mask) const;
161  bool IsSubnetDirectedBroadcast(const Ipv4Mask& mask) const;
168  static bool IsMatchingType(const Address& address);
174  operator Address() const;
183  static Ipv4Address ConvertFrom(const Address& address);
188  Address ConvertTo() const;
189 
193  static Ipv4Address GetZero();
197  static Ipv4Address GetAny();
201  static Ipv4Address GetBroadcast();
205  static Ipv4Address GetLoopback();
206 
207  private:
213  static uint8_t GetType();
214  uint32_t m_address;
216 
224  friend bool operator==(const Ipv4Address& a, const Ipv4Address& b);
225 
233  friend bool operator!=(const Ipv4Address& a, const Ipv4Address& b);
234 
242  friend bool operator<(const Ipv4Address& a, const Ipv4Address& b);
243 };
244 
256 class Ipv4Mask
257 {
258  public:
262  Ipv4Mask();
268  Ipv4Mask(uint32_t mask);
272  Ipv4Mask(const char* mask);
279  bool IsMatch(Ipv4Address a, Ipv4Address b) const;
284  uint32_t Get() const;
289  void Set(uint32_t mask);
294  uint32_t GetInverse() const;
301  void Print(std::ostream& os) const;
305  uint16_t GetPrefixLength() const;
309  static Ipv4Mask GetLoopback();
313  static Ipv4Mask GetZero();
317  static Ipv4Mask GetOnes();
318 
326  friend bool operator==(const Ipv4Mask& a, const Ipv4Mask& b);
327 
335  friend bool operator!=(const Ipv4Mask& a, const Ipv4Mask& b);
336 
337  private:
338  uint32_t m_mask;
339 };
340 
343 
351 std::ostream& operator<<(std::ostream& os, const Ipv4Address& address);
359 std::ostream& operator<<(std::ostream& os, const Ipv4Mask& mask);
367 std::istream& operator>>(std::istream& is, Ipv4Address& address);
375 std::istream& operator>>(std::istream& is, Ipv4Mask& mask);
376 
377 inline bool
378 operator==(const Ipv4Address& a, const Ipv4Address& b)
379 {
380  return a.m_address == b.m_address;
381 }
382 
383 inline bool
384 operator!=(const Ipv4Address& a, const Ipv4Address& b)
385 {
386  return a.m_address != b.m_address;
387 }
388 
389 inline bool
390 operator<(const Ipv4Address& a, const Ipv4Address& b)
391 {
392  return a.m_address < b.m_address;
393 }
394 
401 {
402  public:
411  size_t operator()(const Ipv4Address& x) const;
412 };
413 
414 inline bool
415 operator==(const Ipv4Mask& a, const Ipv4Mask& b)
416 {
417  return a.m_mask == b.m_mask;
418 }
419 
420 inline bool
421 operator!=(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()
address
Definition: first.py:47
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:678
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
ATTRIBUTE_HELPER_HEADER(ValueClassTest)
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159