| | varnish-cache/bin/varnishtest/vtc_client.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2008-2011 Varnish Software AS |
2 |
|
* All rights reserved. |
3 |
|
* |
4 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
5 |
|
* |
6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
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 |
|
* |
17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
27 |
|
* SUCH DAMAGE. |
28 |
|
*/ |
29 |
|
|
30 |
|
#include "config.h" |
31 |
|
|
32 |
|
#include <sys/socket.h> |
33 |
|
#include <sys/un.h> |
34 |
|
|
35 |
|
#include <netdb.h> |
36 |
|
#include <stdio.h> |
37 |
|
#include <stdlib.h> |
38 |
|
#include <string.h> |
39 |
|
#include <poll.h> |
40 |
|
#include <unistd.h> |
41 |
|
|
42 |
|
#include "vtc.h" |
43 |
|
|
44 |
|
#include "vsa.h" |
45 |
|
#include "vss.h" |
46 |
|
#include "vtcp.h" |
47 |
|
#include "vus.h" |
48 |
|
|
49 |
|
struct client { |
50 |
|
unsigned magic; |
51 |
|
#define CLIENT_MAGIC 0x6242397c |
52 |
|
char *name; |
53 |
|
struct vtclog *vl; |
54 |
|
VTAILQ_ENTRY(client) list; |
55 |
|
struct vtc_sess *vsp; |
56 |
|
|
57 |
|
char *spec; |
58 |
|
|
59 |
|
char connect[256]; |
60 |
|
char *addr; |
61 |
|
|
62 |
|
char *proxy_spec; |
63 |
|
int proxy_version; |
64 |
|
|
65 |
|
unsigned running; |
66 |
|
pthread_t tp; |
67 |
|
}; |
68 |
|
|
69 |
|
static VTAILQ_HEAD(, client) clients = VTAILQ_HEAD_INITIALIZER(clients); |
70 |
|
|
71 |
|
/********************************************************************** |
72 |
|
* Send the proxy header |
73 |
|
*/ |
74 |
|
|
75 |
|
static void |
76 |
12 |
client_proxy(struct vtclog *vl, int fd, int version, const char *speca) |
77 |
|
{ |
78 |
|
const struct suckaddr *sac, *sas; |
79 |
|
char *spec, *save, *p; |
80 |
|
struct vsb *tlv; |
81 |
|
|
82 |
12 |
spec = strdup(speca); |
83 |
12 |
AN(spec); |
84 |
12 |
save = NULL; |
85 |
|
|
86 |
12 |
p = strtok_r(spec, " ", &save); |
87 |
12 |
AN(p); |
88 |
12 |
sac = VSS_ResolveOne(NULL, p, NULL, 0, SOCK_STREAM, AI_PASSIVE); |
89 |
12 |
if (sac == NULL) |
90 |
0 |
vtc_fatal(vl, "Could not resolve client address"); |
91 |
|
|
92 |
12 |
p = strtok_r(NULL, " ", &save); |
93 |
12 |
AN(p); |
94 |
12 |
sas = VSS_ResolveOne(NULL, p, NULL, 0, SOCK_STREAM, AI_PASSIVE); |
95 |
12 |
if (sas == NULL) |
96 |
0 |
vtc_fatal(vl, "Could not resolve client address"); |
97 |
|
|
98 |
12 |
tlv = VSB_new_auto(); |
99 |
14 |
while ((p = strtok_r(NULL, " ", &save)) != NULL) |
100 |
2 |
vtc_proxy_tlv(vl, tlv, p); |
101 |
|
|
102 |
12 |
free(spec); |
103 |
|
|
104 |
12 |
AZ(VSB_finish(tlv)); |
105 |
12 |
if (vtc_send_proxy(fd, version, sac, sas, tlv)) |
106 |
0 |
vtc_fatal(vl, "Write failed: %s", strerror(errno)); |
107 |
12 |
VSA_free(&sac); |
108 |
12 |
VSA_free(&sas); |
109 |
12 |
} |
110 |
|
|
111 |
|
/********************************************************************** |
112 |
|
* Socket connect. |
113 |
|
*/ |
114 |
|
|
115 |
|
static int |
116 |
1978 |
client_tcp_connect(struct vtclog *vl, const char *addr, double tmo, |
117 |
|
const char **errp) |
118 |
|
{ |
119 |
|
int fd; |
120 |
|
char mabuf[VTCP_ADDRBUFSIZE], mpbuf[VTCP_PORTBUFSIZE]; |
121 |
|
|
122 |
1978 |
AN(addr); |
123 |
1978 |
AN(errp); |
124 |
1978 |
fd = VTCP_open(addr, NULL, tmo, errp); |
125 |
1978 |
if (fd < 0) |
126 |
0 |
return (fd); |
127 |
1978 |
VTCP_myname(fd, mabuf, sizeof mabuf, mpbuf, sizeof mpbuf); |
128 |
3956 |
vtc_log(vl, 3, "connected fd %d from %s %s to %s", fd, mabuf, mpbuf, |
129 |
1978 |
addr); |
130 |
1978 |
return (fd); |
131 |
1978 |
} |
132 |
|
|
133 |
|
/* cf. VTCP_Open() */ |
134 |
|
static int v_matchproto_(vus_resolved_f) |
135 |
252 |
uds_open(void *priv, const struct sockaddr_un *uds) |
136 |
|
{ |
137 |
|
double *p; |
138 |
|
int s, i, tmo; |
139 |
|
struct pollfd fds[1]; |
140 |
|
socklen_t sl; |
141 |
|
|
142 |
252 |
sl = VUS_socklen(uds); |
143 |
|
|
144 |
252 |
AN(priv); |
145 |
252 |
AN(uds); |
146 |
252 |
p = priv; |
147 |
252 |
assert(*p > 0.); |
148 |
252 |
tmo = (int)(*p * 1e3); |
149 |
|
|
150 |
252 |
s = socket(uds->sun_family, SOCK_STREAM, 0); |
151 |
252 |
if (s < 0) |
152 |
0 |
return (s); |
153 |
|
|
154 |
252 |
VTCP_nonblocking(s); |
155 |
252 |
i = connect(s, (const void*)uds, sl); |
156 |
252 |
if (i == 0) |
157 |
252 |
return (s); |
158 |
0 |
if (errno != EINPROGRESS) { |
159 |
0 |
closefd(&s); |
160 |
0 |
return (-1); |
161 |
|
} |
162 |
|
|
163 |
0 |
fds[0].fd = s; |
164 |
0 |
fds[0].events = POLLWRNORM; |
165 |
0 |
fds[0].revents = 0; |
166 |
0 |
i = poll(fds, 1, tmo); |
167 |
|
|
168 |
0 |
if (i == 0) { |
169 |
0 |
closefd(&s); |
170 |
0 |
errno = ETIMEDOUT; |
171 |
0 |
return (-1); |
172 |
|
} |
173 |
|
|
174 |
0 |
return (VTCP_connected(s)); |
175 |
252 |
} |
176 |
|
|
177 |
|
static int |
178 |
252 |
client_uds_connect(struct vtclog *vl, const char *path, double tmo, |
179 |
|
const char **errp) |
180 |
|
{ |
181 |
|
int fd; |
182 |
|
|
183 |
252 |
assert(tmo >= 0); |
184 |
|
|
185 |
252 |
errno = 0; |
186 |
252 |
fd = VUS_resolver(path, uds_open, &tmo, errp); |
187 |
252 |
if (fd < 0) { |
188 |
0 |
*errp = strerror(errno); |
189 |
0 |
return (fd); |
190 |
|
} |
191 |
252 |
vtc_log(vl, 3, "connected fd %d to %s", fd, path); |
192 |
252 |
return (fd); |
193 |
252 |
} |
194 |
|
|
195 |
|
static int |
196 |
2231 |
client_connect(struct vtclog *vl, struct client *c) |
197 |
|
{ |
198 |
|
const char *err; |
199 |
|
int fd; |
200 |
|
|
201 |
2231 |
vtc_log(vl, 3, "Connect to %s", c->addr); |
202 |
2231 |
if (VUS_is(c->addr)) |
203 |
252 |
fd = client_uds_connect(vl, c->addr, 10., &err); |
204 |
|
else |
205 |
1979 |
fd = client_tcp_connect(vl, c->addr, 10., &err); |
206 |
2231 |
if (fd < 0) |
207 |
0 |
vtc_fatal(c->vl, "Failed to open %s: %s", |
208 |
0 |
c->addr, err); |
209 |
|
/* VTCP_blocking does its own checks, trust it */ |
210 |
2231 |
VTCP_blocking(fd); |
211 |
2231 |
if (c->proxy_spec != NULL) |
212 |
12 |
client_proxy(vl, fd, c->proxy_version, c->proxy_spec); |
213 |
2231 |
return (fd); |
214 |
|
} |
215 |
|
|
216 |
|
/********************************************************************** |
217 |
|
* Client thread |
218 |
|
*/ |
219 |
|
|
220 |
|
static int |
221 |
2231 |
client_conn(void *priv, struct vtclog *vl) |
222 |
|
{ |
223 |
|
struct client *c; |
224 |
|
|
225 |
2231 |
CAST_OBJ_NOTNULL(c, priv, CLIENT_MAGIC); |
226 |
2231 |
return (client_connect(vl, c)); |
227 |
|
} |
228 |
|
|
229 |
|
static void |
230 |
2231 |
client_disc(void *priv, struct vtclog *vl, int *fdp) |
231 |
|
{ |
232 |
2231 |
(void)priv; |
233 |
2231 |
vtc_log(vl, 3, "closing fd %d", *fdp); |
234 |
2231 |
VTCP_close(fdp); |
235 |
2231 |
} |
236 |
|
|
237 |
|
/********************************************************************** |
238 |
|
* Allocate and initialize a client |
239 |
|
*/ |
240 |
|
|
241 |
|
static struct client * |
242 |
1151 |
client_new(const char *name) |
243 |
|
{ |
244 |
|
struct client *c; |
245 |
|
|
246 |
1151 |
ALLOC_OBJ(c, CLIENT_MAGIC); |
247 |
1151 |
AN(c); |
248 |
1151 |
REPLACE(c->name, name); |
249 |
1151 |
c->vl = vtc_logopen("%s", name); |
250 |
1151 |
AN(c->vl); |
251 |
1151 |
c->vsp = Sess_New(c->vl, name); |
252 |
1151 |
AN(c->vsp); |
253 |
|
|
254 |
1151 |
bprintf(c->connect, "%s", "${v1_sock}"); |
255 |
1151 |
VTAILQ_INSERT_TAIL(&clients, c, list); |
256 |
1151 |
return (c); |
257 |
|
} |
258 |
|
|
259 |
|
/********************************************************************** |
260 |
|
* Clean up client |
261 |
|
*/ |
262 |
|
|
263 |
|
static void |
264 |
1151 |
client_delete(struct client *c) |
265 |
|
{ |
266 |
|
|
267 |
1151 |
CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC); |
268 |
1151 |
Sess_Destroy(&c->vsp); |
269 |
1151 |
vtc_logclose(c->vl); |
270 |
1151 |
free(c->spec); |
271 |
1151 |
free(c->name); |
272 |
1151 |
free(c->addr); |
273 |
1151 |
free(c->proxy_spec); |
274 |
|
/* XXX: MEMLEAK (?)*/ |
275 |
1151 |
FREE_OBJ(c); |
276 |
1151 |
} |
277 |
|
|
278 |
|
/********************************************************************** |
279 |
|
* Start the client thread |
280 |
|
*/ |
281 |
|
|
282 |
|
static void |
283 |
1774 |
client_start(struct client *c) |
284 |
|
{ |
285 |
|
struct vsb *vsb; |
286 |
|
|
287 |
1774 |
CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC); |
288 |
1774 |
vtc_log(c->vl, 2, "Starting client"); |
289 |
1774 |
c->running = 1; |
290 |
1774 |
vsb = macro_expand(c->vl, c->connect); |
291 |
1774 |
AN(vsb); |
292 |
1774 |
REPLACE(c->addr, VSB_data(vsb)); |
293 |
1774 |
VSB_destroy(&vsb); |
294 |
1774 |
c->tp = Sess_Start_Thread( |
295 |
1774 |
c, |
296 |
1774 |
c->vsp, |
297 |
|
client_conn, |
298 |
|
client_disc, |
299 |
1774 |
c->addr, |
300 |
|
NULL, |
301 |
1774 |
c->spec |
302 |
|
); |
303 |
1774 |
} |
304 |
|
|
305 |
|
/********************************************************************** |
306 |
|
* Wait for client thread to stop |
307 |
|
*/ |
308 |
|
|
309 |
|
static void |
310 |
1774 |
client_wait(struct client *c) |
311 |
|
{ |
312 |
|
void *res; |
313 |
|
|
314 |
1774 |
CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC); |
315 |
1774 |
vtc_log(c->vl, 2, "Waiting for client"); |
316 |
1774 |
PTOK(pthread_join(c->tp, &res)); |
317 |
1774 |
if (res != NULL) |
318 |
0 |
vtc_fatal(c->vl, "Client returned \"%s\"", (char *)res); |
319 |
1774 |
c->tp = 0; |
320 |
1774 |
c->running = 0; |
321 |
1774 |
} |
322 |
|
|
323 |
|
/********************************************************************** |
324 |
|
* Run the client thread |
325 |
|
*/ |
326 |
|
|
327 |
|
static void |
328 |
1577 |
client_run(struct client *c) |
329 |
|
{ |
330 |
|
|
331 |
1577 |
client_start(c); |
332 |
1577 |
client_wait(c); |
333 |
1577 |
} |
334 |
|
|
335 |
|
|
336 |
|
/********************************************************************** |
337 |
|
* Client command dispatch |
338 |
|
*/ |
339 |
|
|
340 |
|
void |
341 |
3046 |
cmd_client(CMD_ARGS) |
342 |
|
{ |
343 |
|
struct client *c, *c2; |
344 |
|
|
345 |
3046 |
(void)priv; |
346 |
|
|
347 |
3046 |
if (av == NULL) { |
348 |
|
/* Reset and free */ |
349 |
2164 |
VTAILQ_FOREACH_SAFE(c, &clients, list, c2) { |
350 |
1151 |
VTAILQ_REMOVE(&clients, c, list); |
351 |
1151 |
if (c->tp != 0) |
352 |
21 |
client_wait(c); |
353 |
1151 |
client_delete(c); |
354 |
1151 |
} |
355 |
1013 |
return; |
356 |
|
} |
357 |
|
|
358 |
2033 |
AZ(strcmp(av[0], "client")); |
359 |
2033 |
av++; |
360 |
|
|
361 |
2033 |
VTC_CHECK_NAME(vl, av[0], "Client", 'c'); |
362 |
2957 |
VTAILQ_FOREACH(c, &clients, list) |
363 |
1806 |
if (!strcmp(c->name, av[0])) |
364 |
882 |
break; |
365 |
2033 |
if (c == NULL) |
366 |
1151 |
c = client_new(av[0]); |
367 |
2033 |
av++; |
368 |
|
|
369 |
5904 |
for (; *av != NULL; av++) { |
370 |
3871 |
if (vtc_error) |
371 |
0 |
break; |
372 |
|
|
373 |
3871 |
if (!strcmp(*av, "-wait")) { |
374 |
175 |
client_wait(c); |
375 |
175 |
continue; |
376 |
|
} |
377 |
|
|
378 |
|
/* Don't muck about with a running client */ |
379 |
3696 |
if (c->running) |
380 |
1 |
client_wait(c); |
381 |
|
|
382 |
3696 |
AZ(c->running); |
383 |
3696 |
if (Sess_GetOpt(c->vsp, &av)) |
384 |
33 |
continue; |
385 |
|
|
386 |
3663 |
if (!strcmp(*av, "-connect")) { |
387 |
148 |
bprintf(c->connect, "%s", av[1]); |
388 |
148 |
av++; |
389 |
148 |
continue; |
390 |
|
} |
391 |
3515 |
if (!strcmp(*av, "-proxy1")) { |
392 |
8 |
REPLACE(c->proxy_spec, av[1]); |
393 |
8 |
c->proxy_version = 1; |
394 |
8 |
av++; |
395 |
8 |
continue; |
396 |
|
} |
397 |
3507 |
if (!strcmp(*av, "-proxy2")) { |
398 |
4 |
REPLACE(c->proxy_spec, av[1]); |
399 |
4 |
c->proxy_version = 2; |
400 |
4 |
av++; |
401 |
4 |
continue; |
402 |
|
} |
403 |
3503 |
if (!strcmp(*av, "-start")) { |
404 |
197 |
client_start(c); |
405 |
197 |
continue; |
406 |
|
} |
407 |
3306 |
if (!strcmp(*av, "-run")) { |
408 |
1577 |
client_run(c); |
409 |
1577 |
continue; |
410 |
|
} |
411 |
1729 |
if (**av == '-') |
412 |
0 |
vtc_fatal(c->vl, "Unknown client argument: %s", *av); |
413 |
1729 |
REPLACE(c->spec, *av); |
414 |
1729 |
} |
415 |
3046 |
} |