varnish-cache/lib/libvcc/vcc_backend.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 */
31
32
#include "config.h"
33
34
#include <math.h>
35
#include <stdlib.h>
36
#include <string.h>
37
#include <sys/stat.h>
38
#include <sys/types.h>
39
#include <sys/socket.h>
40
#include <netdb.h>
41
42
43
#include "vcc_compile.h"
44
#include "vsa.h"
45
#include "vss.h"
46
#include "vus.h"
47
48
/*--------------------------------------------------------------------
49
 * Struct sockaddr is not really designed to be a compile time
50
 * initialized data structure, so we encode it as a byte-string
51
 * and put it in an official sockaddr when we load the VCL.
52
 */
53
54
static void
55 54200
Emit_Sockaddr(struct vcc *tl, struct vsb *vsb1, const struct token *t_host,
56
    const struct token *t_port)
57
{
58
        const char *ipv4, *ipv4a, *ipv6, *ipv6a, *pa;
59
        char buf[BUFSIZ];
60
61 54200
        AN(t_host->dec);
62
63 54200
        if (t_port != NULL)
64 41240
                bprintf(buf, "%s %s", t_host->dec, t_port->dec);
65
        else
66 12960
                bprintf(buf, "%s", t_host->dec);
67 108400
        Resolve_Sockaddr(tl, buf, "80",
68 54200
            &ipv4, &ipv4a, &ipv6, &ipv6a, &pa, 2, t_host, "Backend host");
69 54200
        ERRCHK(tl);
70 54120
        if (ipv4 != NULL) {
71 107760
                VSB_printf(vsb1,
72
                    "\t.ipv4 = (const struct suckaddr *)%s,\n",
73 53880
                    ipv4);
74 53880
        }
75 54120
        if (ipv6 != NULL) {
76 560
                VSB_printf(vsb1,
77
                    "\t.ipv6 = (const struct suckaddr *)%s,\n",
78 280
                    ipv6);
79 280
        }
80 54120
        VSB_cat(vsb1, "\t.uds_path = (void *) 0,\n");
81 54200
}
82
83
/*
84
 * a string represents an IP address if getaddrinfo(AI_NUMERICHOST) succeeds
85
 */
86
static int
87 400
isip(const char *addr)
88
{
89 400
        char buf[vsa_suckaddr_len];
90
91 400
        return (VSS_ResolveOne(buf, addr, "80", AF_UNSPEC, SOCK_STREAM,
92 400
            AI_NUMERICHOST | AI_NUMERICSERV) != NULL);
93 400
}
94
95
/*
96
 * For UDS, we do not create a VSA. We run the VUS_resolver() checks and, if
97
 * it's a path, can be accessed, and is a socket. If so, just emit the path
98
 * field and set the IP suckaddrs to NULL.
99
 */
100
101
static int
102 1280
uds_resolved(void *priv, const struct sockaddr_un *uds)
103
{
104 1280
        (void) priv;
105 1280
        (void) uds;
106 1280
        return (42);
107
}
108
109
static void
110 1240
emit_path(struct vsb *vsb1, char *path)
111
{
112 1240
        VSB_printf(vsb1, "\t.uds_path = \"%s\",\n", path);
113 1240
        VSB_cat(vsb1, "\t.ipv4 = (void *) 0,\n");
114 1240
        VSB_cat(vsb1, "\t.ipv6 = (void *) 0,\n");
115 1240
}
116
117
static void
118 1360
Emit_UDS_Path(struct vcc *tl, struct vsb *vsb1,
119
    const struct token *t_path, const char *errid)
120
{
121
        struct stat st;
122
        const char *vus_err;
123
124 1360
        AN(t_path);
125 1360
        AN(t_path->dec);
126
127 1360
        if (! VUS_is(t_path->dec)) {
128 80
                VSB_printf(tl->sb,
129
                           "%s: Must be a valid path or abstract socket:\n",
130 40
                           errid);
131 40
                vcc_ErrWhere(tl, t_path);
132 40
                return;
133
        }
134 1320
        if (VUS_resolver(t_path->dec, uds_resolved, NULL, &vus_err) != 42) {
135 40
                VSB_printf(tl->sb, "%s: %s\n", errid, vus_err);
136 40
                vcc_ErrWhere(tl, t_path);
137 40
                return;
138
        }
139 1280
        if (*t_path->dec == '@') {
140 0
                emit_path(vsb1, t_path->dec);
141 0
                return;
142
        }
143 1280
        assert(*t_path->dec == '/');
144 1280
        errno = 0;
145 1280
        if (stat(t_path->dec, &st) != 0) {
146 40
                int err = errno;
147 80
                VSB_printf(tl->sb, "%s: Cannot stat: %s\n", errid,
148 40
                           strerror(errno));
149 40
                vcc_ErrWhere(tl, t_path);
150 40
                if (err == ENOENT || err == EACCES)
151 40
                        vcc_Warn(tl);
152
                else
153 0
                        return;
154 1280
        } else if (!S_ISSOCK(st.st_mode)) {
155 40
                VSB_printf(tl->sb, "%s: Not a socket:\n", errid);
156 40
                vcc_ErrWhere(tl, t_path);
157 40
                return;
158
        }
159 1240
        emit_path(vsb1, t_path->dec);
160 1360
}
161
162
/*--------------------------------------------------------------------
163
 * Disallow mutually exclusive field definitions
164
 */
165
166
static void
167 56960
vcc_Redef(struct vcc *tl, const char *redef, const struct token **t_did,
168
    const struct token *t_field)
169
{
170 56960
        if (*t_did != NULL) {
171 120
                VSB_printf(tl->sb, "%s redefinition at:\n", redef);
172 120
                vcc_ErrWhere(tl, t_field);
173 120
                VSB_cat(tl->sb, "Previous definition:\n");
174 120
                vcc_ErrWhere(tl, *t_did);
175 120
                return;
176
        }
177 56840
        *t_did = t_field;
178 56960
}
179
180
/*--------------------------------------------------------------------
181
 * Parse a backend probe specification
182
 */
