Skip to content

Commit

Permalink
Fix Get link local address for socket impl (#21316)
Browse files Browse the repository at this point in the history
  • Loading branch information
jepenven-silabs authored and pull[bot] committed Feb 1, 2024
1 parent b3fba08 commit 3552427
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/inet/InetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,8 @@ CHIP_ERROR InterfaceId::GetLinkLocalAddr(IPAddress * llAddr) const

struct ifaddrs * ifaddr;
const int rv = getifaddrs(&ifaddr);
bool found = false;

if (rv == -1)
{
return INET_ERROR_ADDRESS_NOT_FOUND;
Expand All @@ -881,17 +883,18 @@ CHIP_ERROR InterfaceId::GetLinkLocalAddr(IPAddress * llAddr) const
((mPlatformInterface == 0) || (mPlatformInterface == if_nametoindex(ifaddr_iter->ifa_name))))
{
struct in6_addr * sin6_addr = &(reinterpret_cast<struct sockaddr_in6 *>(ifaddr_iter->ifa_addr))->sin6_addr;
if (sin6_addr->s6_addr[0] == 0xfe && (sin6_addr->s6_addr[1] & 0xc0) == 0x80) // Link Local Address
if ((sin6_addr->s6_addr[0] == 0xfe) && ((sin6_addr->s6_addr[1] & 0xc0) == 0x80)) // Link Local Address
{
(*llAddr) = IPAddress((reinterpret_cast<struct sockaddr_in6 *>(ifaddr_iter->ifa_addr))->sin6_addr);
found = true;
break;
}
}
}
}
freeifaddrs(ifaddr);

return CHIP_NO_ERROR;
return (found) ? CHIP_NO_ERROR : INET_ERROR_ADDRESS_NOT_FOUND;
}

#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
Expand Down
4 changes: 3 additions & 1 deletion src/inet/InetInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ class InterfaceId
* @retval #CHIP_ERROR_INVALID_ARGUMENT If the link local address
* is nullptr.
* @retval #INET_ERROR_ADDRESS_NOT_FOUND If the link does not have
* any address configured.
* any address configured
* or if no link local (fe80::)
* address is present.
* @retval #CHIP_NO_ERROR On success.
*/
CHIP_ERROR GetLinkLocalAddr(IPAddress * llAddr) const;
Expand Down

0 comments on commit 3552427

Please sign in to comment.