getaddrinfo.h: handle undefined EAI_NODATA
[gnulib.git] / tests / test-getaddrinfo.c
1 /* Test the getaddrinfo module.
2
3    Copyright (C) 2006-2008 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 "getaddrinfo.h"
22 #include <arpa/inet.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 /* BeOS does not have AF_UNSPEC.  */
27 #ifndef AF_UNSPEC
28 # define AF_UNSPEC 0
29 #endif
30
31 #ifndef EAI_SERVICE
32 # define EAI_SERVICE 0
33 #endif
34
35 int simple (char *host, char *service)
36 {
37   char buf[BUFSIZ];
38   struct addrinfo hints;
39   struct addrinfo *ai0, *ai;
40   int res;
41
42   printf ("Finding %s service %s...\n", host, service);
43
44   /* This initializes "hints" but does not use it.  Is there a reason
45      for this?  If so, please fix this comment.  */
46   memset (&hints, 0, sizeof (hints));
47   hints.ai_flags = AI_CANONNAME;
48   hints.ai_family = AF_UNSPEC;
49   hints.ai_socktype = SOCK_STREAM;
50
51   res = getaddrinfo (host, service, 0, &ai0);
52
53   printf ("res %d: %s\n", res, gai_strerror (res));
54
55   if (res != 0)
56     {
57       /* IRIX reports EAI_NONAME for "https".  Don't fail the test
58          merely because of this.  */
59       if (res == EAI_NONAME)
60         return 0;
61       /* Solaris reports EAI_SERVICE for "http" and "https".  Don't
62          fail the test merely because of this.  */
63       if (res == EAI_SERVICE)
64         return 0;
65       /* AIX reports EAI_NODATA for "https".  Don't fail the test
66          merely because of this.  */
67       if (res == EAI_NODATA)
68         return 0;
69
70       return 1;
71     }
72
73   for (ai = ai0; ai; ai = ai->ai_next)
74     {
75       printf ("\tflags %x\n", ai->ai_flags);
76       printf ("\tfamily %x\n", ai->ai_family);
77       printf ("\tsocktype %x\n", ai->ai_socktype);
78       printf ("\tprotocol %x\n", ai->ai_protocol);
79       printf ("\taddrlen %ld: ", (unsigned long) ai->ai_addrlen);
80       printf ("\tFound %s\n",
81               inet_ntop (ai->ai_family,
82                          &((struct sockaddr_in *)
83                           ai->ai_addr)->sin_addr,
84                          buf, sizeof (buf) - 1));
85       if (ai->ai_canonname)
86         printf ("\tFound %s...\n", ai->ai_canonname);
87
88       {
89         char ipbuf[BUFSIZ];
90         char portbuf[BUFSIZ];
91
92         res = getnameinfo (ai->ai_addr, ai->ai_addrlen,
93                            ipbuf, sizeof (ipbuf) - 1,
94                            portbuf, sizeof (portbuf) - 1,
95                            NI_NUMERICHOST|NI_NUMERICSERV);
96         printf ("\t\tgetnameinfo %d: %s\n", res, gai_strerror (res));
97         if (res == 0)
98           {
99             printf ("\t\tip %s\n", ipbuf);
100             printf ("\t\tport %s\n", portbuf);
101           }
102       }
103
104     }
105
106   freeaddrinfo (ai0);
107
108   return 0;
109 }
110
111 #define HOST1 "www.gnu.org"
112 #define SERV1 "http"
113 #define HOST2 "www.ibm.com"
114 #define SERV2 "https"
115 #define HOST3 "microsoft.com"
116 #define SERV3 "http"
117 #define HOST4 "google.org"
118 #define SERV4 "ldap"
119
120 int main (void)
121 {
122 #if _WIN32
123   {
124     WORD requested;
125     WSADATA data;
126     int err;
127
128     requested = MAKEWORD (1, 1);
129     err = WSAStartup (requested, &data);
130     if (err != 0)
131       return 1;
132
133     if (data.wVersion < requested)
134       {
135         WSACleanup ();
136         return 2;
137       }
138   }
139 #endif
140
141   return simple (HOST1, SERV1)
142     + simple (HOST2, SERV2)
143     + simple (HOST3, SERV3)
144     + simple (HOST4, SERV4);
145 }