在 Obj 中查找 IPV6 地址。 C iphone
Finding IPV6 address in Obj. C iphone
我目前正在编写一个 unity 插件,允许我使用 Game Center 匹配和 Mirror。
为此,我需要找到主机的 IPV6 以将其发送给其他玩家,但是,我无法在 obj C 中找到执行此操作的简单方法。
在升级我的 IPV6 代码之前,这是我用来获取 IPV4 的代码:
+ (NSString *)GetInternalIpAddress
{
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
任何帮助将不胜感激!!!
这是我最终使用的:
+(NSString *)GetIP
{
struct ifaddrs *ifa, *ifa_tmp;
char addr[INET6_ADDRSTRLEN];
if (getifaddrs(&ifa) == -1) {
perror("getifaddrs failed");
exit(1);
}
ifa_tmp = ifa;
while (ifa_tmp) {
if ((ifa_tmp->ifa_addr) && ((ifa_tmp->ifa_addr->sa_family == AF_INET) ||
(ifa_tmp->ifa_addr->sa_family == AF_INET6))) {
if (ifa_tmp->ifa_addr->sa_family == AF_INET) {
// create IPv4 string
struct sockaddr_in *in = (struct sockaddr_in*) ifa_tmp->ifa_addr;
inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
} else { // AF_INET6
// create IPv6 string
struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa_tmp->ifa_addr;
inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
if([[NSString stringWithUTF8String:ifa_tmp->ifa_name] isEqual: @"en0"] && ![[NSString stringWithUTF8String:addr] containsString:@"::"])
{
printf("name = %s\n", ifa_tmp->ifa_name);
printf("addr = %s\n", addr);
return([NSString stringWithUTF8String:addr]);
}
}
}
ifa_tmp = ifa_tmp->ifa_next;
}
freeifaddrs(ifa);
return nil;
}
我目前正在编写一个 unity 插件,允许我使用 Game Center 匹配和 Mirror。
为此,我需要找到主机的 IPV6 以将其发送给其他玩家,但是,我无法在 obj C 中找到执行此操作的简单方法。
在升级我的 IPV6 代码之前,这是我用来获取 IPV4 的代码:
+ (NSString *)GetInternalIpAddress
{
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
任何帮助将不胜感激!!!
这是我最终使用的:
+(NSString *)GetIP
{
struct ifaddrs *ifa, *ifa_tmp;
char addr[INET6_ADDRSTRLEN];
if (getifaddrs(&ifa) == -1) {
perror("getifaddrs failed");
exit(1);
}
ifa_tmp = ifa;
while (ifa_tmp) {
if ((ifa_tmp->ifa_addr) && ((ifa_tmp->ifa_addr->sa_family == AF_INET) ||
(ifa_tmp->ifa_addr->sa_family == AF_INET6))) {
if (ifa_tmp->ifa_addr->sa_family == AF_INET) {
// create IPv4 string
struct sockaddr_in *in = (struct sockaddr_in*) ifa_tmp->ifa_addr;
inet_ntop(AF_INET, &in->sin_addr, addr, sizeof(addr));
} else { // AF_INET6
// create IPv6 string
struct sockaddr_in6 *in6 = (struct sockaddr_in6*) ifa_tmp->ifa_addr;
inet_ntop(AF_INET6, &in6->sin6_addr, addr, sizeof(addr));
if([[NSString stringWithUTF8String:ifa_tmp->ifa_name] isEqual: @"en0"] && ![[NSString stringWithUTF8String:addr] containsString:@"::"])
{
printf("name = %s\n", ifa_tmp->ifa_name);
printf("addr = %s\n", addr);
return([NSString stringWithUTF8String:addr]);
}
}
}
ifa_tmp = ifa_tmp->ifa_next;
}
freeifaddrs(ifa);
return nil;
}