Get rid of getnameinfo and use inet_ntop instead

Compiling quark against musl slowed down request-times considerably.
After further analysis, I found out that the library does a DNS-
request on each address passed to getnameinfo.
Given we chroot into a folder, the /etc/resolv.conf was missing,
which led to the really long response-times (~3-5s).
After hardlinking the /etc/resolv.conf inside the chroot, the
times dropped to ~200ms, as now the library knew which NS to
contact directly.

This obviously isn't fast enough.

Thanks to Hiltjo's useful tips I rewrote the section using
inet_ntop (POSIX 2001).
Now the response-times are back to 1-2ms and we don't need
to copy /etc/resolv.conf everywhere we go.

FYI: This is not a bug in musl, but rather different behaviour.
This commit is contained in:
FRIGN 2014-08-11 16:16:37 +02:00
parent 55912e14bf
commit 9e2662c5e9

18
quark.c
View file

@ -12,6 +12,7 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h>
@ -448,8 +449,21 @@ serve(int fd) {
result = fork();
if (result == 0) {
close(fd);
host[0] = 0;
getnameinfo(&sa, salen, host, sizeof host, NULL, 0, NI_NOFQDN);
/* get host */
switch(sa.sa_family) {
case AF_INET:
inet_ntop(AF_INET, &(((struct sockaddr_in *)&sa)->sin_addr),
host, sizeof host);
break;
case AF_INET6:
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)&sa)->sin6_addr),
host, sizeof host);
break;
default:
host[0] = 0;
}
result = request();
shutdown(req.fd, SHUT_RD);
status = -1;