A Discrete-Event Network Simulator
API
ipv4-address.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005 INRIA
4  *
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;
8  *
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.
13  *
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
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #ifndef IPV4_ADDRESS_H
22 #define IPV4_ADDRESS_H
23 
24 #include <stdint.h>
25 #include <ostream>
26 #include "ns3/address.h"
27 #include "ns3/attribute-helper.h"
28 #include "ns3/deprecated.h"
29 
30 namespace ns3 {
31 
32 class Ipv4Mask;
33 
41 class Ipv4Address {
42 public:
43  Ipv4Address ();
48  explicit Ipv4Address (uint32_t address);
58  Ipv4Address (char const *address);
63  uint32_t Get (void) const;
68  void Set (uint32_t address);
78  void Set (char const *address);
88  bool IsEqual (const Ipv4Address &other) const
89  {
90  return m_address == other.m_address;
91  }
98  void Serialize (uint8_t buf[4]) const;
105  static Ipv4Address Deserialize (const uint8_t buf[4]);
112  void Print (std::ostream &os) const;
113 
117  bool IsInitialized (void) const;
121  bool IsAny (void) const;
125  bool IsLocalhost (void) const;
129  bool IsBroadcast (void) const;
133  bool IsMulticast (void) const;
137  bool IsLocalMulticast (void) const;
148  Ipv4Address CombineMask (Ipv4Mask const &mask) const;
160  Ipv4Address GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
173  bool IsSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
180  static bool IsMatchingType (const Address &address);
186  operator Address () const;
195  static Ipv4Address ConvertFrom (const Address &address);
199  static Ipv4Address GetZero (void);
203  static Ipv4Address GetAny (void);
207  static Ipv4Address GetBroadcast (void);
211  static Ipv4Address GetLoopback (void);
212 
213 private:
214 
219  Address ConvertTo (void) const;
220 
226  static uint8_t GetType (void);
227  uint32_t m_address;
229 
237  friend bool operator == (Ipv4Address const &a, Ipv4Address const &b);
238 
246  friend bool operator != (Ipv4Address const &a, Ipv4Address const &b);
247 
255  friend bool operator < (Ipv4Address const &a, Ipv4Address const &b);
256 };
257 
269 class Ipv4Mask {
270 public:
274  Ipv4Mask ();
280  Ipv4Mask (uint32_t mask);
284  Ipv4Mask (char const *mask);
291  bool IsMatch (Ipv4Address a, Ipv4Address b) const;
300  bool IsEqual (Ipv4Mask other) const;
305  uint32_t Get (void) const;
310  void Set (uint32_t mask);
315  uint32_t GetInverse (void) const;
322  void Print (std::ostream &os) const;
326  uint16_t GetPrefixLength (void) const;
330  static Ipv4Mask GetLoopback (void);
334  static Ipv4Mask GetZero (void);
338  static Ipv4Mask GetOnes (void);
339 
347  friend bool operator == (Ipv4Mask const &a, Ipv4Mask const &b);
348 
356  friend bool operator != (Ipv4Mask const &a, Ipv4Mask const &b);
357 
358 
359 private:
360  uint32_t m_mask;
361 };
362 
365 
373 std::ostream& operator<< (std::ostream& os, Ipv4Address const& address);
381 std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask);
389 std::istream & operator >> (std::istream &is, Ipv4Address &address);
397 std::istream & operator >> (std::istream &is, Ipv4Mask &mask);
398 
399 inline bool operator == (const Ipv4Address &a, const Ipv4Address &b)
400 {
401  return (a.m_address == b.m_address);
402 }
403 
404 inline bool operator != (const Ipv4Address &a, const Ipv4Address &b)
405 {
406  return (a.m_address != b.m_address);
407 }
408 
409 inline bool operator < (const Ipv4Address &a, const Ipv4Address &b)
410 {
411  return (a.m_address < b.m_address);
412 }
413 
420 public:
429  size_t operator() (Ipv4Address const &x) const;
430 };
431 
432 inline bool operator == (Ipv4Mask const &a, Ipv4Mask const &b)
433 {
434  return (a.m_mask == b.m_mask);
435 }
436 
437 inline bool operator != (Ipv4Mask const &a, Ipv4Mask const &b)
438 {
439  return (a.m_mask != b.m_mask);
440 }
441 
442 
443 } // namespace ns3
444 
445 #endif /* IPV4_ADDRESS_H */
Ipv4Mask()
Will initialize to a garbage value (0x66666666)
Definition: ipv4-address.cc:78
static Ipv4Address Deserialize(const uint8_t buf[4])
static Ipv4Mask GetOnes(void)
static Ipv4Address GetAny(void)
friend bool operator<(Ipv4Address const &a, Ipv4Address const &b)
Less than to operator.
Definition: ipv4-address.h:409
bool IsBroadcast(void) const
bool IsInitialized(void) const
bool IsMatch(Ipv4Address a, Ipv4Address b) const
bool IsLocalhost(void) const
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:269
uint32_t m_address
IPv4 address.
Definition: ipv4-address.h:227
uint32_t GetInverse(void) const
Return the inverse mask in host order.
friend bool operator==(Ipv4Mask const &a, Ipv4Mask const &b)
Equal to operator.
Definition: ipv4-address.h:432
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:160
a polymophic address class
Definition: address.h:90
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:160
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:137
bool IsMulticast(void) const
bool IsSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
NS_DEPRECATED_3_31 bool IsEqual(Ipv4Mask other) const
Ipv4Address CombineMask(Ipv4Mask const &mask) const
Combine this address with a network mask.
static Ipv4Mask GetZero(void)
friend bool operator!=(Ipv4Mask const &a, Ipv4Mask const &b)
Not equal to operator.
Definition: ipv4-address.h:437
bool IsLocalMulticast(void) const
static bool IsMatchingType(const Address &address)
static Ipv4Address GetBroadcast(void)
NS_DEPRECATED_3_31 bool IsEqual(const Ipv4Address &other) const
Comparison operation between two Ipv4Addresses.
Definition: ipv4-address.h:88
bool operator!=(Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > a, Callback< R, T1, T2, T3, T4, T5, T6, T7, T8, T9 > b)
Inequality test.
Definition: callback.h:1606
Every class exported by the ns3 library is enclosed in the ns3 namespace.
address
Definition: first.py:44
void Set(uint32_t mask)
input mask is in host order.
static Ipv4Address GetZero(void)
static Ipv4Address GetLoopback(void)
Ipv4Address GetSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
size_t operator()(Ipv4Address const &x) const
Returns the hash of an IPv4 address.
static Ipv4Mask GetLoopback(void)
uint32_t m_mask
IP mask.
Definition: ipv4-address.h:360
void Set(uint32_t address)
input address is in host order.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
bool IsAny(void) const
#define NS_DEPRECATED_3_31
Tag for things deprecated in version ns-3.31.
Definition: deprecated.h:89
friend bool operator==(Ipv4Address const &a, Ipv4Address const &b)
Equal to operator.
Definition: ipv4-address.h:399
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:142
static uint8_t GetType(void)
Get the underlying address type (automatically assigned).
bool m_initialized
IPv4 address has been explicitly initialized to a valid value.
Definition: ipv4-address.h:228
uint16_t GetPrefixLength(void) const
friend bool operator!=(Ipv4Address const &a, Ipv4Address const &b)
Not equal to operator.
Definition: ipv4-address.h:404
static Ipv4Address ConvertFrom(const Address &address)
void Print(std::ostream &os) const
Print this address to the given output stream.
uint32_t Get(void) const
Get the host-order 32-bit IP address.
Address ConvertTo(void) const
Convert to an Address type.
void Print(std::ostream &os) const
Print this mask to the given output stream.
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
Class providing an hash for IPv4 addresses.
Definition: ipv4-address.h:419