From 70634bf3d7a807d995a3618a11a9be076fe5fa40 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Mon, 22 Mar 2021 00:53:49 +0800 Subject: [PATCH] [udp] bind to Thread interface by default (#6318) Bind to Thread network by default so that UDP sockets in CLI can send multicast packets through Thread network interface. --- src/core/coap/coap.cpp | 3 +-- src/core/meshcop/dtls.cpp | 2 +- src/core/net/dnssd_server.cpp | 2 +- src/core/net/srp_server.cpp | 2 +- src/core/net/udp6.cpp | 34 ++++++++++++---------------------- src/core/net/udp6.hpp | 30 ++++++------------------------ 6 files changed, 22 insertions(+), 51 deletions(-) diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index c936c4d4c..976194068 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -1666,8 +1666,7 @@ Error Coap::Start(uint16_t aPort, otNetifIdentifier aNetifIdentifier) SuccessOrExit(error = mSocket.Open(&Coap::HandleUdpReceive, this)); socketOpened = true; - SuccessOrExit(error = mSocket.BindToNetif(aNetifIdentifier)); - SuccessOrExit(error = mSocket.Bind(aPort)); + SuccessOrExit(error = mSocket.Bind(aPort, aNetifIdentifier)); exit: if (error != kErrorNone && socketOpened) diff --git a/src/core/meshcop/dtls.cpp b/src/core/meshcop/dtls.cpp index 8feb764db..57ab274e2 100644 --- a/src/core/meshcop/dtls.cpp +++ b/src/core/meshcop/dtls.cpp @@ -215,7 +215,7 @@ Error Dtls::Bind(uint16_t aPort) VerifyOrExit(mState == kStateOpen, error = kErrorInvalidState); VerifyOrExit(mTransportCallback == nullptr, error = kErrorAlready); - SuccessOrExit(error = mSocket.Bind(aPort)); + SuccessOrExit(error = mSocket.Bind(aPort, OT_NETIF_UNSPECIFIED)); exit: return error; diff --git a/src/core/net/dnssd_server.cpp b/src/core/net/dnssd_server.cpp index 38435b7ff..15aa10063 100644 --- a/src/core/net/dnssd_server.cpp +++ b/src/core/net/dnssd_server.cpp @@ -66,7 +66,7 @@ Error Server::Start(void) VerifyOrExit(!IsRunning()); SuccessOrExit(error = mSocket.Open(&Server::HandleUdpReceive, this)); - SuccessOrExit(error = mSocket.Bind(kPort)); + SuccessOrExit(error = mSocket.Bind(kPort, OT_NETIF_UNSPECIFIED)); exit: otLogInfoDns("[server] started: %s", ErrorToString(error)); diff --git a/src/core/net/srp_server.cpp b/src/core/net/srp_server.cpp index ab6e202e3..6d7cad833 100644 --- a/src/core/net/srp_server.cpp +++ b/src/core/net/srp_server.cpp @@ -424,7 +424,7 @@ void Server::Start(void) VerifyOrExit(!IsRunning()); SuccessOrExit(error = mSocket.Open(HandleUdpReceive, this)); - SuccessOrExit(error = mSocket.Bind(0)); + SuccessOrExit(error = mSocket.Bind(0, OT_NETIF_UNSPECIFIED)); SuccessOrExit(error = PublishServerData()); diff --git a/src/core/net/udp6.cpp b/src/core/net/udp6.cpp index d79a3091a..f97e5c623 100644 --- a/src/core/net/udp6.cpp +++ b/src/core/net/udp6.cpp @@ -87,17 +87,7 @@ Error Udp::Socket::Open(otUdpReceive aHandler, void *aContext) return Get().Open(*this, aHandler, aContext); } -Error Udp::Socket::Bind(const SockAddr &aSockAddr) -{ - return Get().Bind(*this, aSockAddr); -} - -Error Udp::Socket::Bind(uint16_t aPort) -{ - return Bind(SockAddr(aPort)); -} - -Error Udp::Socket::BindToNetif(otNetifIdentifier aNetifIdentifier) +Error Udp::Socket::Bind(const SockAddr &aSockAddr, otNetifIdentifier aNetifIdentifier) { OT_UNUSED_VARIABLE(aNetifIdentifier); @@ -108,15 +98,23 @@ Error Udp::Socket::BindToNetif(otNetifIdentifier aNetifIdentifier) #endif #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - Get().BindToNetif(*this, aNetifIdentifier); + if (aNetifIdentifier == OT_NETIF_BACKBONE) + { + Get().SetBackboneSocket(*this); + } #endif -#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE + SuccessOrExit(error = Get().Bind(*this, aSockAddr)); + exit: -#endif return error; } +Error Udp::Socket::Bind(uint16_t aPort, otNetifIdentifier aNetifIdentifier) +{ + return Bind(SockAddr(aPort), aNetifIdentifier); +} + Error Udp::Socket::Connect(const SockAddr &aSockAddr) { return Get().Connect(*this, aSockAddr); @@ -253,14 +251,6 @@ exit: } #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE -void Udp::BindToNetif(SocketHandle &aSocket, otNetifIdentifier aNetifIdentifier) -{ - if (aNetifIdentifier == OT_NETIF_BACKBONE) - { - SetBackboneSocket(aSocket); - } -} - void Udp::SetBackboneSocket(SocketHandle &aSocket) { RemoveSocket(aSocket); diff --git a/src/core/net/udp6.hpp b/src/core/net/udp6.hpp index 8c23c16aa..04d53affa 100644 --- a/src/core/net/udp6.hpp +++ b/src/core/net/udp6.hpp @@ -174,36 +174,27 @@ 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. + * @param[in] aNetifIdentifier The network interface identifier. * * @retval kErrorNone Successfully bound the socket. * @retval kErrorInvalidArgs Unable to bind to Thread network interface with the given address. * @retval kErrorFailed Failed to bind UDP Socket. * */ - Error Bind(const SockAddr &aSockAddr); - - /** - * This method binds the UDP socket to a specified network interface. - * - * @param[in] aNetifIdentifier The network interface identifier. - * - * @retval kErrorNone Successfully bound to the network interface. - * @retval kErrorFailed Failed to bind to the network interface. - * - */ - Error BindToNetif(otNetifIdentifier aNetifIdentifier); + Error Bind(const SockAddr &aSockAddr, otNetifIdentifier aNetifIdentifier = OT_NETIF_THREAD); /** * This method binds the UDP socket. * - * @param[in] aPort A port number. + * @param[in] aPort A port number. + * @param[in] aNetifIdentifier The network interface identifier. * * @retval kErrorNone Successfully bound the socket. * @retval kErrorFailed Failed to bind UDP Socket. * */ - Error Bind(uint16_t aPort); + Error Bind(uint16_t aPort, otNetifIdentifier aNetifIdentifier = OT_NETIF_THREAD); /** * This method binds the UDP socket. @@ -468,15 +459,6 @@ public: */ Error Bind(SocketHandle &aSocket, const SockAddr &aSockAddr); - /** - * This method binds a UDP socket to the Network interface. - * - * @param[in] aSocket A reference to the socket. - * @param[in] aNetifIdentifier The network interface identifier. - * - */ - void BindToNetif(SocketHandle &aSocket, otNetifIdentifier aNetifIdentifier); - /** * This method connects a UDP socket. *