QNX 4.25L gethostbyname problem

I have QNX 4.25L (from the cdrom ‘Product Suite May 2001’) freshly installed.

The problem is gethostbyname() does not read from /etc/hosts file.

THe /etc/resolv.conf already has the line:

lookup file bind

My dns does not have ‘localhost’. Hence, I expect gethostbyname() can
pick up localhost from /etc/hosts. But, it does not. However, ‘telnet’,
‘ping’ can pick up localhost.

A program like this cannot resolve the host name without using dns.

#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>

int main (int argc, char** argv)
{
struct hostent* p;

p = gethostbyname (argv[1]);

if (p != 0)
{
char* s;

printf (“host=%s\n”, p->h_name);
printf (“length=%d\n”, p->h_length);
s = p->h_addr;

if (s != 0)
{
printf (“h_addr=%d.”, *s++);
printf ("%d.", *s++);
printf ("%d.", *s++);
printf ("%d\n", *s++);
}
}
else
printf (“cannot find %s\n”, argv[1]);


p = gethostbyname (“localhost”);

if (p != 0)
printf (“host=%s\n”, p->h_name);
else
printf (“cannot find localhost\n”);


return 0;
}

$ cc -o a a.c -lsocket -lrpc
/usr/watcom/10.6/bin/wcc386 -zq -ms -4r -i=/usr/watcom/10.6/usr/include -i=/usr/include a.c
/usr/watcom/10.6/bin/wlink op quiet form qnx flat na a op priv=3 op c libp /usr/watcom/10.6/usr/lib:/usr/lib:. f /tmp/a.o l /usr/watcom/10.6/usr/lib/socket3r.lib l /usr/lib/rpc3r.lib op offset=40k op st=32k
$ ./a yahoo.com
host=yahoo.com
length=4
h_addr=216.115.108.245
cannot find localhost

If I remove /etc/resolv.conf (according to documentation, it will use
/etc/hosts),

$ ./a fireopal
cannot find fireopal
cannot find localhost
$ ping fireopal
PING fireopal.empress.com (197.43.220.25): 56 data bytes
64 bytes from 197.43.220.25: icmp_seq=0 ttl=255 time=0 ms
64 bytes from 197.43.220.25: icmp_seq=1 ttl=255 time=0 ms



I wonder if ‘telnet’, ‘ping’, … are linked with different library
from what I have.

Any idea? THanks in advance.


Gordon Man
gman@empress.com

: $ cc -o a a.c -lsocket -lrpc
: /usr/watcom/10.6/bin/wcc386 -zq -ms -4r -i=/usr/watcom/10.6/usr/include -i=/usr/include a.c
: /usr/watcom/10.6/bin/wlink op quiet form qnx flat na a op priv=3 op c libp /usr/watcom/10.6/usr/lib:/usr/lib:. f /tmp/a.o l /usr/watcom/10.6/usr/lib/socket3r.lib l /usr/lib/rpc3r.lib op offset=40k op st=32k

Okay, I figure that out myself.
I need to do:
cc -o a a.c -L/usr/lib -lsocket -lrpc

That works.

Now, the question is why by default, the wrong library is linked.


Gordon Man
gman@empress.com

Sorry, my solution does not work. It works for gethostbyname but it
breaks gethostname(). It now returns a node number (e.g. 3) instead
of the hostname I want.

Any suggustion?

gman_@empress.com wrote:

: : $ cc -o a a.c -lsocket -lrpc
: : /usr/watcom/10.6/bin/wcc386 -zq -ms -4r -i=/usr/watcom/10.6/usr/include -i=/usr/include a.c
: : /usr/watcom/10.6/bin/wlink op quiet form qnx flat na a op priv=3 op c libp /usr/watcom/10.6/usr/lib:/usr/lib:. f /tmp/a.o l /usr/watcom/10.6/usr/lib/socket3r.lib l /usr/lib/rpc3r.lib op offset=40k op st=32k

: Okay, I figure that out myself.
: I need to do:
: cc -o a a.c -L/usr/lib -lsocket -lrpc

: That works.

: Now, the question is why by default, the wrong library is linked.

: –
: Gordon Man
: gman@empress.com


Gordon Man
gman@empress.com

Okay, I am talking to myself.

It actaully works. It does not work because bash (downloaded from qnx)
set HOSTNAME to 3 (the node number) and gethostname from /usr/lib picks
that up. watcom one does not. unset HOSTNAME solves the gethostname
problem.

Why do I use bash? The sh/ksh comes with QNX cannot run the shell
scripts that can run everywhere else (including win32). It cannot
handle shell function properly.


gman_@empress.com wrote:
: Sorry, my solution does not work. It works for gethostbyname but it
: breaks gethostname(). It now returns a node number (e.g. 3) instead
: of the hostname I want.

: Any suggustion?

: gman_@empress.com wrote:

