| | 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 |
1993 |
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 |
1993 |
AN(addr); |
| 123 |
1993 |
AN(errp); |
| 124 |
1993 |
fd = VTCP_open(addr, NULL, tmo, errp); |
| 125 |
1993 |
if (fd < 0) |
| 126 |
0 |
return (fd); |
| 127 |
1993 |
VTCP_myname(fd, mabuf, sizeof mabuf, mpbuf, sizeof mpbuf); |
| 128 |
3986 |
vtc_log(vl, 3, "connected fd %d from %s %s to %s", fd, mabuf, mpbuf, |
| 129 |
1993 |
addr); |
| 130 |
1993 |
return (fd); |
| 131 |
1993 |
} |
| 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 |
2246 |
client_connect(struct vtclog *vl, struct client *c) |
| 197 |
|
{ |
| 198 |
|
const char *err; |
| 199 |
|
int fd; |
| 200 |
|
|
| 201 |
2246 |
vtc_log(vl, 3, "Connect to %s", c->addr); |
| 202 |
2246 |
if (VUS_is(c->addr)) |
| 203 |
252 |
fd = client_uds_connect(vl, c->addr, 10., &err); |
| 204 |
|
else |
| 205 |
1994 |
fd = client_tcp_connect(vl, c->addr, 10., &err); |
| 206 |
2246 |
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 |
2246 |
VTCP_blocking(fd); |
| 211 |
2246 |
if (c->proxy_spec != NULL) |
| 212 |
12 |
client_proxy(vl, fd, c->proxy_version, c->proxy_spec); |
| 213 |
2246 |
return (fd); |
| 214 |
|
} |
| 215 |
|
|
| 216 |
|
/********************************************************************** |
| 217 |
|
* Client thread |
| 218 |
|
*/ |
| 219 |
|
|
| 220 |
|
static int |
| 221 |
2246 |
client_conn(void *priv, struct vtclog *vl) |
| 222 |
|
{ |
| 223 |
|
struct client *c; |
| 224 |
|
|
| 225 |
2246 |
CAST_OBJ_NOTNULL(c, priv, CLIENT_MAGIC); |
| 226 |
2246 |
return (client_connect(vl, c)); |
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
static void |
| 230 |
2245 |
client_disc(void *priv, struct vtclog *vl, int *fdp) |
| 231 |
|
{ |
| 232 |
2245 |
(void)priv; |
| 233 |
2245 |
vtc_log(vl, 3, "closing fd %d", *fdp); |
| 234 |
2245 |
VTCP_close(fdp); |
| 235 |
2245 |
} |
| 236 |
|
|
| 237 |
|
/********************************************************************** |
| 238 |
|
* Allocate and initialize a client |
| 239 |
|
*/ |
| 240 |
|
|
| 241 |
|
static struct client * |
| 242 |
1162 |
client_new(const char *name) |
| 243 |
|
{ |
| 244 |
|
struct client *c; |
| 245 |
|
|
| 246 |
1162 |
ALLOC_OBJ(c, CLIENT_MAGIC); |
| 247 |
1162 |
AN(c); |
| 248 |
1162 |
REPLACE(c->name, name); |
| 249 |
1162 |
c->vl = vtc_logopen("%s", name); |
| 250 |
1162 |
AN(c->vl); |
| 251 |
1162 |
c->vsp = Sess_New(c->vl, name); |
| 252 |
1162 |
AN(c->vsp); |
| 253 |
|
|
| 254 |
1162 |
bprintf(c->connect, "%s", "${v1_sock}"); |
| 255 |
1162 |
VTAILQ_INSERT_TAIL(&clients, c, list); |
| 256 |
1162 |
return (c); |
| 257 |
|
} |
| 258 |
|
|
| 259 |
|
/********************************************************************** |
| 260 |
|
* Clean up client |
| 261 |
|
*/ |
| 262 |
|
|
| 263 |
|
static void |
| 264 |
1162 |
client_delete(struct client *c) |
| 265 |
|
{ |
| 266 |
|
|
| 267 |
1162 |
CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC); |
| 268 |
1162 |
Sess_Destroy(&c->vsp); |
| 269 |
1162 |
vtc_logclose(c->vl); |
| 270 |
1162 |
free(c->spec); |
| 271 |
1162 |
free(c->name); |
| 272 |
1162 |
free(c->addr); |
| 273 |
1162 |
free(c->proxy_spec); |
| 274 |
|
/* XXX: MEMLEAK (?)*/ |
| 275 |
1162 |
FREE_OBJ(c); |
| 276 |
1162 |
} |
| 277 |
|
|
| 278 |
|
/********************************************************************** |
| 279 |
|
* Start the client thread |
| 280 |
|
*/ |
| 281 |
|
|
| 282 |
|
static void |
| 283 |
1787 |
client_start(struct client *c) |
| 284 |
|
{ |
| 285 |
|
struct vsb *vsb; |
| 286 |
|
|
| 287 |
1787 |
CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC); |
| 288 |
1787 |
vtc_log(c->vl, 2, "Starting client"); |
| 289 |
1787 |
c->running = 1; |
| 290 |
1787 |
vsb = macro_expand(c->vl, c->connect); |
| 291 |
1787 |
AN(vsb); |
| 292 |
1787 |
REPLACE(c->addr, VSB_data(vsb)); |
| 293 |
1787 |
VSB_destroy(&vsb); |
| 294 |
1787 |
c->tp = Sess_Start_Thread( |
| 295 |
1787 |
c, |
| 296 |
1787 |
c->vsp, |
| 297 |
|
client_conn, |
| 298 |
|
client_disc, |
| 299 |
1787 |
c->addr, |
| 300 |
|
NULL, |
| 301 |
1787 |
c->spec |
| 302 |
|
); |
| 303 |
1787 |
} |
| 304 |
|
|
| 305 |
|
/********************************************************************** |
| 306 |
|
* Wait for client thread to stop |
| 307 |
|
*/ |
| 308 |
|
|
| 309 |
|
static void |
| 310 |
1787 |
client_wait(struct client *c) |
| 311 |
|
{ |
| 312 |
|
void *res; |
| 313 |
|
|
| 314 |
1787 |
CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC); |
| 315 |
1787 |
vtc_log(c->vl, 2, "Waiting for client"); |
| 316 |
1787 |
PTOK(pthread_join(c->tp, &res)); |
| 317 |
1787 |
if (res != NULL) |
| 318 |
0 |
vtc_fatal(c->vl, "Client returned \"%s\"", (char *)res); |
| 319 |
1787 |
c->tp = 0; |
| 320 |
1787 |
c->running = 0; |
| 321 |
1787 |
} |
| 322 |
|
|
| 323 |
|
/********************************************************************** |
| 324 |
|
* Run the client thread |
| 325 |
|
*/ |
| 326 |
|
|
| 327 |
|
static void |
| 328 |
1589 |
client_run(struct client *c) |
| 329 |
|
{ |
| 330 |
|
|
| 331 |
1589 |
client_start(c); |
| 332 |
1589 |
client_wait(c); |
| 333 |
1589 |
} |
| 334 |
|
|
| 335 |
|
|
| 336 |
|
/********************************************************************** |
| 337 |
|
* Client command dispatch |
| 338 |
|
*/ |
| 339 |
|
|
| 340 |
|
void |
| 341 |
3070 |
cmd_client(CMD_ARGS) |
| 342 |
|
{ |
| 343 |
|
struct client *c, *c2; |
| 344 |
|
|
| 345 |
3070 |
(void)priv; |
| 346 |
|
|
| 347 |
3070 |
if (av == NULL) { |
| 348 |
|
/* Reset and free */ |
| 349 |
2185 |
VTAILQ_FOREACH_SAFE(c, &clients, list, c2) { |
| 350 |
1162 |
VTAILQ_REMOVE(&clients, c, list); |
| 351 |
1162 |
if (c->tp != 0) |
| 352 |
21 |
client_wait(c); |
| 353 |
1162 |
client_delete(c); |
| 354 |
1162 |
} |
| 355 |
1023 |
return; |
| 356 |
|
} |
| 357 |
|
|
| 358 |
2047 |
AZ(strcmp(av[0], "client")); |
| 359 |
2047 |
av++; |
| 360 |
|
|
| 361 |
2047 |
VTC_CHECK_NAME(vl, av[0], "Client", 'c'); |
| 362 |
2977 |
VTAILQ_FOREACH(c, &clients, list) |
| 363 |
1815 |
if (!strcmp(c->name, av[0])) |
| 364 |
885 |
break; |
| 365 |
2047 |
if (c == NULL) |
| 366 |
1162 |
c = client_new(av[0]); |
| 367 |
2047 |
av++; |
| 368 |
|
|
| 369 |
5947 |
for (; *av != NULL; av++) { |
| 370 |
3900 |
if (vtc_error) |
| 371 |
0 |
break; |
| 372 |
|
|
| 373 |
3900 |
if (!strcmp(*av, "-wait")) { |
| 374 |
176 |
client_wait(c); |
| 375 |
176 |
continue; |
| 376 |
|
} |
| 377 |
|
|
| 378 |
|
/* Don't muck about with a running client */ |
| 379 |
3724 |
if (c->running) |
| 380 |
1 |
client_wait(c); |
| 381 |
|
|
| 382 |
3724 |
AZ(c->running); |
| 383 |
3724 |
if (Sess_GetOpt(c->vsp, &av)) |
| 384 |
34 |
continue; |
| 385 |
|
|
| 386 |
3690 |
if (!strcmp(*av, "-connect")) { |
| 387 |
150 |
bprintf(c->connect, "%s", av[1]); |
| 388 |
150 |
av++; |
| 389 |
150 |
continue; |
| 390 |
|
} |
| 391 |
3540 |
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 |
3532 |
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 |
3528 |
if (!strcmp(*av, "-start")) { |
| 404 |
198 |
client_start(c); |
| 405 |
198 |
continue; |
| 406 |
|
} |
| 407 |
3330 |
if (!strcmp(*av, "-run")) { |
| 408 |
1589 |
client_run(c); |
| 409 |
1589 |
continue; |
| 410 |
|
} |
| 411 |
1741 |
if (**av == '-') |
| 412 |
0 |
vtc_fatal(c->vl, "Unknown client argument: %s", *av); |
| 413 |
1741 |
REPLACE(c->spec, *av); |
| 414 |
1741 |
} |
| 415 |
3070 |
} |