Remove call to obsolete gethostbyname, using getaddrinfo

Differential Revision: https://reviews.llvm.org/D67230

llvm-svn: 371195
This commit is contained in:
Serge Guelton 2019-09-06 11:06:23 +00:00
parent 2ebd24cc13
commit 90d32df7db
1 changed files with 10 additions and 4 deletions

View File

@ -32,10 +32,16 @@ bool HostInfoPosix::GetHostname(std::string &s) {
char hostname[PATH_MAX];
hostname[sizeof(hostname) - 1] = '\0';
if (::gethostname(hostname, sizeof(hostname) - 1) == 0) {
struct hostent *h = ::gethostbyname(hostname);
if (h)
s.assign(h->h_name);
else
struct addrinfo hints;
struct addrinfo *res = nullptr;
std::memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_CANONNAME;
int err = ::getaddrinfo(hostname, nullptr, &hints, &res);
if (err == 0) {
assert(res->ai_canonname && "getaddrinfo found a canonical name");
s.assign(res->ai_canonname);
freeaddrinfo(res);
} else
s.assign(hostname);
return true;
}