1 /* Test the getaddrinfo module.
3 Copyright (C) 2006-2007 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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /* Written by Simon Josefsson. */
22 #include "getaddrinfo.h"
23 #include "inet_ntop.h"
27 /* BeOS does not have AF_UNSPEC. */
32 int simple (char *host, char *service)
35 struct addrinfo hints;
36 struct addrinfo *ai0, *ai;
39 printf ("Finding %s service %s...\n", host, service);
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);
47 printf ("res %d: %s\n", res, gai_strerror (res));
52 for (ai = ai0; ai; ai = ai->ai_next)
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));
65 printf ("\tFound %s...\n", ai->ai_canonname);
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));
78 printf ("\t\tip %s\n", ipbuf);
79 printf ("\t\tport %s\n", portbuf);
90 #define HOST1 "www.gnu.org"
92 #define HOST2 "www.ibm.com"
94 #define HOST3 "microsoft.com"
96 #define HOST4 "google.org"
107 requested = MAKEWORD (1, 1);
108 err = WSAStartup (requested, &data);
112 if (data.wVersion < requested)
120 return simple (HOST1, SERV1)
121 + simple (HOST2, SERV2)
122 + simple (HOST3, SERV3)
123 + simple (HOST4, SERV4);