From 01d0b1d6e3fe76d1cd09eeb6474c69e712f37b16 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 8 Apr 2015 14:14:16 -0700 Subject: Merge the multiple implementations of getting the local hostname This commit moves the functionality from QtNetwork's QHostInfo to QtCore. Note that due to Windows ws2_32.dll's quirky behavior of requiring WSAStartup before calling gethostname, this change required moving the initialization to QtCore too. On Linux systems, gethostname() gets the name from uname(), so we bypass the middle man and save one memcpy. Change-Id: I27eaacb532114dd188c4ffff13d32655a6301346 Reviewed-by: Erik Verbruggen Reviewed-by: Oliver Wolff Reviewed-by: Oswald Buddenhagen Reviewed-by: Thiago Macieira --- src/network/kernel/qhostinfo.cpp | 16 +++++++++-- src/network/kernel/qhostinfo_unix.cpp | 9 ------ src/network/kernel/qhostinfo_win.cpp | 13 +-------- src/network/kernel/qhostinfo_winrt.cpp | 39 -------------------------- src/network/socket/qnativesocketengine.cpp | 7 ++--- src/network/socket/qnativesocketengine_p.h | 14 --------- src/network/socket/qnativesocketengine_win.cpp | 19 ------------- 7 files changed, 18 insertions(+), 99 deletions(-) (limited to 'src/network') diff --git a/src/network/kernel/qhostinfo.cpp b/src/network/kernel/qhostinfo.cpp index a2ac9065fd..c6c09542e7 100644 --- a/src/network/kernel/qhostinfo.cpp +++ b/src/network/kernel/qhostinfo.cpp @@ -415,10 +415,22 @@ void QHostInfo::setErrorString(const QString &str) /*! \fn QString QHostInfo::localHostName() - Returns the host name of this machine. + Returns this machine's host name, if one is configured. Note that hostnames + are not guaranteed to be globally unique, especially if they were + configured automatically. - \sa hostName() + This function does not guarantee the returned host name is a Fully + Qualified Domain Name (FQDN). For that, use fromName() to resolve the + returned name to an FQDN. + + This function returns the same as QSysInfo::machineHostName(). + + \sa hostName(), localDomainName() */ +QString QHostInfo::localHostName() +{ + return QSysInfo::machineHostName(); +} /*! \fn QString QHostInfo::localDomainName() diff --git a/src/network/kernel/qhostinfo_unix.cpp b/src/network/kernel/qhostinfo_unix.cpp index 90a6f763f7..266a67771c 100644 --- a/src/network/kernel/qhostinfo_unix.cpp +++ b/src/network/kernel/qhostinfo_unix.cpp @@ -315,15 +315,6 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName) return results; } -QString QHostInfo::localHostName() -{ - char hostName[512]; - if (gethostname(hostName, sizeof(hostName)) == -1) - return QString(); - hostName[sizeof(hostName) - 1] = '\0'; - return QString::fromLocal8Bit(hostName); -} - QString QHostInfo::localDomainName() { #if !defined(Q_OS_VXWORKS) && !defined(Q_OS_ANDROID) diff --git a/src/network/kernel/qhostinfo_win.cpp b/src/network/kernel/qhostinfo_win.cpp index e044728198..fc65ce9fa2 100644 --- a/src/network/kernel/qhostinfo_win.cpp +++ b/src/network/kernel/qhostinfo_win.cpp @@ -111,7 +111,7 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName) QMutexLocker locker(&qPrivCEMutex); #endif - QWindowsSockInit winSock; + QSysInfo::machineHostName(); // this initializes ws2_32.dll // Load res_init on demand. static QBasicAtomicInt triedResolve = Q_BASIC_ATOMIC_INITIALIZER(false); @@ -256,17 +256,6 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName) return results; } -QString QHostInfo::localHostName() -{ - QWindowsSockInit winSock; - - char hostName[512]; - if (gethostname(hostName, sizeof(hostName)) == -1) - return QString(); - hostName[sizeof(hostName) - 1] = '\0'; - return QString::fromLocal8Bit(hostName); -} - // QString QHostInfo::localDomainName() defined in qnetworkinterface_win.cpp QT_END_NAMESPACE diff --git a/src/network/kernel/qhostinfo_winrt.cpp b/src/network/kernel/qhostinfo_winrt.cpp index 1a97fe0e40..3d2344726b 100644 --- a/src/network/kernel/qhostinfo_winrt.cpp +++ b/src/network/kernel/qhostinfo_winrt.cpp @@ -130,45 +130,6 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName) return results; } -QString QHostInfo::localHostName() -{ - ComPtr statics; - GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Networking_Connectivity_NetworkInformation).Get(), &statics); - - ComPtr> hostNames; - statics->GetHostNames(&hostNames); - if (!hostNames) - return QString(); - - unsigned int size; - hostNames->get_Size(&size); - if (size == 0) - return QString(); - - for (unsigned int i = 0; i < size; ++i) { - ComPtr hostName; - hostNames->GetAt(i, &hostName); - HostNameType type; - hostName->get_Type(&type); - if (type != HostNameType_DomainName) - continue; - - HString name; - hostName->get_CanonicalName(name.GetAddressOf()); - UINT32 length; - PCWSTR rawString = name.GetRawBuffer(&length); - return QString::fromWCharArray(rawString, length); - } - ComPtr firstHost; - hostNames->GetAt(0, &firstHost); - - HString name; - firstHost->get_CanonicalName(name.GetAddressOf()); - UINT32 length; - PCWSTR rawString = name.GetRawBuffer(&length); - return QString::fromWCharArray(rawString, length); -} - // QString QHostInfo::localDomainName() defined in qnetworkinterface_win.cpp QT_END_NAMESPACE diff --git a/src/network/socket/qnativesocketengine.cpp b/src/network/socket/qnativesocketengine.cpp index 33bf3af0b5..22151b8e56 100644 --- a/src/network/socket/qnativesocketengine.cpp +++ b/src/network/socket/qnativesocketengine.cpp @@ -152,10 +152,6 @@ QT_BEGIN_NAMESPACE /*! \internal Constructs the private class and initializes all data members. - - On Windows, WSAStartup is called "recursively" for every - concurrent QNativeSocketEngine. This is safe, because WSAStartup and - WSACleanup are reference counted. */ QNativeSocketEnginePrivate::QNativeSocketEnginePrivate() : socketDescriptor(-1), @@ -163,6 +159,9 @@ QNativeSocketEnginePrivate::QNativeSocketEnginePrivate() : writeNotifier(0), exceptNotifier(0) { +#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) + QSysInfo::machineHostName(); // this initializes ws2_32.dll +#endif } /*! \internal diff --git a/src/network/socket/qnativesocketengine_p.h b/src/network/socket/qnativesocketengine_p.h index 24909bf310..653309302c 100644 --- a/src/network/socket/qnativesocketengine_p.h +++ b/src/network/socket/qnativesocketengine_p.h @@ -173,16 +173,6 @@ private: Q_DISABLE_COPY(QNativeSocketEngine) }; -#ifdef Q_OS_WIN -class QWindowsSockInit -{ -public: - QWindowsSockInit(); - ~QWindowsSockInit(); - int version; -}; -#endif - class QSocketNotifier; class QNativeSocketEnginePrivate : public QAbstractSocketEnginePrivate @@ -196,10 +186,6 @@ public: QSocketNotifier *readNotifier, *writeNotifier, *exceptNotifier; -#ifdef Q_OS_WIN - QWindowsSockInit winSock; -#endif - enum ErrorString { NonBlockingInitFailedErrorString, BroadcastingInitFailedErrorString, diff --git a/src/network/socket/qnativesocketengine_win.cpp b/src/network/socket/qnativesocketengine_win.cpp index 7856db0487..8c3f68f04f 100644 --- a/src/network/socket/qnativesocketengine_win.cpp +++ b/src/network/socket/qnativesocketengine_win.cpp @@ -320,25 +320,6 @@ static inline int qt_socket_getMaxMsgSize(qintptr socketDescriptor) return value; } -QWindowsSockInit::QWindowsSockInit() -: version(0) -{ - //### should we try for 2.2 on all platforms ?? - WSAData wsadata; - - // IPv6 requires Winsock v2.0 or better. - if (WSAStartup(MAKEWORD(2,0), &wsadata) != 0) { - qWarning("QTcpSocketAPI: WinSock v2.0 initialization failed."); - } else { - version = 0x20; - } -} - -QWindowsSockInit::~QWindowsSockInit() -{ - WSACleanup(); -} - // MS Transport Provider IOCTL to control // reporting PORT_UNREACHABLE messages // on UDP sockets via recv/WSARecv/etc. -- cgit v1.2.3