]> gitweb.fperrin.net Git - iftop.git/blob - config/hostentp_ghba_r.c
Import iftop-1.0pre4
[iftop.git] / config / hostentp_ghba_r.c
1 /*
2  * hostentp_ghba_r.c:
3  * Test program to see whether gethostbyaddr_r takes 7 arguments and returns
4  * struct hostent*. 
5  */
6
7 static const char rcsid[] = "$Id: hostentp_ghba_r.c,v 1.1 2002/11/04 12:27:35 chris Exp $";
8
9 #include <sys/types.h>
10
11 #include <errno.h>
12 #include <netdb.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <netinet/in.h>
16
17 int main(void) {
18     struct in_addr localhost;
19     struct hostent hostbuf, *hp;
20     char *buf;
21     int herr;
22     size_t buflen = 1024;
23     
24     localhost.s_addr = htonl(INADDR_LOOPBACK);
25     buf = malloc(buflen);
26     while ((hp = gethostbyaddr_r((char*)&localhost, sizeof(struct in_addr),
27                                  AF_INET, &hostbuf, buf, buflen, &herr))
28                     == NULL
29            && errno == ERANGE)
30         buf = (char*)realloc(buf, buflen *= 2);
31         
32     /* We assume that the loopback address can always be resolved if
33      * gethostbyaddr_r is actually working. */
34     if (hp == NULL) {
35         fprintf(stderr, "errno = %d, herr = %d\n", errno, herr);
36         return -1;
37     } else
38         return 0;
39 }