1

I have found this nice piece of code to get a hostname from a IP Address. The problem is that it always fail both on simulator and on my 3GS.

See below the code, the error is commented within:

+ (NSArray *)hostnamesForAddress:(NSString *)address {
    // Get the host reference for the given address.
    CFStreamError streamError;
    struct addrinfo      hints;
    struct addrinfo      *result = NULL;
    memset(&hints, 0, sizeof(hints));
    hints.ai_flags    = AI_NUMERICHOST;
    hints.ai_family   = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
    int errorStatus = getaddrinfo([address cStringUsingEncoding:NSASCIIStringEncoding], NULL, &hints, &result);
    if (errorStatus != 0) return nil;
    CFDataRef addressRef = CFDataCreate(NULL, (UInt8 *)result->ai_addr, result->ai_addrlen);
    if (addressRef == nil) return nil;
    freeaddrinfo(result);
    CFHostRef hostRef = CFHostCreateWithAddress(kCFAllocatorDefault, addressRef);
    if (hostRef == nil) return nil;
    CFRelease(addressRef);
    BOOL isSuccess = CFHostStartInfoResolution(hostRef, kCFHostNames, &streamError);
    // always false
    if (!isSuccess){
        NSLog(@"error:%@",[self convertCFStreamErrorIntoNSError:streamError]);
        // error:Error Domain=kCFErrorDomainCFNetwork Code=2 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error 2.)" UserInfo=0x1cd29190 {kCFGetAddrInfoFailureKey=8}
        return nil;
    }

    // Get the hostnames for the host reference.
    CFArrayRef hostnamesRef = CFHostGetNames(hostRef, NULL);
    NSMutableArray *hostnames = [NSMutableArray array];
    for (int currentIndex = 0; currentIndex < [(__bridge NSArray *)hostnamesRef count]; currentIndex++) {
        [hostnames addObject:[(__bridge NSArray *)hostnamesRef objectAtIndex:currentIndex]];
    }

    return hostnames;
}

Could someone say me what is wrong ?

neilco
  • 7,964
  • 2
  • 36
  • 41
Kamax
  • 212
  • 2
  • 14

1 Answers1

0

This sounds like a DNS lookup failure. The key part of the error message is:

{kCFGetAddrInfoFailureKey=8}

From the CFNetwork Error Codes Reference for kCFGetAddrInfoFailureKey:

If an error of type kCFHostErrorUnknown is returned, this key returns the last error code returned by getaddrinfo in response to a DNS lookup. To interpret the results, look up the error code in /usr/include/netdb.h.

From /usr/include/netdb.h:

#define EAI_NONAME  8  /* hostname nor servname provided, or not known */

Try using a different DNS server.

neilco
  • 7,964
  • 2
  • 36
  • 41
  • I have forget to say that i use this code to scan my local network. I don't specially have a line to say which DNS to use. How could this be done on Iphone/simulator ? – Kamax Oct 17 '13 at 05:43
  • Did u reslove the issue? @Kamax I am getting the same error – Alejandro Vargas Nov 25 '14 at 12:25
  • Typical consumer routers found at home doesn't come with build-in DNS server. They rely on services delivered by ISP or further, which has no knowledge about your network. First you will need to install one ([PiHole](https://pi-hole.net) for example), then assign domain names for selected local IPs, then finally your code will start to work and will receive those values. – Pawel Hofman Feb 15 '21 at 12:47