Commit f4fa23d1 authored by Dominik Charousset's avatar Dominik Charousset

Filter loopback devices more efficiently

parent e46bb7fc
...@@ -170,7 +170,6 @@ void add_addr(if_device_ptr ptr, std::vector<std::string>& res) { ...@@ -170,7 +170,6 @@ void add_addr(if_device_ptr ptr, std::vector<std::string>& res) {
template <class F> template <class F>
void for_each_device(bool include_localhost, F fun) { void for_each_device(bool include_localhost, F fun) {
char host[NI_MAXHOST];
if_device_ptr tmp = nullptr; if_device_ptr tmp = nullptr;
if (getifaddrs(&tmp) != 0) { if (getifaddrs(&tmp) != 0) {
perror("getifaddrs"); perror("getifaddrs");
...@@ -182,11 +181,8 @@ void for_each_device(bool include_localhost, F fun) { ...@@ -182,11 +181,8 @@ void for_each_device(bool include_localhost, F fun) {
if (include_localhost) { if (include_localhost) {
fun(i); fun(i);
} else if (family == AF_INET || family == AF_INET6) { } else if (family == AF_INET || family == AF_INET6) {
auto len = static_cast<socklen_t>(family == AF_INET // filter loopback devices
? sizeof(sockaddr_in) if ((i->ifa_flags & IFF_LOOPBACK) == 0) {
: sizeof(sockaddr_in6));
auto ok = getnameinfo(i->ifa_addr, len, host, NI_MAXHOST, nullptr, 0, 0);
if (ok == 0 && strcmp("localhost", host) != 0) {
fun(i); fun(i);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment