Leo
Leo

Reputation: 11

How to get peers' IPs and ports from tracker response

I write simple torrent-client in Qt and I don't understand how to get peers' IPs and ports from tracker response. I get response successfully, but exactly value of key peers looks unreadable:

d8:completei1976e10:incompletei54e8:intervali1800e5:peers6:TQ+ГХ§e

Why it looks so and how to make this data readable?

In BitTorrent specification it is said that value of peers is always sent in Big-Endian. I don't know if it can be a reason for unreadability but I suspect that.

Upvotes: 1

Views: 1234

Answers (1)

Michael Schm.
Michael Schm.

Reputation: 2534

Like Encombe said in the comments it's BigEndian. You can do it programmatically this way:

QByteArray peerTmp = "TQ+ГХ§e";
QHostAddress host;
uchar *data = (uchar *)peerTmp.constData();
uint ipAddress = 0;
uint port = (int(data[4]) << 8) + data[5]; 
ipAddress += uint(data[0]) << 24;
ipAddress += uint(data[1]) << 16;
ipAddress += uint(data[2]) << 8;
ipAddress += uint(data[3]);
host.setAddress(ipAddress);
qDebug() << "IP" << host.toString() << ":" << port;

IP 84.81.XX.208:37840

or if you use qFromBigEndian i. e.

QHostAddress peerIPAddress(qFromBigEndian<qint32>("TQ+Г"));
qDebug() << "IP" << peerIPAddress.toString();

See : http://doc.qt.io/qt-5/qtnetwork-torrent-trackerclient-cpp.html

Upvotes: 1

Related Questions