gpt4 book ai didi

iphone - iOS 上 IPv6 的 DNS 解析仅同步?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:31 25 4
gpt4 key购买 nike

(这是一项正在进行的工作。我想知道是否有人可以改进它)

在 Objective C 中,使用 NSHost 很容易解析主机名。

[[NSHost hostWithName:@"www.google.com"] address]

遗憾的是 iOS (iPhone) 只包含 NSHost 的私有(private)版本。

我发现有很多方法可以使用其他对象或方法来执行此操作,但所有这些方法在结果中都只获得了 IPv4 地址。因此,这是目前我发现的唯一有效方法。

我首先尝试使用异步 CFHostStartInfoResolution,就像 bdunagan 一样, 但无法适配 IPv6。

你们中的一些人会很高兴让一种方法起作用,所以这里有一个,但是如果你知道一种异步的方法,我会很高兴了解它......因为目前我使用弹出窗口来提醒慢速蜂窝连接可能发生的下一次卡住

/**
Give the IPs corresponding to a Hostname

Sometime only 1 IPv4 is shown even if there's more.
Sometime only 1 IPv6 is shown even if there's more.
Certainly due to iOS Memory optimisation when locally cached

@author Christian Gonzalvez, http://wiki.gonzofamily.com
@param hostName A hostname
@return an Array of NSString of all the corresponding IP addresses. The first
is the Canonical name, the following are IPs (all NSString)
*/
+ (NSArray *)addressesForHostname:(NSString *)hostname
{
const char* hostnameC = [hostname UTF8String];

struct addrinfo hints, *res;
struct sockaddr_in *s4;
struct sockaddr_in6 *s6;
int retval;
char buf[64];
NSMutableArray *result; //the array which will be return
NSMutableArray *result4; //the array of IPv4, to order them at the end
NSString *previousIP = nil;

memset (&hints, 0, sizeof (struct addrinfo));
hints.ai_family = PF_UNSPEC;//AF_INET6;
hints.ai_flags = AI_CANONNAME;
//AI_ADDRCONFIG, AI_ALL, AI_CANONNAME, AI_NUMERICHOST
//AI_NUMERICSERV, AI_PASSIVE, OR AI_V4MAPPED

retval = getaddrinfo(hostnameC, NULL, &hints, &res);
if (retval == 0)
{

if (res->ai_canonname)
{
result = [NSMutableArray arrayWithObject:[NSString stringWithUTF8String:res->ai_canonname]];
}
else
{
//it means the DNS didn't know this host
return nil;
}
result4= [NSMutableArray array];
while (res) {
switch (res->ai_family){
case AF_INET6:
s6 = (struct sockaddr_in6 *)res->ai_addr;
if(inet_ntop(res->ai_family, (void *)&(s6->sin6_addr), buf, sizeof(buf))
== NULL)
{
NSLog(@"inet_ntop failed for v6!\n");
}
else
{
//surprisingly every address is in double, let's add this test
if (![previousIP isEqualToString:[NSString stringWithUTF8String:buf]]) {
[result addObject:[NSString stringWithUTF8String:buf]];
}
}
break;

case AF_INET:
s4 = (struct sockaddr_in *)res->ai_addr;
if(inet_ntop(res->ai_family, (void *)&(s4->sin_addr), buf, sizeof(buf))
== NULL)
{
NSLog(@"inet_ntop failed for v4!\n");
}
else
{
//surprisingly every address is in double, let's add this test
if (![previousIP isEqualToString:[NSString stringWithUTF8String:buf]]) {
[result4 addObject:[NSString stringWithUTF8String:buf]];
}
}
break;
default:
NSLog(@"Neither IPv4 nor IPv6!");

}
//surprisingly every address is in double, let's add this test
previousIP = [NSString stringWithUTF8String:buf];

res = res->ai_next;
}
}else{
NSLog(@"no IP found");
return nil;
}

return [result arrayByAddingObjectsFromArray:result4];
}

注意:我注意到大多数时候只返回 1 个 IPv6,我怀疑这是由于本地缓存时 iOS 内存优化所致。如果你一次又一次地运行这个方法,有时你有 3 个 IPv6,但后来你只有 1 个 IPv4。

最佳答案

如果你想让一个方法在后台线程上运行,最简单的方法是使用performSelectorInBackground:withObject: ;这是 NSObject 的一个实例方法,所以任何对象都可以使用它而无需任何额外的工作(包括,足够有趣的是,class 对象,这在这种情况下很好,因为这是一个类方法):

[[self class] performSelectorInBackground:@selector(addressesForHostName:) 
withObject:theHostName];

在方法内部,您需要为线程设置一个自动释放池。您还需要设置某种回调方法以将返回值返回到主线程。确保您没有尝试在后台线程上执行任何 GUI 事件。只有在主线程上这样做才是安全的。

+ (NSArray *)addressesForHostname:(NSString *)hostname
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// Do your stuff...

// Wait until done to allow memory to be managed properly
// If we did not do this, the array might be deallocated
// before the main thread had a chance to retain it
[self performSelectorOnMainThread:@selector(addressesCallback:)
withObject:[result arrayByAddingObjectsFromArray:result4]
waitUntilDone:YES];
// Inside a class method, self refers to the class object.

[pool drain];
}

如果您一开始不在主线程上,或者如果您需要更多控制权,您还可以查看 NSOperation ,它更强大,因此需要更多的工作。不过,它仍然比显式线程管理更容易!

希望能解决您的问题。听起来这个方法可以满足您的需求,您只需要它不阻塞主线程即可。

关于iphone - iOS 上 IPv6 的 DNS 解析仅同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6068037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com