1ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner/* this program is used to test that getaddrinfo() works correctly
2ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner * without a 'hints' argument
3ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner */
4ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner
5ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner#include <sys/types.h>
6ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner#include <sys/socket.h>
7ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner#include <netdb.h>
8ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner
9ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner#include <stdio.h>  /* for printf */
10ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner#include <string.h> /* for memset */
11ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner#include <netinet/in.h>  /* for IPPROTO_TCP */
12ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner
13ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner#define  SERVER_NAME  "www.android.com"
14ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner#define  PORT_NUMBER  "9999"
15ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner
16ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turnerint main(void)
17ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner{
18ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    struct addrinfo  hints;
19ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    struct addrinfo* res;
20ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    int              ret;
21ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner
22ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    /* first, try without any hints */
23ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    ret = getaddrinfo( SERVER_NAME, PORT_NUMBER, NULL, &res);
24ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    if (ret != 0) {
25ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner        printf("first getaddrinfo returned error: %s\n", gai_strerror(ret));
26ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner        return 1;
27ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    }
28ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner
29ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    freeaddrinfo(res);
30ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner
31ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    /* now try with the hints */
32ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    memset(&hints, 0, sizeof(hints));
33ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    hints.ai_family   = AF_UNSPEC;
34ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    hints.ai_socktype = SOCK_STREAM;
35ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    hints.ai_protocol = IPPROTO_TCP;
36ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner
37ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    ret = getaddrinfo( SERVER_NAME, PORT_NUMBER, &hints, &res );
38ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    if (ret != 0) {
39ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner        printf("second getaddrinfo returned error: %s\n", gai_strerror(ret));
40ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner        return 1;
41ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    }
42ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner
43ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner    return 0;
44ac6a88edbf65ff3f9e40d6e4bf1e55a002be6d6cDavid 'Digit' Turner}
45