1

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.

Leo
  • 11
  • 1
  • See: https://stackoverflow.com/questions/50094674/how-to-parse-ip-and-port-from-http-tracker-response/ `T Q + Г Х § => 84 81 43 xx yy 167 (yy*256+167=??) => 84.81.43.xx:(yy*256+167)` – Encombe May 12 '18 at 15:00
  • 2
    Possible duplicate of [How to parse Ip and port from http tracker response](https://stackoverflow.com/questions/50094674/how-to-parse-ip-and-port-from-http-tracker-response) – hlt May 12 '18 at 15:09
  • I saw that question, but I still can't understand how to decode information I got. Can you explain it, please? – Leo May 12 '18 at 20:42
  • The problem is not in decoding this response, but exactly in understanding how to get the readable representation of this: `TQ+ГХ§`. It's not what you said me about. – Leo May 12 '18 at 21:19
  • `TQ+ГХ§` is a character representation of the 6 bytes raw binary data, 4 bytes IPv4 + 2 bytes PORT in bigendian. You need to read 4 bytes an turn them to a IP and then read 2 bytes and turn them to a PORT. – Encombe May 12 '18 at 21:41

1 Answers1

1

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

user3606329
  • 2,405
  • 1
  • 16
  • 28