Get host name From IP address iOS 10
Asked Answered
M

0

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);
  }
Melentha answered 18/4, 2017 at 5:8 Comment(9)
Where exactly do your functions fail?Bogan
@MartinR in first code snippet function fails in BOOL succeeded = CFHostStartInfoResolution(hostRef, kCFHostNames, NULL); if (!succeeded) { return nil; } this lineMelentha
@MartinR and the error that i get is CFStreamError) error = (domain = 12, error = 8)Melentha
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 <numericIPaddress> in the Terminal resolve to the host name?Bogan
Your second method works for me as well.Bogan
@MartinR but in my case same ip returns as hostname :(Melentha
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)?Rattlehead
@Melentha have you found any success .If yes, please suggest me some solution ,i am combatting same issue since few days.Austere
@Melentha did you find anything on this? I am also not getting hostname.Jalopy

© 2022 - 2024 — McMap. All rights reserved.