Skip to content

Commit

Permalink
Rearrange code in IPEndPointBasis (#10849)
Browse files Browse the repository at this point in the history
* Rearrange code in IPEndPointBasis

#### Problem

Code for different Inet implementations is scattered across many `#if`s.

#### Change overview

- Groups code by implementation.
- Moves doxygen comment to the header.

Since comparing moved code in diff format is difficult, this change
explicitly does NOT make any changes to code, only its position
within the file.

This is a step toward #7715 _Virtualize System and Inet interfaces_.

#### Testing

CI; no change to functionality.

* fix mis-merge
  • Loading branch information
kpschoedel authored and pull[bot] committed Nov 10, 2021
1 parent 1c6d884 commit 1148636
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 135 deletions.
244 changes: 109 additions & 135 deletions src/inet/IPEndPointBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,44 +130,6 @@ static CHIP_ERROR CheckMulticastGroupArgs(InterfaceId aInterfaceId, const IPAddr

#if CHIP_SYSTEM_CONFIG_USE_LWIP

#if INET_CONFIG_ENABLE_IPV4
#if LWIP_IPV4 && LWIP_IGMP
static CHIP_ERROR LwIPIPv4JoinLeaveMulticastGroup(InterfaceId aInterfaceId, const IPAddress & aAddress,
err_t (*aMethod)(struct netif *, const ip4_addr_t *))
{
struct netif * const lNetif = IPEndPointBasis::FindNetifFromInterfaceId(aInterfaceId);
VerifyOrReturnError(lNetif != nullptr, INET_ERROR_UNKNOWN_INTERFACE);

const ip4_addr_t lIPv4Address = aAddress.ToIPv4();
const err_t lStatus = aMethod(lNetif, &lIPv4Address);

if (lStatus == ERR_MEM)
{
return CHIP_ERROR_NO_MEMORY;
}
return chip::System::MapErrorLwIP(lStatus);
}
#endif // LWIP_IPV4 && LWIP_IGMP
#endif // INET_CONFIG_ENABLE_IPV4

#ifdef HAVE_IPV6_MULTICAST
static CHIP_ERROR LwIPIPv6JoinLeaveMulticastGroup(InterfaceId aInterfaceId, const IPAddress & aAddress,
err_t (*aMethod)(struct netif *, const ip6_addr_t *))
{
struct netif * const lNetif = IPEndPointBasis::FindNetifFromInterfaceId(aInterfaceId);
VerifyOrReturnError(lNetif != nullptr, INET_ERROR_UNKNOWN_INTERFACE);

const ip6_addr_t lIPv6Address = aAddress.ToIPv6();
const err_t lStatus = aMethod(lNetif, &lIPv6Address);

if (lStatus == ERR_MEM)
{
return CHIP_ERROR_NO_MEMORY;
}
return chip::System::MapErrorLwIP(lStatus);
}
#endif // LWIP_IPV6_MLD && LWIP_IPV6_ND && LWIP_IPV6

CHIP_ERROR IPEndPointBasis::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback)
{
CHIP_ERROR lRetval = CHIP_ERROR_NOT_IMPLEMENTED;
Expand Down Expand Up @@ -215,6 +177,44 @@ CHIP_ERROR IPEndPointBasis::SetMulticastLoopback(IPVersion aIPVersion, bool aLoo

void IPEndPointBasis::InitImpl() {}

#if INET_CONFIG_ENABLE_IPV4
#if LWIP_IPV4 && LWIP_IGMP
static CHIP_ERROR LwIPIPv4JoinLeaveMulticastGroup(InterfaceId aInterfaceId, const IPAddress & aAddress,
err_t (*aMethod)(struct netif *, const ip4_addr_t *))
{
struct netif * const lNetif = IPEndPointBasis::FindNetifFromInterfaceId(aInterfaceId);
VerifyOrReturnError(lNetif != nullptr, INET_ERROR_UNKNOWN_INTERFACE);

const ip4_addr_t lIPv4Address = aAddress.ToIPv4();
const err_t lStatus = aMethod(lNetif, &lIPv4Address);

if (lStatus == ERR_MEM)
{
return CHIP_ERROR_NO_MEMORY;
}
return chip::System::MapErrorLwIP(lStatus);
}
#endif // LWIP_IPV4 && LWIP_IGMP
#endif // INET_CONFIG_ENABLE_IPV4

#ifdef HAVE_IPV6_MULTICAST
static CHIP_ERROR LwIPIPv6JoinLeaveMulticastGroup(InterfaceId aInterfaceId, const IPAddress & aAddress,
err_t (*aMethod)(struct netif *, const ip6_addr_t *))
{
struct netif * const lNetif = IPEndPointBasis::FindNetifFromInterfaceId(aInterfaceId);
VerifyOrReturnError(lNetif != nullptr, INET_ERROR_UNKNOWN_INTERFACE);

const ip6_addr_t lIPv6Address = aAddress.ToIPv6();
const err_t lStatus = aMethod(lNetif, &lIPv6Address);

if (lStatus == ERR_MEM)
{
return CHIP_ERROR_NO_MEMORY;
}
return chip::System::MapErrorLwIP(lStatus);
}
#endif // LWIP_IPV6_MLD && LWIP_IPV6_ND && LWIP_IPV6

#if INET_CONFIG_ENABLE_IPV4
CHIP_ERROR IPEndPointBasis::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join)
{
Expand All @@ -237,6 +237,24 @@ CHIP_ERROR IPEndPointBasis::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfa
#endif // HAVE_IPV6_MULTICAST
}

struct netif * IPEndPointBasis::FindNetifFromInterfaceId(InterfaceId aInterfaceId)
{
struct netif * lRetval = NULL;

#if LWIP_VERSION_MAJOR >= 2 && LWIP_VERSION_MINOR >= 0 && defined(NETIF_FOREACH)
NETIF_FOREACH(lRetval)
{
if (lRetval == aInterfaceId)
break;
}
#else // LWIP_VERSION_MAJOR < 2 || !defined(NETIF_FOREACH)
for (lRetval = netif_list; lRetval != NULL && lRetval != aInterfaceId; lRetval = lRetval->next)
;
#endif // LWIP_VERSION_MAJOR >= 2 && LWIP_VERSION_MINOR >= 0 && defined(NETIF_FOREACH)

return (lRetval);
}

void IPEndPointBasis::HandleDataReceived(System::PacketBufferHandle && aBuffer)
{
if ((mState == State::kListening) && (OnMessageReceived != NULL))
Expand All @@ -257,32 +275,6 @@ void IPEndPointBasis::HandleDataReceived(System::PacketBufferHandle && aBuffer)
}
}

/**
* @brief Get LwIP IP layer source and destination addressing information.
*
* @param[in] aBuffer the packet buffer containing the IP message
*
* @returns a pointer to the address information on success; otherwise,
* NULL if there is insufficient space in the packet for
* the address information.
*
* @details
* When using LwIP information about the packet is 'hidden' in the
* reserved space before the start of the data in the packet
* buffer. This is necessary because the system layer events only
* have two arguments, which in this case are used to convey the
* pointer to the end point and the pointer to the buffer.
*
* In most cases this trick of storing information before the data
* works because the first buffer in an LwIP IP message contains
* the space that was used for the Ethernet/IP/UDP headers. However,
* given the current size of the IPPacketInfo structure (40 bytes),
* it is possible for there to not be enough room to store the
* structure along with the payload in a single packet buffer. In
* practice, this should only happen for extremely large IPv4
* packets that arrive without an Ethernet header.
*
*/
IPPacketInfo * IPEndPointBasis::GetPacketInfo(const System::PacketBufferHandle & aBuffer)
{
uintptr_t lStart;
Expand All @@ -303,24 +295,6 @@ IPPacketInfo * IPEndPointBasis::GetPacketInfo(const System::PacketBufferHandle &
return (lPacketInfo);
}

struct netif * IPEndPointBasis::FindNetifFromInterfaceId(InterfaceId aInterfaceId)
{
struct netif * lRetval = NULL;

#if LWIP_VERSION_MAJOR >= 2 && LWIP_VERSION_MINOR >= 0 && defined(NETIF_FOREACH)
NETIF_FOREACH(lRetval)
{
if (lRetval == aInterfaceId)
break;
}
#else // LWIP_VERSION_MAJOR < 2 || !defined(NETIF_FOREACH)
for (lRetval = netif_list; lRetval != NULL && lRetval != aInterfaceId; lRetval = lRetval->next)
;
#endif // LWIP_VERSION_MAJOR >= 2 && LWIP_VERSION_MINOR >= 0 && defined(NETIF_FOREACH)

return (lRetval);
}

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
Expand Down Expand Up @@ -372,7 +346,24 @@ static CHIP_ERROR SocketsSetMulticastLoopback(int aSocket, IPVersion aIPVersion,
#endif // IPV6_MULTICAST_LOOP
}

CHIP_ERROR IPEndPointBasis::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback)
{
CHIP_ERROR lRetval = CHIP_ERROR_NOT_IMPLEMENTED;

lRetval = SocketsSetMulticastLoopback(mSocket, aIPVersion, aLoopback);
SuccessOrExit(lRetval);

exit:
return (lRetval);
}

void IPEndPointBasis::InitImpl()
{
mBoundIntfId = INET_NULL_INTERFACEID;
}

#if INET_CONFIG_ENABLE_IPV4

static CHIP_ERROR SocketsIPv4JoinLeaveMulticastGroup(int aSocket, InterfaceId aInterfaceId, const IPAddress & aAddress,
int aCommand)
{
Expand Down Expand Up @@ -407,6 +398,12 @@ static CHIP_ERROR SocketsIPv4JoinLeaveMulticastGroup(int aSocket, InterfaceId aI
}
return CHIP_NO_ERROR;
}

CHIP_ERROR IPEndPointBasis::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join)
{
return SocketsIPv4JoinLeaveMulticastGroup(mSocket, aInterfaceId, aAddress, join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP);
}

