LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2006, 10:40 PM   #1
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Rep: Reputation: 15
problem with gethostbyname


I am finding problem with the gethostbyname function. I write the following small programme to see weather its working properly or not,

Code:
#include<netdb.h>

int main()
{
        struct hostent * hostinfo;

        hostinfo = (struct hostent *)malloc(sizeof(struct hostent));

        if(hostinfo == -1)
        {
                error("malloc");
                exit(1);
        }

        hostinfo = gethostbyname("localhost");

        printf("host mane is %s\n",hostinfo->h_name);
        printf("IP address %s\n",hostinfo->h_addr);
}
When i run this program it gives the following output,


host mane is localhost.localdomain
IP address


That is, it does not display the ip address. When I use "www.google.com" instead of localhost even then it gives me some wrong output like this

host mane is www.l.google.com
IP address @�www.@�www.@顓ogle.com

The /etc/hosts entries are proper.

Does anybody knows where can be the problem.
I am using FC3, kernel 2.6.10 and glibc-2.3.3.

Thanks
Hiren
 
Old 05-07-2006, 11:49 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
There is a LQ-forum dedicated to programming. I have reported the post so a moderator can move it.

With regards to the question. h_addr is not a character array, so the conversion specifier %s is incorrect. h_addr is an IP-address, stored as a couple of 8-bit characters (e.g. 127.0.0.1 (without the dots).

Two gethostbyname examples
example using h_addr_list element
example using h_addr element
 
Old 05-08-2006, 12:25 AM   #3
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Original Poster
Rep: Reputation: 15
I will take care of posting such questions on Programmign section next time

Oh yes now I got the thing, the first example you provided made me understand every thing.

Actaully in the hostent structure it was writen the following line and hance I used %s,

char **h_addr_list; /* list of addresses */

#define h_addr h_addr_list[0] /* for backward compatibility */

But now its fine,
Thanks

Last edited by hiren_bhatt; 05-08-2006 at 12:36 AM.
 
Old 05-08-2006, 12:25 AM   #4
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Original Poster
Rep: Reputation: 15
Then what should I give in gethostbyaddr function, if I give like this

Code:
hostinfo = gethostbyaddr("127.0.0.1",4,2);
It gives me Segmentation fault. 4 is the length of IP address and 2 is the type.

Last edited by hiren_bhatt; 05-08-2006 at 12:33 AM.
 
Old 05-08-2006, 05:29 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
The segmentation fault probably occurs as you have a variable hostinfo of type hostent (not a pointer to a type hostent). However, gethostbyaddr returns a pointer to a struct hostent.
Code:
// not tested
struct hostent *ptr_hostinfo;
ptr_hostinfo=gethostbyaddr(.....);
From the examples that I found, it looks like the first argument must (again) be an array of 8-bit bytes and not a string (character array). Have a look at inet_aton to convert 127.0.0.1 to a value usable by gethostbyaddr.
Last, don't use 2 for the type. Use one of the predefined constants. See man gethostbyaddr for valid ones for the function.

Last edited by Wim Sturkenboom; 05-08-2006 at 05:33 AM.
 
Old 05-08-2006, 08:36 AM   #6
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Original Poster
Rep: Reputation: 15
Well I had all ready taken a pointer of hostent type and not a variable. Yes exactly i tried the inet_aton()and then pass it to the gethostbyaddr. It worked fine, but I found a strange thing, it may can be some silly mistake of mine. Well this is as follows.
The type to be used is 2(i have found it from gethostbyname it self), if I keep 1 it will give me segmentation fault.
Code:
include<netdb.h>

int main()
{
   struct hostent * hostinfo;
   int i,err;
   struct in_addr * cp;
   char * ptr;

   hostinfo = (struct hostent *)malloc(sizeof(struct hostent));

   if(hostinfo == -1)
   {
        error("malloc");
        exit(1);
   }
   ptr = (char *)"127.0.0.1";

   inet_aton(ptr,cp);

   hostinfo = gethostbyaddr(cp,4,2);

   printf("host mane is %s\n",hostinfo->h_name);
}
Now you can see there are two variables of type int i.e 'i' and 'err'. If i keep only one variable it will compile but will give segmentation fault when run, and if i keep 2 variables it gives the proper output i.e. "host mane is localhost.localdomain"
You can also compile this on your machine and see what happens, does same thing happens or not. I have tested this atleast 10 time before posting.

I didnt understand why this is happening. If you have some clue
 
Old 05-08-2006, 04:05 PM   #7
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 05-09-2006, 01:03 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
When compiling, I get several warnings. I use gcc -Wall myfile.c -o myfile

First of all, you forgot an # before include on the first line (might have been a copy error). Next

1) malloc() unknow (line 10); include stdlib.h; this will make that the compiler knows the malloc() function
2) malloc returns a pointer; on fail it returns NULL and not -1 (line 12)
3) error() is not defined (line 14); should probably perror
4) exit() unknown (line 15); include stdlin.h
5) inet_aton unknown (line 19); include arpa/inet.h (and maybe sys/socket.h , netinet/in.h)
6) printf() unknown (line 23); include stdio.h
7) i and err are not used
8) main is supposed to return a value

