Operating System - HP-UX
1820291 Members
3400 Online
109622 Solutions
New Discussion юеВ

Re: gethostbyname frustrating error.

 
JUP
Regular Advisor

gethostbyname frustrating error.

I originally left a reply on an old thread regarding this problem but thought I'd start a new one so I can assign points.

My problem is that "gethostbyname()" does not work in my application. It fails to find the host name I give it in the /etc/hosts files.

This is what I have done to test it:

1. changed nsswitch.conf to look at files, files and then files (no DNS or NIS). This was done in SAM.
2. /etc/resolve.conf does not exist. We are not using a DNS.
3. Tried nslookup - this works well. I give nslookup ny host name and it returns the correct IP address from /etc/hosts.
4. I wrote a small C program that just calls GetHostByName() for my host name and it works well.

However in my main application (also in C) it does not work. It fails and returns a NULL.

I have read in another thread that there may be a solution to this and a link was given. As luck would have it, that link does not exist anymore.

Any ideas here would be greatly appreciated.

Thanks in advance
PA
10 REPLIES 10
Rainer von Bongartz
Honored Contributor

Re: gethostbyname frustrating error.

Did you compile your main application with identical compiler options and switches as you did compiling your test code ??

Regards
Rainer
He's a real UNIX Man, sitting in his UNIX LAN making all his UNIX plans for nobody ...
Massimo Bianchi
Honored Contributor

Re: gethostbyname frustrating error.

Hi,
You said:

changed nsswitch.conf to look at files, files and then files (no DNS or NIS). This was done in SAM.


Usually SAM can put some extra char in the files.

Did you check with a
cat -tve /etc/nsswitch.conf| more

to see ?

Check permission for /etc/hosts ?

HTH,
Massimo


Michael Kelly_5
Valued Contributor

Re: gethostbyname frustrating error.

Everything you have reported implies that gethostbyname() is working correctly (your small C test program would seem to confirm this). The fact that gethostbyname() returns NULL in your application would imply that there is an error in your application that is passing bad (corrupted) data to gethostbynam().
The fact that NULL is returned simply means that the requested name was not found.
Are you able to debug your application to check EXACTLY what is being passed to gethostbyname()?
The most obvious thing to look for is non-printing characters embedded in the hostname string.

HTH,
Michael.
The nice thing about computers is that they do exactly what you tell them. The problem with computers is that they do EXACTLY what you tell them.
Bill Hassell
Honored Contributor

Re: gethostbyname frustrating error.

An alternative to nslookup is to use nsquery which is a direct interface to gethostbyname, etc. nsquery provides all the flexibility that is missing in nslookup including testing of nsswitch-style directives on the command line. man nsquery gives you the details.


Bill Hassell, sysadmin
T G Manikandan
Honored Contributor

Re: gethostbyname frustrating error.

Try changing the hostname of the system to the FQDN way i.e.hostname.domainname

system1.hp.com


Revert
JUP
Regular Advisor

Re: gethostbyname frustrating error.

Thank you everyone for your replies. It is much appreciated. However still no luck.
To answer everyones questions:

1. yes I used the same compiler switches.
2. the nsswitch.conf file looks like this:

#cat -tve /etc/nsswitch.conf
hosts: files [NOTFOUND=continue UNAVAIL=continue] files [NOTFOUND=continue U
NAVAIL=continue TRYAGAIN=continue] files [NOTFOUND=return UNAVAIL=continue TRY
AGAIN=return] $

And the permissions of /etc/hosts look ok. They are:

-r--r--r-- 1 bin bin 707 Jul 8 10:25 /etc/hosts


3. In my main application I hardcoded a string variable with my host name to make sure the string is not corrupted. Then I passed this char array to gethostbyname(). There is no chance for corrupted vars etc. I printed out the nameof the variable with ">>>>" on either side of the name to make sure no chars before or after the var name.

4. I can run nsquery successfully too. I pass it the hostname and I get the IP address.

5. I changed the hostname to hostname.hp.com as an example in both /etc/hosts and the application. Still no luck.

If anyone can access this link or knows where the document that its referring to is kept, that will help tremendously. This was the link that was referred to in someone elses previous post regarding a similar problem.

The link is:

http://europe-support.external.hp.com/cki/bin/doc.pl/sid=664f46e90e024acf9b/screen=ckiDisplayDocument?docId=200000057171900


So I am still not sure how to fix this problem.

Thanks again everyone for your time and efforts
PA
Bill Hassell
Honored Contributor

Re: gethostbyname frustrating error.

Since you are using files and no DNS, you can remove the nsswitch.conf file as it does not change the behavior of the search if there is no /etc/resolv.conf. As you've seen with nslookup and nsquery, both hostname and IP_address lookups all look at /etc/hosts and nothing else.

I would create a very small program that accepts a hostname and call gethostbyname to eliminate any additional code from masking the underlying problem. Make sure that /etc/hosts is readable for everyone (644 owned by root). The man page gives several conditions for a NULL pointer including invalid len param.


Bill Hassell, sysadmin
Tim Adamson_1
Honored Contributor

Re: gethostbyname frustrating error.

Have you considered using tusc to see what is going on? You can download it from http://hpux.connect.org.uk/ Refer to the man page to determine how to run it. At least you might be able to determine what value is being passed to gethostbyname().

Cheers!
Yesterday is history, tomorrow is a mystery, today is a gift. That's why it's called the present.
JUP
Regular Advisor

Re: gethostbyname frustrating error.

Thank You everyone for your suggestions.
I've worked it out.

This is the solution:

The main application uses a C libarary called NSL (this library comes with HP-UX) however the small test program I wrote does not use it.

We have two HP servers - same HP-UX but one with newer libraries (including the NSL libraries) than the other.

If I compile my main application on one of the servers using its NSL it gethostbyname() does not work. If I compile the same application on the other server (with newer NSL files) gethostbyname() works.

As my small app didn't use the NSL library then it worked everytime.

Whats strange is that the gethostbyname() function does not appear to need the NSL libraries yet the NSL libraries affect this system call. Wierd !!!

The trick here is to update the NSL C libraries that came with HP-UX on the older server as that is our development server.

Any ideas on how to do that would be
appreciated.


Thanks again
PA
Brian Hackley
Honored Contributor

Re: gethostbyname frustrating error.

Hi,

At 11.x libnsl.sl and libnsl.a were moved from libc. So now they are updated as part of the 11.x ONC Cumulative patch e.g. PHNE_28103 for 11.11. The name service switch at 11.x behaves differently than 10.20, and is used by gethostbyXXXX. Also be aware that the libnss_compat.1 libnss_files.1 libnss_nisplus.1libnss_dns.1 libnss_nis.1 also come into play and are occasionally updated via patch e.g 11.11PHNE_24129 for libnss_dns.

If you are using archive libs instead of dynamic libs, then compiling in an old libnsl.a
is the risk you assume with archive libs.

Hope that helps,

-> Brian Hackley
Ask me about telecommuting!