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