From 8985f29e8eab0b7f2a1159329ab87e73c1176b11 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 6 Apr 2026 21:45:32 -0700 Subject: [PATCH] [udp] change `Ip6::Udp::GetUdpSockets()` to return `LinkedList` (#12843) This change updates `Ip6::Udp::GetUdpSockets()` to return a reference to the `LinkedList` 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. --- src/core/api/udp_api.cpp | 5 ++++- src/core/net/udp6.hpp | 6 +++--- tests/nexus/platform/nexus_udp.cpp | 21 ++++++++++----------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/core/api/udp_api.cpp b/src/core/api/udp_api.cpp index dcca993fc..ed42d9a8a 100644 --- a/src/core/api/udp_api.cpp +++ b/src/core/api/udp_api.cpp @@ -81,7 +81,10 @@ exit: return error; } -otUdpSocket *otUdpGetSockets(otInstance *aInstance) { return AsCoreType(aInstance).Get().GetUdpSockets(); } +otUdpSocket *otUdpGetSockets(otInstance *aInstance) +{ + return AsCoreType(aInstance).Get().GetUdpSockets().GetHead(); +} #if OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE void otUdpForwardSetForwarder(otInstance *aInstance, otUdpForwarder aForwarder, void *aContext) diff --git a/src/core/net/udp6.hpp b/src/core/net/udp6.hpp index b2ecc923a..ef88cbfaf 100644 --- a/src/core/net/udp6.hpp +++ b/src/core/net/udp6.hpp @@ -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 &GetUdpSockets(void) { return mSockets; } #if OPENTHREAD_CONFIG_UDP_FORWARD_ENABLE /** diff --git a/tests/nexus/platform/nexus_udp.cpp b/tests/nexus/platform/nexus_udp.cpp index 577e722dc..c3f802630 100644 --- a/tests/nexus/platform/nexus_udp.cpp +++ b/tests/nexus/platform/nexus_udp.cpp @@ -177,23 +177,22 @@ bool Udp::HandleReceive(const Message &aMessage, const Ip6::Headers &aHeaders) ExitNow(); } - for (Ip6::Udp::SocketHandle *socket = GetNode().Get().GetUdpSockets(); socket != nullptr; - socket = socket->GetNext()) + for (Ip6::Udp::SocketHandle &socket : GetNode().Get().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(); }