From: Arjen de Korte Date: Fri, 31 Jan 2025 12:38:55 +0000 (+0100) Subject: lib: hostpid - Use getaddrinfo instead of gethostbyname X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fd8cf6a6499f9c2821244864d4a507d7da1bb55f;p=thirdparty%2Fdovecot%2Fcore.git lib: hostpid - Use getaddrinfo instead of gethostbyname gethostbyname is obsolote, so we should stop using it. --- diff --git a/src/lib/hostpid.c b/src/lib/hostpid.c index 3e91ade569..68ae2d7838 100644 --- a/src/lib/hostpid.c +++ b/src/lib/hostpid.c @@ -50,20 +50,30 @@ void hostpid_deinit(void) const char *my_hostdomain(void) { - struct hostent *hent; const char *name; + const struct addrinfo hints = { + .ai_family = AF_UNSPEC, + .ai_socktype = SOCK_STREAM, + .ai_flags = AI_CANONNAME, + }; + struct addrinfo *res; - if (my_domain == NULL) { - name = getenv(MY_HOSTDOMAIN_ENV); - if (name == NULL) { - hent = gethostbyname(my_hostname); - name = hent != NULL ? hent->h_name : NULL; - if (name == NULL) { - /* failed, use just the hostname */ - name = my_hostname; - } - } + if (my_domain != NULL) + return my_domain; + + name = getenv(MY_HOSTDOMAIN_ENV); + if (name != NULL) { my_domain = i_strdup(name); + return my_domain; + } + + if (getaddrinfo(my_hostname, NULL, &hints, &res) == 0) { + my_domain = i_strdup(res->ai_canonname); + freeaddrinfo(res); + return my_domain; } + + /* failed, use just the hostname */ + my_domain = i_strdup(my_hostname); return my_domain; }