183
184
static void
185 1880
vcc_ParseProbeSpec(struct vcc *tl, const struct symbol *sym, char **namep)
186
{
187
        struct fld_spec *fs;
188
        const struct token *t_field;
189 1880
        const struct token *t_did = NULL, *t_window = NULL, *t_threshold = NULL;
190 1880
        struct token *t_initial = NULL;
191
        unsigned window, threshold, initial, status, exp_close;
192
        char buf[32];
193
        const char *name;
194
        double t;
195
196 1880
        fs = vcc_FldSpec(tl,
197
            "?url",
198
            "?request",
199
            "?expected_response",
200
            "?timeout",
201
            "?interval",
202
            "?window",
203
            "?threshold",
204
            "?initial",
205
            "?expect_close",
206
            NULL);
207
208 1880
        SkipToken(tl, '{');
209
210 1880
        if (sym != NULL) {
211 840
                name = sym->rname;
212 840
        } else {
213 1040
                bprintf(buf, "vgc_probe__%d", tl->nprobe++);
214 1040
                name = buf;
215
        }
216 1880
        Fh(tl, 0, "static const struct vrt_backend_probe %s[] = {{\n", name);
217 1880
        Fh(tl, 0, "\t.magic = VRT_BACKEND_PROBE_MAGIC,\n");
218 1880
        if (namep != NULL)
219 1880
                *namep = TlDup(tl, name);
220
221 1880
        window = 0;
222 1880
        threshold = 0;
223 1880
        initial = 0;
224 1880
        status = 0;
225 1880
        exp_close = 1;
226 6280
        while (tl->t->tok != '}') {
227
228 4640
                vcc_IsField(tl, &t_field, fs);
229 4640
                ERRCHK(tl);
230 4640
                if (vcc_IdIs(t_field, "url")) {
231 520
                        vcc_Redef(tl, "Probe request", &t_did, t_field);
232 520
                        ERRCHK(tl);
233 480
                        ExpectErr(tl, CSTR);
234 480
                        Fh(tl, 0, "\t.url = ");
235 480
                        EncToken(tl->fh, tl->t);
236 480
                        Fh(tl, 0, ",\n");
237 480
                        vcc_NextToken(tl);
238 4600
                } else if (vcc_IdIs(t_field, "request")) {
239 200
                        vcc_Redef(tl, "Probe request", &t_did, t_field);
240 200
                        ERRCHK(tl);
241 160
                        ExpectErr(tl, CSTR);
242 160
                        Fh(tl, 0, "\t.request =\n");
243 640
                        while (tl->t->tok == CSTR) {
244 480
                                Fh(tl, 0, "\t\t");
245 480
                                EncToken(tl->fh, tl->t);
246 480
                                Fh(tl, 0, " \"\\r\\n\"\n");
247 480
                                vcc_NextToken(tl);
248
                        }
249 160
                        Fh(tl, 0, "\t\t\"\\r\\n\",\n");
250 4080
                } else if (vcc_IdIs(t_field, "timeout")) {
251 400
                        Fh(tl, 0, "\t.timeout = ");
252 400
                        vcc_Duration(tl, &t);
253 400
                        ERRCHK(tl);
254 360
                        Fh(tl, 0, "%g,\n", t);
255 3880
                } else if (vcc_IdIs(t_field, "interval")) {
256 1040
                        Fh(tl, 0, "\t.interval = ");
257 1040
                        vcc_Duration(tl, &t);
258 1040
                        ERRCHK(tl);
259 1040
                        Fh(tl, 0, "%g,\n", t);
260 3520
                } else if (vcc_IdIs(t_field, "window")) {
261 800
                        t_window = tl->t;
262 800
                        window = vcc_UintVal(tl);
263 800
                        ERRCHK(tl);
264 2480
                } else if (vcc_IdIs(t_field, "initial")) {
265 560
                        t_initial = tl->t;
266 560
                        initial = vcc_UintVal(tl);
267 560
                        ERRCHK(tl);
268 1680
                } else if (vcc_IdIs(t_field, "expected_response")) {
269 80
                        status = vcc_UintVal(tl);
270 80
                        if (status < 100 || status > 999) {
271 40
                                VSB_cat(tl->sb,
272
                                    "Must specify .expected_response with "
273
                                    "exactly three digits "
274
                                    "(100 <= x <= 999)\n");
275 40
                                vcc_ErrWhere(tl, tl->t);
276 40
                                return;
277
                        }
278 40
                        ERRCHK(tl);
279 1080
                } else if (vcc_IdIs(t_field, "threshold")) {
280 880
                        t_threshold = tl->t;
281 880
                        threshold = vcc_UintVal(tl);
282 880
                        ERRCHK(tl);
283 1040
                } else if (vcc_IdIs(t_field, "expect_close")) {
284 160
                        exp_close = vcc_BoolVal(tl);
285 160
                        ERRCHK(tl);
286 80
                } else {
287 0
                        vcc_ErrToken(tl, t_field);
288 0
                        vcc_ErrWhere(tl, t_field);
289 0
                        ErrInternal(tl);
290 0
                        return;
291
                }
292
293 4400
                SkipToken(tl, ';');
294
        }
295 1640
        free(fs);
296
297 1640
        if (t_threshold != NULL || t_window != NULL) {
298 920
                if (t_threshold == NULL && t_window != NULL) {
299 40
                        VSB_cat(tl->sb,
300
                            "Must specify .threshold with .window\n");
301 40
                        vcc_ErrWhere(tl, t_window);
302 40
                        return;
303 880
                } else if (t_threshold != NULL && t_window == NULL) {
304 120
                        if (threshold > 64) {
305 40
                                VSB_cat(tl->sb,
306
                                    "Threshold must be 64 or less.\n");
307 40
                                vcc_ErrWhere(tl, t_threshold);
308 40
                                return;
309
                        }
310 80
                        window = threshold + 1;
311 840
                } else if (window > 64) {
312 40
                        AN(t_window);
313 40
                        VSB_cat(tl->sb, "Window must be 64 or less.\n");
314 40
                        vcc_ErrWhere(tl, t_window);
315 40
                        return;
316
                }
317 800
                if (threshold > window ) {
318 40
                        VSB_cat(tl->sb,
319
                            "Threshold cannot be greater than window.\n");
320 40
                        AN(t_threshold);
321 40
                        vcc_ErrWhere(tl, t_threshold);
322 40
                        AN(t_window);
323 40
                        vcc_ErrWhere(tl, t_window);
324 40
                }
325 800
                Fh(tl, 0, "\t.window = %u,\n", window);
326 800
                Fh(tl, 0, "\t.threshold = %u,\n", threshold);
327 800
        }
328 1520
        if (t_initial != NULL)
329 560
                Fh(tl, 0, "\t.initial = %u,\n", initial);
330
        else
331 960
                Fh(tl, 0, "\t.initial = ~0U,\n");
332 1520
        if (status > 0)
333 40
                Fh(tl, 0, "\t.exp_status = %u,\n", status);
334 1520
        Fh(tl, 0, "\t.exp_close = %u,\n", exp_close);
335 1520
        Fh(tl, 0, "}};\n");
336 1520
        SkipToken(tl, '}');
337 1880
}
338
339
/*--------------------------------------------------------------------
340
 * Parse and emit a probe definition
341
 */
