mirror of
https://github.com/espressif/openthread.git
synced 2026-08-26 04:09:52 +00:00
[udp] bind to an interface when sending/connecting (#6332)
This commit is contained in:
@@ -68,7 +68,7 @@ otError otUdpBind(otInstance *aInstance, otUdpSocket *aSocket, const otSockAddr
|
||||
Instance &instance = *static_cast<Instance *>(aInstance);
|
||||
|
||||
return instance.Get<Ip6::Udp>().Bind(*static_cast<Ip6::Udp::SocketHandle *>(aSocket),
|
||||
*static_cast<const Ip6::SockAddr *>(aSockName));
|
||||
*static_cast<const Ip6::SockAddr *>(aSockName), OT_NETIF_THREAD);
|
||||
}
|
||||
|
||||
otError otUdpConnect(otInstance *aInstance, otUdpSocket *aSocket, const otSockAddr *aSockName)
|
||||
|
||||
+17
-22
@@ -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<Udp>().SetBackboneSocket(*this);
|
||||
}
|
||||
#endif
|
||||
|
||||
SuccessOrExit(error = Get<Udp>().Bind(*this, aSockAddr));
|
||||
|
||||
exit:
|
||||
return error;
|
||||
return Get<Udp>().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<ThreadNetif>().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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user