简体   繁体   English

Qt4 DNS代理QUdpSocket

[英]Qt4 DNS Proxy QUdpSocket

I'm essentially trying to make a DNS proxy application using Qt4. 我实质上是在尝试使用Qt4创建DNS代理应用程序。 If I set my DNS nameserver to 'localhost' then I want to forward all DNS requests to the server specified in the remoteSocket object. 如果我将DNS名称服务器设置为“ localhost”,那么我想将所有DNS请求转发到remoteSocket对象中指定的服务器。 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. 除了将数据从remoteSocket对象发送回请求DNS查找的localSocket对象之外,其他一切似乎都工作正常。

When writing to localSocket, is there anything specific I need to know about that? 写入localSocket时,我需要了解什么具体信息吗? The problem seems to be in readResponse(). 问题似乎出在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 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. 我假设使用QUdpSocket :: bind(),生成的套接字对象将能够使用访问方法来获取peerAddress / peerPort,但是事实并非如此。

The final solution was to do: 最终的解决方案是:

QHostAddress sender;
quint16 senderPort;

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

And in readResponse(), 在readResponse()中,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM