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