From: Vincent Bernat Date: Wed, 28 Jan 2015 13:55:31 +0000 (+0100) Subject: priv: replace the use of `gethostbyname()` by `getaddrinfo()` X-Git-Tag: 0.7.14~43 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=65e5293aeedfca68c4fd1f7f1bf22d43ca65e2ad;p=thirdparty%2Flldpd.git priv: replace the use of `gethostbyname()` by `getaddrinfo()` `gethostbyname()` was uses to get the canonical name. `getaddrinfo()` should work even if there is no IPv4 at all in the system. --- diff --git a/src/daemon/priv.c b/src/daemon/priv.c index f660bafd..0d9ea4a2 100644 --- a/src/daemon/priv.c +++ b/src/daemon/priv.c @@ -35,7 +35,6 @@ #include #include #include -#include #include #if defined HOST_OS_FREEBSD || HOST_OS_OSX || HOST_OS_DRAGONFLY @@ -216,11 +215,14 @@ static void asroot_gethostname() { struct utsname un; - struct hostent *hp; + struct addrinfo hints = { + .ai_flags = AI_CANONNAME + }; + struct addrinfo *res; int len; if (uname(&un) < 0) fatal("privsep", "failed to get system information"); - if ((hp = gethostbyname(un.nodename)) == NULL) { + if (getaddrinfo(un.nodename, NULL, &hints, &res) != 0) { log_info("privsep", "unable to get system name"); #ifdef HAVE_RES_INIT res_init(); @@ -229,9 +231,10 @@ asroot_gethostname() must_write(PRIV_PRIVILEGED, &len, sizeof(int)); must_write(PRIV_PRIVILEGED, un.nodename, len + 1); } else { - len = strlen(hp->h_name); + len = strlen(res->ai_canonname); must_write(PRIV_PRIVILEGED, &len, sizeof(int)); - must_write(PRIV_PRIVILEGED, hp->h_name, len + 1); + must_write(PRIV_PRIVILEGED, res->ai_canonname, len + 1); + freeaddrinfo(res); } }