Port test-getaddrinfo to Solaris.
[gnulib.git] / tests / test-getaddrinfo.c
1 /* Test the getaddrinfo module.
2
3    Copyright (C) 2006-2007 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 "inet_ntop.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 int simple (char *host, char *service)
32 {
33   char buf[BUFSIZ];
34   struct addrinfo hints;
35   struct addrinfo *ai0, *ai;
36   int res;
37
38   printf ("Finding %s service %s...\n", host, service);
39
40   /* This initializes "hints" but does not use it.  Is there a reason
41      for this?  If so, please fix this comment.  */
42   memset (&hints, 0, sizeof (hints));
43   hints.ai_flags = AI_CANONNAME;
44   hints.ai_family = AF_UNSPEC;
45   hints.ai_socktype = SOCK_STREAM;
46
47   res = getaddrinfo (host, service, 0, &ai0);
48
49   printf ("res %d: %s\n", res, gai_strerror (res));
50
51   if (res != 0)
52     return 1;
53
54   for (ai = ai0; ai; ai = ai->ai_next)
55     {
56       printf ("\tflags %x\n", ai->ai_flags);
57       printf ("\tfamily %x\n", ai->ai_family);
58       printf ("\tsocktype %x\n", ai->ai_socktype);
59       printf ("\tprotocol %x\n", ai->ai_protocol);
60       printf ("\taddrlen %ld: ", (unsigned long) ai->ai_addrlen);
61       printf ("\tFound %s\n",
62               inet_ntop (ai->ai_family,
63                          &((struct sockaddr_in *)
64                           ai->ai_addr)->sin_addr,
65                          buf, sizeof (buf) - 1));
66       if (ai->ai_canonname)
67         printf ("\tFound %s...\n", ai->ai_canonname);
68
69       {
70         char ipbuf[BUFSIZ];
71         char portbuf[BUFSIZ];
72
73         res = getnameinfo (ai->ai_addr, ai->ai_addrlen,
74                            ipbuf, sizeof (ipbuf) - 1,
75                            portbuf, sizeof (portbuf) - 1,
76                            NI_NUMERICHOST|NI_NUMERICSERV);
77         printf ("\t\tgetnameinfo %d: %s\n", res, gai_strerror (res));
78         if (res == 0)
79           {
80             printf ("\t\tip %s\n", ipbuf);
81             printf ("\t\tport %s\n", portbuf);
82           }
83       }
84
85     }
86
87   freeaddrinfo (ai0);
88
89   return 0;
90 }
91
92 /* Use numbers for http and https services, rather than names, because
93    Solaris 8 /etc/services does not define these service names by
94    default.  */
95 #define HOST1 "www.gnu.org"
96 #define SERV1 "80"
97 #define HOST2 "www.ibm.com"
98 #define SERV2 "443"
99 #define HOST3 "microsoft.com"
100 #define SERV3 "80"
101 #define HOST4 "google.org"
102 #define SERV4 "ldap"
103
104 int main (void)
105 {
106 #if _WIN32
107   {
108     WORD requested;
109     WSADATA data;
110     int err;
111
112     requested = MAKEWORD (1, 1);
113     err = WSAStartup (requested, &data);
114     if (err != 0)
115       return 1;
116
117     if (data.wVersion < requested)
118       {
119         WSACleanup ();
120         return 2;
121       }
122   }
123 #endif
124
125   return simple (HOST1, SERV1)
126     + simple (HOST2, SERV2)
127     + simple (HOST3, SERV3)
128     + simple (HOST4, SERV4);
129 }