mirror of
https://github.com/espressif/openthread.git
synced 2026-08-18 08:29:52 +00:00
[udp] forbid udp duplicate open (#6626)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
@@ -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[])
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user