vinyl-cache/bin/vinyltest/vtest2/src/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 273
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 273
        spec = strdup(speca);
83 273
        AN(spec);
84 273
        save = NULL;
85
86 273
        p = strtok_r(spec, " ", &save);
87 273
        AN(p);
88 273
        sac = VSS_ResolveOne(NULL, p, NULL, 0, SOCK_STREAM, AI_PASSIVE);
89 273
        if (sac == NULL)
90 0
                vtc_fatal(vl, "Could not resolve client address");
91
92 273
        p = strtok_r(NULL, " ", &save);
93 273
        AN(p);
94 273
        sas = VSS_ResolveOne(NULL, p, NULL, 0, SOCK_STREAM, AI_PASSIVE);
95 273
        if (sas == NULL)
96 0
                vtc_fatal(vl, "Could not resolve client address");
97
98 273
        tlv = VSB_new_auto();
99 315
        while ((p = strtok_r(NULL, " ", &save)) != NULL)
100 42
                vtc_proxy_tlv(vl, tlv, p);
101
102 273
        free(spec);
103
104 273
        AZ(VSB_finish(tlv));
105 273
        if (vtc_send_proxy(fd, version, sac, sas, tlv))
106 0
                vtc_fatal(vl, "Write failed: %s", strerror(errno));
107 273
        VSA_free(&sac);
108 273
        VSA_free(&sas);
109 273
}
110
111
/**********************************************************************
112
 * Socket connect.
113
 */
114
115
static int
116 43888
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 43888
        AN(addr);
123 43888
        AN(errp);
124 43888
        fd = VTCP_open(addr, NULL, tmo, errp);
125 43888
        if (fd < 0)
126 0
                return (fd);
127 43888
        VTCP_myname(fd, mabuf, sizeof mabuf, mpbuf, sizeof mpbuf);
128 87776
        vtc_log(vl, 3, "connected fd %d from %s %s to %s", fd, mabuf, mpbuf,
129 43888
                addr);
130 43888
        return (fd);
131 43888
}
132
133
/* cf. VTCP_Open() */
134
static int v_matchproto_(vus_resolved_f)
135 7413
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 7413
        sl = VUS_socklen(uds);
143
144 7413
        AN(priv);
145 7413
        AN(uds);
146 7413
        p = priv;
147 7413
        assert(*p > 0.);
148 7413
        tmo = (int)(*p * 1e3);
149
150 7413
        s = socket(uds->sun_family, SOCK_STREAM, 0);
151 7413
        if (s < 0)
152 0
                return (s);
153
154 7413
        VTCP_nonblocking(s);
155 7413
        i = connect(s, (const void*)uds, sl);
156 7413
        if (i == 0)
157 7413
                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 7413
}
176
177
static int
178 7413
client_uds_connect(struct vtclog *vl, const char *path, double tmo,
179
                   const char **errp)
180
{
181
        int fd;
182
183 7413
        assert(tmo >= 0);
184
185 7413
        errno = 0;
186 7413
        fd = VUS_resolver(path, uds_open, &tmo, errp);
187 7413
        if (fd < 0) {
188 0
                *errp = strerror(errno);
189 0
                return (fd);
190
        }
191 7413
        vtc_log(vl, 3, "connected fd %d to %s", fd, path);
192 7413
        return (fd);
193 7413
}
194
195
static int
196 51303
client_connect(struct vtclog *vl, struct client *c)
197
{
198
        const char *err;
199
        int fd;
200
201 51303
        vtc_log(vl, 3, "Connect to %s", c->addr);
202 51303
        if (VUS_is(c->addr))
203 7413
                fd = client_uds_connect(vl, c->addr, 10., &err);
204
        else
205 43890
                fd = client_tcp_connect(vl, c->addr, 10., &err);
206 51303
        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 51303
        VTCP_blocking(fd);
211 51303
        if (c->proxy_spec != NULL)
212 273
                client_proxy(vl, fd, c->proxy_version, c->proxy_spec);
213 51303
        return (fd);
214
}
215
216
/**********************************************************************
217
 * Client thread
218
 */
219
220
static int
221 51303
client_conn(void *priv, struct vtclog *vl)
222
{
223
        struct client *c;
224
225 51303
        CAST_OBJ_NOTNULL(c, priv, CLIENT_MAGIC);
226 51303
        return (client_connect(vl, c));
227
}
228
229
static void
230 51287
client_disc(void *priv, struct vtclog *vl, int *fdp)
231
{
232 51287
        (void)priv;
233 51287
        vtc_log(vl, 3, "closing fd %d", *fdp);
234 0
        VTCP_close(fdp);
235 0
}
236 51287
237 51287
/**********************************************************************
238
 * Allocate and initialize a client
239 51287
 */
240
241
static struct client *
242
client_new(const char *name)
243
{
244
        struct client *c;
245
246 28434
        ALLOC_OBJ(c, CLIENT_MAGIC);
247
        AN(c);
248
        REPLACE(c->name, name);
249
        c->vl = vtc_logopen("%s", name);
250 28434
        AN(c->vl);
251 28434
        c->vsp = Sess_New(c->vl, name);
252 28434
        AN(c->vsp);
253 28434
254 28434
        bprintf(c->connect, "%s", "${v1_sock}");
255 28434
        VTAILQ_INSERT_TAIL(&clients, c, list);
256 28434
        return (c);
257
}
258 28434
259 28434
/**********************************************************************
260 28434
 * Clean up client
261
 */
