mirror of https://github.com/kiwix/libkiwix.git
Merge branch 'master' of ssh://git.code.sf.net/p/kiwix/kiwix
This commit is contained in:
commit
c8c53f68c5
|
@ -67,16 +67,21 @@ std::map<std::string, std::string> kiwix::getNetworkInterfaces() {
|
||||||
struct ifreq *ifreq;
|
struct ifreq *ifreq;
|
||||||
ifreq = ifconf.ifc_req;
|
ifreq = ifconf.ifc_req;
|
||||||
for (i = 0; i < ifconf.ifc_len; ) {
|
for (i = 0; i < ifconf.ifc_len; ) {
|
||||||
/* Get the network interface name */
|
if (ifreq->ifr_addr.sa_family == AF_INET) {
|
||||||
std::string interfaceName = std::string(ifreq->ifr_name);
|
|
||||||
|
|
||||||
/* Get the network interface ip */
|
/* Get the network interface ip */
|
||||||
char host[128];
|
char host[128] = { 0 };
|
||||||
getnameinfo(&(ifreq->ifr_addr), sizeof ifreq->ifr_addr, host, sizeof host, 0, 0, NI_NUMERICHOST);
|
const int error = getnameinfo(&(ifreq->ifr_addr), sizeof ifreq->ifr_addr,
|
||||||
|
host, sizeof host,
|
||||||
|
0, 0, NI_NUMERICHOST);
|
||||||
|
if (!error) {
|
||||||
|
std::string interfaceName = std::string(ifreq->ifr_name);
|
||||||
std::string interfaceIp = std::string(host);
|
std::string interfaceIp = std::string(host);
|
||||||
|
|
||||||
/* Add to the map */
|
/* Add to the map */
|
||||||
interfaces.insert(std::pair<std::string, std::string>(interfaceName, interfaceIp));
|
interfaces.insert(std::pair<std::string, std::string>(interfaceName, interfaceIp));
|
||||||
|
} else {
|
||||||
|
perror("getnameinfo()");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* some systems have ifr_addr.sa_len and adjust the length that
|
/* some systems have ifr_addr.sa_len and adjust the length that
|
||||||
* way, but not mine. weird */
|
* way, but not mine. weird */
|
||||||
|
@ -96,14 +101,14 @@ std::string kiwix::getBestPublicIp() {
|
||||||
std::map<std::string, std::string> interfaces = kiwix::getNetworkInterfaces();
|
std::map<std::string, std::string> interfaces = kiwix::getNetworkInterfaces();
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
if (interfaces.find("eth0") != interfaces.end()) {
|
const char* const prioritizedNames[] =
|
||||||
return interfaces.find("eth0")->second;
|
{ "eth0", "eth1", "wlan0", "wlan1", "en0", "en1" };
|
||||||
} else if (interfaces.find("eth1") != interfaces.end()) {
|
const int count = (sizeof prioritizedNames) / (sizeof prioritizedNames[0]);
|
||||||
return interfaces.find("eth1")->second;
|
for (int i = 0; i < count; ++i) {
|
||||||
} else if (interfaces.find("wlan0") != interfaces.end()) {
|
std::map<std::string, std::string>::const_iterator it =
|
||||||
return interfaces.find("wlan0")->second;
|
interfaces.find(prioritizedNames[i]);
|
||||||
} else if (interfaces.find("wlan1") != interfaces.end()) {
|
if (it != interfaces.end())
|
||||||
return interfaces.find("wlan1")->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue