[udp] change Ip6::Udp::GetUdpSockets() to return LinkedList (#12843)

This change updates `Ip6::Udp::GetUdpSockets()` to return a reference
to the `LinkedList<SocketHandle>` instead of a pointer to the head of
the list. This allows for cleaner iteration using range-based for
loops and provides a more idiomatic C++ interface.

Call sites are updated accordingly. Specifically, the Nexus UDP
platform  code now uses a range-based for loop to iterate through the
sockets.
This commit is contained in:
Abtin Keshavarzian
2026-04-06 23:45:32 -05:00
committed by GitHub
parent ac01d4b132
commit 8985f29e8e
3 changed files with 17 additions and 15 deletions
+4 -1
View File
@@ -81,7 +81,10 @@ exit:
return error;
}
otUdpSocket *otUdpGetSockets(otInstance *aInstance) { return AsCoreType(aInstance).Get<Ip6::Udp>().GetUdpSockets(); }
otUdpSocket *otUdpGetSockets(otInstance *aInstance)
{
return AsCoreType(aInstance).Get<Ip6::Udp>().GetUdpSockets().GetHead();
}
#if OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE
void otUdpForwardSetForwarder(otInstance *aInstance, otUdpForwarder aForwarder, void *aContext)
+3 -3
View File
@@ -500,11 +500,11 @@ public:
void HandlePayload(Message &aMessage, MessageInfo &aMessageInfo);
/**
* Returns the head of UDP Sockets list.
* Returns the UDP Sockets linked list.
*
* @returns A pointer to the head of UDP Socket linked list.
* @returns The UDP Sockets linked list.
*/
SocketHandle *GetUdpSockets(void) { return mSockets.GetHead(); }
LinkedList<SocketHandle> &GetUdpSockets(void) { return mSockets; }
#if OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE
/**
+10 -11
View File
@@ -177,23 +177,22 @@ bool Udp::HandleReceive(const Message &aMessage, const Ip6::Headers &aHeaders)
ExitNow();
}
for (Ip6::Udp::SocketHandle *socket = GetNode().Get<Ip6::Udp>().GetUdpSockets(); socket != nullptr;
socket = socket->GetNext())
for (Ip6::Udp::SocketHandle &socket : GetNode().Get<Ip6::Udp>().GetUdpSockets())
{
Ip6::MessageInfo messageInfo;
if (!socket->ShouldUsePlatformUdp())
if (!socket.ShouldUsePlatformUdp())
{
continue;
}
if (socket->GetSockName().GetPort() != aHeaders.GetDestinationPort())
if (socket.GetSockName().GetPort() != aHeaders.GetDestinationPort())
{
continue;
}
if (socket->GetSockName().GetAddress().IsUnspecified() ||
socket->GetSockName().GetAddress() == aHeaders.GetDestinationAddress())
if (socket.GetSockName().GetAddress().IsUnspecified() ||
socket.GetSockName().GetAddress() == aHeaders.GetDestinationAddress())
{
// Found a matching socket.
}
@@ -202,16 +201,16 @@ bool Udp::HandleReceive(const Message &aMessage, const Ip6::Headers &aHeaders)
continue;
}
if (socket->GetPeerName().GetPort() != 0)
if (socket.GetPeerName().GetPort() != 0)
{
if (socket->GetPeerName().GetPort() != aHeaders.GetSourcePort() ||
socket->GetPeerName().GetAddress() != aHeaders.GetSourceAddress())
if (socket.GetPeerName().GetPort() != aHeaders.GetSourcePort() ||
socket.GetPeerName().GetAddress() != aHeaders.GetSourceAddress())
{
continue;
}
}
if (socket->mHandler == nullptr)
if (socket.mHandler == nullptr)
{
continue;
}
@@ -229,7 +228,7 @@ bool Udp::HandleReceive(const Message &aMessage, const Ip6::Headers &aHeaders)
VerifyOrExit(payload != nullptr);
payload->RemoveHeader(sizeof(Ip6::Header) + sizeof(Ip6::Udp::Header));
socket->mHandler(socket->mContext, payload, &messageInfo);
socket.mHandler(socket.mContext, payload, &messageInfo);
payload->Free();
}