[udp] forbid udp duplicate open (#6626)

This commit is contained in:
Simon Lin
2021-06-23 17:01:47 -07:00
committed by GitHub
parent d177c610f0
commit e9e875de24
7 changed files with 58 additions and 2 deletions
+1 -1
View File
@@ -53,7 +53,7 @@ extern "C" {
* @note This number versions both OpenThread platform and user APIs.
*
*/
#define OPENTHREAD_API_VERSION (128)
#define OPENTHREAD_API_VERSION (129)
/**
* @addtogroup api-instance
+11
View File
@@ -160,6 +160,17 @@ otMessage *otUdpNewMessage(otInstance *aInstance, const otMessageSettings *aSett
*/
otError otUdpOpen(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCallback, void *aContext);
/**
* Check if a UDP socket is open.
*
* @param[in] aInstance A pointer to an OpenThread instance.
* @param[in] aSocket A pointer to a UDP socket structure.
*
* @returns Whether the UDP socket is open.
*
*/
bool otUdpIsOpen(otInstance *aInstance, const otUdpSocket *aSocket);
/**
* Close a UDP/IPv6 socket.
*
+7 -1
View File
@@ -109,7 +109,13 @@ otError UdpExample::ProcessOpen(uint8_t aArgsLength, Arg aArgs[])
OT_UNUSED_VARIABLE(aArgsLength);
OT_UNUSED_VARIABLE(aArgs);
return otUdpOpen(mInterpreter.mInstance, &mSocket, HandleUdpReceive, this);
otError error;
VerifyOrExit(!otUdpIsOpen(mInterpreter.mInstance, &mSocket), error = OT_ERROR_ALREADY);
error = otUdpOpen(mInterpreter.mInstance, &mSocket, HandleUdpReceive, this);
exit:
return error;
}
otError UdpExample::ProcessSend(uint8_t aArgsLength, Arg aArgs[])
+7
View File
@@ -56,6 +56,13 @@ otError otUdpOpen(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCal
return instance.Get<Ip6::Udp>().Open(*static_cast<Ip6::Udp::SocketHandle *>(aSocket), aCallback, aContext);
}
bool otUdpIsOpen(otInstance *aInstance, const otUdpSocket *aSocket)
{
Instance &instance = *static_cast<Instance *>(aInstance);
return instance.Get<Ip6::Udp>().IsOpen(*static_cast<const Ip6::Udp::SocketHandle *>(aSocket));
}
otError otUdpClose(otInstance *aInstance, otUdpSocket *aSocket)
{
Instance &instance = *static_cast<Instance *>(aInstance);
+5
View File
@@ -133,8 +133,13 @@ Error Server::UpdateService(void)
void Server::Start(void)
{
VerifyOrExit(!mSocket.IsOpen());
IgnoreError(mSocket.Open(&Server::HandleUdpReceive, this));
IgnoreError(mSocket.Bind(kDhcpServerPort));
exit:
return;
}
void Server::Stop(void)
+9
View File
@@ -87,6 +87,11 @@ Error Udp::Socket::Open(otUdpReceive aHandler, void *aContext)
return Get<Udp>().Open(*this, aHandler, aContext);
}
bool Udp::Socket::IsOpen(void) const
{
return Get<Udp>().IsOpen(*this);
}
Error Udp::Socket::Bind(const SockAddr &aSockAddr, otNetifIdentifier aNetifIdentifier)
{
return Get<Udp>().Bind(*this, aSockAddr, aNetifIdentifier);
@@ -186,6 +191,8 @@ Error Udp::Open(SocketHandle &aSocket, otUdpReceive aHandler, void *aContext)
{
Error error = kErrorNone;
OT_ASSERT(!IsOpen(aSocket));
aSocket.GetSockName().Clear();
aSocket.GetPeerName().Clear();
aSocket.mHandler = aHandler;
@@ -308,6 +315,8 @@ Error Udp::Close(SocketHandle &aSocket)
{
Error error = kErrorNone;
VerifyOrExit(IsOpen(aSocket));
#if OPENTHREAD_CONFIG_PLATFORM_UDP_ENABLE
error = otPlatUdpClose(&aSocket);
#endif
+18
View File
@@ -171,6 +171,14 @@ public:
*/
Error Open(otUdpReceive aHandler, void *aContext);
/**
* This method returns if the UDP socket is open.
*
* @returns If the UDP socket is open.
*
*/
bool IsOpen(void) const;
/**
* This method binds the UDP socket.
*
@@ -446,6 +454,16 @@ public:
*/
Error Open(SocketHandle &aSocket, otUdpReceive aHandler, void *aContext);
/**
* This method returns if a UDP socket is open.
*
* @param[in] aSocket A reference to the socket.
*
* @returns If the UDP socket is open.
*
*/
bool IsOpen(const SocketHandle &aSocket) const { return mSockets.Contains(aSocket); }
/**
* This method binds a UDP socket.
*