Code snippet below:
NSString *host = @"time.google.com";
CFHostRef cfHostRef = CFHostCreateWithName(nil, (__bridge CFStringRef) host);
Boolean didLookup = CFHostStartInfoResolution(cfHostRef, kCFHostAddresses, nil);
NSLog(@"didLookup=%d", didLookup);
CFArrayRef addresses = CFHostGetAddressing(cfHostRef, &didLookup);
struct sockaddr *remoteAddr;
long count = CFArrayGetCount(addresses);
NSLog(@"count=%ld (this should include both ip4 and ip6)", count);
for(int i = 0; i < count; i++) {
CFDataRef saData = (CFDataRef)CFArrayGetValueAtIndex(addresses, i);
remoteAddr = (struct sockaddr*)CFDataGetBytePtr(saData);
NSLog(@"family=%d (AF_INET=%d, AF_INET6=%d)",remoteAddr->sa_family, AF_INET, AF_INET6);
NSString* addrPretty = nil;
switch (remoteAddr->sa_family) {
case AF_INET: {
char dest[INET_ADDRSTRLEN];
struct sockaddr_in *ip4 = (struct sockaddr_in *) remoteAddr;
addrPretty = [NSString stringWithFormat: @"%s", inet_ntop(AF_INET, &ip4->sin_addr, dest, INET_ADDRSTRLEN)];
break;
}
case AF_INET6: {
char dest[INET6_ADDRSTRLEN];
struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *) remoteAddr;
addrPretty = [NSString stringWithFormat: @"%s", inet_ntop(AF_INET6, &ip6->sin6_addr, dest, INET6_ADDRSTRLEN)];
break;
}
default:
break;
}
NSLog(@"addrPretty=%@", addrPretty);
}
As far as I understand this should print out both IPv4 and IPv6 addresses, but it only does the former. This is as tested on both a simulator and a real device, on different networks.
Note that I can traceroute6 -I time.google.com and see IPv6 addresses just fine, and I can also do set q=AAAA in the nslookup prompt and get the expected addresses when performing the query for time.google.com in the same prompt.