[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.
This commit is contained in:
Yakun Xu
2021-03-21 09:53:49 -07:00
committed by GitHub
parent de3ddb7169
commit 70634bf3d7
6 changed files with 22 additions and 51 deletions
+1 -2
View File
@@ -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)
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -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));
+1 -1
View File
@@ -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());
+12 -22
View File
@@ -87,17 +87,7 @@ Error Udp::Socket::Open(otUdpReceive aHandler, void *aContext)
return Get<Udp>().Open(*this, aHandler, aContext);
}
Error Udp::Socket::Bind(const SockAddr &aSockAddr)
{
return Get<Udp>().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<Udp>().BindToNetif(*this, aNetifIdentifier);
if (aNetifIdentifier == OT_NETIF_BACKBONE)
{
Get<Udp>().SetBackboneSocket(*this);
}
#endif
#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE
SuccessOrExit(error = Get<Udp>().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<Udp>().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);
+6 -24
View File
@@ -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.
*