Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / lib / libc / getaddrinfo / gaitest.c
blobb4ec84f24433d47a143b5f18566086366ddeb9d7
1 /* $NetBSD: gaitest.c,v 1.5 2004/01/02 15:07:39 itojun Exp $ */
3 /*
4 * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, and 2002 WIDE Project.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
29 * SUCH DAMAGE.
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netdb.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
42 struct addrinfo ai;
44 char host[NI_MAXHOST];
45 char serv[NI_MAXSERV];
46 int vflag = 0;
48 static void usage(void);
49 static void print1(const char *, const struct addrinfo *, char *, char *);
50 int main(int, char *[]);
52 static void
53 usage()
55 fprintf(stderr, "usage: test [-f family] [-s socktype] [-p proto] [-DPRSv46] host serv\n");
58 static void
59 print1(title, res, h, s)
60 const char *title;
61 const struct addrinfo *res;
62 char *h;
63 char *s;
65 char *start, *end;
66 int error;
67 const int niflag = NI_NUMERICHOST;
69 if (res->ai_addr) {
70 error = getnameinfo(res->ai_addr, res->ai_addr->sa_len,
71 host, sizeof(host), serv, sizeof(serv),
72 niflag);
73 h = host;
74 s = serv;
75 } else
76 error = 0;
78 if (vflag) {
79 start = "\t";
80 end = "\n";
81 } else {
82 start = " ";
83 end = "";
85 printf("%s%s", title, end);
86 printf("%sflags 0x%x%s", start, res->ai_flags, end);
87 printf("%sfamily %d%s", start, res->ai_family, end);
88 printf("%ssocktype %d%s", start, res->ai_socktype, end);
89 printf("%sprotocol %d%s", start, res->ai_protocol, end);
90 printf("%saddrlen %d%s", start, res->ai_addrlen, end);
91 if (error)
92 printf("%serror %d%s", start, error, end);
93 else {
94 printf("%shost %s%s", start, h, end);
95 printf("%sserv %s%s", start, s, end);
97 #if 0
98 if (res->ai_canonname)
99 printf("%scname \"%s\"%s", start, res->ai_canonname, end);
100 #endif
101 if (!vflag)
102 printf("\n");
107 main(argc, argv)
108 int argc;
109 char *argv[];
111 struct addrinfo *res;
112 int error, i;
113 char *p, *q;
114 extern int optind;
115 extern char *optarg;
116 int c;
117 char nbuf[10];
119 memset(&ai, 0, sizeof(ai));
120 ai.ai_family = PF_UNSPEC;
121 ai.ai_flags |= AI_CANONNAME;
122 while ((c = getopt(argc, argv, "Df:p:PRs:Sv46")) != -1) {
123 switch (c) {
124 case 'D':
125 ai.ai_socktype = SOCK_DGRAM;
126 break;
127 case 'f':
128 ai.ai_family = atoi(optarg);
129 break;
130 case 'p':
131 ai.ai_protocol = atoi(optarg);
132 break;
133 case 'P':
134 ai.ai_flags |= AI_PASSIVE;
135 break;
136 case 'R':
137 ai.ai_socktype = SOCK_RAW;
138 break;
139 case 's':
140 ai.ai_socktype = atoi(optarg);
141 break;
142 case 'S':
143 ai.ai_socktype = SOCK_STREAM;
144 break;
145 case 'v':
146 vflag++;
147 break;
148 case '4':
149 ai.ai_family = PF_INET;
150 break;
151 case '6':
152 ai.ai_family = PF_INET6;
153 break;
154 default:
155 usage();
156 exit(1);
159 argc -= optind;
160 argv += optind;
162 if (argc != 2){
163 usage();
164 exit(1);
167 p = *argv[0] ? argv[0] : NULL;
168 q = *argv[1] ? argv[1] : NULL;
170 print1("arg:", &ai, p ? p : "(empty)", q ? q : "(empty)");
172 error = getaddrinfo(p, q, &ai, &res);
173 if (error) {
174 printf("%s\n", gai_strerror(error));
175 exit(1);
178 i = 1;
179 do {
180 snprintf(nbuf, sizeof(nbuf), "ai%d:", i);
181 print1(nbuf, res, NULL, NULL);
183 i++;
184 } while ((res = res->ai_next) != NULL);
186 exit(0);