Qt4 DNS代理QUdpSocket [英] Qt4 DNS Proxy QUdpSocket

查看:172
本文介绍了Qt4 DNS代理QUdpSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

当写到localSocket时,有什么具体需要知道吗?问题似乎在readResponse()。

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);
}


推荐答案

我假设使用QUdpSocket :: bind(),所得到的套接字对象将能够使用访问方法获得peerAddress / peerPort,但是不是这样。

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.

最终的解决方案做:

QHostAddress sender;
quint16 senderPort;

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

在readResponse()中,

And in readResponse(),

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

这篇关于Qt4 DNS代理QUdpSocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