1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 University of Washington
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 
19 #ifndef IPV4_ADDRESS_GENERATOR_H
20 #define IPV4_ADDRESS_GENERATOR_H
21 
22 #include "ns3/ipv4-address.h"
23 
24 namespace ns3 {
25 
26 /**
27  * \ingroup address
28  * \ingroup ipv4
29  *
30  * \brief This generator assigns addresses sequentially from a provided
31  * network address; used in topology code.
32  *
33  * \note BEWARE: this class acts as a Singleton.
34  * In other terms, two different instances of Ipv4AddressGenerator will
35  * pick IPv4 numbers from the same pool. Changing the network in one of them
36  * will also change the network in the other instances.
37  *
38  */
39 class Ipv4AddressGenerator {
40 public:
41   /**
42    * \brief Initialise the base network, mask and address for the generator
43    *
44    * The first call to NextAddress() or GetAddress() will return the
45    * value passed in.
46    *
47    * \param net The network for the base Ipv4Address
48    * \param mask The network mask of the base Ipv4Address
49    * \param addr The base address used for initialization
50    */
51   static void Init (const Ipv4Address net, const Ipv4Mask mask,
52                     const Ipv4Address addr = "0.0.0.1");
53 
54   /**
55    * \brief Get the next network according to the given Ipv4Mask
56    *
57    * This operation is a pre-increment, meaning that the internal state
58    * is changed before returning the new network address.
59    *
60    * This also resets the address to the base address that was
61    * used for initialization.
62    *
63    * \param mask The Ipv4Mask used to set the next network
64    * \returns the IPv4 address of the next network
65    */
66   static Ipv4Address NextNetwork (const Ipv4Mask mask);
67 
68   /**
69    * \brief Get the current network of the given Ipv4Mask
70    *
71    * Does not change the internal state; this just peeks at the current
72    * network
73    *
74    * \param mask The Ipv4Mask for the current network
75    * \returns the IPv4 address of the current network
76    */
77   static Ipv4Address GetNetwork (const Ipv4Mask mask);
78 
79   /**
80    * \brief Set the address for the given mask
81    *
82    * \param addr The address to set for the current mask
83    * \param mask The Ipv4Mask whose address is to be set
84    */
85   static void InitAddress (const Ipv4Address addr, const Ipv4Mask mask);
86 
87   /**
88    * \brief Allocate the next Ipv4Address for the configured network and mask
89    *
90    * This operation is a post-increment, meaning that the first address
91    * allocated will be the one that was initially configured.
92    *
93    * \param mask The Ipv4Mask for the current network
94    * \returns the IPv4 address
95    */
96   static Ipv4Address NextAddress (const Ipv4Mask mask);
97 
98   /**
99    * \brief Get the Ipv4Address that will be allocated upon NextAddress ()
100    *
101    * Does not change the internal state; just is used to peek the next
102    * address that will be allocated upon NextAddress ()
103    *
104    * \param mask The Ipv4Mask for the current network
105    * \returns the IPv4 address
106    */
107   static Ipv4Address GetAddress (const Ipv4Mask mask);
108 
109   /**
110    * \brief Reset the networks and Ipv4Address to zero
111    */
112   static void Reset (void);
113 
114   /**
115    * \brief Add the Ipv4Address to the list of IPv4 entries
116    *
117    * Typically, this is used by external address allocators that want
118    * to make use of this class's ability to track duplicates.  AddAllocated
119    * is always called internally for any address generated by NextAddress ()
120    *
121    * \param addr The Ipv4Address to be added to the list of Ipv4 entries
122    * \returns true on success
123    */
124   static bool AddAllocated (const Ipv4Address addr);
125 
126   /**
127    * \brief Check the Ipv4Address allocation in the list of IPv4 entries
128    *
129    * \param addr The Ipv4Address to be checked in the list of Ipv4 entries
130    * \returns true if the network is already allocated
131    */
132   static bool IsAddressAllocated (const Ipv4Address addr);
133 
134   /**
135    * \brief Check if a network has already allocated addresses
136    *
137    * \param addr The Ipv4 network to be checked
138    * \param mask The Ipv4 network mask
139    * \returns true if the network is already allocated
140    */
141   static bool IsNetworkAllocated (const Ipv4Address addr, const Ipv4Mask mask);
142 
143   /**
144    * \brief Used to turn off fatal errors and assertions, for testing
145    */
146   static void TestMode (void);
147 };
148 
149 } // namespace ns3
150 
151 #endif /* IPV4_ADDRESS_GENERATOR_H */
152