Search code examples
c++networkingdnsreverse-dns

C++ reverse dns lookup (on local network)


I'm currently working on a "list what's up in the local network"-tool and now i'm stuck at the "gimme a hostname for this IP"-part.

I already tried getnameinfo and gethostbyaddr, but the first one returns (almost) always the given IP instead of a hostname, the second one returns some strange stuff like ANantes-651-1-49-net.w2-0.abo.wanadoo.fr...

My question is: How could i make this work?

Note: It's not like there is no hostname associated with at least one of the IPs, for example dig +short -x 192.168.178.1 return fritz.box. as it should and also host 192.168.178.1 gives the correct answer 1.178.168.192.in-addr.arpa domain name pointer fritz.box.

Additional note: On my research i stumbled upon a library, called "adns", but by "stumbled upon" i mean "almost literally stumbled upon", as there's not the slightest trace of a documentation, examples or such... So if someone actually has an example on how to use "adns", that would be great...


Solution

  • Oookay... Seems, like i screwed up several things in an almost "monkey wanna banana"-style...

    No.1:

    I used this:

    ...
    sockaddr_in address;
    memset(&address, 0, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("192.168.178.1");
    struct hostent *h =gethostbyaddr((const void *)&address, sizeof(address), AF_INET);
    ...
    

    Instead of the correct:

    ...
    in_addr address;
    inet_aton("192.168.178.1",&address);
    struct hostent *h = gethostbyaddr((const void *)&address, sizeof(address), AF_INET);
    ...
    

    No.2:

    I had a line where i checked, if the returned hostname was empty to replace it with a placeholder, depending on the selected "report format" (plain text/xml/csv). But for whatever reason, i wrote it in a way, that it ALWAYS replaced the hostname with the said placeholder (dang! it was kinda obvious, but i didn't see it the whole time...).

    BUT FINALLY: YAY! It works...