]> gitweb.fperrin.net Git - iftop.git/blob - config/int_ghba_r.c
Import iftop-1.0pre4
[iftop.git] / config / int_ghba_r.c
1 /*
2  * int_ghba_r.c:
3  * Test program to see whether gethostbyaddr_r takes 8 arguments and returns
4  * int.
5  */
6
7 static const char rcsid[] = "$Id: int_ghba_r.c,v 1.4 2011/10/03 18:19:13 pdw Exp $";
8
9 #include <sys/socket.h>
10 #include <sys/types.h>
11
12 #include <errno.h>
13 #include <netdb.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <netinet/in.h>
17
18 int main(void) {
19     struct in_addr localhost;
20     struct hostent hostbuf, *hp;
21     char *buf;
22     int res, herr;
23     size_t buflen = 1024;
24     
25     localhost.s_addr = htonl(INADDR_LOOPBACK);
26     buf = malloc(buflen);
27     while ((res = gethostbyaddr_r((char*)&localhost, sizeof localhost, AF_INET,
28                                   &hostbuf, buf, buflen, &hp, &herr))
29                     == 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 (res || hp == NULL) {
35         fprintf(stderr, "errno = %d, herr = %d, res = %d\n", errno, herr, res);
36         return -1;
37     } else
38         return 0;
39 }