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 1348
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 1348
        AN(t_host->dec);
62
63 1348
        if (t_port != NULL)
64 1026
                bprintf(buf, "%s %s", t_host->dec, t_port->dec);
65
        else
66 322
                bprintf(buf, "%s", t_host->dec);
67 2696
        Resolve_Sockaddr(tl, buf, "80",
68 1348
            &ipv4, &ipv4a, &ipv6, &ipv6a, &pa, 2, t_host, "Backend host");
69 1348
        ERRCHK(tl);
70 1346
        if (ipv4 != NULL) {
71 2680
                VSB_printf(vsb1,
72
                    "\t.ipv4 = (const struct suckaddr *)%s,\n",
73 1340
                    ipv4);
74 1340
        }
75 1346
        if (ipv6 != NULL) {
76 14
                VSB_printf(vsb1,
77
                    "\t.ipv6 = (const struct suckaddr *)%s,\n",
78 7
                    ipv6);
79 7
        }
80 1346
        VSB_cat(vsb1, "\t.uds_path = (void *) 0,\n");
81 1348
}
82
83
/*
84
 * a string represents an IP address if getaddrinfo(AI_NUMERICHOST) succeeds
85
 */
86
static int
87 10
isip(const char *addr)
88
{
89 10
        char buf[vsa_suckaddr_len];
90
91 10
        return (VSS_ResolveOne(buf, addr, "80", AF_UNSPEC, SOCK_STREAM,
92 10
            AI_NUMERICHOST | AI_NUMERICSERV) != NULL);
93 10
}
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 32
uds_resolved(void *priv, const struct sockaddr_un *uds)
103
{
104 32
        (void) priv;
105 32
        (void) uds;
106 32
        return (42);
107
}
108
109
static void
110 31
emit_path(struct vsb *vsb1, char *path)
111
{
112 31
        VSB_printf(vsb1, "\t.uds_path = \"%s\",\n", path);
113 31
        VSB_cat(vsb1, "\t.ipv4 = (void *) 0,\n");
114 31
        VSB_cat(vsb1, "\t.ipv6 = (void *) 0,\n");
115 31
}
116
117
static void
118 34
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 34
        AN(t_path);
125 34
        AN(t_path->dec);
126
127 34
        if (! VUS_is(t_path->dec)) {
128 2
                VSB_printf(tl->sb,
129
                           "%s: Must be a valid path or abstract socket:\n",
130 1
                           errid);
131 1
                vcc_ErrWhere(tl, t_path);
132 1
                return;
133
        }
134 33
        if (VUS_resolver(t_path->dec, uds_resolved, NULL, &vus_err) != 42) {
135 1
                VSB_printf(tl->sb, "%s: %s\n", errid, vus_err);
136 1
                vcc_ErrWhere(tl, t_path);
137 1
                return;
138
        }
139 32
        if (*t_path->dec == '@') {
140 0
                emit_path(vsb1, t_path->dec);
141 0
                return;
142
        }
143 32
        assert(*t_path->dec == '/');
144 32
        errno = 0;
145 32
        if (stat(t_path->dec, &st) != 0) {
146 1
                int err = errno;
147 2
                VSB_printf(tl->sb, "%s: Cannot stat: %s\n", errid,
148 1
                           strerror(errno));
149 1
                vcc_ErrWhere(tl, t_path);
150 1
                if (err == ENOENT || err == EACCES)
151 1
                        vcc_Warn(tl);
152
                else
153 0
                        return;
154 32
        } else if (!S_ISSOCK(st.st_mode)) {
155 1
                VSB_printf(tl->sb, "%s: Not a socket:\n", errid);
156 1
                vcc_ErrWhere(tl, t_path);
157 1
                return;
158
        }
159 31
        emit_path(vsb1, t_path->dec);
160 34
}
161
162
/*--------------------------------------------------------------------
163
 * Disallow mutually exclusive field definitions
164
 */
165
166
static void
167 1417
vcc_Redef(struct vcc *tl, const char *redef, const struct token **t_did,
168
    const struct token *t_field)
169
{
170 1417
        if (*t_did != NULL) {
171 3
                VSB_printf(tl->sb, "%s redefinition at:\n", redef);
172 3
                vcc_ErrWhere(tl, t_field);
173 3
                VSB_cat(tl->sb, "Previous definition:\n");
174 3
                vcc_ErrWhere(tl, *t_did);
175 3
                return;
176
        }
177 1414
        *t_did = t_field;
178 1417
}
179
180
/*--------------------------------------------------------------------
181
 * Parse a backend probe specification
182
 */
183
184
static void
185 47
vcc_ParseProbeSpec(struct vcc *tl, const struct symbol *sym, char **namep)
186
{
187
        struct fld_spec *fs;
188
        const struct token *t_field;
189 47
        const struct token *t_did = NULL, *t_window = NULL, *t_threshold = NULL;
190 47
        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 47
        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 47
        SkipToken(tl, '{');
209
210 47
        if (sym != NULL) {
211 21
                name = sym->rname;
212 21
        } else {
213 26
                bprintf(buf, "vgc_probe__%d", tl->nprobe++);
214 26
                name = buf;
215
        }
216 47
        Fh(tl, 0, "static const struct vrt_backend_probe %s[] = {{\n", name);
217 47
        Fh(tl, 0, "\t.magic = VRT_BACKEND_PROBE_MAGIC,\n");
218 47
        if (namep != NULL)
219 47
                *namep = TlDup(tl, name);
220
221 47
        window = 0;
222 47
        threshold = 0;
223 47
        initial = 0;
224 47
        status = 0;
225 47
        exp_close = 1;
226 157
        while (tl->t->tok != '}') {
227
228 116
                vcc_IsField(tl, &t_field, fs);
229 116
                ERRCHK(tl);
230 116
                if (vcc_IdIs(t_field, "url")) {
231 13
                        vcc_Redef(tl, "Probe request", &t_did, t_field);
232 13
                        ERRCHK(tl);
233 12
                        ExpectErr(tl, CSTR);
234 12
                        Fh(tl, 0, "\t.url = ");
235 12
                        EncToken(tl->fh, tl->t);
236 12
                        Fh(tl, 0, ",\n");
237 12
                        vcc_NextToken(tl);
238 115
                } else if (vcc_IdIs(t_field, "request")) {
239 5
                        vcc_Redef(tl, "Probe request", &t_did, t_field);
240 5
                        ERRCHK(tl);
241 4
                        ExpectErr(tl, CSTR);
242 4
                        Fh(tl, 0, "\t.request =\n");
243 16
                        while (tl->t->tok == CSTR) {
244 12
                                Fh(tl, 0, "\t\t");
245 12
                                EncToken(tl->fh, tl->t);
246 12
                                Fh(tl, 0, " \"\\r\\n\"\n");
247 12
                                vcc_NextToken(tl);
248
                        }
249 4
                        Fh(tl, 0, "\t\t\"\\r\\n\",\n");
250 102
                } else if (vcc_IdIs(t_field, "timeout")) {
251 10
                        Fh(tl, 0, "\t.timeout = ");
252 10
                        vcc_Duration(tl, &t);
253 10
                        ERRCHK(tl);
254 9
                        Fh(tl, 0, "%g,\n", t);
255 97
                } else if (vcc_IdIs(t_field, "interval")) {
256 26
                        Fh(tl, 0, "\t.interval = ");
257 26
                        vcc_Duration(tl, &t);
258 26
                        ERRCHK(tl);
259 26
                        Fh(tl, 0, "%g,\n", t);
260 88
                } else if (vcc_IdIs(t_field, "window")) {
261 20
                        t_window = tl->t;
262 20
                        window = vcc_UintVal(tl);
263 20
                        ERRCHK(tl);
264 62
                } else if (vcc_IdIs(t_field, "initial")) {
265 14
                        t_initial = tl->t;
266 14
                        initial = vcc_UintVal(tl);
267 14
                        ERRCHK(tl);
268 42
                } else if (vcc_IdIs(t_field, "expected_response")) {
269 2
                        status = vcc_UintVal(tl);
270 2
                        if (status < 100 || status > 999) {
271 1
                                VSB_cat(tl->sb,
272
                                    "Must specify .expected_response with "
273
                                    "exactly three digits "
274
                                    "(100 <= x <= 999)\n");
275 1
                                vcc_ErrWhere(tl, tl->t);
276 1
                                return;
277
                        }
278 1
                        ERRCHK(tl);
279 27
                } else if (vcc_IdIs(t_field, "threshold")) {
280 22
                        t_threshold = tl->t;
281 22
                        threshold = vcc_UintVal(tl);
282 22
                        ERRCHK(tl);
283 26
                } else if (vcc_IdIs(t_field, "expect_close")) {
284 4
                        exp_close = vcc_BoolVal(tl);
285 4
                        ERRCHK(tl);
286 2
                } 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 110
                SkipToken(tl, ';');
294
        }
295 41
        free(fs);
296
297 41
        if (t_threshold != NULL || t_window != NULL) {
298 23
                if (t_threshold == NULL && t_window != NULL) {
299 1
                        VSB_cat(tl->sb,
300
                            "Must specify .threshold with .window\n");
301 1
                        vcc_ErrWhere(tl, t_window);
302 1
                        return;
303 22
                } else if (t_threshold != NULL && t_window == NULL) {
304 3
                        if (threshold > 64) {
305 1
                                VSB_cat(tl->sb,
306
                                    "Threshold must be 64 or less.\n");
307 1
                                vcc_ErrWhere(tl, t_threshold);
308 1
                                return;
309
                        }
310 2
                        window = threshold + 1;
311 21
                } else if (window > 64) {
312 1
                        AN(t_window);
313 1
                        VSB_cat(tl->sb, "Window must be 64 or less.\n");
314 1
                        vcc_ErrWhere(tl, t_window);
315 1
                        return;
316
                }
317 20
                if (threshold > window ) {
318 1
                        VSB_cat(tl->sb,
319
                            "Threshold cannot be greater than window.\n");
320 1
                        AN(t_threshold);
321 1
                        vcc_ErrWhere(tl, t_threshold);
322 1
                        AN(t_window);
323 1
                        vcc_ErrWhere(tl, t_window);
324 1
                }
325 20
                Fh(tl, 0, "\t.window = %u,\n", window);
326 20
                Fh(tl, 0, "\t.threshold = %u,\n", threshold);
327 20
        }
328 38
        if (t_initial != NULL)
329 14
                Fh(tl, 0, "\t.initial = %u,\n", initial);
330
        else
331 24
                Fh(tl, 0, "\t.initial = ~0U,\n");
332 38
        if (status > 0)
333 1
                Fh(tl, 0, "\t.exp_status = %u,\n", status);
334 38
        Fh(tl, 0, "\t.exp_close = %u,\n", exp_close);
335 38
        Fh(tl, 0, "}};\n");
336 38
        SkipToken(tl, '}');
337 47
}
338
339
/*--------------------------------------------------------------------
340
 * Parse and emit a probe definition
341
 */