342
343
void
344 880
vcc_ParseProbe(struct vcc *tl)
345
{
346
        struct symbol *sym;
347
        char *p;
348
349 880
        vcc_NextToken(tl);                      /* ID: probe */
350
351 880
        vcc_ExpectVid(tl, "backend probe");     /* ID: name */
352 880
        ERRCHK(tl);
353
354 880
        sym = VCC_HandleSymbol(tl, PROBE);
355 880
        ERRCHK(tl);
356 840
        AN(sym);
357 840
        vcc_ParseProbeSpec(tl, sym, &p);
358
359 840
        if (sym->type == DEFAULT)
360 400
                tl->default_probe = p;
361
        else
362 440
                free(p);
363 880
}
364
365
/*--------------------------------------------------------------------
366
 * Parse and emit a backend host definition
367
 *
368
 * The struct vrt_backend is emitted to Fh().
369
 */
370
371
static void
372 66160
vcc_ParseHostDef(struct vcc *tl, struct symbol *sym,
373
    const struct token *t_be, const char *vgcname)
374
{
375
        const struct token *t_field;
376
        const struct token *t_val;
377
        const struct token *t_probe;
378 66160
        const struct token *t_host = NULL;
379 66160
        const struct token *t_port = NULL;
380 66160
        const struct token *t_path = NULL;
381 66160
        const struct token *t_hosthdr = NULL;
382 66160
        const struct token *t_authority = NULL;
383 66160
        const struct token *t_did = NULL;
384 66160
        const struct token *t_preamble = NULL;
385
        struct symbol *pb;
386
        struct fld_spec *fs;
387
        struct inifin *ifp;
388
        struct vsb *vsb1;
389 66160
        struct symbol *via = NULL;
390 66160
        vtim_dur connect_timeout = NAN;
391 66160
        vtim_dur first_byte_timeout = NAN;
392 66160
        vtim_dur between_bytes_timeout = NAN;
393 66160
        vtim_dur backend_wait_timeout = NAN;
394
        char *p, *pp;
395
        unsigned u;
396
        int l;
397
398 70320
        if (tl->t->tok == ID &&
399 9760
            (vcc_IdIs(tl->t, "none") || vcc_IdIs(tl->t, "None"))) {
400 9760
                vcc_NextToken(tl);
401 9760
                SkipToken(tl, ';');
402 9760
                ifp = New_IniFin(tl);
403 9760
                VSB_printf(ifp->ini, "\t(void)%s;", vgcname);
404 9760
                VSB_printf(ifp->fin, "\t\t(void)%s;", vgcname);
405 9760
                return;
406
        }
407
408 56400
        SkipToken(tl, '{');
409
410
        /* Check for old syntax */
411 56400
        if (tl->t->tok == ID && vcc_IdIs(tl->t, "set")) {
412 40
                VSB_cat(tl->sb,
413
                    "NB: Backend Syntax has changed:\n"
414
                    "Remove \"set\" and \"backend\" in front"
415
                    " of backend fields.\n" );
416 40
                vcc_ErrToken(tl, tl->t);
417 40
                VSB_cat(tl->sb, " at ");
418 40
                vcc_ErrWhere(tl, tl->t);
419 40
                return;
420
        }
421
422 56360
        fs = vcc_FldSpec(tl,
423
            "?host",
424
            "?port",
425
            "?path",
426
            "?host_header",
427
            "?connect_timeout",
428
            "?first_byte_timeout",
429
            "?between_bytes_timeout",
430
            "?probe",
431
            "?max_connections",
432
            "?proxy_header",
433
            "?preamble",
434
            "?via",
435
            "?authority",
436
            "?wait_timeout",
437
            "?wait_limit",
438
            NULL);
439
440 56360
        tl->fb = VSB_new_auto();
441 56360
        AN(tl->fb);
442
443 112720
        Fb(tl, 0, "\nstatic const struct vrt_backend vgc_dir_priv_%s = {\n",
444 56360
            vgcname);
445
446 56360
        Fb(tl, 0, "\t.magic = VRT_BACKEND_MAGIC,\n");
447 56360
        Fb(tl, 0, "\t.endpoint = &vgc_dir_ep_%s,\n", vgcname);
448 56360
        Fb(tl, 0, "\t.vcl_name = \"%.*s", PF(t_be));
449 56360
        Fb(tl, 0, "\",\n");
450
451 157200
        while (tl->t->tok != '}') {
452
453 101560
                vcc_IsField(tl, &t_field, fs);
454 101560
                ERRCHK(tl);
455 101480
                if (vcc_IdIs(t_field, "host")) {
456 54800
                        vcc_Redef(tl, "Address", &t_did, t_field);
457 54800
                        ERRCHK(tl);
458 54800
                        ExpectErr(tl, CSTR);
459 54800
                        assert(tl->t->dec != NULL);
460 54800
                        t_host = tl->t;
461 54800
                        vcc_NextToken(tl);
462 54800
                        SkipToken(tl, ';');
463 101480
                } else if (vcc_IdIs(t_field, "port")) {
464 41320
                        ExpectErr(tl, CSTR);
465 41320
                        assert(tl->t->dec != NULL);
466 41320
                        t_port = tl->t;
467 41320
                        vcc_NextToken(tl);
468 41320
                        SkipToken(tl, ';');
469 46680
                } else if (vcc_IdIs(t_field, "path")) {
470 1480
                        if (tl->syntax < VCL_41) {
471 40
                                VSB_cat(tl->sb,
472
                                    "Unix socket backends only supported"
473
                                    " in VCL4.1 and higher.\n");
474 40
                                vcc_ErrToken(tl, tl->t);
475 40
                                VSB_cat(tl->sb, " at ");
476 40
                                vcc_ErrWhere(tl, tl->t);
477 40
                                VSB_destroy(&tl->fb);
478 40
                                return;
479
                        }
480 1440
                        vcc_Redef(tl, "Address", &t_did, t_field);
481 1440
                        ERRCHK(tl);
482 1400
                        ExpectErr(tl, CSTR);
483 1400
                        assert(tl->t->dec != NULL);
484 1400
                        t_path = tl->t;
485 1400
                        vcc_NextToken(tl);
486 1400
                        SkipToken(tl, ';');
487 5280
                } else if (vcc_IdIs(t_field, "host_header")) {
488 240
                        ExpectErr(tl, CSTR);
489 240
                        assert(tl->t->dec != NULL);
490 240
                        t_hosthdr = tl->t;
491 240
                        vcc_NextToken(tl);
492 240
                        SkipToken(tl, ';');
493 3880
                } else if (vcc_IdIs(t_field, "connect_timeout")) {
494 120
                        Fb(tl, 0, "\t.connect_timeout = ");
495 120
                        vcc_Duration(tl, &connect_timeout);
496 120
                        ERRCHK(tl);
497 120
                        Fb(tl, 0, "%g,\n", connect_timeout);
498 120
                        SkipToken(tl, ';');
499 3640
                } else if (vcc_IdIs(t_field, "first_byte_timeout")) {
500 120
                        Fb(tl, 0, "\t.first_byte_timeout = ");
501 120
                        vcc_Duration(tl, &first_byte_timeout);
502 120
                        ERRCHK(tl);
503 120
                        Fb(tl, 0, "%g,\n", first_byte_timeout);
504 120
                        SkipToken(tl, ';');
505 3520
                } else if (vcc_IdIs(t_field, "between_bytes_timeout")) {
506 40
                        Fb(tl, 0, "\t.between_bytes_timeout = ");
507 40
                        vcc_Duration(tl, &between_bytes_timeout);
508 40
                        ERRCHK(tl);
509 40
                        Fb(tl, 0, "%g,\n", between_bytes_timeout);
510 40
                        SkipToken(tl, ';');
511 3400
                } else if (vcc_IdIs(t_field, "max_connections")) {
512 440
                        u = vcc_UintVal(tl);
513 440
                        ERRCHK(tl);
514 440
                        SkipToken(tl, ';');
515 440
                        Fb(tl, 0, "\t.max_connections = %u,\n", u);
516 3360
                } else if (vcc_IdIs(t_field, "proxy_header")) {
517 240
                        t_val = tl->t;
518 240
                        u = vcc_UintVal(tl);
519 240
                        ERRCHK(tl);
520 240
                        if (u != 1 && u != 2) {
521 0
                                VSB_cat(tl->sb,
522
                                    ".proxy_header must be 1 or 2\n");
523 0
                                vcc_ErrWhere(tl, t_val);
524 0
                                VSB_destroy(&tl->fb);
525 0
                                return;
526
                        }
527 240
                        SkipToken(tl, ';');
528 240
                        Fb(tl, 0, "\t.proxy_header = %u,\n", u);
529 2920
                } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == '{') {
530 1040
                        vcc_ParseProbeSpec(tl, NULL, &p);
531 1040
                        Fb(tl, 0, "\t.probe = %s,\n", p);
532 1040
                        free(p);
533 1040
                        ERRCHK(tl);
534 2280
                } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == ID) {
535 440
                        t_probe = tl->t;
536 440
                        pb = VCC_SymbolGet(tl, SYM_MAIN, SYM_PROBE,
537
                            SYMTAB_EXISTING, XREF_REF);
538 440
                        ERRCHK(tl);
539 400
                        AN(pb);
540 400
                        if (pb->type == DEFAULT) {
541 40
                                if (tl->default_probe == NULL) {
542 40
                                        VSB_cat(tl->sb,
543
                                            "No default probe defined\n");
544 40
                                        vcc_ErrToken(tl, t_probe);
545 40
                                        VSB_cat(tl->sb, " at\n");
546 40
                                        vcc_ErrWhere(tl, t_probe);
547 40
                                }
548 40
                                pb = PROBE->default_sym;
549 40
                        }
550 400
                        ERRCHK(tl);
551 360
                        Fb(tl, 0, "\t.probe = %s,\n", pb->rname);
552 360
                        SkipToken(tl, ';');
553 1560
                } else if (vcc_IdIs(t_field, "probe")) {
554 40
                        VSB_cat(tl->sb, "Expected '{' or name of probe, got ");
555 40
                        vcc_ErrToken(tl, tl->t);
556 40
                        VSB_cat(tl->sb, " at\n");
557 40
                        vcc_ErrWhere(tl, tl->t);
558 40
                        VSB_destroy(&tl->fb);
559 40
                        return;
560 1160
                } else if (vcc_IdIs(t_field, "preamble")) {
561 40
                        ExpectErr(tl, CBLOB);
562 40
                        t_preamble = tl->t;
563 40
                        vcc_NextToken(tl);
564 40
                        SkipToken(tl, ';');
565 1160
                } else if (vcc_IdIs(t_field, "via")) {
566 480
                        via = VCC_SymbolGet(tl, SYM_MAIN, SYM_BACKEND,
567
                            SYMTAB_EXISTING, XREF_REF);
568 480
                        ERRCHK(tl);
569 480
                        AN(via);
570 480
                        AN(via->rname);
571
572 480
                        if (via->extra != NULL) {
573 40
                                AZ(strcmp(via->extra, "via"));
574 40
                                VSB_cat(tl->sb,
575
                                        "Cannot stack .via backends at\n");
576 40
                                vcc_ErrWhere(tl, tl->t);
577 40
                                VSB_destroy(&tl->fb);
578 40
                                return;
579
                        }
580
581 440
                        AN(sym);
582 440
                        AZ(sym->extra);
583 440
                        sym->extra = "via";
584 440
                        SkipToken(tl, ';');
585 1080
                } else if (vcc_IdIs(t_field, "authority")) {
586 240
                        ExpectErr(tl, CSTR);
587 240
                        assert(tl->t->dec != NULL);
588 240
                        t_authority = tl->t;
589 240
                        vcc_NextToken(tl);
590 240
                        SkipToken(tl, ';');
591 640
                } else if (vcc_IdIs(t_field, "wait_timeout")) {
592 200
                        Fb(tl, 0, "\t.backend_wait_timeout = ");
593 200
                        vcc_Duration(tl, &backend_wait_timeout);
594 200
                        ERRCHK(tl);
595 200
                        Fb(tl, 0, "%g,\n", backend_wait_timeout);
596 200
                        SkipToken(tl, ';');
597 400
                } else if (vcc_IdIs(t_field, "wait_limit")) {
598 200
                        u = vcc_UintVal(tl);
599 200
                        ERRCHK(tl);
600 200
                        SkipToken(tl, ';');
601 200
                        Fb(tl, 0, "\t.backend_wait_limit = %u,\n", u);
602 200
                } else {
603 0
                        ErrInternal(tl);
604 0
                        VSB_destroy(&tl->fb);
605 0
                        return;
606
                }
