Skip to content

Commit

Permalink
Revisions to Inet::IPAddress (#10791)
Browse files Browse the repository at this point in the history
* Revisions to Inet::IPAddress

#### Problem

`Inet::IPAddress` has some redundancies and outdated comments.

#### Change overview

- Convert `IPAddress::From…()` functions that are semantically
  constructors into constructors.
- Replace several identical `union`s with `IPAddress::SockAddr`.
- Fix outdated documentation regarding IPv4-in-IPv6.
- Fix a use of `IPAddress::kMaxStringLength` (from #10450 review).

#### Testing

CI; no changes to functionality intended.

* review
  • Loading branch information
kpschoedel authored Oct 22, 2021
1 parent b953fe7 commit 33ca522
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 263 deletions.
4 changes: 2 additions & 2 deletions src/inet/IPAddress-StringFuncts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ bool IPAddress::FromString(const char * str, IPAddress & output)
if (inet_pton(AF_INET, str, &ipv4Addr) < 1)
return false;
#endif // !CHIP_SYSTEM_CONFIG_USE_LWIP
output = FromIPv4(ipv4Addr);
output = IPAddress(ipv4Addr);
}
else
#endif // INET_CONFIG_ENABLE_IPV4
Expand All @@ -121,7 +121,7 @@ bool IPAddress::FromString(const char * str, IPAddress & output)
if (inet_pton(AF_INET6, str, &ipv6Addr) < 1)
return false;
#endif // !CHIP_SYSTEM_CONFIG_USE_LWIP
output = FromIPv6(ipv6Addr);
output = IPAddress(ipv6Addr);
}

return true;
Expand Down
142 changes: 67 additions & 75 deletions src/inet/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* related enumerated constants. The CHIP Inet Layer uses objects
* of this class to represent Internet protocol addresses of both
* IPv4 and IPv6 address families. (IPv4 addresses are stored
* internally in the V4COMPAT format, reserved for that purpose.)
* internally as IPv4-Mapped IPv6 addresses.)
*
*/

Expand Down Expand Up @@ -70,55 +70,76 @@ IPAddress & IPAddress::operator=(const IPAddress & other)

#if CHIP_SYSTEM_CONFIG_USE_LWIP

#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
ip_addr_t IPAddress::ToLwIPAddr(void) const
IPAddress::IPAddress(const ip6_addr_t & ipv6Addr)
{
ip_addr_t ret;
static_assert(sizeof(ip6_addr_t) == sizeof(Addr), "ip6_addr_t size mismatch");
memcpy(Addr, &ipv6Addr, sizeof(ipv6Addr));
}

switch (Type())
#if INET_CONFIG_ENABLE_IPV4

IPAddress::IPAddress(const ip4_addr_t & ipv4Addr)
{
Addr[0] = 0;
Addr[1] = 0;
Addr[2] = htonl(0xFFFF);
Addr[3] = ipv4Addr.addr;
}

IPAddress::IPAddress(const ip_addr_t & addr)
{
switch (IP_GET_TYPE(&addr))
{
#if INET_CONFIG_ENABLE_IPV4
case IPAddressType::kIPv4:
IP_SET_TYPE_VAL(ret, IPADDR_TYPE_V4);
*ip_2_ip4(&ret) = IPAddress::ToIPv4();
case IPADDR_TYPE_V4:
*this = IPAddress(*ip_2_ip4(&addr));
break;
#endif // INET_CONFIG_ENABLE_IPV4

case IPAddressType::kIPv6:
IP_SET_TYPE_VAL(ret, IPADDR_TYPE_V6);
*ip_2_ip6(&ret) = IPAddress::ToIPv6();
case IPADDR_TYPE_V6:
*this = IPAddress(*ip_2_ip6(&addr));
break;

default:
#if INET_CONFIG_ENABLE_IPV4
ret = *IP_ADDR_ANY;
#else
ret = *IP6_ADDR_ANY;
#endif
*this = Any;
break;
}
}

return ret;
ip4_addr_t IPAddress::ToIPv4() const
{
ip4_addr_t ipAddr;
memcpy(&ipAddr, &Addr[3], sizeof(ipAddr));
return ipAddr;
}

IPAddress IPAddress::FromLwIPAddr(const ip_addr_t & addr)
#endif // INET_CONFIG_ENABLE_IPV4

