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 <stdlib.h>
35
#include <string.h>
36
#include <sys/stat.h>
37
38
#include "vcc_compile.h"
39
#include "vus.h"
40
41
const char *
42 12
vcc_default_probe(struct vcc *tl)
43
{
44
45 12
        if (tl->default_probe != NULL)
46 0
                return (tl->default_probe);
47 12
        VSB_cat(tl->sb, "No default probe defined\n");
48 12
        vcc_ErrToken(tl, tl->t);
49 12
        VSB_cat(tl->sb, " at\n");
50 12
        vcc_ErrWhere(tl, tl->t);
51 12
        return ("");
52 12
}
53
54
/*--------------------------------------------------------------------
55
 * Struct sockaddr is not really designed to be a compile time
56
 * initialized data structure, so we encode it as a byte-string
57
 * and put it in an official sockaddr when we load the VCL.
58
 */
59
60
static void
61 15684
Emit_Sockaddr(struct vcc *tl, struct vsb *vsb1, const struct token *t_host,
62
    const struct token *t_port)
63
{
64
        const char *ipv4, *ipv4a, *ipv6, *ipv6a, *pa;
65
        char buf[BUFSIZ];
66
67 15684
        AN(t_host->dec);
68
69 15684
        if (t_port != NULL)
70 11868
                bprintf(buf, "%s %s", t_host->dec, t_port->dec);
71
        else
72 3816
                bprintf(buf, "%s", t_host->dec);
73 31368
        Resolve_Sockaddr(tl, buf, "80",
74 15684
            &ipv4, &ipv4a, &ipv6, &ipv6a, &pa, 2, t_host, "Backend host");
75 15684
        ERRCHK(tl);
76 15660
        if (ipv4 != NULL) {
77 31176
                VSB_printf(vsb1,
78
                    "\t.ipv4 = (const struct suckaddr *)%s,\n",
79 15588
                    ipv4);
80 15588
        }
81 15660
        if (ipv6 != NULL) {
82 312
                VSB_printf(vsb1,
83
                    "\t.ipv6 = (const struct suckaddr *)%s,\n",
84 156
                    ipv6);
85 156
        }
86 15660
        VSB_cat(vsb1, "\t.uds_path = (void *) 0,\n");
87 15684
}
88
89
/*
90
 * For UDS, we do not create a VSA. We run the VUS_resolver() checks and, if
91
 * it's a path, can be accessed, and is a socket. If so, just emit the path
92
 * field and set the IP suckaddrs to NULL.
93
 */
94
95
static int
96 384
uds_resolved(void *priv, const struct sockaddr_un *uds)
97
{
98 384
        (void) priv;
99 384
        (void) uds;
100 384
        return (42);
101
}
102
103
static void
104 372
emit_path(struct vsb *vsb1, char *path)
105
{
106 372
        VSB_printf(vsb1, "\t.uds_path = \"%s\",\n", path);
107 372
        VSB_cat(vsb1, "\t.ipv4 = (void *) 0,\n");
108 372
        VSB_cat(vsb1, "\t.ipv6 = (void *) 0,\n");
109 372
}
110
111
static void
112 408
Emit_UDS_Path(struct vcc *tl, struct vsb *vsb1,
113
    const struct token *t_path, const char *errid)
114
{
115
        struct stat st;
116
        const char *vus_err;
117
118 408
        AN(t_path);
119 408
        AN(t_path->dec);
120
121 408
        if (! VUS_is(t_path->dec)) {
122 24
                VSB_printf(tl->sb,
123
                           "%s: Must be a valid path or abstract socket:\n",
124 12
                           errid);
125 12
                vcc_ErrWhere(tl, t_path);
126 12
                return;
127
        }
128 396
        if (VUS_resolver(t_path->dec, uds_resolved, NULL, &vus_err) != 42) {
129 12
                VSB_printf(tl->sb, "%s: %s\n", errid, vus_err);
130 12
                vcc_ErrWhere(tl, t_path);
131 12
                return;
132
        }
133 384
        if (*t_path->dec == '@') {
134 0
                emit_path(vsb1, t_path->dec);
135 0
                return;
136
        }
137 384
        assert(*t_path->dec == '/');
138 384
        errno = 0;
139 384
        if (stat(t_path->dec, &st) != 0) {
140 12
                int err = errno;
141 24
                VSB_printf(tl->sb, "%s: Cannot stat: %s\n", errid,
142 12
                           strerror(errno));
143 12
                vcc_ErrWhere(tl, t_path);
144 12
                if (err == ENOENT || err == EACCES)
145 12
                        vcc_Warn(tl);
146
                else
147 0
                        return;
148 384
        } else if (!S_ISSOCK(st.st_mode)) {
149 12
                VSB_printf(tl->sb, "%s: Not a socket:\n", errid);
150 12
                vcc_ErrWhere(tl, t_path);
151 12
                return;
152
        }
153 372
        emit_path(vsb1, t_path->dec);
154 408
}
155
156
/*--------------------------------------------------------------------
157
 * Disallow mutually exclusive field definitions
158
 */
159
160
static void
161 16488
vcc_Redef(struct vcc *tl, const char *redef, const struct token **t_did,
162
    const struct token *t_field)
163
{
164 16488
        if (*t_did != NULL) {
165 36
                VSB_printf(tl->sb, "%s redefinition at:\n", redef);
166 36
                vcc_ErrWhere(tl, t_field);
167 36
                VSB_cat(tl->sb, "Previous definition:\n");
168 36
                vcc_ErrWhere(tl, *t_did);
169 36
                return;
170
        }
171 16452
        *t_did = t_field;
172 16488
}
173
174
/*--------------------------------------------------------------------
175
 * Parse a backend probe specification
176
 */
177
178
static void
179 540
vcc_ParseProbeSpec(struct vcc *tl, const struct symbol *sym, char **namep)
180
{
181
        struct fld_spec *fs;
182
        const struct token *t_field;
183 540
        const struct token *t_did = NULL, *t_window = NULL, *t_threshold = NULL;
184 540
        struct token *t_initial = NULL;
185
        unsigned window, threshold, initial, status, exp_close;
186
        char buf[32];
187
        const char *name;
188
        double t;
189
190 540
        fs = vcc_FldSpec(tl,
191
            "?url",
192
            "?request",
193
            "?expected_response",
194
            "?timeout",
195
            "?interval",
196
            "?window",
197
            "?threshold",
198
            "?initial",
199
            "?expect_close",
200
            NULL);
201
202 540
        SkipToken(tl, '{');
203
204 540
        if (sym != NULL) {
205 132
                name = sym->rname;
206 132
        } else {
207 408
                bprintf(buf, "vgc_probe__%d", tl->nprobe++);
208 408
                name = buf;
209
        }
210 540
        Fh(tl, 0, "static const struct vrt_backend_probe %s[] = {{\n", name);
211 540
        Fh(tl, 0, "\t.magic = VRT_BACKEND_PROBE_MAGIC,\n");
212 540
        if (namep != NULL)
213 408
                *namep = TlDup(tl, name);
214
215 540
        window = 0;
216 540
        threshold = 0;
217 540
        initial = 0;
218 540
        status = 0;
219 540
        exp_close = 1;
220 1740
        while (tl->t->tok != '}') {
221
222 1272
                vcc_IsField(tl, &t_field, fs);
223 1272
                ERRCHK(tl);
224 1272
                if (vcc_IdIs(t_field, "url")) {
225 132
                        vcc_Redef(tl, "Probe request", &t_did, t_field);
226 132
                        ERRCHK(tl);
227 120
                        ExpectErr(tl, CSTR);
228 120
                        Fh(tl, 0, "\t.url = ");
229 120
                        EncToken(tl->fh, tl->t);
230 120
                        Fh(tl, 0, ",\n");
231 120
                        vcc_NextToken(tl);
232 1260
                } else if (vcc_IdIs(t_field, "request")) {
233 60
                        vcc_Redef(tl, "Probe request", &t_did, t_field);
234 60
                        ERRCHK(tl);
235 48
                        ExpectErr(tl, CSTR);
236 48
                        Fh(tl, 0, "\t.request =\n");
237 192
                        while (tl->t->tok == CSTR) {
238 144
                                Fh(tl, 0, "\t\t");
239 144
                                EncToken(tl->fh, tl->t);
240 144
                                Fh(tl, 0, " \"\\r\\n\"\n");
241 144
                                vcc_NextToken(tl);
242
                        }
243 48
                        Fh(tl, 0, "\t\t\"\\r\\n\",\n");
244 1128
                } else if (vcc_IdIs(t_field, "timeout")) {
245 108
                        Fh(tl, 0, "\t.timeout = ");
246 108
                        vcc_Duration(tl, &t);
247 108
                        ERRCHK(tl);
248 96
                        Fh(tl, 0, "%g,\n", t);
249 1068
                } else if (vcc_IdIs(t_field, "interval")) {
250 288
                        Fh(tl, 0, "\t.interval = ");
251 288
                        vcc_Duration(tl, &t);
252 288
                        ERRCHK(tl);
253 288
                        Fh(tl, 0, "%g,\n", t);
254 972
                } else if (vcc_IdIs(t_field, "window")) {
255 216
                        t_window = tl->t;
256 216
                        window = vcc_UintVal(tl);
257 216
                        ERRCHK(tl);
258 684
                } else if (vcc_IdIs(t_field, "initial")) {
259 156
                        t_initial = tl->t;
260 156
                        initial = vcc_UintVal(tl);
261 156
                        ERRCHK(tl);
262 468
                } else if (vcc_IdIs(t_field, "expected_response")) {
263 24
                        status = vcc_UintVal(tl);
264 24
                        if (status < 100 || status > 999) {
265 12
                                VSB_cat(tl->sb,
266
                                    "Must specify .expected_response with "
267
                                    "exactly three digits "
268
                                    "(100 <= x <= 999)\n");
269 12
                                vcc_ErrWhere(tl, tl->t);
270 12
                                return;
271
                        }
272 12
                        ERRCHK(tl);
273 300
                } else if (vcc_IdIs(t_field, "threshold")) {
274 240
                        t_threshold = tl->t;
275 240
                        threshold = vcc_UintVal(tl);
276 240
                        ERRCHK(tl);
277 288
                } else if (vcc_IdIs(t_field, "expect_close")) {
278 48
                        exp_close = vcc_BoolVal(tl);
279 48
                        ERRCHK(tl);
280 24
                } else {
281 0
                        vcc_ErrToken(tl, t_field);
282 0
                        vcc_ErrWhere(tl, t_field);
283 0
                        ErrInternal(tl);
284 0
                        return;
285
                }
286
287 1200
                SkipToken(tl, ';');
288
        }
289 468
        free(fs);
290
291 468
        if (t_threshold != NULL || t_window != NULL) {
292 252
                if (t_threshold == NULL && t_window != NULL) {
293 12
                        VSB_cat(tl->sb,
294
                            "Must specify .threshold with .window\n");
295 12
                        vcc_ErrWhere(tl, t_window);
296 12
                        return;
297 240
                } else if (t_threshold != NULL && t_window == NULL) {
298 36
                        if (threshold > 64) {
299 12
                                VSB_cat(tl->sb,
300
                                    "Threshold must be 64 or less.\n");
301 12
                                vcc_ErrWhere(tl, t_threshold);
302 12
                                return;
303
                        }
304 24
                        window = threshold + 1;
305 228
                } else if (window > 64) {
306 12
                        AN(t_window);
307 12
                        VSB_cat(tl->sb, "Window must be 64 or less.\n");
308 12
                        vcc_ErrWhere(tl, t_window);
309 12
                        return;
310
                }
311 216
                if (threshold > window ) {
312 12
                        VSB_cat(tl->sb,
313
                            "Threshold can not be greater than window.\n");
314 12
                        AN(t_threshold);
315 12
                        vcc_ErrWhere(tl, t_threshold);
316 12
                        AN(t_window);
317 12
                        vcc_ErrWhere(tl, t_window);
318 12
                }
319 216
                Fh(tl, 0, "\t.window = %u,\n", window);
320 216
                Fh(tl, 0, "\t.threshold = %u,\n", threshold);
321 216
        }
322 432
        if (t_initial != NULL)
323 156
                Fh(tl, 0, "\t.initial = %u,\n", initial);
324
        else
325 276
                Fh(tl, 0, "\t.initial = ~0U,\n");
326 432
        if (status > 0)
327 12
                Fh(tl, 0, "\t.exp_status = %u,\n", status);
328 432
        Fh(tl, 0, "\t.exp_close = %u,\n", exp_close);
329 432
        Fh(tl, 0, "}};\n");
330 432
        SkipToken(tl, '}');
331 540
}
332
333
/*--------------------------------------------------------------------
334
 * Parse and emit a probe definition
335
 */
336
337
void
338 240
vcc_ParseProbe(struct vcc *tl)
339
{
340
        struct symbol *sym;
341
        char *p;
342
343 240
        vcc_NextToken(tl);                      /* ID: probe */
344
345 240
        vcc_ExpectVid(tl, "backend probe");     /* ID: name */
346 240
        ERRCHK(tl);
347 240
        if (vcc_IdIs(tl->t, "default")) {
348 96
                vcc_NextToken(tl);
349 96
                vcc_ParseProbeSpec(tl, NULL, &p);
350 96
                tl->default_probe = p;
351 96
        } else {
352 144
                sym = VCC_HandleSymbol(tl, PROBE);
353 144
                ERRCHK(tl);
354 132
                AN(sym);
355 132
                vcc_ParseProbeSpec(tl, sym, NULL);
356
        }
357 240
}
358
359
/*--------------------------------------------------------------------
360
 * Parse and emit a backend host definition
361
 *
362
 * The struct vrt_backend is emitted to Fh().
363
 */
364
365
static void
366 19008
vcc_ParseHostDef(struct vcc *tl, struct symbol *sym,
367
    const struct token *t_be, const char *vgcname)
368
{
369
        const struct token *t_field;
370
        const struct token *t_val;
371 19008
        const struct token *t_host = NULL;
372 19008
        const struct token *t_port = NULL;
373 19008
        const struct token *t_path = NULL;
374 19008
        const struct token *t_hosthdr = NULL;
375 19008
        const struct token *t_authority = NULL;
376 19008
        const struct token *t_did = NULL;
377 19008
        const struct token *t_preamble = NULL;
378
        struct symbol *pb;
379
        struct fld_spec *fs;
380
        struct inifin *ifp;
381
        struct vsb *vsb1;
382 19008
        struct symbol *via = NULL;
383
        char *p;
384
        unsigned u;
385
        double t;
386
387 20256
        if (tl->t->tok == ID &&
388 2664
            (vcc_IdIs(tl->t, "none") || vcc_IdIs(tl->t, "None"))) {
389 2664
                vcc_NextToken(tl);
390 2664
                SkipToken(tl, ';');
391 2664
                ifp = New_IniFin(tl);
392 2664
                VSB_printf(ifp->ini, "\t(void)%s;", vgcname);
393 2664
                VSB_printf(ifp->fin, "\t\t(void)%s;", vgcname);
394 2664
                return;
395
        }
396
397 16344
        SkipToken(tl, '{');
398
399
        /* Check for old syntax */
400 16344
        if (tl->t->tok == ID && vcc_IdIs(tl->t, "set")) {
401 12
                VSB_cat(tl->sb,
402
                    "NB: Backend Syntax has changed:\n"
403
                    "Remove \"set\" and \"backend\" in front"
404
                    " of backend fields.\n" );
405 12
                vcc_ErrToken(tl, tl->t);
406 12
                VSB_cat(tl->sb, " at ");
407 12
                vcc_ErrWhere(tl, tl->t);
408 12
                return;
409
        }
410
411 16332
        fs = vcc_FldSpec(tl,
412
            "?host",
413
            "?port",
414
            "?path",
415
            "?host_header",
416
            "?connect_timeout",
417
            "?first_byte_timeout",
418
            "?between_bytes_timeout",
419
            "?probe",
420
            "?max_connections",
421
            "?proxy_header",
422
            "?preamble",
423
            "?via",
424
            "?authority",
425
            NULL);
426
427 16332
        tl->fb = VSB_new_auto();
428 16332
        AN(tl->fb);
429
430 32664
        Fb(tl, 0, "\nstatic const struct vrt_backend vgc_dir_priv_%s = {\n",
431 16332
            vgcname);
432
433 16332
        Fb(tl, 0, "\t.magic = VRT_BACKEND_MAGIC,\n");
434 16332
        Fb(tl, 0, "\t.endpoint = &vgc_dir_ep_%s,\n", vgcname);
435 16332
        Fb(tl, 0, "\t.vcl_name = \"%.*s", PF(t_be));
436 16332
        Fb(tl, 0, "\",\n");
437
438 45180
        while (tl->t->tok != '}') {
439
440 29064
                vcc_IsField(tl, &t_field, fs);
441 29064
                ERRCHK(tl);
442 29040
                if (vcc_IdIs(t_field, "host")) {
443 15864
                        vcc_Redef(tl, "Address", &t_did, t_field);
444 15864
                        ERRCHK(tl);
445 15864
                        ExpectErr(tl, CSTR);
446 15864
                        assert(tl->t->dec != NULL);
447 15864
                        t_host = tl->t;
448 15864
                        vcc_NextToken(tl);
449 15864
                        SkipToken(tl, ';');
450 29040
                } else if (vcc_IdIs(t_field, "port")) {
451 11892
                        ExpectErr(tl, CSTR);
452 11892
                        assert(tl->t->dec != NULL);
453 11892
                        t_port = tl->t;
454 11892
                        vcc_NextToken(tl);
455 11892
                        SkipToken(tl, ';');
456 13176
                } else if (vcc_IdIs(t_field, "path")) {
457 444
                        if (tl->syntax < VCL_41) {
458 12
                                VSB_cat(tl->sb,
459
                                    "Unix socket backends only supported"
460
                                    " in VCL4.1 and higher.\n");
461 12
                                vcc_ErrToken(tl, tl->t);
462 12
                                VSB_cat(tl->sb, " at ");
463 12
                                vcc_ErrWhere(tl, tl->t);
464 12
                                VSB_destroy(&tl->fb);
465 12
                                return;
466
                        }
467 432
                        vcc_Redef(tl, "Address", &t_did, t_field);
468 432
                        ERRCHK(tl);
469 420
                        ExpectErr(tl, CSTR);
470 420
                        assert(tl->t->dec != NULL);
471 420
                        t_path = tl->t;
472 420
                        vcc_NextToken(tl);
473 420
                        SkipToken(tl, ';');
474 1260
                } else if (vcc_IdIs(t_field, "host_header")) {
475 48
                        ExpectErr(tl, CSTR);
476 48
                        assert(tl->t->dec != NULL);
477 48
                        t_hosthdr = tl->t;
478 48
                        vcc_NextToken(tl);
479 48
                        SkipToken(tl, ';');
480 840
                } else if (vcc_IdIs(t_field, "connect_timeout")) {
481 0
                        Fb(tl, 0, "\t.connect_timeout = ");
482 0
                        vcc_Duration(tl, &t);
483 0
                        ERRCHK(tl);
484 0
                        Fb(tl, 0, "%g,\n", t);
485 0
                        SkipToken(tl, ';');
486 792
                } else if (vcc_IdIs(t_field, "first_byte_timeout")) {
487 36
                        Fb(tl, 0, "\t.first_byte_timeout = ");
488 36
                        vcc_Duration(tl, &t);
489 36
                        ERRCHK(tl);
490 36
                        Fb(tl, 0, "%g,\n", t);
491 36
                        SkipToken(tl, ';');
492 792
                } else if (vcc_IdIs(t_field, "between_bytes_timeout")) {
493 12
                        Fb(tl, 0, "\t.between_bytes_timeout = ");
494 12
                        vcc_Duration(tl, &t);
495 12
                        ERRCHK(tl);
496 12
                        Fb(tl, 0, "%g,\n", t);
497 12
                        SkipToken(tl, ';');
498 756
                } else if (vcc_IdIs(t_field, "max_connections")) {
499 60
                        u = vcc_UintVal(tl);
500 60
                        ERRCHK(tl);
501 60
                        SkipToken(tl, ';');
502 60
                        Fb(tl, 0, "\t.max_connections = %u,\n", u);
503 744
                } else if (vcc_IdIs(t_field, "proxy_header")) {
504 72
                        t_val = tl->t;
505 72
                        u = vcc_UintVal(tl);
506 72
                        ERRCHK(tl);
507 72
                        if (u != 1 && u != 2) {
508 0
                                VSB_cat(tl->sb,
509
                                    ".proxy_header must be 1 or 2\n");
510 0
                                vcc_ErrWhere(tl, t_val);
511 0
                                VSB_destroy(&tl->fb);
512 0
                                return;
513
                        }
514 72
                        SkipToken(tl, ';');
515 72
                        Fb(tl, 0, "\t.proxy_header = %u,\n", u);
516 684
                } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == '{') {
517 312
                        vcc_ParseProbeSpec(tl, NULL, &p);
518 312
                        Fb(tl, 0, "\t.probe = %s,\n", p);
519 312
                        free(p);
520 312
                        ERRCHK(tl);
521 492
                } else if (vcc_IdIs(t_field, "probe") && tl->t->tok == ID) {
522 132
                        if (vcc_IdIs(tl->t, "default")) {
523 12
                                vcc_NextToken(tl);
524 12
                                (void)vcc_default_probe(tl);
525 12
                        } else {
526 120
                                pb = VCC_SymbolGet(tl, SYM_MAIN, SYM_PROBE,
527
                                    SYMTAB_EXISTING, XREF_REF);
528 120
                                ERRCHK(tl);
529 108
                                AN(pb);
530 108
                                Fb(tl, 0, "\t.probe = %s,\n", pb->rname);
531
                        }
532 120
                        SkipToken(tl, ';');
533 276
                } else if (vcc_IdIs(t_field, "probe")) {
534 12
                        VSB_cat(tl->sb, "Expected '{' or name of probe, got ");
535 12
                        vcc_ErrToken(tl, tl->t);
536 12
                        VSB_cat(tl->sb, " at\n");
537 12
                        vcc_ErrWhere(tl, tl->t);
538 12
                        VSB_destroy(&tl->fb);
539 12
                        return;
540 156
                } else if (vcc_IdIs(t_field, "preamble")) {
541 12
                        ExpectErr(tl, CBLOB);
542 12
                        t_preamble = tl->t;
543 12
                        vcc_NextToken(tl);
544 12
                        SkipToken(tl, ';');
545 156
                } else if (vcc_IdIs(t_field, "via")) {
546 108
                        via = VCC_SymbolGet(tl, SYM_MAIN, SYM_BACKEND,
547
                            SYMTAB_EXISTING, XREF_REF);
548 108
                        ERRCHK(tl);
549 108
                        AN(via);
550 108
                        AN(via->rname);
551
552 108
                        if (via->extra != NULL) {
553 12
                                AZ(strcmp(via->extra, "via"));
554 12
                                VSB_cat(tl->sb,
555
                                        "Can not stack .via backends at\n");
556 12
                                vcc_ErrWhere(tl, tl->t);
557 12
                                VSB_destroy(&tl->fb);
558 12
                                return;
559
                        }
560
561 96
                        AN(sym);
562 96
                        AZ(sym->extra);
563 96
                        sym->extra = "via";
564 96
                        SkipToken(tl, ';');
565 132
                } else if (vcc_IdIs(t_field, "authority")) {
566 36
                        ExpectErr(tl, CSTR);
567 36
                        assert(tl->t->dec != NULL);
568 36
                        t_authority = tl->t;
569 36
                        vcc_NextToken(tl);
570 36
                        SkipToken(tl, ';');
571 36
                } else {
572 0
                        ErrInternal(tl);
573 0
                        VSB_destroy(&tl->fb);
574 0
                        return;
575
                }
576
577
        }
578
579 16116
        vcc_FieldsOk(tl, fs);
580 16116
        free(fs);
581 16116
        ERRCHK(tl);
582
583 16116
        ExpectErr(tl, '}');
584
585 16116
        if (t_host == NULL && t_path == NULL) {
586 12
                VSB_cat(tl->sb, "Expected .host or .path.\n");
587 12
                vcc_ErrWhere(tl, t_be);
588 12
                VSB_destroy(&tl->fb);
589 12
                return;
590
        }
591
592 16104
        if (via != NULL && t_path != NULL) {
593 12
                VSB_cat(tl->sb, "Cannot set both .via and .path.\n");
594 12
                vcc_ErrWhere(tl, t_be);
595 12
                return;
596
        }
597
598 16092
        if (via != NULL)
599 84
                AZ(via->extra);
600
601 16092
        vsb1 = VSB_new_auto();
602 16092
        AN(vsb1);
603 32184
        VSB_printf(vsb1,
604
            "\nstatic const struct vrt_endpoint vgc_dir_ep_%s = {\n",
605 16092
            vgcname);
606 16092
        VSB_cat(vsb1, "\t.magic = VRT_ENDPOINT_MAGIC,\n");
607
608 16092
        assert(t_host != NULL || t_path != NULL);
609 16092
        if (t_host != NULL)
610
                /* Check that the hostname makes sense */
611 15684
                Emit_Sockaddr(tl, vsb1, t_host, t_port);
612
        else
613
                /* Check that the path can be a legal UDS */
614 408
                Emit_UDS_Path(tl, vsb1, t_path, "Backend path");
615 16092
        ERRCHK(tl);
616
617 16032
        if (t_preamble != NULL)
618 12
                VSB_printf(vsb1, "\t.preamble = %s,\n", t_preamble->dec);
619
620 16032
        VSB_cat(vsb1, "};\n");
621 16032
        AZ(VSB_finish(vsb1));
622 16032
        Fh(tl, 0, "%s", VSB_data(vsb1));
623 16032
        VSB_destroy(&vsb1);
624
625
        /* Emit the hosthdr field, fall back to .host if not specified */
626
        /* If .path is specified, set "0.0.0.0". */
627 16032
        Fb(tl, 0, "\t.hosthdr = ");
628 16032
        if (t_hosthdr != NULL)
629 48
                EncToken(tl->fb, t_hosthdr);
630 15984
        else if (t_host != NULL)
631 15636
                EncToken(tl->fb, t_host);
632
        else
633 348
                Fb(tl, 0, "\"0.0.0.0\"");
634 16032
        Fb(tl, 0, ",\n");
635
636
        /*
637
         * Emit the authority field, falling back to hosthdr, then host.
638
         *
639
         * When authority is "", sending the TLV is disabled.
640
         *
641
         * Falling back to host may result in an IP address in authority,
642
         * which is an illegal SNI HostName (RFC 4366 ch. 3.1). But we
643
         * document the potential error, rather than try to find out
644
         * whether or not Emit_Sockaddr() had to look up a name.
645
         */
646 16032
        if (via != NULL) {
647 84
                AN(t_host);
648 84
                Fb(tl, 0, "\t.authority = ");
649 84
                if (t_authority != NULL)
650 36
                        EncToken(tl->fb, t_authority);
651 48
                else if (t_hosthdr != NULL)
652 12
                        EncToken(tl->fb, t_hosthdr);
653
                else
654 36
                        EncToken(tl->fb, t_host);
655 84
                Fb(tl, 0, ",\n");
656 84
        }
657
658
        /* Close the struct */
659 16032
        Fb(tl, 0, "};\n");
660
661 16032
        vcc_NextToken(tl);
662
663 16032
        AZ(VSB_finish(tl->fb));
664 16032
        Fh(tl, 0, "%s", VSB_data(tl->fb));
665 16032
        VSB_destroy(&tl->fb);
666
667 16032
        ifp = New_IniFin(tl);
668 32064
        VSB_printf(ifp->ini,
669
            "\t%s =\n\t    VRT_new_backend_clustered(ctx, vsc_cluster,\n"
670
            "\t\t&vgc_dir_priv_%s, %s);\n",
671 16032
            vgcname, vgcname, via ? via->rname : "NULL");
672 32064
        VSB_printf(ifp->ini,
673 16032
            "\tif (%s)\n\t\tVRT_StaticDirector(%s);", vgcname, vgcname);
674 16032
        VSB_printf(ifp->fin, "\t\tVRT_delete_backend(ctx, &%s);", vgcname);
675 19008
}
676
677
/*--------------------------------------------------------------------
678
 * Parse directors and backends
679
 */
680
681
void
682 19068
vcc_ParseBackend(struct vcc *tl)
683
{
684
        struct token *t_first, *t_be;
685 19068
        struct symbol *sym = NULL;
686
        const char *dn;
687
688 19068
        tl->ndirector++;
689 19068
        t_first = tl->t;
690 19068
        SkipToken(tl, ID);              /* ID: backend */
691
692 19068
        vcc_ExpectVid(tl, "backend");   /* ID: name */
693 19068
        ERRCHK(tl);
694
695 19056
        t_be = tl->t;
696 19056
        if (vcc_IdIs(tl->t, "default")) {
697 780
                if (tl->first_director != NULL) {
698 36
                        tl->first_director->noref = 0;
699 36
                        tl->first_director = NULL;
700 36
                        tl->default_director = NULL;
701 36
                }
702 780
                if (tl->default_director != NULL) {
703 12
                        VSB_cat(tl->sb,
704
                            "Only one default director possible.\n");
705 12
                        vcc_ErrWhere(tl, t_first);
706 12
                        return;
707
                }
708 768
                vcc_NextToken(tl);
709 768
                dn = "vgc_backend_default";
710 768
                tl->default_director = dn;
711 768
        } else {
712 18276
                sym = VCC_HandleSymbol(tl, BACKEND);
713 18276
                ERRCHK(tl);
714 18240
                AN(sym);
715 18240
                dn = sym->rname;
716 18240
                if (tl->default_director == NULL) {
717 15096
                        tl->first_director = sym;
718 15096
                        tl->default_director = dn;
719 15096
                        sym->noref = 1;
720 15096
                }
721
        }
722 19008
        Fh(tl, 0, "\nstatic VCL_BACKEND %s;\n", dn);
723 19008
        vcc_ParseHostDef(tl, sym, t_be, dn);
724 19008
        if (tl->err) {
725 624
                VSB_printf(tl->sb,
726 312
                    "\nIn %.*s specification starting at:\n", PF(t_first));
727 312
                vcc_ErrWhere(tl, t_first);
728 312
                return;
729
        }
730 19068
}
731
732
void
733 35004
vcc_Backend_Init(struct vcc *tl)
734
{
735
        struct inifin *ifp;
736
737 35004
        Fh(tl, 0, "\nstatic struct vsmw_cluster *vsc_cluster;\n");
738 35004
        ifp = New_IniFin(tl);
739 35004
        VSB_cat(ifp->ini, "\tvsc_cluster = VRT_VSM_Cluster_New(ctx,\n"
740
            "\t    ndirector * VRT_backend_vsm_need(ctx));\n");
741 35004
        VSB_cat(ifp->ini, "\tif (vsc_cluster == 0)\n\t\treturn(1);");
742 35004
        VSB_cat(ifp->fin, "\t\tVRT_VSM_Cluster_Destroy(ctx, &vsc_cluster);");
743 35004
}