607
608
        }
609
610 55640
        vcc_FieldsOk(tl, fs);
611 55640
        free(fs);
612 55640
        ERRCHK(tl);
613
614 55640
        if (isnan(connect_timeout))
615 55520
                Fb(tl, 0, "\t.connect_timeout = -1.0,\n");
616 55640
        if (isnan(first_byte_timeout))
617 55520
                Fb(tl, 0, "\t.first_byte_timeout = -1.0,\n");
618 55640
        if (isnan(between_bytes_timeout))
619 55600
                Fb(tl, 0, "\t.between_bytes_timeout = -1.0,\n");
620 55640
        if (isnan(backend_wait_timeout))
621 55440
                Fb(tl, 0, "\t.backend_wait_timeout = -1.0,\n");
622
623 55640
        ExpectErr(tl, '}');
624
625 55640
        if (t_host == NULL && t_path == NULL) {
626 40
                VSB_cat(tl->sb, "Expected .host or .path.\n");
627 40
                vcc_ErrWhere(tl, t_be);
628 40
                VSB_destroy(&tl->fb);
629 40
                return;
630
        }
631
632 55600
        if (via != NULL && t_path != NULL) {
633 40
                VSB_cat(tl->sb, "Cannot set both .via and .path.\n");
634 40
                vcc_ErrWhere(tl, t_be);
635 40
                return;
636
        }
