mirror of https://github.com/omar-polo/gmid.git
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:
parent
b2782022c9
commit
cd12ad1132
3
gmid.h
3
gmid.h
|
@ -114,6 +114,9 @@ struct address {
|
||||||
socklen_t slen;
|
socklen_t slen;
|
||||||
int16_t port;
|
int16_t port;
|
||||||
|
|
||||||
|
/* pretty-printed version of `ss' */
|
||||||
|
char pp[NI_MAXHOST];
|
||||||
|
|
||||||
/* used in the server */
|
/* used in the server */
|
||||||
struct conf *conf;
|
struct conf *conf;
|
||||||
int sock;
|
int sock;
|
||||||
|
|
14
parse.y
14
parse.y
|
@ -1280,7 +1280,7 @@ getservice(const char *n)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
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 address *addr;
|
||||||
struct sockaddr_in *sin;
|
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->ai_protocol = ai->ai_protocol;
|
||||||
addr->slen = ai->ai_addrlen;
|
addr->slen = ai->ai_addrlen;
|
||||||
memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
|
memcpy(&addr->ss, ai->ai_addr, ai->ai_addrlen);
|
||||||
|
strlcpy(addr->pp, pp, sizeof(addr->pp));
|
||||||
|
|
||||||
/* for commodity */
|
/* for commodity */
|
||||||
switch (addr->ai_family) {
|
switch (addr->ai_family) {
|
||||||
|
@ -1330,6 +1331,7 @@ void
|
||||||
listen_on(const char *hostname, const char *servname)
|
listen_on(const char *hostname, const char *servname)
|
||||||
{
|
{
|
||||||
struct addrinfo hints, *res, *res0;
|
struct addrinfo hints, *res, *res0;
|
||||||
|
char pp[NI_MAXHOST];
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
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) {
|
for (res = res0; res; res = res->ai_next) {
|
||||||
add_to_addr_queue(&host->addrs, res);
|
if (getnameinfo(res->ai_addr, res->ai_addrlen, pp, sizeof(pp),
|
||||||
add_to_addr_queue(&conf->addrs, res);
|
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);
|
freeaddrinfo(res0);
|
||||||
|
|
11
server.c
11
server.c
|
@ -119,15 +119,8 @@ match_host(struct vhost *v, struct client *c)
|
||||||
if (addr == NULL)
|
if (addr == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (*c->domain == '\0') {
|
if (*c->domain == '\0')
|
||||||
if (getnameinfo((struct sockaddr *)&addr->ss, addr->slen,
|
strlcpy(c->domain, addr->pp, sizeof(c->domain));
|
||||||
c->domain, sizeof(c->domain), NULL, 0,
|
|
||||||
NI_NUMERICHOST) == -1) {
|
|
||||||
log_warn("failed to fill the domain; getnameinfo");
|
|
||||||
*c->domain = '\0';
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matches(v->domain, c->domain))
|
if (matches(v->domain, c->domain))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Reference in New Issue