diff options
author | Arkadiy Illarionov <qarkai@gmail.com> | 2019-03-16 15:55:01 +0300 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2019-03-27 09:42:36 +0100 |
commit | e5aa36f4f1bb63d2ff621f701291cca391aa53a4 (patch) | |
tree | 33ab31b8f8b8aeba0e009d6e7efabe6ff16be2da /sal/osl | |
parent | 74907a1f7702a9dab3ffc6f6072c6972cbbde4ea (diff) |
Replace gethostbyname_r with getaddrinfo
The gethostbyname*() functions are obsolete.
Change-Id: I14a55eba3f111a3280f23955ffd86843079c7e75
Reviewed-on: https://gerrit.libreoffice.org/69337
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'sal/osl')
-rw-r--r-- | sal/osl/unx/socket.cxx | 105 | ||||
-rw-r--r-- | sal/osl/unx/system.cxx | 89 | ||||
-rw-r--r-- | sal/osl/unx/system.hxx | 2 |
3 files changed, 46 insertions, 150 deletions
diff --git a/sal/osl/unx/socket.cxx b/sal/osl/unx/socket.cxx index 98bf54450e72..1dd62e330029 100644 --- a/sal/osl/unx/socket.cxx +++ b/sal/osl/unx/socket.cxx @@ -91,7 +91,7 @@ #define OSL_INVALID_SOCKET -1 #define OSL_SOCKET_ERROR -1 -/* Buffer size for gethostbyname */ +/* Buffer size for getnameinfo */ #define MAX_HOSTBUFFER_SIZE 2048 static const unsigned long FamilyMap[]= { @@ -558,26 +558,38 @@ oslSocketResult SAL_CALL osl_getAddrOfSocketAddr( oslSocketAddr pAddr, sal_Seque instead! */ -/* wrap around different interfaces to reentrant gethostbyname */ -static struct hostent* osl_gethostbyname_r ( - const char *name, struct hostent *result, - char *buffer, int buflen, int *h_errnop) -{ -#if defined(LINUX) || defined(ANDROID) || defined(FREEBSD) || defined(DRAGONFLY) - struct hostent *result_; /* will be the same as result */ - int e; - e = gethostbyname_r (name, result, buffer, buflen, - &result_, h_errnop); - return e ? nullptr : result_ ; -#elif defined(AIX) - *h_errnop = gethostbyname_r (name, result, (struct hostent_data *)buffer); - (void)buflen; - return *h_errnop ? NULL : result ; -#else - return gethostbyname_r( name, result, buffer, buflen, h_errnop); -#endif -} +namespace { + +struct oslAddrInfo +{ + addrinfo* pAddrInfoList = nullptr; + int nError; + + oslAddrInfo(const char* name, bool isInet = false) + { + addrinfo aHints; + memset(&aHints, 0, sizeof(addrinfo)); + if (isInet) + aHints.ai_family = AF_INET; + aHints.ai_flags = AI_CANONNAME; + + nError = getaddrinfo(name, nullptr, &aHints, &pAddrInfoList); + } + + ~oslAddrInfo() + { + if (nError == 0) + freeaddrinfo(pAddrInfoList); + } + const char* getHostName() + { + assert(pAddrInfoList); + return pAddrInfoList->ai_canonname; + } +}; + +} static bool isFullQualifiedDomainName (const sal_Char *pHostName) { /* a FQDN (aka 'hostname.domain.top_level_domain' ) @@ -597,19 +609,9 @@ static sal_Char* getFullQualifiedDomainName (const sal_Char *pHostName) } else { - struct hostent aHostByName; - struct hostent *pHostByName; - sal_Char pQualifiedHostBuffer[ MAX_HOSTBUFFER_SIZE ]; - int nErrorNo; - - pHostByName = osl_gethostbyname_r ( - pHostName, - &aHostByName, pQualifiedHostBuffer, - sizeof(pQualifiedHostBuffer), &nErrorNo ); - if (pHostByName != nullptr) - { - pFullQualifiedName = strdup(pHostByName->h_name); - } + oslAddrInfo aAddrInfo(pHostName); + if (aAddrInfo.nError == 0) + pFullQualifiedName = strdup(aAddrInfo.getHostName()); } return pFullQualifiedName; @@ -621,22 +623,17 @@ struct oslHostAddrImpl oslSocketAddr pSockAddr; }; -static oslHostAddr hostentToHostAddr (const struct hostent *he) +static oslHostAddr addrinfoToHostAddr (const addrinfo* ai) { - oslHostAddr pAddr= nullptr; - oslSocketAddr pSockAddr = nullptr; - - sal_Char *cn; - - if ((he == nullptr) || (he->h_name == nullptr) || (he->h_addr_list[0] == nullptr)) + if (!ai || !ai->ai_canonname || !ai->ai_addr) return nullptr; - cn = getFullQualifiedDomainName (he->h_name); + sal_Char* cn = getFullQualifiedDomainName(ai->ai_canonname); SAL_WARN_IF( !cn, "sal.osl", "couldn't get full qualified domain name" ); if (cn == nullptr) return nullptr; - pSockAddr = createSocketAddr(); + oslSocketAddr pSockAddr = createSocketAddr(); SAL_WARN_IF( !pSockAddr, "sal.osl", "insufficient memory" ); if (pSockAddr == nullptr) { @@ -644,14 +641,12 @@ static oslHostAddr hostentToHostAddr (const struct hostent *he) return nullptr; } - pSockAddr->m_sockaddr.sa_family= he->h_addrtype; - if (pSockAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet)) + if (ai->ai_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet)) { - struct sockaddr_in *sin= reinterpret_cast<sockaddr_in *>(&pSockAddr->m_sockaddr); memcpy ( - &(sin->sin_addr.s_addr), - he->h_addr_list[0], - he->h_length); + &(pSockAddr->m_sockaddr), + ai->ai_addr, + ai->ai_addrlen); } else { @@ -665,7 +660,7 @@ static oslHostAddr hostentToHostAddr (const struct hostent *he) return nullptr; } - pAddr= static_cast<oslHostAddr>(malloc(sizeof(struct oslHostAddrImpl))); + oslHostAddr pAddr = static_cast<oslHostAddr>(malloc(sizeof(struct oslHostAddrImpl))); SAL_WARN_IF( !pAddr, "sal.osl", "allocation error" ); if (pAddr == nullptr) { @@ -767,17 +762,9 @@ oslHostAddr SAL_CALL osl_createHostAddrByName(rtl_uString *ustrHostname) oslHostAddr osl_psz_createHostAddrByName (const sal_Char *pszHostname) { - struct hostent aHe; - struct hostent *pHe; - sal_Char heBuffer[ MAX_HOSTBUFFER_SIZE ]; - int nErrorNo; - - pHe = osl_gethostbyname_r ( - pszHostname, - &aHe, heBuffer, - sizeof(heBuffer), &nErrorNo ); + oslAddrInfo aAddrInfo(pszHostname, /* isInet */ true); - return hostentToHostAddr (pHe); + return addrinfoToHostAddr (aAddrInfo.pAddrInfoList); } oslHostAddr SAL_CALL osl_createHostAddrByAddr (const oslSocketAddr pAddr) diff --git a/sal/osl/unx/system.cxx b/sal/osl/unx/system.cxx index 2d58555b8307..f17a8c19493a 100644 --- a/sal/osl/unx/system.cxx +++ b/sal/osl/unx/system.cxx @@ -50,95 +50,6 @@ static pthread_mutex_t getrtl_mutex = PTHREAD_MUTEX_INITIALIZER; #endif //defined(MACOSX) -extern int h_errno; - -struct hostent *gethostbyname_r(const char *name, struct hostent *result, - char *buffer, size_t buflen, int *h_errnop) -{ - /* buffer layout: name\0 - * array_of_pointer_to_aliases - * NULL - * alias1\0...aliasn\0 - * array_of_pointer_to_addresses - * NULL - * addr1addr2addr3...addrn - */ - struct hostent* res; - - RTL_MUTEX_LOCK - - if ( (res = gethostbyname(name)) ) - { - int nname, naliases, naddr_list, naliasesdata; - char **p, **parray, *data; - - /* Check buffer size before copying, we want to leave the - * buffers unmodified in case something goes wrong. - * - * Is this required? - */ - - nname= strlen(res->h_name)+1; - - naliases = naddr_list = naliasesdata = 0; - - for ( p = res->h_aliases; *p != nullptr; p++) { - naliases++; - naliasesdata += strlen(*p)+1; - } - - for ( p = res->h_addr_list; *p != nullptr; p++) - naddr_list++; - - if ( nname - + (naliases+1)*sizeof(char*) + naliasesdata - + (naddr_list+1)*sizeof(char*) + naddr_list*res->h_length - <= buflen ) - { - memcpy(result, res, sizeof(struct hostent)); - - strcpy(buffer, res->h_name); - result->h_name = buffer; - buffer += nname; - - parray = reinterpret_cast<char**>(buffer); - result->h_aliases = parray; - data = buffer + (naliases+1)*sizeof(char*); - for ( p = res->h_aliases; *p != nullptr; p++) { - int n = strlen(*p)+1; - *parray++ = data; - memcpy(data, *p, n); - data += n; - } - *parray = nullptr; - buffer = data; - parray = reinterpret_cast<char**>(buffer); - result->h_addr_list = parray; - data = buffer + (naddr_list+1)*sizeof(char*); - for ( p = res->h_addr_list; *p != nullptr; p++) { - *parray++ = data; - memcpy(data, *p, res->h_length); - data += res->h_length; - } - *parray = nullptr; - - res = result; - } - else - { - errno = ERANGE; - res = nullptr; - } - } - else - { - *h_errnop = h_errno; - } - - RTL_MUTEX_UNLOCK - - return res; -} #endif // OSX || IOS || OPENBSD || NETBSD #if defined(MACOSX) diff --git a/sal/osl/unx/system.hxx b/sal/osl/unx/system.hxx index 57c418070a73..bbf9c0449357 100644 --- a/sal/osl/unx/system.hxx +++ b/sal/osl/unx/system.hxx @@ -376,8 +376,6 @@ extern struct spwd *getspnam_r(const char *name, struct spwd *result, struct tm *localtime_r(const time_t *timep, struct tm *buffer); struct tm *gmtime_r(const time_t *timep, struct tm *buffer); #endif -struct hostent *gethostbyname_r(const char *name, struct hostent *result, - char *buffer, size_t buflen, int *h_errnop); #endif /* !defined(FREEBSD) */ #endif |