637
638 55560
        if (via != NULL)
639 400
                AZ(via->extra);
640
641 55560
        vsb1 = VSB_new_auto();
642 55560
        AN(vsb1);
643 111120
        VSB_printf(vsb1,
644
            "\nstatic const struct vrt_endpoint vgc_dir_ep_%s = {\n",
645 55560
            vgcname);
646 55560
        VSB_cat(vsb1, "\t.magic = VRT_ENDPOINT_MAGIC,\n");
647
648 55560
        assert(t_host != NULL || t_path != NULL);
649 55560
        if (t_host != NULL)
650
                /* Check that the hostname makes sense */
651 54200
                Emit_Sockaddr(tl, vsb1, t_host, t_port);
652
        else
653
                /* Check that the path can be a legal UDS */
654 1360
                Emit_UDS_Path(tl, vsb1, t_path, "Backend path");
655 55560
        ERRCHK(tl);
656
657 55360
        if (t_preamble != NULL)
658 40
                VSB_printf(vsb1, "\t.preamble = %s,\n", t_preamble->dec);
659
660 55360
        VSB_cat(vsb1, "};\n");
661 55360
        AZ(VSB_finish(vsb1));
662 55360
        Fh(tl, 0, "%s", VSB_data(vsb1));
663 55360
        VSB_destroy(&vsb1);
