|
|
@ -124,7 +124,7 @@ namespace util |
|
|
|
|
|
|
|
|
|
|
|
void SetThreadName (const char *name) { |
|
|
|
void SetThreadName (const char *name) { |
|
|
|
#if defined(__APPLE__) |
|
|
|
#if defined(__APPLE__) |
|
|
|
pthread_setname_np(name); |
|
|
|
pthread_setname_np((char*)name); |
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) |
|
|
|
#elif defined(__FreeBSD__) || defined(__OpenBSD__) |
|
|
|
pthread_set_name_np(pthread_self(), name); |
|
|
|
pthread_set_name_np(pthread_self(), name); |
|
|
|
#elif defined(__NetBSD__) |
|
|
|
#elif defined(__NetBSD__) |
|
|
@ -384,30 +384,36 @@ namespace net |
|
|
|
return boost::asio::ip::address::from_string("127.0.0.1"); |
|
|
|
return boost::asio::ip::address::from_string("127.0.0.1"); |
|
|
|
#else |
|
|
|
#else |
|
|
|
int af = (ipv6 ? AF_INET6 : AF_INET); |
|
|
|
int af = (ipv6 ? AF_INET6 : AF_INET); |
|
|
|
ifaddrs *addrs, *cur = nullptr; |
|
|
|
ifaddrs *addrs; |
|
|
|
if(getifaddrs(&addrs) == 0) |
|
|
|
try |
|
|
|
{ |
|
|
|
{ |
|
|
|
// got ifaddrs
|
|
|
|
ifaddrs *addrs; |
|
|
|
cur = addrs; |
|
|
|
if (!getifaddrs(&addrs)) |
|
|
|
while(cur) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
std::string cur_ifname(cur->ifa_name); |
|
|
|
for (auto cur = addrs; cur; cur = cur->ifa_next) |
|
|
|
if (cur_ifname == ifname && cur->ifa_addr && cur->ifa_addr->sa_family == af) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
// match
|
|
|
|
std::string cur_ifname(cur->ifa_name); |
|
|
|
char addr[INET6_ADDRSTRLEN]; |
|
|
|
if (cur_ifname == ifname && cur->ifa_addr && cur->ifa_addr->sa_family == af) |
|
|
|
memset (addr, 0, INET6_ADDRSTRLEN); |
|
|
|
{ |
|
|
|
if(af == AF_INET) |
|
|
|
// match
|
|
|
|
inet_ntop(af, &((sockaddr_in *)cur->ifa_addr)->sin_addr, addr, INET6_ADDRSTRLEN); |
|
|
|
char addr[INET6_ADDRSTRLEN]; |
|
|
|
else |
|
|
|
memset (addr, 0, INET6_ADDRSTRLEN); |
|
|
|
inet_ntop(af, &((sockaddr_in6 *)cur->ifa_addr)->sin6_addr, addr, INET6_ADDRSTRLEN); |
|
|
|
if(af == AF_INET) |
|
|
|
freeifaddrs(addrs); |
|
|
|
inet_ntop(af, &((sockaddr_in *)cur->ifa_addr)->sin_addr, addr, INET6_ADDRSTRLEN); |
|
|
|
std::string cur_ifaddr(addr); |
|
|
|
else |
|
|
|
return boost::asio::ip::address::from_string(cur_ifaddr); |
|
|
|
inet_ntop(af, &((sockaddr_in6 *)cur->ifa_addr)->sin6_addr, addr, INET6_ADDRSTRLEN); |
|
|
|
|
|
|
|
freeifaddrs(addrs); |
|
|
|
|
|
|
|
std::string cur_ifaddr(addr); |
|
|
|
|
|
|
|
return boost::asio::ip::address::from_string(cur_ifaddr); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
cur = cur->ifa_next; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
catch (std::exception& ex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint(eLogError, "NetIface: Exception while searching address using ifaddr: ", ex); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(addrs) freeifaddrs(addrs); |
|
|
|
if(addrs) freeifaddrs(addrs); |
|
|
|
std::string fallback; |
|
|
|
std::string fallback; |
|
|
|
if(ipv6) |
|
|
|
if(ipv6) |
|
|
@ -483,27 +489,31 @@ namespace net |
|
|
|
FREE(pAddresses); |
|
|
|
FREE(pAddresses); |
|
|
|
return boost::asio::ip::address_v6 (); |
|
|
|
return boost::asio::ip::address_v6 (); |
|
|
|
#else |
|
|
|
#else |
|
|
|
ifaddrs *addrs, *cur = nullptr; |
|
|
|
ifaddrs *addrs; |
|
|
|
auto err = getifaddrs(&addrs); |
|
|
|
try |
|
|
|
if (!err) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
cur = addrs; |
|
|
|
if (!getifaddrs(&addrs)) |
|
|
|
while(cur) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
if (cur->ifa_addr && cur->ifa_addr->sa_family == AF_INET6) |
|
|
|
for (auto cur = addrs; cur; cur = cur->ifa_next) |
|
|
|
{ |
|
|
|
{ |
|
|
|
sockaddr_in6* sa = (sockaddr_in6*)cur->ifa_addr; |
|
|
|
if (cur->ifa_addr && cur->ifa_addr->sa_family == AF_INET6) |
|
|
|
if (IsYggdrasilAddress(sa->sin6_addr.s6_addr)) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
boost::asio::ip::address_v6::bytes_type bytes; |
|
|
|
sockaddr_in6* sa = (sockaddr_in6*)cur->ifa_addr; |
|
|
|
memcpy (bytes.data (), &sa->sin6_addr, 16); |
|
|
|
if (IsYggdrasilAddress(sa->sin6_addr.s6_addr)) |
|
|
|
freeifaddrs(addrs); |
|
|
|
{ |
|
|
|
return boost::asio::ip::address_v6 (bytes); |
|
|
|
boost::asio::ip::address_v6::bytes_type bytes; |
|
|
|
|
|
|
|
memcpy (bytes.data (), &sa->sin6_addr, 16); |
|
|
|
|
|
|
|
freeifaddrs(addrs); |
|
|
|
|
|
|
|
return boost::asio::ip::address_v6 (bytes); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
cur = cur->ifa_next; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
catch (std::exception& ex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint(eLogError, "NetIface: Exception while searching Yggdrasill address using ifaddr: ", ex); |
|
|
|
|
|
|
|
} |
|
|
|
LogPrint(eLogWarning, "NetIface: Interface with Yggdrasil network address not found"); |
|
|
|
LogPrint(eLogWarning, "NetIface: Interface with Yggdrasil network address not found"); |
|
|
|
if(addrs) freeifaddrs(addrs); |
|
|
|
if(addrs) freeifaddrs(addrs); |
|
|
|
return boost::asio::ip::address_v6 (); |
|
|
|
return boost::asio::ip::address_v6 (); |
|
|
|