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