~ubuntu-branches/ubuntu/quantal/ns3/quantal

« back to all changes in this revision

Viewing changes to ns-3.13/src/network/utils/ipv4-address.h

  • Committer: Package Import Robot
  • Author(s): YunQiang Su, Aron Xu, YunQiang Su, Upstream
  • Date: 2012-01-06 00:35:42 UTC
  • mfrom: (10.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20120106003542-vcn5g03mhapm991h
Tags: 3.13+dfsg-1
[ Aron Xu ]:
        add tag binary and binary-indep, 
  for not build doc when --binary-arch (Closes: #654493).
[ YunQiang Su ]
        add waf 1.5/1.6 source to debian directory, 
  and build waf from there (Closes: #642217).
[ Upstream ]
  Successfully link with --as-needed option (Closes: #642225).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
33
/** 
 
34
 * \ingroup address
 
35
 *
 
36
 * \brief Ipv4 addresses are stored in host order in this class.
 
37
 */
 
38
class Ipv4Address {
 
39
public:
 
40
  Ipv4Address ();
 
41
  /**
 
42
   * input address is in host order.
 
43
   * \param address The host order 32-bit address
 
44
   */
 
45
  explicit Ipv4Address (uint32_t address);
 
46
  /** 
 
47
    * \brief Constructs an Ipv4Address by parsing a the input C-string
 
48
    *
 
49
    * Input address is in format:
 
50
    * hhh.xxx.xxx.lll
 
51
    * where h is the high byte and l the
 
52
    * low byte
 
53
    * \param address C-string containing the address as described above
 
54
    */
 
55
  Ipv4Address (char const *address);
 
56
  /** 
 
57
   * Get the host-order 32-bit IP address
 
58
   * \return the host-order 32-bit IP address
 
59
   */
 
60
  uint32_t Get (void) const;
 
61
  /**
 
62
   * input address is in host order.
 
63
   * \param address The host order 32-bit address
 
64
   */
 
65
  void Set (uint32_t address);
 
66
  /** 
 
67
    * \brief Sets an Ipv4Address by parsing a the input C-string
 
68
    *
 
69
    * Input address is in format:
 
70
    * hhh.xxx.xxx.lll
 
71
    * where h is the high byte and l the
 
72
    * low byte
 
73
    * \param address C-string containing the address as described above
 
74
    */
 
75
  void Set (char const *address);
 
76
  /**
 
77
   * \brief Comparison operation between two Ipv4Addresses
 
78
   * \param other address to which to compare this address
 
79
   * \return True if the addresses are equal. False otherwise.
 
80
   */
 
81
  bool IsEqual (const Ipv4Address &other) const
 
82
  {
 
83
    return m_address == other.m_address;
 
84
  }
 
85
  /**
 
86
   * Serialize this address to a 4-byte buffer
 
87
   *
 
88
   * \param buf output buffer to which this address gets overwritten with this
 
89
   * Ipv4Address
 
90
   */
 
91
  void Serialize (uint8_t buf[4]) const;
 
92
  /**
 
93
   * \param buf buffer to read address from
 
94
   * \return an Ipv4Address
 
95
   * 
 
96
   * The input address is expected to be in network byte order format.
 
97
   */
 
98
  static Ipv4Address Deserialize (const uint8_t buf[4]);
 
99
  /**
 
100
   * \brief Print this address to the given output stream
 
101
   *
 
102
   * The print format is in the typical "192.168.1.1"
 
103
   * \param os The output stream to which this Ipv4Address is printed
 
104
   */
 
105
  void Print (std::ostream &os) const;
 
106
  /**
 
107
    * \return true if address is 255.255.255.255; false otherwise
 
108
    */
 
109
  bool IsBroadcast (void) const;
 
110
  /**
 
111
    * \return true only if address is in the range 224.0.0.0 - 239.255.255.255
 
112
    */
 
113
  bool IsMulticast (void) const;
 
114
  /**
 
115
    * \return true only if address is in local multicast address scope, 224.0.0.0/24
 
116
    */
 
117
  bool IsLocalMulticast (void) const;
 
118
  /**
 
119
   * \brief Combine this address with a network mask
 
120
   *
 
121
   * This method returns an IPv4 address that is this address combined
 
122
   * (bitwise and) with a network mask, yielding an IPv4 network
 
123
   * address.
 
124
   *
 
125
   * \param mask a network mask 
 
126
   */
 
127
  Ipv4Address CombineMask (Ipv4Mask const &mask) const;
 
128
  /**
 
129
   * \brief Generate subnet-directed broadcast address corresponding to mask
 
130
   *
 
131
   * The subnet-directed broadcast address has the host bits set to all
 
132
   * ones.  If this method is called with a mask of 255.255.255.255,
 
133
   * (i.e., the address is a /32 address), the program will assert, since
 
134
   * there is no subnet associated with a /32 address.
 
135
   *
 
136
   * \param mask a network mask 
 
137
   */
 
138
  Ipv4Address GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
 
139
  /**
 
140
   * \brief Generate subnet-directed broadcast address corresponding to mask
 
141
   * 
 
142
   * The subnet-directed broadcast address has the host bits set to all
 
143
   * ones.  If this method is called with a mask of 255.255.255.255,
 
144
   * (i.e., the address is a /32 address), the program will assert, since
 
145
   * there is no subnet associated with a /32 address.
 
146
   *
 
147
   * \param mask a network mask 
 
148
   * \return true if the address, when combined with the input mask, has all
 
149
   * of its host bits set to one
 
150
   */
 
151
  bool IsSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
 
152
  /**
 
153
   * \param address an address to compare type with
 
154
   *
 
155
   * \return true if the type of the address stored internally
 
156
   * is compatible with the type of the input address, false otherwise.
 
157
   */
 
158
  static bool IsMatchingType (const Address &address);
 
159
  /**
 
160
   * Convert an instance of this class to a polymorphic Address instance.
 
161
   *
 
162
   * \return a new Address instance
 
163
   */
 
164
  operator Address () const;
 
165
  /**
 
166
   * \param address a polymorphic address
 
167
   * \return a new Ipv4Address from the polymorphic address
 
168
   *
 
169
   * This function performs a type check and asserts if the
 
170
   * type of the input address is not compatible with an
 
171
   * Ipv4Address.
 
172
   */
 
173
  static Ipv4Address ConvertFrom (const Address &address);
 
174
  /**
 
175
   * \return the 0.0.0.0 address
 
176
   */
 
177
  static Ipv4Address GetZero (void);
 
178
  /**
 
179
   * \return the 0.0.0.0 address
 
180
   */
 
181
  static Ipv4Address GetAny (void);
 
182
  /**
 
183
   * \return the 255.255.255.255 address
 
184
   */
 
185
  static Ipv4Address GetBroadcast (void);
 
186
  /**
 
187
   * \return the 127.0.0.1 address
 
188
   */
 
189
  static Ipv4Address GetLoopback (void);
 
190
 
 
191
private:
 
192
  Address ConvertTo (void) const;
 
193
  static uint8_t GetType (void);
 
194
  uint32_t m_address;
 
195
 
 
196
  friend bool operator == (Ipv4Address const &a, Ipv4Address const &b);
 
197
  friend bool operator != (Ipv4Address const &a, Ipv4Address const &b);
 
198
  friend bool operator < (Ipv4Address const &addrA, Ipv4Address const &addrB);
 
199
};
 
200
 
 
201
/**
 
202
 * \ingroup address
 
203
 *
 
204
 * \brief a class to represent an Ipv4 address mask
 
205
 * 
 
206
 * The constructor takes arguments according to a few formats. 
 
207
 * Ipv4Mask ("255.255.255.255"), Ipv4Mask ("/32"), and Ipv4Mask (0xffffffff)
 
208
 * are all equivalent.
 
209
 */
 
210
class Ipv4Mask {
 
211
public:
 
212
  /**
 
213
   * Will initialize to a garbage value (0x66666666)
 
214
   */
 
215
  Ipv4Mask ();
 
216
  /**
 
217
   * \param mask bitwise integer representation of the mask
 
218
   * 
 
219
   * For example, the integer input 0xffffff00 yields a 24-bit mask
 
220
   */
 
221
  Ipv4Mask (uint32_t mask);
 
222
  /**
 
223
   * \param mask String constant either in "255.255.255.0" or "/24" format
 
224
   */
 
225
  Ipv4Mask (char const *mask);
 
226
  /**
 
227
   * \param a first address to compare
 
228
   * \param b second address to compare
 
229
   * \return true if both addresses are equal in their masked bits, 
 
230
   * corresponding to this mask
 
231
   */
 
232
  bool IsMatch (Ipv4Address a, Ipv4Address b) const;
 
233
  /**
 
234
   * \param other a mask to compare 
 
235
   * \return true if the mask equals the mask passed as input parameter
 
236
   */
 
237
  bool IsEqual (Ipv4Mask other) const;
 
238
  /** 
 
239
   * Get the host-order 32-bit IP mask
 
240
   * \return the host-order 32-bit IP mask
 
241
   */
 
242
  uint32_t Get (void) const;
 
243
  /**
 
244
   * input mask is in host order.
 
245
   * \param mask The host order 32-bit mask
 
246
   */
 
247
  void Set (uint32_t mask);
 
248
  /**
 
249
   * \brief Return the inverse mask in host order. 
 
250
   */
 
251
  uint32_t GetInverse (void) const;
 
252
  /**
 
253
   * \brief Print this mask to the given output stream
 
254
   *
 
255
   * The print format is in the typical "255.255.255.0"
 
256
   * \param os The output stream to which this Ipv4Address is printed
 
257
   */
 
258
  void Print (std::ostream &os) const;
 
259
  /**
 
260
   * \return the prefix length of mask (the yy in x.x.x.x/yy notation)
 
261
   */
 
262
  uint16_t GetPrefixLength (void) const;
 
263
  /**
 
264
   * \return the 255.0.0.0 mask corresponding to a typical loopback address
 
265
   */
 
266
  static Ipv4Mask GetLoopback (void);
 
267
  /**
 
268
   * \return the 0.0.0.0 mask
 
269
   */
 
270
  static Ipv4Mask GetZero (void);
 
271
  /**
 
272
   * \return the 255.255.255.255 mask
 
273
   */
 
274
  static Ipv4Mask GetOnes (void);
 
275
 
 
276
private:
 
277
  uint32_t m_mask;
 
278
};
 
279
 
 
280
/**
 
281
 * \class ns3::Ipv4AddressValue
 
282
 * \brief hold objects of type ns3::Ipv4Address
 
283
 */
 
284
/**
 
285
 * \class ns3::Ipv4MaskValue
 
286
 * \brief hold objects of type ns3::Ipv4Mask
 
287
 */
 
288
 
 
289
ATTRIBUTE_HELPER_HEADER (Ipv4Address);
 
290
ATTRIBUTE_HELPER_HEADER (Ipv4Mask);
 
291
 
 
292
std::ostream& operator<< (std::ostream& os, Ipv4Address const& address);
 
293
std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask);
 
294
std::istream & operator >> (std::istream &is, Ipv4Address &address);
 
295
std::istream & operator >> (std::istream &is, Ipv4Mask &mask);
 
296
 
 
297
inline bool operator == (const Ipv4Address &a, const Ipv4Address &b)
 
298
{
 
299
  return (a.m_address == b.m_address);
 
300
}
 
301
inline bool operator != (const Ipv4Address &a, const Ipv4Address &b)
 
302
{
 
303
  return (a.m_address != b.m_address);
 
304
}
 
305
inline bool operator < (const Ipv4Address &a, const Ipv4Address &b)
 
306
{
 
307
  return (a.m_address < b.m_address);
 
308
}
 
309
 
 
310
 
 
311
class Ipv4AddressHash : public std::unary_function<Ipv4Address, size_t> {
 
312
public:
 
313
  size_t operator() (Ipv4Address const &x) const;
 
314
};
 
315
 
 
316
bool operator == (Ipv4Mask const &a, Ipv4Mask const &b);
 
317
bool operator != (Ipv4Mask const &a, Ipv4Mask const &b);
 
318
 
 
319
} // namespace ns3
 
320
 
 
321
#endif /* IPV4_ADDRESS_H */