1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Jeremy Lainé <jeremy.laine@m4x.org>
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 <winsock2.h>
41 #include "qdnslookup_p.h"
42 
43 #include <qurl.h>
44 #include <private/qsystemerror_p.h>
45 
46 #include <qt_windows.h>
47 #include <windns.h>
48 #include <memory.h>
49 
50 QT_BEGIN_NAMESPACE
51 
query(const int requestType,const QByteArray & requestName,const QHostAddress & nameserver,QDnsLookupReply * reply)52 void QDnsLookupRunnable::query(const int requestType, const QByteArray &requestName, const QHostAddress &nameserver, QDnsLookupReply *reply)
53 {
54     // Perform DNS query.
55     PDNS_RECORD dns_records = 0;
56     const QString requestNameUtf16 = QString::fromUtf8(requestName.data(), requestName.size());
57     IP4_ARRAY srvList;
58     memset(&srvList, 0, sizeof(IP4_ARRAY));
59     if (!nameserver.isNull()) {
60         if (nameserver.protocol() == QAbstractSocket::IPv4Protocol) {
61             // The below code is referenced from: http://support.microsoft.com/kb/831226
62             srvList.AddrCount = 1;
63             srvList.AddrArray[0] = htonl(nameserver.toIPv4Address());
64         } else if (nameserver.protocol() == QAbstractSocket::IPv6Protocol) {
65             // For supoprting IPv6 nameserver addresses, we'll need to switch
66             // from DnsQuey() to DnsQueryEx() as it supports passing an IPv6
67             // address in the nameserver list
68             qWarning("%s", QDnsLookupPrivate::msgNoIpV6NameServerAdresses);
69             reply->error = QDnsLookup::ResolverError;
70             reply->errorString = tr(QDnsLookupPrivate::msgNoIpV6NameServerAdresses);
71             return;
72         }
73     }
74     const DNS_STATUS status = DnsQuery_W(reinterpret_cast<const wchar_t*>(requestNameUtf16.utf16()), requestType, DNS_QUERY_STANDARD, &srvList, &dns_records, NULL);
75     switch (status) {
76     case ERROR_SUCCESS:
77         break;
78     case DNS_ERROR_RCODE_FORMAT_ERROR:
79         reply->error = QDnsLookup::InvalidRequestError;
80         reply->errorString = tr("Server could not process query");
81         return;
82     case DNS_ERROR_RCODE_SERVER_FAILURE:
83         reply->error = QDnsLookup::ServerFailureError;
84         reply->errorString = tr("Server failure");
85         return;
86     case DNS_ERROR_RCODE_NAME_ERROR:
87         reply->error = QDnsLookup::NotFoundError;
88         reply->errorString = tr("Non existent domain");
89         return;
90     case DNS_ERROR_RCODE_REFUSED:
91         reply->error = QDnsLookup::ServerRefusedError;
92         reply->errorString = tr("Server refused to answer");
93         return;
94     default:
95         reply->error = QDnsLookup::InvalidReplyError;
96         reply->errorString = QSystemError(status, QSystemError::NativeError).toString();
97         return;
98     }
99 
100     // Extract results.
101     for (PDNS_RECORD ptr = dns_records; ptr != NULL; ptr = ptr->pNext) {
102         const QString name = QUrl::fromAce( QString::fromWCharArray( ptr->pName ).toLatin1() );
103         if (ptr->wType == QDnsLookup::A) {
104             QDnsHostAddressRecord record;
105             record.d->name = name;
106             record.d->timeToLive = ptr->dwTtl;
107             record.d->value = QHostAddress(ntohl(ptr->Data.A.IpAddress));
108             reply->hostAddressRecords.append(record);
109         } else if (ptr->wType == QDnsLookup::AAAA) {
110             Q_IPV6ADDR addr;
111             memcpy(&addr, &ptr->Data.AAAA.Ip6Address, sizeof(Q_IPV6ADDR));
112 
113             QDnsHostAddressRecord record;
114             record.d->name = name;
115             record.d->timeToLive = ptr->dwTtl;
116             record.d->value = QHostAddress(addr);
117             reply->hostAddressRecords.append(record);
118         } else if (ptr->wType == QDnsLookup::CNAME) {
119             QDnsDomainNameRecord record;
120             record.d->name = name;
121             record.d->timeToLive = ptr->dwTtl;
122             record.d->value = QUrl::fromAce(QString::fromWCharArray(ptr->Data.Cname.pNameHost).toLatin1());
123             reply->canonicalNameRecords.append(record);
124         } else if (ptr->wType == QDnsLookup::MX) {
125             QDnsMailExchangeRecord record;
126             record.d->name = name;
127             record.d->exchange = QUrl::fromAce(QString::fromWCharArray(ptr->Data.Mx.pNameExchange).toLatin1());
128             record.d->preference = ptr->Data.Mx.wPreference;
129             record.d->timeToLive = ptr->dwTtl;
130             reply->mailExchangeRecords.append(record);
131         } else if (ptr->wType == QDnsLookup::NS) {
132             QDnsDomainNameRecord record;
133             record.d->name = name;
134             record.d->timeToLive = ptr->dwTtl;
135             record.d->value = QUrl::fromAce(QString::fromWCharArray(ptr->Data.Ns.pNameHost).toLatin1());
136             reply->nameServerRecords.append(record);
137         } else if (ptr->wType == QDnsLookup::PTR) {
138             QDnsDomainNameRecord record;
139             record.d->name = name;
140             record.d->timeToLive = ptr->dwTtl;
141             record.d->value = QUrl::fromAce(QString::fromWCharArray(ptr->Data.Ptr.pNameHost).toLatin1());
142             reply->pointerRecords.append(record);
143         } else if (ptr->wType == QDnsLookup::SRV) {
144             QDnsServiceRecord record;
145             record.d->name = name;
146             record.d->target = QUrl::fromAce(QString::fromWCharArray(ptr->Data.Srv.pNameTarget).toLatin1());
147             record.d->port = ptr->Data.Srv.wPort;
148             record.d->priority = ptr->Data.Srv.wPriority;
149             record.d->timeToLive = ptr->dwTtl;
150             record.d->weight = ptr->Data.Srv.wWeight;
151             reply->serviceRecords.append(record);
152         } else if (ptr->wType == QDnsLookup::TXT) {
153             QDnsTextRecord record;
154             record.d->name = name;
155             record.d->timeToLive = ptr->dwTtl;
156             for (unsigned int i = 0; i < ptr->Data.Txt.dwStringCount; ++i) {
157                 record.d->values << QString::fromWCharArray((ptr->Data.Txt.pStringArray[i])).toLatin1();;
158             }
159             reply->textRecords.append(record);
160         }
161     }
162 
163     DnsRecordListFree(dns_records, DnsFreeRecordList);
164 }
165 
166 QT_END_NAMESPACE
167