1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qdnslookup_p.h"
41
42 #include <qfunctions_winrt.h>
43 #include <qurl.h>
44 #include <qdebug.h>
45
46 #include <wrl.h>
47 #include <windows.foundation.h>
48 #include <windows.foundation.collections.h>
49 #include <windows.networking.h>
50 #include <windows.networking.sockets.h>
51
52 using namespace Microsoft::WRL;
53 using namespace Microsoft::WRL::Wrappers;
54 using namespace ABI::Windows::Foundation;
55 using namespace ABI::Windows::Foundation::Collections;
56 using namespace ABI::Windows::Networking;
57 using namespace ABI::Windows::Networking::Connectivity;
58 using namespace ABI::Windows::Networking::Sockets;
59
60 #define E_NO_SUCH_HOST 0x80072af9
61
62 QT_BEGIN_NAMESPACE
63
query(const int requestType,const QByteArray & requestName,const QHostAddress & nameserver,QDnsLookupReply * reply)64 void QDnsLookupRunnable::query(const int requestType, const QByteArray &requestName, const QHostAddress &nameserver, QDnsLookupReply *reply)
65 {
66 // TODO: Add nameserver support for winRT
67 if (!nameserver.isNull())
68 qWarning("Ignoring nameserver as its currently not supported on WinRT");
69
70 // TODO: is there any way to do "proper" dns lookup?
71 if (requestType != QDnsLookup::A && requestType != QDnsLookup::AAAA
72 && requestType != QDnsLookup::ANY) {
73 reply->error = QDnsLookup::InvalidRequestError;
74 reply->errorString = QLatin1String("WinRT only supports IPv4 and IPv6 requests");
75 return;
76 }
77
78 QString aceHostname = QUrl::fromAce(requestName);
79 if (aceHostname.isEmpty()) {
80 reply->error = QDnsLookup::InvalidRequestError;
81 reply->errorString = requestName.isEmpty() ? tr("No hostname given") : tr("Invalid hostname");
82 return;
83 }
84
85 ComPtr<IHostNameFactory> hostnameFactory;
86 HRESULT hr = RoGetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Networking_HostName).Get(),
87 IID_PPV_ARGS(&hostnameFactory));
88 if (FAILED(hr)) {
89 reply->error = QDnsLookup::ResolverError;
90 reply->errorString = QLatin1String("Could not obtain hostname factory");
91 return;
92 }
93 ComPtr<IHostName> host;
94 HStringReference hostNameRef((const wchar_t*)aceHostname.utf16());
95 hr = hostnameFactory->CreateHostName(hostNameRef.Get(), &host);
96 Q_ASSERT_SUCCEEDED(hr);
97
98 ComPtr<IDatagramSocketStatics> datagramSocketStatics;
99 hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Networking_Sockets_DatagramSocket).Get(), &datagramSocketStatics);
100 Q_ASSERT_SUCCEEDED(hr);
101
102 ComPtr<IAsyncOperation<IVectorView<EndpointPair *> *>> op;
103 hr = datagramSocketStatics->GetEndpointPairsAsync(host.Get(),
104 HString::MakeReference(L"0").Get(),
105 &op);
106 Q_ASSERT_SUCCEEDED(hr);
107
108 ComPtr<IVectorView<EndpointPair *>> endpointPairs;
109 hr = QWinRTFunctions::await(op, endpointPairs.GetAddressOf(), QWinRTFunctions::YieldThread, 60 * 1000);
110 if (hr == E_NO_SUCH_HOST || !endpointPairs) {
111 reply->error = QDnsLookup::NotFoundError;
112 reply->errorString = tr("Host %1 could not be found.").arg(aceHostname);
113 return;
114 }
115 if (FAILED(hr)) {
116 reply->error = QDnsLookup::ServerFailureError;
117 reply->errorString = tr("Unknown error");
118 return;
119 }
120
121 unsigned int size;
122 hr = endpointPairs->get_Size(&size);
123 Q_ASSERT_SUCCEEDED(hr);
124 // endpoint pairs might contain duplicates so we temporarily store addresses in a QSet
125 QSet<QHostAddress> addresses;
126 for (unsigned int i = 0; i < size; ++i) {
127 ComPtr<IEndpointPair> endpointpair;
128 hr = endpointPairs->GetAt(i, &endpointpair);
129 Q_ASSERT_SUCCEEDED(hr);
130 ComPtr<IHostName> remoteHost;
131 hr = endpointpair->get_RemoteHostName(&remoteHost);
132 Q_ASSERT_SUCCEEDED(hr);
133 HostNameType type;
134 hr = remoteHost->get_Type(&type);
135 Q_ASSERT_SUCCEEDED(hr);
136 if (type == HostNameType_Bluetooth || type == HostNameType_DomainName
137 || (requestType != QDnsLookup::ANY
138 && ((type == HostNameType_Ipv4 && requestType == QDnsLookup::AAAA)
139 || (type == HostNameType_Ipv6 && requestType == QDnsLookup::A))))
140 continue;
141
142 HString name;
143 hr = remoteHost->get_CanonicalName(name.GetAddressOf());
144 Q_ASSERT_SUCCEEDED(hr);
145 UINT32 length;
146 PCWSTR rawString = name.GetRawBuffer(&length);
147 addresses.insert(QHostAddress(QString::fromWCharArray(rawString, length)));
148 }
149 for (const QHostAddress &address : qAsConst(addresses)) {
150 QDnsHostAddressRecord record;
151 record.d->name = aceHostname;
152 record.d->value = address;
153 reply->hostAddressRecords.append(record);
154 }
155 }
156
157 QT_END_NAMESPACE
158