tests: avoid some compiler warnings
[gnulib.git] / tests / test-getaddrinfo.c
1 /* Test the getaddrinfo module.
2
3    Copyright (C) 2006-2009 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 3 of the License, or
8    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
17
18 /* Written by Simon Josefsson.  */
19
20 #include <config.h>
21 #include <netdb.h>
22
23 #include <arpa/inet.h>
24 #include <errno.h>
25 #include <netinet/in.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 /* Whether to print debugging messages.  */
30 #define ENABLE_DEBUGGING 0
31
32 #if ENABLE_DEBUGGING
33 # define dbgprintf printf
34 #else
35 # define dbgprintf if (0) printf
36 #endif
37
38 /* BeOS does not have AF_UNSPEC.  */
39 #ifndef AF_UNSPEC
40 # define AF_UNSPEC 0
41 #endif
42
43 #ifndef EAI_SERVICE
44 # define EAI_SERVICE 0
45 #endif
46
47 static int
48 simple (char const *host, char const *service)
49 {
50   char buf[BUFSIZ];
51   static int skip = 0;
52   struct addrinfo hints;
53   struct addrinfo *ai0, *ai;
54   int res;
55   int err;
56
57   /* Once we skipped the test, do not try anything else */
58   if (skip)
59     return 0;
60
61   dbgprintf ("Finding %s service %s...\n", host, service);
62
63   /* This initializes "hints" but does not use it.  Is there a reason
64      for this?  If so, please fix this comment.  */
65   memset (&hints, 0, sizeof (hints));
66   hints.ai_flags = AI_CANONNAME;
67   hints.ai_family = AF_UNSPEC;
68   hints.ai_socktype = SOCK_STREAM;
69
70   res = getaddrinfo (host, service, 0, &ai0);
71   err = errno;
72
73   dbgprintf ("res %d: %s\n", res, gai_strerror (res));
74
75   if (res != 0)
76     {
77       /* EAI_AGAIN is returned if no network is available. Don't fail
78          the test merely because someone is down the country on their
79          in-law's farm. */
80       if (res == EAI_AGAIN)
81         {
82           skip++;
83           fprintf (stderr, "skipping getaddrinfo test: no network?\n");
84           return 77;
85         }
86       /* IRIX reports EAI_NONAME for "https".  Don't fail the test
87          merely because of this.  */
88       if (res == EAI_NONAME)
89         return 0;
90       /* Solaris reports EAI_SERVICE for "http" and "https".  Don't
91          fail the test merely because of this.  */
92       if (res == EAI_SERVICE)
93         return 0;
94       /* AIX reports EAI_NODATA for "https".  Don't fail the test
95          merely because of this.  */
96       if (res == EAI_NODATA)
97         return 0;
98       /* Provide details if errno was set.  */
99       if (res == EAI_SYSTEM)
100         dbgprintf ("system error: %s\n", strerror (err));
101
102       return 1;
103     }
104
105   for (ai = ai0; ai; ai = ai->ai_next)
106     {
107       dbgprintf ("\tflags %x\n", ai->ai_flags);
108       dbgprintf ("\tfamily %x\n", ai->ai_family);
109       dbgprintf ("\tsocktype %x\n", ai->ai_socktype);
110       dbgprintf ("\tprotocol %x\n", ai->ai_protocol);
111       dbgprintf ("\taddrlen %ld: ", (unsigned long) ai->ai_addrlen);
112       dbgprintf ("\tFound %s\n",
113                  inet_ntop (ai->ai_family,
114                             &((struct sockaddr_in *)
115                               ai->ai_addr)->sin_addr,
116                             buf, sizeof (buf) - 1));
117       if (ai->ai_canonname)
118         dbgprintf ("\tFound %s...\n", ai->ai_canonname);
119
120       {
121         char ipbuf[BUFSIZ];
122         char portbuf[BUFSIZ];
123
124         res = getnameinfo (ai->ai_addr, ai->ai_addrlen,
125                            ipbuf, sizeof (ipbuf) - 1,
126                            portbuf, sizeof (portbuf) - 1,
127                            NI_NUMERICHOST|NI_NUMERICSERV);
128         dbgprintf ("\t\tgetnameinfo %d: %s\n", res, gai_strerror (res));
129         if (res == 0)
130           {
131             dbgprintf ("\t\tip %s\n", ipbuf);
132             dbgprintf ("\t\tport %s\n", portbuf);
133           }
134       }
135
136     }
137
138   freeaddrinfo (ai0);
139
140   return 0;
141 }
142
143 #define HOST1 "www.gnu.org"
144 #define SERV1 "http"
145 #define HOST2 "www.ibm.com"
146 #define SERV2 "https"
147 #define HOST3 "microsoft.com"
148 #define SERV3 "http"
149 #define HOST4 "google.org"
150 #define SERV4 "ldap"
151
152 int main (void)
153 {
154 #if _WIN32
155   {
156     WORD requested;
157     WSADATA data;
158     int err;
159
160     requested = MAKEWORD (1, 1);
161     err = WSAStartup (requested, &data);
162     if (err != 0)
163       return 1;
164
165     if (data.wVersion < requested)
166       {
167         WSACleanup ();
168         return 2;
169       }
170   }
171 #endif
172
173   return simple (HOST1, SERV1)
174     + simple (HOST2, SERV2)
175     + simple (HOST3, SERV3)
176     + simple (HOST4, SERV4);
177 }