0

我正在用 C 编写一个 torrent 客户端,它实现了 UDP 跟踪器协议,如下所述:

http://www.libtorrent.org/udp_tracker_protocol.html

我的项目位于:https ://github.com/unovongalixor/uvgTorrent

我已成功发送连接请求并接收到我的连接 ID。

我使用以下代码将我的所有数据准备在一个打包结构中,然后通过 udp 发送到跟踪器。有趣的是,如果我通过包装 htonll() 来破坏连接 ID,我会收到协议所述的错误。即 action == 3 并且错误消息指示“连接 ID 不匹配”。

但是,当我在收到连接 ID 后发送连接 ID 时,会收到垃圾响应。action == 16777216。交易 ID 与我提供的匹配,但没有其他预期信息。

有人有类似的经历吗?这是我的代码。

this->generate_transID(this);

/* set up the packet to send to server */
int32_t transID = *this->last_transaction_id;

struct tracker_announce_request conn_request;
conn_request.connection_id = *this->connection_id;
conn_request.action = htonl(1);
conn_request.transaction_id = transID;

/* extract info hash */
Linkedlist * info_hash_list = string_utils.split(torrent->hash, ':');
const char * info_hash = (char *) info_hash_list->get(info_hash_list, 2);
// convert 50 character info_hash stringlocated in magnet_uri to 20 byte array
string_utils.hex_to_int8_t(info_hash, conn_request.info_hash, 40); // need to verify that tracker is receiving the correct value here

// generate peer id
for(int i = 0; i<=19; i++){
    conn_request.peer_id[i] = rand_utils.nrand8_t(rand() % 10);
}

conn_request.downloaded = htonll((uint64_t)0);
conn_request.left = htonll((uint64_t)0);
conn_request.uploaded = htonll((uint64_t)0);
conn_request.event = 0;
conn_request.ip = htonl(0);
conn_request.key = htonl(rand_utils.nrand32(rand() % 10));
conn_request.num_want = htonl(-1);
conn_request.port = htonl(5050);
conn_request.extensions = htonl(0);
// send packet
this->tracker_socket->send(this->tracker_socket, &conn_request, sizeof(struct tracker_announce_request));
4

0 回答 0