From 66b9f2db503fea66c560baa1bd7fcfa69c27172e Mon Sep 17 00:00:00 2001 From: Carsten Patzke <carsten.patzke@desy.de> Date: Fri, 18 Sep 2020 13:40:40 +0200 Subject: [PATCH] Made ResolveHostnameToIp thread safe --- common/cpp/src/system_io/system_io.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/common/cpp/src/system_io/system_io.cpp b/common/cpp/src/system_io/system_io.cpp index 29c213017..51ac70d75 100644 --- a/common/cpp/src/system_io/system_io.cpp +++ b/common/cpp/src/system_io/system_io.cpp @@ -5,7 +5,7 @@ #include <cerrno> #include <cstring> #include <algorithm> - +#include <mutex> #if defined(__linux__) || defined (__APPLE__) #include <sys/socket.h> @@ -290,8 +290,11 @@ int SystemIO::FileOpenModeToPosixFileOpenMode(int open_flags) const { } std::string SystemIO::ResolveHostnameToIp(const std::string& hostname, Error* err) const { - hostent* record = gethostbyname(hostname.c_str()); - if (record == nullptr) { + static std::mutex lock; + std::unique_lock<std::mutex> local_lock(lock); + + const hostent* record = gethostbyname(hostname.c_str()); // gethostbyname seems not to be thread safe! + if (record == nullptr || record->h_addr == nullptr) { *err = IOErrorTemplates::kUnableToResolveHostname.Generate(); return ""; } @@ -312,12 +315,10 @@ std::unique_ptr<sockaddr_in> SystemIO::BuildSockaddrIn(const std::string& addres uint16_t port = 0; std::tie(host, port) = *hostname_port_tuple; -// this is not thread safe call we should not resolve hostname here - we actually already have ip in address. -// todo: remove this -// host = ResolveHostnameToIp(host, err); -// if (*err != nullptr) { -// return nullptr; -// } + host = ResolveHostnameToIp(host, err); + if (*err != nullptr) { + return nullptr; + } short family = AddressFamilyToPosixFamily(AddressFamilies::INET); if (family == -1) { -- GitLab