diff --git a/examples/drivers/windows/otLwf/filter.h b/examples/drivers/windows/otLwf/filter.h index 14ab782e5..6883bf598 100644 --- a/examples/drivers/windows/otLwf/filter.h +++ b/examples/drivers/windows/otLwf/filter.h @@ -200,9 +200,6 @@ typedef struct _MS_FILTER ULONG otCachedAddrCount; IN6_ADDR otLinkLocalAddr; otNetifAddress otAutoAddresses[OPENTHREAD_CONFIG_NUM_SLAAC_ADDRESSES]; -#if OPENTHREAD_ENABLE_DHCP6_CLIENT - otDhcpAddress otDhcpAddresses[OPENTHREAD_CONFIG_NUM_DHCP_PREFIXES]; -#endif // OPENTHREAD_ENABLE_DHCP6_CLIENT union { diff --git a/examples/drivers/windows/otLwf/precomp.h b/examples/drivers/windows/otLwf/precomp.h index 74485299a..d7b879803 100644 --- a/examples/drivers/windows/otLwf/precomp.h +++ b/examples/drivers/windows/otLwf/precomp.h @@ -70,7 +70,6 @@ RtlCopyBufferToMdl( #include #include #include -#include #include #include #include diff --git a/examples/drivers/windows/otLwf/thread.c b/examples/drivers/windows/otLwf/thread.c index 2139f456f..0415157f5 100644 --- a/examples/drivers/windows/otLwf/thread.c +++ b/examples/drivers/windows/otLwf/thread.c @@ -547,10 +547,6 @@ void otLwfStateChangedCallback(uint32_t aFlags, _In_ void *aContext) #if OPENTHREAD_ENABLE_DHCP6_SERVER otDhcp6ServerUpdate(pFilter->otCtx); #endif // OPENTHREAD_ENABLE_DHCP6_SERVER - -#if OPENTHREAD_ENABLE_DHCP6_CLIENT - otDhcp6ClientUpdate(pFilter->otCtx, pFilter->otDhcpAddresses, ARRAYSIZE(pFilter->otDhcpAddresses), NULL); -#endif // OPENTHREAD_ENABLE_DHCP6_CLIENT } if ((aFlags & OT_CHANGED_THREAD_ML_ADDR) != 0) diff --git a/include/openthread/Makefile.am b/include/openthread/Makefile.am index 6df7f0b07..05ff28d0f 100644 --- a/include/openthread/Makefile.am +++ b/include/openthread/Makefile.am @@ -61,7 +61,6 @@ openthread_headers = \ dataset.h \ dataset_ftd.h \ diag.h \ - dhcp6_client.h \ dhcp6_server.h \ dns.h \ error.h \ diff --git a/include/openthread/dhcp6_client.h b/include/openthread/dhcp6_client.h deleted file mode 100644 index 751dc2642..000000000 --- a/include/openthread/dhcp6_client.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2016, The OpenThread Authors. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -/** - * @file - * @brief - * This file includes the platform abstraction for the Thread DHCPv6 client. - */ - -#ifndef OPENTHREAD_DHCP6_CLIENT_H_ -#define OPENTHREAD_DHCP6_CLIENT_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @addtogroup api-dhcp6 - * - * @{ - * - */ - -/** - * This structure represents a DHCPv6 address. - * - */ -typedef struct otDhcpAddress -{ - otNetifAddress mAddress; ///< The network interface address. - uint32_t mPreferredLifetime; ///< The preferred lifetime. - uint32_t mValidLifetime; ///< The valid lifetime. -} otDhcpAddress; - -/** - * Update all automatically created IPv6 addresses for prefixes from current Network Data with DHCP procedure. - * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[inout] aAddresses A pointer to an array of automatically created IPv6 addresses. - * @param[in] aNumAddresses The number of slots in aAddresses array. - * @param[in] aContext A pointer to data passed to aIidCreate function. - * - */ -void otDhcp6ClientUpdate(otInstance *aInstance, otDhcpAddress *aAddresses, uint32_t aNumAddresses, void *aContext); - -/** - * @} - * - */ - -#ifdef __cplusplus -} // end of extern "C" -#endif - -#endif // OPENTHREAD_DHCP6_CLIENT_H_ diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index c31611817..acb2482ea 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -64,7 +64,6 @@ #endif #ifndef OTDLL -#include #include #include #include @@ -324,10 +323,6 @@ Interpreter::Interpreter(Instance *aInstance) mIcmpHandler.mContext = this; otIcmp6RegisterHandler(mInstance, &mIcmpHandler); -#if OPENTHREAD_ENABLE_DHCP6_CLIENT - memset(mDhcpAddresses, 0, sizeof(mDhcpAddresses)); -#endif // OPENTHREAD_ENABLE_DHCP6_CLIENT - #if OPENTHREAD_ENABLE_DNS_CLIENT memset(mResolvingHostname, 0, sizeof(mResolvingHostname)); #endif // OPENTHREAD_ENABLE_DNS_CLIENT @@ -3829,9 +3824,6 @@ void Interpreter::HandleNetifStateChanged(otChangedFlags aFlags) otDhcp6ServerUpdate(mInstance); #endif // OPENTHREAD_ENABLE_DHCP6_SERVER -#if OPENTHREAD_ENABLE_DHCP6_CLIENT - otDhcp6ClientUpdate(mInstance, mDhcpAddresses, OT_ARRAY_LENGTH(mDhcpAddresses), NULL); -#endif // OPENTHREAD_ENABLE_DHCP6_CLIENT #endif exit: diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index c020f8323..7122f9d52 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -59,7 +59,6 @@ #include "common/instance.hpp" #ifndef OTDLL -#include #include #include #include "common/timer.hpp" @@ -433,9 +432,6 @@ private: TimerMilli mPingTimer; otNetifAddress mSlaacAddresses[OPENTHREAD_CONFIG_NUM_SLAAC_ADDRESSES]; -#if OPENTHREAD_ENABLE_DHCP6_CLIENT - otDhcpAddress mDhcpAddresses[OPENTHREAD_CONFIG_NUM_DHCP_PREFIXES]; -#endif // OPENTHREAD_ENABLE_DHCP6_CLIENT otIcmp6Handler mIcmpHandler; #if OPENTHREAD_ENABLE_DNS_CLIENT diff --git a/src/core/api/dhcp6_api.cpp b/src/core/api/dhcp6_api.cpp index 9bb664e76..a7c01672d 100644 --- a/src/core/api/dhcp6_api.cpp +++ b/src/core/api/dhcp6_api.cpp @@ -33,22 +33,12 @@ #include "openthread-core-config.h" -#include #include #include "common/instance.hpp" using namespace ot; -#if OPENTHREAD_ENABLE_DHCP6_CLIENT -void otDhcp6ClientUpdate(otInstance *aInstance, otDhcpAddress *aAddresses, uint32_t aNumAddresses, void *aContext) -{ - Instance &instance = *static_cast(aInstance); - - instance.GetThreadNetif().GetDhcp6Client().UpdateAddresses(aInstance, aAddresses, aNumAddresses, aContext); -} -#endif - #if OPENTHREAD_ENABLE_DHCP6_SERVER void otDhcp6ServerUpdate(otInstance *aInstance) { diff --git a/src/core/net/dhcp6.hpp b/src/core/net/dhcp6.hpp index 649354c25..7f7c505ea 100644 --- a/src/core/net/dhcp6.hpp +++ b/src/core/net/dhcp6.hpp @@ -472,7 +472,7 @@ public: * @returns A pointer to the IPv6 address. * */ - otIp6Address *GetAddress(void) { return &mAddress; } + Ip6::Address &GetAddress(void) { return mAddress; } /** * This method sets the IPv6 address. @@ -515,7 +515,7 @@ public: void SetValidLifetime(uint32_t aValidLifetime) { mValidLifetime = HostSwap32(aValidLifetime); } private: - otIp6Address mAddress; ///< IPv6 address + Ip6::Address mAddress; ///< IPv6 address uint32_t mPreferredLifetime; ///< Preferred Lifetime uint32_t mValidLifetime; ///< Valid Lifetime } OT_TOOL_PACKED_END; diff --git a/src/core/net/dhcp6_client.cpp b/src/core/net/dhcp6_client.cpp index fd9e4d70b..b43220dfe 100644 --- a/src/core/net/dhcp6_client.cpp +++ b/src/core/net/dhcp6_client.cpp @@ -59,42 +59,30 @@ Dhcp6Client::Dhcp6Client(Instance &aInstance) , mSocket(aInstance.GetThreadNetif().GetIp6().GetUdp()) , mTrickleTimer(aInstance, &Dhcp6Client::HandleTrickleTimer, NULL, this) , mStartTime(0) - , mAddresses(NULL) - , mNumAddresses(0) + , mIdentityAssociationCurrent(NULL) { - memset(mIdentityAssociations, 0, sizeof(IdentityAssociation)); - - for (uint8_t i = 0; i < (OPENTHREAD_CONFIG_NUM_DHCP_PREFIXES - 1); i++) - { - mIdentityAssociations[i].SetNext(&(mIdentityAssociations[i + 1])); - } - - mIdentityAssociationHead = NULL; - mIdentityAssociationAvail = &mIdentityAssociations[0]; + memset(mIdentityAssociations, 0, sizeof(mIdentityAssociations)); } -void Dhcp6Client::UpdateAddresses(otInstance * aInstance, - otDhcpAddress *aAddresses, - uint32_t aNumAddresses, - void * aContext) +bool Dhcp6Client::MatchNetifAddressWithPrefix(const otNetifAddress &aNetifAddress, const otIp6Prefix &aIp6Prefix) { - OT_UNUSED_VARIABLE(aContext); + return aIp6Prefix.mLength == aNetifAddress.mPrefixLength && + otIp6PrefixMatch(&aNetifAddress.mAddress, &aIp6Prefix.mPrefix) >= aIp6Prefix.mLength; +} +void Dhcp6Client::UpdateAddresses(void) +{ bool found = false; bool newAgent = false; - otDhcpAddress * address = NULL; otNetworkDataIterator iterator; otBorderRouterConfig config; - mAddresses = aAddresses; - mNumAddresses = aNumAddresses; - // remove addresses directly if prefix not valid in network data - for (uint8_t i = 0; i < mNumAddresses; i++) + for (uint8_t i = 0; i < OT_ARRAY_LENGTH(mIdentityAssociations); i++) { - address = &mAddresses[i]; + IdentityAssociation &ia = mIdentityAssociations[i]; - if (address->mValidLifetime == 0) + if (ia.mStatus == kIaStatusInvalid || ia.mValidLifetime == 0) { continue; } @@ -102,16 +90,14 @@ void Dhcp6Client::UpdateAddresses(otInstance * aInstance, found = false; iterator = OT_NETWORK_DATA_ITERATOR_INIT; - while ((otNetDataGetNextOnMeshPrefix(aInstance, &iterator, &config)) == OT_ERROR_NONE) + while ((otNetDataGetNextOnMeshPrefix(&GetInstance(), &iterator, &config)) == OT_ERROR_NONE) { if (!config.mDhcp) { continue; } - if ((otIp6PrefixMatch(&(address->mAddress.mAddress), &(config.mPrefix.mPrefix)) >= - address->mAddress.mPrefixLength) && - (config.mPrefix.mLength == address->mAddress.mPrefixLength)) + if (MatchNetifAddressWithPrefix(ia.mNetifAddress, config.mPrefix)) { found = true; break; @@ -120,17 +106,18 @@ void Dhcp6Client::UpdateAddresses(otInstance * aInstance, if (!found) { - GetNetif().RemoveUnicastAddress(*static_cast(&address->mAddress)); - RemoveIdentityAssociation(config.mRloc16, config.mPrefix); - memset(address, 0, sizeof(*address)); + GetNetif().RemoveUnicastAddress(*static_cast(&ia.mNetifAddress)); + mIdentityAssociations[i].mStatus = kIaStatusInvalid; } } // add IdentityAssociation for new configured prefix iterator = OT_NETWORK_DATA_ITERATOR_INIT; - while (otNetDataGetNextOnMeshPrefix(aInstance, &iterator, &config) == OT_ERROR_NONE) + while (otNetDataGetNextOnMeshPrefix(&GetInstance(), &iterator, &config) == OT_ERROR_NONE) { + IdentityAssociation *ia = NULL; + if (!config.mDhcp) { continue; @@ -138,18 +125,17 @@ void Dhcp6Client::UpdateAddresses(otInstance * aInstance, found = false; - for (uint8_t i = 0; i < mNumAddresses; i++) + for (uint8_t i = 0; i < OT_ARRAY_LENGTH(mIdentityAssociations); i++) { - address = &mAddresses[i]; - - if (address->mAddress.mPrefixLength == 0) + if (mIdentityAssociations[i].mStatus == kIaStatusInvalid) { - continue; + // record an available ia + if (ia == NULL) + { + ia = &mIdentityAssociations[i]; + } } - - if ((otIp6PrefixMatch(&(config.mPrefix.mPrefix), &(address->mAddress.mAddress)) >= - config.mPrefix.mLength) && - (config.mPrefix.mLength == address->mAddress.mPrefixLength)) + else if (MatchNetifAddressWithPrefix(mIdentityAssociations[i].mNetifAddress, config.mPrefix)) { found = true; break; @@ -158,24 +144,18 @@ void Dhcp6Client::UpdateAddresses(otInstance * aInstance, if (!found) { - for (size_t i = 0; i < mNumAddresses; i++) + if (ia != NULL) { - address = &mAddresses[i]; - - if (address->mAddress.mPrefixLength != 0) - { - continue; - } - - memset(address, 0, sizeof(*address)); - - // suppose all configured prefix are ::/64 - memcpy(address->mAddress.mAddress.mFields.m8, config.mPrefix.mPrefix.mFields.m8, 8); - address->mAddress.mPrefixLength = config.mPrefix.mLength; - - AddIdentityAssociation(config.mRloc16, config.mPrefix); - newAgent = true; - break; + ia->mPrefixAgentRloc = config.mRloc16; + ia->mNetifAddress.mAddress = config.mPrefix.mPrefix; + ia->mNetifAddress.mPrefixLength = config.mPrefix.mLength; + ia->mStatus = kIaStatusSolicit; + ia->mValidLifetime = 0; + newAgent = true; + } + else + { + otLogWarnIp6("Insufficient memory for new DHCP prefix"); } } } @@ -190,82 +170,6 @@ void Dhcp6Client::UpdateAddresses(otInstance * aInstance, } } -void Dhcp6Client::AddIdentityAssociation(uint16_t aRloc16, otIp6Prefix &aIp6Prefix) -{ - IdentityAssociation *identityAssociation = NULL; - IdentityAssociation *identityAssociationCursor = NULL; - - VerifyOrExit(mIdentityAssociationAvail); - - identityAssociation = mIdentityAssociationAvail; - mIdentityAssociationAvail = mIdentityAssociationAvail->GetNext(); - - identityAssociation->SetPrefixAgentRloc(aRloc16); - identityAssociation->SetPrefix(aIp6Prefix); - identityAssociation->SetStatus(IdentityAssociation::kStatusSolicit); - - identityAssociation->SetNext(NULL); - - if (mIdentityAssociationHead) - { - // append the new identityassociation to the tail of used list - for (identityAssociationCursor = mIdentityAssociationHead; identityAssociationCursor->GetNext(); - identityAssociationCursor = identityAssociationCursor->GetNext()) - { - } - - identityAssociationCursor->SetNext(identityAssociation); - } - else - { - mIdentityAssociationHead = identityAssociation; - } - -exit: - return; -} - -void Dhcp6Client::RemoveIdentityAssociation(uint16_t aRloc16, otIp6Prefix &aIp6Prefix) -{ - IdentityAssociation *prevIdentityAssociation = NULL; - IdentityAssociation *identityAssociation = NULL; - - VerifyOrExit(mIdentityAssociationHead); - - for (identityAssociation = mIdentityAssociationHead; identityAssociation; - prevIdentityAssociation = identityAssociation, identityAssociation = identityAssociation->GetNext()) - { - if (identityAssociation->GetPrefixAgentRloc() != aRloc16) - { - continue; - } - - if (otIp6PrefixMatch(&(aIp6Prefix.mPrefix), &(identityAssociation->GetPrefix()->mPrefix)) < aIp6Prefix.mLength) - { - continue; - } - - // remove from used list - if (prevIdentityAssociation) - { - prevIdentityAssociation->SetNext(identityAssociation->GetNext()); - } - else - { - mIdentityAssociationHead = identityAssociation->GetNext(); - } - - // return to available list - memset(identityAssociation, 0, sizeof(*identityAssociation)); - identityAssociation->SetNext(mIdentityAssociationAvail); - mIdentityAssociationAvail = identityAssociation; - break; - } - -exit: - return; -} - otError Dhcp6Client::Start(void) { Ip6::SockAddr sockaddr; @@ -287,20 +191,16 @@ otError Dhcp6Client::Stop(void) bool Dhcp6Client::ProcessNextIdentityAssociation() { - bool rval = false; - IdentityAssociation *prevIdentityAssociation = NULL; - IdentityAssociation *identityAssociation = NULL; + bool rval = false; - VerifyOrExit(mIdentityAssociationHead); // not interrupt in-progress solicit - VerifyOrExit((mIdentityAssociationHead->GetStatus() != IdentityAssociation::kStatusSoliciting)); + VerifyOrExit(mIdentityAssociationCurrent == NULL || mIdentityAssociationCurrent->mStatus != kIaStatusSoliciting); mTrickleTimer.Stop(); - for (identityAssociation = mIdentityAssociationHead; identityAssociation; - prevIdentityAssociation = identityAssociation, identityAssociation = identityAssociation->GetNext()) + for (uint8_t i = 0; i < OT_ARRAY_LENGTH(mIdentityAssociations); ++i) { - if (identityAssociation->GetStatus() != IdentityAssociation::kStatusSolicit) + if (mIdentityAssociations[i].mStatus != kIaStatusSolicit) { continue; } @@ -308,13 +208,7 @@ bool Dhcp6Client::ProcessNextIdentityAssociation() // new transaction id Random::FillBuffer(mTransactionId, kTransactionIdSize); - // ensure mIdentityAssociationHead is the prefix agent to solicit. - if (prevIdentityAssociation) - { - prevIdentityAssociation->SetNext(identityAssociation->GetNext()); - identityAssociation->SetNext(mIdentityAssociationHead); - mIdentityAssociationHead = identityAssociation; - } + mIdentityAssociationCurrent = &mIdentityAssociations[i]; mTrickleTimer.Start(TimerMilli::SecToMsec(kTrickleTimerImin), TimerMilli::SecToMsec(kTrickleTimerImax), TrickleTimer::kModeNormal); @@ -337,21 +231,23 @@ bool Dhcp6Client::HandleTrickleTimer(void) { bool rval = true; - VerifyOrExit(mIdentityAssociationHead, rval = false); + VerifyOrExit(mIdentityAssociationCurrent != NULL, rval = false); - switch (mIdentityAssociationHead->GetStatus()) + switch (mIdentityAssociationCurrent->mStatus) { - case IdentityAssociation::kStatusSolicit: - mStartTime = TimerMilli::GetNow(); - mIdentityAssociationHead->SetStatus(IdentityAssociation::kStatusSoliciting); + case kIaStatusSolicit: + mStartTime = TimerMilli::GetNow(); + mIdentityAssociationCurrent->mStatus = kIaStatusSoliciting; // fall through - case IdentityAssociation::kStatusSoliciting: - Solicit(mIdentityAssociationHead->GetPrefixAgentRloc()); + case kIaStatusSoliciting: + Solicit(mIdentityAssociationCurrent->mPrefixAgentRloc); break; - case IdentityAssociation::kStatusSolicitReplied: + case kIaStatusSolicitReplied: + mIdentityAssociationCurrent = NULL; + if (!ProcessNextIdentityAssociation()) { mTrickleTimer.Stop(); @@ -440,23 +336,21 @@ otError Dhcp6Client::AppendClientIdentifier(Message &aMessage) otError Dhcp6Client::AppendIaNa(Message &aMessage, uint16_t aRloc16) { - otError error = OT_ERROR_NONE; - uint8_t count = 0; - uint16_t length = 0; - IdentityAssociation *identityAssociation = NULL; - IaNa option; + otError error = OT_ERROR_NONE; + uint8_t count = 0; + uint16_t length = 0; + IaNa option; - VerifyOrExit(mIdentityAssociationHead, error = OT_ERROR_DROP); + VerifyOrExit(mIdentityAssociationCurrent != NULL, error = OT_ERROR_DROP); - for (identityAssociation = mIdentityAssociationHead; identityAssociation; - identityAssociation = identityAssociation->GetNext()) + for (uint8_t i = 0; i < OT_ARRAY_LENGTH(mIdentityAssociations); ++i) { - if (identityAssociation->GetStatus() == IdentityAssociation::kStatusSolicitReplied) + if (mIdentityAssociations[i].mStatus == kIaStatusSolicitReplied) { continue; } - if (identityAssociation->GetPrefixAgentRloc() == aRloc16) + if (mIdentityAssociations[i].mPrefixAgentRloc == aRloc16) { count++; } @@ -478,21 +372,19 @@ exit: otError Dhcp6Client::AppendIaAddress(Message &aMessage, uint16_t aRloc16) { - otError error = OT_ERROR_NONE; - IdentityAssociation *identityAssociation = NULL; - IaAddress option; + otError error = OT_ERROR_NONE; + IaAddress option; - VerifyOrExit(mIdentityAssociationHead, error = OT_ERROR_DROP); + VerifyOrExit(mIdentityAssociationCurrent, error = OT_ERROR_DROP); option.Init(); - for (identityAssociation = mIdentityAssociationHead; identityAssociation; - identityAssociation = identityAssociation->GetNext()) + for (uint8_t i = 0; i < OT_ARRAY_LENGTH(mIdentityAssociations); ++i) { - if ((identityAssociation->GetStatus() != IdentityAssociation::kStatusSolicitReplied) && - (identityAssociation->GetPrefixAgentRloc() == aRloc16)) + if ((mIdentityAssociations[i].mStatus != kIaStatusSolicitReplied) && + (mIdentityAssociations[i].mPrefixAgentRloc == aRloc16)) { - option.SetAddress(identityAssociation->GetPrefix()->mPrefix); + option.SetAddress(mIdentityAssociations[i].mNetifAddress.mAddress); option.SetPreferredLifetime(0); option.SetValidLifetime(0); SuccessOrExit(error = aMessage.Append(&option, sizeof(option))); @@ -664,47 +556,31 @@ exit: otError Dhcp6Client::ProcessIaAddress(Message &aMessage, uint16_t aOffset) { - otError error = OT_ERROR_NONE; - IdentityAssociation *identityAssociation = NULL; - otDhcpAddress * address = NULL; - otIp6Prefix * prefix = NULL; - + otError error = OT_ERROR_NONE; IaAddress option; VerifyOrExit(((aMessage.Read(aOffset, sizeof(option), &option) == sizeof(option)) && (option.GetLength() == (sizeof(option) - sizeof(Dhcp6Option)))), error = OT_ERROR_PARSE); - for (uint8_t i = 0; i < mNumAddresses; i++) + for (uint8_t i = 0; i < OT_ARRAY_LENGTH(mIdentityAssociations); ++i) { - address = &mAddresses[i]; + IdentityAssociation &ia = mIdentityAssociations[i]; - if (address->mValidLifetime != 0) + if (ia.mStatus == kIaStatusInvalid || ia.mValidLifetime != 0) { continue; } - if (otIp6PrefixMatch(&(address->mAddress.mAddress), option.GetAddress()) >= address->mAddress.mPrefixLength) + if (otIp6PrefixMatch(&ia.mNetifAddress.mAddress, &option.GetAddress()) >= ia.mNetifAddress.mPrefixLength) { - memcpy(address->mAddress.mAddress.mFields.m8, option.GetAddress()->mFields.m8, sizeof(otIp6Address)); - address->mPreferredLifetime = option.GetPreferredLifetime(); - address->mValidLifetime = option.GetValidLifetime(); - address->mAddress.mPreferred = address->mPreferredLifetime != 0; - address->mAddress.mValid = address->mValidLifetime != 0; - GetNetif().AddUnicastAddress(*static_cast(&address->mAddress)); - break; - } - } - - // mark IdentityAssociation as replied - for (identityAssociation = mIdentityAssociationHead; identityAssociation; - identityAssociation = identityAssociation->GetNext()) - { - prefix = identityAssociation->GetPrefix(); - - if (otIp6PrefixMatch(option.GetAddress(), &(prefix->mPrefix)) >= prefix->mLength) - { - identityAssociation->SetStatus(IdentityAssociation::kStatusSolicitReplied); + mIdentityAssociations[i].mNetifAddress.mAddress = option.GetAddress(); + mIdentityAssociations[i].mPreferredLifetime = option.GetPreferredLifetime(); + mIdentityAssociations[i].mValidLifetime = option.GetValidLifetime(); + mIdentityAssociations[i].mNetifAddress.mPreferred = option.GetPreferredLifetime() != 0; + mIdentityAssociations[i].mNetifAddress.mValid = option.GetValidLifetime() != 0; + mIdentityAssociations[i].mStatus = kIaStatusSolicitReplied; + GetNetif().AddUnicastAddress(*static_cast(&ia.mNetifAddress)); break; } } diff --git a/src/core/net/dhcp6_client.hpp b/src/core/net/dhcp6_client.hpp index 5aaa4fe0b..f0e4fb2fd 100644 --- a/src/core/net/dhcp6_client.hpp +++ b/src/core/net/dhcp6_client.hpp @@ -36,8 +36,6 @@ #include "openthread-core-config.h" -#include - #include "common/locator.hpp" #include "common/message.hpp" #include "common/timer.hpp" @@ -71,94 +69,30 @@ enum kTrickleTimerImax = 120, }; +/** + * Status of IdentityAssociation + * + */ +enum IaStatus +{ + kIaStatusInvalid, + kIaStatusSolicit, + kIaStatusSoliciting, + kIaStatusSolicitReplied, +}; + /** * This class implements IdentityAssociation. * */ -OT_TOOL_PACKED_BEGIN -class IdentityAssociation +struct IdentityAssociation { -public: - /** - * Status of IdentityAssociation - * - */ - typedef enum Status - { - kStatusInvalid, - kStatusSolicit, - kStatusSoliciting, - kStatusSolicitReplied, - } Status; - - /** - * This method returns the status of the object. - * - * @returns Status. - * - */ - Status GetStatus(void) const { return static_cast(mStatus); } - - /** - * This method sets the status of the object. - * - * @param[in] aStatus The Status to set. - * - */ - void SetStatus(Status aStatus) { mStatus = static_cast(aStatus); } - - /** - * This method returns the rloc of the DHCP Agent. - * - * @returns Status. - * - */ - uint16_t GetPrefixAgentRloc(void) const { return mPrefixAgentRloc; } - - /** - * This method sets the rloc of the DHCP Agent. - * - * @param[in] aRloc The rloc of the DHCP Agent. - * - */ - void SetPrefixAgentRloc(uint16_t aRloc16) { mPrefixAgentRloc = aRloc16; } - - /** - * This method returns the pointer to the IPv6 prefix. - * - * @returns A pointer to the IPv6 prefix. - * - */ - otIp6Prefix *GetPrefix(void) { return &mIp6Prefix; } - - /** - * This method sets the IPv6 prefix to specified location. - * - * @param[in] aIp6Prefix The reference to the IPv6 prefix to set. - * - */ - void SetPrefix(otIp6Prefix &aIp6Prefix) { memcpy(&mIp6Prefix, &aIp6Prefix, sizeof(otIp6Prefix)); } - - /** - * This method returns the pointer to the next IdentityAssociation. - * - * @returns A pointer to the next IdentityAssociation. - * - */ - IdentityAssociation *GetNext(void) { return mNext; } - - /** - * This method sets the pointer to the next IdentityAssociation. - * - */ - void SetNext(IdentityAssociation *aNext) { mNext = aNext; } - -private: - uint8_t mStatus; ///< Status of IdentityAssocation - uint16_t mPrefixAgentRloc; ///< Rloc of Prefix Agent - otIp6Prefix mIp6Prefix; ///< Prefix - IdentityAssociation *mNext; ///< Pointer to next IdentityAssocation -} OT_TOOL_PACKED_END; + otNetifAddress mNetifAddress; ///< the NetifAddress + uint32_t mPreferredLifetime; ///< The preferred lifetime. + uint32_t mValidLifetime; ///< The valid lifetime. + uint16_t mPrefixAgentRloc; ///< Rloc of Prefix Agent + uint8_t mStatus; ///< Status of IdentityAssocation +}; /** * This class implements DHCPv6 Client. @@ -178,18 +112,16 @@ public: /** * This method update addresses that shall be automatically created using DHCP. * - * @param[in] aInstance A pointer to OpenThread instance. - * @param[inout] aAddresses A pointer to an array containing addresses created by this module. - * @param[in] aNumAddresses The number of elements in aAddresses array. - * @param[in] aContext A pointer to IID creator-specific context data. * */ - void UpdateAddresses(otInstance *aInstance, otDhcpAddress *aAddresses, uint32_t aNumAddresses, void *aContext); + void UpdateAddresses(void); private: otError Start(void); otError Stop(void); + static bool MatchNetifAddressWithPrefix(const otNetifAddress &aNetifAddress, const otIp6Prefix &aIp6Prefix); + otError Solicit(uint16_t aRloc16); void AddIdentityAssociation(uint16_t aRloc16, otIp6Prefix &aIp6Prefix); @@ -222,14 +154,11 @@ private: TrickleTimer mTrickleTimer; - uint8_t mTransactionId[kTransactionIdSize]; - uint32_t mStartTime; - otDhcpAddress *mAddresses; - uint32_t mNumAddresses; + uint8_t mTransactionId[kTransactionIdSize]; + uint32_t mStartTime; IdentityAssociation mIdentityAssociations[OPENTHREAD_CONFIG_NUM_DHCP_PREFIXES]; - IdentityAssociation *mIdentityAssociationHead; - IdentityAssociation *mIdentityAssociationAvail; + IdentityAssociation *mIdentityAssociationCurrent; }; } // namespace Dhcp6 diff --git a/src/core/net/dhcp6_server.cpp b/src/core/net/dhcp6_server.cpp index d6e6b2134..9a8489e3a 100644 --- a/src/core/net/dhcp6_server.cpp +++ b/src/core/net/dhcp6_server.cpp @@ -401,7 +401,7 @@ otError Dhcp6Server::ProcessIaAddress(Message &aMessage, uint16_t aOffset) continue; } - if (otIp6PrefixMatch(option.GetAddress(), &(prefix->mPrefix)) >= prefix->mLength) + if (otIp6PrefixMatch(&option.GetAddress(), &prefix->mPrefix) >= prefix->mLength) { mPrefixAgentsMask |= (1 << i); break; @@ -553,9 +553,8 @@ otError Dhcp6Server::AddIaAddress(Message &aMessage, otIp6Prefix &aIp6Prefix, Cl IaAddress option; option.Init(); - memcpy((option.GetAddress()->mFields.m8), &(aIp6Prefix.mPrefix), 8); - static_cast(option.GetAddress()) - ->SetIid(*reinterpret_cast(aClient.GetDuidLinkLayerAddress())); + memcpy(option.GetAddress().mFields.m8, &aIp6Prefix.mPrefix, 8); + option.GetAddress().SetIid(*reinterpret_cast(aClient.GetDuidLinkLayerAddress())); option.SetPreferredLifetime(OT_DHCP6_DEFAULT_PREFERRED_LIFETIME); option.SetValidLifetime(OT_DHCP6_DEFAULT_VALID_LIFETIME); SuccessOrExit(error = aMessage.Append(&option, sizeof(option))); diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 2f99cb308..729c7b4ad 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -1509,6 +1509,10 @@ void Mle::HandleStateChanged(otChangedFlags aFlags) this->UpdateServiceAlocs(); #endif #endif + +#if OPENTHREAD_ENABLE_DHCP6_CLIENT + GetNetif().GetDhcp6Client().UpdateAddresses(); +#endif // OPENTHREAD_ENABLE_DHCP6_CLIENT } if (aFlags & (OT_CHANGED_THREAD_ROLE | OT_CHANGED_THREAD_KEY_SEQUENCE_COUNTER)) diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index 532f09359..b0b4917c4 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -257,10 +257,6 @@ NcpBase::NcpBase(Instance *aInstance) memset(&mResponseQueue, 0, sizeof(mResponseQueue)); -#if OPENTHREAD_ENABLE_DHCP6_CLIENT - memset(mDhcpAddresses, 0, sizeof(mDhcpAddresses)); -#endif - #if OPENTHREAD_MTD || OPENTHREAD_FTD otMessageQueueInit(&mMessageQueue); otSetStateChangedCallback(mInstance, &NcpBase::HandleStateChanged, this); diff --git a/src/ncp/ncp_base.hpp b/src/ncp/ncp_base.hpp index e72ea7d86..aa93c23bf 100644 --- a/src/ncp/ncp_base.hpp +++ b/src/ncp/ncp_base.hpp @@ -43,7 +43,6 @@ #if OPENTHREAD_FTD #include #endif -#include #include #include @@ -535,10 +534,6 @@ protected: bool mDisableStreamWrite; bool mShouldEmitChildTableUpdate; -#if OPENTHREAD_ENABLE_DHCP6_CLIENT - otDhcpAddress mDhcpAddresses[OPENTHREAD_CONFIG_NUM_DHCP_PREFIXES]; -#endif - #if OPENTHREAD_FTD #if OPENTHREAD_CONFIG_ENABLE_STEERING_DATA_SET_OOB otExtAddress mSteeringDataAddress; diff --git a/src/ncp/ncp_base_mtd.cpp b/src/ncp/ncp_base_mtd.cpp index f2f5a45ed..9f5542ff4 100644 --- a/src/ncp/ncp_base_mtd.cpp +++ b/src/ncp/ncp_base_mtd.cpp @@ -3437,10 +3437,6 @@ void NcpBase::ProcessThreadChangedFlags(void) mChangedPropsSet.AddProperty(SPINEL_PROP_THREAD_ON_MESH_NETS); mChangedPropsSet.AddProperty(SPINEL_PROP_THREAD_OFF_MESH_ROUTES); -#if OPENTHREAD_ENABLE_DHCP6_CLIENT - otDhcp6ClientUpdate(mInstance, mDhcpAddresses, OT_ARRAY_LENGTH(mDhcpAddresses), NULL); -#endif - #if OPENTHREAD_ENABLE_DHCP6_SERVER otDhcp6ServerUpdate(mInstance); #endif