Change copyright notice from GPLv2+ to GPLv3+.
[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   memset (&hints, 0, sizeof (hints));
41   hints.ai_flags = AI_CANONNAME;
42   hints.ai_family = AF_UNSPEC;
43   hints.ai_socktype = SOCK_STREAM;
44   res = getaddrinfo (host, service, 0, &ai0);
45
46   printf ("res %d: %s\n", res, gai_strerror (res));
47
48   if (res != 0)
49     return 1;
50
51   for (ai = ai0; ai; ai = ai->ai_next)
52     {
53       printf ("\tflags %x\n", ai->ai_flags);
54       printf ("\tfamily %x\n", ai->ai_family);
55       printf ("\tsocktype %x\n", ai->ai_socktype);
56       printf ("\tprotocol %x\n", ai->ai_protocol);
57       printf ("\taddrlen %ld: ", (unsigned long) ai->ai_addrlen);
58       printf ("\tFound %s\n",
59               inet_ntop (ai->ai_family,
60                          &((struct sockaddr_in *)
61                           ai->ai_addr)->sin_addr,
62                          buf, sizeof (buf) - 1));
63       if (ai->ai_canonname)
64         printf ("\tFound %s...\n", ai->ai_canonname);
65
66       {
67         char ipbuf[BUFSIZ];
68         char portbuf[BUFSIZ];
69
70         res = getnameinfo (ai->ai_addr, ai->ai_addrlen,
71                            ipbuf, sizeof (ipbuf) - 1,
72                            portbuf, sizeof (portbuf) - 1,
73                            NI_NUMERICHOST|NI_NUMERICSERV);
74         printf ("\t\tgetnameinfo %d: %s\n", res, gai_strerror (res));
75         if (res == 0)
76           {
77             printf ("\t\tip %s\n", ipbuf);
78             printf ("\t\tport %s\n", portbuf);
79           }
80       }
81
82     }
83
84   freeaddrinfo (ai0);
85
86   return 0;
87 }
88
89 #define HOST1 "www.gnu.org"
90 #define SERV1 "http"
91 #define HOST2 "www.ibm.com"
92 #define SERV2 "https"
93 #define HOST3 "microsoft.com"
94 #define SERV3 "http"
95 #define HOST4 "google.org"
96 #define SERV4 "ldap"
97
98 int main (void)
99 {
100 #if _WIN32
101   {
102     WORD requested;
103     WSADATA data;
104     int err;
105
106     requested = MAKEWORD (1, 1);
107     err = WSAStartup (requested, &data);
108     if (err != 0)
109       return 1;
110
111     if (data.wVersion < requested)
112       {
113         WSACleanup ();
114         return 2;
115       }
116   }
117 #endif
118
119   return simple (HOST1, SERV1)
120     + simple (HOST2, SERV2)
121     + simple (HOST3, SERV3)
122     + simple (HOST4, SERV4);
123 }