#if LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
ip_addr_t IPAddress::ToLwIPAddr(void) const
{
IPAddress ret;
ip_addr_t ret;

switch (IP_GET_TYPE(&addr))
switch (Type())
{
#if INET_CONFIG_ENABLE_IPV4
case IPADDR_TYPE_V4:
ret = IPAddress::FromIPv4(*ip_2_ip4(&addr));
case IPAddressType::kIPv4:
IP_SET_TYPE_VAL(ret, IPADDR_TYPE_V4);
*ip_2_ip4(&ret) = IPAddress::ToIPv4();
break;
#endif // INET_CONFIG_ENABLE_IPV4

case IPADDR_TYPE_V6:
ret = IPAddress::FromIPv6(*ip_2_ip6(&addr));
case IPAddressType::kIPv6:
IP_SET_TYPE_VAL(ret, IPADDR_TYPE_V6);
*ip_2_ip6(&ret) = IPAddress::ToIPv6();
break;

default:
ret = Any;
#if INET_CONFIG_ENABLE_IPV4
ret = *IP_ADDR_ANY;
#else
ret = *IP6_ADDR_ANY;
#endif
break;
}

Expand Down Expand Up @@ -150,25 +171,6 @@ lwip_ip_addr_type IPAddress::ToLwIPAddrType(IPAddressType typ)
}
#endif // LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5

#if INET_CONFIG_ENABLE_IPV4
ip4_addr_t IPAddress::ToIPv4() const
{
ip4_addr_t ipAddr;
memcpy(&ipAddr, &Addr[3], sizeof(ipAddr));
return ipAddr;
}

IPAddress IPAddress::FromIPv4(const ip4_addr_t & ipv4Addr)
{
IPAddress ipAddr;
ipAddr.Addr[0] = 0;
ipAddr.Addr[1] = 0;
ipAddr.Addr[2] = htonl(0xFFFF);
ipAddr.Addr[3] = ipv4Addr.addr;
return ipAddr;
}
#endif // INET_CONFIG_ENABLE_IPV4

ip6_addr_t IPAddress::ToIPv6() const
{
ip6_addr_t ipAddr;
Expand All @@ -177,35 +179,33 @@ ip6_addr_t IPAddress::ToIPv6() const
return ipAddr;
}

IPAddress IPAddress::FromIPv6(const ip6_addr_t & ipv6Addr)
{
IPAddress ipAddr;
static_assert(sizeof(ipAddr) == sizeof(Addr), "ip6_addr_t size mismatch");
memcpy(ipAddr.Addr, &ipv6Addr, sizeof(ipv6Addr));
return ipAddr;
}

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

#if INET_CONFIG_ENABLE_IPV4
IPAddress::IPAddress(const struct in_addr & ipv4Addr)
{
Addr[0] = 0;
Addr[1] = 0;
Addr[2] = htonl(0xFFFF);
Addr[3] = ipv4Addr.s_addr;
}
#endif // INET_CONFIG_ENABLE_IPV4

IPAddress::IPAddress(const struct in6_addr & ipv6Addr)
{
static_assert(sizeof(*this) == sizeof(ipv6Addr), "in6_addr size mismatch");
memcpy(Addr, &ipv6Addr, sizeof(ipv6Addr));
}

#if INET_CONFIG_ENABLE_IPV4
struct in_addr IPAddress::ToIPv4() const
{
struct in_addr ipv4Addr;
ipv4Addr.s_addr = Addr[3];
return ipv4Addr;
}

IPAddress IPAddress::FromIPv4(const struct in_addr & ipv4Addr)
{
IPAddress ipAddr;
ipAddr.Addr[0] = 0;
ipAddr.Addr[1] = 0;
ipAddr.Addr[2] = htonl(0xFFFF);
ipAddr.Addr[3] = ipv4Addr.s_addr;
return ipAddr;
}
#endif // INET_CONFIG_ENABLE_IPV4

struct in6_addr IPAddress::ToIPv6() const
Expand All @@ -216,22 +216,14 @@ struct in6_addr IPAddress::ToIPv6() const
return ipAddr;
}

IPAddress IPAddress::FromIPv6(const struct in6_addr & ipv6Addr)
{
IPAddress ipAddr;
static_assert(sizeof(ipAddr) == sizeof(ipv6Addr), "in6_addr size mismatch");
memcpy(ipAddr.Addr, &ipv6Addr, sizeof(ipv6Addr));
return ipAddr;
}

IPAddress IPAddress::FromSockAddr(const struct sockaddr & sockaddr)
IPAddress IPAddress::FromSockAddr(const SockAddr & sockaddr)
{
#if INET_CONFIG_ENABLE_IPV4
if (sockaddr.sa_family == AF_INET)
return FromIPv4(reinterpret_cast<const sockaddr_in *>(&sockaddr)->sin_addr);
if (sockaddr.any.sa_family == AF_INET)
return FromSockAddr(sockaddr.in);
#endif // INET_CONFIG_ENABLE_IPV4
if (sockaddr.sa_family == AF_INET6)
return FromIPv6(reinterpret_cast<const sockaddr_in6 *>(&sockaddr)->sin6_addr);
if (sockaddr.any.sa_family == AF_INET6)
return FromSockAddr(sockaddr.in6);
return Any;
}

Expand Down
Loading

0 comments on commit 33ca522

Please sign in to comment.