2011-05-20 313 views
2

(这是一项正在进行的工作。我不知道是否有人能对其进行改进)的目标C在iOS上使用IPv6的DNS解析仅同步?

,可以很容易地解决与NSHost主机名。

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

很遗憾,iOS(iPhone)仅包含一个私有版本的NSHost。

我发现有很多方法可以与其他对象或方法做到这一点,但它们都只在结果中获得IPv4地址。所以这里是目前唯一有效的方法。

我第一次尝试使用异步CFHostStartInfoResolution与bdunagan一样,但未能使其适应IPv6。

你们中的一些人会欣赏得到一个方法的工作,所以这里是一个,但如果你知道一种方法,这将是异步的,我会很高兴了解它...因为我现在使用Popup来提醒关于可能缓慢蜂窝连接发生

/** 
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]; 
} 

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

+0

...为什么你不能只在后台线程上执行该选择器?也许我有点密... – lxt 2011-05-20 07:20:29

+0

我开始阅读关于线程的Apple文档,它们(没有)密集。还没有找到一个简单的例子。你知道线程吗?我开始认为这是不值得的复杂的文件所需要的时间。如果你认为这是解决方案,我会做,并纠正这篇文章。 – chriscatfr 2011-05-20 11:55:10

回答

1

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

[[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,这是更强大,因此需要更多的工作。不过,它仍然比显式线程管理更简单!

希望能够解决您的问题。这听起来像你有这种方法做你所需要的,你只需要它不会阻塞主线程。

+0

完美的谢谢你,我正在阅读文档中的十几页,你的简单例子是一开始就更清晰,我会看看NSOperation。一旦我做到了,我编辑这个问题。 – chriscatfr 2011-05-21 05:20:52

+0

很高兴能帮到你!你应该保持原样。 StackOverflow格式用于发布问题以保留单个特定问题并回答帖子以包含解决方案。只有在需要澄清或添加细节时才编辑您的问题,以便得到更好的答案。如果你想_me_特别澄清或扩大我的答案的某些元素,请在评论中提问。 – 2011-05-21 05:42:20

1

感谢约什 - 我能做到这一点,但这里是我必须做的:

不是直接调用

self.ipAddressesString = [CJGIpAddress addressesForHostname:@"www.google.com"]; 

我打电话

[self resolveNow:@"www.google.com"]; 

,创造3种新方法:

- (void)resolveNow:(NSString *)hostname 
{ 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 
    [self performSelectorInBackground:@selector(hostname2ipAddresses:) 
            withObject:hostname]; 
} 

- (void)hostname2ipAddresses:(NSString *)hostname 
{ 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
     //Here is my previous lonely line !! safely started in an other thread 
    self.ipAddressesString = [CJGIpAddress addressesForHostname:hostname]; 
    [self performSelectorOnMainThread:@selector(resolutionDidFinish) 
          withObject:nil 
         waitUntilDone:YES]; 
    [pool drain]; 
} 

- (void)resolutionDidFinish 
{ 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
    //My STUFF with self.ipAddressesString (now filled) 
} 

编辑: 在实践中,我在模型中使用所有这些,所以当我在分辨率结束前关闭视图时崩溃了

因此,在视图中,我在dealloc中添加了避免碰撞

- (void)dealloc 
{ 
    self.model.delegate = nil; 
    [super dealloc]; 
} 

然后 - 在模型中 - 我在做任何事情之前测试委托。