diff --git a/gmid.h b/gmid.h index d098fd4..21d793c 100644 --- a/gmid.h +++ b/gmid.h @@ -114,6 +114,9 @@ struct address { socklen_t slen; int16_t port; + /* pretty-printed version of `ss' */ + char pp[NI_MAXHOST]; + /* used in the server */ struct conf *conf; int sock; diff --git a/parse.y b/parse.y index 577af2f..ef7ef67 100644 --- a/parse.y +++ b/parse.y @@ -1280,7 +1280,7 @@ getservice(const char *n) } static void -add_to_addr_queue(struct addrhead *a, struct addrinfo *ai) +add_to_addr_queue(struct addrhead *a, struct addrinfo *ai, const char *pp) { struct address *addr; struct sockaddr_in *sin; @@ -1306,6 +1306,7 @@ add_to_addr_queue(struct addrhead *a, struct addrinfo *ai) addr->ai_protocol = ai->ai_protocol; addr->slen = ai->ai_addrlen; memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen); + strlcpy(addr->pp, pp, sizeof(addr->pp)); /* for commodity */ switch (addr->ai_family) { @@ -1330,6 +1331,7 @@ void listen_on(const char *hostname, const char *servname) { struct addrinfo hints, *res, *res0; + char pp[NI_MAXHOST]; int error; memset(&hints, 0, sizeof(hints)); @@ -1344,8 +1346,14 @@ listen_on(const char *hostname, const char *servname) } for (res = res0; res; res = res->ai_next) { - add_to_addr_queue(&host->addrs, res); - add_to_addr_queue(&conf->addrs, res); + if (getnameinfo(res->ai_addr, res->ai_addrlen, pp, sizeof(pp), + NULL, 0, NI_NUMERICHOST) == -1) { + yyerror("getnameinfo failed: %s", strerror(errno)); + break; + } + + add_to_addr_queue(&host->addrs, res, pp); + add_to_addr_queue(&conf->addrs, res, pp); } freeaddrinfo(res0); diff --git a/server.c b/server.c index b9bb0d8..8e39912 100644 --- a/server.c +++ b/server.c @@ -119,15 +119,8 @@ match_host(struct vhost *v, struct client *c) if (addr == NULL) return 0; - if (*c->domain == '\0') { - if (getnameinfo((struct sockaddr *)&addr->ss, addr->slen, - c->domain, sizeof(c->domain), NULL, 0, - NI_NUMERICHOST) == -1) { - log_warn("failed to fill the domain; getnameinfo"); - *c->domain = '\0'; - return 0; - } - } + if (*c->domain == '\0') + strlcpy(c->domain, addr->pp, sizeof(c->domain)); if (matches(v->domain, c->domain)) return 1;