From: Klemens Nanni Subject: Re: remove host, host_v4 and host_v6; rename host_dns -> host To: Omar Polo , gameoftrees@openbsd.org Date: Mon, 29 May 2023 17:24:37 +0000 On Mon, May 29, 2023 at 06:35:02PM +0200, Omar Polo wrote: > This is an attempt at simplifying how host() works. host() is used in > the configuration file via get_addrs() to prepare a struct address > after a "listen on" directive > > The current code first tries to resolve it as an ipv4 address via > inet_pton(), then as an ipv6 address using getaddrinfo() with > AI_NUMERICHOST, then as an interface name and finally as a name to > resolve via getaddrinfo() again. > > Instead, scratch host(), host_v4() and host_v6() and rename host_dns() > to host(). It preserve the lookup via interface name, and then uses > getaddrinfo() only once wich already knows how to deal with IPv4/v6 > literals and resolving addresses too. Did got copy this code from somewhere initially? I recall doing this kind of host*() cleanup back in 2018 for base; afaict all got cleaned up/unified eventually, so there should be a few programs (with their own nuances) to compare against. > > ok? > > > diff 1adf41e8089257344579fba9c7e03d7ebc0c15b8 748f613ecec2c026f9351e11e7f25fd19d71e9d6 > commit - 1adf41e8089257344579fba9c7e03d7ebc0c15b8 > commit + 748f613ecec2c026f9351e11e7f25fd19d71e9d6 > blob - 3b73a07923fb6fec5efd9fea7f055fc7f917cfae > blob + d01669e79c40ee6a64a1ed544069b368ac00bac0 > --- gotwebd/parse.y > +++ gotwebd/parse.y > @@ -99,14 +99,10 @@ struct address *host_v4(const char *); > int addr_dup_check(struct addresslist *, struct address *, > const char *, const char *); > int add_addr(struct server *, struct address *); > -struct address *host_v4(const char *); > -struct address *host_v6(const char *); > -int host_dns(const char *, struct server *, > - int, in_port_t, const char *, int); > -int host_if(const char *, struct server *, > - int, in_port_t, const char *, int); > int host(const char *, struct server *, > int, in_port_t, const char *, int); > +int host_if(const char *, struct server *, > + int, in_port_t, const char *, int); > int is_if_in_group(const char *, const char *); > > typedef struct { > @@ -1001,50 +997,8 @@ struct address * > return (unsigned short)llval; > } > > -struct address * > -host_v4(const char *s) > -{ > - struct in_addr ina; > - struct sockaddr_in *sain; > - struct address *h; > - > - memset(&ina, 0, sizeof(ina)); > - if (inet_pton(AF_INET, s, &ina) != 1) > - return (NULL); > - > - if ((h = calloc(1, sizeof(*h))) == NULL) > - fatal(__func__); > - sain = (struct sockaddr_in *)&h->ss; > - got_sockaddr_inet_init(sain, &ina); > - return (h); > -} > - > -struct address * > -host_v6(const char *s) > -{ > - struct addrinfo hints, *res; > - struct sockaddr_in6 *sa_in6, *ra; > - struct address *h = NULL; > - > - memset(&hints, 0, sizeof(hints)); > - hints.ai_family = AF_INET6; > - hints.ai_socktype = SOCK_DGRAM; /* dummy */ > - hints.ai_flags = AI_NUMERICHOST; > - if (getaddrinfo(s, "0", &hints, &res) == 0) { > - if ((h = calloc(1, sizeof(*h))) == NULL) > - fatal(__func__); > - sa_in6 = (struct sockaddr_in6 *)&h->ss; > - ra = (struct sockaddr_in6 *)res->ai_addr; > - got_sockaddr_inet6_init(sa_in6, &ra->sin6_addr, > - ra->sin6_scope_id); > - freeaddrinfo(res); > - } > - > - return (h); > -} > - > int > -host_dns(const char *s, struct server *new_srv, int max, > +host(const char *s, struct server *new_srv, int max, > in_port_t port, const char *ifname, int ipproto) > { > struct addrinfo hints, *res0, *res; > @@ -1190,41 +1144,6 @@ host(const char *s, struct server *new_srv, int max, > } > > int > -host(const char *s, struct server *new_srv, int max, > - in_port_t port, const char *ifname, int ipproto) > -{ > - struct address *h; > - > - h = host_v4(s); > - > - /* IPv6 address? */ > - if (h == NULL) > - h = host_v6(s); > - > - if (h != NULL) { > - if (port) > - h->port = port; > - if (ifname != NULL) { > - if (strlcpy(h->ifname, ifname, sizeof(h->ifname)) >= > - sizeof(h->ifname)) { > - log_warnx("%s: interface name truncated", > - __func__); > - free(h); > - return (-1); > - } > - } > - if (ipproto != -1) > - h->ipproto = ipproto; > - > - if (add_addr(new_srv, h)) > - return -1; > - return (1); > - } > - > - return (host_dns(s, new_srv, max, port, ifname, ipproto)); > -} > - > -int > is_if_in_group(const char *ifname, const char *groupname) > { > unsigned int len; >