summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2024-12-20 11:07:40 +0200
committerXisco Fauli <xiscofauli@libreoffice.org>2024-12-20 18:33:44 +0100
commiteb83239ef26a8ddf77d82d1f6805c58ee366c172 (patch)
tree9036e23a8c2faeed9334bd3741159af9e6f3a168
parent46290af4bfe820944e27f86dbb6608a12ad70202 (diff)
correct caching in osl_getLocalHostname*
the change commit eac00017e34e77343b9ac3638bed9c75115a23fe Author: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de> Date: Thu Dec 10 14:24:05 2020 +0100 [API CHANGE] Do not call getaddrinfo if we just want the hostname means that the result returned from osl_getLocalHostname and osl_getLocalHostnameFQDN is always the same, but depends on which one is called first. Rather cache them independently, so they always return the right thing Change-Id: Ifa2f731a6002550cf08f57b53fd6e25e8b79295f Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178872 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk> (cherry picked from commit 46533f9c308bb57545fd25527f3141daec0080d7) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178930 Reviewed-by: Xisco Fauli <xiscofauli@libreoffice.org>
-rw-r--r--sal/osl/unx/socket.cxx79
1 files changed, 40 insertions, 39 deletions
diff --git a/sal/osl/unx/socket.cxx b/sal/osl/unx/socket.cxx
index e875e415e709..2b3a877ec977 100644
--- a/sal/osl/unx/socket.cxx
+++ b/sal/osl/unx/socket.cxx
@@ -862,66 +862,67 @@ void SAL_CALL osl_destroyHostAddr (oslHostAddr pAddr)
namespace
{
-oslSocketResult lcl_getLocalHostname(rtl_uString **ustrLocalHostname, bool bUseFQDN)
+std::pair<oslSocketResult, OUString> lcl_getLocalHostname(bool bUseFQDN)
{
- static auto const init = [bUseFQDN]() -> std::pair<oslSocketResult, OUString> {
- char LocalHostname[256] = "";
+ char LocalHostname[256] = "";
#ifdef SYSV
- struct utsname uts;
+ struct utsname uts;
- if (uname(&uts) < 0)
- return {osl_Socket_Error, OUString()};
+ if (uname(&uts) < 0)
+ return {osl_Socket_Error, OUString()};
- if ((strlen(uts.nodename) + 1) > nBufLen)
- return {osl_Socket_Error, OUString()};
+ if ((strlen(uts.nodename) + 1) > nBufLen)
+ return {osl_Socket_Error, OUString()};
- strncpy(LocalHostname, uts.nodename, sizeof( LocalHostname ));
+ strncpy(LocalHostname, uts.nodename, sizeof( LocalHostname ));
#else /* BSD compatible */
- if (gethostname(LocalHostname, sizeof(LocalHostname)-1) != 0)
- return {osl_Socket_Error, OUString()};
+ if (gethostname(LocalHostname, sizeof(LocalHostname)-1) != 0)
+ return {osl_Socket_Error, OUString()};
#endif /* SYSV */
- LocalHostname[sizeof(LocalHostname)-1] = 0;
-
- /* check if we have an FQDN */
- if (bUseFQDN && strchr(LocalHostname, '.') == nullptr)
- {
- oslHostAddr Addr;
-
- /* no, determine it via dns */
- Addr = osl_psz_createHostAddrByName(LocalHostname);
+ LocalHostname[sizeof(LocalHostname)-1] = 0;
- const char *pStr;
- if ((pStr = osl_psz_getHostnameOfHostAddr(Addr)) != nullptr)
- {
- strncpy(LocalHostname, pStr, sizeof( LocalHostname ));
- LocalHostname[sizeof(LocalHostname)-1] = 0;
- }
- osl_destroyHostAddr(Addr);
- }
+ /* check if we have an FQDN */
+ if (bUseFQDN && strchr(LocalHostname, '.') == nullptr)
+ {
+ oslHostAddr Addr;
- if (LocalHostname[0] != '\0')
- {
- return {osl_Socket_Ok, OUString::createFromAscii(LocalHostname)};
- }
+ /* no, determine it via dns */
+ Addr = osl_psz_createHostAddrByName(LocalHostname);
- return {osl_Socket_Error, OUString()};
- }();
+ const char *pStr;
+ if ((pStr = osl_psz_getHostnameOfHostAddr(Addr)) != nullptr)
+ {
+ strncpy(LocalHostname, pStr, sizeof( LocalHostname ));
+ LocalHostname[sizeof(LocalHostname)-1] = 0;
+ }
+ osl_destroyHostAddr(Addr);
+ }
- rtl_uString_assign(ustrLocalHostname,init.second.pData);
+ if (LocalHostname[0] != '\0')
+ {
+ return {osl_Socket_Ok, OUString::createFromAscii(LocalHostname)};
+ }
- return init.first;
-}
+ return {osl_Socket_Error, OUString()};
}
+} // anonymous namespace
+
oslSocketResult SAL_CALL osl_getLocalHostname(rtl_uString **ustrLocalHostname)
{
- return lcl_getLocalHostname(ustrLocalHostname, false);
+ static auto const init = lcl_getLocalHostname(/*bUseFQDN*/false);
+
+ rtl_uString_assign(ustrLocalHostname,init.second.pData);
+ return init.first;
}
oslSocketResult osl_getLocalHostnameFQDN(rtl_uString **ustrLocalHostname)
{
- return lcl_getLocalHostname(ustrLocalHostname, true);
+ static auto const init = lcl_getLocalHostname(/*bUseFQDN*/true);
+
+ rtl_uString_assign(ustrLocalHostname,init.second.pData);
+ return init.first;
}
oslSocketAddr SAL_CALL osl_resolveHostname(rtl_uString *ustrHostname)