当前位置:网站首页>Qt5 knowledge: DNS query
Qt5 knowledge: DNS query
2022-06-23 12:01:00 【Mr anhydrous】
One 、 explain
DNS Inquire about ,DNS Establish a consistent namespace through a hierarchical distributed database , Used to locate network resources .DNS The query process can be divided into local queries 、 Direct inquiry 、 Recursive queries and iterative queries .
Two 、 General query methods
2.1、 Local inquiry
The host has a recent DNS Query log , There are two main parts in it . One is hosts file , The file is saved in the client system disk , The file path is Windows/system32/drivers/etc/. The other is the client's cache , It can be used ipconfig/displaydns see .
If host initiated DNS Inquire about , First query hosts file , And then in the query DNS cache . If hosts The file was tampered with by a malicious program , Then the Internet will be abnormal , Even open bad Web pages .
Obviously , The local cache will not have http://qq.com Of DNS Record . therefore , Host to local server DNS The server initiates a query .
2.2、 Direct inquiry
Local DNS Server is 192.168.16.1, This is a home router , Local DNS There will be no corresponding... In the cache DNS Record , Because it is not responsible for parsing http://qq.com. therefore , Local DNS The server must forward the query request to the forwarder . This forwarder is the home router WAN Set in mouth DNS Address , There are usually two active and standby .
2.3、 Iterative query
The forwarder is based on the domain name level , Query the root server successively 、.com Domain server 、http://qq.com Domain server , Finally get the authorization response . This query process is called iterative query .
2.4、 recursive query
The forwarder returns the corresponding query results to the local DNS The server 192.168.16.1, Local DNS The server returns the query results to the host , Finally it is concluded that http://qq.com Of ns Record .
3、 ... and 、 Reference code
| dnslookup.h |
#include <QDnsLookup>
#include <QHostAddress>
//! [0]
struct DnsQuery
{
DnsQuery() : type(QDnsLookup::A) {}
QDnsLookup::Type type;
QHostAddress nameServer;
QString name;
};
//! [0]
class DnsManager : public QObject
{
Q_OBJECT
public:
DnsManager();
void setQuery(const DnsQuery &q) { query = q; }
public slots:
void execute();
void showResults();
private:
QDnsLookup *dns;
DnsQuery query;
};
| dnslookup.cpp |
#include "dnslookup.h"
#include <QCoreApplication>
#include <QDnsLookup>
#include <QHostAddress>
#include <QStringList>
#include <QTimer>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <stdio.h>
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;
}
//! [0]
enum CommandLineParseResult
{
CommandLineOk,
CommandLineError,
CommandLineVersionRequested,
CommandLineHelpRequested
};
CommandLineParseResult parseCommandLine(QCommandLineParser &parser, DnsQuery *query, QString *errorMessage)
{
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;
}
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;
}
}
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;
}
query->type = static_cast<QDnsLookup::Type>(type);
}
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;
}
query->name = positionalArguments.first();
return CommandLineOk;
}
//! [0]
DnsManager::DnsManager()
: dns(new QDnsLookup(this))
{
connect(dns, &QDnsLookup::finished, this, &DnsManager::showResults);
}
void DnsManager::execute()
{
// lookup type
dns->setType(query.type);
if (!query.nameServer.isNull())
dns->setNameserver(query.nameServer);
dns->setName(query.name);
dns->lookup();
}
void DnsManager::showResults()
{
if (dns->error() != QDnsLookup::NoError)
printf("Error: %i (%s)\n", dns->error(), qPrintable(dns->errorString()));
// CNAME records
const QList<QDnsDomainNameRecord> cnameRecords = dns->canonicalNameRecords();
for (const QDnsDomainNameRecord &record : cnameRecords)
printf("%s\t%i\tIN\tCNAME\t%s\n", qPrintable(record.name()), record.timeToLive(), qPrintable(record.value()));
// A and AAAA records
const QList<QDnsHostAddressRecord> aRecords = dns->hostAddressRecords();
for (const QDnsHostAddressRecord &record : aRecords) {
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
const QList<QDnsMailExchangeRecord> mxRecords = dns->mailExchangeRecords();
for (const QDnsMailExchangeRecord &record : mxRecords)
printf("%s\t%i\tIN\tMX\t%u %s\n", qPrintable(record.name()), record.timeToLive(), record.preference(), qPrintable(record.exchange()));
// NS records
const QList<QDnsDomainNameRecord> nsRecords = dns->nameServerRecords();
for (const QDnsDomainNameRecord &record : nsRecords)
printf("%s\t%i\tIN\tNS\t%s\n", qPrintable(record.name()), record.timeToLive(), qPrintable(record.value()));
// PTR records
const QList<QDnsDomainNameRecord> ptrRecords = dns->pointerRecords();
for (const QDnsDomainNameRecord &record : ptrRecords)
printf("%s\t%i\tIN\tPTR\t%s\n", qPrintable(record.name()), record.timeToLive(), qPrintable(record.value()));
// SRV records
const QList<QDnsServiceRecord> srvRecords = dns->serviceRecords();
for (const QDnsServiceRecord &record : srvRecords)
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
const QList<QDnsTextRecord> txtRecords = dns->textRecords();
for (const QDnsTextRecord &record : txtRecords) {
QStringList values;
const QList<QByteArray> dnsRecords = record.values();
for (const QByteArray &ba : dnsRecords)
values << "\"" + QString::fromLatin1(ba) + "\"";
printf("%s\t%i\tIN\tTXT\t%s\n", qPrintable(record.name()), record.timeToLive(), qPrintable(values.join(' ')));
}
QCoreApplication::instance()->quit();
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
//! [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]
DnsManager manager;
manager.setQuery(query);
QTimer::singleShot(0, &manager, SLOT(execute()));
return app.exec();
}
边栏推荐
- 基本数据类型和对应的包装类
- Introduction to redis - Chapter 3 - data structures and objects - Dictionary
- Design of routing service for multi Activity Architecture Design
- [comprehensive written test questions] 30 Concatenate substrings of all words
- ROS察微【51】:如何将里程计和 IMU 与 robots_localization 融合
- Use monotone stack to solve problems
- HMS Core 视频编辑服务开放模板能力,助力用户一键Get同款酷炫视频
- Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
- Qt知识:视图框架QGraphicsWidget详解
- Voice data annotation tools and platforms
猜你喜欢

Easy to understand soft route brushing tutorial

Introduction to redis - Chapter 1 - data structures and objects - simple dynamic string (SDS)

Go zero micro Service Practice Series (VI. cache consistency assurance)

Learning notes sweep crawler framework

Mysql, how to calculate the maximum value using stored procedures

年薪中位数超30万,南大AI专业首届毕业生薪资曝光

Getting started with redis - Chapter 2 - data structures and objects - linked lists

机器学习系列5:距离空间(1)

DuPont analysis: what is the investment value of Anyang Iron and Steel Co., Ltd?

运行时应用自我保护(RASP):应用安全的自我修养
随机推荐
Voice data annotation tools and platforms
汉源高科8路电话+1路百兆以太网RJ11电话光端机 8路PCM电话光端机
ROS knowledge: reading point cloud data files
Redis 入门-第一篇-数据结构与对象-简单动态字符串(SDS)
Simulation questions and answers of the latest national fire-fighting facility operators (primary fire-fighting facility operators) in 2022
【零基础微信小程序】基于百度大脑人像分割的证件照换底色小程序实战开发
Meta称英安全法可能“扫描所有私人信息” 或侵犯隐私
MySQL matches multiple values in one field
开源之夏中选名单已公示,基础软件领域成为今年的热门申请
并购增资或将有望启动东软越通新动能?
Three ways to learn at work
Go 语言使用 MySQL 的常见故障分析和应对方法
mysql,如何在使用存储过程计算最大值
One picture decoding opencloudos community open day
Slam Laser 2D (en utilisant Laser Scan matcher)
Oversampling Series II: Fourier transform and signal-to-noise ratio
Qt 知识:使用 QGraphicsPixmapItem类
Leetcode 1209. Delete all adjacent duplicates II in the string (not in the initial version)
蓝桥杯单片机(一)——关闭外设及熄灭LED
Meta said that the UK security law may "scan all private information" or infringe privacy