Skip to content

Commit

Permalink
Various small revisions to UDPEndPoint (#10872)
Browse files Browse the repository at this point in the history
#### Problem

Working toward #7715 Virtualize System and Inet interfaces

#### Change overview

Miscellaneous revisions with no change to functionality.

- Inline the sole use of `PostPacketBufferEvent()`.
- Use anonymous namespace instead of `static`.
- Remove unnecessary scope.

#### Testing

CI; no changes to functionality intended.
  • Loading branch information
kpschoedel authored and pull[bot] committed Feb 9, 2022
1 parent f6723f4 commit 2057588
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 78 deletions.
15 changes: 0 additions & 15 deletions src/inet/IPEndPointBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,6 @@ IPPacketInfo * IPEndPointBasis::GetPacketInfo(const System::PacketBufferHandle &
return (lPacketInfo);
}

CHIP_ERROR IPEndPointBasis::PostPacketBufferEvent(chip::System::LayerLwIP * aLayer, System::Object & aTarget,
System::EventType aEventType, System::PacketBufferHandle && aBuffer)
{
VerifyOrReturnError(aLayer != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

const CHIP_ERROR error =
aLayer->PostEvent(aTarget, aEventType, (uintptr_t) System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(aBuffer));
if (error == CHIP_NO_ERROR)
{
// If PostEvent() succeeded, it has ownership of the buffer, so we need to release it (without freeing it).
static_cast<void>(std::move(aBuffer).UnsafeRelease());
}
return error;
}

struct netif * IPEndPointBasis::FindNetifFromInterfaceId(InterfaceId aInterfaceId)
{
struct netif * lRetval = NULL;
Expand Down
2 changes: 0 additions & 2 deletions src/inet/IPEndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ class DLL_EXPORT IPEndPointBasis : public EndPointBasis
#if CHIP_SYSTEM_CONFIG_USE_LWIP
public:
static struct netif * FindNetifFromInterfaceId(InterfaceId aInterfaceId);
static CHIP_ERROR PostPacketBufferEvent(chip::System::LayerLwIP * aLayer, System::Object & aTarget,
System::EventType aEventType, System::PacketBufferHandle && aBuffer);

protected:
void HandleDataReceived(chip::System::PacketBufferHandle && aBuffer);
Expand Down
128 changes: 67 additions & 61 deletions src/inet/UDPEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@
#include <unistd.h>

// SOCK_CLOEXEC not defined on all platforms, e.g. iOS/macOS:
#ifdef SOCK_CLOEXEC
#define SOCK_FLAGS SOCK_CLOEXEC
#else
#define SOCK_FLAGS 0
#ifndef SOCK_CLOEXEC
#define SOCK_CLOEXEC 0
#endif

#if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_SOCKET_EXTENSIONS
Expand All @@ -79,6 +77,8 @@ chip::System::ObjectPool<UDPEndPoint, INET_CONFIG_NUM_UDP_ENDPOINTS> UDPEndPoint

#if CHIP_SYSTEM_CONFIG_USE_LWIP

namespace {

/*
* Note that for LwIP InterfaceId is already defined to be 'struct
* netif'; consequently, some of the checking performed here could
Expand All @@ -88,7 +88,7 @@ chip::System::ObjectPool<UDPEndPoint, INET_CONFIG_NUM_UDP_ENDPOINTS> UDPEndPoint
* udp_bind_netif(aUDP, intfId);
*
*/
static CHIP_ERROR LwIPBindInterface(struct udp_pcb * aUDP, InterfaceId intfId)
CHIP_ERROR LwIPBindInterface(struct udp_pcb * aUDP, InterfaceId intfId)
{
CHIP_ERROR res = CHIP_NO_ERROR;

Expand Down Expand Up @@ -121,6 +121,8 @@ static CHIP_ERROR LwIPBindInterface(struct udp_pcb * aUDP, InterfaceId intfId)
return res;
}

} // anonymous namespace

CHIP_ERROR UDPEndPoint::BindImpl(IPAddressType addrType, const IPAddress & addr, uint16_t port, InterfaceId intfId)
{
// Lock LwIP stack
Expand Down Expand Up @@ -263,91 +265,89 @@ CHIP_ERROR UDPEndPoint::SendMsgImpl(const IPPacketInfo * pktInfo, System::Packet
// If a source address has been specified, temporarily override the local_ip of the PCB.
// This results in LwIP using the given address being as the source address for the generated
// packet, as if the PCB had been bound to that address.
{
err_t lwipErr = ERR_VAL;
const IPAddress & srcAddr = pktInfo->SrcAddress;
const uint16_t & destPort = pktInfo->DestPort;
const InterfaceId & intfId = pktInfo->Interface;
err_t lwipErr = ERR_VAL;
const IPAddress & srcAddr = pktInfo->SrcAddress;
const uint16_t & destPort = pktInfo->DestPort;
const InterfaceId & intfId = pktInfo->Interface;

#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5

ip_addr_t lwipSrcAddr = srcAddr.ToLwIPAddr();
ip_addr_t lwipDestAddr = destAddr.ToLwIPAddr();
ip_addr_t lwipSrcAddr = srcAddr.ToLwIPAddr();
ip_addr_t lwipDestAddr = destAddr.ToLwIPAddr();

ip_addr_t boundAddr;
ip_addr_copy(boundAddr, mUDP->local_ip);
ip_addr_t boundAddr;
ip_addr_copy(boundAddr, mUDP->local_ip);

if (!ip_addr_isany(&lwipSrcAddr))
{
ip_addr_copy(mUDP->local_ip, lwipSrcAddr);
}
if (!ip_addr_isany(&lwipSrcAddr))
{
ip_addr_copy(mUDP->local_ip, lwipSrcAddr);
}

if (intfId != INET_NULL_INTERFACEID)
lwipErr = udp_sendto_if(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort, intfId);
else
lwipErr = udp_sendto(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort);
if (intfId != INET_NULL_INTERFACEID)
lwipErr = udp_sendto_if(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort, intfId);
else
lwipErr = udp_sendto(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort);

ip_addr_copy(mUDP->local_ip, boundAddr);
ip_addr_copy(mUDP->local_ip, boundAddr);

#else // LWIP_VERSION_MAJOR <= 1 && LWIP_VERSION_MINOR < 5

ipX_addr_t boundAddr;
ipX_addr_copy(boundAddr, mUDP->local_ip);

if (PCB_ISIPV6(mUDP))
{
ip6_addr_t lwipSrcAddr = srcAddr.ToIPv6();
ip6_addr_t lwipDestAddr = destAddr.ToIPv6();
ipX_addr_t boundAddr;
ipX_addr_copy(boundAddr, mUDP->local_ip);

if (!ip6_addr_isany(&lwipSrcAddr))
{
ipX_addr_copy(mUDP->local_ip, *ip6_2_ipX(&lwipSrcAddr));
}
if (PCB_ISIPV6(mUDP))
{
ip6_addr_t lwipSrcAddr = srcAddr.ToIPv6();
ip6_addr_t lwipDestAddr = destAddr.ToIPv6();

if (intfId != INET_NULL_INTERFACEID)
lwipErr =
udp_sendto_if_ip6(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort, intfId);
else
lwipErr = udp_sendto_ip6(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort);
if (!ip6_addr_isany(&lwipSrcAddr))
{
ipX_addr_copy(mUDP->local_ip, *ip6_2_ipX(&lwipSrcAddr));
}

#if INET_CONFIG_ENABLE_IPV4

if (intfId != INET_NULL_INTERFACEID)
lwipErr =
udp_sendto_if_ip6(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort, intfId);
else
{
ip4_addr_t lwipSrcAddr = srcAddr.ToIPv4();
ip4_addr_t lwipDestAddr = destAddr.ToIPv4();
ipX_addr_t boundAddr;
lwipErr = udp_sendto_ip6(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort);
}

if (!ip_addr_isany(&lwipSrcAddr))
{
ipX_addr_copy(mUDP->local_ip, *ip_2_ipX(&lwipSrcAddr));
}
#if INET_CONFIG_ENABLE_IPV4

if (intfId != INET_NULL_INTERFACEID)
lwipErr =
udp_sendto_if(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort, intfId);
else
lwipErr = udp_sendto(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort);
else
{
ip4_addr_t lwipSrcAddr = srcAddr.ToIPv4();
ip4_addr_t lwipDestAddr = destAddr.ToIPv4();
ipX_addr_t boundAddr;

if (!ip_addr_isany(&lwipSrcAddr))
{
ipX_addr_copy(mUDP->local_ip, *ip_2_ipX(&lwipSrcAddr));
}

ipX_addr_copy(mUDP->local_ip, boundAddr);
if (intfId != INET_NULL_INTERFACEID)
lwipErr = udp_sendto_if(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort, intfId);
else
lwipErr = udp_sendto(mUDP, System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(msg), &lwipDestAddr, destPort);
}

ipX_addr_copy(mUDP->local_ip, boundAddr);

#endif // INET_CONFIG_ENABLE_IPV4
#endif // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5

if (lwipErr != ERR_OK)
res = chip::System::MapErrorLwIP(lwipErr);
}

// Unlock LwIP stack
UNLOCK_TCPIP_CORE();

if (lwipErr != ERR_OK)
res = chip::System::MapErrorLwIP(lwipErr);

return res;
}

void UDPEndPoint::CloseImpl()
{

// Lock LwIP stack
LOCK_TCPIP_CORE();

Expand Down Expand Up @@ -507,7 +507,13 @@ void UDPEndPoint::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb, struct
pktInfo->DestPort = pcb->local_port;
}

PostPacketBufferEvent(lSystemLayer, *ep, kInetEvent_UDPDataReceived, std::move(buf));
const CHIP_ERROR error =
lSystemLayer->PostEvent(*ep, kInetEvent_UDPDataReceived, (uintptr_t) System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(buf));
if (error == CHIP_NO_ERROR)
{
// If PostEvent() succeeded, it has ownership of the buffer, so we need to release it (without freeing it).
static_cast<void>(std::move(buf).UnsafeRelease());
}
}

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
Expand Down Expand Up @@ -621,7 +627,7 @@ void UDPEndPoint::Free()

CHIP_ERROR UDPEndPoint::GetSocket(IPAddressType aAddressType)
{
constexpr int lType = (SOCK_DGRAM | SOCK_FLAGS);
constexpr int lType = (SOCK_DGRAM | SOCK_CLOEXEC);
constexpr int lProtocol = 0;

return IPEndPointBasis::GetSocket(aAddressType, lType, lProtocol);
Expand Down

0 comments on commit 2057588

Please sign in to comment.