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);
85  void Serialize (uint8_t buf[4]) const;
92  static Ipv4Address Deserialize (const uint8_t buf[4]);
99  void Print (std::ostream &os) const;
100 
104  bool IsInitialized (void) const;
108  bool IsAny (void) const;
112  bool IsLocalhost (void) const;
116  bool IsBroadcast (void) const;
120  bool IsMulticast (void) const;
124  bool IsLocalMulticast (void) const;
135  Ipv4Address CombineMask (Ipv4Mask const &mask) const;
147  Ipv4Address GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
160  bool IsSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
167  static bool IsMatchingType (const Address &address);
173  operator Address () const;
182  static Ipv4Address ConvertFrom (const Address &address);
186  static Ipv4Address GetZero (void);
190  static Ipv4Address GetAny (void);
194  static Ipv4Address GetBroadcast (void);
198  static Ipv4Address GetLoopback (void);
199 
200 private:
201 
206  Address ConvertTo (void) const;
207 
213  static uint8_t GetType (void);
214  uint32_t m_address;
216 
224  friend bool operator == (Ipv4Address const &a, Ipv4Address const &b);
225 
233  friend bool operator != (Ipv4Address const &a, Ipv4Address const &b);
234 
242  friend bool operator < (Ipv4Address const &a, Ipv4Address const &b);
243 };
244 
256 class Ipv4Mask {
257 public:
261  Ipv4Mask ();
267  Ipv4Mask (uint32_t mask);
271  Ipv4Mask (char const *mask);
278  bool IsMatch (Ipv4Address a, Ipv4Address b) const;
283  uint32_t Get (void) const;
288  void Set (uint32_t mask);
293  uint32_t GetInverse (void) const;
300  void Print (std::ostream &os) const;
304  uint16_t GetPrefixLength (void) const;
308  static Ipv4Mask GetLoopback (void);
312  static Ipv4Mask GetZero (void);
316  static Ipv4Mask GetOnes (void);
317 
325  friend bool operator == (Ipv4Mask const &a, Ipv4Mask const &b);
326 
334  friend bool operator != (Ipv4Mask const &a, Ipv4Mask const &b);
335 
336 
337 private:
338  uint32_t m_mask;
339 };
340 
343 
351 std::ostream& operator<< (std::ostream& os, Ipv4Address const& address);
359 std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask);
367 std::istream & operator >> (std::istream &is, Ipv4Address &address);
375 std::istream & operator >> (std::istream &is, Ipv4Mask &mask);
376 
377 inline bool operator == (const Ipv4Address &a, const Ipv4Address &b)
378 {
379  return (a.m_address == b.m_address);
380 }
381 
382 inline bool operator != (const Ipv4Address &a, const Ipv4Address &b)
383 {
384  return (a.m_address != b.m_address);
385 }
386 
387 inline bool operator < (const Ipv4Address &a, const Ipv4Address &b)
388 {
389  return (a.m_address < b.m_address);
390 }
391 
398 public:
407  size_t operator() (Ipv4Address const &x) const;
408 };
409 
410 inline bool operator == (Ipv4Mask const &a, Ipv4Mask const &b)
411 {
412  return (a.m_mask == b.m_mask);
413 }
414 
415 inline bool operator != (Ipv4Mask const &a, Ipv4Mask const &b)
416 {
417  return (a.m_mask != b.m_mask);
418 }
419 
420 
421 } // namespace ns3
422 
423 #endif /* IPV4_ADDRESS_H */
a polymophic address class
Definition: address.h:91
Class providing an hash for IPv4 addresses.
Definition: ipv4-address.h:397
size_t operator()(Ipv4Address const &x) const
Returns the hash of an IPv4 address.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
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.
static uint8_t GetType(void)
Get the underlying address type (automatically assigned).
bool IsMulticast(void) const
bool IsInitialized(void) const
static Ipv4Address GetBroadcast(void)
static Ipv4Address GetLoopback(void)
static Ipv4Address ConvertFrom(const Address &address)
friend bool operator<(Ipv4Address const &a, Ipv4Address const &b)
Less than to operator.
Definition: ipv4-address.h:387
static bool IsMatchingType(const Address &address)
void Set(uint32_t address)
input address is in host order.
bool IsBroadcast(void) const
static Ipv4Address GetAny(void)
bool IsSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
bool IsLocalMulticast(void) const
bool IsLocalhost(void) const
static Ipv4Address Deserialize(const uint8_t buf[4])
friend bool operator!=(Ipv4Address const &a, Ipv4Address const &b)
Not equal to operator.
Definition: ipv4-address.h:382
bool m_initialized
IPv4 address has been explicitly initialized to a valid value.
Definition: ipv4-address.h:215
Ipv4Address GetSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
uint32_t m_address
IPv4 address.
Definition: ipv4-address.h:214
Ipv4Address CombineMask(Ipv4Mask const &mask) const
Combine this address with a network mask.
friend bool operator==(Ipv4Address const &a, Ipv4Address const &b)
Equal to operator.
Definition: ipv4-address.h:377
Address ConvertTo(void) const
Convert to an Address type.
static Ipv4Address GetZero(void)
bool IsAny(void) const
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:256
friend bool operator==(Ipv4Mask const &a, Ipv4Mask const &b)
Equal to operator.
Definition: ipv4-address.h:410
uint32_t m_mask
IP mask.
Definition: ipv4-address.h:338
void Set(uint32_t mask)
input mask is in host order.
uint32_t GetInverse(void) const
Return the inverse mask in host order.
Ipv4Mask()
Will initialize to a garbage value (0x66666666)
Definition: ipv4-address.cc:78
uint16_t GetPrefixLength(void) const
static Ipv4Mask GetLoopback(void)
friend bool operator!=(Ipv4Mask const &a, Ipv4Mask const &b)
Not equal to operator.
Definition: ipv4-address.h:415
void Print(std::ostream &os) const
Print this mask to the given output stream.
static Ipv4Mask GetZero(void)
bool IsMatch(Ipv4Address a, Ipv4Address b) const
uint32_t Get(void) const
Get the host-order 32-bit IP mask.
static Ipv4Mask GetOnes(void)
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:158
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:176
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:1612
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:162
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139
list x
Random number samples.