2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 Jeremy Lainé <jeremy.laine@m4x.org>
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2012-01-23 17:25:39 +00:00
|
|
|
|
|
|
|
#include "dnslookup.h"
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QDnsLookup>
|
|
|
|
#include <QHostAddress>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QTimer>
|
2014-03-11 08:35:35 +00:00
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include <QCommandLineOption>
|
2012-01-23 17:25:39 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-03-11 08:35:35 +00:00
|
|
|
static int typeFromParameter(const QString &type)
|
|
|
|
{
|
|
|
|
if (type == "a")
|
|
|
|
return QDnsLookup::A;
|
|
|
|
if (type == "aaaa")
|
|
|
|
return QDnsLookup::AAAA;
|
|
|
|
if (type == "any")
|
|
|
|
return QDnsLookup::ANY;
|
|
|
|
if (type == "cname")
|
|
|
|
return QDnsLookup::CNAME;
|
|
|
|
if (type == "mx")
|
|
|
|
return QDnsLookup::MX;
|
|
|
|
if (type == "ns")
|
|
|
|
return QDnsLookup::NS;
|
|
|
|
if (type == "ptr")
|
|
|
|
return QDnsLookup::PTR;
|
|
|
|
if (type == "srv")
|
|
|
|
return QDnsLookup::SRV;
|
|
|
|
if (type == "txt")
|
|
|
|
return QDnsLookup::TXT;
|
|
|
|
return -1;
|
2012-01-23 17:25:39 +00:00
|
|
|
}
|
|
|
|
|
2014-03-11 08:35:35 +00:00
|
|
|
//! [0]
|
|
|
|
|
|
|
|
enum CommandLineParseResult
|
2012-01-23 17:25:39 +00:00
|
|
|
{
|
2014-03-11 08:35:35 +00:00
|
|
|
CommandLineOk,
|
|
|
|
CommandLineError,
|
|
|
|
CommandLineVersionRequested,
|
|
|
|
CommandLineHelpRequested
|
|
|
|
};
|
2012-01-23 17:25:39 +00:00
|
|
|
|
2014-03-11 08:35:35 +00:00
|
|
|
CommandLineParseResult parseCommandLine(QCommandLineParser &parser, DnsQuery *query, QString *errorMessage)
|
2012-01-23 17:25:39 +00:00
|
|
|
{
|
2014-03-11 08:35:35 +00:00
|
|
|
parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
|
|
|
|
const QCommandLineOption nameServerOption("n", "The name server to use.", "nameserver");
|
|
|
|
parser.addOption(nameServerOption);
|
|
|
|
const QCommandLineOption typeOption("t", "The lookup type.", "type");
|
|
|
|
parser.addOption(typeOption);
|
|
|
|
parser.addPositionalArgument("name", "The name to look up.");
|
|
|
|
const QCommandLineOption helpOption = parser.addHelpOption();
|
|
|
|
const QCommandLineOption versionOption = parser.addVersionOption();
|
|
|
|
|
|
|
|
if (!parser.parse(QCoreApplication::arguments())) {
|
|
|
|
*errorMessage = parser.errorText();
|
|
|
|
return CommandLineError;
|
|
|
|
}
|
2012-01-23 17:25:39 +00:00
|
|
|
|
2014-03-11 08:35:35 +00:00
|
|
|
if (parser.isSet(versionOption))
|
|
|
|
return CommandLineVersionRequested;
|
|
|
|
|
|
|
|
if (parser.isSet(helpOption))
|
|
|
|
return CommandLineHelpRequested;
|
|
|
|
|
|
|
|
if (parser.isSet(nameServerOption)) {
|
|
|
|
const QString nameserver = parser.value(nameServerOption);
|
|
|
|
query->nameServer = QHostAddress(nameserver);
|
|
|
|
if (query->nameServer.isNull() || query->nameServer.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol) {
|
|
|
|
*errorMessage = "Bad nameserver address: " + nameserver;
|
|
|
|
return CommandLineError;
|
2012-01-23 17:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-11 08:35:35 +00:00
|
|
|
|
|
|
|
if (parser.isSet(typeOption)) {
|
|
|
|
const QString typeParameter = parser.value(typeOption);
|
|
|
|
const int type = typeFromParameter(typeParameter.toLower());
|
|
|
|
if (type < 0) {
|
|
|
|
*errorMessage = "Bad record type: " + typeParameter;
|
|
|
|
return CommandLineError;
|
2013-10-17 08:21:18 +00:00
|
|
|
}
|
2014-03-11 08:35:35 +00:00
|
|
|
query->type = static_cast<QDnsLookup::Type>(type);
|
2013-10-17 08:21:18 +00:00
|
|
|
}
|
2014-03-11 08:35:35 +00:00
|
|
|
|
|
|
|
const QStringList positionalArguments = parser.positionalArguments();
|
|
|
|
if (positionalArguments.isEmpty()) {
|
|
|
|
*errorMessage = "Argument 'name' missing.";
|
|
|
|
return CommandLineError;
|
|
|
|
}
|
|
|
|
if (positionalArguments.size() > 1) {
|
|
|
|
*errorMessage = "Several 'name' arguments specified.";
|
|
|
|
return CommandLineError;
|
2012-01-23 17:25:39 +00:00
|
|
|
}
|
2014-03-11 08:35:35 +00:00
|
|
|
query->name = positionalArguments.first();
|
|
|
|
|
|
|
|
return CommandLineOk;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
DnsManager::DnsManager()
|
2019-11-01 12:21:32 +00:00
|
|
|
: dns(new QDnsLookup(this))
|
2014-03-11 08:35:35 +00:00
|
|
|
{
|
2019-11-01 12:21:32 +00:00
|
|
|
connect(dns, &QDnsLookup::finished, this, &DnsManager::showResults);
|
2014-03-11 08:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DnsManager::execute()
|
|
|
|
{
|
|
|
|
// lookup type
|
|
|
|
dns->setType(query.type);
|
|
|
|
if (!query.nameServer.isNull())
|
|
|
|
dns->setNameserver(query.nameServer);
|
|
|
|
dns->setName(query.name);
|
2012-01-23 17:25:39 +00:00
|
|
|
dns->lookup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DnsManager::showResults()
|
|
|
|
{
|
|
|
|
if (dns->error() != QDnsLookup::NoError)
|
|
|
|
printf("Error: %i (%s)\n", dns->error(), qPrintable(dns->errorString()));
|
|
|
|
|
|
|
|
// CNAME records
|
2018-12-31 16:41:11 +00:00
|
|
|
const QList<QDnsDomainNameRecord> cnameRecords = dns->canonicalNameRecords();
|
|
|
|
for (const QDnsDomainNameRecord &record : cnameRecords)
|
2012-01-23 17:25:39 +00:00
|
|
|
printf("%s\t%i\tIN\tCNAME\t%s\n", qPrintable(record.name()), record.timeToLive(), qPrintable(record.value()));
|
|
|
|
|
|
|
|
// A and AAAA records
|
2018-12-31 16:41:11 +00:00
|
|
|
const QList<QDnsHostAddressRecord> aRecords = dns->hostAddressRecords();
|
|
|
|
for (const QDnsHostAddressRecord &record : aRecords) {
|
2012-01-23 17:25:39 +00:00
|
|
|
const char *type = (record.value().protocol() == QAbstractSocket::IPv6Protocol) ? "AAAA" : "A";
|
|
|
|
printf("%s\t%i\tIN\t%s\t%s\n", qPrintable(record.name()), record.timeToLive(), type, qPrintable(record.value().toString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// MX records
|
2018-12-31 16:41:11 +00:00
|
|
|
const QList<QDnsMailExchangeRecord> mxRecords = dns->mailExchangeRecords();
|
|
|
|
for (const QDnsMailExchangeRecord &record : mxRecords)
|
2012-01-23 17:25:39 +00:00
|
|
|
printf("%s\t%i\tIN\tMX\t%u %s\n", qPrintable(record.name()), record.timeToLive(), record.preference(), qPrintable(record.exchange()));
|
|
|
|
|
|
|
|
// NS records
|
2018-12-31 16:41:11 +00:00
|
|
|
const QList<QDnsDomainNameRecord> nsRecords = dns->nameServerRecords();
|
|
|
|
for (const QDnsDomainNameRecord &record : nsRecords)
|
2012-01-23 17:25:39 +00:00
|
|
|
printf("%s\t%i\tIN\tNS\t%s\n", qPrintable(record.name()), record.timeToLive(), qPrintable(record.value()));
|
|
|
|
|
|
|
|
// PTR records
|
2018-12-31 16:41:11 +00:00
|
|
|
const QList<QDnsDomainNameRecord> ptrRecords = dns->pointerRecords();
|
|
|
|
for (const QDnsDomainNameRecord &record : ptrRecords)
|
2012-01-23 17:25:39 +00:00
|
|
|
printf("%s\t%i\tIN\tPTR\t%s\n", qPrintable(record.name()), record.timeToLive(), qPrintable(record.value()));
|
|
|
|
|
|
|
|
// SRV records
|
2018-12-31 16:41:11 +00:00
|
|
|
const QList<QDnsServiceRecord> srvRecords = dns->serviceRecords();
|
|
|
|
for (const QDnsServiceRecord &record : srvRecords)
|
2012-01-23 17:25:39 +00:00
|
|
|
printf("%s\t%i\tIN\tSRV\t%u %u %u %s\n", qPrintable(record.name()), record.timeToLive(), record.priority(), record.weight(), record.port(), qPrintable(record.target()));
|
|
|
|
|
|
|
|
// TXT records
|
2018-12-31 16:41:11 +00:00
|
|
|
const QList<QDnsTextRecord> txtRecords = dns->textRecords();
|
|
|
|
for (const QDnsTextRecord &record : txtRecords) {
|
2012-01-23 17:25:39 +00:00
|
|
|
QStringList values;
|
2018-12-31 16:41:11 +00:00
|
|
|
const QList<QByteArray> dnsRecords = record.values();
|
|
|
|
for (const QByteArray &ba : dnsRecords)
|
2012-05-02 14:32:26 +00:00
|
|
|
values << "\"" + QString::fromLatin1(ba) + "\"";
|
2012-05-18 18:00:23 +00:00
|
|
|
printf("%s\t%i\tIN\tTXT\t%s\n", qPrintable(record.name()), record.timeToLive(), qPrintable(values.join(' ')));
|
2012-01-23 17:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QCoreApplication::instance()->quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QCoreApplication app(argc, argv);
|
|
|
|
|
2014-03-11 08:35:35 +00:00
|
|
|
//! [1]
|
|
|
|
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
|
|
|
|
QCoreApplication::setApplicationName(QCoreApplication::translate("QDnsLookupExample", "DNS Lookup Example"));
|
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.setApplicationDescription(QCoreApplication::translate("QDnsLookupExample", "An example demonstrating the class QDnsLookup."));
|
|
|
|
DnsQuery query;
|
|
|
|
QString errorMessage;
|
|
|
|
switch (parseCommandLine(parser, &query, &errorMessage)) {
|
|
|
|
case CommandLineOk:
|
|
|
|
break;
|
|
|
|
case CommandLineError:
|
|
|
|
fputs(qPrintable(errorMessage), stderr);
|
|
|
|
fputs("\n\n", stderr);
|
|
|
|
fputs(qPrintable(parser.helpText()), stderr);
|
|
|
|
return 1;
|
|
|
|
case CommandLineVersionRequested:
|
|
|
|
printf("%s %s\n", qPrintable(QCoreApplication::applicationName()),
|
|
|
|
qPrintable(QCoreApplication::applicationVersion()));
|
|
|
|
return 0;
|
|
|
|
case CommandLineHelpRequested:
|
|
|
|
parser.showHelp();
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
}
|
|
|
|
//! [1]
|
|
|
|
|
2012-01-23 17:25:39 +00:00
|
|
|
DnsManager manager;
|
2014-03-11 08:35:35 +00:00
|
|
|
manager.setQuery(query);
|
2012-01-23 17:25:39 +00:00
|
|
|
QTimer::singleShot(0, &manager, SLOT(execute()));
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|