|
Getting hostname of remote machine with gethostbyaddr()
This program is to find the hostname of a given IPv4 address. The problem
is that it works only for some IPs and does not for some others (refer
output).
Perhaps this means that I need to enable some services or set some configuration files for it to work properly?
The name server on the network is 192.168.240.1. Other relevant files are pasted inline.
Please help!
luv
thomas
------------------------
My program: ip2hostname.c
------------------------
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<netdb.h>
int main(int argc,char*argv[])
{
char szHostName[200];
struct sockaddr_in addr;
struct in_addr ipv6;
struct hostent *h;
if(argc!=2){
printf("No argument.\n");
return 1;
}
/*Fill in address structure*/
addr.sin_family=AF_INET;
addr.sin_port=htons(10000);
addr.sin_addr.s_addr=inet_addr(argv[1]);
h=gethostbyaddr((char*)&(addr.sin_addr),
sizeof(struct in_addr),
AF_INET );
if(h==NULL){
printf("Could not locate %s\n",argv[1]);
return 1;
}
printf("Hello world! %s\n",h->h_name);
}
-------------------
My /etc/hosts file
-------------------
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
192.168.240.64 thomas
----------------------
My /etc/host.conf file
----------------------
order bind,hosts
----------------------------
The output I get
----------------------------
[root@thomas root]# ./ip2hostname 192.168.240.64
Hello world! thomas
#Entry there in /etc/hosts
[root@thomas root]# ./ip2hostname 127.0.0.1
Hello world! localhost.localdomain
#Entry there in /etc/hosts
[root@thomas root]# ./ip2hostname 192.168.240.1
Hello world! server.ushustech.com
#not there in /etc/hosts, so contact made with network i guess
[root@thomas root]# ./ip2hostname 192.168.240.2
Could not locate 192.168.240.2
#i could successfully ping it though
[root@thomas root]# ./ip2hostname 192.168.240.3
Hello world! mail.ushustech.com
#again, not there in /etc/hosts.
[root@thomas root]# ./ip2hostname 192.168.240.4
Hello world! ushusweb.ushustech.com
#again, not there in /etc/hosts.
[root@thomas root]# ./ip2hostname 192.168.240.5
Could not locate 192.168.240.5
#ping worked fine!
[root@thomas root]# ./ip2hostname 192.168.240.6
Could not locate 192.168.240.6
#ping worked.
[root@thomas root]# ./ip2hostname 192.168.240.7
Hello world! owa.ushustech.com
#again, not there in /etc/hosts.
[root@thomas root]# ./ip2hostname 192.168.240.8
Hello world! zafin.ushustech.com
#again, not there in /etc/hosts.
[root@thomas root]# ./ip2hostname 192.168.240.9
Could not locate 192.168.240.9
#ping worked
[root@thomas root]# ./ip2hostname 192.168.240.10
Could not locate 192.168.240.10
#again ping worked. system very much present.
[root@thomas root]# ./ip2hostname 192.168.240.132
Hello world! vinodh.ushustech.com
#again, not there in /etc/hosts. from the network! hurray?
|