: : : $ cc -o a a.c -lsocket -lrpc
: : : /usr/watcom/10.6/bin/wcc386 -zq -ms -4r -i=/usr/watcom/10.6/usr/include -i=/usr/include a.c
: : : /usr/watcom/10.6/bin/wlink op quiet form qnx flat na a op priv=3 op c libp /usr/watcom/10.6/usr/lib:/usr/lib:. f /tmp/a.o l /usr/watcom/10.6/usr/lib/socket3r.lib l /usr/lib/rpc3r.lib op offset=40k op st=32k

: : Okay, I figure that out myself.
: : I need to do:
: : cc -o a a.c -L/usr/lib -lsocket -lrpc

: : That works.

: : Now, the question is why by default, the wrong library is linked.

: : –
: : Gordon Man
: : gman@empress.com

: –
: Gordon Man
: gman@empress.com


Gordon Man
gman@empress.com

gman_@empress.com wrote:

Okay, I am talking to myself.

It actaully works. It does not work because bash (downloaded from qnx)
set HOSTNAME to 3 (the node number) and gethostname from /usr/lib picks
that up. watcom one does not. unset HOSTNAME solves the gethostname
problem.

I faced the similar problem (also unanswered):
gethostname from tcp toolkit 424 tried to open the /etc/uucpname file and if the
file did not exists, returns with an error.
If the /etc/uucpname contains the IP, or a name corresponding to the Socket name (and to
the content of /etc/hosts file), it works. However, ftp client from the same runtime (4.24)
does not need any /etc/uucpname file.

Very strange.

Can anybody answer this ?

Thanks.


Mgr. Martin Gazak, MicroStep-MIS
Ilkovicova 3, 841 04 Bratislava, Slovakia
Tel: +421 2 60291 816
e-mail:matog@microstep-mis.sk

matog@microstep-mis.sk wrote:

gman_@empress.com > wrote:
Okay, I am talking to myself.

It actaully works. It does not work because bash (downloaded from qnx)
set HOSTNAME to 3 (the node number) and gethostname from /usr/lib picks
that up. watcom one does not. unset HOSTNAME solves the gethostname
problem.

I faced the similar problem (also unanswered):
gethostname from tcp toolkit 424 tried to open the /etc/uucpname file and if the
file did not exists, returns with an error.
If the /etc/uucpname contains the IP, or a name corresponding to the Socket name (and to
the content of /etc/hosts file), it works. However, ftp client from the same runtime (4.24)
does not need any /etc/uucpname file.

Very strange.

Can anybody answer this ?

Function call gethostname() search the hostname in this order:

  1. getenv(“HOSTNAME”), if it is set, return this value.

  2. Locate the socket manager, and send a _SOCK_GETHOSTNAME
    request to it, and return the value from socket manager.
    (so SOCK=1 hostname get you hostname from node 1)

Please note, if there is no SOCK= in env, the library
will go to network do a global search, and grab whichever
it can find.

  1. if can’t find the socket manager, then the function try
    to open /etc/uucpname., and then /etc/uucpname, return
    the string in it.

-xtang


Thanks.


Mgr. Martin Gazak, MicroStep-MIS
Ilkovicova 3, 841 04 Bratislava, Slovakia
Tel: +421 2 60291 816
e-mail:> matog@microstep-mis.sk

Finally, I got everything works (I think and I hope).

I am using 4.25L. It does not have /etc/uucpname. So, I think my problem
is not the same as yours.

  1. I need to force it to link with “-L/usr/lib -lsocket -lrpc”.
  2. I need to get the bash source code and make it not to set HOSTNAME.

Just unset HOSTNAME in .bashrc and .profile is not good enough. For the
shell script that starts with ‘:’ will have HOSTNAME set to 3 again
if I use the bash ftp’ed from QNX.

matog@microstep-mis.sk wrote:
: gman_@empress.com wrote:
:> Okay, I am talking to myself.

:> It actaully works. It does not work because bash (downloaded from qnx)
:> set HOSTNAME to 3 (the node number) and gethostname from /usr/lib picks
:> that up. watcom one does not. unset HOSTNAME solves the gethostname
:> problem.

: I faced the similar problem (also unanswered):
: gethostname from tcp toolkit 424 tried to open the /etc/uucpname file and if the
: file did not exists, returns with an error.
: If the /etc/uucpname contains the IP, or a name corresponding to the Socket name (and to
: the content of /etc/hosts file), it works. However, ftp client from the same runtime (4.24)
: does not need any /etc/uucpname file.

: Very strange.

: Can anybody answer this ?

: Thanks.

: —

: Mgr. Martin Gazak, MicroStep-MIS
: Ilkovicova 3, 841 04 Bratislava, Slovakia
: Tel: +421 2 60291 816
: e-mail:matog@microstep-mis.sk


Gordon Man
gman@empress.com

gman_@empress.com wrote:

Finally, I got everything works (I think and I hope).

I am using 4.25L. It does not have /etc/uucpname. So, I think my problem
is not the same as yours.

Finally I found, where is a problem: gethostname is not part of the TCP
toolkit, getsockname must be used without nedd of an /etc/uucpname file.


Mgr. Martin Gazak, MicroStep-MIS
Ilkovicova 3, 841 04 Bratislava, Slovakia
Tel: +421 2 60291 816
e-mail:matog@microstep-mis.sk