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