Fix getaddrinfo on Windows 2000.
[gnulib.git] / tests / test-getaddrinfo.c
1 #include "config.h"
2 #include "getaddrinfo.h"
3 #include "inet_ntop.h"
4 #include <stdio.h>
5 #include <string.h>
6
7 int simple (char *host, char *service)
8 {
9   char buf[BUFSIZ];
10   struct addrinfo hints;
11   struct addrinfo *ai0, *ai;
12   int res;
13
14   printf ("Finding %s service %s...\n", host, service);
15
16   memset (&hints, 0, sizeof (hints));
17   hints.ai_flags = AI_CANONNAME;
18   hints.ai_family = AF_INET;
19   hints.ai_socktype = SOCK_STREAM;
20   res = getaddrinfo (host, 0, 0, &ai0);
21
22   printf ("res %d: %s\n", res, gai_strerror (res));
23
24   if (res != 0)
25     return 1;
26
27   for (ai = ai0; ai; ai = ai->ai_next)
28     {
29       printf ("\tflags %x\n", ai->ai_flags);
30       printf ("\tfamily %x\n", ai->ai_family);
31       printf ("\tsocktype %x\n", ai->ai_socktype);
32       printf ("\tprotocol %x\n", ai->ai_protocol);
33       printf ("\taddrlen %d: ", ai->ai_addrlen);
34       printf ("\tFound %s\n",
35               inet_ntop (ai->ai_family,
36                          &((struct sockaddr_in *)
37                           ai->ai_addr)->sin_addr,
38                          buf, sizeof (buf) - 1));
39       if (ai->ai_canonname)
40         printf ("\tFound %s...\n", ai->ai_canonname);
41     }
42
43   freeaddrinfo (ai0);
44
45   return 0;
46 }
47
48 #define HOST1 "www.gnu.org"
49 #define SERV1 "http"
50 #define HOST2 "www.ibm.com"
51 #define SERV2 "http"
52 #define HOST3 "ibm.org"
53 #define SERV3 "http"
54 #define HOST4 "google.org"
55 #define SERV4 "http"
56
57 int main (void)
58 {
59   return simple (HOST1, SERV1)
60     + simple (HOST2, SERV2)
61     + simple (HOST3, SERV3)
62     + simple (HOST4, SERV4);
63 }