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 
34 /**
35  * \ingroup address
36  *
37  * \brief Ipv4 addresses are stored in host order in this class.
38  *
39  * \see attribute_Ipv4Address
40  */
41 class Ipv4Address {
42 public:
43   Ipv4Address ();
44   /**
45    * input address is in host order.
46    * \param address The host order 32-bit address
47    */
48   explicit Ipv4Address (uint32_t address);
49   /**
50     * \brief Constructs an Ipv4Address by parsing a the input C-string
51     *
52     * Input address is in format:
53     * \c hhh.xxx.xxx.lll
54     * where \c h is the high byte and \c l the
55     * low byte
56     * \param address C-string containing the address as described above
57     */
58   Ipv4Address (char const *address);
59   /**
60    * Get the host-order 32-bit IP address
61    * \return the host-order 32-bit IP address
62    */
63   uint32_t Get (void) const;
64   /**
65    * input address is in host order.
66    * \param address The host order 32-bit address
67    */
68   void Set (uint32_t address);
69   /**
70     * \brief Sets an Ipv4Address by parsing a the input C-string
71     *
72     * Input address is in format:
73     * \c hhh.xxx.xxx.lll
74     * where \c h is the high byte and \c l the
75     * low byte
76     * \param address C-string containing the address as described above
77     */
78   void Set (char const *address);
79   /**
80    * Serialize this address to a 4-byte buffer
81    *
82    * \param buf output buffer to which this address gets overwritten with this
83    * Ipv4Address
84    */
85   void Serialize (uint8_t buf[4]) const;
86   /**
87    * \param buf buffer to read address from
88    * \return an Ipv4Address
89    *
90    * The input address is expected to be in network byte order format.
91    */
92   static Ipv4Address Deserialize (const uint8_t buf[4]);
93   /**
94    * \brief Print this address to the given output stream
95    *
96    * The print format is in the typical "192.168.1.1"
97    * \param os The output stream to which this Ipv4Address is printed
98    */
99   void Print (std::ostream &os) const;
100 
101   /**
102    * \return true if address is initialized (i.e., set to something), false otherwise
103    */
104   bool IsInitialized (void) const;
105   /**
106     * \return true if address is 0.0.0.0; false otherwise
107     */
108   bool IsAny (void) const;
109   /**
110     * \return true if address is 127.0.0.1; false otherwise
111     */
112   bool IsLocalhost (void) const;
113   /**
114     * \return true if address is 255.255.255.255; false otherwise
115     */
116   bool IsBroadcast (void) const;
117   /**
118     * \return true only if address is in the range 224.0.0.0 - 239.255.255.255
119     */
120   bool IsMulticast (void) const;
121   /**
122     * \return true only if address is in local multicast address scope, 224.0.0.0/24
123     */
124   bool IsLocalMulticast (void) const;
125   /**
126    * \brief Combine this address with a network mask
127    *
128    * This method returns an IPv4 address that is this address combined
129    * (bitwise and) with a network mask, yielding an IPv4 network
130    * address.
131    *
132    * \param mask a network mask
133    * \returns the address combined with the mask
134    */
135   Ipv4Address CombineMask (Ipv4Mask const &mask) const;
136   /**
137    * \brief Generate subnet-directed broadcast address corresponding to mask
138    *
139    * The subnet-directed broadcast address has the host bits set to all
140    * ones.  If this method is called with a mask of 255.255.255.255,
141    * (i.e., the address is a /32 address), the program will assert, since
142    * there is no subnet associated with a /32 address.
143    *
144    * \param mask a network mask
145    * \returns a broadcast address for the subnet.
146    */
147   Ipv4Address GetSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
148   /**
149    * \brief Generate subnet-directed broadcast address corresponding to mask
150    *
151    * The subnet-directed broadcast address has the host bits set to all
152    * ones.  If this method is called with a mask of 255.255.255.255,
153    * (i.e., the address is a /32 address), the program will assert, since
154    * there is no subnet associated with a /32 address.
155    *
156    * \param mask a network mask
157    * \return true if the address, when combined with the input mask, has all
158    * of its host bits set to one
159    */
160   bool IsSubnetDirectedBroadcast (Ipv4Mask const &mask) const;
161   /**
162    * \param address an address to compare type with
163    *
164    * \return true if the type of the address stored internally
165    * is compatible with the type of the input address, false otherwise.
166    */
167   static bool IsMatchingType (const Address &address);
168   /**
169    * Convert an instance of this class to a polymorphic Address instance.
170    *
171    * \return a new Address instance
172    */
173   operator Address () const;
174   /**
175    * \param address a polymorphic address
176    * \return a new Ipv4Address from the polymorphic address
177    *
178    * This function performs a type check and asserts if the
179    * type of the input address is not compatible with an
180    * Ipv4Address.
181    */
182   static Ipv4Address ConvertFrom (const Address &address);
183   /**
184    * \return the 0.0.0.0 address
185    */
186   static Ipv4Address GetZero (void);
187   /**
188    * \return the 0.0.0.0 address
189    */
190   static Ipv4Address GetAny (void);
191   /**
192    * \return the 255.255.255.255 address
193    */
194   static Ipv4Address GetBroadcast (void);
195   /**
196    * \return the 127.0.0.1 address
197    */
198   static Ipv4Address GetLoopback (void);
199 
200 private:
201 
202   /**
203    * \brief Convert to an Address type
204    * \return the Address corresponding to this object.
205    */
206   Address ConvertTo (void) const;
207 
208   /**
209    * \brief Get the underlying address type (automatically assigned).
210    *
211    * \returns the address type
212    */
213   static uint8_t GetType (void);
214   uint32_t m_address; //!< IPv4 address
215   bool m_initialized; //!< IPv4 address has been explicitly initialized to a valid value.
216 
217   /**
218    * \brief Equal to operator.
219    *
220    * \param a the first operand.
221    * \param b the first operand.
222    * \returns true if the operands are equal.
223    */
224   friend bool operator == (Ipv4Address const &a, Ipv4Address const &b);
225 
226   /**
227    * \brief Not equal to operator.
228    *
229    * \param a the first operand.
230    * \param b the first operand.
231    * \returns true if the operands are not equal.
232    */
233   friend bool operator != (Ipv4Address const &a, Ipv4Address const &b);
234 
235   /**
236    * \brief Less than to operator.
237    *
238    * \param a the first operand.
239    * \param b the first operand.
240    * \returns true if the first operand is less than the second.
241    */
242   friend bool operator < (Ipv4Address const &a, Ipv4Address const &b);
243 };
244 
245 /**
246  * \ingroup address
247  *
248  * \brief a class to represent an Ipv4 address mask
249  *
250  * The constructor takes arguments according to a few formats.
251  * Ipv4Mask ("255.255.255.255"), Ipv4Mask ("/32"), and Ipv4Mask (0xffffffff)
252  * are all equivalent.
253  *
254  * \see attribute_Ipv4Mask
255  */
256 class Ipv4Mask {
257 public:
258   /**
259    * Will initialize to a garbage value (0x66666666)
260    */
261   Ipv4Mask ();
262   /**
263    * \param mask bitwise integer representation of the mask
264    *
265    * For example, the integer input 0xffffff00 yields a 24-bit mask
266    */
267   Ipv4Mask (uint32_t mask);
268   /**
269    * \param mask String constant either in "255.255.255.0" or "/24" format
270    */
271   Ipv4Mask (char const *mask);
272   /**
273    * \param a first address to compare
274    * \param b second address to compare
275    * \return true if both addresses are equal in their masked bits,
276    * corresponding to this mask
277    */
278   bool IsMatch (Ipv4Address a, Ipv4Address b) const;
279   /**
280    * Get the host-order 32-bit IP mask
281    * \return the host-order 32-bit IP mask
282    */
283   uint32_t Get (void) const;
284   /**
285    * input mask is in host order.
286    * \param mask The host order 32-bit mask
287    */
288   void Set (uint32_t mask);
289   /**
290    * \brief Return the inverse mask in host order.
291    * \return The inverse mask
292    */
293   uint32_t GetInverse (void) const;
294   /**
295    * \brief Print this mask to the given output stream
296    *
297    * The print format is in the typical "255.255.255.0"
298    * \param os The output stream to which this Ipv4Address is printed
299    */
300   void Print (std::ostream &os) const;
301   /**
302    * \return the prefix length of mask (the yy in x.x.x.x/yy notation)
303    */
304   uint16_t GetPrefixLength (void) const;
305   /**
306    * \return the 255.0.0.0 mask corresponding to a typical loopback address
307    */
308   static Ipv4Mask GetLoopback (void);
309   /**
310    * \return the 0.0.0.0 mask
311    */
312   static Ipv4Mask GetZero (void);
313   /**
314    * \return the 255.255.255.255 mask
315    */
316   static Ipv4Mask GetOnes (void);
317 
318   /**
319    * \brief Equal to operator.
320    *
321    * \param a the first operand.
322    * \param b the first operand.
323    * \returns true if the operands are equal.
324    */
325   friend bool operator == (Ipv4Mask const &a, Ipv4Mask const &b);
326 
327   /**
328    * \brief Not equal to operator.
329    *
330    * \param a the first operand.
331    * \param b the first operand.
332    * \returns true if the operands are not equal.
333    */
334   friend bool operator != (Ipv4Mask const &a, Ipv4Mask const &b);
335 
336 
337 private:
338   uint32_t m_mask; //!< IP mask
339 };
340 
341 ATTRIBUTE_HELPER_HEADER (Ipv4Address);
342 ATTRIBUTE_HELPER_HEADER (Ipv4Mask);
343 
344 /**
345  * \brief Stream insertion operator.
346  *
347  * \param os the stream
348  * \param address the address
349  * \returns a reference to the stream
350  */
351 std::ostream& operator<< (std::ostream& os, Ipv4Address const& address);
352 /**
353  * \brief Stream insertion operator.
354  *
355  * \param os the stream
356  * \param mask the mask
357  * \returns a reference to the stream
358  */
359 std::ostream& operator<< (std::ostream& os, Ipv4Mask const& mask);
360 /**
361  * \brief Stream extraction operator.
362  *
363  * \param is the stream
364  * \param address the address
365  * \returns a reference to the stream
366  */
367 std::istream & operator >> (std::istream &is, Ipv4Address &address);
368 /**
369  * \brief Stream extraction operator.
370  *
371  * \param is the stream
372  * \param mask the mask
373  * \returns a reference to the stream
374  */
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 
392 /**
393  * \ingroup address
394  *
395  * \brief Class providing an hash for IPv4 addresses
396  */
397 class Ipv4AddressHash {
398 public:
399   /**
400    * \brief Returns the hash of an IPv4 address.
401    * \param x the address
402    * \return the hash
403    *
404    * This method uses std::hash rather than class Hash
405    * as speed is more important than cryptographic robustness.
406    */
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 */
424