0

I have problem. I want to convert IP address (81.2.195.254) to hostname (www.farnost-hranice.cz).
Here you can try to convert this IP address to see, that it is right:
https://whatismyipaddress.com/hostname-ip
My problem is, that when I try to convert IP address to hostname, it gives me strange (and even not accessible) hostname:
254.195.forpsi.net

What I am doing wrong?

My code is here:

#include <stdio.h>  //scanf , printf
#include <string.h> //strtok
#include <stdlib.h> //realloc
#include <sys/socket.h> //socket
#include <netinet/in.h> //sockaddr_in
#include <arpa/inet.h>  //getsockname
#include <netdb.h>  //hostent
#include <unistd.h> //close
#include <getopt.h> //getopt

int main(void)
{


    struct sockaddr_in sa; // could be IPv4 if you want
        char host[1024];

        sa.sin_family = AF_INET;
        sa.sin_addr.s_addr = inet_addr("81.2.195.254");

        getnameinfo((struct sockaddr*)&sa, sizeof sa, host, sizeof host, NULL, 0, 0);
        printf("hostname: %s", host);
    return 0;
}
Petr Marek
  • 59
  • 1
  • 7

1 Answers1

0

This is as expected, the reverse hostname (aka PTR-record) for the IP address 81.2.195.254 is indeed 254.195.forpsi.net

You can check this yourself like that, for example using the program "host":

$ host 81.2.195.254

254.195.2.81.in-addr.arpa domain name pointer 254.195.forpsi.net.

It is correct, that the hostname www.farnost-hranice.cz points to IP address 81.2.195.254, too, but there is no link the other way round.

Ctx
  • 18,090
  • 24
  • 36
  • 51
  • So in this case it is not possible to get hostname back from it's IP address? – Petr Marek Oct 21 '19 at 15:04
  • @PetrMarek DNS does not know a way to map an IP address to a hostname other than via PTR-records, so if it isn't entered there, unfortunately not. You (or someone authorized) could, however, _change_ this PTR record to that hostname, but this might have other (negative) implications. – Ctx Oct 21 '19 at 15:05