21 #include "ns3/abort.h"
22 #include "ns3/assert.h"
24 #include "ns3/simulation-singleton.h"
40 class Ipv6AddressGeneratorImpl
43 Ipv6AddressGeneratorImpl ();
44 virtual ~Ipv6AddressGeneratorImpl ();
56 void Init (
const Ipv6Address net,
const Ipv6Prefix prefix,
57 const Ipv6Address interfaceId);
71 Ipv6Address NextNetwork (
const Ipv6Prefix prefix);
82 Ipv6Address GetNetwork (
const Ipv6Prefix prefix)
const;
90 void InitAddress (
const Ipv6Address interfaceId,
const Ipv6Prefix prefix);
101 Ipv6Address GetAddress (
const Ipv6Prefix prefix)
const;
112 Ipv6Address NextAddress (
const Ipv6Prefix prefix);
129 bool AddAllocated (
const Ipv6Address addr);
134 void TestMode (
void);
137 static const uint32_t
N_BITS = 128;
138 static const uint32_t MOST_SIGNIFICANT_BIT = 0x80;
145 uint32_t PrefixToIndex (Ipv6Prefix prefix)
const;
160 NetworkState m_netTable[
N_BITS];
169 uint8_t addrHigh[16];
172 std::list<Entry> m_entries;
177 Ipv6AddressGeneratorImpl::Ipv6AddressGeneratorImpl ()
191 uint8_t prefix[16] = { 0};
193 for (uint32_t i = 0; i <
N_BITS; ++i)
195 for (uint32_t j = 0; j < 16; ++j)
197 m_netTable[i].prefix[j] = prefix[j];
199 for (uint32_t j = 0; j < 15; ++j)
201 prefix[15 - j] >>= 1;
202 prefix[15 - j] |= (prefix[15 - j - 1] & 1);
204 prefix[0] |= MOST_SIGNIFICANT_BIT;
205 for (uint32_t j = 0; j < 15; ++j)
207 m_netTable[i].network[j] = 0;
209 m_netTable[i].network[15] = 1;
210 for (uint32_t j = 0; j < 15; ++j)
212 m_netTable[i].addr[j] = 0;
214 m_netTable[i].addr[15] = 1;
215 for (uint32_t j = 0; j < 16; ++j)
217 m_netTable[i].addrMax[j] = ~prefix[j];
219 m_netTable[i].shift = N_BITS - i;
222 m_base = Ipv6Address (
"::1");
226 Ipv6AddressGeneratorImpl::~Ipv6AddressGeneratorImpl ()
232 Ipv6AddressGeneratorImpl::Init (
233 const Ipv6Address net,
234 const Ipv6Prefix prefix,
235 const Ipv6Address interfaceId)
239 m_base = interfaceId;
244 uint8_t prefixBits[16];
245 prefix.GetBytes (prefixBits);
247 net.GetBytes (netBits);
248 uint8_t interfaceIdBits[16];
249 interfaceId.GetBytes (interfaceIdBits);
258 uint32_t index = PrefixToIndex (prefix);
260 uint32_t a = m_netTable[index].shift / 8;
261 uint32_t b = m_netTable[index].shift % 8;
262 for (int32_t j = 15 - a; j >= 0; j--)
264 m_netTable[index].network[j + a] = netBits[j];
266 for (uint32_t j = 0; j < a; j++)
268 m_netTable[index].network[j] = 0;
270 for (uint32_t j = 15; j >= a; j--)
272 m_netTable[index].network[j] = m_netTable[index].network[j] >> b;
273 m_netTable[index].network[j] |= m_netTable[index].network[j - 1] << (8 - b);
275 for (int32_t j = 0; j < 16; j++)
277 m_netTable[index].addr[j] = interfaceIdBits[j];
283 Ipv6AddressGeneratorImpl::GetNetwork (
284 const Ipv6Prefix prefix)
const
287 uint8_t nw[16] = { 0 };
288 uint32_t index = PrefixToIndex (prefix);
289 uint32_t a = m_netTable[index].shift / 8;
290 uint32_t b = m_netTable[index].shift % 8;
291 for (uint32_t j = 0; j < 16 - a; ++j)
293 nw[j] = m_netTable[index].network[j + a];
295 for (uint32_t j = 0; j < 15; j++)
298 nw[j] |= nw[j + 1] >> (8 - b);
300 nw[15] = nw[15] << b;
302 return Ipv6Address (nw);
306 Ipv6AddressGeneratorImpl::NextNetwork (
307 const Ipv6Prefix prefix)
311 uint32_t index = PrefixToIndex (prefix);
313 uint8_t interfaceIdBits[16];
314 m_base.GetBytes (interfaceIdBits);
315 for (int32_t j = 0; j < 16; j++)
317 m_netTable[index].addr[j] = interfaceIdBits[j];
320 for (int32_t j = 15; j >= 0; j--)
322 if (m_netTable[index].network[j] < 0xff)
324 ++m_netTable[index].network[j];
329 ++m_netTable[index].network[j];
334 uint32_t a = m_netTable[index].shift / 8;
335 uint32_t b = m_netTable[index].shift % 8;
336 for (uint32_t j = 0; j < 16 - a; ++j)
338 nw[j] = m_netTable[index].network[j + a];
340 for (uint32_t j = 16 - a; j < 16; ++j)
344 for (uint32_t j = 0; j < 15; j++)
347 nw[j] |= nw[j + 1] >> (8 - b);
349 nw[15] = nw[15] << b;
351 return Ipv6Address (nw);
356 Ipv6AddressGeneratorImpl::InitAddress (
357 const Ipv6Address interfaceId,
358 const Ipv6Prefix prefix)
362 uint32_t index = PrefixToIndex (prefix);
363 uint8_t interfaceIdBits[16];
364 interfaceId.GetBytes (interfaceIdBits);
366 for (uint32_t j = 0; j < 16; ++j)
368 m_netTable[index].addr[j] = interfaceIdBits[j];
373 Ipv6AddressGeneratorImpl::GetAddress (
const Ipv6Prefix prefix)
const
377 uint32_t index = PrefixToIndex (prefix);
379 uint8_t nw[16] = { 0 };
380 uint32_t a = m_netTable[index].shift / 8;
381 uint32_t b = m_netTable[index].shift % 8;
382 for (uint32_t j = 0; j < 16 - a; ++j)
384 nw[j] = m_netTable[index].network[j + a];
386 for (uint32_t j = 0; j < 15; j++)
389 nw[j] |= nw[j + 1] >> (8 - b);
391 nw[15] = nw[15] << b;
392 for (uint32_t j = 0; j < 16; j++)
394 nw[j] |= m_netTable[index].addr[j];
397 return Ipv6Address (nw);
401 Ipv6AddressGeneratorImpl::NextAddress (
const Ipv6Prefix prefix)
405 uint32_t index = PrefixToIndex (prefix);
407 uint8_t ad[16] = { 0 };
408 uint32_t a = m_netTable[index].shift / 8;
409 uint32_t b = m_netTable[index].shift % 8;
410 for (uint32_t j = 0; j < 16 - a; ++j)
412 ad[j] = m_netTable[index].network[j + a];
414 for (uint32_t j = 0; j < 15; j++)
417 ad[j] |= ad[j + 1] >> (8 - b);
419 ad[15] = ad[15] << b;
420 for (uint32_t j = 0; j < 16; j++)
422 ad[j] |= m_netTable[index].addr[j];
424 Ipv6Address addr = Ipv6Address (ad);
426 for (int32_t j = 15; j >= 0; j--)
428 if (m_netTable[index].addr[j] < 0xff)
430 ++m_netTable[index].addr[j];
435 ++m_netTable[index].addr[j];
448 Ipv6AddressGeneratorImpl::AddAllocated (
const Ipv6Address
address)
453 address.GetBytes (addr);
455 std::list<Entry>::iterator i;
457 for (i = m_entries.begin (); i != m_entries.end (); ++i)
459 NS_LOG_LOGIC (
"examine entry: " << Ipv6Address ((*i).addrLow) <<
460 " to " << Ipv6Address ((*i).addrHigh));
465 if (!(Ipv6Address (addr) < Ipv6Address ((*i).addrLow))
466 && ((Ipv6Address (addr) < Ipv6Address ((*i).addrHigh))
467 || (Ipv6Address (addr) == Ipv6Address ((*i).addrHigh))))
469 NS_LOG_LOGIC (
"Ipv6AddressGeneratorImpl::Add(): Address Collision: " << Ipv6Address (addr));
472 NS_FATAL_ERROR (
"Ipv6AddressGeneratorImpl::Add(): Address Collision: " << Ipv6Address (addr));
482 for (uint32_t j = 0; j < 16; j++)
484 taddr[j] = (*i).addrLow[j];
487 if (Ipv6Address (addr) < Ipv6Address (taddr))
498 for (uint32_t j = 0; j < 16; j++)
500 taddr[j] = (*i).addrLow[j];
503 if (Ipv6Address (addr) == Ipv6Address (taddr))
505 std::list<Entry>::iterator j = i;
508 if (j != m_entries.end ())
510 if (Ipv6Address (addr) == Ipv6Address ((*j).addrLow))
513 "Address Collision: " << Ipv6Address (addr));
516 NS_FATAL_ERROR (
"Ipv6AddressGeneratorImpl::Add(): Address Collision: " << Ipv6Address (addr));
523 for (uint32_t j = 0; j < 16; j++)
525 (*i).addrHigh[j] = addr[j];
536 for (uint32_t j = 0; j < 16; j++)
538 taddr[j] = (*i).addrLow[j];
541 if ((Ipv6Address (addr) == Ipv6Address (taddr)))
544 for (uint32_t j = 0; j < 16; j++)
546 (*i).addrLow[j] = addr[j];
553 for (uint32_t j = 0; j < 16; j++)
555 entry.addrLow[j] = entry.addrHigh[j] = addr[j];
557 m_entries.insert (i, entry);
562 Ipv6AddressGeneratorImpl::TestMode (
void)
569 Ipv6AddressGeneratorImpl::PrefixToIndex (Ipv6Prefix prefix)
const
582 uint8_t prefixBits[16];
583 prefix.GetBytes (prefixBits);
585 for (int32_t i = 15; i >= 0; --i)
587 for (uint32_t j = 0; j < 8; ++j)
589 if (prefixBits[i] & 1)
591 uint32_t index = N_BITS - (15 - i) * 8 - j;
592 NS_ABORT_MSG_UNLESS (index > 0 && index < N_BITS,
"Ip64AddressGenerator::PrefixToIndex(): Illegal Prefix");
598 NS_ASSERT_MSG (
false,
"Ipv6AddressGenerator::PrefixToIndex(): Impossible");
604 const Ipv6Address net,
605 const Ipv6Prefix prefix,
606 const Ipv6Address interfaceId)
611 ->Init (net, prefix, interfaceId);
620 ->NextNetwork (prefix);
629 ->GetNetwork (prefix);
634 const Ipv6Address interfaceId,
635 const Ipv6Prefix prefix)
640 ->InitAddress (interfaceId, prefix);
649 ->GetAddress (prefix);
658 ->NextAddress (prefix);
676 ->AddAllocated (addr);
static Ipv6Address NextAddress(const Ipv6Prefix prefix)
Allocate the next Ipv6Address for the configured network and prefix.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
static void TestMode(void)
Used to turn off fatal errors and assertions, for testing.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
static void Reset(void)
Reset the networks and Ipv6Address to zero.
#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.
const uint32_t N_BITS
number of bits in a IPv4 address
static Ipv6Address GetAddress(const Ipv6Prefix prefix)
Get the Ipv6Address that will be allocated upon NextAddress ()
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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...
#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 Ipv6Address NextNetwork(const Ipv6Prefix prefix)
Get the next network according to the given Ipv6Prefix.
static void Init(const Ipv6Address net, const Ipv6Prefix prefix, const Ipv6Address interfaceId="::1")
Initialise the base network and interfaceId for the generator.
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
static bool AddAllocated(const Ipv6Address addr)
Add the Ipv6Address to the list of IPv6 entries.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
static Ipv6Address GetNetwork(const Ipv6Prefix prefix)
Get the current network of the given Ipv6Prefix.
static void InitAddress(const Ipv6Address interfaceId, const Ipv6Prefix prefix)
Set the interfaceId for the given Ipv6Prefix.