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 
29 namespace ns3 {
30 
31 class Ipv4Mask;
32 
40 class Ipv4Address {
41 public:
42  Ipv4Address ();
47  explicit Ipv4Address (uint32_t address);
57  Ipv4Address (char const *address);
62  uint32_t Get (void) const;
67  void Set (uint32_t address);
77  void Set (char const *address);
83  bool IsEqual (const Ipv4Address &other) const
84  {
85  return m_address == other.m_address;
86  }
93  void Serialize (uint8_t buf[4]) const;
100  static Ipv4Address Deserialize (const uint8_t buf[4]);
107  void Print (std::ostream &os) const;
111  bool IsAny (void) const;
115  bool IsLocalhost (void) const;
119  bool IsBroadcast (void) const;
123  bool IsMulticast (void) const;
127  bool IsLocalMulticast (void) const;
138  Ipv4Address CombineMask (Ipv4Mask const &mask) const;
150  Ipv4Address GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
163  bool IsSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
170  static bool IsMatchingType (const Address &address);
176  operator Address () const;
185  static Ipv4Address ConvertFrom (const Address &address);
189  static Ipv4Address GetZero (void);
193  static Ipv4Address GetAny (void);
197  static Ipv4Address GetBroadcast (void);
201  static Ipv4Address GetLoopback (void);
202 
203 private:
204 
209  Address ConvertTo (void) const;
210 
216  static uint8_t GetType (void);
217  uint32_t m_address;
218 
226  friend bool operator == (Ipv4Address const &a, Ipv4Address const &b);
227 
235  friend bool operator != (Ipv4Address const &a, Ipv4Address const &b);
236 
244  friend bool operator < (Ipv4Address const &a, Ipv4Address const &b);
245 };
246 
258 class Ipv4Mask {
259 public:
263  Ipv4Mask ();
269  Ipv4Mask (uint32_t mask);
273  Ipv4Mask (char const *mask);
280  bool IsMatch (Ipv4Address a, Ipv4Address b) const;
285  bool IsEqual (Ipv4Mask other) const;
290  uint32_t Get (void) const;
295  void Set (uint32_t mask);
300  uint32_t GetInverse (void) const;
307  void Print (std::ostream &os) const;
311  uint16_t GetPrefixLength (void) const;
315  static Ipv4Mask GetLoopback (void);
319  static Ipv4Mask GetZero (void);
323  static Ipv4Mask GetOnes (void);
324 
325 private:
326  uint32_t m_mask;
327 };
328 
331 
339 std::ostream& operator<< (std::ostream& os, Ipv4Address const& address);
347 std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask);
355 std::istream & operator >> (std::istream &is, Ipv4Address &address);
363 std::istream & operator >> (std::istream &is, Ipv4Mask &mask);
364 
372 inline bool operator == (const Ipv4Address &a, const Ipv4Address &b)
373 {
374  return (a.m_address == b.m_address);
375 }
383 inline bool operator != (const Ipv4Address &a, const Ipv4Address &b)
384 {
385  return (a.m_address != b.m_address);
386 }
394 inline bool operator < (const Ipv4Address &a, const Ipv4Address &b)
395 {
396  return (a.m_address < b.m_address);
397 }
398 
404 class Ipv4AddressHash : public std::unary_function<Ipv4Address, size_t> {
405 public:
411  size_t operator() (Ipv4Address const &x) const;
412 };
413 
421 bool operator == (Ipv4Mask const &a, Ipv4Mask const &b);
429 bool operator != (Ipv4Mask const &a, Ipv4Mask const &b);
430 
431 } // namespace ns3
432 
433 #endif /* IPV4_ADDRESS_H */
static Ipv4Address Deserialize(const uint8_t buf[4])
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
static Ipv4Address GetAny(void)
friend bool operator<(Ipv4Address const &a, Ipv4Address const &b)
Less than to operator.
Definition: ipv4-address.h:394
a class to represent an Ipv4 address mask
Definition: ipv4-address.h:258
uint32_t m_address
IPv4 address.
Definition: ipv4-address.h:217
bool IsLocalMulticast(void) const
bool IsMulticast(void) const
Ipv4Address CombineMask(Ipv4Mask const &mask) const
Combine this address with a network mask.
Ipv4Address GetSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
a polymophic address class
Definition: address.h:90
bool IsSubnetDirectedBroadcast(Ipv4Mask const &mask) const
Generate subnet-directed broadcast address corresponding to mask.
uint32_t Get(void) const
Get the host-order 32-bit IP address.
bool IsBroadcast(void) const
void Serialize(uint8_t buf[4]) const
Serialize this address to a 4-byte buffer.
void Print(std::ostream &os) const
Print this address to the given output stream.
bool IsAny(void) const
static bool IsMatchingType(const Address &address)
static Ipv4Address GetBroadcast(void)
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool IsEqual(const Ipv4Address &other) const
Comparison operation between two Ipv4Addresses.
Definition: ipv4-address.h:83
address
Definition: first.py:37
static Ipv4Address GetZero(void)
static Ipv4Address GetLoopback(void)
Address ConvertTo(void) const
Convert to an Address type.
uint32_t m_mask
IP mask.
Definition: ipv4-address.h:326
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:40
bool IsLocalhost(void) const
friend bool operator==(Ipv4Address const &a, Ipv4Address const &b)
Equal to operator.
Definition: ipv4-address.h:372
static uint8_t GetType(void)
Get the underlying address type (automatically assigned).
friend bool operator!=(Ipv4Address const &a, Ipv4Address const &b)
Not equal to operator.
Definition: ipv4-address.h:383
static Ipv4Address ConvertFrom(const Address &address)
#define ATTRIBUTE_HELPER_HEADER(type)
Declare the attribute value, accessor and checkers for class type.
Class providing an hash for IPv4 addresses.
Definition: ipv4-address.h:404