axmol/templates/lua-template-runtime/frameworks/runtime-src/proj.ios_mac/Runtime_ios-mac.mm

33 lines
936 B
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 isEqualToString:@"en0"]) // Wi-Fi adapter
return [[NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)cursor->ifa_addr)->sin_addr)]UTF8String];
}
cursor = cursor->ifa_next;
}
freeifaddrs(addrs);
}
return "";
}