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