Skip to content

Commit

Permalink
[Inet] Prune code for LwIP version 1 (#13005)
Browse files Browse the repository at this point in the history
* Remove #ifs for LwIP version 1

#### Problem

CHIP can't build with LwIP version 1, but there are a number of
inherited conditional #if blocks for it.

#### Change overview

- Remove conditional code for LwIP version 1
- Fix conditions that were meant to catch LwIP 2.0 but actually would
  apply to any LwIP x.0

#### Testing

CI; no change to functionality intended

* restyle

* add static_assert for LwIP ≥ 2
  • Loading branch information
kpschoedel authored and pull[bot] committed Dec 23, 2021
1 parent c05d455 commit 1298261
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 310 deletions.
11 changes: 0 additions & 11 deletions src/inet/IPAddress-StringFuncts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,8 @@ char * IPAddress::ToString(char * buf, uint32_t bufSize) const
#if INET_CONFIG_ENABLE_IPV4
if (IsIPv4())
{
#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
ip4_addr_t ip4_addr = ToIPv4();
ip4addr_ntoa_r(&ip4_addr, buf, (int) bufSize);
#else // LWIP_VERSION_MAJOR <= 1
ip_addr_t ip4_addr = ToIPv4();
ipaddr_ntoa_r(&ip4_addr, buf, (int) bufSize);
#endif // LWIP_VERSION_MAJOR <= 1
}
else
#endif // INET_CONFIG_ENABLE_IPV4
Expand Down Expand Up @@ -93,15 +88,9 @@ bool IPAddress::FromString(const char * str, IPAddress & output)
if (strchr(str, ':') == nullptr)
{
#if CHIP_SYSTEM_CONFIG_USE_LWIP
#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
ip4_addr_t ipv4Addr;
if (!ip4addr_aton(str, &ipv4Addr))
return false;
#else // LWIP_VERSION_MAJOR <= 1
ip_addr_t ipv4Addr;
if (!ipaddr_aton(str, &ipv4Addr))
return false;
#endif // LWIP_VERSION_MAJOR <= 1
#else // !CHIP_SYSTEM_CONFIG_USE_LWIP
struct in_addr ipv4Addr;
if (inet_pton(AF_INET, str, &ipv4Addr) < 1)
Expand Down
2 changes: 0 additions & 2 deletions src/inet/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ ip4_addr_t IPAddress::ToIPv4() const

#endif // INET_CONFIG_ENABLE_IPV4

#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
ip_addr_t IPAddress::ToLwIPAddr(void) const
{
ip_addr_t ret;
Expand Down Expand Up @@ -173,7 +172,6 @@ lwip_ip_addr_type IPAddress::ToLwIPAddrType(IPAddressType typ)

return ret;
}
#endif // LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5

ip6_addr_t IPAddress::ToIPv6() const
{
Expand Down
22 changes: 1 addition & 21 deletions src/inet/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,6 @@
#define NL_INET_IPV6_ADDR_LEN_IN_BYTES (16)
#define NL_INET_IPV6_MCAST_GROUP_LEN_IN_BYTES (14)

/**
* @brief Adaptation for LwIP ip4_addr_t type.
*
* @details
* Before LwIP 2.0.0, the \c ip_addr_t type alias referred to a structure comprising
* an IPv4 address. At LwIP 2.0.0 and thereafter, this type alias is renamed \c ip4_addr_t
* and \c ip_addr_t is replaced with an alias to a union of both. Here, the \c ip4_addr_t
* type alias is provided even when the LwIP version is earlier than 2.0.0 so as to prepare
* for the import of the new logic.
*/
#if CHIP_SYSTEM_CONFIG_USE_LWIP && INET_CONFIG_ENABLE_IPV4 && LWIP_VERSION_MAJOR < 2 && LWIP_VERSION_MINOR < 5
typedef ip_addr_t ip4_addr_t;
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && INET_CONFIG_ENABLE_IPV4 && LWIP_VERSION_MAJOR < 2 && LWIP_VERSION_MINOR < 5

#if CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_VERSION_MAJOR == 1 && LWIP_VERSION_MINOR >= 5
typedef u8_t lwip_ip_addr_type;
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_VERSION_MAJOR == 1 && LWIP_VERSION_MINOR >= 5

namespace chip {
namespace Inet {

Expand Down Expand Up @@ -489,7 +471,6 @@ class DLL_EXPORT IPAddress

#if CHIP_SYSTEM_CONFIG_USE_LWIP

#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
/**
* @fn ToLwIPAddr() const
*
Expand All @@ -508,10 +489,9 @@ class DLL_EXPORT IPAddress
*
* @details
* Use <tt>ToLwIPAddrType(IPAddressType)</tt> to convert the IP address type
* to its underlying LwIP address type code. (LWIP_VERSION_MAJOR > 1 only).
* to its underlying LwIP address type code.
*/
static lwip_ip_addr_type ToLwIPAddrType(IPAddressType);
#endif // LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5

ip6_addr_t ToIPv6(void) const;

Expand Down
11 changes: 3 additions & 8 deletions src/inet/InetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ CHIP_ERROR InterfaceId::InterfaceNameToId(const char * intfName, InterfaceId & i
return INET_ERROR_UNKNOWN_INTERFACE;
}
struct netif * intf;
#if LWIP_VERSION_MAJOR >= 2 && LWIP_VERSION_MINOR >= 0 && defined(NETIF_FOREACH)
#if defined(NETIF_FOREACH)
NETIF_FOREACH(intf)
#else
for (intf = netif_list; intf != NULL; intf = intf->next)
Expand All @@ -120,7 +120,7 @@ bool InterfaceIterator::Next()
// Verify the previous netif is still on the list if netifs. If so,
// advance to the next nextif.
struct netif * prevNetif = mCurNetif;
#if LWIP_VERSION_MAJOR >= 2 && LWIP_VERSION_MINOR >= 0 && defined(NETIF_FOREACH)
#if defined(NETIF_FOREACH)
NETIF_FOREACH(mCurNetif)
#else
for (mCurNetif = netif_list; mCurNetif != NULL; mCurNetif = mCurNetif->next)
Expand Down Expand Up @@ -152,12 +152,7 @@ bool InterfaceIterator::IsUp()

bool InterfaceIterator::SupportsMulticast()
{
return HasCurrent() &&
#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
(mCurNetif->flags & (NETIF_FLAG_IGMP | NETIF_FLAG_MLD6 | NETIF_FLAG_BROADCAST)) != 0;
#else
(mCurNetif->flags & NETIF_FLAG_POINTTOPOINT) == 0;
#endif // LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
return HasCurrent() && (mCurNetif->flags & (NETIF_FLAG_IGMP | NETIF_FLAG_MLD6 | NETIF_FLAG_BROADCAST)) != 0;
}

bool InterfaceIterator::HasBroadcastAddress()
Expand Down
93 changes: 2 additions & 91 deletions src/inet/TCPEndPointImplLwIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ CHIP_ERROR TCPEndPointImplLwIP::BindImpl(IPAddressType addrType, const IPAddress
ip_set_option(mTCP, SOF_REUSEADDR);
}

#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5

ip_addr_t ipAddr;
if (addr != IPAddress::Any)
{
Expand All @@ -94,25 +92,6 @@ CHIP_ERROR TCPEndPointImplLwIP::BindImpl(IPAddressType addrType, const IPAddress
else
res = INET_ERROR_WRONG_ADDRESS_TYPE;
res = chip::System::MapErrorLwIP(tcp_bind(mTCP, &ipAddr, port));

#else // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5

if (addrType == IPAddressType::kIPv6)
{
ip6_addr_t ipv6Addr = addr.ToIPv6();
res = chip::System::MapErrorLwIP(tcp_bind_ip6(mTCP, &ipv6Addr, port));
}
#if INET_CONFIG_ENABLE_IPV4
else if (addrType == IPAddressType::kIPv4)
{
ip_addr_t ipv4Addr = addr.ToIPv4();
res = chip::System::MapErrorLwIP(tcp_bind(mTCP, &ipv4Addr, port));
}
#endif // INET_CONFIG_ENABLE_IPV4
else
res = INET_ERROR_WRONG_ADDRESS_TYPE;

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

// Unlock LwIP stack
Expand Down Expand Up @@ -168,25 +147,8 @@ CHIP_ERROR TCPEndPointImplLwIP::ConnectImpl(const IPAddress & addr, uint16_t por
tcp_arg(mTCP, this);
tcp_err(mTCP, LwIPHandleError);

#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
ip_addr_t lwipAddr = addr.ToLwIPAddr();
res = chip::System::MapErrorLwIP(tcp_connect(mTCP, &lwipAddr, port, LwIPHandleConnectComplete));
#else // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
if (addrType == IPAddressType::kIPv6)
{
ip6_addr_t lwipAddr = addr.ToIPv6();
res = chip::System::MapErrorLwIP(tcp_connect_ip6(mTCP, &lwipAddr, port, LwIPHandleConnectComplete));
}
#if INET_CONFIG_ENABLE_IPV4
else if (addrType == IPAddressType::kIPv4)
{
ip_addr_t lwipAddr = addr.ToIPv4();
res = chip::System::MapErrorLwIP(tcp_connect(mTCP, &lwipAddr, port, LwIPHandleConnectComplete));
}
#endif // INET_CONFIG_ENABLE_IPV4
else
res = INET_ERROR_WRONG_ADDRESS_TYPE;
#endif // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5

// Ensure that TCP timers are started
if (res == CHIP_NO_ERROR)
Expand Down Expand Up @@ -221,17 +183,8 @@ CHIP_ERROR TCPEndPointImplLwIP::GetPeerInfo(IPAddress * retAddr, uint16_t * retP
if (mTCP != nullptr)
{
*retPort = mTCP->remote_port;

#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
*retAddr = IPAddress(mTCP->remote_ip);
#else // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
#if INET_CONFIG_ENABLE_IPV4
*retAddr = PCB_ISIPV6(mTCP) ? IPAddress(mTCP->remote_ip.ip6) : IPAddress(mTCP->remote_ip.ip4);
#else // !INET_CONFIG_ENABLE_IPV4
*retAddr = IPAddress(mTCP->remote_ip.ip6);
#endif // !INET_CONFIG_ENABLE_IPV4
#endif // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
res = CHIP_NO_ERROR;
res = CHIP_NO_ERROR;
}

// Unlock LwIP stack
Expand All @@ -251,17 +204,8 @@ CHIP_ERROR TCPEndPointImplLwIP::GetLocalInfo(IPAddress * retAddr, uint16_t * ret
if (mTCP != nullptr)
{
*retPort = mTCP->local_port;

#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
*retAddr = IPAddress(mTCP->local_ip);
#else // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
#if INET_CONFIG_ENABLE_IPV4
*retAddr = PCB_ISIPV6(mTCP) ? IPAddress(mTCP->local_ip.ip6) : IPAddress(mTCP->local_ip.ip4);
#else // !INET_CONFIG_ENABLE_IPV4
*retAddr = IPAddress(mTCP->local_ip.ip6);
#endif // !INET_CONFIG_ENABLE_IPV4
#endif // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
res = CHIP_NO_ERROR;
res = CHIP_NO_ERROR;
}

// Unlock LwIP stack
Expand Down Expand Up @@ -656,8 +600,6 @@ TCPEndPointImplLwIP::BufferOffset TCPEndPointImplLwIP::FindStartOfUnsent()
CHIP_ERROR TCPEndPointImplLwIP::GetPCB(IPAddressType addrType)
{
// IMMPORTANT: This method MUST be called with the LwIP stack LOCKED!

#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
if (mTCP == NULL)
{
switch (addrType)
Expand Down Expand Up @@ -705,37 +647,6 @@ CHIP_ERROR TCPEndPointImplLwIP::GetPCB(IPAddressType addrType)
break;
}
}
#else // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5
if (mTCP == NULL)
{
if (addrType == IPAddressType::kIPv6)
mTCP = tcp_new_ip6();
#if INET_CONFIG_ENABLE_IPV4
else if (addrType == IPAddressType::kIPv4)
mTCP = tcp_new();
#endif // INET_CONFIG_ENABLE_IPV4
else
return INET_ERROR_WRONG_ADDRESS_TYPE;
if (mTCP == NULL)
{
return CHIP_ERROR_NO_MEMORY;
}
else
{
mLwIPEndPointType = LwIPEndPointType::TCP;
}
}
else
{
#if INET_CONFIG_ENABLE_IPV4
const IPAddressType pcbType = PCB_ISIPV6(mTCP) ? IPAddressType::kIPv6 : IPAddressType::kIPv4;
#else // !INET_CONFIG_ENABLE_IPV4
const IPAddressType pcbType = IPAddressType::kIPv6;
#endif // !INET_CONFIG_ENABLE_IPV4
if (addrType != pcbType)
return INET_ERROR_WRONG_ADDRESS_TYPE;
}
#endif // LWIP_VERSION_MAJOR <= 1 || LWIP_VERSION_MINOR >= 5

return CHIP_NO_ERROR;
}
Expand Down
Loading

0 comments on commit 1298261

Please sign in to comment.