tags:

views:

189

answers:

1

I am writing a DNS server for an application I'm working on and I'm running into some difficulties.

I want to bind to port 53 on localhost and listen for DNS lookup requests, then send back the response. Unfortunately, when I call QUdpSocket::bind() the resulting socket is not writeable. Is there a flag I need to pass to make it so I can send data back?

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

Later on, after the connection is established, I want to call one of the QUdpSocket::write* methods, but that is not working as writeable is false..

A: 

First, are you sure you want to bind to QHostAddress::Lockhost? You realise that this will not be visible on any public network, right? It will only be visible to applications on the same physical box..

Second, you're not checking the return parameter from the QUdpSocket::bind call, so you have no way of knowing whether the bind call actually succeeded.

Third, have you tried without the QUdpSocket::ShareAddress hint? The Qt documentation for BindMode enum cryptically states regarding ShareAddress:

However, because any service is allowed to rebind, this option is subject to certain security considerations.

I'm not sure what these security considerations are - but one of the first things to try is to simplify your code.

Finally, perhaps the problem is the code inside your onReadyRead() slot? Posting that code may help us diagnose the issue.

Thomi
Yes, I am sure about binding to LocalHost in this case.The code I pasted was the shortest amount of code possible to reproduce the problem case, so there was no error checking included. I'll include that information in the future.I was able to solve my situation using the same method I used in another question I asked before, at http://stackoverflow.com/questions/1392494/qt4-dns-proxy-qudpsocketThanks
Michael