summaryrefslogtreecommitdiff
path: root/config/hostentp_ghba_r.c
blob: 7c6cef22efd9ec2ca775ad253e1026f2145f3cc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * hostentp_ghba_r.c:
 * Test program to see whether gethostbyaddr_r takes 7 arguments and returns
 * struct hostent*. 
 */

static const char rcsid[] = "$Id$";

#include <sys/types.h>

#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>

int main(void) {
    struct in_addr localhost;
    struct hostent hostbuf, *hp;
    char *buf;
    int herr;
    size_t buflen = 1024;
    
    localhost.s_addr = htonl(INADDR_LOOPBACK);
    buf = malloc(buflen);
    while ((hp = gethostbyaddr_r((char*)&localhost, sizeof(struct in_addr),
                                 AF_INET, &hostbuf, buf, buflen, &herr))
                    == NULL
           && errno == ERANGE)
        buf = (char*)realloc(buf, buflen *= 2);
        
    /* We assume that the loopback address can always be resolved if
     * gethostbyaddr_r is actually working. */
    if (hp == NULL) {
        fprintf(stderr, "errno = %d, herr = %d\n", errno, herr);
        return -1;
    } else
        return 0;
}