diff options
author | itojun <> | 2002-07-05 15:54:30 +0000 |
---|---|---|
committer | itojun <> | 2002-07-05 15:54:30 +0000 |
commit | 52965476629f0bcdf3b871fe0b7e17e8e2da3dcf (patch) | |
tree | 99926b3e8181ec89dacb7dab9e16487a10167645 /src/regress/lib/libc/getaddrinfo/gaitest.c | |
parent | 46524c49952af72875b1e53f5bc8c3af16a4e682 (diff) | |
download | openbsd-52965476629f0bcdf3b871fe0b7e17e8e2da3dcf.tar.gz openbsd-52965476629f0bcdf3b871fe0b7e17e8e2da3dcf.tar.bz2 openbsd-52965476629f0bcdf3b871fe0b7e17e8e2da3dcf.zip |
regress for getaddrinfo/getnameinfo
Diffstat (limited to 'src/regress/lib/libc/getaddrinfo/gaitest.c')
-rw-r--r-- | src/regress/lib/libc/getaddrinfo/gaitest.c | 185 |
1 files changed, 185 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..380b709828 --- /dev/null +++ b/src/regress/lib/libc/getaddrinfo/gaitest.c | |||
@@ -0,0 +1,185 @@ | |||
1 | /* $OpenBSD: gaitest.c,v 1.1 2002/07/05 15:54:30 itojun 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 <unistd.h> | ||
41 | |||
42 | struct addrinfo ai; | ||
43 | |||
44 | char host[NI_MAXHOST]; | ||
45 | char serv[NI_MAXSERV]; | ||
46 | int vflag = 0; | ||
47 | |||
48 | static void usage __P((void)); | ||
49 | static void print1 __P((const char *, const struct addrinfo *, char *, char *)); | ||
50 | int main __P((int, char *[])); | ||
51 | |||
52 | static void | ||
53 | usage() | ||
54 | { | ||
55 | fprintf(stderr, "usage: test [-f family] [-s socktype] [-p proto] [-DPRSv46] host serv\n"); | ||
56 | } | ||
57 | |||
58 | static void | ||
59 | print1(title, res, h, s) | ||
60 | const char *title; | ||
61 | const struct addrinfo *res; | ||
62 | char *h; | ||
63 | char *s; | ||
64 | { | ||
65 | char *start, *end; | ||
66 | int error; | ||
67 | const int niflag = NI_NUMERICHOST; | ||
68 | |||
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; | ||
77 | |||
78 | if (vflag) { | ||
79 | start = "\t"; | ||
80 | end = "\n"; | ||
81 | } else { | ||
82 | start = " "; | ||
83 | end = ""; | ||
84 | } | ||
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); | ||
96 | } | ||
97 | if (res->ai_canonname) | ||
98 | printf("%scname \"%s\"%s", start, res->ai_canonname, end); | ||
99 | if (!vflag) | ||
100 | printf("\n"); | ||
101 | |||
102 | } | ||
103 | |||
104 | int | ||
105 | main(argc, argv) | ||
106 | int argc; | ||
107 | char *argv[]; | ||
108 | { | ||
109 | struct addrinfo *res; | ||
110 | int error, i; | ||
111 | char *p, *q; | ||
112 | extern int optind; | ||
113 | extern char *optarg; | ||
114 | int c; | ||
115 | char nbuf[10]; | ||
116 | |||
117 | memset(&ai, 0, sizeof(ai)); | ||
118 | ai.ai_family = PF_UNSPEC; | ||
119 | ai.ai_flags |= AI_CANONNAME; | ||
120 | while ((c = getopt(argc, argv, "Df:p:PRs:Sv46")) != -1) { | ||
121 | switch (c) { | ||
122 | case 'D': | ||
123 | ai.ai_socktype = SOCK_DGRAM; | ||
124 | break; | ||
125 | case 'f': | ||
126 | ai.ai_family = atoi(optarg); | ||
127 | break; | ||
128 | case 'p': | ||
129 | ai.ai_protocol = atoi(optarg); | ||
130 | break; | ||
131 | case 'P': | ||
132 | ai.ai_flags |= AI_PASSIVE; | ||
133 | break; | ||
134 | case 'R': | ||
135 | ai.ai_socktype = SOCK_RAW; | ||
136 | break; | ||
137 | case 's': | ||
138 | ai.ai_socktype = atoi(optarg); | ||
139 | break; | ||
140 | case 'S': | ||
141 | ai.ai_socktype = SOCK_STREAM; | ||
142 | break; | ||
143 | case 'v': | ||
144 | vflag++; | ||
145 | break; | ||
146 | case '4': | ||
147 | ai.ai_family = PF_INET; | ||
148 | break; | ||
149 | case '6': | ||
150 | ai.ai_family = PF_INET6; | ||
151 | break; | ||
152 | default: | ||
153 | usage(); | ||
154 | exit(1); | ||
155 | } | ||
156 | } | ||
157 | argc -= optind; | ||
158 | argv += optind; | ||
159 | |||
160 | if (argc != 2){ | ||
161 | usage(); | ||
162 | exit(1); | ||
163 | } | ||
164 | |||
165 | p = *argv[0] ? argv[0] : NULL; | ||
166 | q = *argv[1] ? argv[1] : NULL; | ||
167 | |||
168 | print1("arg:", &ai, p ? p : "(empty)", q ? q : "(empty)"); | ||
169 | |||
170 | error = getaddrinfo(p, q, &ai, &res); | ||
171 | if (error) { | ||
172 | printf("%s\n", gai_strerror(error)); | ||
173 | exit(1); | ||
174 | } | ||
175 | |||
176 | i = 1; | ||
177 | do { | ||
178 | snprintf(nbuf, sizeof(nbuf), "ai%d:", i); | ||
179 | print1(nbuf, res, NULL, NULL); | ||
180 | |||
181 | i++; | ||
182 | } while ((res = res->ai_next) != NULL); | ||
183 | |||
184 | exit(0); | ||
185 | } | ||