Make the test compile on BeOS.
[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 2, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Simon Josefsson.  */
20
21 #include "config.h"
22 #include "getaddrinfo.h"
23 #include "inet_ntop.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 int simple (char *host, char *service)
33 {
34   char buf[BUFSIZ];
35   struct addrinfo hints;
36   struct addrinfo *ai0, *ai;
37   int res;
38
39   printf ("Finding %s service %s...\n", host, service);
40
41   memset (&hints, 0, sizeof (hints));
42   hints.ai_flags = AI_CANONNAME;
43   hints.ai_family = AF_UNSPEC;
44   hints.ai_socktype = SOCK_STREAM;
45   res = getaddrinfo (host, service, 0, &ai0);
46
47   printf ("res %d: %s\n", res, gai_strerror (res));
48
49   if (res != 0)
50     return 1;
51
52   for (ai = ai0; ai; ai = ai->ai_next)
53     {
54       printf ("\tflags %x\n", ai->ai_flags);
55       printf ("\tfamily %x\n", ai->ai_family);
56       printf ("\tsocktype %x\n", ai->ai_socktype);
57       printf ("\tprotocol %x\n", ai->ai_protocol);
58       printf ("\taddrlen %ld: ", (unsigned long) ai->ai_addrlen);
59       printf ("\tFound %s\n",
60               inet_ntop (ai->ai_family,
61                          &((struct sockaddr_in *)
62                           ai->ai_addr)->sin_addr,
63                          buf, sizeof (buf) - 1));
64       if (ai->ai_canonname)
65         printf ("\tFound %s...\n", ai->ai_canonname);
66
67       {
68         char ipbuf[BUFSIZ];
69         char portbuf[BUFSIZ];
70
71         res = getnameinfo (ai->ai_addr, ai->ai_addrlen,
72                            ipbuf, sizeof (ipbuf) - 1,
73                            portbuf, sizeof (portbuf) - 1,
74                            NI_NUMERICHOST|NI_NUMERICSERV);
75         printf ("\t\tgetnameinfo %d: %s\n", res, gai_strerror (res));
76         if (res == 0)
77           {
78             printf ("\t\tip %s\n", ipbuf);
79             printf ("\t\tport %s\n", portbuf);
80           }
81       }
82
83     }
84
85   freeaddrinfo (ai0);
86
87   return 0;
88 }
89
90 #define HOST1 "www.gnu.org"
91 #define SERV1 "http"
92 #define HOST2 "www.ibm.com"
93 #define SERV2 "https"
94 #define HOST3 "microsoft.com"
95 #define SERV3 "http"
96 #define HOST4 "google.org"
97 #define SERV4 "ldap"
98
99 int main (void)
100 {
101 #if _WIN32
102   {
103     WORD requested;
104     WSADATA data;
105     int err;
106
107     requested = MAKEWORD (1, 1);
108     err = WSAStartup (requested, &data);
109     if (err != 0)
110       return 1;
111
112     if (data.wVersion < requested)
113       {
114         WSACleanup ();
115         return 2;
116       }
117   }
118 #endif
119
120   return simple (HOST1, SERV1)
121     + simple (HOST2, SERV2)
122     + simple (HOST3, SERV3)
123     + simple (HOST4, SERV4);
124 }