sal/osl/w32/socket.cxx |   17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

New commits:
commit 12f8f2c87ffc8ec6e3e18a6125abe1716733e054
Author:     Stephan Bergmann <sberg...@redhat.com>
AuthorDate: Tue Feb 5 10:32:03 2019 +0100
Commit:     Thorsten Behrens <thorsten.behr...@cib.de>
CommitDate: Mon Dec 14 01:27:36 2020 +0100

    Improve osl_getLocalHostname on Windows
    
    ...returning a non-dotted result obtained from gethostname in case trying 
via
    osl_createHostAddrByName doesn't work either.  The code had been like this 
ever
    since its introduction with 74f3ed51f1e3fa7a199210fd6ffc69d78a535c08 "made
    socket and pipe refcounted, added blocking read write methods, added direct
    access methods for struct sockaddr", but there appears to be no good reason 
not
    to return the non-dotted gethostname value as a fallback (and it may just 
have
    been an oversight not to do so; and on e.g. Linux we simply return whatever 
a
    successful call to gethostname provides, be it dotted or not, ever since at
    least ebd00f5c46707e0dfcdd074f581ae86c0576e69b "socket.cxx -> socket.c", 
too).
    
    (Calls to osl_getLocalHostname are few across the code base, so this change 
in
    behavior is unlikely to cause any regressions.  I came across this wile 
looking
    into <https://bugs.documentfoundation.org/show_bug.cgi?id=107461> "Does not
    support 'file://' scheme with actual hostname", but this change is 
unrelated to
    any potential fix for that issue.  It just felt right to get this fixed in
    passing.)
    
    Change-Id: I78e59140579b9d37ee435a8f121e58544d2235eb
    Reviewed-on: https://gerrit.libreoffice.org/67390
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sberg...@redhat.com>
    (cherry picked from commit 3be2a53ee1a34fd4bc96e7817191fc3e0eb3c917)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107495
    Tested-by: Thorsten Behrens <thorsten.behr...@cib.de>
    Reviewed-by: Thorsten Behrens <thorsten.behr...@cib.de>

diff --git a/sal/osl/w32/socket.cxx b/sal/osl/w32/socket.cxx
index bf27bba1a04d..cce9af46f142 100644
--- a/sal/osl/w32/socket.cxx
+++ b/sal/osl/w32/socket.cxx
@@ -20,6 +20,7 @@
 #include "system.h"
 
 #include <osl/socket.h>
+#include <osl/thread.h>
 #include <osl/diagnose.h>
 #include <rtl/alloc.h>
 #include <sal/log.hxx>
@@ -607,7 +608,7 @@ oslSocketResult SAL_CALL osl_getLocalHostname (rtl_uString 
**strLocalHostname)
         sal_Char Host[256]= "";
         if (gethostname(Host, sizeof(Host)) == 0)
         {
-            /* check if we have an FQDN */
+            /* check if we have an FQDN; if not, try to determine it via dns 
first: */
             if (strchr(Host, '.') == nullptr)
             {
                 oslHostAddr pAddr;
@@ -618,7 +619,6 @@ oslSocketResult SAL_CALL osl_getLocalHostname (rtl_uString 
**strLocalHostname)
                     RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
                 OSL_ASSERT(hostName != nullptr);
 
-                /* no, determine it via dns */
                 pAddr = osl_createHostAddrByName(hostName);
                 rtl_uString_release (hostName);
 
@@ -629,6 +629,19 @@ oslSocketResult SAL_CALL osl_getLocalHostname (rtl_uString 
**strLocalHostname)
 
                 osl_destroyHostAddr (pAddr);
             }
+            if (LocalHostname[0] == u'\0')
+            {
+                OUString u;
+                if (rtl_convertStringToUString(
+                        &u.pData, Host, strlen(Host), 
osl_getThreadTextEncoding(),
+                        (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
+                         | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
+                         | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))
+                    && u.getLength() < SAL_N_ELEMENTS(LocalHostname))
+                {
+                    memcpy(LocalHostname, u.getStr(), (u.getLength() + 1) * 
sizeof sal_Unicode);
+                }
+            }
         }
     }
 
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to