LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 04: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, 10: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 09:28 AM.
 
Old 03-05-2004, 11:27 PM   #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-05-2004 at 11:35 PM.
 
Old 03-06-2004, 07: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, 09: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



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 35 10-23-2017 12:21 AM
scp: copy a file from local machine to remote machine seran Linux - Newbie 8 10-30-2007 12:23 PM
how to open a dilog on local machine when i do ssh to remote machine fahad26 Programming 3 05-03-2005 07:39 PM
How do i use a hostname given by No-IP as my machine name? z-vet Linux - Networking 3 10-22-2004 08:06 PM
using linux to redirect remote vpn traffic to another remote machine brb5548 Linux - Networking 0 06-28-2003 09:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 12:43 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
Open Source Consulting | Domain Registration