Move getaddrinfo.h declarations to netdb.h.
[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 <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   struct addrinfo hints;
49   struct addrinfo *ai0, *ai;
50   int res;
51
52   dbgprintf ("Finding %s service %s...\n", host, service);
53
54   /* This initializes "hints" but does not use it.  Is there a reason
55      for this?  If so, please fix this comment.  */
56   memset (&hints, 0, sizeof (hints));
57   hints.ai_flags = AI_CANONNAME;
58   hints.ai_family = AF_UNSPEC;
59   hints.ai_socktype = SOCK_STREAM;
60
61   res = getaddrinfo (host, service, 0, &ai0);
62
63   dbgprintf ("res %d: %s\n", res, gai_strerror (res));
64
65   if (res != 0)
66     {
67       /* IRIX reports EAI_NONAME for "https".  Don't fail the test
68          merely because of this.  */
69       if (res == EAI_NONAME)
70         return 0;
71       /* Solaris reports EAI_SERVICE for "http" and "https".  Don't
72          fail the test merely because of this.  */
73       if (res == EAI_SERVICE)
74         return 0;
75       /* AIX reports EAI_NODATA for "https".  Don't fail the test
76          merely because of this.  */
77       if (res == EAI_NODATA)
78         return 0;
79
80       return 1;
81     }
82
83   for (ai = ai0; ai; ai = ai->ai_next)
84     {
85       dbgprintf ("\tflags %x\n", ai->ai_flags);
86       dbgprintf ("\tfamily %x\n", ai->ai_family);
87       dbgprintf ("\tsocktype %x\n", ai->ai_socktype);
88       dbgprintf ("\tprotocol %x\n", ai->ai_protocol);
89       dbgprintf ("\taddrlen %ld: ", (unsigned long) ai->ai_addrlen);
90       dbgprintf ("\tFound %s\n",
91                  inet_ntop (ai->ai_family,
92                             &((struct sockaddr_in *)
93                               ai->ai_addr)->sin_addr,
94                             buf, sizeof (buf) - 1));
95       if (ai->ai_canonname)
96         dbgprintf ("\tFound %s...\n", ai->ai_canonname);
97
98       {
99         char ipbuf[BUFSIZ];
100         char portbuf[BUFSIZ];
101
102         res = getnameinfo (ai->ai_addr, ai->ai_addrlen,
103                            ipbuf, sizeof (ipbuf) - 1,
104                            portbuf, sizeof (portbuf) - 1,
105                            NI_NUMERICHOST|NI_NUMERICSERV);
106         dbgprintf ("\t\tgetnameinfo %d: %s\n", res, gai_strerror (res));
107         if (res == 0)
108           {
109             dbgprintf ("\t\tip %s\n", ipbuf);
110             dbgprintf ("\t\tport %s\n", portbuf);
111           }
112       }
113
114     }
115
116   freeaddrinfo (ai0);
117
118   return 0;
119 }
120
121 #define HOST1 "www.gnu.org"
122 #define SERV1 "http"
123 #define HOST2 "www.ibm.com"
124 #define SERV2 "https"
125 #define HOST3 "microsoft.com"
126 #define SERV3 "http"
127 #define HOST4 "google.org"
128 #define SERV4 "ldap"
129
130 int main (void)
131 {
132 #if _WIN32
133   {
134     WORD requested;
135     WSADATA data;
136     int err;
137
138     requested = MAKEWORD (1, 1);
139     err = WSAStartup (requested, &data);
140     if (err != 0)
141       return 1;
142
143     if (data.wVersion < requested)
144       {
145         WSACleanup ();
146         return 2;
147       }
148   }
149 #endif
150
151   return simple (HOST1, SERV1)
152     + simple (HOST2, SERV2)
153     + simple (HOST3, SERV3)
154     + simple (HOST4, SERV4);
155 }