Originally posted by: SystemAdmin
Hi,
I have observed that gethostbyaddr_r function is not returning correct host name, IP address and aliases.
I have /etc/hosts entries as follows:
1.2.3.4 xxx xxx.domain
5.6.7.8 yyy yyy.domain
10.112.5.50 host1 host1.domain.com localhost
When i use gethostbyaddr_r() function, then the hostent is containing 1.2.3.4 though i sent in addr as 10.112.5.20.
Please find the output of test program below:
bash-2.05b$ ./testhost host1
gethostbyname_r for host1...
address: 10.112.5.50, address len: 4
calling gethostbyaddr_r for address: 10.112.5.50
address: 1.2.3.4, address len: 4
checking host alias..
alias = xxx.domain
bash-2.05b$
Please let me know the problem with the code or with the function gethostbyaddr_r().
The code written is as below:
#include <stdio.h>
#include <netdb.h>
#include <unistd.h>
void main(int argc, char **argv)
{
struct hostent h_ent;
struct hostent h_ent_r;
struct hostent h_ent_nonr;
struct hostent_data h_data;
char tmpbuf
16;
int i = 0;
if(argc < 2)
{
printf("usage: %s hostname\n", argv[0]);
exit(0);
}
memset (&h_data, 0, sizeof (h_data));
fprintf(stderr, "gethostbyname_r for %s...\n", argv[1]);
if (gethostbyname_r( argv[1], &h_ent, &h_data) == -1)
{
fprintf(stderr,"error no returned by gethostbyname_r: %d\r\n", h_errno);
exit(1);
}
i = inet_ntop(h_ent.h_addrtype, h_ent.h_addr_list[0], tmpbuf, sizeof(tmpbuf));
fprintf(stderr, "address: %s, address len: %d\n", tmpbuf, h_ent.h_length);
memset (&h_data, 0, sizeof (h_data));
fprintf(stderr, "calling gethostbyaddr_r for address: %s\n", tmpbuf);
if (gethostbyaddr_r( h_ent.h_addr_list[0], h_ent.h_length, h_ent.h_addrtype,
&h_ent_r, &h_data) == -1)
{
fprintf(stderr,"error no returned by gethostbyname_r: %d\r\n", h_errno);
exit(1);
}
i = inet_ntop(h_ent_r.h_addrtype, h_ent_r.h_addr_list[0], tmpbuf, sizeof(tmpbuf));
fprintf(stderr, "address: %s, address len: %d\n", tmpbuf, h_ent_r.h_length);
/*
fprintf(stderr, "calling gethostbyaddr for address: %d.%d.%d.%d\n", (h_ent.h_addr_list[0])[0], (h_ent.h_addr_list[0])[1], (h_ent.h_addr_list[0])[2], (h_ent.h_addr_list[0])[3]);
h_ent_nonr = gethostbyaddr( h_ent.h_addr_list[0], h_ent.h_length, h_ent.h_addrtype);
if(!h_ent_nonr)
{
fprintf(stderr,"error no returned by gethostbyname_r: %d\r\n", h_errno);
exit(1);
}
i = inet_ntop(h_ent_nonr->h_addrtype, h_ent_nonr->h_addr_list[0], tmpbuf, sizeof(tmpbuf));
fprintf(stderr, "address: %s, address len: %d\n", tmpbuf, h_ent_nonr->h_length);
*/
fprintf(stderr, "checking host alias..\n");
if ( h_ent.h_aliases ) {
for ( i = 0; h_ent.h_aliases[i]; i++)
fprintf(stderr, "alias = %s\n", h_ent.h_aliases[i]);
}
}
Many Thanks,
Srini
#AIX-Forum