21 #include "ns3/abort.h"
22 #include "ns3/assert.h"
24 #include "ns3/simulation-singleton.h"
25 #include "ipv6-address-generator.h"
52 static const uint32_t N_BITS = 128;
53 static const uint32_t MOST_SIGNIFICANT_BIT = 0x80;
55 uint32_t PrefixToIndex (
Ipv6Prefix prefix)
const;
76 std::list<Entry> m_entries;
81 Ipv6AddressGeneratorImpl::Ipv6AddressGeneratorImpl ()
91 Ipv6AddressGeneratorImpl::Reset (
void)
95 uint8_t prefix[16] = { 0};
97 for (uint32_t i = 0; i < N_BITS; ++i)
99 for (uint32_t j = 0; j < 16; ++j)
101 m_netTable[i].prefix[j] = prefix[j];
103 for (uint32_t j = 0; j < 15; ++j)
105 prefix[15 - j] >>= 1;
106 prefix[15 - j] |= (prefix[15 - j - 1] & 1);
108 prefix[0] |= MOST_SIGNIFICANT_BIT;
109 for (uint32_t j = 0; j < 15; ++j)
111 m_netTable[i].network[j] = 0;
113 m_netTable[i].network[15] = 1;
114 for (uint32_t j = 0; j < 15; ++j)
116 m_netTable[i].addr[j] = 0;
118 m_netTable[i].addr[15] = 1;
119 for (uint32_t j = 0; j < 16; ++j)
121 m_netTable[i].addrMax[j] = ~prefix[j];
123 m_netTable[i].shift = N_BITS - i;
126 m_base = Ipv6Address (
"::1");
130 Ipv6AddressGeneratorImpl::~Ipv6AddressGeneratorImpl ()
136 Ipv6AddressGeneratorImpl::Init (
137 const Ipv6Address net,
138 const Ipv6Prefix prefix,
139 const Ipv6Address interfaceId)
143 m_base = interfaceId;
148 uint8_t prefixBits[16];
151 net.GetBytes (netBits);
152 uint8_t interfaceIdBits[16];
153 interfaceId.GetBytes (interfaceIdBits);
162 uint32_t index = PrefixToIndex (prefix);
164 uint32_t a = m_netTable[index].shift / 8;
165 uint32_t b = m_netTable[index].shift % 8;
166 for (int32_t j = 15 - a; j >= 0; j--)
168 m_netTable[index].network[j + a] = netBits[j];
170 for (uint32_t j = 0; j < a; j++)
172 m_netTable[index].network[j] = 0;
174 for (uint32_t j = 15; j >= a; j--)
176 m_netTable[index].network[j] = m_netTable[index].network[j] >> b;
177 m_netTable[index].network[j] |= m_netTable[index].network[j - 1] << (8 - b);
179 for (int32_t j = 0; j < 16; j++)
181 m_netTable[index].addr[j] = interfaceIdBits[j];
187 Ipv6AddressGeneratorImpl::GetNetwork (
188 const Ipv6Prefix prefix)
const
192 uint32_t index = PrefixToIndex (prefix);
193 uint32_t a = m_netTable[index].shift / 8;
194 uint32_t b = m_netTable[index].shift % 8;
195 for (uint32_t j = 0; j < 16 - a; ++j)
197 nw[j] = m_netTable[index].network[j + a];
199 for (uint32_t j = 16 - a; j < 16; ++j)
203 for (uint32_t j = 0; j < 15; j++)
206 nw[j] |= nw[j + 1] >> (8 - b);
208 nw[15] = nw[15] << b;
210 return Ipv6Address (nw);
214 Ipv6AddressGeneratorImpl::NextNetwork (
215 const Ipv6Prefix prefix)
219 uint32_t index = PrefixToIndex (prefix);
221 uint8_t interfaceIdBits[16];
223 for (int32_t j = 0; j < 16; j++)
225 m_netTable[index].addr[j] = interfaceIdBits[j];
228 for (int32_t j = 15; j >= 0; j--)
230 if (m_netTable[index].network[j] < 0xff)
232 ++m_netTable[index].network[j];
237 ++m_netTable[index].network[j];
242 uint32_t a = m_netTable[index].shift / 8;
243 uint32_t b = m_netTable[index].shift % 8;
244 for (uint32_t j = 0; j < 16 - a; ++j)
246 nw[j] = m_netTable[index].network[j + a];
248 for (uint32_t j = 16 - a; j < 16; ++j)
252 for (uint32_t j = 0; j < 15; j++)
255 nw[j] |= nw[j + 1] >> (8 - b);
257 nw[15] = nw[15] << b;
259 return Ipv6Address (nw);
264 Ipv6AddressGeneratorImpl::InitAddress (
265 const Ipv6Address interfaceId,
266 const Ipv6Prefix prefix)
270 uint32_t index = PrefixToIndex (prefix);
271 uint8_t interfaceIdBits[16];
272 interfaceId.GetBytes (interfaceIdBits);
274 for (uint32_t j = 0; j < 16; ++j)
276 m_netTable[index].addr[j] = interfaceIdBits[j];
281 Ipv6AddressGeneratorImpl::GetAddress (
const Ipv6Prefix prefix)
const
285 uint32_t index = PrefixToIndex (prefix);
288 uint32_t a = m_netTable[index].shift / 8;
289 uint32_t b = m_netTable[index].shift % 8;
290 for (uint32_t j = 0; j < 16 - a; ++j)
292 nw[j] = m_netTable[index].network[j + a];
294 for (uint32_t j = 16 - a; j < 16; ++j)
298 for (uint32_t j = 0; j < 15; j++)
301 nw[j] |= nw[j + 1] >> (8 - b);
303 nw[15] = nw[15] << b;
304 for (uint32_t j = 0; j < 16; j++)
306 nw[j] |= m_netTable[index].addr[j];
309 return Ipv6Address (nw);
313 Ipv6AddressGeneratorImpl::NextAddress (
const Ipv6Prefix prefix)
317 uint32_t index = PrefixToIndex (prefix);
320 uint32_t a = m_netTable[index].shift / 8;
321 uint32_t b = m_netTable[index].shift % 8;
322 for (uint32_t j = 0; j < 16 - a; ++j)
324 ad[j] = m_netTable[index].network[j + a];
326 for (uint32_t j = 16 - a; j < 16; ++j)
330 for (uint32_t j = 0; j < 15; j++)
333 ad[j] |= ad[j + 1] >> (8 - b);
335 ad[15] = ad[15] << b;
336 for (uint32_t j = 0; j < 16; j++)
338 ad[j] |= m_netTable[index].addr[j];
340 Ipv6Address addr = Ipv6Address (ad);
342 for (int32_t j = 15; j >= 0; j--)
344 if (m_netTable[index].addr[j] < 0xff)
346 ++m_netTable[index].addr[j];
351 ++m_netTable[index].addr[j];
364 Ipv6AddressGeneratorImpl::AddAllocated (
const Ipv6Address address)
369 address.GetBytes (addr);
371 std::list<Entry>::iterator i;
373 for (i = m_entries.begin (); i != m_entries.end (); ++i)
375 NS_LOG_LOGIC (
"examine entry: " << Ipv6Address ((*i).addrLow) <<
376 " to " << Ipv6Address ((*i).addrHigh));
381 if (!(Ipv6Address (addr) < Ipv6Address ((*i).addrLow))
382 && ((Ipv6Address (addr) < Ipv6Address ((*i).addrHigh))
383 || (Ipv6Address (addr) == Ipv6Address ((*i).addrHigh))))
385 NS_LOG_LOGIC (
"Ipv6AddressGeneratorImpl::Add(): Address Collision: " << Ipv6Address (addr));
388 NS_FATAL_ERROR (
"Ipv6AddressGeneratorImpl::Add(): Address Collision: " << Ipv6Address (addr));
398 for (uint32_t j = 0; j < 16; j++)
400 taddr[j] = (*i).addrLow[j];
403 if (Ipv6Address (addr) < Ipv6Address (taddr))
414 for (uint32_t j = 0; j < 16; j++)
416 taddr[j] = (*i).addrLow[j];
419 if (Ipv6Address (addr) == Ipv6Address (taddr))
421 std::list<Entry>::iterator j = i;
424 if (j != m_entries.end ())
426 if (Ipv6Address (addr) == Ipv6Address ((*j).addrLow))
429 "Address Collision: " << Ipv6Address (addr));
432 NS_FATAL_ERROR (
"Ipv6AddressGeneratorImpl::Add(): Address Collision: " << Ipv6Address (addr));
439 for (uint32_t j = 0; j < 16; j++)
441 (*i).addrHigh[j] = addr[j];
452 for (uint32_t j = 0; j < 16; j++)
454 taddr[j] = (*i).addrLow[j];
457 if ((Ipv6Address (addr) == Ipv6Address (taddr)))
460 for (uint32_t j = 0; j < 16; j++)
462 (*i).addrLow[j] = addr[j];
469 for (uint32_t j = 0; j < 16; j++)
471 entry.addrLow[j] = entry.addrHigh[j] = addr[j];
473 m_entries.insert (i, entry);
478 Ipv6AddressGeneratorImpl::TestMode (
void)
485 Ipv6AddressGeneratorImpl::PrefixToIndex (Ipv6Prefix prefix)
const
498 uint8_t prefixBits[16];
499 prefix.GetBytes (prefixBits);
501 for (int32_t i = 15; i >= 0; --i)
503 for (uint32_t j = 0; j < 8; ++j)
505 if (prefixBits[i] & 1)
507 uint32_t index = N_BITS - (15 - i) * 8 - j;
508 NS_ABORT_MSG_UNLESS (index > 0 && index < N_BITS,
"Ip64AddressGenerator::PrefixToIndex(): Illegal Prefix");
514 NS_ASSERT_MSG (
false,
"Ipv6AddressGenerator::PrefixToIndex(): Impossible");
527 ->Init (net, prefix, interfaceId);
536 ->NextNetwork (prefix);
545 ->GetNetwork (prefix);
556 ->InitAddress (interfaceId, prefix);
565 ->GetAddress (prefix);
574 ->NextAddress (prefix);
592 ->AddAllocated (addr);
#define NS_LOG_FUNCTION(parameters)
#define NS_LOG_COMPONENT_DEFINE(name)
static Ipv6Address NextNetwork(const Ipv6Prefix prefix)
Get the next network acoording to the given Ipv6Prefix.
#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
static void InitAddress(const Ipv6Address interfaceId, const Ipv6Prefix prefix)
Set the interfaceId for 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.
void GetBytes(uint8_t buf[16]) const
Get the bytes corresponding to the address.
#define NS_LOG_LOGIC(msg)
static void TestMode(void)
Used to turn off fatal errors and assertions, for testing.
#define NS_ASSERT_MSG(condition, message)
Describes an IPv6 address.
static void Reset(void)
Reset the networks and Ipv6Address to zero.
#define NS_LOG_DEBUG(msg)
static Ipv6Address GetAddress(const Ipv6Prefix prefix)
Get the Ipv6Address that will be allocated upon NextAddress()
Describes an IPv6 prefix. It is just a bitmask like Ipv4Mask.
static bool AddAllocated(const Ipv6Address addr)
Add the Ipv6Address to the list of IPv6 entries.
static Ipv6Address NextAddress(const Ipv6Prefix prefix)
Allocate the next Ipv6Address for the configured network and prefix.
static Ipv6Address GetNetwork(const Ipv6Prefix prefix)
Get the current network of the given Ipv6Prefix.