开发者

iPhone DNS resolution, not all A records returned

I have the following code that should retrive all DNS A records for a URL. When I call this code from the UI, it works fine and returns all the A records, but, when this code is called from some other async thread it always returns only one IP (one A record), Any ideas why?

this is the code:

Boolean result;
CFHostRef hostRef = NULL;
CFArrayRef addresses = NULL;
NSString *hostname = [NSString stringWithCString:server];
hostRef = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)hostname);
if (hostRef) {  
result = CFHostStartInfoResolution(hostRef, kCFHostAddresses, NULL); // pass an error instead of NULL here to find out why it failed
        if (result == TRUE) {
            addresses = CFHostGetAddressing(hostRef, &result);
        }

    }
    if (result == TRUE) 
    {
        NSLog(@"ResolveDNS - Resolved");


        for (CFIndex count = 0; count < CFArrayGetCount(addresses); count++)
        {
            char addr[256];
            struct sockaddr_in *sa = (struct sockaddr_in *)
            CFDataGetBytePtr((CFDataRef)CFArrayGetValueAtIndex(addresses, count));
            // inet_ntop will correctly display both IPv4 and IPv6 addresses
            if (inet_ntop(sa->sin_family, &sa->sin_addr, addr, sizeof(addr)))
                printf("%s:%d \n", 开发者_如何学JAVAaddr, ntohs(sa->sin_port));

            [ipTmpArray addObject:[NSString stringWithCString:addr]];
        }


the same occurs on all my iOS devices (all on 4.2 or 4.3), but not on my Simulator. And if I launch again the same method I sometime get more IP, sometime even all of them.

I suspect it's due to iOS, some kind of Memory optimisation when they record the previous resolutions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