1 /* $NetBSD: h_gai.c,v 1.1 2011/01/12 02:58:40 pgoyette Exp $ */
4 * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, and 2002 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/types.h>
39 #include <sys/socket.h>
41 #include <netinet/in.h>
43 #include <arpa/inet.h>
47 char host[NI_MAXHOST];
48 char serv[NI_MAXSERV];
51 static void usage(void);
52 static void print1(const char *, const struct addrinfo *, char *, char *);
53 int main(int, char *[]);
58 fprintf(stderr, "usage: test [-f family] [-s socktype] [-p proto] [-DPRSv46] host serv\n");
62 print1(const char *title, const struct addrinfo *res, char *h, char *s)
64 const char *start, *end;
66 const int niflag = NI_NUMERICHOST;
69 error = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
70 host, sizeof(host), serv, sizeof(serv),
84 printf("%s%s", title, end);
85 printf("%sflags 0x%x%s", start, res->ai_flags, end);
86 printf("%sfamily %d%s", start, res->ai_family, end);
87 printf("%ssocktype %d%s", start, res->ai_socktype, end);
88 printf("%sprotocol %d%s", start, res->ai_protocol, end);
89 printf("%saddrlen %d%s", start, res->ai_addrlen, end);
91 printf("%serror %d%s", start, error, end);
93 printf("%shost %s%s", start, h, end);
94 printf("%sserv %s%s", start, s, end);
97 if (res->ai_canonname)
98 printf("%scname \"%s\"%s", start, res->ai_canonname, end);
106 main(int argc, char *argv[])
108 struct addrinfo *res;
116 memset(&ai, 0, sizeof(ai));
117 ai.ai_family = PF_UNSPEC;
118 ai.ai_flags |= AI_CANONNAME;
119 while ((c = getopt(argc, argv, "Df:p:PRs:Sv46")) != -1) {
122 ai.ai_socktype = SOCK_DGRAM;
125 ai.ai_family = atoi(optarg);
128 ai.ai_protocol = atoi(optarg);
131 ai.ai_flags |= AI_PASSIVE;
134 ai.ai_socktype = SOCK_RAW;
137 ai.ai_socktype = atoi(optarg);
140 ai.ai_socktype = SOCK_STREAM;
146 ai.ai_family = PF_INET;
149 ai.ai_family = PF_INET6;
164 p = *argv[0] ? argv[0] : NULL;
165 q = *argv[1] ? argv[1] : NULL;
167 strncpy(nbuf, "(empty)", sizeof(nbuf));
168 print1("arg:", &ai, p ? p : nbuf , q ? q : nbuf);
170 error = getaddrinfo(p, q, &ai, &res);
172 printf("%s\n", gai_strerror(error));
178 snprintf(nbuf, sizeof(nbuf), "ai%d:", i);
179 print1(nbuf, res, NULL, NULL);
182 } while ((res = res->ai_next) != NULL);