4 * Copyright (c) 2021, 2022, 2023 Omar Polo <op@omarpolo.com>
5 * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
6 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
7 * Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
8 * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
9 * Copyright (c) 2001 Markus Friedl. All rights reserved.
10 * Copyright (c) 2001 Daniel Hartmeier. All rights reserved.
11 * Copyright (c) 2001 Theo de Raadt. All rights reserved.
13 * Permission to use, copy, modify, and distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41 static const char *default_host = NULL;
42 static uint16_t default_port = 1965;
44 TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files);
46 TAILQ_ENTRY(file) entry;
57 struct file *pushfile(const char *, int);
61 void yyerror(const char *, ...)
62 __attribute__((__format__ (printf, 1, 2)))
63 __attribute__((__nonnull__ (1)));
64 void yywarn(const char *, ...)
65 __attribute__((__format__ (printf, 1, 2)))
66 __attribute__((__nonnull__ (1)));
67 int kw_cmp(const void *, const void *);
79 TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead);
81 TAILQ_ENTRY(sym) entry;
88 int symset(const char *, const char *, int);
89 char *symget(const char *);
91 char *ensure_absolute_path(char*);
92 int check_block_code(int);
93 char *check_block_fmt(char*);
94 int check_strip_no(int);
95 int check_port_num(int);
96 int check_prefork_num(int);
97 void advance_loc(void);
98 void advance_proxy(void);
99 void parsehp(char *, char **, const char **, const char *);
100 int fastcgi_conf(const char *, const char *);
101 void add_param(char *, char *);
102 int getservice(const char *);
103 void listen_on(const char *, const char *);
105 static struct vhost *host;
106 static struct location *loc;
107 static struct proxy *proxy;
108 static char *current_media;
119 #define YYSTYPE YYSTYPE
124 /* %define parse.error verbose */
126 %token ACCESS ALIAS AUTO
128 %token CA CERT CHROOT CLIENT COMBINED COMMON CONDENSED
130 %token FACILITY FASTCGI FOR_HOST
131 %token INCLUDE INDEX IPV6
133 %token LANG LEGACY LISTEN LOCATION LOG
135 %token PARAM PORT PREFORK PROTO PROTOCOLS PROXY
136 %token RELAY_TO REQUIRE RETURN ROOT
137 %token SERVER SNI SOCKET STRIP STYLE SYSLOG
138 %token TCP TOEXT TYPE TYPES
144 %token <v.string> STRING
145 %token <v.number> NUM
147 %type <v.number> bool proxy_port
148 %type <v.string> string numberstring listen_addr
158 | conf error nl { file->errors++; }
161 include : INCLUDE STRING {
164 if ((nfile = pushfile($2, 0)) == NULL) {
165 yyerror("failed to include file %s", $2);
176 bool : ON { $$ = 1; }
180 string : string STRING {
181 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
184 yyerror("string: asprintf: %s", strerror(errno));
195 if (asprintf(&s, "%d", $1) == -1) {
196 yyerror("asprintf: number");
204 varset : STRING '=' string {
207 if (isspace((unsigned char)*s)) {
208 yyerror("macro name cannot contain "
221 option : CHROOT string {
222 if (strlcpy(conf->chroot, $2, sizeof(conf->chroot)) >=
223 sizeof(conf->chroot))
224 yyerror("chroot path too long");
228 yywarn("option `ipv6' is deprecated,"
229 " please use `listen on'");
233 default_host = "0.0.0.0";
237 yywarn("option `port' is deprecated,"
238 " please use `listen on'");
241 | PREFORK NUM { conf->prefork = check_prefork_num($2); }
243 if (tls_config_parse_protocols(&conf->protos, $2) == -1)
244 yyerror("invalid protocols string \"%s\"", $2);
248 if (strlcpy(conf->user, $2, sizeof(conf->user)) >=
250 yyerror("user name too long");
255 log : LOG '{' optnl logopts '}'
259 logopts : /* empty */
260 | logopts logopt optnl
263 logopt : ACCESS string {
264 free(conf->log_access);
265 conf->log_access = $2;
268 conf->log_format = LOG_FORMAT_COMMON;
271 conf->log_format = LOG_FORMAT_COMBINED;
274 conf->log_format = LOG_FORMAT_CONDENSED;
277 conf->log_format = LOG_FORMAT_LEGACY;
279 | SYSLOG FACILITY string {
280 const char *str = $3;
282 conf->log_syslog = 1;
284 if (!strncasecmp(str, "LOG_", 4))
287 if (!strcasecmp(str, "daemon"))
288 conf->log_facility = LOG_DAEMON;
290 else if (!strcasecmp(str, "ftp"))
291 conf->log_facility = LOG_FTP;
293 else if (!strcasecmp(str, "local1"))
294 conf->log_facility = LOG_LOCAL1;
295 else if (!strcasecmp(str, "local2"))
296 conf->log_facility = LOG_LOCAL2;
297 else if (!strcasecmp(str, "local3"))
298 conf->log_facility = LOG_LOCAL3;
299 else if (!strcasecmp(str, "local4"))
300 conf->log_facility = LOG_LOCAL4;
301 else if (!strcasecmp(str, "local5"))
302 conf->log_facility = LOG_LOCAL5;
303 else if (!strcasecmp(str, "local6"))
304 conf->log_facility = LOG_LOCAL6;
305 else if (!strcasecmp(str, "local7"))
306 conf->log_facility = LOG_LOCAL7;
307 else if (!strcasecmp(str, "user"))
308 conf->log_facility = LOG_USER;
310 yywarn("unknown syslog facility `%s'",
316 conf->log_syslog = 0;
319 conf->log_syslog = 1;
323 vhost : SERVER string {
325 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
327 loc = new_location();
328 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
330 TAILQ_INIT(&host->proxies);
332 (void) strlcpy(loc->match, "*", sizeof(loc->match));
333 (void) strlcpy(host->domain, $2, sizeof(host->domain));
335 if (strstr($2, "xn--") != NULL) {
336 yywarn("\"%s\" looks like punycode: you "
337 "should use the decoded hostname", $2);
341 } '{' optnl servbody '}' {
342 if (host->cert_path == NULL ||
343 host->key_path == NULL)
344 yyerror("invalid vhost definition: %s",
346 if (TAILQ_EMPTY(&host->addrs)) {
350 r = snprintf(portno, sizeof(portno), "%d",
352 if (r < 0 || (size_t)r >= sizeof(portno))
355 yywarn("missing `listen on' in server %s,"
356 " assuming %s port %d", host->domain,
357 default_host ? default_host : "*",
359 listen_on(default_host, portno);
362 | error '}' { yyerror("bad server directive"); }
365 servbody : /* empty */
366 | servbody servopt optnl
367 | servbody location optnl
368 | servbody proxy optnl
371 listen_addr : '*' { $$ = NULL; }
375 servopt : ALIAS string {
378 a = xcalloc(1, sizeof(*a));
379 (void) strlcpy(a->alias, $2, sizeof(a->alias));
381 TAILQ_INSERT_TAIL(&host->aliases, a, aliases);
384 ensure_absolute_path($2);
385 free(host->cert_path);
386 host->cert_path = $2;
389 ensure_absolute_path($2);
390 free(host->key_path);
394 ensure_absolute_path($2);
395 free(host->ocsp_path);
396 host->ocsp_path = $2;
398 | PARAM string '=' string {
399 yywarn("the top-level `param' directive is deprecated."
400 " Please use `fastcgi { param ... }`");
403 | LISTEN ON listen_addr {
404 listen_on($3, "1965");
407 | LISTEN ON listen_addr PORT STRING {
412 | LISTEN ON listen_addr PORT NUM {
416 r = snprintf(portno, sizeof(portno), "%d", $5);
417 if (r < 0 || (size_t)r >= sizeof(portno))
420 listen_on($3, portno);
426 proxy : PROXY { advance_proxy(); }
427 proxy_matches '{' optnl proxy_opts '}' {
428 if (*proxy->host == '\0')
429 yyerror("invalid proxy block: missing `relay-to' option");
431 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
432 (proxy->cert_path != NULL && proxy->key_path == NULL))
433 yyerror("invalid proxy block: missing cert or key");
437 proxy_matches : /* empty */
438 | proxy_matches proxy_match
441 proxy_port : /* empty */ { $$ = 1965; }
443 if (($$ = getservice($2)) == -1)
444 yyerror("invalid port number %s", $2);
447 | PORT NUM { $$ = $2; }
450 proxy_match : PROTO string {
451 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
454 | FOR_HOST string proxy_port {
455 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
456 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
462 proxy_opts : /* empty */
463 | proxy_opts proxy_opt optnl
466 proxy_opt : CERT string {
468 ensure_absolute_path($2);
469 proxy->cert_path = $2;
473 ensure_absolute_path($2);
474 proxy->key_path = $2;
477 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
478 yyerror("invalid protocols string \"%s\"", $2);
481 | RELAY_TO string proxy_port {
482 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
483 (void) snprintf(proxy->port, sizeof(proxy->port),
487 | REQUIRE CLIENT CA string {
488 ensure_absolute_path($4);
489 proxy->reqca_path = $4;
492 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
499 proxy->noverifyname = !$2;
503 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
504 /* drop the starting '/' if any */
506 memmove($3, $3+1, strlen($3));
507 (void) strlcpy(loc->match, $3, sizeof(loc->match));
513 locopts : /* empty */
514 | locopts locopt optnl
517 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
518 | BLOCK RETURN NUM string {
520 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
521 loc->block_code = check_block_code($3);
525 (void) strlcpy(loc->block_fmt, "temporary failure",
526 sizeof(loc->block_fmt));
527 loc->block_code = check_block_code($3);
528 if ($3 >= 30 && $3 < 40)
529 yyerror("missing `meta' for block return %d", $3);
532 (void) strlcpy(loc->block_fmt, "temporary failure",
533 sizeof(loc->block_fmt));
534 loc->block_code = 40;
536 | DEFAULT TYPE string {
537 (void) strlcpy(loc->default_mime, $3,
538 sizeof(loc->default_mime));
543 (void) strlcpy(loc->index, $2, sizeof(loc->index));
547 (void) strlcpy(loc->lang, $2,
551 | LOG bool { loc->disable_log = !$2; }
552 | REQUIRE CLIENT CA string {
553 ensure_absolute_path($4);
554 loc->reqca_path = $4;
557 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
560 | STRIP NUM { loc->strip = check_strip_no($2); }
563 fastcgi : FASTCGI '{' optnl fastcgiopts '}'
570 yywarn("`fastcgi path' is deprecated. "
571 "Please use `fastcgi socket path' instead.");
572 loc->fcgi = fastcgi_conf($2, NULL);
577 fastcgiopts : /* empty */
578 | fastcgiopts fastcgiopt optnl
581 fastcgiopt : PARAM string '=' string {
585 loc->fcgi = fastcgi_conf($2, NULL);
588 | SOCKET TCP string PORT NUM {
591 if (asprintf(&c, "%d", $5) == -1)
593 loc->fcgi = fastcgi_conf($3, c);
597 | SOCKET TCP string {
598 loc->fcgi = fastcgi_conf($3, "9000");
600 | SOCKET TCP string PORT string {
601 loc->fcgi = fastcgi_conf($3, $5);
606 loc->fcgi_strip = $2;
610 types : TYPES '{' optnl mediaopts_l '}' ;
612 mediaopts_l : mediaopts_l mediaoptsl nl
616 mediaoptsl : STRING {
623 medianames_l : medianames_l medianamesl
627 medianamesl : numberstring {
628 if (add_mime(&conf->mime, current_media, $1) == -1)
644 static const struct keyword {
648 /* these MUST be sorted */
657 {"combined", COMBINED},
659 {"condensed", CONDENSED},
660 {"default", DEFAULT},
661 {"facility", FACILITY},
662 {"fastcgi", FASTCGI},
663 {"for-host", FOR_HOST},
664 {"include", INCLUDE},
671 {"location", LOCATION},
678 {"prefork", PREFORK},
680 {"protocols", PROTOCOLS},
682 {"relay-to", RELAY_TO},
683 {"require", REQUIRE},
696 {"use-tls", USE_TLS},
698 {"verifyname", VERIFYNAME},
702 yyerror(const char *msg, ...)
709 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
710 vfprintf(stderr, msg, ap);
711 fprintf(stderr, "\n");
716 yywarn(const char *msg, ...)
721 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
722 vfprintf(stderr, msg, ap);
723 fprintf(stderr, "\n");
728 kw_cmp(const void *k, const void *e)
730 return strcmp(k, ((struct keyword *)e)->word);
736 const struct keyword *p;
738 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
739 sizeof(keywords[0]), kw_cmp);
747 #define START_EXPAND 1
748 #define DONE_EXPAND 2
750 static int expanding;
758 if (file->ungetpos > 0)
759 c = file->ungetbuf[--file->ungetpos];
761 c = getc(file->stream);
763 if (c == START_EXPAND)
765 else if (c == DONE_EXPAND)
779 if ((c = igetc()) == EOF) {
780 yyerror("reached end of file while parsing "
782 if (file == topfile || popfile() == EOF)
789 while ((c = igetc()) == '\\') {
795 yylval.lineno = file->lineno;
801 * Fake EOL when hit EOF for the first time. This gets line
802 * count right if last line in included file is syntactically
803 * invalid and has no newline.
805 if (file->eof_reached == 0) {
806 file->eof_reached = 1;
810 if (file == topfile || popfile() == EOF)
824 if (file->ungetpos >= file->ungetsize) {
825 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
829 file->ungetsize *= 2;
831 file->ungetbuf[file->ungetpos++] = c;
839 /* Skip to either EOF or the first real EOL. */
862 while ((c = lgetc(0)) == ' ' || c == '\t')
865 yylval.lineno = file->lineno;
867 while ((c = lgetc(0)) != '\n' && c != EOF)
869 if (c == '$' && !expanding) {
871 if ((c = lgetc(0)) == EOF)
873 if (p + 1 >= buf + sizeof(buf) -1) {
874 yyerror("string too long");
877 if (isalnum(c) || c == '_') {
887 yyerror("macro `%s' not defined", buf);
890 yylval.v.string = xstrdup(val);
893 if (c == '@' && !expanding) {
895 if ((c = lgetc(0)) == EOF)
898 if (p + 1 >= buf + sizeof(buf) - 1) {
899 yyerror("string too long");
902 if (isalnum(c) || c == '_') {
912 yyerror("macro '%s' not defined", buf);
915 p = val + strlen(val) - 1;
916 lungetc(DONE_EXPAND);
921 lungetc(START_EXPAND);
930 if ((c = lgetc(quotec)) == EOF)
935 } else if (c == '\\') {
936 if ((next = lgetc(quotec)) == EOF)
938 if (next == quotec || next == ' ' ||
941 else if (next == '\n') {
946 } else if (c == quotec) {
949 } else if (c == '\0') {
950 yyerror("invalid syntax");
953 if (p + 1 >= buf + sizeof(buf) - 1) {
954 yyerror("string too long");
959 yylval.v.string = strdup(buf);
960 if (yylval.v.string == NULL)
961 fatal("yylex: strdup");
965 #define allowed_to_end_number(x) \
966 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
968 if (c == '-' || isdigit(c)) {
971 if ((size_t)(p-buf) >= sizeof(buf)) {
972 yyerror("string too long");
975 } while ((c = lgetc(0)) != EOF && isdigit(c));
977 if (p == buf + 1 && buf[0] == '-')
979 if (c == EOF || allowed_to_end_number(c)) {
980 const char *errstr = NULL;
983 yylval.v.number = strtonum(buf, LLONG_MIN,
986 yyerror("\"%s\" invalid number: %s",
1001 #define allowed_in_string(x) \
1002 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1003 x != '{' && x != '}' && \
1004 x != '!' && x != '=' && x != '#' && \
1005 x != ',' && x != ';'))
1007 if (isalnum(c) || c == ':' || c == '_') {
1010 if ((size_t)(p-buf) >= sizeof(buf)) {
1011 yyerror("string too long");
1014 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
1017 if ((token = lookup(buf)) == STRING)
1018 yylval.v.string = xstrdup(buf);
1022 yylval.lineno = file->lineno;
1031 pushfile(const char *name, int secret)
1035 nfile = xcalloc(1, sizeof(*nfile));
1036 nfile->name = xstrdup(name);
1037 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
1038 log_warn("can't open %s", nfile->name);
1043 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
1044 nfile->ungetsize = 16;
1045 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
1046 TAILQ_INSERT_TAIL(&files, nfile, entry);
1055 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
1056 prev->errors += file->errors;
1058 TAILQ_REMOVE(&files, file, entry);
1059 fclose(file->stream);
1061 free(file->ungetbuf);
1064 return file ? 0 : EOF;
1068 parse_conf(struct conf *c, const char *filename)
1070 struct sym *sym, *next;
1072 default_host = NULL;
1073 default_port = 1965;
1077 file = pushfile(filename, 0);
1083 errors = file->errors;
1086 /* Free macros and check which have not been used. */
1087 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1088 /* TODO: warn if !sym->used */
1089 if (!sym->persist) {
1092 TAILQ_REMOVE(&symhead, sym, entry);
1103 symset(const char *name, const char *val, int persist)
1107 TAILQ_FOREACH(sym, &symhead, entry) {
1108 if (!strcmp(name, sym->name))
1118 TAILQ_REMOVE(&symhead, sym, entry);
1123 sym = xcalloc(1, sizeof(*sym));
1124 sym->name = xstrdup(name);
1125 sym->val = xstrdup(val);
1127 sym->persist = persist;
1129 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1134 cmdline_symset(char *s)
1139 if ((val = strrchr(s, '=')) == NULL)
1141 sym = xcalloc(1, val - s + 1);
1142 memcpy(sym, s, val - s);
1143 ret = symset(sym, val + 1, 1);
1149 symget(const char *nam)
1153 TAILQ_FOREACH(sym, &symhead, entry) {
1154 if (strcmp(nam, sym->name) == 0) {
1163 ensure_absolute_path(char *path)
1165 if (path == NULL || *path != '/')
1166 yyerror("not an absolute path: %s", path);
1171 check_block_code(int n)
1173 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1174 yyerror("invalid block code %d", n);
1179 check_block_fmt(char *fmt)
1183 for (s = fmt; *s; ++s) {
1194 yyerror("invalid format specifier %%%c", *s);
1202 check_strip_no(int n)
1205 yyerror("invalid strip number %d", n);
1210 check_port_num(int n)
1212 if (n <= 0 || n >= UINT16_MAX)
1213 yyerror("port number is %s: %d",
1214 n <= 0 ? "too small" : "too large",
1220 check_prefork_num(int n)
1222 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1223 yyerror("invalid prefork number %d", n);
1230 loc = new_location();
1231 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1237 proxy = new_proxy();
1238 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1242 parsehp(char *str, char **host, const char **port, const char *def)
1249 if ((at = strchr(str, ':')) != NULL) {
1255 strtonum(*port, 1, UINT16_MAX, &errstr);
1257 yyerror("port is %s: %s", errstr, *port);
1261 fastcgi_conf(const char *path, const char *port)
1266 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1267 if (!strcmp(f->path, path) &&
1268 ((port == NULL && *f->port == '\0') ||
1269 !strcmp(f->port, port)))
1274 f = xcalloc(1, sizeof(*f));
1276 (void)strlcpy(f->path, path, sizeof(f->path));
1278 (void)strlcpy(f->port, port, sizeof(f->port));
1279 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1285 add_param(char *name, char *val)
1288 struct envhead *h = &loc->params;
1290 e = xcalloc(1, sizeof(*e));
1291 (void) strlcpy(e->name, name, sizeof(e->name));
1292 (void) strlcpy(e->value, val, sizeof(e->value));
1293 TAILQ_INSERT_TAIL(h, e, envs);
1297 getservice(const char *n)
1303 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1305 s = getservbyname(n, "tcp");
1307 s = getservbyname(n, "udp");
1310 return (ntohs(s->s_port));
1313 return ((unsigned short)llval);
1317 add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1319 struct address *addr;
1320 struct sockaddr_in *sin;
1321 struct sockaddr_in6 *sin6;
1323 if (ai->ai_addrlen > sizeof(addr->ss))
1324 fatalx("ai_addrlen larger than a sockaddr_storage");
1326 TAILQ_FOREACH(addr, a, addrs) {
1327 if (addr->ai_flags == ai->ai_flags &&
1328 addr->ai_family == ai->ai_family &&
1329 addr->ai_socktype == ai->ai_socktype &&
1330 addr->ai_protocol == ai->ai_protocol &&
1331 addr->slen == ai->ai_addrlen &&
1332 !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1336 addr = xcalloc(1, sizeof(*addr));
1337 addr->ai_flags = ai->ai_flags;
1338 addr->ai_family = ai->ai_family;
1339 addr->ai_socktype = ai->ai_socktype;
1340 addr->ai_protocol = ai->ai_protocol;
1341 addr->slen = ai->ai_addrlen;
1342 memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1345 switch (addr->ai_family) {
1347 sin = (struct sockaddr_in *)&addr->ss;
1348 addr->port = ntohs(sin->sin_port);
1351 sin6 = (struct sockaddr_in6 *)&addr->ss;
1352 addr->port = ntohs(sin6->sin6_port);
1355 fatalx("unknown socket family %d", addr->ai_family);
1360 TAILQ_INSERT_HEAD(a, addr, addrs);
1364 listen_on(const char *hostname, const char *servname)
1366 struct addrinfo hints, *res, *res0;
1369 memset(&hints, 0, sizeof(hints));
1370 hints.ai_family = AF_UNSPEC;
1371 hints.ai_socktype = SOCK_STREAM;
1372 hints.ai_flags = AI_PASSIVE;
1373 error = getaddrinfo(hostname, servname, &hints, &res0);
1375 yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1376 gai_strerror(errno));
1380 for (res = res0; res; res = res->ai_next) {
1381 add_to_addr_queue(&host->addrs, res);
1382 add_to_addr_queue(&conf->addrs, res);