The loop iterated over addr but always passed the list head *ifa
to matches_with_net, so it only ever tested the first interface
while returning the iterated interface's name. Match against
*addr.
Fixes: https://tracker.ceph.com/issues/77847
Signed-off-by: Sun Yuechi <sunyuechi@iscas.ac.cn>
auto free_ifa = make_scope_guard([ifa] { freeifaddrs(ifa); });
const unsigned int prefix_len = std::max(sizeof(in_addr::s_addr), sizeof(in6_addr::s6_addr)) * CHAR_BIT;
for (auto addr = ifa; addr != nullptr; addr = addr->ifa_next) {
- if (matches_with_net(*ifa, (const struct sockaddr *) &network, prefix_len,
+ if (matches_with_net(*addr, (const struct sockaddr *) &network, prefix_len,
CEPH_PICK_ADDRESS_IPV4 | CEPH_PICK_ADDRESS_IPV6)) {
return addr->ifa_name;
}