From e9fa435652ef064515bd5c04c0b5e5c4a30ebca4 Mon Sep 17 00:00:00 2001 From: Maurice Kalinowski Date: Fri, 4 Nov 2016 13:46:36 +0100 Subject: winrt: Add support for Windows Information Protection Windows Information Protection is used for splitting corporate and personal data, requiring connections to be established in a different way instantiating ThreadNetworkContext. Usage is enabled via the QT_WINRT_USE_THREAD_NETWORK_CONTEXT environment variable. Change-Id: I3aaa097b66fc616d42cd05a1e20bbcb004f6e467 Reviewed-by: James Tong Reviewed-by: Oliver Wolff --- src/network/socket/qabstractsocket.cpp | 13 +++-- src/network/socket/qnativesocketengine_winrt.cpp | 68 +++++++++++++++++++++++- 2 files changed, 76 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp index 02bba2d293..57c40194a3 100644 --- a/src/network/socket/qabstractsocket.cpp +++ b/src/network/socket/qabstractsocket.cpp @@ -1113,10 +1113,15 @@ void QAbstractSocketPrivate::_q_connectToNextAddress() // Tries to connect to the address. If it succeeds immediately // (localhost address on BSD or any UDP connect), emit // connected() and return. - if (socketEngine->connectToHost(host, port)) { - //_q_testConnection(); - fetchConnectionParameters(); - return; + if ( +#if defined(Q_OS_WINRT) && _MSC_VER >= 1900 + !qEnvironmentVariableIsEmpty("QT_WINRT_USE_THREAD_NETWORK_CONTEXT") ? + socketEngine->connectToHostByName(hostName, port) : +#endif + socketEngine->connectToHost(host, port)) { + //_q_testConnection(); + fetchConnectionParameters(); + return; } // Check that we're in delayed connection state. If not, try diff --git a/src/network/socket/qnativesocketengine_winrt.cpp b/src/network/socket/qnativesocketengine_winrt.cpp index 3daca38959..d0b8bff7f9 100644 --- a/src/network/socket/qnativesocketengine_winrt.cpp +++ b/src/network/socket/qnativesocketengine_winrt.cpp @@ -75,6 +75,9 @@ using namespace ABI::Windows::Storage::Streams; using namespace ABI::Windows::Networking; using namespace ABI::Windows::Networking::Connectivity; using namespace ABI::Windows::Networking::Sockets; +#if _MSC_VER >= 1900 +using namespace ABI::Windows::Security::EnterpriseData; +#endif typedef ITypedEventHandler ClientConnectedHandler; typedef ITypedEventHandler DatagramReceivedHandler; @@ -84,6 +87,45 @@ typedef IAsyncOperationWithProgress IAsyncBufferOperation; QT_BEGIN_NAMESPACE +#if _MSC_VER >= 1900 +static HRESULT qt_winrt_try_create_thread_network_context(QString host, ComPtr &context) +{ + HRESULT hr; + ComPtr protectionPolicyManager; + + hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Security_EnterpriseData_ProtectionPolicyManager).Get(), + &protectionPolicyManager); + RETURN_HR_IF_FAILED("Could not access ProtectionPolicyManager statics."); + + ComPtr hostNameFactory; + hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Networking_HostName).Get(), + &hostNameFactory); + RETURN_HR_IF_FAILED("Could not access HostName factory."); + + ComPtr hostName; + HStringReference hostRef(reinterpret_cast(host.utf16()), host.length()); + hr = hostNameFactory->CreateHostName(hostRef.Get(), &hostName); + RETURN_HR_IF_FAILED("Could not create hostname."); + + ComPtr> op; + hr = protectionPolicyManager->GetPrimaryManagedIdentityForNetworkEndpointAsync(hostName.Get(), &op); + RETURN_HR_IF_FAILED("Could not get identity operation."); + + HSTRING hIdentity; + hr = QWinRTFunctions::await(op, &hIdentity); + RETURN_HR_IF_FAILED("Could not wait for identity operation."); + + // Implies there is no need for a network context for this address + if (hIdentity == nullptr) + return S_OK; + + hr = protectionPolicyManager->CreateCurrentThreadNetworkContext(hIdentity, &context); + RETURN_HR_IF_FAILED("Could not create thread network context"); + + return S_OK; +} +#endif // _MSC_VER >= 1900 + static inline QString qt_QStringFromHString(const HString &string) { UINT32 length; @@ -367,9 +409,23 @@ bool QNativeSocketEngine::connectToHost(const QHostAddress &address, quint16 por bool QNativeSocketEngine::connectToHostByName(const QString &name, quint16 port) { Q_D(QNativeSocketEngine); + HRESULT hr; + +#if _MSC_VER >= 1900 + ComPtr networkContext; + if (!qEnvironmentVariableIsEmpty("QT_WINRT_USE_THREAD_NETWORK_CONTEXT")) { + hr = qt_winrt_try_create_thread_network_context(name, networkContext); + if (FAILED(hr)) { + setError(QAbstractSocket::ConnectionRefusedError, QLatin1String("Could not create thread network context.")); + d->socketState = QAbstractSocket::ConnectedState; + return true; + } + } +#endif // _MSC_VER >= 1900 + HStringReference hostNameRef(reinterpret_cast(name.utf16())); ComPtr hostNameFactory; - HRESULT hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Networking_HostName).Get(), + hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Networking_HostName).Get(), &hostNameFactory); Q_ASSERT_SUCCEEDED(hr); ComPtr remoteHost; @@ -390,6 +446,16 @@ bool QNativeSocketEngine::connectToHostByName(const QString &name, quint16 port) } Q_ASSERT_SUCCEEDED(hr); +#if _MSC_VER >= 1900 + if (networkContext != nullptr) { + ComPtr networkContextCloser; + hr = networkContext.As(&networkContextCloser); + Q_ASSERT_SUCCEEDED(hr); + hr = networkContextCloser->Close(); + Q_ASSERT_SUCCEEDED(hr); + } +#endif // _MSC_VER >= 1900 + d->socketState = QAbstractSocket::ConnectingState; QEventDispatcherWinRT::runOnXamlThread([d, &hr]() { hr = d->connectOp->put_Completed(Callback( -- cgit v1.2.3