Qt4 DNS 代理 QUdpSocket

发布于 2024-08-04 22:40:44 字数 1273 浏览 3 评论 0原文

我本质上是想使用 Qt4 制作一个 DNS 代理应用程序。如果我将 DNS 名称服务器设置为“localhost”,那么我想将所有 DNS 请求转发到 RemoteSocket 对象中指定的服务器。除了将数据从 RemoteSocket 对象发送回请求 DNS 查找的 localSocket 对象之外,一切似乎都工作正常。

当写入 localSocket 时,我需要了解什么具体信息吗?问题似乎出在 readResponse() 中。

#include "dns.h"

Dns::Dns()
{
}

void Dns::initSocket()
{
    localDatagram = new QByteArray();
    remoteDatagram = new QByteArray();

    localSocket = new QUdpSocket();
    connect(localSocket, SIGNAL(readyRead()), this, SLOT(readRequest()), Qt::DirectConnection);
    localSocket->bind(QHostAddress::LocalHost, 53);

    remoteSocket = new QUdpSocket();
    remoteSocket->connectToHost(QHostAddress("4.2.2.1"), 53);
    connect(remoteSocket, SIGNAL(readyRead()), this, SLOT(readResponse()), Qt::DirectConnection);

}

void Dns::readRequest()
{
    while (localSocket->hasPendingDatagrams()) {
        localDatagram->resize(localSocket->pendingDatagramSize());\
        localSocket->readDatagram(localDatagram->data(), localDatagram->size());
        remoteSocket->write(*localDatagram);
    }
}

void Dns::readResponse()
{
    QByteArray bytes(remoteSocket->readAll());
    qDebug() << "BYTES: [" << bytes.toBase64() << "]";
    localSocket->write(bytes);
}

I'm essentially trying to make a DNS proxy application using Qt4. If I set my DNS nameserver to 'localhost' then I want to forward all DNS requests to the server specified in the remoteSocket object. Everything seems to be working fine except sending the data from the remoteSocket object back to the localSocket object which is requesting the DNS lookup.

When writing to localSocket, is there anything specific I need to know about that? The problem seems to be in readResponse().

#include "dns.h"

Dns::Dns()
{
}

void Dns::initSocket()
{
    localDatagram = new QByteArray();
    remoteDatagram = new QByteArray();

    localSocket = new QUdpSocket();
    connect(localSocket, SIGNAL(readyRead()), this, SLOT(readRequest()), Qt::DirectConnection);
    localSocket->bind(QHostAddress::LocalHost, 53);

    remoteSocket = new QUdpSocket();
    remoteSocket->connectToHost(QHostAddress("4.2.2.1"), 53);
    connect(remoteSocket, SIGNAL(readyRead()), this, SLOT(readResponse()), Qt::DirectConnection);

}

void Dns::readRequest()
{
    while (localSocket->hasPendingDatagrams()) {
        localDatagram->resize(localSocket->pendingDatagramSize());\
        localSocket->readDatagram(localDatagram->data(), localDatagram->size());
        remoteSocket->write(*localDatagram);
    }
}

void Dns::readResponse()
{
    QByteArray bytes(remoteSocket->readAll());
    qDebug() << "BYTES: [" << bytes.toBase64() << "]";
    localSocket->write(bytes);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2024-08-11 22:40:44

我假设使用 QUdpSocket::bind(),生成的套接字对象将能够使用访问方法获取对等地址/对等端口,但事实并非如此。

最终的解决方案是:

QHostAddress sender;
quint16 senderPort;

localSocket->readDatagram(localDatagram->data(), localDatagram->size(), &sender, &senderPort);

在 readResponse() 中,

localSocket->writeDatagram(bytes, sender, senderPort);

I was assuming that using QUdpSocket::bind(), the resulting socket object would be able to obtain the peerAddress/peerPort using the access methods, however that was not the case.

The final solution was to do:

QHostAddress sender;
quint16 senderPort;

localSocket->readDatagram(localDatagram->data(), localDatagram->size(), &sender, &senderPort);

And in readResponse(),

localSocket->writeDatagram(bytes, sender, senderPort);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文