summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2019-02-05 10:32:03 +0100
committerIlhan Yesil <ilhanyesil@gmx.de>2019-09-19 15:12:30 +0200
commite090497a500d63edf0f26d4a506d27294b82e3a1 (patch)
tree5aa94047bc5e323b8ce61a052932343f2fc0a039 /sal
parent42af34720f5fb5be34e0b5b757e7b7a69595f91c (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> (cherry picked from commit 3be2a53ee1a34fd4bc96e7817191fc3e0eb3c917) Reviewed-on: https://gerrit.libreoffice.org/79013 Reviewed-by: Ilhan Yesil <ilhanyesil@gmx.de> Tested-by: Ilhan Yesil <ilhanyesil@gmx.de>
Diffstat (limited to 'sal')
-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 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);
+ }
+ }
}
}