342
343
void
344 22
vcc_ParseProbe(struct vcc *tl)
345
{
346
        struct symbol *sym;
347
        char *p;
348
349 22
        vcc_NextToken(tl);                      /* ID: probe */
350
351 22
        vcc_ExpectVid(tl, "backend probe");     /* ID: name */
352 22
        ERRCHK(tl);
353
354 22
        sym = VCC_HandleSymbol(tl, PROBE);
355 22
        ERRCHK(tl);
356 21
        AN(sym);
357 21
        vcc_ParseProbeSpec(tl, sym, &p);
358
359 21
        if (sym->type == DEFAULT)
360 10
                tl->default_probe = p;
361
        else
362 11
                free(p);
363 22
}
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 1642
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 1642
        const struct token *t_host = NULL;
379 1642
        const struct token *t_port = NULL;
380 1642
        const struct token *t_path = NULL;
381 1642
        const struct token *t_hosthdr = NULL;
382 1642
        const struct token *t_authority = NULL;
383 1642
        const struct token *t_did = NULL;
384 1642
        const struct token *t_preamble = NULL;
385
        struct symbol *pb;
386
        struct fld_spec *fs;
387
        struct inifin *ifp;
388
        struct vsb *vsb1;
389 1642
        struct symbol *via = NULL;
390 1642
        vtim_dur connect_timeout = NAN;
391 1642
        vtim_dur first_byte_timeout = NAN;
392 1642
        vtim_dur between_bytes_timeout = NAN;
393 1642
        vtim_dur backend_wait_timeout = NAN;
394
        char *p, *pp;
395
        unsigned u;
396
        int l;
397
398 1746
        if (tl->t->tok == ID &&
399 239
            (vcc_IdIs(tl->t, "none") || vcc_IdIs(tl->t, "None"))) {
400 239
                vcc_NextToken(tl);
401 239
                SkipToken(tl, ';');
402 239
                ifp = New_IniFin(tl);
403 239
                VSB_printf(ifp->ini, "\t(void)%s;", vgcname);
404 239
                VSB_printf(ifp->fin, "\t\t(void)%s;", vgcname);
405 239
                return;
406
        }
407
408 1403
        SkipToken(tl, '{');
409
410
        /* Check for old syntax */
411 1403
        if (tl->t->tok == ID && vcc_IdIs(tl->t, "set")) {
412 1
                VSB_cat(tl->sb,
413
                    "NB: Backend Syntax has changed:\n"
414
                    "Remove \"set\" and \"backend\" in front"
415
                    " of backend fields.\n" );
416 1
                vcc_ErrToken(tl, tl->t);
417 1
                VSB_cat(tl->sb, " at ");
418 1
                vcc_ErrWhere(tl, tl->t);
419 1
                return;
420
        }
421
422 1402
        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 1402
        tl->fb = VSB_new_auto();
441 1402
        AN(tl->fb);
442
443 2804
        Fb(tl, 0, "\nstatic const struct vrt_backend vgc_dir_priv_%s = {\n",
444 1402
            vgcname);
445
446 1402
        Fb(tl, 0, "\t.magic = VRT_BACKEND_MAGIC,\n");
447 1402
        Fb(tl, 0, "\t.endpoint = &vgc_dir_ep_%s,\n", vgcname);
448 1402
        Fb(tl, 0, "\t.vcl_name = \"%.*s", PF(t_be));
449 1402
        Fb(tl, 0, "\",\n");
450
451 3911
        while (tl->t->tok != '}') {
452
453 2527
                vcc_IsField(tl, &t_field, fs);
454 2527
                ERRCHK(tl);
455 2525
                if (vcc_IdIs(t_field, "host")) {
456 1363
                        vcc_Redef(tl, "Address", &t_did, t_field);
457 1363
                        ERRCHK(tl);
458 1363
                        ExpectErr(tl, CSTR);
459 1363
                        assert(tl->t->dec != NULL);
460 1363
                        t_host = tl->t;
461 1363
                        vcc_NextToken(tl);
462 1363
                        SkipToken(tl, ';');
463 2525
                } else if (vcc_IdIs(t_field, "port")) {
464 1028
                        ExpectErr(tl, CSTR);
465 1028
                        assert(tl->t->dec != NULL);
466 1028
                        t_port = tl->t;
467 1028
                        vcc_NextToken(tl);
468 1028
                        SkipToken(tl, ';');
469 1162
                } else if (vcc_IdIs(t_field, "path")) {
470 37
                        if (tl->syntax < VCL_41) {
471 1
                                VSB_cat(tl->sb,
472
                                    "Unix socket backends only supported"
473
                                    " in VCL4.1 and higher.\n");
474 1
                                vcc_ErrToken(tl, tl->t);
475 1
                                VSB_cat(tl->sb, " at ");
476 1
                                vcc_ErrWhere(tl, tl->t);
477 1
                                VSB_destroy(&tl->fb);
478 1
                                return;
479
                        }
480 36
                        vcc_Redef(tl, "Address", &t_did, t_field);
481 36
                        ERRCHK(tl);
482 35
                        ExpectErr(tl, CSTR);
483 35
                        assert(tl->t->dec != NULL);
484 35
                        t_path = tl->t;
485 35
                        vcc_NextToken(tl);
486 35
                        SkipToken(tl, ';');
487 132
                } else if (vcc_IdIs(t_field, "host_header")) {
488 6
                        ExpectErr(tl, CSTR);
489 6
                        assert(tl->t->dec != NULL);
490 6
                        t_hosthdr = tl->t;
491 6
                        vcc_NextToken(tl);
492 6
                        SkipToken(tl, ';');
493 97
                } else if (vcc_IdIs(t_field, "connect_timeout")) {
494 3
                        Fb(tl, 0, "\t.connect_timeout = ");
495 3
                        vcc_Duration(tl, &connect_timeout);
496 3
                        ERRCHK(tl);
497 3
                        Fb(tl, 0, "%g,\n", connect_timeout);
498 3
                        SkipToken(tl, ';');
499 91
                } else if (vcc_IdIs(t_field, "first_byte_timeout")) {
500 3
                        Fb(tl, 0, "\t.first_byte_timeout = ");
501 3
                        vcc_Duration(tl, &first_byte_timeout);
502 3
                        ERRCHK(tl);
503 3
                        Fb(tl, 0, "%g,\n", first_byte_timeout);
504 3
                        SkipToken(tl, ';');
505 88
                } else if (vcc_IdIs(t_field, "between_bytes_timeout")) {
506 1
                        Fb(tl, 0, "\t.between_bytes_timeout = ");
507 1
                        vcc_Duration(tl, &between_bytes_timeout);
508 1
                        ERRCHK(tl);
509 1
                        Fb(tl, 0, "%g,\n", between_bytes_timeout);
510 1
                        SkipToken(tl, ';');
511 85
                } else if (vcc_IdIs(t_field, "max_connections")) {
512 11
                        u = vcc_UintVal(tl);
513 11
                        ERRCHK(tl);
514 11
                        SkipToken(tl, ';');
515 11
                        Fb(tl, 0, "\t.max_connections = %u,\n", u);
516 84
                } else if (vcc_IdIs(t_field, "proxy_header")) {
517 6
                        t_val = tl->t;
518 6
                        u = vcc_UintVal(tl);
519 6
                        ERRCHK(tl);
520 6
                        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 6
                        SkipToken(tl, ';');
528 6
                        Fb(tl, 0, "\t.proxy_header = %u,\n", u);
529 73
                } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == '{') {
530 26
                        vcc_ParseProbeSpec(tl, NULL, &p);
531 26
                        Fb(tl, 0, "\t.probe = %s,\n", p);
532 26
                        free(p);
533 26
                        ERRCHK(tl);
534 57
                } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == ID) {
535 11
                        t_probe = tl->t;
536 11
                        pb = VCC_SymbolGet(tl, SYM_MAIN, SYM_PROBE,
537
                            SYMTAB_EXISTING, XREF_REF);
538 11
                        ERRCHK(tl);
539 10
                        AN(pb);
540 10
                        if (pb->type == DEFAULT) {
541 1
                                if (tl->default_probe == NULL) {
542 1
                                        VSB_cat(tl->sb,
543
                                            "No default probe defined\n");
544 1
                                        vcc_ErrToken(tl, t_probe);
545 1
                                        VSB_cat(tl->sb, " at\n");
546 1
                                        vcc_ErrWhere(tl, t_probe);
547 1
                                }
548 1
                                pb = PROBE->default_sym;
549 1
                        }
550 10
                        ERRCHK(tl);
551 9
                        Fb(tl, 0, "\t.probe = %s,\n", pb->rname);
552 9
                        SkipToken(tl, ';');
553 39
                } else if (vcc_IdIs(t_field, "probe")) {
554 1
                        VSB_cat(tl->sb, "Expected '{' or name of probe, got ");
555 1
                        vcc_ErrToken(tl, tl->t);
556 1
                        VSB_cat(tl->sb, " at\n");
557 1
                        vcc_ErrWhere(tl, tl->t);
558 1
                        VSB_destroy(&tl->fb);
559 1
                        return;
560 29
                } else if (vcc_IdIs(t_field, "preamble")) {
561 1
                        ExpectErr(tl, CBLOB);
562 1
                        t_preamble = tl->t;
563 1
                        vcc_NextToken(tl);
564 1
                        SkipToken(tl, ';');
565 29
                } else if (vcc_IdIs(t_field, "via")) {
566 12
                        via = VCC_SymbolGet(tl, SYM_MAIN, SYM_BACKEND,
567
                            SYMTAB_EXISTING, XREF_REF);
568 12
                        ERRCHK(tl);
569 12
                        AN(via);
570 12
                        AN(via->rname);
571
572 12
                        if (via->extra != NULL) {
573 1
                                AZ(strcmp(via->extra, "via"));
574 1
                                VSB_cat(tl->sb,
575
                                        "Cannot stack .via backends at\n");
576 1
                                vcc_ErrWhere(tl, tl->t);
577 1
                                VSB_destroy(&tl->fb);
578 1
                                return;
579
                        }
580
581 11
                        AN(sym);
582 11
                        AZ(sym->extra);
583 11
                        sym->extra = "via";
584 11
                        SkipToken(tl, ';');
585 27
                } else if (vcc_IdIs(t_field, "authority")) {
586 6
                        ExpectErr(tl, CSTR);
587 6
                        assert(tl->t->dec != NULL);
588 6
                        t_authority = tl->t;
589 6
                        vcc_NextToken(tl);
590 6
                        SkipToken(tl, ';');
591 16
                } else if (vcc_IdIs(t_field, "wait_timeout")) {
592 5
                        Fb(tl, 0, "\t.backend_wait_timeout = ");
593 5
                        vcc_Duration(tl, &backend_wait_timeout);
594 5
                        ERRCHK(tl);
595 5
                        Fb(tl, 0, "%g,\n", backend_wait_timeout);
596 5
                        SkipToken(tl, ';');
597 10
                } else if (vcc_IdIs(t_field, "wait_limit")) {
598 5
                        u = vcc_UintVal(tl);
599 5
                        ERRCHK(tl);
600 5
                        SkipToken(tl, ';');
601 5
                        Fb(tl, 0, "\t.backend_wait_limit = %u,\n", u);
602 5
                } else {
603 0
                        ErrInternal(tl);
604 0
                        VSB_destroy(&tl->fb);
605 0
                        return;
606
                }
607
608
        }
609
610 1384
        vcc_FieldsOk(tl, fs);
611 1384
        free(fs);
612 1384
        ERRCHK(tl);
613
614 1384
        if (isnan(connect_timeout))
615 1381
                Fb(tl, 0, "\t.connect_timeout = -1.0,\n");
616 1384
        if (isnan(first_byte_timeout))
617 1381
                Fb(tl, 0, "\t.first_byte_timeout = -1.0,\n");
618 1384
        if (isnan(between_bytes_timeout))
619 1383
                Fb(tl, 0, "\t.between_bytes_timeout = -1.0,\n");
620 1384
        if (isnan(backend_wait_timeout))
621 1379
                Fb(tl, 0, "\t.backend_wait_timeout = -1.0,\n");
622
623 1384
        ExpectErr(tl, '}');
624
625 1384
        if (t_host == NULL && t_path == NULL) {
626 1
                VSB_cat(tl->sb, "Expected .host or .path.\n");
627 1
                vcc_ErrWhere(tl, t_be);
628 1
                VSB_destroy(&tl->fb);
629 1
                return;
630
        }
631
632 1383
        if (via != NULL && t_path != NULL) {
633 1
                VSB_cat(tl->sb, "Cannot set both .via and .path.\n");
634 1
                vcc_ErrWhere(tl, t_be);
635 1
                return;
636
        }
637
638 1382
        if (via != NULL)
639 10
                AZ(via->extra);
640
641 1382
        vsb1 = VSB_new_auto();
642 1382
        AN(vsb1);
643 2764
        VSB_printf(vsb1,
644
            "\nstatic const struct vrt_endpoint vgc_dir_ep_%s = {\n",
645 1382
            vgcname);
646 1382
        VSB_cat(vsb1, "\t.magic = VRT_ENDPOINT_MAGIC,\n");
647
648 1382
        assert(t_host != NULL || t_path != NULL);
649 1382
        if (t_host != NULL)
650
                /* Check that the hostname makes sense */
651 1348
                Emit_Sockaddr(tl, vsb1, t_host, t_port);
652
        else
653
                /* Check that the path can be a legal UDS */
654 34
                Emit_UDS_Path(tl, vsb1, t_path, "Backend path");
655 1382
        ERRCHK(tl);
656
657 1377
        if (t_preamble != NULL)
658 1
                VSB_printf(vsb1, "\t.preamble = %s,\n", t_preamble->dec);
659
660 1377
        VSB_cat(vsb1, "};\n");
661 1377
        AZ(VSB_finish(vsb1));
662 1377
        Fh(tl, 0, "%s", VSB_data(vsb1));
663 1377
        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 1377
        Fb(tl, 0, "\t.hosthdr = ");
668 1377
        if (t_hosthdr != NULL)
669 6
                EncToken(tl->fb, t_hosthdr);
670 1371
        else if (t_host != NULL)
671 1342
                EncToken(tl->fb, t_host);
672
        else
673 29
                Fb(tl, 0, "\"0.0.0.0\"");
674 1377
        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 1377
        if (via != NULL) {
689 10
                AN(t_host);
690 10
                if (t_authority != NULL)
691 6
                        t_val = t_authority;
692 4
                else if (t_hosthdr != NULL)
693 3
                        t_val = t_hosthdr;
694
                else
695 1
                        t_val = t_host;
696 10
                p = t_val->dec;
697
698 10
                if (isip(p)) {
699 2
                        if (t_val == t_authority)
700 0
                                VSB_cat(tl->sb, ".authority can not be an ip address with .via.\n");
701
                        else {
702 4
                                VSB_printf(tl->sb, ".%s used as authority can not be an ip address with .via.\n",
703 2
                                        t_val == t_hosthdr ? "host_header" : "host");
704 2
                                VSB_cat(tl->sb, "Hint: configure .authority explicitly\n");
705
                        }
706 2
                        vcc_ErrWhere(tl, t_val);
707 2
                }
708
709 10
                pp = strchr(p, ':');
710 10
                l = (pp == NULL) ? -1 : (int)(pp - p);
711
712 10
                Fb(tl, 0, "\t.authority = ");
713 10
                VSB_quote(tl->fb, p, l, VSB_QUOTE_CSTR);
714 10
                Fb(tl, 0, ",\n");
715 10
        }
716
717
        /* Close the struct */
718 1377
        Fb(tl, 0, "};\n");
719
720 1377
        vcc_NextToken(tl);
721
722 1377
        AZ(VSB_finish(tl->fb));
723 1377
        Fh(tl, 0, "%s", VSB_data(tl->fb));
724 1377
        VSB_destroy(&tl->fb);
725
726 1377
        ifp = New_IniFin(tl);
727 2754
        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 1377
            vgcname, vgcname, via ? via->rname : "NULL");
731 2754
        VSB_printf(ifp->ini,
732 1377
            "\tif (%s)\n\t\tVRT_StaticDirector(%s);", vgcname, vgcname);
733 1377
        VSB_printf(ifp->fin, "\t\tVRT_delete_backend(ctx, &%s);", vgcname);
734 1642
}
735
736
/*--------------------------------------------------------------------
737
 * Parse directors and backends
738
 */
739
740
void
741 1647
vcc_ParseBackend(struct vcc *tl)
742
{
743
        struct token *t_first, *t_be;
744
        struct symbol *sym;
745
        const char *dn;
746
747 1647
        tl->ndirector++;
748 1647
        t_first = tl->t;
749 1647
        SkipToken(tl, ID);              /* ID: backend */
750
751 1647
        vcc_ExpectVid(tl, "backend");   /* ID: name */
752 1647
        ERRCHK(tl);
753
754 1646
        t_be = tl->t;
755
756 1646
        sym = VCC_HandleSymbol(tl, BACKEND);
757 1646
        ERRCHK(tl);
758 1643
        AN(sym);
759
760 1643
        if (sym->type == DEFAULT) {
761 70
                if (tl->first_director != NULL) {
762 4
                        tl->first_director->noref = 0;
763 4
                        tl->first_director = NULL;
764 4
                        tl->default_director = NULL;
765 4
                }
766 70
                if (tl->default_director != NULL) {
767 1
                        VSB_cat(tl->sb,
768
                            "Only one default director possible.\n");
769 1
                        vcc_ErrWhere(tl, t_first);
770 1
                        return;
771
                }
772 69
                dn = "vgc_backend_default";
773 69
                tl->default_director = dn;
774 69
        } else {
775 1573
                dn = sym->rname;
776 1573
                if (tl->default_director == NULL) {
777 1307
                        tl->first_director = sym;
778 1307
                        tl->default_director = dn;
779 1307
                        sym->noref = 1;
780 1307
                }
781
        }
782
783 1642
        Fh(tl, 0, "\nstatic VCL_BACKEND %s;\n", dn);
784 1642
        vcc_ParseHostDef(tl, sym, t_be, dn);
785 1642
        if (tl->err) {
786 56
                VSB_printf(tl->sb,
787 28
                    "\nIn %.*s specification starting at:\n", PF(t_first));
788 28
                vcc_ErrWhere(tl, t_first);
789 28
                return;
790
        }
791 1647
}
792
793
void
794 3025
vcc_Backend_Init(struct vcc *tl)
795
{
796
        struct inifin *ifp;
797
798 3025
        Fh(tl, 0, "\nstatic struct vsmw_cluster *vsc_cluster;\n");
799 3025
        ifp = New_IniFin(tl);
800 3025
        VSB_cat(ifp->ini, "\tvsc_cluster = VRT_VSM_Cluster_New(ctx,\n"
801
            "\t    ndirector * VRT_backend_vsm_need(ctx));\n");
802 3025
        VSB_cat(ifp->ini, "\tif (vsc_cluster == 0)\n\t\treturn(1);");
803 3025
        VSB_cat(ifp->fin, "\t\tVRT_VSM_Cluster_Destroy(ctx, &vsc_cluster);");
804 3025
}