mirror of https://github.com/axmolengine/axmol.git
37 lines
1.1 KiB
Plaintext
37 lines
1.1 KiB
Plaintext
|
|
#include <ifaddrs.h>
|
|
#include <arpa/inet.h>
|
|
#include <net/if.h>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
using namespace std;
|
|
|
|
string getIPAddress()
|
|
{
|
|
BOOL success;
|
|
struct ifaddrs * addrs;
|
|
const struct ifaddrs * cursor;
|
|
|
|
success = getifaddrs(&addrs) == 0;
|
|
if (success) {
|
|
cursor = addrs;
|
|
while (cursor != NULL) {
|
|
// the second test keeps from picking up the loopback address
|
|
if (cursor->ifa_addr->sa_family == AF_INET && (cursor->ifa_flags & IFF_LOOPBACK) == 0)
|
|
{
|
|
NSString *name = [NSString stringWithUTF8String:cursor->ifa_name];
|
|
if ([name rangeOfString:@"en" options:NSCaseInsensitiveSearch].length > 0) {
|
|
string ipaddr = [[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)]UTF8String];
|
|
if (!ipaddr.empty()) {
|
|
return ipaddr;
|
|
}
|
|
}
|
|
}
|
|
cursor = cursor->ifa_next;
|
|
}
|
|
freeifaddrs(addrs);
|
|
}
|
|
return "";
|
|
}
|