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 #include "ns3/deprecated.h"
26 
27 #include <ostream>
28 #include <stdint.h>
29 
30 namespace ns3
31 {
32 
33 class Ipv4Mask;
34 
43 {
44  public:
45  Ipv4Address();
50  explicit Ipv4Address(uint32_t address);
60  Ipv4Address(const char* address);
65  uint32_t Get() const;
70  void Set(uint32_t address);
80  void Set(const char* address);
87  void Serialize(uint8_t buf[4]) const;
94  static Ipv4Address Deserialize(const uint8_t buf[4]);
101  void Print(std::ostream& os) const;
102 
106  bool IsInitialized() const;
110  bool IsAny() const;
114  bool IsLocalhost() const;
118  bool IsBroadcast() const;
122  bool IsMulticast() const;
126  bool IsLocalMulticast() const;
137  Ipv4Address CombineMask(const Ipv4Mask& mask) const;
162  bool IsSubnetDirectedBroadcast(const Ipv4Mask& mask) const;
169  static bool IsMatchingType(const Address& address);
175  operator Address() const;
184  static Ipv4Address ConvertFrom(const Address& address);
189  Address ConvertTo() const;
190 
194  static Ipv4Address GetZero();
198  static Ipv4Address GetAny();
202  static Ipv4Address GetBroadcast();
206  static Ipv4Address GetLoopback();
207 
208  private:
214  static uint8_t GetType();
215  uint32_t m_address;
217 
225  friend bool operator==(const Ipv4Address& a, const Ipv4Address& b);
226 
234  friend bool operator!=(const Ipv4Address& a, const Ipv4Address& b);
235 
243  friend bool operator<(const Ipv4Address& a, const Ipv4Address& b);
244 };
245 
257 class Ipv4Mask
258 {
259  public:
263  Ipv4Mask();
269  Ipv4Mask(uint32_t mask);
273  Ipv4Mask(const char* mask);
280  bool IsMatch(Ipv4Address a, Ipv4Address b) const;
285  uint32_t Get() const;
290  void Set(uint32_t mask);
295  uint32_t GetInverse() const;
302  void Print(std::ostream& os) const;
306  uint16_t GetPrefixLength() const;
310  static Ipv4Mask GetLoopback();
314  static Ipv4Mask GetZero();
318  static Ipv4Mask GetOnes();
319 
327  friend bool operator==(const Ipv4Mask& a, const Ipv4Mask& b);
328 
336  friend bool operator!=(const Ipv4Mask& a, const Ipv4Mask& b);
337 
338  private:
339  uint32_t m_mask;
340 };
341 
344 
352 std::ostream& operator<<(std::ostream& os, const Ipv4Address& address);
360 std::ostream& operator<<(std::ostream& os, const Ipv4Mask& mask);
368 std::istream& operator>>(std::istream& is, Ipv4Address& address);
376 std::istream& operator>>(std::istream& is, Ipv4Mask& mask);
377 
378 inline bool
379 operator==(const Ipv4Address& a, const Ipv4Address& b)
380 {
381  return (a.m_address == b.m_address);
382 }
383 
384 inline bool
385 operator!=(const Ipv4Address& a, const Ipv4Address& b)
386 {
387  return (a.m_address != b.m_address);
388 }
389 
390 inline bool
391 operator<(const Ipv4Address& a, const Ipv4Address& b)
392 {
393  return (a.m_address < b.m_address);
394 }
395 
402 {
403  public:
412  size_t operator()(const Ipv4Address& x) const;
413 };
414 
415 inline bool
416 operator==(const Ipv4Mask& a, const Ipv4Mask& b)
417 {
418  return (a.m_mask == b.m_mask);
419 }
420 
421 inline bool
422 operator!=(const Ipv4Mask& a, const Ipv4Mask& b)
423 {
424  return (a.m_mask != b.m_mask);
425 }
426 
427 } // namespace ns3
428 
429 #endif /* IPV4_ADDRESS_H */
a polymophic address class
Definition: address.h:100
Class providing an hash for IPv4 addresses.
Definition: ipv4-address.h:402
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:43
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:385
friend bool operator<(const Ipv4Address &a, const Ipv4Address &b)
Less than to operator.
Definition: ipv4-address.h:391
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:379
bool m_initialized
IPv4 address has been explicitly initialized to a valid value.
Definition: ipv4-address.h:216
uint32_t m_address
IPv4 address.
Definition: ipv4-address.h:215
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:258
uint32_t m_mask
IP mask.
Definition: ipv4-address.h:339
friend bool operator!=(const Ipv4Mask &a, const Ipv4Mask &b)
Not equal to operator.
Definition: ipv4-address.h:422
static Ipv4Mask GetOnes()
void Set(uint32_t mask)
input mask is in host order.
Definition: ipv4-address.cc:98
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:416
uint16_t GetPrefixLength() const
uint32_t Get() const
Get the host-order 32-bit IP mask.
Definition: ipv4-address.cc:91
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
Definition: ipv4-address.cc:77
static Ipv4Mask GetLoopback()
static Ipv4Mask GetZero()
address
Definition: first.py:40
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:681
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:153
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129