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;
}