diff --git a/src/inet/InetInterface.cpp b/src/inet/InetInterface.cpp index f0a6310877d8cb..a13ff4e01e5ddb 100644 --- a/src/inet/InetInterface.cpp +++ b/src/inet/InetInterface.cpp @@ -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; @@ -881,9 +883,10 @@ CHIP_ERROR InterfaceId::GetLinkLocalAddr(IPAddress * llAddr) const ((mPlatformInterface == 0) || (mPlatformInterface == if_nametoindex(ifaddr_iter->ifa_name)))) { struct in6_addr * sin6_addr = &(reinterpret_cast(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(ifaddr_iter->ifa_addr))->sin6_addr); + found = true; break; } } @@ -891,7 +894,7 @@ CHIP_ERROR InterfaceId::GetLinkLocalAddr(IPAddress * llAddr) const } 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 diff --git a/src/inet/InetInterface.h b/src/inet/InetInterface.h index d87c1080b89e6c..f2e89fe4697919 100644 --- a/src/inet/InetInterface.h +++ b/src/inet/InetInterface.h @@ -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;