LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-27-2008, 05:01 PM   #1
virtualCoder
Member
 
Registered: Sep 2007
Distribution: Ubuntu
Posts: 33

Rep: Reputation: 15
Calling gethostbyname twice on same ip addr


Hello all,

This problem is related to a server-client program. My program that crashes is the client; the server is on the internet.

The client program seems to crash when I call gethostbyname the second time on the same ip address inside a child thread (where the first time called was in the main thread).

So here it is:
-main thread connects with a socket (and called gethostbyname along the way), keeps commmunicating.
-then it launches a child thread that has to connect to another socket and calls gethostbyname on the same ip address as parent thread. This is where it crashes.

The reason why gethostbyname gets called on the same ip address is because sometimes the server it needs to communicate with is the same one the parent thread was communicating with but this is not always true.

I used gdb to find out that it was crashing exactly at gethostbyname().

Here is part of the output when it crashes:

*** glibc detected *** ./FD: free(): invalid next size (normal): 0x00000000006054d0 ***
======= Backtrace: =========
/lib/libc.so.6[0x2b425b8f0b0a]
/lib/libc.so.6(cfree+0x8c)[0x2b425b8f46fc]
/lib/libc.so.6(fclose+0x14c)[0x2b425b8df93c]
/lib/libc.so.6[0x2b425b961223]
/lib/libc.so.6(__res_maybe_init+0x65)[0x2b425b962b15]
/lib/libc.so.6(__nss_hostname_digits_dots+0x41)[0x2b425b964531]
/lib/libc.so.6(gethostbyname+0x90)[0x2b425b968fc0]
./FD(__gxx_personality_v0+0x257)[0x401017]
/lib/libpthread.so.0[0x2b425b669317]
/lib/libc.so.6(clone+0x6d)[0x2b425b952d5d]

Then it shows a memory map.
Looking at the backtrace, I can see that the problem seems to occur inside the gethostbyname() call.

In case one cannot call gethostbyname twice on the same ip address, i am willing to work around it by storing previous data.

If this is the case, can someone please kindly explain why this happens or how gethostbyname() works, making this happen.
 
Old 09-27-2008, 05:16 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
gethostbyname isn't reentrant. Try gethostbyname_r and see if it works. Unless by "child thread" you mean a forked process.
ta0kira
 
Old 09-28-2008, 06:45 AM   #3
virtualCoder
Member
 
Registered: Sep 2007
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: 15
Oh thanks, that hopefully is the problem.
I was using pthreads.
 
Old 09-28-2008, 10:54 AM   #4
virtualCoder
Member
 
Registered: Sep 2007
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: 15
Hmm, the problem still existed with gethostbyname_r() so I worked around it and the repeated ip address does not get passed to gethostbyname again.
That seems to have got my program working.

I still wonder why gethostbyname was crashing on the second call on same ip address (even the reentrant one).

Thanks ta0kira for reminding me of using reentrant functions when working with pthreads.
 
Old 09-28-2008, 12:45 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I just noticed this: How are you using gethostbyname with an IP address? Do you use the hostent* to get an IP address via h_addr_list?
ta0kira
 
Old 09-28-2008, 03:10 PM   #6
virtualCoder
Member
 
Registered: Sep 2007
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: 15
I meant the ip address in string format, e.g. "127.0.0.1".

But I found the problem in my code, it is very embarrassing.
I overlooked this code too many times:
char *buffer = (char*)malloc(sizeof(100));
instead of:
char *buffer = (char*)malloc(100*sizeof(char));

This silly problem caused the program to crash whether I called free() or whether any system call called free().
It seems as gethostbyname() also calls free() (I guess after the first call), and there was another system call that crashed the program when it internally called free(); that made be suspicious of what might be really going on.

Everything seems to be working fine now.

Out of curiosity (and lack of knowledge), I am still not sure how calling this makes all subsequent free() calls anywhere else on other variables fails as well. In other words can someone explain what malloc exactly does when this is called:

char *buffer = (char*)malloc(sizeof(100));
 
Old 09-28-2008, 04:38 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You allocate a buffer too small to store your string: sizeof(100) = sizeof(int) = 4

You were overwriting malloc internal data. That explains malloc/free failure.
 
Old 09-28-2008, 04:48 PM   #8
virtualCoder
Member
 
Registered: Sep 2007
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: 15
Right thanks, I didnt think like that! That solves all my issues
 
Old 09-28-2008, 07:30 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I think inet_atoi would be better in this case.
ta0kira
 
Old 09-29-2008, 04:29 PM   #10
virtualCoder
Member
 
Registered: Sep 2007
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: 15
What does inet_atoi do and where should I use it? It does not show up in the man pages.
 
Old 09-29-2008, 07:17 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
inet_aton: http://analyser.oli.tudelft.nl/beej/...ts.html#ipaddr
 
Old 09-30-2008, 09:36 AM   #12
virtualCoder
Member
 
Registered: Sep 2007
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: 15
Thanks for the direction. I guess you meant to say inet_aton() and not inet_atoi.

I'm amazed every time I see Beej's networking article on the net again and again. Then again good work is supposed to become popular.
 
Old 09-30-2008, 04:07 PM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Yes, sorry. Should have looked at the reference myself! I did a web search and it came up, so I thought my memory was correct, but didn't actually look at my code that uses it!
ta0kira
 
  


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
problem with gethostbyname hiren_bhatt Programming 8 05-14-2006 09:30 PM
Obtain IP addr. from MAC addr? Ryand833 Linux - Wireless Networking 3 06-30-2005 01:59 PM
Using the gethostbyname command mdlister Linux - Newbie 5 03-14-2005 07:50 AM
gethostbyname in script? farry Linux - Newbie 2 03-14-2005 02:14 AM
odd recursion: calling "by hand" vs calling by cronscript... prx Programming 4 02-12-2005 04:59 PM

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

All times are GMT -5. The time now is 02:32 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