Skip to content

Commit b8ef924

Browse files
fenghuizhangcopybara-github
authored andcommitted
Support IPv6-only environments in GetLocalHostNicAddresses.
PiperOrigin-RevId: 938089407
1 parent d1c217a commit b8ef924

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

tpu_raiden/core/tpu_utils.cc

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,35 @@ std::vector<HostNicAddress> GetLocalHostNicAddresses() {
367367
nics.push_back({ifa->ifa_name, ip_str, node});
368368
}
369369
}
370+
} else if (ifa->ifa_addr->sa_family == AF_INET6) {
371+
if (std::strcmp(ifa->ifa_name, "lo") != 0) {
372+
char host[INET6_ADDRSTRLEN];
373+
auto* s_in6 = reinterpret_cast<struct sockaddr_in6*>(ifa->ifa_addr);
374+
if (IN6_IS_ADDR_LINKLOCAL(&s_in6->sin6_addr)) {
375+
continue;
376+
}
377+
inet_ntop(AF_INET6, &s_in6->sin6_addr, host, INET6_ADDRSTRLEN);
378+
std::string ip_str(host);
379+
auto it = std::find_if(
380+
nics.begin(), nics.end(),
381+
[&](const HostNicAddress& n) { return n.ip_address == ip_str; });
382+
if (it == nics.end()) {
383+
int node = GetInterfaceNumaNode(ifa->ifa_name);
384+
nics.push_back({ifa->ifa_name, ip_str, node});
385+
}
386+
}
370387
}
371388
}
372389
freeifaddrs(ifaddr);
373390
}
374391
if (nics.empty()) {
375-
nics.push_back({"lo", "127.0.0.1", -1});
392+
int fd = socket(AF_INET, SOCK_STREAM, 0);
393+
if (fd >= 0) {
394+
close(fd);
395+
nics.push_back({"lo", "127.0.0.1", -1});
396+
} else {
397+
nics.push_back({"lo", "::1", -1});
398+
}
376399
}
377400
return nics;
378401
}

0 commit comments

Comments
 (0)