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