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