664
665
        /* Emit the hosthdr field, fall back to .host if not specified */
666
        /* If .path is specified, set "0.0.0.0". */
667 55360
        Fb(tl, 0, "\t.hosthdr = ");
668 55360
        if (t_hosthdr != NULL)
669 240
                EncToken(tl->fb, t_hosthdr);
670 55120
        else if (t_host != NULL)
671 53960
                EncToken(tl->fb, t_host);
672
        else
673 1160
                Fb(tl, 0, "\"0.0.0.0\"");
674 55360
        Fb(tl, 0, ",\n");
675
676
        /*
677
         * Emit the authority field, falling back to hosthdr, then host.
678
         *
679
         * When authority is "", sending the TLV is disabled.
680
         *
681
         * authority must be a valid SNI HostName (RFC 4366 ch. 3.1), but the
682
         * RFC does not define what that is and defers to "DNS hostname".
683
         *
684
         * So instead of trying to attempt a solution to that pandora's box, we
685
         * just implement >>Literal IPv4 and IPv6 addresses are not permitted in
686
         * "HostName".<<
687
         */
688 55360
        if (via != NULL) {
689 400
                AN(t_host);
690 400
                if (t_authority != NULL)
691 240
                        t_val = t_authority;
692 160
                else if (t_hosthdr != NULL)
693 120
                        t_val = t_hosthdr;
694
                else
695 40
                        t_val = t_host;
696 400
                p = t_val->dec;
697
698 400
                if (isip(p)) {
699 80
                        if (t_val == t_authority)
700 0
                                VSB_cat(tl->sb, ".authority can not be an ip address with .via.\n");
701
                        else {
702 160
                                VSB_printf(tl->sb, ".%s used as authority can not be an ip address with .via.\n",
703 80
                                        t_val == t_hosthdr ? "host_header" : "host");
704 80
                                VSB_cat(tl->sb, "Hint: configure .authority explicitly\n");
705
                        }
706 80
                        vcc_ErrWhere(tl, t_val);
707 80
                }
708
709 400
                pp = strchr(p, ':');
710 400
                l = (pp == NULL) ? -1 : (int)(pp - p);
711
712 400
                Fb(tl, 0, "\t.authority = ");
713 400
                VSB_quote(tl->fb, p, l, VSB_QUOTE_CSTR);
714 400
                Fb(tl, 0, ",\n");
715 400
        }