It runs on my system without segfaults. However, there are some issues that are not right.
1) You don't need malloc. gethostbyname returns a pointer that already points to a hostent structure and you can / have to directly assign it to hostinfo. The way you use it makes that you loose the pointer to the allocated memory and that you will never be able to free that memory (except by terminating the program).
BTW you don't free the allocated memory
2) I always battle with lines like ptr=(char*)"127.0.0.1"; (it might be correct). Personally I would declare a variable to hold the string
Code:
char myaddr[16];
strcpy(myaddr,"127.0.0.1");
inet_aton(myaddr,cp);
3) Although you pass a pointer (cp) as the second argument of inet_aton, this pointer points to nowhere. Either you have to use malloc or define a variable that can hold the info (as shown below).
Code:
struct in_addr cp; // not *cp
char myaddr[16];
strcpy(myaddr,"127.0.0.1");
inet_aton(myaddr,&cp);
By using the &cp, you pass the address of cp (which is the pointer to cp). This is more than likely the cause of the segmentation fault.
4) Last but not least, you must check return values. If gethostbyaddr retuns NULL, your last printf statement will give a segmentation fault.

With regards to debugging. Either learn to use the debugger (gdb if you are using linux) or use printf statements. Just add printf before and afterlines that you don't trust. In your situation, this will be after nearly every statement. Something like
Code:
include<netdb.h>

int main()
{
   struct hostent * hostinfo;
   int i,err;
   struct in_addr * cp;
   char * ptr;

   hostinfo = (struct hostent *)malloc(sizeof(struct hostent));
   printf("1\n");

   if(hostinfo == -1)
   {
        error("malloc");
        exit(1);
   }
   ptr = (char *)"127.0.0.1";
   printf("2\n");

   inet_aton(ptr,cp);
   printf("3\n");

   hostinfo = gethostbyaddr(cp,4,2);
   printf("4\n");

   printf("host mane is %s\n",hostinfo->h_name);
}
If you see 2 in your output, but not 3, you know where (one of) the problems are.

Just for the fun a little utility where you can pass the IP address as an argument to the executable and it will give you the hostname.
Code:
#include <stdio.h>
#include <netdb.h>
#include <stdlib.h>
#include <arpa/inet.h>

int main(int argc, char *argv[])
{
   struct hostent * hostinfo;
   struct in_addr addr;
   if(argc!=2)
   {
      printf("Use: %s IP address\n",argv[0]);
      return 1;
   }

   if(inet_aton(argv[1],&addr)==0)
   {
      printf("inet_aton returned 0\n");
      return 2;

   }
   hostinfo = gethostbyaddr(&addr,4,2);
   if(hostinfo==NULL)
   {
      printf("gethostbyaddr returned NULL\n");
      return 2;
   }

   printf("host name is %s\n",hostinfo->h_name);

   return 0;
}

Last edited by Wim Sturkenboom; 05-09-2006 at 01:05 AM.
 
Old 05-14-2006, 09:30 PM   #9
hiren_bhatt
Member
 
Registered: Oct 2005
Distribution: FC3,Debian
Posts: 127

Original Poster
Rep: Reputation: 15
Sorry for a delayed reply.

Anyways thanks for all that great tips and that small program too.
I will take care of these all things from next time, it solved lot of my doubt.

Thanks
Hiren
 
  


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
gethostbyname in script? farry Linux - Newbie 2 03-14-2005 02:14 AM
gethostbyname() error jnusa Programming 0 01-12-2005 04:56 AM
Problem with # and gethostbyname lolilol Linux - Software 0 04-26-2004 05:29 AM
Problem w/ gethostbyname() robeb Linux - Networking 3 05-22-2002 08:42 AM
gethostbyname error for aimstr8 Linux - Security 7 03-09-2002 04:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:53 PM.

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