pretty-print the socket address at configuration parsing time

saves a getnameinfo(NI_NUMERICHOST) at runtime, even if it's pretty
cheap.
This commit is contained in:
Omar Polo 2024-05-29 08:03:59 +00:00
parent b2782022c9
commit cd12ad1132
3 changed files with 16 additions and 12 deletions

3
gmid.h
View File

@ -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;

14
parse.y
View File

@ -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);

View File

@ -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;