Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revisions to Inet::IPAddress #10791

Merged
merged 2 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(*this) == sizeof(Addr), "ip6_addr_t size mismatch");
kpschoedel marked this conversation as resolved.
Show resolved Hide resolved
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;
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
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;
msandstedt marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
Loading