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 = "*";
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
159 | conf error '\n' { file->errors++; }
162 include : INCLUDE STRING {
165 if ((nfile = pushfile($2, 0)) == NULL) {
166 yyerror("failed to include file %s", $2);
177 bool : ON { $$ = 1; }
181 string : string STRING {
182 if (asprintf(&$$, "%s%s", $1, $2) == -1) {
185 yyerror("string: asprintf: %s", strerror(errno));
196 if (asprintf(&s, "%d", $1) == -1) {
197 yyerror("asprintf: number");
205 varset : STRING '=' string {
208 if (isspace((unsigned char)*s)) {
209 yyerror("macro name cannot contain "
222 option : CHROOT string {
223 if (strlcpy(conf->chroot, $2, sizeof(conf->chroot)) >=
224 sizeof(conf->chroot))
225 yyerror("chroot path too long");
229 yywarn("option `ipv6' is deprecated,"
230 " please use `listen on'");
234 default_host = "0.0.0.0";
238 yywarn("option `port' is deprecated,"
239 " please use `listen on'");
242 | PREFORK NUM { conf->prefork = check_prefork_num($2); }
244 if (tls_config_parse_protocols(&conf->protos, $2) == -1)
245 yyerror("invalid protocols string \"%s\"", $2);
249 if (strlcpy(conf->user, $2, sizeof(conf->user)) >=
251 yyerror("user name too long");
256 log : LOG '{' optnl logopts '}'
260 logopts : /* empty */
261 | logopts logopt optnl
264 logopt : ACCESS string {
265 free(conf->log_access);
266 conf->log_access = $2;
269 conf->log_format = LOG_FORMAT_COMMON;
272 conf->log_format = LOG_FORMAT_COMBINED;
275 conf->log_format = LOG_FORMAT_CONDENSED;
278 conf->log_format = LOG_FORMAT_LEGACY;
280 | SYSLOG FACILITY string {
281 const char *str = $3;
283 conf->log_syslog = 1;
285 if (!strncasecmp(str, "LOG_", 4))
288 if (!strcasecmp(str, "daemon"))
289 conf->log_facility = LOG_DAEMON;
291 else if (!strcasecmp(str, "ftp"))
292 conf->log_facility = LOG_FTP;
294 else if (!strcasecmp(str, "local1"))
295 conf->log_facility = LOG_LOCAL1;
296 else if (!strcasecmp(str, "local2"))
297 conf->log_facility = LOG_LOCAL2;
298 else if (!strcasecmp(str, "local3"))
299 conf->log_facility = LOG_LOCAL3;
300 else if (!strcasecmp(str, "local4"))
301 conf->log_facility = LOG_LOCAL4;
302 else if (!strcasecmp(str, "local5"))
303 conf->log_facility = LOG_LOCAL5;
304 else if (!strcasecmp(str, "local6"))
305 conf->log_facility = LOG_LOCAL6;
306 else if (!strcasecmp(str, "local7"))
307 conf->log_facility = LOG_LOCAL7;
308 else if (!strcasecmp(str, "user"))
309 conf->log_facility = LOG_USER;
311 yywarn("unknown syslog facility `%s'",
317 conf->log_syslog = 0;
320 conf->log_syslog = 1;
324 vhost : SERVER string {
326 TAILQ_INSERT_HEAD(&conf->hosts, host, vhosts);
328 loc = new_location();
329 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
331 TAILQ_INIT(&host->proxies);
333 (void) strlcpy(loc->match, "*", sizeof(loc->match));
334 (void) strlcpy(host->domain, $2, sizeof(host->domain));
336 if (strstr($2, "xn--") != NULL) {
337 yywarn("\"%s\" looks like punycode: you "
338 "should use the decoded hostname", $2);
342 } '{' optnl servbody '}' {
343 if (host->cert_path == NULL ||
344 host->key_path == NULL)
345 yyerror("invalid vhost definition: %s",
347 if (TAILQ_EMPTY(&host->addrs)) {
351 r = snprintf(portno, sizeof(portno), "%d",
353 if (r < 0 || (size_t)r >= sizeof(portno))
356 yywarn("missing `listen on' in server %s,"
357 " assuming %s port %d", $2, 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");
406 | LISTEN ON listen_addr PORT STRING {
411 | LISTEN ON listen_addr PORT NUM {
415 r = snprintf(portno, sizeof(portno), "%d", $5);
416 if (r < 0 || (size_t)r >= sizeof(portno))
419 listen_on($3, portno);
425 proxy : PROXY { advance_proxy(); }
426 proxy_matches '{' optnl proxy_opts '}' {
427 if (*proxy->host == '\0')
428 yyerror("invalid proxy block: missing `relay-to' option");
430 if ((proxy->cert_path == NULL && proxy->key_path != NULL) ||
431 (proxy->cert_path != NULL && proxy->key_path == NULL))
432 yyerror("invalid proxy block: missing cert or key");
436 proxy_matches : /* empty */
437 | proxy_matches proxy_match
440 proxy_port : /* empty */ { $$ = 1965; }
442 if (($$ = getservice($2)) == -1)
443 yyerror("invalid port number %s", $2);
446 | PORT NUM { $$ = $2; }
449 proxy_match : PROTO string {
450 (void) strlcpy(proxy->match_proto, $2, sizeof(proxy->match_proto));
453 | FOR_HOST string proxy_port {
454 (void) strlcpy(proxy->match_host, $2, sizeof(proxy->match_host));
455 (void) snprintf(proxy->match_port, sizeof(proxy->match_port),
461 proxy_opts : /* empty */
462 | proxy_opts proxy_opt optnl
465 proxy_opt : CERT string {
467 ensure_absolute_path($2);
468 proxy->cert_path = $2;
472 ensure_absolute_path($2);
473 proxy->key_path = $2;
476 if (tls_config_parse_protocols(&proxy->protocols, $2) == -1)
477 yyerror("invalid protocols string \"%s\"", $2);
480 | RELAY_TO string proxy_port {
481 (void) strlcpy(proxy->host, $2, sizeof(proxy->host));
482 (void) snprintf(proxy->port, sizeof(proxy->port),
486 | REQUIRE CLIENT CA string {
487 ensure_absolute_path($4);
488 proxy->reqca_path = $4;
491 (void) strlcpy(proxy->sni, $2, sizeof(proxy->sni));
498 proxy->noverifyname = !$2;
502 location : LOCATION { advance_loc(); } string '{' optnl locopts '}' {
503 /* drop the starting '/' if any */
505 memmove($3, $3+1, strlen($3));
506 (void) strlcpy(loc->match, $3, sizeof(loc->match));
512 locopts : /* empty */
513 | locopts locopt optnl
516 locopt : AUTO INDEX bool { loc->auto_index = $3 ? 1 : -1; }
517 | BLOCK RETURN NUM string {
519 (void) strlcpy(loc->block_fmt, $4, sizeof(loc->block_fmt));
520 loc->block_code = check_block_code($3);
524 (void) strlcpy(loc->block_fmt, "temporary failure",
525 sizeof(loc->block_fmt));
526 loc->block_code = check_block_code($3);
527 if ($3 >= 30 && $3 < 40)
528 yyerror("missing `meta' for block return %d", $3);
531 (void) strlcpy(loc->block_fmt, "temporary failure",
532 sizeof(loc->block_fmt));
533 loc->block_code = 40;
535 | DEFAULT TYPE string {
536 (void) strlcpy(loc->default_mime, $3,
537 sizeof(loc->default_mime));
542 (void) strlcpy(loc->index, $2, sizeof(loc->index));
546 (void) strlcpy(loc->lang, $2,
550 | LOG bool { loc->disable_log = !$2; }
551 | REQUIRE CLIENT CA string {
552 ensure_absolute_path($4);
553 loc->reqca_path = $4;
556 (void) strlcpy(loc->dir, $2, sizeof(loc->dir));
559 | STRIP NUM { loc->strip = check_strip_no($2); }
562 fastcgi : FASTCGI '{' optnl fastcgiopts '}'
569 yywarn("`fastcgi path' is deprecated. "
570 "Please use `fastcgi socket path' instead.");
571 loc->fcgi = fastcgi_conf($2, NULL);
576 fastcgiopts : /* empty */
577 | fastcgiopts fastcgiopt optnl
580 fastcgiopt : PARAM string '=' string {
584 loc->fcgi = fastcgi_conf($2, NULL);
587 | SOCKET TCP string PORT NUM {
590 if (asprintf(&c, "%d", $5) == -1)
592 loc->fcgi = fastcgi_conf($3, c);
596 | SOCKET TCP string {
597 loc->fcgi = fastcgi_conf($3, "9000");
599 | SOCKET TCP string PORT string {
600 loc->fcgi = fastcgi_conf($3, $5);
605 loc->fcgi_strip = $2;
609 types : TYPES '{' optnl mediaopts_l '}' ;
611 mediaopts_l : mediaopts_l mediaoptsl nl
615 mediaoptsl : STRING {
618 } medianames_l optsemicolon
622 medianames_l : medianames_l medianamesl
626 medianamesl : numberstring {
627 if (add_mime(&conf->mime, current_media, $1) == -1)
636 optnl : '\n' optnl /* zero or more newlines */
637 | ';' optnl /* semicolons too */
647 static const struct keyword {
651 /* these MUST be sorted */
660 {"combined", COMBINED},
662 {"condensed", CONDENSED},
663 {"default", DEFAULT},
664 {"facility", FACILITY},
665 {"fastcgi", FASTCGI},
666 {"for-host", FOR_HOST},
667 {"include", INCLUDE},
674 {"location", LOCATION},
681 {"prefork", PREFORK},
683 {"protocols", PROTOCOLS},
685 {"relay-to", RELAY_TO},
686 {"require", REQUIRE},
699 {"use-tls", USE_TLS},
701 {"verifyname", VERIFYNAME},
705 yyerror(const char *msg, ...)
712 fprintf(stderr, "%s:%d error: ", config_path, yylval.lineno);
713 vfprintf(stderr, msg, ap);
714 fprintf(stderr, "\n");
719 yywarn(const char *msg, ...)
724 fprintf(stderr, "%s:%d warning: ", config_path, yylval.lineno);
725 vfprintf(stderr, msg, ap);
726 fprintf(stderr, "\n");
731 kw_cmp(const void *k, const void *e)
733 return strcmp(k, ((struct keyword *)e)->word);
739 const struct keyword *p;
741 p = bsearch(s, keywords, sizeof(keywords)/sizeof(keywords[0]),
742 sizeof(keywords[0]), kw_cmp);
750 #define START_EXPAND 1
751 #define DONE_EXPAND 2
753 static int expanding;
761 if (file->ungetpos > 0)
762 c = file->ungetbuf[--file->ungetpos];
764 c = getc(file->stream);
766 if (c == START_EXPAND)
768 else if (c == DONE_EXPAND)
782 if ((c = igetc()) == EOF) {
783 yyerror("reached end of file while parsing "
785 if (file == topfile || popfile() == EOF)
792 while ((c = igetc()) == '\\') {
798 yylval.lineno = file->lineno;
804 * Fake EOL when hit EOF for the first time. This gets line
805 * count right if last line in included file is syntactically
806 * invalid and has no newline.
808 if (file->eof_reached == 0) {
809 file->eof_reached = 1;
813 if (file == topfile || popfile() == EOF)
827 if (file->ungetpos >= file->ungetsize) {
828 void *p = reallocarray(file->ungetbuf, file->ungetsize, 2);
832 file->ungetsize *= 2;
834 file->ungetbuf[file->ungetpos++] = c;
842 /* Skip to either EOF or the first real EOL. */
865 while ((c = lgetc(0)) == ' ' || c == '\t')
868 yylval.lineno = file->lineno;
870 while ((c = lgetc(0)) != '\n' && c != EOF)
872 if (c == '$' && !expanding) {
874 if ((c = lgetc(0)) == EOF)
876 if (p + 1 >= buf + sizeof(buf) -1) {
877 yyerror("string too long");
880 if (isalnum(c) || c == '_') {
890 yyerror("macro `%s' not defined", buf);
893 yylval.v.string = xstrdup(val);
896 if (c == '@' && !expanding) {
898 if ((c = lgetc(0)) == EOF)
901 if (p + 1 >= buf + sizeof(buf) - 1) {
902 yyerror("string too long");
905 if (isalnum(c) || c == '_') {
915 yyerror("macro '%s' not defined", buf);
918 p = val + strlen(val) - 1;
919 lungetc(DONE_EXPAND);
924 lungetc(START_EXPAND);
933 if ((c = lgetc(quotec)) == EOF)
938 } else if (c == '\\') {
939 if ((next = lgetc(quotec)) == EOF)
941 if (next == quotec || next == ' ' ||
944 else if (next == '\n') {
949 } else if (c == quotec) {
952 } else if (c == '\0') {
953 yyerror("invalid syntax");
956 if (p + 1 >= buf + sizeof(buf) - 1) {
957 yyerror("string too long");
962 yylval.v.string = strdup(buf);
963 if (yylval.v.string == NULL)
964 fatal("yylex: strdup");
968 #define allowed_to_end_number(x) \
969 (isspace(x) || x == ')' || x ==',' || x == '/' || x == '}' || x == '=')
971 if (c == '-' || isdigit(c)) {
974 if ((size_t)(p-buf) >= sizeof(buf)) {
975 yyerror("string too long");
978 } while ((c = lgetc(0)) != EOF && isdigit(c));
980 if (p == buf + 1 && buf[0] == '-')
982 if (c == EOF || allowed_to_end_number(c)) {
983 const char *errstr = NULL;
986 yylval.v.number = strtonum(buf, LLONG_MIN,
989 yyerror("\"%s\" invalid number: %s",
1004 #define allowed_in_string(x) \
1005 (isalnum(x) || (ispunct(x) && x != '(' && x != ')' && \
1006 x != '{' && x != '}' && \
1007 x != '!' && x != '=' && x != '#' && \
1008 x != ',' && x != ';'))
1010 if (isalnum(c) || c == ':' || c == '_') {
1013 if ((size_t)(p-buf) >= sizeof(buf)) {
1014 yyerror("string too long");
1017 } while ((c = lgetc(0)) != EOF && (allowed_in_string(c)));
1020 if ((token = lookup(buf)) == STRING)
1021 yylval.v.string = xstrdup(buf);
1025 yylval.lineno = file->lineno;
1034 pushfile(const char *name, int secret)
1038 nfile = xcalloc(1, sizeof(*nfile));
1039 nfile->name = xstrdup(name);
1040 if ((nfile->stream = fopen(nfile->name, "r")) == NULL) {
1041 log_warn("can't open %s", nfile->name);
1046 nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0;
1047 nfile->ungetsize = 16;
1048 nfile->ungetbuf = xcalloc(1, nfile->ungetsize);
1049 TAILQ_INSERT_TAIL(&files, nfile, entry);
1058 if ((prev = TAILQ_PREV(file, files, entry)) != NULL)
1059 prev->errors += file->errors;
1061 TAILQ_REMOVE(&files, file, entry);
1062 fclose(file->stream);
1064 free(file->ungetbuf);
1067 return file ? 0 : EOF;
1071 parse_conf(struct conf *c, const char *filename)
1073 struct sym *sym, *next;
1076 default_port = 1965;
1080 file = pushfile(filename, 0);
1086 errors = file->errors;
1089 /* Free macros and check which have not been used. */
1090 TAILQ_FOREACH_SAFE(sym, &symhead, entry, next) {
1091 /* TODO: warn if !sym->used */
1092 if (!sym->persist) {
1095 TAILQ_REMOVE(&symhead, sym, entry);
1106 symset(const char *name, const char *val, int persist)
1110 TAILQ_FOREACH(sym, &symhead, entry) {
1111 if (!strcmp(name, sym->name))
1121 TAILQ_REMOVE(&symhead, sym, entry);
1126 sym = xcalloc(1, sizeof(*sym));
1127 sym->name = xstrdup(name);
1128 sym->val = xstrdup(val);
1130 sym->persist = persist;
1132 TAILQ_INSERT_TAIL(&symhead, sym, entry);
1137 cmdline_symset(char *s)
1142 if ((val = strrchr(s, '=')) == NULL)
1144 sym = xcalloc(1, val - s + 1);
1145 memcpy(sym, s, val - s);
1146 ret = symset(sym, val + 1, 1);
1152 symget(const char *nam)
1156 TAILQ_FOREACH(sym, &symhead, entry) {
1157 if (strcmp(nam, sym->name) == 0) {
1166 ensure_absolute_path(char *path)
1168 if (path == NULL || *path != '/')
1169 yyerror("not an absolute path: %s", path);
1174 check_block_code(int n)
1176 if (n < 10 || n >= 70 || (n >= 20 && n <= 29))
1177 yyerror("invalid block code %d", n);
1182 check_block_fmt(char *fmt)
1186 for (s = fmt; *s; ++s) {
1197 yyerror("invalid format specifier %%%c", *s);
1205 check_strip_no(int n)
1208 yyerror("invalid strip number %d", n);
1213 check_port_num(int n)
1215 if (n <= 0 || n >= UINT16_MAX)
1216 yyerror("port number is %s: %d",
1217 n <= 0 ? "too small" : "too large",
1223 check_prefork_num(int n)
1225 if (n <= 0 || n >= PROC_MAX_INSTANCES)
1226 yyerror("invalid prefork number %d", n);
1233 loc = new_location();
1234 TAILQ_INSERT_TAIL(&host->locations, loc, locations);
1240 proxy = new_proxy();
1241 TAILQ_INSERT_TAIL(&host->proxies, proxy, proxies);
1245 parsehp(char *str, char **host, const char **port, const char *def)
1252 if ((at = strchr(str, ':')) != NULL) {
1258 strtonum(*port, 1, UINT16_MAX, &errstr);
1260 yyerror("port is %s: %s", errstr, *port);
1264 fastcgi_conf(const char *path, const char *port)
1269 TAILQ_FOREACH(f, &conf->fcgi, fcgi) {
1270 if (!strcmp(f->path, path) &&
1271 ((port == NULL && *f->port == '\0') ||
1272 !strcmp(f->port, port)))
1277 f = xcalloc(1, sizeof(*f));
1279 (void)strlcpy(f->path, path, sizeof(f->path));
1281 (void)strlcpy(f->port, port, sizeof(f->port));
1282 TAILQ_INSERT_TAIL(&conf->fcgi, f, fcgi);
1288 add_param(char *name, char *val)
1291 struct envhead *h = &loc->params;
1293 e = xcalloc(1, sizeof(*e));
1294 (void) strlcpy(e->name, name, sizeof(e->name));
1295 (void) strlcpy(e->value, val, sizeof(e->value));
1296 TAILQ_INSERT_TAIL(h, e, envs);
1300 getservice(const char *n)
1306 llval = strtonum(n, 0, UINT16_MAX, &errstr);
1308 s = getservbyname(n, "tcp");
1310 s = getservbyname(n, "udp");
1313 return (ntohs(s->s_port));
1316 return ((unsigned short)llval);
1320 add_to_addr_queue(struct addrhead *a, struct addrinfo *ai)
1322 struct address *addr;
1323 struct sockaddr_in *sin;
1324 struct sockaddr_in6 *sin6;
1326 if (ai->ai_addrlen > sizeof(addr->ss))
1327 fatalx("ai_addrlen larger than a sockaddr_storage");
1329 TAILQ_FOREACH(addr, a, addrs) {
1330 if (addr->ai_flags == ai->ai_flags &&
1331 addr->ai_family == ai->ai_family &&
1332 addr->ai_socktype == ai->ai_socktype &&
1333 addr->ai_protocol == ai->ai_protocol &&
1334 addr->slen == ai->ai_addrlen &&
1335 !memcmp(&addr->ss, ai->ai_addr, addr->slen))
1339 addr = xcalloc(1, sizeof(*addr));
1340 addr->ai_flags = ai->ai_flags;
1341 addr->ai_family = ai->ai_family;
1342 addr->ai_socktype = ai->ai_socktype;
1343 addr->ai_protocol = ai->ai_protocol;
1344 addr->slen = ai->ai_addrlen;
1345 memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
1348 switch (addr->ai_family) {
1350 sin = (struct sockaddr_in *)&addr->ss;
1351 addr->port = ntohs(sin->sin_port);
1354 sin6 = (struct sockaddr_in6 *)&addr->ss;
1355 addr->port = ntohs(sin6->sin6_port);
1358 fatalx("unknown socket family %d", addr->ai_family);
1363 TAILQ_INSERT_HEAD(a, addr, addrs);
1367 listen_on(const char *hostname, const char *servname)
1369 struct addrinfo hints, *res, *res0;
1372 memset(&hints, 0, sizeof(hints));
1373 hints.ai_family = AF_UNSPEC;
1374 hints.ai_socktype = SOCK_STREAM;
1375 hints.ai_flags = AI_PASSIVE;
1376 error = getaddrinfo(hostname, servname, &hints, &res0);
1378 yyerror("listen on \"%s\" port %s: %s", hostname, servname,
1379 gai_strerror(errno));
1383 for (res = res0; res; res = res->ai_next) {
1384 add_to_addr_queue(&host->addrs, res);
1385 add_to_addr_queue(&conf->addrs, res);