4

I need to fetch host name from ip address. Am able to fetch ip, mac addresses of all devices connected to my devices network, but hostname always returns nil.

i had tried below code snippets to retrieve hostname but it always returns nil in my network

CODE SNIPPET 1

+(NSString *)getHostFromIPAddress:(NSString*)ipAddress {
struct addrinfo *result = NULL;
struct addrinfo hints;

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([ipAddress 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 succeeded = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL);
if (!succeeded) {
    return nil;
}

NSMutableArray *hostnames = [NSMutableArray array];

CFArrayRef hostnamesRef = CFHostGetNames(hostRef, NULL);
for (int currentIndex = 0; currentIndex < [(__bridge NSArray *)hostnamesRef count]; currentIndex++) {
    [hostnames addObject:[(__bridge NSArray *)hostnamesRef objectAtIndex:currentIndex]];
}

return hostnames[0];
} 

CODE SNIPPET 2

#pragma mark - Get Host from IP
+(NSString *)getHostFromIPAddress:(NSString*)ipAddress {
struct addrinfo *result = NULL;
struct addrinfo hints;

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 error;
struct addrinfo *results = NULL;

error = getaddrinfo([ipAddress cStringUsingEncoding:NSASCIIStringEncoding], NULL, NULL, &results);
if (error != 0)
{
    NSLog (@"Could not get any info for the address");
    return @""; // or exit(1);
}

for (struct addrinfo *r = results; r; r = r->ai_next)
{
    char hostname[NI_MAXHOST] = {0};
    error = getnameinfo(r->ai_addr, r->ai_addrlen, hostname, sizeof hostname, NULL, 0 , 0);
    if (error != 0)
    {
        continue; // try next one
    }
    else
    {
        return [NSString stringWithUTF8String:hostname];;
        NSLog (@"Found hostname: %s", hostname);
        break;
    }
}

freeaddrinfo(results);
  }
Vaisakh
  • 2,919
  • 4
  • 26
  • 44
  • Where exactly do your functions fail? – Martin R Apr 28 '17 at 05:30
  • @MartinR in first code snippet function fails in BOOL succeeded = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL); if (!succeeded) { return nil; } this line – Vaisakh Apr 28 '17 at 06:01
  • @MartinR and the error that i get is CFStreamError) error = (domain = 12, error = 8) – Vaisakh Apr 28 '17 at 06:19
  • Actually your first method works without problems for me. Are you sure that the numeric IP addresses are correct? Do you test it on an iOS device or in the simulator? Does name resolution work in your local network. Does `host ` in the Terminal resolve to the host name? – Martin R Apr 28 '17 at 06:55
  • Your second method works for me as well. – Martin R Apr 28 '17 at 06:58
  • @MartinR but in my case same ip returns as hostname :( – Vaisakh Apr 28 '17 at 08:08
  • It sounds like reverse DNS lookup isn't set up properly on your network. Is this a home/small office network (that you manage yourself), or a corporate network (managed by someone else)? – Jeff Loughlin May 01 '17 at 18:30
  • @Vaisakh have you found any success .If yes, please suggest me some solution ,i am combatting same issue since few days. – Mehsam Saeed Nov 26 '18 at 06:59
  • @Vaisakh did you find anything on this? I am also not getting hostname. – Bhavesh Lathigara Jun 26 '20 at 09:00

0 Answers0