summaryrefslogtreecommitdiff
path: root/sal/osl
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2019-02-05 10:32:03 +0100
committerStephan Bergmann <sbergman@redhat.com>2019-02-05 12:24:09 +0100
commit3be2a53ee1a34fd4bc96e7817191fc3e0eb3c917 (patch)
treea378eeb9478a184aad234c45aac10749c851c27d /sal/osl
parentaa974a1b3798e04424623ad331e9f5a0ae01a34b (diff)
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 <sbergman@redhat.com>
Diffstat (limited to 'sal/osl')
-rw-r--r--sal/osl/w32/socket.cxx17
1 files changed, 15 insertions, 2 deletions
diff --git a/sal/osl/w32/socket.cxx b/sal/osl/w32/socket.cxx
index 592103e630d6..013bf895866e 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 <rtl/byteseq.h>
@@ -602,7 +603,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;
@@ -613,7 +614,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);
@@ -624,6 +624,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);
+ }
+ }
}
}