[Libreoffice-commits] core.git: sal/osl

Arkadiy Illarionov qarkai at gmail.com
Sun Oct 29 14:51:35 UTC 2017


 sal/osl/unx/socket.cxx |  110 +++++++++++++++++++++++++++++--------------------
 1 file changed, 67 insertions(+), 43 deletions(-)

New commits:
commit a6750166cdd9b39f4e948201143c70ea95def600
Author: Arkadiy Illarionov <qarkai at gmail.com>
Date:   Tue Sep 26 00:18:27 2017 +0300

    tdf#94695 Replace gethostbyaddr with getnameinfo
    
    Change-Id: I7ac99a6f470998364e9e43b749a0914d8a3fc096
    Reviewed-on: https://gerrit.libreoffice.org/42769
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Thorsten Behrens <Thorsten.Behrens at CIB.de>
    Reviewed-by: Stephan Bergmann <sbergman at redhat.com>

diff --git a/sal/osl/unx/socket.cxx b/sal/osl/unx/socket.cxx
index f7a98efafc82..c7261f1eec76 100644
--- a/sal/osl/unx/socket.cxx
+++ b/sal/osl/unx/socket.cxx
@@ -576,25 +576,6 @@ static struct hostent* osl_gethostbyname_r (
 #endif
 }
 
-static sal_Char* getFullQualifiedDomainName (const sal_Char *pHostName)
-{
-    struct hostent  aHostByName;
-    struct hostent *pHostByName;
-    sal_Char        pQualifiedHostBuffer[ MAX_HOSTBUFFER_SIZE ];
-    sal_Char  *pFullQualifiedName = nullptr;
-    int     nErrorNo;
-
-    pHostByName = osl_gethostbyname_r (
-        pHostName,
-        &aHostByName, pQualifiedHostBuffer,
-        sizeof(pQualifiedHostBuffer), &nErrorNo );
-    if (pHostByName != nullptr)
-    {
-        pFullQualifiedName = strdup(pHostByName->h_name);
-    }
-    return pFullQualifiedName;
-}
-
 static bool isFullQualifiedDomainName (const sal_Char *pHostName)
 {
     /* a FQDN (aka 'hostname.domain.top_level_domain' )
@@ -604,6 +585,34 @@ static bool isFullQualifiedDomainName (const sal_Char *pHostName)
     return strchr( pHostName, (int)'.' ) != nullptr;
 }
 
+static sal_Char* getFullQualifiedDomainName (const sal_Char *pHostName)
+{
+    sal_Char  *pFullQualifiedName = nullptr;
+
+    if (isFullQualifiedDomainName(pHostName))
+    {
+        pFullQualifiedName = strdup(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);
+        }
+    }
+
+    return pFullQualifiedName;
+}
+
 struct oslHostAddrImpl
 {
     sal_Char        *pHostName;
@@ -620,20 +629,10 @@ static oslHostAddr hostentToHostAddr (const struct hostent *he)
     if ((he == nullptr) || (he->h_name == nullptr) || (he->h_addr_list[0] == nullptr))
         return nullptr;
 
-    if (isFullQualifiedDomainName(he->h_name))
-    {
-        cn= strdup(he->h_name);
-        SAL_WARN_IF( !cn, "sal.osl", "insufficient memory" );
-        if (cn == nullptr)
-            return nullptr;
-    }
-    else
-    {
-        cn = getFullQualifiedDomainName (he->h_name);
-        SAL_WARN_IF( !cn, "sal.osl", "couldn't get full qualified domain name" );
-        if (cn == nullptr)
-            return nullptr;
-    }
+    cn = getFullQualifiedDomainName (he->h_name);
+    SAL_WARN_IF( !cn, "sal.osl", "couldn't get full qualified domain name" );
+    if (cn == nullptr)
+        return nullptr;
 
     pSockAddr = createSocketAddr();
     SAL_WARN_IF( !pSockAddr, "sal.osl", "insufficient memory" );
@@ -788,19 +787,44 @@ oslHostAddr SAL_CALL osl_createHostAddrByAddr (const oslSocketAddr pAddr)
 
     if (pAddr->m_sockaddr.sa_family == FAMILY_TO_NATIVE(osl_Socket_FamilyInet))
     {
-        const struct sockaddr_in *sin= reinterpret_cast<sockaddr_in *>(&pAddr->m_sockaddr);
-        struct hostent *he;
-
+        const struct sockaddr_in *sin = reinterpret_cast<sockaddr_in *>(&pAddr->m_sockaddr);
         if (sin->sin_addr.s_addr == htonl(INADDR_ANY))
             return nullptr;
 
-        char const * addr = reinterpret_cast<char const *>(&sin->sin_addr);
-            // at least some Androids apparently have a gethostbyaddr with char*
-            // instead of void* argument
-        he= gethostbyaddr(addr,
-                          sizeof (sin->sin_addr),
-                          sin->sin_family);
-        return hostentToHostAddr (he);
+        char host[MAX_HOSTBUFFER_SIZE];
+        int res = getnameinfo(&pAddr->m_sockaddr, sizeof(struct sockaddr_in),
+                              host, sizeof(host), nullptr, 0, NI_NAMEREQD);
+        if (res != 0)
+            return nullptr;
+
+        sal_Char *cn = getFullQualifiedDomainName(host);
+        SAL_WARN_IF( !cn, "sal.osl", "couldn't get full qualified domain name" );
+        if (cn == nullptr)
+            return nullptr;
+
+        oslSocketAddr pSockAddr = createSocketAddr();
+        SAL_WARN_IF( !pSockAddr, "sal.osl", "insufficient memory" );
+        if (pSockAddr == nullptr)
+        {
+            free(cn);
+            return nullptr;
+        }
+
+        memcpy(&pSockAddr->m_sockaddr, &pAddr->m_sockaddr, sizeof(pAddr->m_sockaddr));
+
+        oslHostAddr pHostAddr = static_cast<oslHostAddr>(malloc(sizeof(struct oslHostAddrImpl)));
+        SAL_WARN_IF( !pAddr, "sal.osl", "allocation error" );
+        if (pHostAddr == nullptr)
+        {
+            destroySocketAddr(pSockAddr);
+            free(cn);
+            return nullptr;
+        }
+
+        pHostAddr->pHostName = cn;
+        pHostAddr->pSockAddr = pSockAddr;
+
+        return pHostAddr;
     }
 
     return nullptr;


More information about the Libreoffice-commits mailing list