我在C中编写了一个torrent客户端,它实现了UDP跟踪协议,如下所述:
http://www.libtorrent.org/udp_tracker_protocol.html
我的项目位于:https://github.com/unovongalixor/uvgTorrent
我正在成功发送连接请求并接收我的连接ID。
我使用以下代码在打包结构中准备我的所有数据,然后通过udp将其发送到跟踪器。有趣的是,如果我通过包装它来破坏连接ID htonll()我得到一个错误,如协议所述。即action == 3并显示错误消息" Connection ID不匹配。"
然而,当我收到连接ID时,我收到垃圾回复。 action == 16777216.交易ID与我提供的交易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));