mirror of
https://github.com/espressif/openthread.git
synced 2026-07-08 13:20:25 +00:00
Make client responsible for configuring IPv6 addresses. (#410)
* OpenThread no longer automatically configures IPv6 addresses based on network data. * Added IPv6 address configuration to CLI to support certification tests. * Added notification to indicate when the Thread Network Data is updated. * Added otIp6PrefixMatch API to compute prefix match.
This commit is contained in:
+108
@@ -42,6 +42,7 @@
|
||||
#include "cli_dataset.hpp"
|
||||
#include <common/encoding.hpp>
|
||||
#include <common/new.hpp>
|
||||
#include <platform/random.h>
|
||||
#include <platform/uart.h>
|
||||
|
||||
using Thread::Encoding::BigEndian::HostSwap16;
|
||||
@@ -113,6 +114,8 @@ uint16_t Interpreter::sLength;
|
||||
uint16_t Interpreter::sCount;
|
||||
uint32_t Interpreter::sInterval;
|
||||
|
||||
static otNetifAddress sAutoAddresses[8];
|
||||
|
||||
void Interpreter::Init(void)
|
||||
{
|
||||
sIcmpEcho = new(&sIcmpEchoBuf) Ip6::IcmpEcho(&HandleEchoResponse, NULL);
|
||||
@@ -120,6 +123,7 @@ void Interpreter::Init(void)
|
||||
sLength = 8;
|
||||
sCount = 1;
|
||||
sInterval = 1000;
|
||||
otSetStateChangedCallback(&HandleNetifStateChanged, NULL);
|
||||
}
|
||||
|
||||
int Interpreter::Hex2Bin(const char *aHex, uint8_t *aBin, uint16_t aBinLength)
|
||||
@@ -1780,5 +1784,109 @@ exit:
|
||||
return;
|
||||
}
|
||||
|
||||
void Interpreter::HandleNetifStateChanged(uint32_t aFlags, void *aContext)
|
||||
{
|
||||
otNetworkDataIterator iterator;
|
||||
otBorderRouterConfig config;
|
||||
|
||||
VerifyOrExit((aFlags & OT_THREAD_NETDATA_UPDATED) != 0, ;);
|
||||
|
||||
// remove addresses
|
||||
for (size_t i = 0; i < sizeof(sAutoAddresses) / sizeof(sAutoAddresses[0]); i++)
|
||||
{
|
||||
otNetifAddress *address = &sAutoAddresses[i];
|
||||
bool found = false;
|
||||
|
||||
if (address->mValidLifetime == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
iterator = OT_NETWORK_DATA_ITERATOR_INIT;
|
||||
|
||||
while (otGetNextOnMeshPrefix(false, &iterator, &config) == kThreadError_None)
|
||||
{
|
||||
if (config.mSlaac == false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (otIp6PrefixMatch(&config.mPrefix.mPrefix, &address->mAddress) >= config.mPrefix.mLength &&
|
||||
config.mPrefix.mLength == address->mPrefixLength)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
otRemoveUnicastAddress(address);
|
||||
address->mValidLifetime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// add addresses
|
||||
iterator = OT_NETWORK_DATA_ITERATOR_INIT;
|
||||
|
||||
while (otGetNextOnMeshPrefix(false, &iterator, &config) == kThreadError_None)
|
||||
{
|
||||
bool found = false;
|
||||
|
||||
if (config.mSlaac == false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < sizeof(sAutoAddresses) / sizeof(sAutoAddresses[0]); i++)
|
||||
{
|
||||
otNetifAddress *address = &sAutoAddresses[i];
|
||||
|
||||
if (address->mValidLifetime == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (otIp6PrefixMatch(&config.mPrefix.mPrefix, &address->mAddress) >= config.mPrefix.mLength &&
|
||||
config.mPrefix.mLength == address->mPrefixLength)
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
for (size_t i = 0; i < sizeof(sAutoAddresses) / sizeof(sAutoAddresses[0]); i++)
|
||||
{
|
||||
otNetifAddress *address = &sAutoAddresses[i];
|
||||
|
||||
if (address->mValidLifetime != 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
memset(address, 0, sizeof(*address));
|
||||
memcpy(&address->mAddress, &config.mPrefix.mPrefix, 8);
|
||||
|
||||
for (size_t j = 8; j < sizeof(address->mAddress); j++)
|
||||
{
|
||||
address->mAddress.mFields.m8[j] = (uint8_t)otPlatRandomGet();
|
||||
}
|
||||
|
||||
address->mPrefixLength = config.mPrefix.mLength;
|
||||
address->mPreferredLifetime = config.mPreferred ? 0xffffffff : 0;
|
||||
address->mValidLifetime = 0xffffffff;
|
||||
otAddUnicastAddress(address);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
(void)aContext;
|
||||
return;
|
||||
}
|
||||
|
||||
} // namespace Cli
|
||||
} // namespace Thread
|
||||
|
||||
Reference in New Issue
Block a user