diff options
Diffstat (limited to 'src/regress/lib/libc/getaddrinfo/gaitest.c')
-rw-r--r-- | src/regress/lib/libc/getaddrinfo/gaitest.c | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/src/regress/lib/libc/getaddrinfo/gaitest.c b/src/regress/lib/libc/getaddrinfo/gaitest.c new file mode 100644 index 0000000000..a54681b157 --- /dev/null +++ b/src/regress/lib/libc/getaddrinfo/gaitest.c | |||
@@ -0,0 +1,186 @@ | |||
1 | /* $OpenBSD: gaitest.c,v 1.3 2003/09/02 23:52:16 david Exp $ */ | ||
2 | /* $NetBSD: gaitest.c,v 1.3 2002/07/05 15:47:43 itojun Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, and 2002 WIDE Project. | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * 3. Neither the name of the project nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #include <sys/types.h> | ||
34 | #include <sys/socket.h> | ||
35 | #include <netinet/in.h> | ||
36 | #include <arpa/inet.h> | ||
37 | #include <netdb.h> | ||
38 | #include <stdio.h> | ||
39 | #include <stdlib.h> | ||
40 | #include <string.h> | ||
41 | #include <unistd.h> | ||
42 | |||
43 | struct addrinfo ai; | ||
44 | |||
45 | char host[NI_MAXHOST]; | ||
46 | char serv[NI_MAXSERV]; | ||
47 | int vflag = 0; | ||
48 | |||
49 | static void usage(void); | ||
50 | static void print1(const char *, const struct addrinfo *, char *, char *); | ||
51 | int main(int, char *[]); | ||
52 | |||
53 | static void | ||
54 | usage() | ||
55 | { | ||
56 | fprintf(stderr, "usage: test [-f family] [-s socktype] [-p proto] [-DPRSv46] host serv\n"); | ||
57 | } | ||
58 | |||
59 | static void | ||
60 | print1(title, res, h, s) | ||
61 | const char *title; | ||
62 | const struct addrinfo *res; | ||
63 | char *h; | ||
64 | char *s; | ||
65 | { | ||
66 | char *start, *end; | ||
67 | int error; | ||
68 | const int niflag = NI_NUMERICHOST; | ||
69 | |||
70 | if (res->ai_addr) { | ||
71 | error = getnameinfo(res->ai_addr, res->ai_addr->sa_len, | ||
72 | host, sizeof(host), serv, sizeof(serv), | ||
73 | niflag); | ||
74 | h = host; | ||
75 | s = serv; | ||
76 | } else | ||
77 | error = 0; | ||
78 | |||
79 | if (vflag) { | ||
80 | start = "\t"; | ||
81 | end = "\n"; | ||
82 | } else { | ||
83 | start = " "; | ||
84 | end = ""; | ||
85 | } | ||
86 | printf("%s%s", title, end); | ||
87 | printf("%sflags 0x%x%s", start, res->ai_flags, end); | ||
88 | printf("%sfamily %d%s", start, res->ai_family, end); | ||
89 | printf("%ssocktype %d%s", start, res->ai_socktype, end); | ||
90 | printf("%sprotocol %d%s", start, res->ai_protocol, end); | ||
91 | printf("%saddrlen %d%s", start, res->ai_addrlen, end); | ||
92 | if (error) | ||
93 | printf("%serror %d%s", start, error, end); | ||
94 | else { | ||
95 | printf("%shost %s%s", start, h, end); | ||
96 | printf("%sserv %s%s", start, s, end); | ||
97 | } | ||
98 | if (res->ai_canonname) | ||
99 | printf("%scname \"%s\"%s", start, res->ai_canonname, end); | ||
100 | if (!vflag) | ||
101 | printf("\n"); | ||
102 | |||
103 | } | ||
104 | |||
105 | int | ||
106 | main(argc, argv) | ||
107 | int argc; | ||
108 | char *argv[]; | ||
109 | { | ||
110 | struct addrinfo *res; | ||
111 | int error, i; | ||
112 | char *p, *q; | ||
113 | extern int optind; | ||
114 | extern char *optarg; | ||
115 | int c; | ||
116 | char nbuf[10]; | ||
117 | |||
118 | memset(&ai, 0, sizeof(ai)); | ||
119 | ai.ai_family = PF_UNSPEC; | ||
120 | ai.ai_flags |= AI_CANONNAME; | ||
121 | while ((c = getopt(argc, argv, "Df:p:PRs:Sv46")) != -1) { | ||
122 | switch (c) { | ||
123 | case 'D': | ||
124 | ai.ai_socktype = SOCK_DGRAM; | ||
125 | break; | ||
126 | case 'f': | ||
127 | ai.ai_family = atoi(optarg); | ||
128 | break; | ||
129 | case 'p': | ||
130 | ai.ai_protocol = atoi(optarg); | ||
131 | break; | ||
132 | case 'P': | ||
133 | ai.ai_flags |= AI_PASSIVE; | ||
134 | break; | ||
135 | case 'R': | ||
136 | ai.ai_socktype = SOCK_RAW; | ||
137 | break; | ||
138 | case 's': | ||
139 | ai.ai_socktype = atoi(optarg); | ||
140 | break; | ||
141 | case 'S': | ||
142 | ai.ai_socktype = SOCK_STREAM; | ||
143 | break; | ||
144 | case 'v': | ||
145 | vflag++; | ||
146 | break; | ||
147 | case '4': | ||
148 | ai.ai_family = PF_INET; | ||
149 | break; | ||
150 | case '6': | ||
151 | ai.ai_family = PF_INET6; | ||
152 | break; | ||
153 | default: | ||
154 | usage(); | ||
155 | exit(1); | ||
156 | } | ||
157 | } | ||
158 | argc -= optind; | ||
159 | argv += optind; | ||
160 | |||
161 | if (argc != 2){ | ||
162 | usage(); | ||
163 | exit(1); | ||
164 | } | ||
165 | |||
166 | p = *argv[0] ? argv[0] : NULL; | ||
167 | q = *argv[1] ? argv[1] : NULL; | ||
168 | |||
169 | print1("arg:", &ai, p ? p : "(empty)", q ? q : "(empty)"); | ||
170 | |||
171 | error = getaddrinfo(p, q, &ai, &res); | ||
172 | if (error) { | ||
173 | printf("%s\n", gai_strerror(error)); | ||
174 | exit(1); | ||
175 | } | ||
176 | |||
177 | i = 1; | ||
178 | do { | ||
179 | snprintf(nbuf, sizeof(nbuf), "ai%d:", i); | ||
180 | print1(nbuf, res, NULL, NULL); | ||
181 | |||
182 | i++; | ||
183 | } while ((res = res->ai_next) != NULL); | ||
184 | |||
185 | exit(0); | ||
186 | } | ||