11

I'm looking for a way to resolve the hostname of a device in my LAN from its ip address on this LAN.

I wrote a program in C which works perfectly on Linux using gethostbyaddr() function.

When I tried that on OS X or iOS it doesn't work.

It seems that there is a problem with gethostbyaddr() in OS X and iOS.

Anyway, if you have another idea to get hostname of remote machine from it's IP in iOS, it'll make my day.

This is the code I used:

First test:

192.168.0.101 is the ip address of the machine that we are querying for hostname.

#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

struct hostent *he;
struct in_addr ipv4addr;
inet_pton(AF_INET, "192.168.0.101", &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
printf("Host name: %s\n", he->h_name);

This code works well on linux, but it doesn't on OS X nor iOS.

Second test:

+ (NSArray *)hostnamesForAddress:(NSString *)address {
    // Get the host reference for the given address.
    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, NULL);
    if (!isSuccess) return nil;

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

    return hostnames;
}

This code stucks at CFHostStartInfoResolution returning nil at this point.

Thx in advance.

taredhot
  • 111
  • 1
  • 4
  • I think you want the CFHost API, check out [this](http://blog.toshsoft.de/index.php?/archives/5-3-Ways-to-Resolve-a-Hostname-in-iOS.html) – Jonathan Nov 30 '12 at 16:39
  • I tried with CFHost API, same problem when I try to retrive the hostname from an existing ip address on the network, the request timed out. But when I try to resolve 127.0.0.1 which is the localhost addres, it works. – taredhot Nov 30 '12 at 23:12
  • did you resolve the issue? I am getting the same nil value @taredhot – Alejandro Vargas Nov 25 '14 at 12:23
  • Did you find a solution for that? – BlackM Aug 06 '16 at 09:46

1 Answers1

2

It's actually a one-liner. [[NSHost hostWithAddress:@"173.194.34.24"] name]

Ecco
  • 1,323
  • 1
  • 11
  • 15
  • 1
    And actually there's no reason `gethostbyaddr` doesn't work on OS X / iOS. Either your DNS server is wrong, or your code is, but theoretically it should work. What does `host 192.168.0.101` say? – Ecco Dec 08 '12 at 23:00
  • To my experience and I've tried this with 3 active devices of my network, it isn't. I believe `NSHost hostWithAddress:` is only for global IPs, not locals. I've been trying to do what OP wants for hours. Also, the result of `gethostbyaddr` is NULL, so the printf() OP uses at the end, ends up in a crash. Do you know any other way? – isklikas Jul 02 '15 at 23:11
  • decrease 1 for post OSX platform instead of IOS – famfamfam Oct 18 '18 at 13:31