20 #include "ns3/abort.h"
21 #include "ns3/assert.h"
23 #include "ns3/simulation-singleton.h"
38 class Ipv4AddressGeneratorImpl
41 Ipv4AddressGeneratorImpl ();
42 virtual ~Ipv4AddressGeneratorImpl ();
54 void Init (
const Ipv4Address net,
const Ipv4Mask mask,
55 const Ipv4Address addr);
66 Ipv4Address GetNetwork (
const Ipv4Mask mask)
const;
80 Ipv4Address NextNetwork (
const Ipv4Mask mask);
88 void InitAddress (
const Ipv4Address addr,
const Ipv4Mask mask);
99 Ipv4Address NextAddress (
const Ipv4Mask mask);
110 Ipv4Address GetAddress (
const Ipv4Mask mask)
const;
127 bool AddAllocated (
const Ipv4Address addr);
132 void TestMode (
void);
134 static const uint32_t
N_BITS = 32;
135 static const uint32_t MOST_SIGNIFICANT_BIT = 0x80000000;
142 uint32_t MaskToIndex (Ipv4Mask mask)
const;
157 NetworkState m_netTable[
N_BITS];
169 std::list<Entry> m_entries;
173 Ipv4AddressGeneratorImpl::Ipv4AddressGeneratorImpl ()
174 : m_entries (), m_test (false)
205 for (uint32_t i = 0; i <
N_BITS; ++i)
207 m_netTable[i].mask = mask;
209 mask |= MOST_SIGNIFICANT_BIT;
210 m_netTable[i].network = 1;
211 m_netTable[i].addr = 1;
212 m_netTable[i].addrMax = ~m_netTable[i].mask;
213 m_netTable[i].shift = N_BITS - i;
219 Ipv4AddressGeneratorImpl::~Ipv4AddressGeneratorImpl ()
225 Ipv4AddressGeneratorImpl::Init (
226 const Ipv4Address net,
228 const Ipv4Address addr)
235 uint32_t maskBits = mask.Get ();
236 uint32_t netBits = net.Get ();
237 uint32_t addrBits = addr.Get ();
241 NS_ABORT_MSG_UNLESS ((netBits & ~maskBits) == 0,
"Ipv4AddressGeneratorImpl::Init (): Inconsistent network and mask");
242 NS_ABORT_MSG_UNLESS ((addrBits & maskBits) == 0,
"Ipv4AddressGeneratorImpl::Init (): Inconsistent address and mask");
250 uint32_t index = MaskToIndex (mask);
252 m_netTable[index].network = netBits >> m_netTable[index].shift;
254 NS_ABORT_MSG_UNLESS (addrBits <= m_netTable[index].addrMax,
"Ipv4AddressGeneratorImpl::Init(): Address overflow");
255 m_netTable[index].addr = addrBits;
260 Ipv4AddressGeneratorImpl::GetNetwork (
261 const Ipv4Mask mask)
const
265 uint32_t index = MaskToIndex (mask);
266 return Ipv4Address (m_netTable[index].network << m_netTable[index].shift);
270 Ipv4AddressGeneratorImpl::NextNetwork (
283 uint32_t index = MaskToIndex (mask);
284 ++m_netTable[index].network;
285 return Ipv4Address (m_netTable[index].network << m_netTable[index].shift);
289 Ipv4AddressGeneratorImpl::InitAddress (
290 const Ipv4Address addr,
295 uint32_t index = MaskToIndex (mask);
296 uint32_t addrBits = addr.Get ();
298 NS_ABORT_MSG_UNLESS (addrBits <= m_netTable[index].addrMax,
"Ipv4AddressGeneratorImpl::InitAddress(): Address overflow");
299 m_netTable[index].addr = addrBits;
303 Ipv4AddressGeneratorImpl::GetAddress (
304 const Ipv4Mask mask)
const
308 uint32_t index = MaskToIndex (mask);
311 (m_netTable[index].network << m_netTable[index].shift) |
312 m_netTable[index].addr);
316 Ipv4AddressGeneratorImpl::NextAddress (
const Ipv4Mask mask)
326 uint32_t index = MaskToIndex (mask);
329 "Ipv4AddressGeneratorImpl::NextAddress(): Address overflow");
331 Ipv4Address addr = Ipv4Address (
332 (m_netTable[index].network << m_netTable[index].shift) |
333 m_netTable[index].addr);
335 ++m_netTable[index].addr;
345 Ipv4AddressGeneratorImpl::AddAllocated (
const Ipv4Address
address)
349 uint32_t addr = address.Get ();
351 NS_ABORT_MSG_UNLESS (addr,
"Ipv4AddressGeneratorImpl::Add(): Allocating the broadcast address is not a good idea");
353 std::list<Entry>::iterator i;
355 for (i = m_entries.begin (); i != m_entries.end (); ++i)
357 NS_LOG_LOGIC (
"examine entry: " << Ipv4Address ((*i).addrLow) <<
358 " to " << Ipv4Address ((*i).addrHigh));
363 if (addr >= (*i).addrLow && addr <= (*i).addrHigh)
365 NS_LOG_LOGIC (
"Ipv4AddressGeneratorImpl::Add(): Address Collision: " << Ipv4Address (addr));
368 NS_FATAL_ERROR (
"Ipv4AddressGeneratorImpl::Add(): Address Collision: " << Ipv4Address (addr));
377 if (addr < (*i).addrLow - 1)
388 if (addr == (*i).addrHigh + 1)
390 std::list<Entry>::iterator j = i;
393 if (j != m_entries.end ())
395 if (addr == (*j).addrLow)
398 "Address Collision: " << Ipv4Address (addr));
401 NS_FATAL_ERROR (
"Ipv4AddressGeneratorImpl::Add(): Address Collision: " << Ipv4Address (addr));
408 (*i).addrHigh = addr;
418 if (addr == (*i).addrLow - 1)
427 entry.addrLow = entry.addrHigh = addr;
428 m_entries.insert (i, entry);
433 Ipv4AddressGeneratorImpl::TestMode (
void)
440 Ipv4AddressGeneratorImpl::MaskToIndex (Ipv4Mask mask)
const
456 uint32_t maskBits = mask.Get ();
458 for (uint32_t i = 0; i <
N_BITS; ++i)
462 uint32_t index = N_BITS - i;
463 NS_ABORT_MSG_UNLESS (index > 0 && index < N_BITS,
"Ipv4AddressGenerator::MaskToIndex(): Illegal Mask");
468 NS_ASSERT_MSG (
false,
"Ipv4AddressGenerator::MaskToIndex(): Impossible");
474 const Ipv4Address net,
476 const Ipv4Address addr)
481 ->Init (net, mask, addr);
490 ->NextNetwork (mask);
504 const Ipv4Address addr,
510 ->InitAddress (addr, mask);
528 ->NextAddress (mask);
546 ->AddAllocated (addr);
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
static T * Get(void)
Get a pointer to the singleton instance.
static void Init(const Ipv4Address net, const Ipv4Mask mask, const Ipv4Address addr="0.0.0.1")
Initialise the base network, mask and address for the generator.
const uint32_t N_BITS
number of bits in a IPv4 address
static Ipv4Address NextAddress(const Ipv4Mask mask)
Allocate the next Ipv4Address for the configured network and mask.
static Ipv4Address GetAddress(const Ipv4Mask mask)
Get the Ipv4Address that will be allocated upon NextAddress ()
static bool AddAllocated(const Ipv4Address addr)
Add the Ipv4Address to the list of IPv4 entries.
static void InitAddress(const Ipv4Address addr, const Ipv4Mask mask)
Set the address for the given mask.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static void Reset(void)
Reset the networks and Ipv4Address to zero.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
void Reset(void)
Reset the initial value of every attribute as well as the value of every global to what they were bef...
static Ipv4Address NextNetwork(const Ipv4Mask mask)
Get the next network according to the given Ipv4Mask.
static void TestMode(void)
Used to turn off fatal errors and assertions, for testing.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
static Ipv4Address GetNetwork(const Ipv4Mask mask)
Get the current network of the given Ipv4Mask.
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.