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);
137 bool IsAddressAllocated (
const Ipv6Address addr);
146 bool IsNetworkAllocated (
const Ipv6Address addr,
const Ipv6Prefix prefix);
151 void TestMode (
void);
154 static const uint32_t
N_BITS = 128;
155 static const uint32_t MOST_SIGNIFICANT_BIT = 0x80;
162 uint32_t PrefixToIndex (Ipv6Prefix prefix)
const;
177 NetworkState m_netTable[
N_BITS];
186 uint8_t addrHigh[16];
189 std::list<Entry> m_entries;
194 Ipv6AddressGeneratorImpl::Ipv6AddressGeneratorImpl ()
208 uint8_t prefix[16] = { 0};
210 for (uint32_t i = 0; i <
N_BITS; ++i)
212 for (uint32_t j = 0; j < 16; ++j)
214 m_netTable[i].prefix[j] = prefix[j];
216 for (uint32_t j = 0; j < 15; ++j)
218 prefix[15 - j] >>= 1;
219 prefix[15 - j] |= (prefix[15 - j - 1] & 1);
221 prefix[0] |= MOST_SIGNIFICANT_BIT;
222 for (uint32_t j = 0; j < 15; ++j)
224 m_netTable[i].network[j] = 0;
226 m_netTable[i].network[15] = 1;
227 for (uint32_t j = 0; j < 15; ++j)
229 m_netTable[i].addr[j] = 0;
231 m_netTable[i].addr[15] = 1;
232 for (uint32_t j = 0; j < 16; ++j)
234 m_netTable[i].addrMax[j] = ~prefix[j];
236 m_netTable[i].shift =
N_BITS - i;
239 m_base = Ipv6Address (
"::1");
243 Ipv6AddressGeneratorImpl::~Ipv6AddressGeneratorImpl ()
249 Ipv6AddressGeneratorImpl::Init (
250 const Ipv6Address net,
251 const Ipv6Prefix prefix,
252 const Ipv6Address interfaceId)
256 m_base = interfaceId;
261 uint8_t prefixBits[16];
262 prefix.GetBytes (prefixBits);
264 net.GetBytes (netBits);
265 uint8_t interfaceIdBits[16];
266 interfaceId.GetBytes (interfaceIdBits);
275 uint32_t index = PrefixToIndex (prefix);
277 uint32_t a = m_netTable[index].shift / 8;
278 uint32_t b = m_netTable[index].shift % 8;
279 for (int32_t j = 15 - a; j >= 0; j--)
281 m_netTable[index].network[j + a] = netBits[j];
283 for (uint32_t j = 0; j < a; j++)
285 m_netTable[index].network[j] = 0;
287 for (uint32_t j = 15; j >= a; j--)
289 m_netTable[index].network[j] = m_netTable[index].network[j] >> b;
290 m_netTable[index].network[j] |= m_netTable[index].network[j - 1] << (8 - b);
292 for (int32_t j = 0; j < 16; j++)
294 m_netTable[index].addr[j] = interfaceIdBits[j];
300 Ipv6AddressGeneratorImpl::GetNetwork (
301 const Ipv6Prefix prefix)
const 304 uint8_t nw[16] = { 0 };
305 uint32_t index = PrefixToIndex (prefix);
306 uint32_t a = m_netTable[index].shift / 8;
307 uint32_t b = m_netTable[index].shift % 8;
308 for (uint32_t j = 0; j < 16 - a; ++j)
310 nw[j] = m_netTable[index].network[j + a];
312 for (uint32_t j = 0; j < 15; j++)
315 nw[j] |= nw[j + 1] >> (8 - b);
317 nw[15] = nw[15] << b;
319 return Ipv6Address (nw);
323 Ipv6AddressGeneratorImpl::NextNetwork (
324 const Ipv6Prefix prefix)
328 uint32_t index = PrefixToIndex (prefix);
330 uint8_t interfaceIdBits[16];
331 m_base.GetBytes (interfaceIdBits);
332 for (int32_t j = 0; j < 16; j++)
334 m_netTable[index].addr[j] = interfaceIdBits[j];
337 for (int32_t j = 15; j >= 0; j--)
339 if (m_netTable[index].network[j] < 0xff)
341 ++m_netTable[index].network[j];
346 ++m_netTable[index].network[j];
351 uint32_t a = m_netTable[index].shift / 8;
352 uint32_t b = m_netTable[index].shift % 8;
353 for (uint32_t j = 0; j < 16 - a; ++j)
355 nw[j] = m_netTable[index].network[j + a];
357 for (uint32_t j = 16 - a; j < 16; ++j)
361 for (uint32_t j = 0; j < 15; j++)
364 nw[j] |= nw[j + 1] >> (8 - b);
366 nw[15] = nw[15] << b;
368 return Ipv6Address (nw);
373 Ipv6AddressGeneratorImpl::InitAddress (
374 const Ipv6Address interfaceId,
375 const Ipv6Prefix prefix)
379 uint32_t index = PrefixToIndex (prefix);
380 uint8_t interfaceIdBits[16];
381 interfaceId.GetBytes (interfaceIdBits);
383 for (uint32_t j = 0; j < 16; ++j)
385 m_netTable[index].addr[j] = interfaceIdBits[j];
390 Ipv6AddressGeneratorImpl::GetAddress (
const Ipv6Prefix prefix)
const 394 uint32_t index = PrefixToIndex (prefix);
396 uint8_t nw[16] = { 0 };
397 uint32_t a = m_netTable[index].shift / 8;
398 uint32_t b = m_netTable[index].shift % 8;
399 for (uint32_t j = 0; j < 16 - a; ++j)
401 nw[j] = m_netTable[index].network[j + a];
403 for (uint32_t j = 0; j < 15; j++)
406 nw[j] |= nw[j + 1] >> (8 - b);
408 nw[15] = nw[15] << b;
409 for (uint32_t j = 0; j < 16; j++)
411 nw[j] |= m_netTable[index].addr[j];
414 return Ipv6Address (nw);
418 Ipv6AddressGeneratorImpl::NextAddress (
const Ipv6Prefix prefix)
422 uint32_t index = PrefixToIndex (prefix);
424 uint8_t ad[16] = { 0 };
425 uint32_t a = m_netTable[index].shift / 8;
426 uint32_t b = m_netTable[index].shift % 8;
427 for (uint32_t j = 0; j < 16 - a; ++j)
429 ad[j] = m_netTable[index].network[j + a];
431 for (uint32_t j = 0; j < 15; j++)
434 ad[j] |= ad[j + 1] >> (8 - b);
436 ad[15] = ad[15] << b;
437 for (uint32_t j = 0; j < 16; j++)
439 ad[j] |= m_netTable[index].addr[j];
441 Ipv6Address addr = Ipv6Address (ad);
443 for (int32_t j = 15; j >= 0; j--)
445 if (m_netTable[index].addr[j] < 0xff)
447 ++m_netTable[index].addr[j];
452 ++m_netTable[index].addr[j];
465 Ipv6AddressGeneratorImpl::AddAllocated (
const Ipv6Address
address)
472 std::list<Entry>::iterator i;
474 for (i = m_entries.begin (); i != m_entries.end (); ++i)
476 NS_LOG_LOGIC (
"examine entry: " << Ipv6Address ((*i).addrLow) <<
477 " to " << Ipv6Address ((*i).addrHigh));
482 if (!(Ipv6Address (addr) < Ipv6Address ((*i).addrLow))
483 && ((Ipv6Address (addr) < Ipv6Address ((*i).addrHigh))
484 || (Ipv6Address (addr) == Ipv6Address ((*i).addrHigh))))
486 NS_LOG_LOGIC (
"Ipv6AddressGeneratorImpl::Add(): Address Collision: " << Ipv6Address (addr));
489 NS_FATAL_ERROR (
"Ipv6AddressGeneratorImpl::Add(): Address Collision: " << Ipv6Address (addr));
499 for (uint32_t j = 0; j < 16; j++)
501 taddr[j] = (*i).addrLow[j];
504 if (Ipv6Address (addr) < Ipv6Address (taddr))
515 for (uint32_t j = 0; j < 16; j++)
517 taddr[j] = (*i).addrLow[j];
520 if (Ipv6Address (addr) == Ipv6Address (taddr))
522 std::list<Entry>::iterator j = i;
525 if (j != m_entries.end ())
527 if (Ipv6Address (addr) == Ipv6Address ((*j).addrLow))
530 "Address Collision: " << Ipv6Address (addr));
533 NS_FATAL_ERROR (
"Ipv6AddressGeneratorImpl::Add(): Address Collision: " << Ipv6Address (addr));
540 for (uint32_t j = 0; j < 16; j++)
542 (*i).addrHigh[j] = addr[j];
553 for (uint32_t j = 0; j < 16; j++)
555 taddr[j] = (*i).addrLow[j];
558 if ((Ipv6Address (addr) == Ipv6Address (taddr)))
561 for (uint32_t j = 0; j < 16; j++)
563 (*i).addrLow[j] = addr[j];
570 for (uint32_t j = 0; j < 16; j++)
572 entry.addrLow[j] = entry.addrHigh[j] = addr[j];
574 m_entries.insert (i, entry);
579 Ipv6AddressGeneratorImpl::IsAddressAllocated (
const Ipv6Address
address)
586 std::list<Entry>::iterator i;
588 for (i = m_entries.begin (); i != m_entries.end (); ++i)
590 NS_LOG_LOGIC (
"examine entry: " << Ipv6Address ((*i).addrLow) <<
591 " to " << Ipv6Address ((*i).addrHigh));
593 if (!(Ipv6Address (addr) < Ipv6Address ((*i).addrLow))
594 && ((Ipv6Address (addr) < Ipv6Address ((*i).addrHigh))
595 || (Ipv6Address (addr) == Ipv6Address ((*i).addrHigh))))
597 NS_LOG_LOGIC (
"Ipv6AddressGeneratorImpl::IsAddressAllocated(): Address Collision: " << Ipv6Address (addr));
605 Ipv6AddressGeneratorImpl::IsNetworkAllocated (
const Ipv6Address
address,
const Ipv6Prefix prefix)
611 "Ipv6AddressGeneratorImpl::IsNetworkAllocated(): network address and mask don't match " <<
address <<
" " << prefix);
613 std::list<Entry>::iterator i;
615 for (i = m_entries.begin (); i != m_entries.end (); ++i)
617 NS_LOG_LOGIC (
"examine entry: " << Ipv6Address ((*i).addrLow) <<
" to " << Ipv6Address ((*i).addrHigh));
618 Ipv6Address low = Ipv6Address ((*i).addrLow);
619 Ipv6Address high = Ipv6Address ((*i).addrHigh);
621 if (
address == low.CombinePrefix (prefix) ||
address == high.CombinePrefix (prefix))
623 NS_LOG_LOGIC (
"Ipv6AddressGeneratorImpl::IsNetworkAllocated(): Network already allocated: " <<
624 address <<
" " << low <<
"-" << high);
634 Ipv6AddressGeneratorImpl::TestMode (
void)
641 Ipv6AddressGeneratorImpl::PrefixToIndex (Ipv6Prefix prefix)
const 654 uint8_t prefixBits[16];
655 prefix.GetBytes (prefixBits);
657 for (int32_t i = 15; i >= 0; --i)
659 for (uint32_t j = 0; j < 8; ++j)
661 if (prefixBits[i] & 1)
663 uint32_t index =
N_BITS - (15 - i) * 8 - j;
670 NS_ASSERT_MSG (
false,
"Ipv6AddressGenerator::PrefixToIndex(): Impossible");
676 const Ipv6Address net,
677 const Ipv6Prefix prefix,
678 const Ipv6Address interfaceId)
683 ->Init (net, prefix, interfaceId);
692 ->NextNetwork (prefix);
701 ->GetNetwork (prefix);
706 const Ipv6Address interfaceId,
707 const Ipv6Prefix prefix)
712 ->InitAddress (interfaceId, prefix);
721 ->GetAddress (prefix);
730 ->NextAddress (prefix);
748 ->AddAllocated (addr);
757 ->IsAddressAllocated (addr);
766 ->IsNetworkAllocated (addr, prefix);
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 "...
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
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.
static bool IsNetworkAllocated(const Ipv6Address addr, const Ipv6Prefix prefix)
Check if a network has already allocated addresses.
#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 bool IsAddressAllocated(const Ipv6Address addr)
Check the Ipv6Address allocation in the list of IPv6 entries.
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...
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.