716
717
        /* Close the struct */
718 55360
        Fb(tl, 0, "};\n");
719
720 55360
        vcc_NextToken(tl);
721
722 55360
        AZ(VSB_finish(tl->fb));
723 55360
        Fh(tl, 0, "%s", VSB_data(tl->fb));
724 55360
        VSB_destroy(&tl->fb);
725
726 55360
        ifp = New_IniFin(tl);
727 110720
        VSB_printf(ifp->ini,
728
            "\t%s =\n\t    VRT_new_backend_clustered(ctx, vsc_cluster,\n"
729
            "\t\t&vgc_dir_priv_%s, %s);\n",
730 55360
            vgcname, vgcname, via ? via->rname : "NULL");
731 110720
        VSB_printf(ifp->ini,
732 55360
            "\tif (%s)\n\t\tVRT_StaticDirector(%s);", vgcname, vgcname);
733 55360
        VSB_printf(ifp->fin, "\t\tVRT_delete_backend(ctx, &%s);", vgcname);
734 66160
}
735
736
/*--------------------------------------------------------------------
737
 * Parse directors and backends
738
 */
739
740
void
741 66360
vcc_ParseBackend(struct vcc *tl)
742
{
743
        struct token *t_first, *t_be;
744
        struct symbol *sym;
745
        const char *dn;
746
747 66360
        tl->ndirector++;
748 66360
        t_first = tl->t;
749 66360
        SkipToken(tl, ID);              /* ID: backend */
750
751 66360
        vcc_ExpectVid(tl, "backend");   /* ID: name */
752 66360
        ERRCHK(tl);
753
754 66320
        t_be = tl->t;
755
756 66320
        sym = VCC_HandleSymbol(tl, BACKEND);
757 66320
        ERRCHK(tl);
758 66200
        AN(sym);
759
760 66200
        if (sym->type == DEFAULT) {
761 3000
                if (tl->first_director != NULL) {
762 160
                        tl->first_director->noref = 0;
763 160
                        tl->first_director = NULL;
764 160
                        tl->default_director = NULL;
765 160
                }
766 3000
                if (tl->default_director != NULL) {
767 40
                        VSB_cat(tl->sb,
768
                            "Only one default director possible.\n");
769 40
                        vcc_ErrWhere(tl, t_first);
770 40
                        return;
771
                }
772 2960
                dn = "vgc_backend_default";
773 2960
                tl->default_director = dn;
774 2960
        } else {
775 63200
                dn = sym->rname;
776 63200
                if (tl->default_director == NULL) {
777 52520
                        tl->first_director = sym;
778 52520
                        tl->default_director = dn;
779 52520
                        sym->noref = 1;
780 52520
                }
781
        }
782
783 66160
        Fh(tl, 0, "\nstatic VCL_BACKEND %s;\n", dn);
784 66160
        vcc_ParseHostDef(tl, sym, t_be, dn);
785 66160
        if (tl->err) {
786 2240
                VSB_printf(tl->sb,
787 1120
                    "\nIn %.*s specification starting at:\n", PF(t_first));
788 1120
                vcc_ErrWhere(tl, t_first);
789 1120
                return;
790
        }
791 66360
}
792
793
void
794 121960
vcc_Backend_Init(struct vcc *tl)
795
{
796
        struct inifin *ifp;
797
798 121960
        Fh(tl, 0, "\nstatic struct vsmw_cluster *vsc_cluster;\n");
799 121960
        ifp = New_IniFin(tl);
800 121960
        VSB_cat(ifp->ini, "\tvsc_cluster = VRT_VSM_Cluster_New(ctx,\n"
801
            "\t    ndirector * VRT_backend_vsm_need(ctx));\n");
802 121960
        VSB_cat(ifp->ini, "\tif (vsc_cluster == 0)\n\t\treturn(1);");
803 121960
        VSB_cat(ifp->fin, "\t\tVRT_VSM_Cluster_Destroy(ctx, &vsc_cluster);");
804 121960
}