262
263
static void
264
client_delete(struct client *c)
265
{
266
267
        CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC);
268 28434
        Sess_Destroy(&c->vsp);
269
        vtc_logclose(c->vl);
270
        free(c->spec);
271 28434
        free(c->name);
272 28434
        free(c->addr);
273 28434
        free(c->proxy_spec);
274 28434
        /* XXX: MEMLEAK (?)*/
275 28434
        FREE_OBJ(c);
276 28434
}
277 28434
278
/**********************************************************************
279 28434
 * Start the client thread
280 28434
 */
281
282
static void
283
client_start(struct client *c)
284
{
285
        struct vsb *vsb;
286
287 41664
        CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC);
288
        vtc_log(c->vl, 2, "Starting client");
289
        c->running = 1;
290
        vsb = macro_expand(c->vl, c->connect);
291 41664
        AN(vsb);
292 41664
        REPLACE(c->addr, VSB_data(vsb));
293 41664
        VSB_destroy(&vsb);
294 41664
        c->tp = Sess_Start_Thread(
295 41664
            c,
296 41664
            c->vsp,
297 41664
            client_conn,
298 41664
            client_disc,
299 41664
            c->addr,
300 41664
            NULL,
301
            c->spec
302
        );
303 41664
}
304
305 41664
/**********************************************************************
306
 * Wait for client thread to stop
307 41664
 */
308
309
static void
310
client_wait(struct client *c)
311
{
312
        void *res;
313
314 41664
        CHECK_OBJ_NOTNULL(c, CLIENT_MAGIC);
315
        vtc_log(c->vl, 2, "Waiting for client");
316
        PTOK(pthread_join(c->tp, &res));
317
        if (res != NULL)
318 41664
                vtc_fatal(c->vl, "Client returned \"%s\"", (char *)res);
319 41664
        c->tp = 0;
320 41664
        c->running = 0;
321 41664
}
322 0
323 41664
/**********************************************************************
324 41664
 * Run the client thread
325 41664
 */
326
327
static void
328
client_run(struct client *c)
329
{
330
331
        client_start(c);
332 35280
        client_wait(c);
333
}
334
335 35280
336 35280
/**********************************************************************
337 35280
 * Client command dispatch
338
 */
339
340
void
341
cmd_client(CMD_ARGS)
342
{
343
        struct client *c, *c2;
344
345 72618
        (void)priv;
346
347
        if (av == NULL) {
348
                /* Reset and free */
349 72618
                VTAILQ_FOREACH_SAFE(c, &clients, list, c2) {
350
                        VTAILQ_REMOVE(&clients, c, list);
351 72618
                        if (c->tp != 0)
352
                                client_wait(c);
353 51765
                        client_delete(c);
354 28434
                }
355 28434
                return;
356 504
        }
357 28434
358 28434
        AZ(strcmp(av[0], "client"));
359 23331
        av++;
360
361
        VTC_CHECK_NAME(vl, av[0], "Client", 'c');
362 49287
        VTAILQ_FOREACH(c, &clients, list)
363 49287
                if (!strcmp(c->name, av[0]))
364
                        break;
365 49287
        if (c == NULL)
366 86709
                c = client_new(av[0]);
367 58275
        av++;
368 20853
369 49287
        for (; *av != NULL; av++) {
370 28434
                if (vtc_error)
371 49287
                        break;
372
373 142695
                if (!strcmp(*av, "-wait")) {
374 93408
                        client_wait(c);
375 0
                        continue;
376
                }
377 93408
378 5838
                /* Don't muck about with a running client */
379 5838
                if (c->running)
380
                        client_wait(c);
381
382
                AZ(c->running);
383 87570
                if (Sess_GetOpt(c->vsp, &av))
384 42
                        continue;
385
386 87570
                if (!strcmp(*av, "-connect")) {
387 87570
                        bprintf(c->connect, "%s", av[1]);
388 1281
                        av++;
389
                        continue;
390 86289
                }
391 3738
                if (!strcmp(*av, "-proxy1")) {
392 3738
                        REPLACE(c->proxy_spec, av[1]);
393 3738
                        c->proxy_version = 1;
394
                        av++;
395 82551
                        continue;
396 189
                }
397 189
                if (!strcmp(*av, "-proxy2")) {
398 189
                        REPLACE(c->proxy_spec, av[1]);
399 189
                        c->proxy_version = 2;
400
                        av++;
401 82362
                        continue;
402 84
                }
403 84
                if (!strcmp(*av, "-start")) {
404 84
                        client_start(c);
405 84
                        continue;
406
                }
407 82278
                if (!strcmp(*av, "-run")) {
408 6384
                        client_run(c);
409 6384
                        continue;
410
                }
411 75894
                if (**av == '-')
412 35280
                        vtc_fatal(c->vl, "Unknown client argument: %s", *av);
413 35280
                REPLACE(c->spec, *av);
414
        }
415 40614
}