Vijay Yadav
Vijay Yadav

Reputation: 511

how to resolve the hostname of a device in my LAN from its ip address on this LAN?

I have to resolve the hostname of a device in my LAN from its ip address on this LAN. I have some code that works for external ip address but not the internally connected devices .

Below i have attached the code .

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

  int error;
struct addrinfo *results = NULL;

error = getaddrinfo("173.194.34.24", NULL, NULL, &results);
if (error != 0)
{
    NSLog (@"Could not get any info for the address");

}

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
    {
        NSLog (@"Found hostname: %s", hostname);
        break;
    }
}


freeaddrinfo(results);

or with NSHost

NSLog(@"%@ \n%@",[NSHost currentHost],[[NSHost hostWithAddress:@"172.17.241.61"] names]);

Upvotes: 2

Views: 660

Answers (1)

Tapani
Tapani

Reputation: 3231

Only way to make a DNS lookup directly to a specific DNS is to implement the protocol yourself or use some library.

https://serverfault.com/questions/173187/what-does-a-dns-request-look-like

https://www.ietf.org/rfc/rfc1035.txt

https://github.com/adeboer/rfc1035lib

Upvotes: 1

Related Questions