summaryrefslogtreecommitdiff
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:45 +0200
commit1f77a868953c5ad21f08192455a1ca948501bbbb (patch)
tree483fc3e2b2aafd2831bc7f806220b49f0ce5f327
parentf51fbaf45c6239bbf210e25dd61563f48d63fa93 (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.) Reviewed-on: https://gerrit.libreoffice.org/67390 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com> (cherry picked from commit 3be2a53ee1a34fd4bc96e7817191fc3e0eb3c917) Change-Id: I78e59140579b9d37ee435a8f121e58544d2235eb Reviewed-on: https://gerrit.libreoffice.org/79012 Reviewed-by: Ilhan Yesil <ilhanyesil@gmx.de> Tested-by: Ilhan Yesil <ilhanyesil@gmx.de>
-rw-r--r--sal/osl/w32/socket.cxx19
1 files changed, 16 insertions, 3 deletions
diff --git a/sal/osl/w32/socket.cxx b/sal/osl/w32/socket.cxx
index ea6ea0df3bc0..685301c3d38f 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>
@@ -979,8 +980,8 @@ oslSocketResult SAL_CALL osl_getLocalHostname (rtl_uString **strLocalHostname)
sal_Char Host[256]= "";
if (gethostname(Host, sizeof(Host)) == 0)
{
- /* check if we have an FQDN */
- if (strchr(Host, '.') == NULL)
+ /* check if we have an FQDN; if not, try to determine it via dns first: */
+ if (strchr(Host, '.') == nullptr)
{
oslHostAddr pAddr;
rtl_uString *hostName= NULL;
@@ -990,7 +991,6 @@ oslSocketResult SAL_CALL osl_getLocalHostname (rtl_uString **strLocalHostname)
RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
OSL_ASSERT(hostName != 0);
- /* no, determine it via dns */
pAddr = osl_createHostAddrByName(hostName);
rtl_uString_release (hostName);
@@ -1001,6 +1001,19 @@ oslSocketResult SAL_CALL osl_getLocalHostname (rtl_uString **strLocalHostname)
osl_destroyHostAddr ((oslHostAddr)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);
+ }
+ }
}
}