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 "inet_ntop.h"
26 /* BeOS does not have AF_UNSPEC. */
32 # define EAI_SERVICE 0
35 int simple (char *host, char *service)
38 struct addrinfo hints;
39 struct addrinfo *ai0, *ai;
42 printf ("Finding %s service %s...\n", host, service);
44 /* This initializes "hints" but does not use it. Is there a reason
45 for this? If so, please fix this comment. */
46 memset (&hints, 0, sizeof (hints));
47 hints.ai_flags = AI_CANONNAME;
48 hints.ai_family = AF_UNSPEC;
49 hints.ai_socktype = SOCK_STREAM;
51 res = getaddrinfo (host, service, 0, &ai0);
53 printf ("res %d: %s\n", res, gai_strerror (res));
57 /* IRIX reports EAI_NONAME for "https". Don't fail the test
58 merely because of this. */
59 if (res == EAI_NONAME)
61 /* Solaris reports EAI_SERVICE for "http" and "https". Don't
62 fail the test merely because of this. */
63 if (res == EAI_SERVICE)
69 for (ai = ai0; ai; ai = ai->ai_next)
71 printf ("\tflags %x\n", ai->ai_flags);
72 printf ("\tfamily %x\n", ai->ai_family);
73 printf ("\tsocktype %x\n", ai->ai_socktype);
74 printf ("\tprotocol %x\n", ai->ai_protocol);
75 printf ("\taddrlen %ld: ", (unsigned long) ai->ai_addrlen);
76 printf ("\tFound %s\n",
77 inet_ntop (ai->ai_family,
78 &((struct sockaddr_in *)
79 ai->ai_addr)->sin_addr,
80 buf, sizeof (buf) - 1));
82 printf ("\tFound %s...\n", ai->ai_canonname);
88 res = getnameinfo (ai->ai_addr, ai->ai_addrlen,
89 ipbuf, sizeof (ipbuf) - 1,
90 portbuf, sizeof (portbuf) - 1,
91 NI_NUMERICHOST|NI_NUMERICSERV);
92 printf ("\t\tgetnameinfo %d: %s\n", res, gai_strerror (res));
95 printf ("\t\tip %s\n", ipbuf);
96 printf ("\t\tport %s\n", portbuf);
107 #define HOST1 "www.gnu.org"
109 #define HOST2 "www.ibm.com"
110 #define SERV2 "https"
111 #define HOST3 "microsoft.com"
113 #define HOST4 "google.org"
124 requested = MAKEWORD (1, 1);
125 err = WSAStartup (requested, &data);
129 if (data.wVersion < requested)
137 return simple (HOST1, SERV1)
138 + simple (HOST2, SERV2)
139 + simple (HOST3, SERV3)
140 + simple (HOST4, SERV4);