Note that there are some explanatory texts on larger screens.

plurals
  1. PODNS resolution with IPv6 on iOS only synchronous?
    primarykey
    data
    text
    <p><em>(This is a work in progress. I wonder if someone could to improve it)</em></p> <p>in Objective C, it's easy to resolve a hostname with NSHost.</p> <pre><code>[[NSHost hostWithName:@"www.google.com"] address] </code></pre> <p>Sadly iOS (iPhone) contains only a private version of NSHost.</p> <p>I found many ways of doing this with other Objects or methods, but all of them got only IPv4 addresses in the results. So here is for the moment the only efficient method I have found.</p> <p>I first tried to use the asynchronous CFHostStartInfoResolution as did <a href="http://www.bdunagan.com/2009/11/28/iphone-tip-no-nshost/" rel="nofollow">bdunagan</a>, but failed to adapt it to IPv6.</p> <p>Some of you will appreciate to get a method working, so here is one, but if you know a way which would be Asynchronous I would appreciate to learn about it... cause for the moment I use a Popup to alert about the next freeze that could occur with slow cellular connection</p> <pre><code>/** 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 (&amp;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, &amp;hints, &amp;res); if (retval == 0) { if (res-&gt;ai_canonname) { result = [NSMutableArray arrayWithObject:[NSString stringWithUTF8String:res-&gt;ai_canonname]]; } else { //it means the DNS didn't know this host return nil; } result4= [NSMutableArray array]; while (res) { switch (res-&gt;ai_family){ case AF_INET6: s6 = (struct sockaddr_in6 *)res-&gt;ai_addr; if(inet_ntop(res-&gt;ai_family, (void *)&amp;(s6-&gt;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-&gt;ai_addr; if(inet_ntop(res-&gt;ai_family, (void *)&amp;(s4-&gt;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-&gt;ai_next; } }else{ NSLog(@"no IP found"); return nil; } return [result arrayByAddingObjectsFromArray:result4]; } </code></pre> <p>NB: I noticed that most of the time only 1 IPv6 is returned, I suspect it's due to iOS Memory optimisation when locally cached. if you run this method again and again, sometime you have 3 IPv6, but then you have only 1 IPv4.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload