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 int simple (char *host, char *service)
30 struct addrinfo hints;
31 struct addrinfo *ai0, *ai;
34 printf ("Finding %s service %s...\n", host, service);
36 memset (&hints, 0, sizeof (hints));
37 hints.ai_flags = AI_CANONNAME;
38 hints.ai_family = AF_UNSPEC;
39 hints.ai_socktype = SOCK_STREAM;
40 res = getaddrinfo (host, service, 0, &ai0);
42 printf ("res %d: %s\n", res, gai_strerror (res));
47 for (ai = ai0; ai; ai = ai->ai_next)
49 printf ("\tflags %x\n", ai->ai_flags);
50 printf ("\tfamily %x\n", ai->ai_family);
51 printf ("\tsocktype %x\n", ai->ai_socktype);
52 printf ("\tprotocol %x\n", ai->ai_protocol);
53 printf ("\taddrlen %ld: ", (unsigned long) ai->ai_addrlen);
54 printf ("\tFound %s\n",
55 inet_ntop (ai->ai_family,
56 &((struct sockaddr_in *)
57 ai->ai_addr)->sin_addr,
58 buf, sizeof (buf) - 1));
60 printf ("\tFound %s...\n", ai->ai_canonname);
66 res = getnameinfo (ai->ai_addr, ai->ai_addrlen,
67 ipbuf, sizeof (ipbuf) - 1,
68 portbuf, sizeof (portbuf) - 1,
69 NI_NUMERICHOST|NI_NUMERICSERV);
70 printf ("\t\tgetnameinfo %d: %s\n", res, gai_strerror (res));
73 printf ("\t\tip %s\n", ipbuf);
74 printf ("\t\tport %s\n", portbuf);
85 #define HOST1 "www.gnu.org"
87 #define HOST2 "www.ibm.com"
89 #define HOST3 "microsoft.com"
91 #define HOST4 "google.org"
102 requested = MAKEWORD (1, 1);
103 err = WSAStartup (requested, &data);
107 if (data.wVersion < requested)
115 return simple (HOST1, SERV1)
116 + simple (HOST2, SERV2)
117 + simple (HOST3, SERV3)
118 + simple (HOST4, SERV4);