1 /* Test the getaddrinfo module.
3 Copyright (C) 2006-2008 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. */
21 #include "getaddrinfo.h"
22 #include <arpa/inet.h>
23 #include <netinet/in.h>
27 /* BeOS does not have AF_UNSPEC. */
33 # define EAI_SERVICE 0
36 int simple (char *host, char *service)
39 struct addrinfo hints;
40 struct addrinfo *ai0, *ai;
43 printf ("Finding %s service %s...\n", host, service);
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;
52 res = getaddrinfo (host, service, 0, &ai0);
54 printf ("res %d: %s\n", res, gai_strerror (res));
58 /* IRIX reports EAI_NONAME for "https". Don't fail the test
59 merely because of this. */
60 if (res == EAI_NONAME)
62 /* Solaris reports EAI_SERVICE for "http" and "https". Don't
63 fail the test merely because of this. */
64 if (res == EAI_SERVICE)
66 /* AIX reports EAI_NODATA for "https". Don't fail the test
67 merely because of this. */
68 if (res == EAI_NODATA)
74 for (ai = ai0; ai; ai = ai->ai_next)
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));
87 printf ("\tFound %s...\n", ai->ai_canonname);
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));
100 printf ("\t\tip %s\n", ipbuf);
101 printf ("\t\tport %s\n", portbuf);
112 #define HOST1 "www.gnu.org"
114 #define HOST2 "www.ibm.com"
115 #define SERV2 "https"
116 #define HOST3 "microsoft.com"
118 #define HOST4 "google.org"
129 requested = MAKEWORD (1, 1);
130 err = WSAStartup (requested, &data);
134 if (data.wVersion < requested)
142 return simple (HOST1, SERV1)
143 + simple (HOST2, SERV2)
144 + simple (HOST3, SERV3)
145 + simple (HOST4, SERV4);