From 086be076ee66411f21b5004696f452309479b13e Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Wed, 24 Mar 2021 03:47:11 +0800 Subject: [PATCH] [udp] bind to an interface when sending/connecting (#6332) --- src/core/api/udp_api.cpp | 2 +- src/core/net/udp6.cpp | 39 +++++++++++++++++---------------------- src/core/net/udp6.hpp | 3 ++- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/core/api/udp_api.cpp b/src/core/api/udp_api.cpp index bd907e721..7bd961b28 100644 --- a/src/core/api/udp_api.cpp +++ b/src/core/api/udp_api.cpp @@ -68,7 +68,7 @@ otError otUdpBind(otInstance *aInstance, otUdpSocket *aSocket, const otSockAddr Instance &instance = *static_cast(aInstance); return instance.Get().Bind(*static_cast(aSocket), - *static_cast(aSockName)); + *static_cast(aSockName), OT_NETIF_THREAD); } otError otUdpConnect(otInstance *aInstance, otUdpSocket *aSocket, const otSockAddr *aSockName) diff --git a/src/core/net/udp6.cpp b/src/core/net/udp6.cpp index f97e5c623..a01504151 100644 --- a/src/core/net/udp6.cpp +++ b/src/core/net/udp6.cpp @@ -89,25 +89,7 @@ Error Udp::Socket::Open(otUdpReceive aHandler, void *aContext) Error Udp::Socket::Bind(const SockAddr &aSockAddr, otNetifIdentifier aNetifIdentifier) { - OT_UNUSED_VARIABLE(aNetifIdentifier); - - Error error = kErrorNone; - -#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE - SuccessOrExit(error = otPlatUdpBindToNetif(this, aNetifIdentifier)); -#endif - -#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE - if (aNetifIdentifier == OT_NETIF_BACKBONE) - { - Get().SetBackboneSocket(*this); - } -#endif - - SuccessOrExit(error = Get().Bind(*this, aSockAddr)); - -exit: - return error; + return Get().Bind(*this, aSockAddr, aNetifIdentifier); } Error Udp::Socket::Bind(uint16_t aPort, otNetifIdentifier aNetifIdentifier) @@ -220,10 +202,23 @@ exit: return error; } -Error Udp::Bind(SocketHandle &aSocket, const SockAddr &aSockAddr) +Error Udp::Bind(SocketHandle &aSocket, const SockAddr &aSockAddr, otNetifIdentifier aNetifIdentifier) { + OT_UNUSED_VARIABLE(aNetifIdentifier); + Error error = kErrorNone; +#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE + SuccessOrExit(error = otPlatUdpBindToNetif(&aSocket, aNetifIdentifier)); +#endif + +#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE + if (aNetifIdentifier == OT_NETIF_BACKBONE) + { + SetBackboneSocket(aSocket); + } +#endif + VerifyOrExit(aSockAddr.GetAddress().IsUnspecified() || Get().HasUnicastAddress(aSockAddr.GetAddress()), error = kErrorInvalidArgs); @@ -295,7 +290,7 @@ Error Udp::Connect(SocketHandle &aSocket, const SockAddr &aSockAddr) if (!aSocket.IsBound()) { - SuccessOrExit(error = Bind(aSocket, aSocket.GetSockName())); + SuccessOrExit(error = Bind(aSocket, aSocket.GetSockName(), OT_NETIF_THREAD)); } #if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE @@ -356,7 +351,7 @@ Error Udp::SendTo(SocketHandle &aSocket, Message &aMessage, const MessageInfo &a if (!aSocket.IsBound()) { - SuccessOrExit(error = Bind(aSocket, aSocket.GetSockName())); + SuccessOrExit(error = Bind(aSocket, aSocket.GetSockName(), OT_NETIF_THREAD)); } messageInfoLocal.SetSockPort(aSocket.GetSockName().mPort); diff --git a/src/core/net/udp6.hpp b/src/core/net/udp6.hpp index 04d53affa..1992cb415 100644 --- a/src/core/net/udp6.hpp +++ b/src/core/net/udp6.hpp @@ -451,13 +451,14 @@ public: * * @param[in] aSocket A reference to the socket. * @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(SocketHandle &aSocket, const SockAddr &aSockAddr); + Error Bind(SocketHandle &aSocket, const SockAddr &aSockAddr, otNetifIdentifier aNetifIdentifier); /** * This method connects a UDP socket.