#endif // INET_CONFIG_ENABLE_IPV4

#if INET_IPV6_ADD_MEMBERSHIP || INET_IPV6_DROP_MEMBERSHIP
Expand All @@ -431,29 +428,6 @@ static CHIP_ERROR SocketsIPv6JoinLeaveMulticastGroup(int aSocket, InterfaceId aI
}
#endif // INET_IPV6_ADD_MEMBERSHIP || INET_IPV6_DROP_MEMBERSHIP

CHIP_ERROR IPEndPointBasis::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback)
{
CHIP_ERROR lRetval = CHIP_ERROR_NOT_IMPLEMENTED;

lRetval = SocketsSetMulticastLoopback(mSocket, aIPVersion, aLoopback);
SuccessOrExit(lRetval);

exit:
return (lRetval);
}

void IPEndPointBasis::InitImpl()
{
mBoundIntfId = INET_NULL_INTERFACEID;
}

#if INET_CONFIG_ENABLE_IPV4
CHIP_ERROR IPEndPointBasis::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join)
{
return SocketsIPv4JoinLeaveMulticastGroup(mSocket, aInterfaceId, aAddress, join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP);
}
#endif // INET_CONFIG_ENABLE_IPV4

CHIP_ERROR IPEndPointBasis::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfaceId, const IPAddress & aAddress, bool join)
{
#if CHIP_SYSTEM_CONFIG_USE_PLATFORM_MULTICAST_API
Expand Down Expand Up @@ -972,36 +946,6 @@ CHIP_ERROR IPEndPointBasis::IPv6JoinLeaveMulticastGroupImpl(InterfaceId aInterfa
return CHIP_ERROR_NOT_IMPLEMENTED;
}

CHIP_ERROR IPEndPointBasis::ConfigureProtocol(IPAddressType aAddressType, const nw_parameters_t & aParameters)
{
CHIP_ERROR res = CHIP_NO_ERROR;

nw_protocol_stack_t protocolStack = nw_parameters_copy_default_protocol_stack(aParameters);
nw_protocol_options_t ipOptions = nw_protocol_stack_copy_internet_protocol(protocolStack);

switch (aAddressType)
{

case IPAddressType::kIPv6:
nw_ip_options_set_version(ipOptions, nw_ip_version_6);
break;

#if INET_CONFIG_ENABLE_IPV4
case IPAddressType::kIPv4:
nw_ip_options_set_version(ipOptions, nw_ip_version_4);
break;
#endif // INET_CONFIG_ENABLE_IPV4

default:
res = INET_ERROR_WRONG_ADDRESS_TYPE;
break;
}
nw_release(ipOptions);
nw_release(protocolStack);

return res;
}

CHIP_ERROR IPEndPointBasis::Bind(IPAddressType aAddressType, const IPAddress & aAddress, uint16_t aPort,
const nw_parameters_t & aParameters)
{
Expand Down Expand Up @@ -1074,6 +1018,36 @@ CHIP_ERROR IPEndPointBasis::SendMsg(const IPPacketInfo * aPktInfo, chip::System:
return res;
}

CHIP_ERROR IPEndPointBasis::ConfigureProtocol(IPAddressType aAddressType, const nw_parameters_t & aParameters)
{
CHIP_ERROR res = CHIP_NO_ERROR;

nw_protocol_stack_t protocolStack = nw_parameters_copy_default_protocol_stack(aParameters);
nw_protocol_options_t ipOptions = nw_protocol_stack_copy_internet_protocol(protocolStack);

switch (aAddressType)
{

case IPAddressType::kIPv6:
nw_ip_options_set_version(ipOptions, nw_ip_version_6);
break;

#if INET_CONFIG_ENABLE_IPV4
case IPAddressType::kIPv4:
nw_ip_options_set_version(ipOptions, nw_ip_version_4);
break;
#endif // INET_CONFIG_ENABLE_IPV4

default:
res = INET_ERROR_WRONG_ADDRESS_TYPE;
break;
}
nw_release(ipOptions);
nw_release(protocolStack);

return res;
}

void IPEndPointBasis::HandleDataReceived(const nw_connection_t & aConnection)
{

Expand Down
19 changes: 19 additions & 0 deletions src/inet/IPEndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ class DLL_EXPORT IPEndPointBasis : public EndPointBasis
protected:
void HandleDataReceived(chip::System::PacketBufferHandle && aBuffer);

/**
* Get LwIP IP layer source and destination addressing information.
*
* @param[in] aBuffer The packet buffer containing the IP message.
*
* @returns a pointer to the address information on success; otherwise,
* nullptr if there is insufficient space in the packet for
* the address information.
*
* When using LwIP information about the packet is 'hidden' in the reserved space before the start of the
* data in the packet buffer. This is necessary because the system layer events only have two arguments,
* which in this case are used to convey the pointer to the end point and the pointer to the buffer.
*
* In most cases this trick of storing information before the data works because the first buffer in an
* LwIP IP message contains the space that was used for the Ethernet/IP/UDP headers. However, given the
* current size of the IPPacketInfo structure (40 bytes), it is possible for there to not be enough room
* to store the structure along with the payload in a single packet buffer. In practice, this should only
* happen for extremely large IPv4 packets that arrive without an Ethernet header.
*/
static IPPacketInfo * GetPacketInfo(const chip::System::PacketBufferHandle & aBuffer);
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

Expand Down

0 comments on commit 1148636

Please sign in to comment.