25 |
25 |
26 NS_LOG_COMPONENT_DEFINE ("Ipv4AddressGenerator"); |
26 NS_LOG_COMPONENT_DEFINE ("Ipv4AddressGenerator"); |
27 |
27 |
28 namespace ns3 { |
28 namespace ns3 { |
29 |
29 |
|
30 /** |
|
31 * \internal |
|
32 * \ingroup address |
|
33 * |
|
34 * \brief Implementation class of Ipv4AddressGenerator |
|
35 * This generator assigns addresses sequentially from a provided |
|
36 * network address; used in topology code. It also keeps track of all |
|
37 * addresses assigned to perform duplicate detection. |
|
38 */ |
30 class Ipv4AddressGeneratorImpl |
39 class Ipv4AddressGeneratorImpl |
31 { |
40 { |
32 public: |
41 public: |
33 Ipv4AddressGeneratorImpl (); |
42 Ipv4AddressGeneratorImpl (); |
34 virtual ~Ipv4AddressGeneratorImpl (); |
43 virtual ~Ipv4AddressGeneratorImpl (); |
35 |
44 |
|
45 /** |
|
46 * \internal |
|
47 * \brief Initialise the base network, mask and address for the generator |
|
48 * |
|
49 * The first call to NextAddress() or GetAddress() will return the |
|
50 * value passed in. |
|
51 * |
|
52 * \param net The network for the base Ipv4Address |
|
53 * \param mask The network mask of the base Ipv4Address |
|
54 * \param addr The base address used for initialization |
|
55 */ |
36 void Init (const Ipv4Address net, const Ipv4Mask mask, |
56 void Init (const Ipv4Address net, const Ipv4Mask mask, |
37 const Ipv4Address addr); |
57 const Ipv4Address addr); |
38 |
58 |
|
59 /** |
|
60 * \internal |
|
61 * \brief Get the current network of the given Ipv4Mask |
|
62 * |
|
63 * Does not change the internal state; this just peeks at the current |
|
64 * network |
|
65 * |
|
66 * \param mask The Ipv4Mask for the current network |
|
67 * \returns the IPv4 address of the current network |
|
68 */ |
39 Ipv4Address GetNetwork (const Ipv4Mask mask) const; |
69 Ipv4Address GetNetwork (const Ipv4Mask mask) const; |
|
70 |
|
71 /** |
|
72 * \internal |
|
73 * \brief Get the next network according to the given Ipv4Mask |
|
74 * |
|
75 * This operation is a pre-increment, meaning that the internal state |
|
76 * is changed before returning the new network address. |
|
77 * |
|
78 * This also resets the address to the base address that was |
|
79 * used for initialization. |
|
80 * |
|
81 * \param mask The Ipv4Mask used to set the next network |
|
82 * \returns the IPv4 address of the next network |
|
83 */ |
40 Ipv4Address NextNetwork (const Ipv4Mask mask); |
84 Ipv4Address NextNetwork (const Ipv4Mask mask); |
41 |
85 |
|
86 /** |
|
87 * \internal |
|
88 * \brief Set the address for the given mask |
|
89 * |
|
90 * \param addr The address to set for the current mask |
|
91 * \param mask The Ipv4Mask whose address is to be set |
|
92 */ |
42 void InitAddress (const Ipv4Address addr, const Ipv4Mask mask); |
93 void InitAddress (const Ipv4Address addr, const Ipv4Mask mask); |
|
94 |
|
95 /** |
|
96 * \internal |
|
97 * \brief Allocate the next Ipv4Address for the configured network and mask |
|
98 * |
|
99 * This operation is a post-increment, meaning that the first address |
|
100 * allocated will be the one that was initially configured. |
|
101 * |
|
102 * \param mask The Ipv4Mask for the current network |
|
103 * \returns the IPv4 address |
|
104 */ |
|
105 Ipv4Address NextAddress (const Ipv4Mask mask); |
|
106 |
|
107 /** |
|
108 * \internal |
|
109 * \brief Get the Ipv4Address that will be allocated upon NextAddress () |
|
110 * |
|
111 * Does not change the internal state; just is used to peek the next |
|
112 * address that will be allocated upon NextAddress () |
|
113 * |
|
114 * \param mask The Ipv4Mask for the current network |
|
115 * \returns the IPv4 address |
|
116 */ |
43 Ipv4Address GetAddress (const Ipv4Mask mask) const; |
117 Ipv4Address GetAddress (const Ipv4Mask mask) const; |
44 Ipv4Address NextAddress (const Ipv4Mask mask); |
118 |
45 |
119 /** |
|
120 * \internal |
|
121 * \brief Reset the networks and Ipv4Address to zero |
|
122 */ |
46 void Reset (void); |
123 void Reset (void); |
|
124 |
|
125 /** |
|
126 * \internal |
|
127 * \brief Add the Ipv4Address to the list of IPv4 entries |
|
128 * |
|
129 * Typically, this is used by external address allocators that want |
|
130 * to make use of this class's ability to track duplicates. AddAllocated |
|
131 * is always called internally for any address generated by NextAddress () |
|
132 * |
|
133 * \param addr The Ipv4Address to be added to the list of Ipv4 entries |
|
134 * \returns true on success |
|
135 */ |
47 bool AddAllocated (const Ipv4Address addr); |
136 bool AddAllocated (const Ipv4Address addr); |
48 |
137 |
|
138 /** |
|
139 * \internal |
|
140 * \brief Used to turn off fatal errors and assertions, for testing |
|
141 */ |
49 void TestMode (void); |
142 void TestMode (void); |
50 private: |
143 private: |
51 static const uint32_t N_BITS = 32; |
144 static const uint32_t N_BITS = 32; //!< /internal the number of bits in the address |
52 static const uint32_t MOST_SIGNIFICANT_BIT = 0x80000000; |
145 static const uint32_t MOST_SIGNIFICANT_BIT = 0x80000000; //!< /internal MSB set to 1 |
53 |
146 |
|
147 /** |
|
148 * \internal |
|
149 * \brief Create an index number for the network mask |
|
150 * \param mask the mask to index |
|
151 * \returns an index |
|
152 */ |
54 uint32_t MaskToIndex (Ipv4Mask mask) const; |
153 uint32_t MaskToIndex (Ipv4Mask mask) const; |
55 |
154 |
|
155 /** |
|
156 * \internal |
|
157 * \brief This class holds the state for a given network |
|
158 */ |
56 class NetworkState |
159 class NetworkState |
57 { |
160 { |
58 public: |
161 public: |
59 uint32_t mask; |
162 uint32_t mask; //!< /internal the network mask |
60 uint32_t shift; |
163 uint32_t shift; //!< /internal a shift |
61 uint32_t network; |
164 uint32_t network; //!< /internal the network |
62 uint32_t addr; |
165 uint32_t addr; //!< /internal the address |
63 uint32_t addrMax; |
166 uint32_t addrMax; //!< /internal the maximum address |
64 }; |
167 }; |
65 |
168 |
66 NetworkState m_netTable[N_BITS]; |
169 NetworkState m_netTable[N_BITS]; //!< /internal the available networks |
67 |
170 |
|
171 /** |
|
172 * \internal |
|
173 * \brief This class holds the allocated addresses |
|
174 */ |
68 class Entry |
175 class Entry |
69 { |
176 { |
70 public: |
177 public: |
71 uint32_t addrLow; |
178 uint32_t addrLow; //!< /internal the lowest allocated address |
72 uint32_t addrHigh; |
179 uint32_t addrHigh; //!< /internal the highest allocated address |
73 }; |
180 }; |
74 |
181 |
75 std::list<Entry> m_entries; |
182 std::list<Entry> m_entries; //!< /internal contained of allocated addresses |
76 bool m_test; |
183 bool m_test; //!< /internal test mode (if true) |
77 }; |
184 }; |
78 |
185 |
79 Ipv4AddressGeneratorImpl::Ipv4AddressGeneratorImpl () |
186 Ipv4AddressGeneratorImpl::Ipv4AddressGeneratorImpl () |
80 : m_entries (), m_test (false) |
187 : m_entries (), m_test (false) |
81 { |
188 { |