PDA

View Full Version : tftp with QUdpSocket - cannot do - have to use Q3SocketDevice



piper
30th November 2010, 11:33
I'm talking to a measurement instrument using tftp.

The funny thing about tftp protocol is that you connect to a server to a specified port, and the server responses from a different, connection specific port (which the server decides), which you then use in further communications.

With QUdpSocket you cannot get the new server port number. The documentation of peerPort() says 'Returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0.'

If you connect a QUdpSocket to a server for tftp protocol you never receive any data (at least I didn't). readyRead() never triggers, using a timer an polling the socket never reads anything.

If you don't connect and just send datagrams, you receive the replies, but as the socket is not in connected state, peerPort returns 0. (Why this limitation, if this could be lifted then QUdpSocket could be used???)

So, I ended up using Q3SocketDevice, this works, my problem is now solved. :cool:

But what happens when one day we get Qt5, which probably has Q4Support, but no Q3Support anymore... No tftp possible with Qt anymore ??

Maybe this ConnectedState limitation in peerPort() should be lifted ?

wysota
30th November 2010, 12:33
If QUdpSocket doesn't work then you must have messed something up.

yvan40
5th July 2012, 10:18
Hy, I'm facing the same problem!

Here is a test code which is sending a write request to a TFTP server.
The server sends a response to this request, so I should receive the signal "readyRead()" from the QUdpSocket.
But I never get it!




basicTester::basicTester(QObject *parent) :
QObject(parent)
{
dAddr = QHostAddress("192.168.111.84"); //tftp server address
dPort = 69;

sock = new QUdpSocket(this);
connect(sock, SIGNAL(readyRead()), this, SLOT(readData()));
connect(sock, SIGNAL(stateChanged(QAbstractSocket::SocketState)) ,
this, SLOT(stateChanged(QAbstractSocket::SocketState)));

sock->connectToHost(dAddr, dPort);
}

void basicTester::stateChanged(QAbstractSocket::SocketS tate sockstate){
qDebug() << sock->state();
switch(sockstate){
case QAbstractSocket::ConnectedState:
qDebug() << "Socket is connected!";
qDebug() << "sock peer : " << sock->peerAddress() << sock->peerPort();
qDebug() << "sock local: " << sock->localAddress() << sock->localPort();
send();
break;
}
}

void basicTester::send() {
QByteArray dgram;
QString remote("myfile.txt");
QString modestring ("octet");
dgram.resize(( 2 + remote.length() + 1 + modestring.length() + 1 ));

//TFTP WRQ
dgram[0] = 0x00;
dgram[1] = 0x02;
memcpy( dgram.data()+2, remote.toAscii(), remote.length() +1 );
memcpy( dgram.data()+2+remote.length()+1, modestring.toAscii(), modestring.length() +1 );

qDebug() << "Writing dgram to " << dAddr << ":" << dPort;
if(sock->write(dgram) == -1){
qDebug() << "Error sending WRQ packet " << sock->errorString();
}
}

void basicTester::readData() {
qDebug() << "basicTester::readData";
}


I've tested with 3 different tftp server, and I always get the same result.
Using wireshark I can see that the server is sending the response, but the QUdpSocket doesn't get it.

I've made a basic UDP echo server (from Qt Example), I've replaced the tftp server by this echoServer, and there I get the "readyRead()" signal!

The difference I can observe with wireshark is:

with TFTP server:
-> Client sends request from source port 3538(or what ever is generated on connection) ; destination port 69
-> Server sends response from source port 4096 ; destination port 3528

with UDP Echo Server:
-> Client sends request from source port 3545(or what ever is generated on connection) ; destination port 69
-> Server sends response from source port 69; destination port 3545


So the problem seems to be that the tftp server responds with a different source port than the one we used to connect.

Any clue on how to solve this?

wysota
5th July 2012, 11:04
The clue is to understand that UDP is a connectionless protocol. Calling connectToHost() does not establish any "connection" between the hosts. It only emulates a connection by associating a {host,port} pair with the socket and when a datagram is received from that pair, it is treated as part of a "connection" (and one that is sent is getting sent to that associated host and port). In your case the datagram doesn't match the {host,port} pair so readyRead() is not emitted. If you comment out your connectToHost() call and use readDatagram() and writeDatagram(), everything should work fine.

yvan40
5th July 2012, 17:25
Thanks for the answer!

I know that UDP is connectionLess, but using "connectToHost" is the only way I founded to get the client "local port".

If I just call writeDatagram(), then I don't get my local port, and I need this port for the readDatagram()!

wysota
5th July 2012, 21:04
Use QUdpSocket::bind() to bind your socket to a specified port.

yvan40
6th July 2012, 09:24
perfect!
When I tried bind() I still had the conenctToHost(), my bad!

Problem solved, thank's a lot!

the correct source code, if it can help someone else:


basicTester::basicTester(QObject *parent) :
QObject(parent)
{
dAddr = QHostAddress("192.168.111.84"); //tftp server address
dPort = 69;
sock = new QUdpSocket(this);
connect(sock, SIGNAL(readyRead()), this, SLOT(readData()));
sock->bind(2152); /// \TODO need generate random client TID, and check if port available (select another if not) ; see RFC1350
}

void basicTester::send() {
QByteArray dgram;
QString remote("myfile.txt");
QString modestring ("octet");
dgram.resize(( 2 + remote.length() + 1 + modestring.length() + 1 ));

//TFTP WRQ
dgram[0] = 0x00;
dgram[1] = 0x02;
memcpy( dgram.data()+2, remote.toAscii(), remote.length() +1 );
memcpy( dgram.data()+2+remote.length()+1, modestring.toAscii(), modestring.length() +1 );

qDebug() << "Writing dgram to " << dAddr << ":" << dPort;
if(sock->writeDatagram(dgram, dAddr, dPort) == -1){
qDebug() << "Error sending WRQ packet " << sock->errorString();
}
qDebug() << "sock peer : " << sock->peerAddress() << sock->peerPort();
qDebug() << "sock local: " << sock->localAddress() << sock->localPort();
}

void basicTester::readData() {
qDebug() << "basicTester::readData";
while(sock->hasPendingDatagrams()){
QByteArray datagram;
datagram.resize(sock->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
sock->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
qDebug() << "tftp server response : " << datagram.toHex();
}
}

piper
5th October 2012, 11:50
perfect!


sock->bind(2152); /// \TODO need generate random client TID, and check if port available (select another if not) ; see RFC1350


No need to worry about available ports if you let bind() handle it for you like this:


sock->bind();