summaryrefslogtreecommitdiff
path: root/tests/bionic/libc/common/test_getaddrinfo.c
blob: 444bef8e5fade0c78c246996b2b71661f30fd20c (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
40
41
42
43
44
/* this program is used to test that getaddrinfo() works correctly
 * without a 'hints' argument
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#include <stdio.h>  /* for printf */
#include <string.h> /* for memset */
#include <netinet/in.h>  /* for IPPROTO_TCP */

#define  SERVER_NAME  "www.android.com"
#define  PORT_NUMBER  "9999"

int main(void)
{
    struct addrinfo  hints;
    struct addrinfo* res;
    int              ret;

    /* first, try without any hints */
    ret = getaddrinfo( SERVER_NAME, PORT_NUMBER, NULL, &res);
    if (ret != 0) {
        printf("first getaddrinfo returned error: %s\n", gai_strerror(ret));
        return 1;
    }

    freeaddrinfo(res);

    /* now try with the hints */
    memset(&hints, 0, sizeof(hints));
    hints.ai_family   = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    ret = getaddrinfo( SERVER_NAME, PORT_NUMBER, &hints, &res );
    if (ret != 0) {
        printf("second getaddrinfo returned error: %s\n", gai_strerror(ret));
        return 1;
    }

    return 0;
}