20 #include "ns3/abort.h"
21 #include "ns3/assert.h"
23 #include "ns3/simulation-singleton.h"
24 #include "ipv4-address-generator.h"
51 static const uint32_t N_BITS = 32;
52 static const uint32_t MOST_SIGNIFICANT_BIT = 0x80000000;
54 uint32_t MaskToIndex (
Ipv4Mask mask)
const;
75 std::list<Entry> m_entries;
79 Ipv4AddressGeneratorImpl::Ipv4AddressGeneratorImpl ()
80 : m_entries (), m_test (false)
87 Ipv4AddressGeneratorImpl::Reset (
void)
111 for (uint32_t i = 0; i < N_BITS; ++i)
113 m_netTable[i].mask = mask;
115 mask |= MOST_SIGNIFICANT_BIT;
116 m_netTable[i].network = 1;
117 m_netTable[i].addr = 1;
118 m_netTable[i].addrMax = ~mask;
119 m_netTable[i].shift = N_BITS - i;
125 Ipv4AddressGeneratorImpl::~Ipv4AddressGeneratorImpl ()
131 Ipv4AddressGeneratorImpl::Init (
132 const Ipv4Address net,
134 const Ipv4Address addr)
141 uint32_t maskBits = mask.Get ();
142 uint32_t netBits = net.Get ();
143 uint32_t addrBits = addr.Get ();
147 NS_ABORT_MSG_UNLESS ((netBits & ~maskBits) == 0,
"Ipv4AddressGeneratorImpl::Init (): Inconsistent network and mask");
148 NS_ABORT_MSG_UNLESS ((addrBits & maskBits) == 0,
"Ipv4AddressGeneratorImpl::Init (): Inconsistent address and mask");
156 uint32_t index = MaskToIndex (mask);
158 m_netTable[index].network = netBits >> m_netTable[index].shift;
159 NS_ABORT_MSG_UNLESS (addrBits <= m_netTable[index].addrMax,
"Ipv4AddressGeneratorImpl::Init(): Address overflow");
160 m_netTable[index].addr = addrBits;
165 Ipv4AddressGeneratorImpl::GetNetwork (
166 const Ipv4Mask mask)
const
170 uint32_t index = MaskToIndex (mask);
171 return Ipv4Address (m_netTable[index].network << m_netTable[index].shift);
175 Ipv4AddressGeneratorImpl::NextNetwork (
188 uint32_t index = MaskToIndex (mask);
189 ++m_netTable[index].network;
190 return Ipv4Address (m_netTable[index].network << m_netTable[index].shift);
194 Ipv4AddressGeneratorImpl::InitAddress (
195 const Ipv4Address addr,
200 uint32_t index = MaskToIndex (mask);
201 uint32_t addrBits = addr.Get ();
203 NS_ABORT_MSG_UNLESS (addrBits <= m_netTable[index].addrMax,
"Ipv4AddressGeneratorImpl::InitAddress(): Address overflow");
204 m_netTable[index].addr = addrBits;
208 Ipv4AddressGeneratorImpl::GetAddress (
209 const Ipv4Mask mask)
const
213 uint32_t index = MaskToIndex (mask);
216 (m_netTable[index].network << m_netTable[index].shift) |
217 m_netTable[index].addr);
221 Ipv4AddressGeneratorImpl::NextAddress (
const Ipv4Mask mask)
231 uint32_t index = MaskToIndex (mask);
234 "Ipv4AddressGeneratorImpl::NextAddress(): Address overflow");
236 Ipv4Address addr = Ipv4Address (
237 (m_netTable[index].network << m_netTable[index].shift) |
238 m_netTable[index].addr);
240 ++m_netTable[index].addr;
250 Ipv4AddressGeneratorImpl::AddAllocated (
const Ipv4Address address)
254 uint32_t addr = address.Get ();
256 NS_ABORT_MSG_UNLESS (addr,
"Ipv4AddressGeneratorImpl::Add(): Allocating the broadcast address is not a good idea");
258 std::list<Entry>::iterator i;
260 for (i = m_entries.begin (); i != m_entries.end (); ++i)
262 NS_LOG_LOGIC (
"examine entry: " << Ipv4Address ((*i).addrLow) <<
263 " to " << Ipv4Address ((*i).addrHigh));
268 if (addr >= (*i).addrLow && addr <= (*i).addrHigh)
270 NS_LOG_LOGIC (
"Ipv4AddressGeneratorImpl::Add(): Address Collision: " << Ipv4Address (addr));
273 NS_FATAL_ERROR (
"Ipv4AddressGeneratorImpl::Add(): Address Collision: " << Ipv4Address (addr));
282 if (addr < (*i).addrLow - 1)
293 if (addr == (*i).addrHigh + 1)
295 std::list<Entry>::iterator j = i;
298 if (j != m_entries.end ())
300 if (addr == (*j).addrLow)
303 "Address Collision: " << Ipv4Address (addr));
306 NS_FATAL_ERROR (
"Ipv4AddressGeneratorImpl::Add(): Address Collision: " << Ipv4Address (addr));
313 (*i).addrHigh = addr;
323 if (addr == (*i).addrLow - 1)
332 entry.addrLow = entry.addrHigh = addr;
333 m_entries.insert (i, entry);
338 Ipv4AddressGeneratorImpl::TestMode (
void)
345 Ipv4AddressGeneratorImpl::MaskToIndex (Ipv4Mask mask)
const
361 uint32_t maskBits = mask.Get ();
363 for (uint32_t i = 0; i < N_BITS; ++i)
367 uint32_t index = N_BITS - i;
368 NS_ABORT_MSG_UNLESS (index > 0 && index < N_BITS,
"Ipv4AddressGenerator::MaskToIndex(): Illegal Mask");
373 NS_ASSERT_MSG (
false,
"Ipv4AddressGenerator::MaskToIndex(): Impossible");
378 Ipv4AddressGenerator::Init (
379 const Ipv4Address net,
381 const Ipv4Address addr)
386 ->Init (net, mask, addr);
390 Ipv4AddressGenerator::NextNetwork (
const Ipv4Mask mask)
395 ->NextNetwork (mask);
399 Ipv4AddressGenerator::GetNetwork (
const Ipv4Mask mask)
408 Ipv4AddressGenerator::InitAddress (
409 const Ipv4Address addr,
415 ->InitAddress (addr, mask);
419 Ipv4AddressGenerator::GetAddress (
const Ipv4Mask mask)
428 Ipv4AddressGenerator::NextAddress (
const Ipv4Mask mask)
433 ->NextAddress (mask);
437 Ipv4AddressGenerator::Reset (
void)
446 Ipv4AddressGenerator::AddAllocated (
const Ipv4Address addr)
451 ->AddAllocated (addr);
455 Ipv4AddressGenerator::TestMode (
void)
#define NS_LOG_FUNCTION(parameters)
a class to represent an Ipv4 address mask
#define NS_LOG_COMPONENT_DEFINE(name)
#define NS_LOG_FUNCTION_NOARGS()
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if cond is false.
#define NS_FATAL_ERROR(msg)
fatal error handling
#define NS_LOG_LOGIC(msg)
#define NS_ASSERT_MSG(condition, message)
Ipv4 addresses are stored in host order in this class.