1 /* Test the getaddrinfo module.
3 Copyright (C) 2006-2009 Free Software Foundation, Inc.
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.
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.
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/>. */
18 /* Written by Simon Josefsson. */
23 #include <arpa/inet.h>
25 #include <netinet/in.h>
29 /* Whether to print debugging messages. */
30 #define ENABLE_DEBUGGING 0
33 # define dbgprintf printf
35 # define dbgprintf if (0) printf
38 /* BeOS does not have AF_UNSPEC. */
44 # define EAI_SERVICE 0
48 simple (char const *host, char const *service)
52 struct addrinfo hints;
53 struct addrinfo *ai0, *ai;
57 /* Once we skipped the test, do not try anything else */
61 dbgprintf ("Finding %s service %s...\n", host, service);
63 /* This initializes "hints" but does not use it. Is there a reason
64 for this? If so, please fix this comment. */
65 memset (&hints, 0, sizeof (hints));
66 hints.ai_flags = AI_CANONNAME;
67 hints.ai_family = AF_UNSPEC;
68 hints.ai_socktype = SOCK_STREAM;
70 res = getaddrinfo (host, service, 0, &ai0);
73 dbgprintf ("res %d: %s\n", res, gai_strerror (res));
77 /* EAI_AGAIN is returned if no network is available. Don't fail
78 the test merely because someone is down the country on their
83 fprintf (stderr, "skipping getaddrinfo test: no network?\n");
86 /* IRIX reports EAI_NONAME for "https". Don't fail the test
87 merely because of this. */
88 if (res == EAI_NONAME)
90 /* Solaris reports EAI_SERVICE for "http" and "https". Don't
91 fail the test merely because of this. */
92 if (res == EAI_SERVICE)
95 /* AIX reports EAI_NODATA for "https". Don't fail the test
96 merely because of this. */
97 if (res == EAI_NODATA)
100 /* Provide details if errno was set. */
101 if (res == EAI_SYSTEM)
102 dbgprintf ("system error: %s\n", strerror (err));
107 for (ai = ai0; ai; ai = ai->ai_next)
109 dbgprintf ("\tflags %x\n", ai->ai_flags);
110 dbgprintf ("\tfamily %x\n", ai->ai_family);
111 dbgprintf ("\tsocktype %x\n", ai->ai_socktype);
112 dbgprintf ("\tprotocol %x\n", ai->ai_protocol);
113 dbgprintf ("\taddrlen %ld: ", (unsigned long) ai->ai_addrlen);
114 dbgprintf ("\tFound %s\n",
115 inet_ntop (ai->ai_family,
116 &((struct sockaddr_in *)
117 ai->ai_addr)->sin_addr,
118 buf, sizeof (buf) - 1));
119 if (ai->ai_canonname)
120 dbgprintf ("\tFound %s...\n", ai->ai_canonname);
124 char portbuf[BUFSIZ];
126 res = getnameinfo (ai->ai_addr, ai->ai_addrlen,
127 ipbuf, sizeof (ipbuf) - 1,
128 portbuf, sizeof (portbuf) - 1,
129 NI_NUMERICHOST|NI_NUMERICSERV);
130 dbgprintf ("\t\tgetnameinfo %d: %s\n", res, gai_strerror (res));
133 dbgprintf ("\t\tip %s\n", ipbuf);
134 dbgprintf ("\t\tport %s\n", portbuf);
145 #define HOST1 "www.gnu.org"
147 #define HOST2 "www.ibm.com"
148 #define SERV2 "https"
149 #define HOST3 "microsoft.com"
151 #define HOST4 "google.org"
162 requested = MAKEWORD (1, 1);
163 err = WSAStartup (requested, &data);
167 if (data.wVersion < requested)
175 return simple (HOST1, SERV1)
176 + simple (HOST2, SERV2)
177 + simple (HOST3, SERV3)
178 + simple (HOST4, SERV4);