From 5e233fb61752b55892511576e08d430bdc5e22d9 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Sat, 29 Aug 2020 09:52:31 +0800 Subject: [PATCH] [posix] allow bind to Thread interface only (#4965) --- Android.mk | 1 + include/openthread/instance.h | 2 +- include/openthread/platform/udp.h | 20 +++++ src/core/BUILD.gn | 2 + src/core/CMakeLists.txt | 1 + src/core/Makefile.am | 2 + src/core/api/message_api.cpp | 4 +- src/core/backbone_router/bbr_manager.cpp | 16 ++-- src/core/coap/coap.hpp | 5 +- src/core/common/instance.hpp | 6 +- src/core/meshcop/announce_begin_client.cpp | 6 +- src/core/meshcop/border_agent.cpp | 20 ++--- src/core/meshcop/commissioner.cpp | 48 +++++------ src/core/meshcop/dataset_manager.cpp | 28 +++---- src/core/meshcop/dataset_manager_ftd.cpp | 12 +-- src/core/meshcop/energy_scan_client.cpp | 10 +-- src/core/meshcop/joiner.cpp | 6 +- src/core/meshcop/joiner_router.cpp | 18 ++-- src/core/meshcop/meshcop_leader.cpp | 18 ++-- src/core/meshcop/panid_query_client.cpp | 10 +-- src/core/net/ip6.cpp | 2 +- src/core/net/udp6.cpp | 13 ++- src/core/net/udp6.hpp | 36 +++++--- src/core/thread/address_resolver.cpp | 28 +++---- src/core/thread/announce_begin_server.cpp | 4 +- src/core/thread/dua_manager.cpp | 23 +++--- src/core/thread/energy_scan_server.cpp | 10 +-- src/core/thread/mesh_forwarder.cpp | 2 +- src/core/thread/mle.cpp | 2 +- src/core/thread/mle_router.cpp | 30 +++---- src/core/thread/mlr_manager.cpp | 6 +- src/core/thread/network_data.cpp | 6 +- src/core/thread/network_data_leader_ftd.cpp | 22 ++--- src/core/thread/network_diagnostic.cpp | 36 ++++---- src/core/thread/panid_query_server.cpp | 10 +-- src/core/thread/thread_netif.cpp | 33 +------- src/core/thread/thread_netif.hpp | 15 +--- src/core/thread/thread_tlvs.hpp | 2 - src/core/thread/tmf.cpp | 82 ++++++++++++++++++ src/core/thread/tmf.hpp | 92 +++++++++++++++++++++ src/posix/platform/netif.cpp | 56 +++++++------ src/posix/platform/platform-posix.h | 13 +++ src/posix/platform/udp.cpp | 83 +++++++++++++++---- tests/scripts/expect/tun-udp.exp | 62 ++++++++++++++ 44 files changed, 608 insertions(+), 295 deletions(-) create mode 100644 src/core/thread/tmf.cpp create mode 100644 src/core/thread/tmf.hpp create mode 100755 tests/scripts/expect/tun-udp.exp diff --git a/Android.mk b/Android.mk index 928cf4dfb..240260b4e 100644 --- a/Android.mk +++ b/Android.mk @@ -272,6 +272,7 @@ LOCAL_SRC_FILES := \ src/core/thread/router_table.cpp \ src/core/thread/src_match_controller.cpp \ src/core/thread/thread_netif.cpp \ + src/core/thread/tmf.cpp \ src/core/thread/topology.cpp \ src/core/utils/channel_manager.cpp \ src/core/utils/channel_monitor.cpp \ diff --git a/include/openthread/instance.h b/include/openthread/instance.h index b6e77899d..36ea89d36 100644 --- a/include/openthread/instance.h +++ b/include/openthread/instance.h @@ -53,7 +53,7 @@ extern "C" { * @note This number versions both OpenThread platform and user APIs. * */ -#define OPENTHREAD_API_VERSION (25) +#define OPENTHREAD_API_VERSION (26) /** * @addtogroup api-instance diff --git a/include/openthread/platform/udp.h b/include/openthread/platform/udp.h index e8d0e7ab7..d4906cc63 100644 --- a/include/openthread/platform/udp.h +++ b/include/openthread/platform/udp.h @@ -39,6 +39,12 @@ extern "C" { #endif +typedef enum otNetifIdentifier +{ + OT_NETIF_UNSPECIFIED = 0, ///< Unspecified network interface. + OT_NETIF_THREAD, ///< The Thread interface. +} otNetifIdentifier; + /** * This function initializes the UDP socket by platform. * @@ -72,6 +78,20 @@ otError otPlatUdpClose(otUdpSocket *aUdpSocket); */ otError otPlatUdpBind(otUdpSocket *aUdpSocket); +/** + * This function binds the UDP socket to a platform network interface. + * + * Note: only available when `OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE` is used. + * + * @param[in] aUdpSocket A pointer to the UDP socket. + * @param[in] aNetifIdentifier The network interface identifier. + * + * @retval OT_ERROR_NONE Successfully bound UDP socket. + * @retval OT_ERROR_FAILED Failed to bind UDP. + * + */ +otError otPlatUdpBindToNetif(otUdpSocket *aUdpSocket, otNetifIdentifier aNetifIdentifier); + /** * This function connects UDP socket by platform. * diff --git a/src/core/BUILD.gn b/src/core/BUILD.gn index f7be24008..dcd0f837e 100644 --- a/src/core/BUILD.gn +++ b/src/core/BUILD.gn @@ -538,6 +538,8 @@ openthread_core_files = [ "thread/thread_uri_paths.hpp", "thread/time_sync_service.cpp", "thread/time_sync_service.hpp", + "thread/tmf.cpp", + "thread/tmf.hpp", "thread/topology.cpp", "thread/topology.hpp", "utils/channel_manager.cpp", diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 156ceefff..d83fc9cc2 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -202,6 +202,7 @@ set(COMMON_SOURCES thread/src_match_controller.cpp thread/thread_netif.cpp thread/time_sync_service.cpp + thread/tmf.cpp thread/topology.cpp utils/channel_manager.cpp utils/channel_monitor.cpp diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 8404a3cc2..0aa23e0f5 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -244,6 +244,7 @@ SOURCES_COMMON = \ thread/src_match_controller.cpp \ thread/thread_netif.cpp \ thread/time_sync_service.cpp \ + thread/tmf.cpp \ thread/topology.cpp \ utils/channel_manager.cpp \ utils/channel_monitor.cpp \ @@ -468,6 +469,7 @@ HEADERS_COMMON = \ thread/thread_tlvs.hpp \ thread/thread_uri_paths.hpp \ thread/time_sync_service.hpp \ + thread/tmf.hpp \ thread/topology.hpp \ utils/channel_manager.hpp \ utils/channel_monitor.hpp \ diff --git a/src/core/api/message_api.cpp b/src/core/api/message_api.cpp index 2adf27f91..2dd903d2e 100644 --- a/src/core/api/message_api.cpp +++ b/src/core/api/message_api.cpp @@ -201,8 +201,8 @@ void otMessageGetBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo) instance.Get().GetMessageQueue().GetInfo(aBufferInfo->mMleMessages, aBufferInfo->mMleBuffers); - instance.Get().GetRequestMessages().GetInfo(aBufferInfo->mCoapMessages, aBufferInfo->mCoapBuffers); - instance.Get().GetCachedResponses().GetInfo(messages, buffers); + instance.Get().GetRequestMessages().GetInfo(aBufferInfo->mCoapMessages, aBufferInfo->mCoapBuffers); + instance.Get().GetCachedResponses().GetInfo(messages, buffers); aBufferInfo->mCoapMessages += messages; aBufferInfo->mCoapBuffers += buffers; diff --git a/src/core/backbone_router/bbr_manager.cpp b/src/core/backbone_router/bbr_manager.cpp index 686094002..9ae17bfda 100644 --- a/src/core/backbone_router/bbr_manager.cpp +++ b/src/core/backbone_router/bbr_manager.cpp @@ -70,15 +70,15 @@ void Manager::HandleNotifierEvents(Events aEvents) { if (Get().GetState() == OT_BACKBONE_ROUTER_STATE_DISABLED) { - Get().RemoveResource(mMulticastListenerRegistration); - Get().RemoveResource(mDuaRegistration); + Get().RemoveResource(mMulticastListenerRegistration); + Get().RemoveResource(mDuaRegistration); mTimer.Stop(); mMulticastListenersTable.Clear(); } else { - Get().AddResource(mMulticastListenerRegistration); - Get().AddResource(mDuaRegistration); + Get().AddResource(mMulticastListenerRegistration); + Get().AddResource(mDuaRegistration); if (!mTimer.IsRunning()) { mTimer.Start(kTimerInterval); @@ -182,7 +182,7 @@ void Manager::SendMulticastListenerRegistrationResponse(const Coap::Message & otError error = OT_ERROR_NONE; Coap::Message *message = nullptr; - VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(message->SetDefaultResponseHeader(aMessage)); SuccessOrExit(message->SetPayloadMarker()); @@ -203,7 +203,7 @@ void Manager::SendMulticastListenerRegistrationResponse(const Coap::Message & } } - SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); exit: if (error != OT_ERROR_NONE && message != nullptr) @@ -266,7 +266,7 @@ void Manager::SendDuaRegistrationResponse(const Coap::Message & aMessage, otError error = OT_ERROR_NONE; Coap::Message *message = nullptr; - VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(message->SetDefaultResponseHeader(aMessage)); SuccessOrExit(message->SetPayloadMarker()); @@ -274,7 +274,7 @@ void Manager::SendDuaRegistrationResponse(const Coap::Message & aMessage, SuccessOrExit(Tlv::AppendUint8Tlv(*message, ThreadTlv::kStatus, aStatus)); SuccessOrExit(Tlv::AppendTlv(*message, ThreadTlv::kTarget, &aTarget, sizeof(aTarget))); - SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); exit: if (error != OT_ERROR_NONE && message != nullptr) diff --git a/src/core/coap/coap.hpp b/src/core/coap/coap.hpp index 06788e3e8..5e9387879 100644 --- a/src/core/coap/coap.hpp +++ b/src/core/coap/coap.hpp @@ -623,12 +623,13 @@ public: */ otError Stop(void); +protected: + Ip6::Udp::Socket mSocket; + private: static otError Send(CoapBase &aCoapBase, ot::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); otError Send(ot::Message &aMessage, const Ip6::MessageInfo &aMessageInfo); - - Ip6::Udp::Socket mSocket; }; } // namespace Coap diff --git a/src/core/common/instance.hpp b/src/core/common/instance.hpp index b8e6ef3e5..828f3cc32 100644 --- a/src/core/common/instance.hpp +++ b/src/core/common/instance.hpp @@ -59,9 +59,9 @@ #include "mac/link_raw.hpp" #endif #if OPENTHREAD_FTD || OPENTHREAD_MTD -#include "coap/coap.hpp" #include "common/code_utils.hpp" #include "crypto/mbedtls.hpp" +#include "thread/tmf.hpp" #if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE #include "utils/heap.hpp" #endif @@ -608,9 +608,9 @@ template <> inline Ip6::Mpl &Instance::Get(void) return mIp6.mMpl; } -template <> inline Coap::Coap &Instance::Get(void) +template <> inline Tmf::TmfAgent &Instance::Get(void) { - return mThreadNetif.mCoap; + return mThreadNetif.mTmfAgent; } #if OPENTHREAD_CONFIG_DTLS_ENABLE diff --git a/src/core/meshcop/announce_begin_client.cpp b/src/core/meshcop/announce_begin_client.cpp index cff05ee20..6ce0a601e 100644 --- a/src/core/meshcop/announce_begin_client.cpp +++ b/src/core/meshcop/announce_begin_client.cpp @@ -64,7 +64,7 @@ otError AnnounceBeginClient::SendRequest(uint32_t aChannelMask, Coap::Message * message = nullptr; VerifyOrExit(Get().IsActive(), error = OT_ERROR_INVALID_STATE); - VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(aAddress.IsMulticast() ? OT_COAP_TYPE_NON_CONFIRMABLE : OT_COAP_TYPE_CONFIRMABLE, @@ -83,9 +83,9 @@ otError AnnounceBeginClient::SendRequest(uint32_t aChannelMask, messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(aAddress); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoMeshCoP("sent announce begin query"); diff --git a/src/core/meshcop/border_agent.cpp b/src/core/meshcop/border_agent.cpp index 95ba5882d..b1bc33e0f 100644 --- a/src/core/meshcop/border_agent.cpp +++ b/src/core/meshcop/border_agent.cpp @@ -538,7 +538,7 @@ void BorderAgent::HandleRelayTransmit(const Coap::Message &aMessage) SuccessOrExit(error = Tlv::FindUint16Tlv(aMessage, Tlv::kJoinerRouterLocator, joinerRouterRloc)); - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_RELAY_TX)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -547,13 +547,13 @@ void BorderAgent::HandleRelayTransmit(const Coap::Message &aMessage) SuccessOrExit(error = message->SetLength(offset + aMessage.GetLength() - aMessage.GetOffset())); aMessage.CopyTo(aMessage.GetOffset(), offset, aMessage.GetLength() - aMessage.GetOffset(), *message); - messageInfo.SetSockPort(kCoapUdpPort); + messageInfo.SetSockPort(Tmf::kUdpPort); messageInfo.SetSockAddr(Get().GetMeshLocal16()); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); messageInfo.SetPeerAddr(Get().GetMeshLocal16()); messageInfo.GetPeerAddr().GetIid().SetLocator(joinerRouterRloc); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoMeshCoP("Sent to joiner router request on %s", OT_URI_PATH_RELAY_TX); @@ -581,7 +581,7 @@ otError BorderAgent::ForwardToLeader(const Coap::Message & aMessage, Coap::Message * message = nullptr; uint16_t offset = 0; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); if (aSeparate) { @@ -606,11 +606,11 @@ otError BorderAgent::ForwardToLeader(const Coap::Message & aMessage, aMessage.CopyTo(aMessage.GetOffset(), offset, aMessage.GetLength() - aMessage.GetOffset(), *message); SuccessOrExit(error = Get().GetLeaderAloc(messageInfo.GetPeerAddr())); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); messageInfo.SetSockAddr(Get().GetMeshLocal16()); - messageInfo.SetSockPort(kCoapUdpPort); + messageInfo.SetSockPort(Tmf::kUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo, HandleCoapResponse, forwardContext)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo, HandleCoapResponse, forwardContext)); // HandleCoapResponse is responsible to free this forward context. forwardContext = nullptr; @@ -676,7 +676,7 @@ otError BorderAgent::Start(void) coaps.AddResource(mProxyTransmit); coaps.AddResource(mRelayTransmit); - Get().AddResource(mRelayReceive); + Get().AddResource(mRelayReceive); SetState(OT_BORDER_AGENT_STATE_STARTED); @@ -718,7 +718,7 @@ otError BorderAgent::Stop(void) coaps.RemoveResource(mProxyTransmit); coaps.RemoveResource(mRelayTransmit); - Get().RemoveResource(mRelayReceive); + Get().RemoveResource(mRelayReceive); coaps.Stop(); diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 032493cc0..cd592d8af 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -136,15 +136,15 @@ exit: void Commissioner::AddCoapResources(void) { - Get().AddResource(mRelayReceive); - Get().AddResource(mDatasetChanged); + Get().AddResource(mRelayReceive); + Get().AddResource(mDatasetChanged); Get().AddResource(mJoinerFinalize); } void Commissioner::RemoveCoapResources(void) { - Get().RemoveResource(mRelayReceive); - Get().RemoveResource(mDatasetChanged); + Get().RemoveResource(mRelayReceive); + Get().RemoveResource(mDatasetChanged); Get().RemoveResource(mJoinerFinalize); } @@ -696,7 +696,7 @@ otError Commissioner::SendMgmtCommissionerGetRequest(const uint8_t *aTlvs, uint8 Ip6::MessageInfo messageInfo; MeshCoP::Tlv tlv; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_COMMISSIONER_GET)); @@ -715,9 +715,9 @@ otError Commissioner::SendMgmtCommissionerGetRequest(const uint8_t *aTlvs, uint8 messageInfo.SetSockAddr(Get().GetMeshLocal16()); SuccessOrExit(error = Get().GetLeaderAloc(messageInfo.GetPeerAddr())); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo, - Commissioner::HandleMgmtCommissionerGetResponse, this)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo, + Commissioner::HandleMgmtCommissionerGetResponse, this)); otLogInfoMeshCoP("sent MGMT_COMMISSIONER_GET.req to leader"); @@ -761,7 +761,7 @@ otError Commissioner::SendMgmtCommissionerSetRequest(const otCommissioningDatase Coap::Message * message; Ip6::MessageInfo messageInfo; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_COMMISSIONER_SET)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -801,9 +801,9 @@ otError Commissioner::SendMgmtCommissionerSetRequest(const otCommissioningDatase messageInfo.SetSockAddr(Get().GetMeshLocal16()); SuccessOrExit(error = Get().GetLeaderAloc(messageInfo.GetPeerAddr())); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo, - Commissioner::HandleMgmtCommissionerSetResponse, this)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo, + Commissioner::HandleMgmtCommissionerSetResponse, this)); otLogInfoMeshCoP("sent MGMT_COMMISSIONER_SET.req to leader"); @@ -848,7 +848,7 @@ otError Commissioner::SendPetition(void) mTransmitAttempts++; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_LEADER_PETITION)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -859,10 +859,10 @@ otError Commissioner::SendPetition(void) SuccessOrExit(error = commissionerId.AppendTo(*message)); SuccessOrExit(error = Get().GetLeaderAloc(messageInfo.GetPeerAddr())); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); messageInfo.SetSockAddr(Get().GetMeshLocal16()); - SuccessOrExit( - error = Get().SendMessage(*message, messageInfo, Commissioner::HandleLeaderPetitionResponse, this)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo, + Commissioner::HandleLeaderPetitionResponse, this)); otLogInfoMeshCoP("sent petition"); @@ -948,7 +948,7 @@ void Commissioner::SendKeepAlive(uint16_t aSessionId) Coap::Message * message = nullptr; Ip6::MessageInfo messageInfo; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_LEADER_KEEP_ALIVE)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -961,9 +961,9 @@ void Commissioner::SendKeepAlive(uint16_t aSessionId) messageInfo.SetSockAddr(Get().GetMeshLocal16()); SuccessOrExit(error = Get().GetLeaderAloc(messageInfo.GetPeerAddr())); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo, - Commissioner::HandleLeaderKeepAliveResponse, this)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo, + Commissioner::HandleLeaderKeepAliveResponse, this)); otLogInfoMeshCoP("sent keep alive"); @@ -1093,7 +1093,7 @@ void Commissioner::HandleDatasetChanged(Coap::Message &aMessage, const Ip6::Mess otLogInfoMeshCoP("received dataset changed"); - SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); + SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); otLogInfoMeshCoP("sent dataset changed acknowledgment"); @@ -1203,7 +1203,7 @@ otError Commissioner::SendRelayTransmit(Message &aMessage, const Ip6::MessageInf uint16_t offset; Ip6::MessageInfo messageInfo; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST); SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_RELAY_TX)); @@ -1228,9 +1228,9 @@ otError Commissioner::SendRelayTransmit(Message &aMessage, const Ip6::MessageInf messageInfo.SetPeerAddr(Get().GetMeshLocal16()); messageInfo.GetPeerAddr().GetIid().SetLocator(mJoinerRloc); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); aMessage.Free(); diff --git a/src/core/meshcop/dataset_manager.cpp b/src/core/meshcop/dataset_manager.cpp index 16c1eb41d..820cadfb5 100644 --- a/src/core/meshcop/dataset_manager.cpp +++ b/src/core/meshcop/dataset_manager.cpp @@ -278,7 +278,7 @@ void DatasetManager::SendSet(void) } } - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, mUriSet)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -288,9 +288,9 @@ void DatasetManager::SendSet(void) messageInfo.SetSockAddr(Get().GetMeshLocal16()); IgnoreError(Get().GetLeaderAloc(messageInfo.GetPeerAddr())); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = - Get().SendMessage(*message, messageInfo, &DatasetManager::HandleCoapResponse, this)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit( + error = Get().SendMessage(*message, messageInfo, &DatasetManager::HandleCoapResponse, this)); otLogInfoMeshCoP("Sent %s to leader", mUriSet); @@ -392,7 +392,7 @@ void DatasetManager::SendGetResponse(const Coap::Message & aRequest, IgnoreError(mLocal.Read(dataset)); - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -431,7 +431,7 @@ void DatasetManager::SendGetResponse(const Coap::Message & aRequest, IgnoreError(message->SetLength(message->GetLength() - 1)); } - SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); otLogInfoMeshCoP("sent dataset get response"); @@ -449,7 +449,7 @@ otError DatasetManager::SendSetRequest(const otOperationalDataset &aDataset, con Coap::Message * message; Ip6::MessageInfo messageInfo; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, mUriSet)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -578,8 +578,8 @@ otError DatasetManager::SendSetRequest(const otOperationalDataset &aDataset, con messageInfo.SetSockAddr(Get().GetMeshLocal16()); IgnoreError(Get().GetLeaderAloc(messageInfo.GetPeerAddr())); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoMeshCoP("sent dataset set request to leader"); @@ -667,7 +667,7 @@ otError DatasetManager::SendGetRequest(const otOperationalDatasetComponents &aDa datasetTlvs[length++] = Tlv::kChannelMask; } - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, mUriGet)); @@ -703,8 +703,8 @@ otError DatasetManager::SendGetRequest(const otOperationalDatasetComponents &aDa } messageInfo.SetSockAddr(Get().GetMeshLocal16()); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoMeshCoP("sent dataset get request"); @@ -729,7 +729,7 @@ ActiveDataset::ActiveDataset(Instance &aInstance) , mResourceSet(OT_URI_PATH_ACTIVE_SET, &ActiveDataset::HandleSet, this) #endif { - Get().AddResource(mResourceGet); + Get().AddResource(mResourceGet); } bool ActiveDataset::IsPartiallyComplete(void) const @@ -778,7 +778,7 @@ PendingDataset::PendingDataset(Instance &aInstance) , mResourceSet(OT_URI_PATH_PENDING_SET, &PendingDataset::HandleSet, this) #endif { - Get().AddResource(mResourceGet); + Get().AddResource(mResourceGet); } void PendingDataset::Clear(void) diff --git a/src/core/meshcop/dataset_manager_ftd.cpp b/src/core/meshcop/dataset_manager_ftd.cpp index 55a3313d3..aad142571 100644 --- a/src/core/meshcop/dataset_manager_ftd.cpp +++ b/src/core/meshcop/dataset_manager_ftd.cpp @@ -276,14 +276,14 @@ void DatasetManager::SendSetResponse(const Coap::Message & aRequest, otError error = OT_ERROR_NONE; Coap::Message *message; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, Tlv::kState, static_cast(aState))); - SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); otLogInfoMeshCoP("sent dataset set response"); @@ -460,12 +460,12 @@ exit: void ActiveDataset::StartLeader(void) { IgnoreError(GenerateLocal()); - Get().AddResource(mResourceSet); + Get().AddResource(mResourceSet); } void ActiveDataset::StopLeader(void) { - Get().RemoveResource(mResourceSet); + Get().RemoveResource(mResourceSet); } void ActiveDataset::HandleSet(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo) @@ -486,12 +486,12 @@ exit: void PendingDataset::StartLeader(void) { StartDelayTimer(); - Get().AddResource(mResourceSet); + Get().AddResource(mResourceSet); } void PendingDataset::StopLeader(void) { - Get().RemoveResource(mResourceSet); + Get().RemoveResource(mResourceSet); } void PendingDataset::HandleSet(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo) diff --git a/src/core/meshcop/energy_scan_client.cpp b/src/core/meshcop/energy_scan_client.cpp index 65971d5b1..7b4b7b9e0 100644 --- a/src/core/meshcop/energy_scan_client.cpp +++ b/src/core/meshcop/energy_scan_client.cpp @@ -55,7 +55,7 @@ EnergyScanClient::EnergyScanClient(Instance &aInstance) , mContext(nullptr) , mEnergyScan(OT_URI_PATH_ENERGY_REPORT, &EnergyScanClient::HandleReport, this) { - Get().AddResource(mEnergyScan); + Get().AddResource(mEnergyScan); } otError EnergyScanClient::SendQuery(uint32_t aChannelMask, @@ -72,7 +72,7 @@ otError EnergyScanClient::SendQuery(uint32_t aChannelM Coap::Message * message = nullptr; VerifyOrExit(Get().IsActive(), error = OT_ERROR_INVALID_STATE); - VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(aAddress.IsMulticast() ? OT_COAP_TYPE_NON_CONFIRMABLE : OT_COAP_TYPE_CONFIRMABLE, @@ -92,8 +92,8 @@ otError EnergyScanClient::SendQuery(uint32_t aChannelM messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(aAddress); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoMeshCoP("sent energy scan query"); @@ -141,7 +141,7 @@ void EnergyScanClient::HandleReport(Coap::Message &aMessage, const Ip6::MessageI mCallback(mask, energyList.list, energyList.tlv.GetLength(), mContext); } - SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); + SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); otLogInfoMeshCoP("sent energy scan report response"); diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index 492208826..d5928b035 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -70,7 +70,7 @@ Joiner::Joiner(Instance &aInstance) SetIdFromIeeeEui64(); mDiscerner.Clear(); memset(mJoinerRouters, 0, sizeof(mJoinerRouters)); - Get().AddResource(mJoinerEntrust); + Get().AddResource(mJoinerEntrust); } void Joiner::SetIdFromIeeeEui64(void) @@ -611,12 +611,12 @@ void Joiner::SendJoinerEntrustResponse(const Coap::Message &aRequest, const Ip6: Coap::Message * message; Ip6::MessageInfo responseInfo(aRequestInfo); - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); message->SetSubType(Message::kSubTypeJoinerEntrust); responseInfo.GetSockAddr().Clear(); - SuccessOrExit(error = Get().SendMessage(*message, responseInfo)); + SuccessOrExit(error = Get().SendMessage(*message, responseInfo)); SetState(OT_JOINER_STATE_JOINED); diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index 99eaf7c97..c6b2a824f 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -61,7 +61,7 @@ JoinerRouter::JoinerRouter(Instance &aInstance) , mJoinerUdpPort(0) , mIsJoinerPortConfigured(false) { - Get().AddResource(mRelayTransmit); + Get().AddResource(mRelayTransmit); } void JoinerRouter::HandleNotifierEvents(Events aEvents) @@ -143,7 +143,7 @@ void JoinerRouter::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &a SuccessOrExit(error = GetBorderAgentRloc(Get(), borderAgentRloc)); - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_RELAY_RX)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -163,9 +163,9 @@ void JoinerRouter::HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &a messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(Get().GetMeshLocal16()); messageInfo.GetPeerAddr().GetIid().SetLocator(borderAgentRloc); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoMeshCoP("Sent relay rx"); @@ -239,7 +239,7 @@ void JoinerRouter::DelaySendingJoinerEntrust(const Ip6::MessageInfo &aMessageInf VerifyOrExit(message != nullptr, error = OT_ERROR_NO_BUFS); metadata.mMessageInfo = aMessageInfo; - metadata.mMessageInfo.SetPeerPort(kCoapUdpPort); + metadata.mMessageInfo.SetPeerPort(Tmf::kUdpPort); metadata.mSendTime = TimerMilli::GetNow() + kJoinerEntrustTxDelay; metadata.mKek = aKek; @@ -314,11 +314,11 @@ otError JoinerRouter::SendJoinerEntrust(const Ip6::MessageInfo &aMessageInfo) message = PrepareJoinerEntrustMessage(); VerifyOrExit(message != nullptr, error = OT_ERROR_NO_BUFS); - IgnoreError(Get().AbortTransaction(&JoinerRouter::HandleJoinerEntrustResponse, this)); + IgnoreError(Get().AbortTransaction(&JoinerRouter::HandleJoinerEntrustResponse, this)); otLogInfoMeshCoP("Sending JOIN_ENT.ntf"); - SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo, - &JoinerRouter::HandleJoinerEntrustResponse, this)); + SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo, + &JoinerRouter::HandleJoinerEntrustResponse, this)); otLogInfoMeshCoP("Sent joiner entrust length = %d", message->GetLength()); otLogCertMeshCoP("[THCI] direction=send | type=JOIN_ENT.ntf"); @@ -341,7 +341,7 @@ Coap::Message *JoinerRouter::PrepareJoinerEntrustMessage(void) NetworkNameTlv networkName; const Tlv * tlv; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST); SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_JOINER_ENTRUST)); diff --git a/src/core/meshcop/meshcop_leader.cpp b/src/core/meshcop/meshcop_leader.cpp index 173d23389..f85c69d35 100644 --- a/src/core/meshcop/meshcop_leader.cpp +++ b/src/core/meshcop/meshcop_leader.cpp @@ -60,8 +60,8 @@ Leader::Leader(Instance &aInstance) , mDelayTimerMinimal(DelayTimerTlv::kDelayTimerMinimal) , mSessionId(Random::NonCrypto::GetUint16()) { - Get().AddResource(mPetition); - Get().AddResource(mKeepAlive); + Get().AddResource(mPetition); + Get().AddResource(mKeepAlive); } void Leader::HandlePetition(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo) @@ -127,7 +127,7 @@ void Leader::SendPetitionResponse(const Coap::Message & aRequest, otError error = OT_ERROR_NONE; Coap::Message *message; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -144,7 +144,7 @@ void Leader::SendPetitionResponse(const Coap::Message & aRequest, SuccessOrExit(error = Tlv::AppendUint16Tlv(*message, Tlv::kCommissionerSessionId, mSessionId)); } - SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); otLogInfoMeshCoP("sent petition response"); @@ -219,14 +219,14 @@ void Leader::SendKeepAliveResponse(const Coap::Message & aRequest, otError error = OT_ERROR_NONE; Coap::Message *message; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, Tlv::kState, static_cast(aState))); - SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); otLogInfoMeshCoP("sent keep alive response"); @@ -249,14 +249,14 @@ void Leader::SendDatasetChanged(const Ip6::Address &aAddress) Ip6::MessageInfo messageInfo; Coap::Message * message; - VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DATASET_CHANGED)); messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(aAddress); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoMeshCoP("sent dataset changed"); diff --git a/src/core/meshcop/panid_query_client.cpp b/src/core/meshcop/panid_query_client.cpp index 217ef781f..ef2e987df 100644 --- a/src/core/meshcop/panid_query_client.cpp +++ b/src/core/meshcop/panid_query_client.cpp @@ -54,7 +54,7 @@ PanIdQueryClient::PanIdQueryClient(Instance &aInstance) , mContext(nullptr) , mPanIdQuery(OT_URI_PATH_PANID_CONFLICT, &PanIdQueryClient::HandleConflict, this) { - Get().AddResource(mPanIdQuery); + Get().AddResource(mPanIdQuery); } otError PanIdQueryClient::SendQuery(uint16_t aPanId, @@ -69,7 +69,7 @@ otError PanIdQueryClient::SendQuery(uint16_t aPanId, Coap::Message * message = nullptr; VerifyOrExit(Get().IsActive(), error = OT_ERROR_INVALID_STATE); - VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(aAddress.IsMulticast() ? OT_COAP_TYPE_NON_CONFIRMABLE : OT_COAP_TYPE_CONFIRMABLE, @@ -87,8 +87,8 @@ otError PanIdQueryClient::SendQuery(uint16_t aPanId, messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(aAddress); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoMeshCoP("sent panid query"); @@ -130,7 +130,7 @@ void PanIdQueryClient::HandleConflict(Coap::Message &aMessage, const Ip6::Messag mCallback(panId, mask, mContext); } - SuccessOrExit(Get().SendEmptyAck(aMessage, responseInfo)); + SuccessOrExit(Get().SendEmptyAck(aMessage, responseInfo)); otLogInfoMeshCoP("sent panid query conflict response"); diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 7e9c480ef..b4d1b4065 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -1054,7 +1054,7 @@ otError Ip6::ProcessReceiveCallback(Message & aMessage, ExitNow(error = OT_ERROR_NO_ROUTE); } #if !OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE - else if ((destPort == kCoapUdpPort) && Get().IsTmfMessage(aMessageInfo)) + else if ((destPort == Tmf::kUdpPort) && Get().IsTmfMessage(aMessageInfo)) { // do not pass TMF messages ExitNow(error = OT_ERROR_NO_ROUTE); diff --git a/src/core/net/udp6.cpp b/src/core/net/udp6.cpp index 4bd952f74..ba56ae478 100644 --- a/src/core/net/udp6.cpp +++ b/src/core/net/udp6.cpp @@ -100,6 +100,13 @@ otError Udp::Socket::Bind(uint16_t aPort) return Bind(SockAddr(aPort)); } +#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE +otError Udp::Socket::BindToNetif(otNetifIdentifier aNetifIdentifier) +{ + return otPlatUdpBindToNetif(this, aNetifIdentifier); +} +#endif + otError Udp::Socket::Connect(const SockAddr &aSockAddr) { return Get().Connect(*this, aSockAddr); @@ -172,6 +179,9 @@ otError Udp::Bind(SocketHandle &aSocket, const SockAddr &aSockAddr) { otError error = OT_ERROR_NONE; + VerifyOrExit(aSockAddr.GetAddress().IsUnspecified() || Get().HasUnicastAddress(aSockAddr.GetAddress()), + error = OT_ERROR_INVALID_ARGS); + aSocket.mSockName = aSockAddr; if (!aSocket.IsBound()) @@ -191,6 +201,7 @@ otError Udp::Bind(SocketHandle &aSocket, const SockAddr &aSockAddr) } #endif +exit: return error; } @@ -270,7 +281,7 @@ otError Udp::SendTo(SocketHandle &aSocket, Message &aMessage, const MessageInfo #if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE if (!IsMlePort(aSocket.mSockName.mPort) && - !(aSocket.mSockName.mPort == ot::kCoapUdpPort && aMessage.GetSubType() == Message::kSubTypeJoinerEntrust)) + !(aSocket.mSockName.mPort == ot::Tmf::kUdpPort && aMessage.GetSubType() == Message::kSubTypeJoinerEntrust)) { SuccessOrExit(error = otPlatUdpSend(&aSocket, &aMessage, &messageInfoLocal)); } diff --git a/src/core/net/udp6.hpp b/src/core/net/udp6.hpp index dc1b61457..1d5df513c 100644 --- a/src/core/net/udp6.hpp +++ b/src/core/net/udp6.hpp @@ -37,6 +37,7 @@ #include "openthread-core-config.h" #include +#include #include "common/linked_list.hpp" #include "common/locator.hpp" @@ -167,21 +168,35 @@ public: /** * This method binds the UDP socket. * - * @param[in] aSockAddr A reference to the socket address. + * @param[in] aSockAddr A reference to the socket address. * - * @retval OT_ERROR_NONE Successfully bound the socket. - * @retval OT_ERROR_FAILED Failed to bind UDP Socket. + * @retval OT_ERROR_NONE Successfully bound the socket. + * @retval OT_ERROR_INVALID_ARGS Unable to bind to Thread network interface with the given address. + * @retval OT_ERROR_FAILED Failed to bind UDP Socket. * */ otError Bind(const SockAddr &aSockAddr); +#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE + /** + * This method binds the UDP socket to a specified network interface. + * + * @param[in] aNetifIdentifier The network interface identifier. + * + * @retval OT_ERROR_NONE Successfully bound to the network interface. + * @retval OT_ERROR_FAILED Failed to bind to the network interface. + * + */ + otError BindToNetif(otNetifIdentifier aNetifIdentifier); +#endif // OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE + /** * This method binds the UDP socket. * - * @param[in] aPort A port number. + * @param[in] aPort A port number. * - * @retval OT_ERROR_NONE Successfully bound the socket. - * @retval OT_ERROR_FAILED Failed to bind UDP Socket. + * @retval OT_ERROR_NONE Successfully bound the socket. + * @retval OT_ERROR_FAILED Failed to bind UDP Socket. * */ otError Bind(uint16_t aPort); @@ -413,11 +428,12 @@ public: /** * This method binds a UDP socket. * - * @param[in] aSocket A reference to the socket. - * @param[in] aSockAddr A reference to the socket address. + * @param[in] aSocket A reference to the socket. + * @param[in] aSockAddr A reference to the socket address. * - * @retval OT_ERROR_NONE Successfully bound the socket. - * @retval OT_ERROR_FAILED Failed to bind UDP Socket. + * @retval OT_ERROR_NONE Successfully bound the socket. + * @retval OT_ERROR_INVALID_ARGS Unable to bind to Thread network interface with the given address. + * @retval OT_ERROR_FAILED Failed to bind UDP Socket. * */ otError Bind(SocketHandle &aSocket, const SockAddr &aSockAddr); diff --git a/src/core/thread/address_resolver.cpp b/src/core/thread/address_resolver.cpp index e638b2c5f..229b70670 100644 --- a/src/core/thread/address_resolver.cpp +++ b/src/core/thread/address_resolver.cpp @@ -65,9 +65,9 @@ AddressResolver::AddressResolver(Instance &aInstance) , mQueryRetryList() , mIcmpHandler(&AddressResolver::HandleIcmpReceive, this) { - Get().AddResource(mAddressError); - Get().AddResource(mAddressQuery); - Get().AddResource(mAddressNotification); + Get().AddResource(mAddressError); + Get().AddResource(mAddressQuery); + Get().AddResource(mAddressNotification); IgnoreError(Get().RegisterHandler(mIcmpHandler)); } @@ -523,7 +523,7 @@ otError AddressResolver::SendAddressQuery(const Ip6::Address &aEid) Coap::Message * message; Ip6::MessageInfo messageInfo; - VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); message->Init(OT_COAP_TYPE_NON_CONFIRMABLE, OT_COAP_CODE_POST); SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_ADDRESS_QUERY)); @@ -534,9 +534,9 @@ otError AddressResolver::SendAddressQuery(const Ip6::Address &aEid) messageInfo.GetPeerAddr().SetToRealmLocalAllRoutersMulticast(); messageInfo.SetSockAddr(Get().GetMeshLocal16()); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoArp("Sending address query for %s", aEid.ToString().AsCString()); @@ -615,7 +615,7 @@ void AddressResolver::HandleAddressNotification(Coap::Message &aMessage, const I LogCacheEntryChange(kEntryUpdated, kReasonReceivedNotification, *entry); - if (Get().SendEmptyAck(aMessage, aMessageInfo) == OT_ERROR_NONE) + if (Get().SendEmptyAck(aMessage, aMessageInfo) == OT_ERROR_NONE) { otLogInfoArp("Sending address notification acknowledgment"); } @@ -634,7 +634,7 @@ void AddressResolver::SendAddressError(const Ip6::Address & aTarget, Coap::Message * message; Ip6::MessageInfo messageInfo; - VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); message->Init(aDestination == nullptr ? OT_COAP_TYPE_NON_CONFIRMABLE : OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST); SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_ADDRESS_ERROR)); @@ -653,9 +653,9 @@ void AddressResolver::SendAddressError(const Ip6::Address & aTarget, } messageInfo.SetSockAddr(Get().GetMeshLocal16()); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoArp("Sending address error for target %s", aTarget.ToString().AsCString()); @@ -692,7 +692,7 @@ void AddressResolver::HandleAddressError(Coap::Message &aMessage, const Ip6::Mes if (aMessage.IsConfirmable() && !aMessageInfo.GetSockAddr().IsMulticast()) { - if (Get().SendEmptyAck(aMessage, aMessageInfo) == OT_ERROR_NONE) + if (Get().SendEmptyAck(aMessage, aMessageInfo) == OT_ERROR_NONE) { otLogInfoArp("Sent address error notification acknowledgment"); } @@ -797,7 +797,7 @@ void AddressResolver::SendAddressQueryResponse(const Ip6::Address & a Coap::Message * message; Ip6::MessageInfo messageInfo; - VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST); SuccessOrExit(error = message->AppendUriPathOptions(OT_URI_PATH_ADDRESS_NOTIFY)); @@ -814,9 +814,9 @@ void AddressResolver::SendAddressQueryResponse(const Ip6::Address & a messageInfo.SetPeerAddr(aDestination); messageInfo.SetSockAddr(Get().GetMeshLocal16()); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoArp("Sending address notification for target %s", aTarget.ToString().AsCString()); diff --git a/src/core/thread/announce_begin_server.cpp b/src/core/thread/announce_begin_server.cpp index 01ce40ca7..5f3872321 100644 --- a/src/core/thread/announce_begin_server.cpp +++ b/src/core/thread/announce_begin_server.cpp @@ -51,7 +51,7 @@ AnnounceBeginServer::AnnounceBeginServer(Instance &aInstance) : AnnounceSenderBase(aInstance, AnnounceBeginServer::HandleTimer) , mAnnounceBegin(OT_URI_PATH_ANNOUNCE_BEGIN, &AnnounceBeginServer::HandleRequest, this) { - Get().AddResource(mAnnounceBegin); + Get().AddResource(mAnnounceBegin); } void AnnounceBeginServer::SendAnnounce(uint32_t aChannelMask, uint8_t aCount, uint16_t aPeriod) @@ -82,7 +82,7 @@ void AnnounceBeginServer::HandleRequest(Coap::Message &aMessage, const Ip6::Mess if (aMessage.IsConfirmable() && !aMessageInfo.GetSockAddr().IsMulticast()) { - SuccessOrExit(Get().SendEmptyAck(aMessage, responseInfo)); + SuccessOrExit(Get().SendEmptyAck(aMessage, responseInfo)); otLogInfoMeshCoP("sent announce begin response"); } diff --git a/src/core/thread/dua_manager.cpp b/src/core/thread/dua_manager.cpp index baf12386c..43241d37b 100644 --- a/src/core/thread/dua_manager.cpp +++ b/src/core/thread/dua_manager.cpp @@ -76,7 +76,7 @@ DuaManager::DuaManager(Instance &aInstance) mChildDuaRegisteredMask.Clear(); #endif - Get().AddResource(mDuaNotification); + Get().AddResource(mDuaNotification); } void DuaManager::HandleDomainPrefixUpdate(BackboneRouter::Leader::DomainPrefixState aState) @@ -86,7 +86,7 @@ void DuaManager::HandleDomainPrefixUpdate(BackboneRouter::Leader::DomainPrefixSt { if (mIsDuaPending) { - IgnoreError(Get().AbortTransaction(&DuaManager::HandleDuaResponse, this)); + IgnoreError(Get().AbortTransaction(&DuaManager::HandleDuaResponse, this)); } #if OPENTHREAD_CONFIG_DUA_ENABLE @@ -240,7 +240,7 @@ void DuaManager::RemoveDomainUnicastAddress(void) { if (mDuaState == kRegistering && mIsDuaPending) { - IgnoreError(Get().AbortTransaction(&DuaManager::HandleDuaResponse, this)); + IgnoreError(Get().AbortTransaction(&DuaManager::HandleDuaResponse, this)); } mDuaState = kNotExist; @@ -422,7 +422,7 @@ void DuaManager::PerformNextRegistration(void) #endif // OPENTHREAD_CONFIG_DUA_ENABLE // Prepare DUA.req - VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DUA_REGISTRATION_REQUEST)); @@ -488,10 +488,11 @@ void DuaManager::PerformNextRegistration(void) Get().GetServer16()); } - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); messageInfo.SetSockAddr(Get().GetMeshLocal16()); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo, &DuaManager::HandleDuaResponse, this)); + SuccessOrExit(error = + Get().SendMessage(*message, messageInfo, &DuaManager::HandleDuaResponse, this)); mIsDuaPending = true; @@ -550,7 +551,7 @@ void DuaManager::HandleDuaNotification(Coap::Message &aMessage, const Ip6::Messa VerifyOrExit(aMessage.GetCode() == OT_COAP_CODE_POST, error = OT_ERROR_PARSE); - if (aMessage.IsConfirmable() && Get().SendEmptyAck(aMessage, aMessageInfo) == OT_ERROR_NONE) + if (aMessage.IsConfirmable() && Get().SendEmptyAck(aMessage, aMessageInfo) == OT_ERROR_NONE) { otLogInfoDua("Sent DUA.ntf acknowledgment"); } @@ -655,7 +656,7 @@ void DuaManager::SendAddressNotification(Ip6::Address & aAddress, Ip6::MessageInfo messageInfo; otError error; - VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DUA_REGISTRATION_NOTIFY)); @@ -665,10 +666,10 @@ void DuaManager::SendAddressNotification(Ip6::Address & aAddress, SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kTarget, &aAddress, sizeof(aAddress))); messageInfo.GetPeerAddr().SetToRoutingLocator(Get().GetMeshLocalPrefix(), aChild.GetRloc16()); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); messageInfo.SetSockAddr(Get().GetMeshLocal16()); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoDua("Sent ADDR_NTF for child %04x DUA %s", aChild.GetRloc16(), aAddress.ToString().AsCString()); @@ -700,7 +701,7 @@ void DuaManager::UpdateChildDomainUnicastAddress(const Child &aChild, Mle::Child if (mIsDuaPending && mChildIndexDuaRegistering == childIndex) #endif { - IgnoreError(Get().AbortTransaction(&DuaManager::HandleDuaResponse, this)); + IgnoreError(Get().AbortTransaction(&DuaManager::HandleDuaResponse, this)); // Reset mRegisterCurrentChildIndex properly mRegisterCurrentChildIndex = mRegisterCurrentChildIndex && (aState == Mle::ChildDuaState::kRemoved); diff --git a/src/core/thread/energy_scan_server.cpp b/src/core/thread/energy_scan_server.cpp index 06e601a30..1e8031901 100644 --- a/src/core/thread/energy_scan_server.cpp +++ b/src/core/thread/energy_scan_server.cpp @@ -58,7 +58,7 @@ EnergyScanServer::EnergyScanServer(Instance &aInstance) , mTimer(aInstance, EnergyScanServer::HandleTimer, this) , mEnergyScan(OT_URI_PATH_ENERGY_SCAN, &EnergyScanServer::HandleRequest, this) { - Get().AddResource(mEnergyScan); + Get().AddResource(mEnergyScan); } void EnergyScanServer::HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo) @@ -96,7 +96,7 @@ void EnergyScanServer::HandleRequest(Coap::Message &aMessage, const Ip6::Message if (aMessage.IsConfirmable() && !aMessageInfo.GetSockAddr().IsMulticast()) { - SuccessOrExit(Get().SendEmptyAck(aMessage, responseInfo)); + SuccessOrExit(Get().SendEmptyAck(aMessage, responseInfo)); otLogInfoMeshCoP("sent energy scan query response"); } @@ -175,7 +175,7 @@ void EnergyScanServer::SendReport(void) Ip6::MessageInfo messageInfo; Coap::Message * message; - VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_ENERGY_REPORT)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -191,8 +191,8 @@ void EnergyScanServer::SendReport(void) messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(mCommissioner); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoMeshCoP("sent scan results"); diff --git a/src/core/thread/mesh_forwarder.cpp b/src/core/thread/mesh_forwarder.cpp index dff16a62a..aceee8150 100644 --- a/src/core/thread/mesh_forwarder.cpp +++ b/src/core/thread/mesh_forwarder.cpp @@ -1271,7 +1271,7 @@ otError MeshForwarder::GetFramePriority(const uint8_t * aFrame, dstPort = reinterpret_cast(aFrame)->GetDestinationPort(); } - if ((dstPort == Mle::kUdpPort) || (dstPort == kCoapUdpPort)) + if ((dstPort == Mle::kUdpPort) || (dstPort == Tmf::kUdpPort)) { aPriority = Message::kPriorityNet; } diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index d16881df6..fb90279f1 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -889,7 +889,7 @@ void Mle::SetRloc16(uint16_t aRloc16) // Clear cached CoAP with old RLOC source if (oldRloc16 != Mac::kShortAddrInvalid) { - Get().ClearRequests(mMeshLocal16.GetAddress()); + Get().ClearRequests(mMeshLocal16.GetAddress()); } } diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 8ecbfb93c..1a8a4dea4 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -105,7 +105,7 @@ void MleRouter::HandlePartitionChange(void) mPreviousPartitionIdTimeout = GetNetworkIdTimeout(); Get().Clear(); - IgnoreError(Get().AbortTransaction(&MleRouter::HandleAddressSolicitResponse, this)); + IgnoreError(Get().AbortTransaction(&MleRouter::HandleAddressSolicitResponse, this)); mRouterTable.Clear(); } @@ -217,8 +217,8 @@ exit: void MleRouter::StopLeader(void) { - Get().RemoveResource(mAddressSolicit); - Get().RemoveResource(mAddressRelease); + Get().RemoveResource(mAddressSolicit); + Get().RemoveResource(mAddressRelease); Get().StopLeader(); Get().StopLeader(); StopAdvertiseTimer(); @@ -362,8 +362,8 @@ void MleRouter::SetStateLeader(uint16_t aRloc16) Get().Start(); Get().StartLeader(); Get().StartLeader(); - Get().AddResource(mAddressSolicit); - Get().AddResource(mAddressRelease); + Get().AddResource(mAddressSolicit); + Get().AddResource(mAddressRelease); Get().SetForwardingEnabled(true); Get().SetTimerExpirations(kMplRouterDataMessageTimerExpirations); Get().SetBeaconEnabled(true); @@ -3578,7 +3578,7 @@ otError MleRouter::SendAddressSolicit(ThreadStatusTlv::Status aStatus) VerifyOrExit(!mAddressSolicitPending, OT_NOOP); - VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_ADDRESS_SOLICIT)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -3600,10 +3600,10 @@ otError MleRouter::SendAddressSolicit(ThreadStatusTlv::Status aStatus) SuccessOrExit(error = GetLeaderAddress(messageInfo.GetPeerAddr())); messageInfo.SetSockAddr(GetMeshLocal16()); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); - SuccessOrExit( - error = Get().SendMessage(*message, messageInfo, &MleRouter::HandleAddressSolicitResponse, this)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo, + &MleRouter::HandleAddressSolicitResponse, this)); mAddressSolicitPending = true; Log("Send Address Solicit", messageInfo.GetPeerAddr()); @@ -3624,7 +3624,7 @@ void MleRouter::SendAddressRelease(void) Ip6::MessageInfo messageInfo; Coap::Message * message; - VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_ADDRESS_RELEASE)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -3636,8 +3636,8 @@ void MleRouter::SendAddressRelease(void) messageInfo.SetSockAddr(GetMeshLocal16()); SuccessOrExit(error = GetLeaderAddress(messageInfo.GetPeerAddr())); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); Log("Send Address Release", messageInfo.GetPeerAddr()); @@ -3860,7 +3860,7 @@ void MleRouter::SendAddressSolicitResponse(const Coap::Message & aRequest, ThreadRouterMaskTlv routerMaskTlv; Coap::Message * message; - VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -3880,7 +3880,7 @@ void MleRouter::SendAddressSolicitResponse(const Coap::Message & aRequest, SuccessOrExit(error = routerMaskTlv.AppendTo(*message)); } - SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); Log("Send Address Reply", aMessageInfo.GetPeerAddr()); @@ -3920,7 +3920,7 @@ void MleRouter::HandleAddressRelease(Coap::Message &aMessage, const Ip6::Message IgnoreError(mRouterTable.Release(routerId)); - SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); + SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); Log("Send Address Release Reply", aMessageInfo.GetPeerAddr()); diff --git a/src/core/thread/mlr_manager.cpp b/src/core/thread/mlr_manager.cpp index 1b0e42d2b..d82fb1023 100644 --- a/src/core/thread/mlr_manager.cpp +++ b/src/core/thread/mlr_manager.cpp @@ -281,7 +281,7 @@ void MlrManager::SendMulticastListenerRegistration(void) VerifyOrExit(addressesNum > 0, error = OT_ERROR_NOT_FOUND); - VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST); SuccessOrExit(message->SetToken(Coap::Message::kDefaultTokenLength)); @@ -306,10 +306,10 @@ void MlrManager::SendMulticastListenerRegistration(void) Get().GetServer16()); } - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); messageInfo.SetSockAddr(mle.GetMeshLocal16()); - SuccessOrExit(error = Get().SendMessage( + SuccessOrExit(error = Get().SendMessage( *message, messageInfo, &MlrManager::HandleMulticastListenerRegistrationResponse, this)); mMlrPending = true; diff --git a/src/core/thread/network_data.cpp b/src/core/thread/network_data.cpp index 0e5a10014..d39241122 100644 --- a/src/core/thread/network_data.cpp +++ b/src/core/thread/network_data.cpp @@ -806,7 +806,7 @@ otError NetworkData::SendServerDataNotification(uint16_t aRloc16, Coap::Response Coap::Message * message = nullptr; Ip6::MessageInfo messageInfo; - VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_SERVER_DATA)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -827,8 +827,8 @@ otError NetworkData::SendServerDataNotification(uint16_t aRloc16, Coap::Response IgnoreError(Get().GetLeaderAloc(messageInfo.GetPeerAddr())); messageInfo.SetSockAddr(Get().GetMeshLocal16()); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo, aHandler, aContext)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo, aHandler, aContext)); otLogInfoNetData("Sent server data notification"); diff --git a/src/core/thread/network_data_leader_ftd.cpp b/src/core/thread/network_data_leader_ftd.cpp index de07466b8..b62f67d89 100644 --- a/src/core/thread/network_data_leader_ftd.cpp +++ b/src/core/thread/network_data_leader_ftd.cpp @@ -76,16 +76,16 @@ void Leader::Reset(void) void Leader::Start(void) { - Get().AddResource(mServerData); - Get().AddResource(mCommissioningDataGet); - Get().AddResource(mCommissioningDataSet); + Get().AddResource(mServerData); + Get().AddResource(mCommissioningDataGet); + Get().AddResource(mCommissioningDataSet); } void Leader::Stop(void) { - Get().RemoveResource(mServerData); - Get().RemoveResource(mCommissioningDataGet); - Get().RemoveResource(mCommissioningDataSet); + Get().RemoveResource(mServerData); + Get().RemoveResource(mCommissioningDataGet); + Get().RemoveResource(mCommissioningDataSet); } void Leader::IncrementVersion(void) @@ -164,7 +164,7 @@ void Leader::HandleServerData(Coap::Message &aMessage, const Ip6::MessageInfo &a networkData.GetLength()); } - SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); + SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); otLogInfoNetData("Sent network data registration acknowledgment"); @@ -305,7 +305,7 @@ void Leader::SendCommissioningGetResponse(const Coap::Message & aRequest, uint8_t * data = nullptr; uint8_t length = 0; - VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -350,7 +350,7 @@ void Leader::SendCommissioningGetResponse(const Coap::Message & aRequest, IgnoreError(message->SetLength(message->GetLength() - 1)); } - SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); otLogInfoMeshCoP("sent commissioning dataset get response"); @@ -369,14 +369,14 @@ void Leader::SendCommissioningSetResponse(const Coap::Message & aRequest, otError error = OT_ERROR_NONE; Coap::Message *message; - VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetDefaultResponseHeader(aRequest)); SuccessOrExit(error = message->SetPayloadMarker()); SuccessOrExit(error = Tlv::AppendUint8Tlv(*message, MeshCoP::Tlv::kState, static_cast(aState))); - SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, aMessageInfo)); otLogInfoMeshCoP("sent commissioning dataset set response"); diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index 7c7fd494b..dcf914fb1 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -63,10 +63,10 @@ NetworkDiagnostic::NetworkDiagnostic(Instance &aInstance) , mReceiveDiagnosticGetCallback(nullptr) , mReceiveDiagnosticGetCallbackContext(nullptr) { - Get().AddResource(mDiagnosticGetRequest); - Get().AddResource(mDiagnosticGetQuery); - Get().AddResource(mDiagnosticGetAnswer); - Get().AddResource(mDiagnosticReset); + Get().AddResource(mDiagnosticGetRequest); + Get().AddResource(mDiagnosticGetQuery); + Get().AddResource(mDiagnosticGetAnswer); + Get().AddResource(mDiagnosticReset); } void NetworkDiagnostic::SetReceiveDiagnosticGetCallback(otReceiveDiagnosticGetCallback aCallback, @@ -85,7 +85,7 @@ otError NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address &aDestination, Ip6::MessageInfo messageInfo; otCoapResponseHandler handler = nullptr; - VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); if (aDestination.IsMulticast()) { @@ -119,9 +119,9 @@ otError NetworkDiagnostic::SendDiagnosticGet(const Ip6::Address &aDestination, } messageInfo.SetPeerAddr(aDestination); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo, handler, this)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo, handler, this)); otLogInfoNetDiag("Sent diagnostic get"); @@ -181,7 +181,7 @@ void NetworkDiagnostic::HandleDiagnosticGetAnswer(Coap::Message &aMessage, const mReceiveDiagnosticGetCallback(&aMessage, &aMessageInfo, mReceiveDiagnosticGetCallbackContext); } - SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); + SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); otLogInfoNetDiag("Sent diagnostic answer acknowledgment"); @@ -487,13 +487,13 @@ void NetworkDiagnostic::HandleDiagnosticGetQuery(Coap::Message &aMessage, const // DIAG_GET.qry may be sent as a confirmable message. if (aMessage.IsConfirmable()) { - if (Get().SendEmptyAck(aMessage, aMessageInfo) == OT_ERROR_NONE) + if (Get().SendEmptyAck(aMessage, aMessageInfo) == OT_ERROR_NONE) { otLogInfoNetDiag("Sent diagnostic get query acknowledgment"); } } - VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DIAGNOSTIC_GET_ANSWER)); @@ -513,7 +513,7 @@ void NetworkDiagnostic::HandleDiagnosticGetQuery(Coap::Message &aMessage, const } messageInfo.SetPeerAddr(aMessageInfo.GetPeerAddr()); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); SuccessOrExit(error = FillRequestedTlvs(aMessage, *message, networkDiagnosticTlv)); @@ -523,7 +523,7 @@ void NetworkDiagnostic::HandleDiagnosticGetQuery(Coap::Message &aMessage, const IgnoreError(message->SetLength(message->GetLength() - 1)); } - SuccessOrExit(error = Get().SendMessage(*message, messageInfo, nullptr, this)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo, nullptr, this)); otLogInfoNetDiag("Sent diagnostic get answer"); @@ -560,7 +560,7 @@ void NetworkDiagnostic::HandleDiagnosticGetRequest(Coap::Message &aMessage, cons VerifyOrExit(networkDiagnosticTlv.GetType() == NetworkDiagnosticTlv::kTypeList, error = OT_ERROR_PARSE); - VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->SetDefaultResponseHeader(aMessage)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -573,7 +573,7 @@ void NetworkDiagnostic::HandleDiagnosticGetRequest(Coap::Message &aMessage, cons IgnoreError(message->SetLength(message->GetOffset() - 1)); } - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoNetDiag("Sent diagnostic get response"); @@ -593,7 +593,7 @@ otError NetworkDiagnostic::SendDiagnosticReset(const Ip6::Address &aDestination, Coap::Message * message = nullptr; Ip6::MessageInfo messageInfo; - VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = Get().NewMessage()) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_DIAGNOSTIC_RESET)); @@ -617,9 +617,9 @@ otError NetworkDiagnostic::SendDiagnosticReset(const Ip6::Address &aDestination, } messageInfo.SetPeerAddr(aDestination); - messageInfo.SetPeerPort(kCoapUdpPort); + messageInfo.SetPeerPort(Tmf::kUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoNetDiag("Sent network diagnostic reset"); @@ -672,7 +672,7 @@ void NetworkDiagnostic::HandleDiagnosticReset(Coap::Message &aMessage, const Ip6 } } - SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); + SuccessOrExit(Get().SendEmptyAck(aMessage, aMessageInfo)); otLogInfoNetDiag("Sent diagnostic reset acknowledgment"); diff --git a/src/core/thread/panid_query_server.cpp b/src/core/thread/panid_query_server.cpp index a45e5ebd6..d0a7d4903 100644 --- a/src/core/thread/panid_query_server.cpp +++ b/src/core/thread/panid_query_server.cpp @@ -53,7 +53,7 @@ PanIdQueryServer::PanIdQueryServer(Instance &aInstance) , mTimer(aInstance, PanIdQueryServer::HandleTimer, this) , mPanIdQuery(OT_URI_PATH_PANID_QUERY, &PanIdQueryServer::HandleQuery, this) { - Get().AddResource(mPanIdQuery); + Get().AddResource(mPanIdQuery); } void PanIdQueryServer::HandleQuery(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo) @@ -80,7 +80,7 @@ void PanIdQueryServer::HandleQuery(Coap::Message &aMessage, const Ip6::MessageIn if (aMessage.IsConfirmable() && !aMessageInfo.GetSockAddr().IsMulticast()) { - SuccessOrExit(Get().SendEmptyAck(aMessage, responseInfo)); + SuccessOrExit(Get().SendEmptyAck(aMessage, responseInfo)); otLogInfoMeshCoP("sent panid query response"); } @@ -115,7 +115,7 @@ void PanIdQueryServer::SendConflict(void) Ip6::MessageInfo messageInfo; Coap::Message * message; - VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get())) != nullptr, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = message->Init(OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST, OT_URI_PATH_PANID_CONFLICT)); SuccessOrExit(error = message->SetPayloadMarker()); @@ -128,8 +128,8 @@ void PanIdQueryServer::SendConflict(void) messageInfo.SetSockAddr(Get().GetMeshLocal16()); messageInfo.SetPeerAddr(mCommissioner); - messageInfo.SetPeerPort(kCoapUdpPort); - SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); + messageInfo.SetPeerPort(Tmf::kUdpPort); + SuccessOrExit(error = Get().SendMessage(*message, messageInfo)); otLogInfoMeshCoP("sent panid conflict"); diff --git a/src/core/thread/thread_netif.cpp b/src/core/thread/thread_netif.cpp index 3ab80dfd5..113ef49d9 100644 --- a/src/core/thread/thread_netif.cpp +++ b/src/core/thread/thread_netif.cpp @@ -49,7 +49,7 @@ namespace ot { ThreadNetif::ThreadNetif(Instance &aInstance) : Netif(aInstance) - , mCoap(aInstance) + , mTmfAgent(aInstance) #if OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE , mDhcp6Client(aInstance) #endif @@ -127,7 +127,6 @@ ThreadNetif::ThreadNetif(Instance &aInstance) , mTimeSync(aInstance) #endif { - Get().SetInterceptor(&ThreadNetif::TmfFilter, this); } void ThreadNetif::Up(void) @@ -145,7 +144,7 @@ void ThreadNetif::Up(void) SubscribeAllNodesMulticast(); IgnoreError(Get().Enable()); - IgnoreError(Get().Start(kCoapUdpPort)); + IgnoreError(Get().Start()); #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE IgnoreError(Get().Start()); #endif @@ -171,7 +170,7 @@ void ThreadNetif::Down(void) #if OPENTHREAD_CONFIG_DTLS_ENABLE Get().Stop(); #endif - IgnoreError(Get().Stop()); + IgnoreError(Get().Stop()); IgnoreError(Get().Disable()); RemoveAllExternalUnicastAddresses(); UnsubscribeAllExternalMulticastAddresses(); @@ -205,35 +204,9 @@ exit: return error; } -otError ThreadNetif::TmfFilter(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext) -{ - OT_UNUSED_VARIABLE(aMessage); - - return static_cast(aContext)->IsTmfMessage(aMessageInfo) ? OT_ERROR_NONE : OT_ERROR_NOT_TMF; -} - bool ThreadNetif::IsOnMesh(const Ip6::Address &aAddress) const { return Get().IsOnMesh(aAddress); } -bool ThreadNetif::IsTmfMessage(const Ip6::MessageInfo &aMessageInfo) -{ - bool rval = true; - - // A TMF message must comply with following rules: - // 1. The destination is a Mesh Local Address or a Link-Local Multicast Address or a Realm-Local Multicast Address, - // and the source is a Mesh Local Address. Or - // 2. Both the destination and the source are Link-Local Addresses. - VerifyOrExit( - ((Get().IsMeshLocalAddress(aMessageInfo.GetSockAddr()) || - aMessageInfo.GetSockAddr().IsLinkLocalMulticast() || aMessageInfo.GetSockAddr().IsRealmLocalMulticast()) && - Get().IsMeshLocalAddress(aMessageInfo.GetPeerAddr())) || - ((aMessageInfo.GetSockAddr().IsLinkLocal() || aMessageInfo.GetSockAddr().IsLinkLocalMulticast()) && - aMessageInfo.GetPeerAddr().IsLinkLocal()), - rval = false); -exit: - return rval; -} - } // namespace ot diff --git a/src/core/thread/thread_netif.hpp b/src/core/thread/thread_netif.hpp index 8737f7044..4eaf2c0ec 100644 --- a/src/core/thread/thread_netif.hpp +++ b/src/core/thread/thread_netif.hpp @@ -36,9 +36,9 @@ #include "openthread-core-config.h" -#include "coap/coap.hpp" #include "coap/coap_secure.hpp" #include "mac/mac.hpp" +#include "thread/tmf.hpp" #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE #include "meshcop/border_agent.hpp" @@ -180,19 +180,8 @@ public: */ bool IsOnMesh(const Ip6::Address &aAddress) const; - /** - * This method returns whether Thread Management Framework Addressing Rules are met. - * - * @retval TRUE if Thread Management Framework Addressing Rules are met. - * @retval FALSE if Thread Management Framework Addressing Rules are not met. - * - */ - bool IsTmfMessage(const Ip6::MessageInfo &aMessageInfo); - private: - static otError TmfFilter(const Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext); - - Coap::Coap mCoap; + Tmf::TmfAgent mTmfAgent; #if OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE Dhcp6::Client mDhcp6Client; #endif // OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE diff --git a/src/core/thread/thread_tlvs.hpp b/src/core/thread/thread_tlvs.hpp index a769b3820..3d986bf51 100644 --- a/src/core/thread/thread_tlvs.hpp +++ b/src/core/thread/thread_tlvs.hpp @@ -50,8 +50,6 @@ using ot::Encoding::BigEndian::HostSwap32; enum { - kCoapUdpPort = 61631, - // Thread 1.2.0 5.19.13 limits the number of IPv6 addresses should be [1, 15]. kIPv6AddressesNumMin = 1, kIPv6AddressesNumMax = 15, diff --git a/src/core/thread/tmf.cpp b/src/core/thread/tmf.cpp new file mode 100644 index 000000000..24963d0df --- /dev/null +++ b/src/core/thread/tmf.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2020, 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 + * This file implements Thread Management Framework (TMF) functionalities. + */ + +#include "thread/tmf.hpp" + +#include "common/locator-getters.hpp" + +namespace ot { +namespace Tmf { + +otError TmfAgent::Start(void) +{ + otError error; + + SuccessOrExit(error = Coap::Start(kUdpPort)); +#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE + error = mSocket.BindToNetif(OT_NETIF_THREAD); + VerifyOrExit(OT_ERROR_NONE == error, IgnoreError(mSocket.Close())); +#endif + +exit: + return error; +} + +otError TmfAgent::Filter(const ot::Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext) +{ + OT_UNUSED_VARIABLE(aMessage); + + return static_cast(aContext)->IsTmfMessage(aMessageInfo) ? OT_ERROR_NONE : OT_ERROR_NOT_TMF; +} + +bool TmfAgent::IsTmfMessage(const Ip6::MessageInfo &aMessageInfo) const +{ + bool rval = true; + + // A TMF message must comply with following rules: + // 1. The destination is a Mesh Local Address or a Link-Local Multicast Address or a Realm-Local Multicast Address, + // and the source is a Mesh Local Address. Or + // 2. Both the destination and the source are Link-Local Addresses. + VerifyOrExit( + ((Get().IsMeshLocalAddress(aMessageInfo.GetSockAddr()) || + aMessageInfo.GetSockAddr().IsLinkLocalMulticast() || aMessageInfo.GetSockAddr().IsRealmLocalMulticast()) && + Get().IsMeshLocalAddress(aMessageInfo.GetPeerAddr())) || + ((aMessageInfo.GetSockAddr().IsLinkLocal() || aMessageInfo.GetSockAddr().IsLinkLocalMulticast()) && + aMessageInfo.GetPeerAddr().IsLinkLocal()), + rval = false); +exit: + return rval; +} + +} // namespace Tmf +} // namespace ot diff --git a/src/core/thread/tmf.hpp b/src/core/thread/tmf.hpp new file mode 100644 index 000000000..cebd9c6e9 --- /dev/null +++ b/src/core/thread/tmf.hpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2020, 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 + * This file includes definitions for TMF functionality. + */ + +#ifndef OT_CORE_THREAD_TMF_HPP_ +#define OT_CORE_THREAD_TMF_HPP_ + +#include "coap/coap.hpp" + +namespace ot { +namespace Tmf { + +enum +{ + kUdpPort = 61631, ///< TMF UDP Port +}; + +/** + * This class implements functionality of the Thread TMF agent. + * + */ +class TmfAgent : public Coap::Coap +{ +public: + /** + * This constructor initializes the object. + * + * @param[in] aInstance A reference to the OpenThread instance. + * + */ + explicit TmfAgent(Instance &aInstance) + : Coap::Coap(aInstance) + { + SetInterceptor(&Filter, this); + } + + /** + * This method starts the TMF agent. + * + * @retval OT_ERROR_NONE Successfully started the CoAP service. + * @retval OT_ERROR_ALREADY Already started. + * + */ + otError Start(void); + + /** + * This method returns whether Thread Management Framework Addressing Rules are met. + * + * @retval TRUE if Thread Management Framework Addressing Rules are met. + * @retval FALSE if Thread Management Framework Addressing Rules are not met. + * + */ + bool IsTmfMessage(const Ip6::MessageInfo &aMessageInfo) const; + +private: + otError Start(uint16_t aPort); + static otError Filter(const ot::Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo, void *aContext); +}; + +} // namespace Tmf +} // namespace ot + +#endif // OT_CORE_THREAD_TMF_HPP_ diff --git a/src/posix/platform/netif.cpp b/src/posix/platform/netif.cpp index 3176e85d0..6abba510e 100644 --- a/src/posix/platform/netif.cpp +++ b/src/posix/platform/netif.cpp @@ -147,6 +147,9 @@ extern int #include "common/logging.hpp" #include "net/ip6_address.hpp" +unsigned int gNetifIndex = 0; +char gNetifName[IFNAMSIZ]; + #if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE #ifndef OPENTHREAD_POSIX_TUN_DEVICE @@ -213,8 +216,6 @@ static int sNetlinkFd = -1; ///< Used to receive netlink events. #if OPENTHREAD_POSIX_USE_MLD_MONITOR static int sMLDMonitorFd = -1; ///< Used to receive MLD events. #endif -static unsigned int sTunIndex = 0; -static char sTunName[IFNAMSIZ]; #if OPENTHREAD_POSIX_USE_MLD_MONITOR // ff02::16 static const otIp6Address kMLDv2MulticastAddress = { @@ -312,7 +313,7 @@ static void UpdateUnicast(otInstance *aInstance, const otIp6Address &aAddress, u struct in6_ifreq ifr6; memcpy(&ifr6.ifr6_addr, &aAddress, sizeof(ifr6.ifr6_addr)); - ifr6.ifr6_ifindex = static_cast(sTunIndex); + ifr6.ifr6_ifindex = static_cast(gNetifIndex); ifr6.ifr6_prefixlen = aPrefixLength; if (aIsAdded) @@ -330,7 +331,7 @@ static void UpdateUnicast(otInstance *aInstance, const otIp6Address &aAddress, u struct in6_aliasreq ifr6; memset(&ifr6, 0, sizeof(ifr6)); - strlcpy(ifr6.ifra_name, sTunName, sizeof(ifr6.ifra_name)); + strlcpy(ifr6.ifra_name, gNetifName, sizeof(ifr6.ifra_name)); ifr6.ifra_addr.sin6_family = AF_INET6; ifr6.ifra_addr.sin6_len = sizeof(ifr6.ifra_addr); memcpy(&ifr6.ifra_addr.sin6_addr, &aAddress, sizeof(struct in6_addr)); @@ -376,7 +377,7 @@ static void UpdateMulticast(otInstance *aInstance, const otIp6Address &aAddress, VerifyOrExit(sIpFd >= 0, OT_NOOP); memcpy(&mreq.ipv6mr_multiaddr, &aAddress, sizeof(mreq.ipv6mr_multiaddr)); - mreq.ipv6mr_interface = sTunIndex; + mreq.ipv6mr_interface = gNetifIndex; int err; err = setsockopt(sIpFd, IPPROTO_IPV6, (aIsAdded ? IPV6_JOIN_GROUP : IPV6_LEAVE_GROUP), &mreq, sizeof(mreq)); @@ -419,7 +420,7 @@ static void UpdateLink(otInstance *aInstance) VerifyOrExit(sIpFd >= 0, OT_NOOP); memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, sTunName, sizeof(ifr.ifr_name)); + strncpy(ifr.ifr_name, gNetifName, sizeof(ifr.ifr_name)); VerifyOrExit(ioctl(sIpFd, SIOCGIFFLAGS, &ifr) == 0, perror("ioctl"); error = OT_ERROR_FAILED); ifState = ((ifr.ifr_flags & IFF_UP) == IFF_UP) ? true : false; @@ -601,7 +602,8 @@ static void processNetifAddrEvent(otInstance *aInstance, struct nlmsghdr *aNetli otError error = OT_ERROR_NONE; struct sockaddr_in6 addr6; - VerifyOrExit(ifaddr->ifa_index == static_cast(sTunIndex) && ifaddr->ifa_family == AF_INET6, OT_NOOP); + VerifyOrExit(ifaddr->ifa_index == static_cast(gNetifIndex) && ifaddr->ifa_family == AF_INET6, + OT_NOOP); rtaLength = IFA_PAYLOAD(aNetlinkMessage); @@ -699,7 +701,7 @@ static void processNetifLinkEvent(otInstance *aInstance, struct nlmsghdr *aNetli struct ifinfomsg *ifinfo = reinterpret_cast(NLMSG_DATA(aNetlinkMessage)); otError error = OT_ERROR_NONE; - VerifyOrExit(ifinfo->ifi_index == static_cast(sTunIndex), OT_NOOP); + VerifyOrExit(ifinfo->ifi_index == static_cast(gNetifIndex), OT_NOOP); SuccessOrExit(error = otIp6SetEnabled(aInstance, ifinfo->ifi_flags & IFF_UP)); exit: @@ -756,7 +758,7 @@ static void processNetifAddrEvent(otInstance *aInstance, struct rt_msghdr *rtm) { ifam = reinterpret_cast(rtm); - VerifyOrExit(ifam->ifam_index == static_cast(sTunIndex), OT_NOOP); + VerifyOrExit(ifam->ifam_index == static_cast(gNetifIndex), OT_NOOP); addrbuf = (uint8_t *)&ifam[1]; addrmask = (unsigned int)ifam->ifam_addrs; @@ -766,7 +768,7 @@ static void processNetifAddrEvent(otInstance *aInstance, struct rt_msghdr *rtm) { ifmam = reinterpret_cast(rtm); - VerifyOrExit(ifmam->ifmam_index == static_cast(sTunIndex), OT_NOOP); + VerifyOrExit(ifmam->ifmam_index == static_cast(gNetifIndex), OT_NOOP); addrbuf = (uint8_t *)&ifmam[1]; addrmask = (unsigned int)ifmam->ifmam_addrs; @@ -847,7 +849,7 @@ static void processNetifAddrEvent(otInstance *aInstance, struct rt_msghdr *rtm) OT_UNUSED_VARIABLE(addressString); // if otLog*Plat is disabled, we'll get a warning memset(&ifr6, 0, sizeof(ifr6)); - strlcpy(ifr6.ifra_name, sTunName, sizeof(ifr6.ifra_name)); + strlcpy(ifr6.ifra_name, gNetifName, sizeof(ifr6.ifra_name)); ifr6.ifra_addr.sin6_family = AF_INET6; ifr6.ifra_addr.sin6_len = sizeof(ifr6.ifra_addr); memcpy(&ifr6.ifra_addr.sin6_addr, &addr6.sin6_addr, sizeof(struct in6_addr)); @@ -940,7 +942,7 @@ static void processNetifInfoEvent(otInstance *aInstance, struct rt_msghdr *rtm) struct if_msghdr *ifm = reinterpret_cast(rtm); otError error = OT_ERROR_NONE; - VerifyOrExit(ifm->ifm_index == static_cast(sTunIndex), OT_NOOP); + VerifyOrExit(ifm->ifm_index == static_cast(gNetifIndex), OT_NOOP); UpdateLink(aInstance); @@ -1054,7 +1056,7 @@ void platformNetifDeinit(void) } #endif - sTunIndex = 0; + gNetifIndex = 0; } #if OPENTHREAD_POSIX_USE_MLD_MONITOR @@ -1063,13 +1065,13 @@ static void mldListenerInit(void) struct ipv6_mreq mreq6; sMLDMonitorFd = SocketWithCloseExec(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6, kSocketNonBlock); - mreq6.ipv6mr_interface = sTunIndex; + mreq6.ipv6mr_interface = gNetifIndex; memcpy(&mreq6.ipv6mr_multiaddr, kMLDv2MulticastAddress.mFields.m8, sizeof(kMLDv2MulticastAddress.mFields.m8)); VerifyOrDie(setsockopt(sMLDMonitorFd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq6, sizeof(mreq6)) == 0, OT_EXIT_FAILURE); #if defined(__linux__) - VerifyOrDie(setsockopt(sMLDMonitorFd, SOL_SOCKET, SO_BINDTODEVICE, sTunName, - static_cast(strnlen(sTunName, IFNAMSIZ))) == 0, + VerifyOrDie(setsockopt(sMLDMonitorFd, SOL_SOCKET, SO_BINDTODEVICE, gNetifName, + static_cast(strnlen(gNetifName, IFNAMSIZ))) == 0, OT_EXIT_FAILURE); #endif } @@ -1099,7 +1101,7 @@ static void processMLDEvent(otInstance *aInstance) for (struct ifaddrs *ifAddr = ifAddrs; ifAddr != nullptr; ifAddr = ifAddr->ifa_next) { if (ifAddr->ifa_addr != nullptr && ifAddr->ifa_addr->sa_family == AF_INET6 && - strncmp(sTunName, ifAddr->ifa_name, IFNAMSIZ) == 0) + strncmp(gNetifName, ifAddr->ifa_name, IFNAMSIZ) == 0) { struct sockaddr_in6 *addr6 = reinterpret_cast(ifAddr->ifa_addr); @@ -1254,7 +1256,7 @@ static otError destroyTunnel(void) struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); - strncpy(ifr.ifr_name, sTunName, sizeof(ifr.ifr_name)); + strncpy(ifr.ifr_name, gNetifName, sizeof(ifr.ifr_name)); VerifyOrExit(ioctl(sIpFd, SIOCIFDESTROY, &ifr) == 0, perror("ioctl"); error = OT_ERROR_FAILED); error = OT_ERROR_NONE; @@ -1355,13 +1357,13 @@ void platformNetifInit(otInstance *aInstance, const char *aInterfaceName) VerifyOrDie(sIpFd >= 0, OT_EXIT_ERROR_ERRNO); platformConfigureNetLink(); - platformConfigureTunDevice(aInstance, aInterfaceName, sTunName, sizeof(sTunName)); + platformConfigureTunDevice(aInstance, aInterfaceName, gNetifName, sizeof(gNetifName)); - sTunIndex = if_nametoindex(sTunName); - VerifyOrDie(sTunIndex > 0, OT_EXIT_FAILURE); + gNetifIndex = if_nametoindex(gNetifName); + VerifyOrDie(gNetifIndex > 0, OT_EXIT_FAILURE); #if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE - platformUdpInit(sTunName); + platformUdpInit(gNetifName); #endif #if OPENTHREAD_POSIX_USE_MLD_MONITOR mldListenerInit(); @@ -1382,7 +1384,7 @@ void platformNetifUpdateFdSet(fd_set *aReadFdSet, fd_set *aWriteFdSet, fd_set *a { OT_UNUSED_VARIABLE(aWriteFdSet); - VerifyOrExit(sTunIndex > 0, OT_NOOP); + VerifyOrExit(gNetifIndex > 0, OT_NOOP); assert(sTunFd >= 0); assert(sNetlinkFd >= 0); @@ -1420,7 +1422,7 @@ exit: void platformNetifProcess(const fd_set *aReadFdSet, const fd_set *aWriteFdSet, const fd_set *aErrorFdSet) { OT_UNUSED_VARIABLE(aWriteFdSet); - VerifyOrExit(sTunIndex > 0, OT_NOOP); + VerifyOrExit(gNetifIndex > 0, OT_NOOP); if (FD_ISSET(sTunFd, aErrorFdSet)) { @@ -1469,10 +1471,10 @@ otError otPlatGetNetif(otInstance *aInstance, const char **outNetIfName, unsigne otError error; - VerifyOrExit(sTunIndex != 0, error = OT_ERROR_FAILED); + VerifyOrExit(gNetifIndex != 0, error = OT_ERROR_FAILED); - *outNetIfName = sTunName; - *outNetIfIndex = sTunIndex; + *outNetIfName = gNetifName; + *outNetIfIndex = gNetifIndex; error = OT_ERROR_NONE; exit: diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index 78dcab40e..d22cf1081 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -38,6 +38,7 @@ #include "openthread-posix-config.h" #include +#include #include #include #include @@ -394,6 +395,18 @@ enum SocketBlockOption */ int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption); +/** + * The name of Thread network interface. + * + */ +extern char gNetifName[IFNAMSIZ]; + +/** + * The index of Thread network interface. + * + */ +extern unsigned int gNetifIndex; + #ifdef __cplusplus } #endif diff --git a/src/posix/platform/udp.cpp b/src/posix/platform/udp.cpp index 06bea026a..16692d907 100644 --- a/src/posix/platform/udp.cpp +++ b/src/posix/platform/udp.cpp @@ -55,8 +55,6 @@ #if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE -static uint32_t sPlatNetifIndex = 0; - static const size_t kMaxUdpSize = 1280; static void *FdToHandle(int aFd) @@ -99,7 +97,7 @@ static otError transmitPacket(int aFd, uint8_t *aPayload, uint16_t aLength, cons if (IsLinkLocal(peerAddr.sin6_addr) && !aMessageInfo.mIsHostInterface) { // sin6_scope_id only works for link local destinations - peerAddr.sin6_scope_id = sPlatNetifIndex; + peerAddr.sin6_scope_id = gNetifIndex; } memset(control, 0, sizeof(control)); @@ -139,7 +137,7 @@ static otError transmitPacket(int aFd, uint8_t *aPayload, uint16_t aLength, cons cmsg->cmsg_type = IPV6_PKTINFO; cmsg->cmsg_len = CMSG_LEN(sizeof(pktinfo)); - pktinfo.ipi6_ifindex = aMessageInfo.mIsHostInterface ? 0 : sPlatNetifIndex; + pktinfo.ipi6_ifindex = aMessageInfo.mIsHostInterface ? 0 : gNetifIndex; memcpy(&pktinfo.ipi6_addr, &aMessageInfo.mSockAddr, sizeof(pktinfo.ipi6_addr)); memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo)); @@ -208,7 +206,7 @@ static otError receivePacket(int aFd, uint8_t *aPayload, uint16_t &aLength, otMe memcpy(&pktinfo, CMSG_DATA(cmsg), sizeof(pktinfo)); - aMessageInfo.mIsHostInterface = (pktinfo.ipi6_ifindex != sPlatNetifIndex); + aMessageInfo.mIsHostInterface = (pktinfo.ipi6_ifindex != gNetifIndex); memcpy(&aMessageInfo.mSockAddr, &pktinfo.ipi6_addr, sizeof(aMessageInfo.mSockAddr)); } } @@ -257,10 +255,8 @@ otError otPlatUdpBind(otUdpSocket *aUdpSocket) otError error = OT_ERROR_NONE; int fd; - assert(sPlatNetifIndex != 0); + assert(gNetifIndex != 0); assert(aUdpSocket->mHandle != nullptr); - VerifyOrExit(sPlatNetifIndex != 0, error = OT_ERROR_INVALID_STATE); - VerifyOrExit(aUdpSocket->mHandle != nullptr, error = OT_ERROR_INVALID_ARGS); VerifyOrExit(aUdpSocket->mSockName.mPort != 0, error = OT_ERROR_INVALID_ARGS); fd = FdFromHandle(aUdpSocket->mHandle); @@ -280,18 +276,52 @@ otError otPlatUdpBind(otUdpSocket *aUdpSocket) VerifyOrExit(0 == setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, sizeof(on)), error = OT_ERROR_FAILED); } - VerifyOrExit(0 == setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &sPlatNetifIndex, sizeof(sPlatNetifIndex)), + VerifyOrExit(0 == setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &gNetifIndex, sizeof(gNetifIndex)), error = OT_ERROR_FAILED); exit: if (error == OT_ERROR_FAILED) { - perror("otPlatUdpBind"); + otLogCritPlat("Failed to bind UDP socket: %s", strerror(errno)); } return error; } +otError otPlatUdpBindToNetif(otUdpSocket *aUdpSocket, otNetifIdentifier aNetifIdentifier) +{ + otError error = OT_ERROR_NONE; + int fd = FdFromHandle(aUdpSocket->mHandle); + + switch (aNetifIdentifier) + { + case OT_NETIF_UNSPECIFIED: + { +#if __linux__ + VerifyOrExit(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, nullptr, 0) == 0, error = OT_ERROR_FAILED); +#else // __NetBSD__ || __FreeBSD__ || __APPLE__ + unsigned int netifIndex = 0; + VerifyOrExit(setsockopt(fd, IPPROTO_IP, IP_BOUND_IF, &netifIndex, sizeof(netifIndex)), error = OT_ERROR_FAILED); +#endif // __linux__ + break; + } + case OT_NETIF_THREAD: + { +#if __linux__ + VerifyOrExit(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &gNetifName, strlen(gNetifName)) == 0, + error = OT_ERROR_FAILED); +#else // __NetBSD__ || __FreeBSD__ || __APPLE__ + VerifyOrExit(setsockopt(fd, IPPROTO_IP, IP_BOUND_IF, &gNetifIndex, sizeof(gNetifIndex)), + error = OT_ERROR_FAILED); +#endif // __linux__ + break; + } + } + +exit: + return error; +} + otError otPlatUdpConnect(otUdpSocket *aUdpSocket) { otError error = OT_ERROR_NONE; @@ -316,11 +346,27 @@ otError otPlatUdpConnect(otUdpSocket *aUdpSocket) #ifdef __APPLE__ sin6.sin6_family = AF_UNSPEC; #else + char netifName[IFNAMSIZ]; + socklen_t len = sizeof(netifName); + + if (getsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &netifName, &len) != 0) + { + otLogWarnPlat("Failed to read socket bound device: %s", strerror(errno)); + len = 0; + } + // There is a bug in linux that connecting to AF_UNSPEC does not disconnect. // We create new socket to disconnect. SuccessOrExit(error = otPlatUdpClose(aUdpSocket)); SuccessOrExit(error = otPlatUdpSocket(aUdpSocket)); SuccessOrExit(error = otPlatUdpBind(aUdpSocket)); + + if (len > 0 && netifName[0] != '\0') + { + fd = FdFromHandle(aUdpSocket->mHandle); + VerifyOrExit(setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, &netifName, len) == 0, error = OT_ERROR_FAILED); + } + ExitNow(); #endif } @@ -371,7 +417,7 @@ exit: void platformUdpUpdateFdSet(otInstance *aInstance, fd_set *aReadFdSet, int *aMaxFd) { - VerifyOrExit(sPlatNetifIndex != 0, OT_NOOP); + VerifyOrExit(gNetifIndex != 0, OT_NOOP); for (otUdpSocket *socket = otUdpGetSockets(aInstance); socket != nullptr; socket = socket->mNext) { @@ -402,20 +448,22 @@ void platformUdpInit(const char *aIfName) DieNow(OT_EXIT_INVALID_ARGUMENTS); } - sPlatNetifIndex = if_nametoindex(aIfName); - - if (sPlatNetifIndex == 0) + if (aIfName != gNetifName) { - perror("if_nametoindex"); + VerifyOrDie(strlen(aIfName) < sizeof(gNetifName) - 1, OT_EXIT_INVALID_ARGUMENTS); + assert(gNetifIndex == 0); + strcpy(gNetifName, aIfName); + gNetifIndex = if_nametoindex(gNetifName); + VerifyOrDie(gNetifIndex != 0, OT_EXIT_ERROR_ERRNO); } + + assert(gNetifIndex != 0); } void platformUdpProcess(otInstance *aInstance, const fd_set *aReadFdSet) { otMessageSettings msgSettings = {false, OT_MESSAGE_PRIORITY_NORMAL}; - VerifyOrExit(sPlatNetifIndex != 0, OT_NOOP); - for (otUdpSocket *socket = otUdpGetSockets(aInstance); socket != nullptr; socket = socket->mNext) { int fd = FdFromHandle(socket->mHandle); @@ -455,7 +503,6 @@ void platformUdpProcess(otInstance *aInstance, const fd_set *aReadFdSet) } } -exit: return; } diff --git a/tests/scripts/expect/tun-udp.exp b/tests/scripts/expect/tun-udp.exp new file mode 100755 index 000000000..981121329 --- /dev/null +++ b/tests/scripts/expect/tun-udp.exp @@ -0,0 +1,62 @@ +#!/usr/bin/expect -f +# +# Copyright (c) 2020, 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. +# + +# This script verifies UDP socket can connect and disconnect when platform UDP is enabled. + +source "tests/scripts/expect/_common.exp" + +set spawn_id [spawn_node 1] + +send "panid 0xface\n" +expect "Done" + +send "ifconfig up\n" +expect "Done" + +send "thread start\n" +expect "Done" + +wait_for "state" "leader" + +send "udp open\n" +expect "Done" + +send "udp bind :: 1234\n" +expect "Done" + +send "udp connect fdde:ad00:beef:0:bb1:ebd6:ad10:f33 1234\n" +expect "Done" + +send "udp connect :: 0\n" +expect "Done" + +send "udp close\n" +expect "Done" + +dispose