Improve name: "count-one-bits" is better than "popcount".
[gnulib.git] / tests / test-getaddrinfo.c
1 /* Test the getaddrinfo module.
2
3    Copyright (C) 2006-2007 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Simon Josefsson.  */
20
21 #include "config.h"
22 #include "getaddrinfo.h"
23 #include "inet_ntop.h"
24 #include <stdio.h>
25 #include <string.h>
26
27 int simple (char *host, char *service)
28 {
29   char buf[BUFSIZ];
30   struct addrinfo hints;
31   struct addrinfo *ai0, *ai;
32   int res;
33
34   printf ("Finding %s service %s...\n", host, service);
35
36   memset (&hints, 0, sizeof (hints));
37   hints.ai_flags = AI_CANONNAME;
38   hints.ai_family = AF_UNSPEC;
39   hints.ai_socktype = SOCK_STREAM;
40   res = getaddrinfo (host, service, 0, &ai0);
41
42   printf ("res %d: %s\n", res, gai_strerror (res));
43
44   if (res != 0)
45     return 1;
46
47   for (ai = ai0; ai; ai = ai->ai_next)
48     {
49       printf ("\tflags %x\n", ai->ai_flags);
50       printf ("\tfamily %x\n", ai->ai_family);
51       printf ("\tsocktype %x\n", ai->ai_socktype);
52       printf ("\tprotocol %x\n", ai->ai_protocol);
53       printf ("\taddrlen %ld: ", (unsigned long) ai->ai_addrlen);
54       printf ("\tFound %s\n",
55               inet_ntop (ai->ai_family,
56                          &((struct sockaddr_in *)
57                           ai->ai_addr)->sin_addr,
58                          buf, sizeof (buf) - 1));
59       if (ai->ai_canonname)
60         printf ("\tFound %s...\n", ai->ai_canonname);
61
62       {
63         char ipbuf[BUFSIZ];
64         char portbuf[BUFSIZ];
65
66         res = getnameinfo (ai->ai_addr, ai->ai_addrlen,
67                            ipbuf, sizeof (ipbuf) - 1,
68                            portbuf, sizeof (portbuf) - 1,
69                            NI_NUMERICHOST|NI_NUMERICSERV);
70         printf ("\t\tgetnameinfo %d: %s\n", res, gai_strerror (res));
71         if (res == 0)
72           {
73             printf ("\t\tip %s\n", ipbuf);
74             printf ("\t\tport %s\n", portbuf);
75           }
76       }
77
78     }
79
80   freeaddrinfo (ai0);
81
82   return 0;
83 }
84
85 #define HOST1 "www.gnu.org"
86 #define SERV1 "http"
87 #define HOST2 "www.ibm.com"
88 #define SERV2 "https"
89 #define HOST3 "microsoft.com"
90 #define SERV3 "http"
91 #define HOST4 "google.org"
92 #define SERV4 "ldap"
93
94 int main (void)
95 {
96 #if _WIN32
97   {
98     WORD requested;
99     WSADATA data;
100     int err;
101
102     requested = MAKEWORD (1, 1);
103     err = WSAStartup (requested, &data);
104     if (err != 0)
105       return 1;
106
107     if (data.wVersion < requested)
108       {
109         WSACleanup ();
110         return 2;
111       }
112   }
113 #endif
114
115   return simple (HOST1, SERV1)
116     + simple (HOST2, SERV2)
117     + simple (HOST3, SERV3)
118     + simple (HOST4, SERV4);
119 }