LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices



Reply
 
Search this Thread
Old 03-05-2004, 05:39 AM   #1
thomjakub
LQ Newbie
 
Registered: Nov 2003
Posts: 3

Rep: Reputation: 0
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?
 
Old 03-05-2004, 11:59 AM   #2
bnice
Member
 
Registered: Feb 2004
Location: Sacramento, CA
Distribution: Slack 9.1, slackware-current
Posts: 284

Rep: Reputation: 30
you didn't show us /etc/resolv.conf

bare minimum:
Code:
search ushustech.com
nameserver 192.168.240.1
Optional: list a hostname/address pair for each host on network.

Last edited by bnice; 03-06-2004 at 10:28 AM.
 
Old 03-06-2004, 12:27 AM   #3
thomjakub
LQ Newbie
 
Registered: Nov 2003
Posts: 3

Original Poster
Rep: Reputation: 0
----------------------
my /etc/resolv.conf
----------------------

search ushustech.com
nameserver 192.168.240.1

--------------------------

the server 192.168.240.1 is a Windows machine. Could that be the problem? Do I need to do anything at the server-end?

My code seems to work fine (unchanged) when i run it from Windows using Winsock. I am very much puzzled! I thought -nix OSes had better support for networking than you-know-what. :-(

luv
thomas

Last edited by thomjakub; 03-06-2004 at 12:35 AM.
 
Old 03-06-2004, 08:43 AM   #4
bnice
Member
 
Registered: Feb 2004
Location: Sacramento, CA
Distribution: Slack 9.1, slackware-current
Posts: 284

Rep: Reputation: 30
windows machines are rather chatty on the network. By default they broadcast thier address and any available services periodically. *nixes generally only speak when they are spoken to.
 
Old 03-06-2004, 10:08 AM   #5
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
There is a problem with your DNS server. You may not have A record entries for each host. I don't know how you have your DNS set up. It could be active directory or using WINS to dynamically update DNS or neither. In any case, there is nothing wrong with the code because it compiles and runs fine on my BSD box.
Code:
cookjh@cookwrksta01 ~/test $>./a.out 216.109.118.67
Hello world! p4.www.dcn.yahoo.com
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't SSH to remote machine: Connection closed by remote host Avatar Linux - Networking 34 05-24-2013 10:28 AM
scp: copy a file from local machine to remote machine seran Linux - Newbie 8 10-30-2007 01:23 PM
how to open a dilog on local machine when i do ssh to remote machine fahad26 Programming 3 05-03-2005 08:39 PM
How do i use a hostname given by No-IP as my machine name? z-vet Linux - Networking 3 10-22-2004 09:06 PM
using linux to redirect remote vpn traffic to another remote machine brb5548 Linux - Networking 0 06-28-2003 10:07 PM


All times are GMT -5. The time now is 04:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration