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