如何解码跟踪器响应(Bittorent)中的对等值
How can I decode the peers value in the tracker response (Bittorent)
我正试图自己实现bittorent协议,但我在使用c++解码跟踪器响应中的"Peers"值时遇到了问题。
根据bittorent协议文档:
peers:(二进制模型)peers值可以是由6个字节的倍数组成的字符串,而不是使用上述字典模型。前4个字节是IP地址,后2个字节是端口号。全网(big-endian)表示法。
如何使用c++解码这些ip和端口号?
我已经重复了这个代码,有一点不正确:
void DecodePeers(OrderedMap<std::string, int> &map, const char * buffer, int i)
{
int counter = 0;
while (*(buffer + counter) != NULL)
{
//std::vector<TByte> portNum;
short port;
for (int i = counter; i < counter + 4; i++)
{
//*(peerIp + i - counter) = *(buffer + i);
}
counter += 4;
//*(peerIp + 4) = ' ';
char twobytes[2];
twobytes[0] = *(buffer + counter + 0);
twobytes[1] = *(buffer + counter + 1);
unsigned int x;
std::stringstream ss;
ss << std::hex << twobytes[0];
ss >> x;
// output it as a signed type
std::cout << static_cast<int>(x) << std::endl;
port = ((twobytes[1] << 8) | twobytes[0]);
//port = (short) twobytes;
counter += 2;
//std::string str(portNum.begin(), portNum.end());
std::cout << std::endl;
std::cout << port << std::endl ;
char * bbuffer = new char[100];
for (int i = 0; i < 1; i++) {
//unsigned int inte = (unsigned int)str;(
//_itoa_s((int) *str.c_str(), bbuffer, 100, 10);
//sprintf_s(bbuffer, 50, (const char *) "%d", str.c_str());
}
std::cout << std::endl;
//int port = atoi(portNum);
//map.Insert(str, port);
}
}
有人知道我如何将这些数字翻译成数字可读的响应吗?
Example of peers value: P^L♠*j.t╤u→πe199711
我试图在您的缓冲区上使用此代码来解码比特洪流协议对等
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
char *buf = "P^L♠*j.t╤u→πe199711";
int chunks = strlen(buf) / 6 ;
int offset = 0;
unsigned char *ip;
unsigned short *port;
int recsDone = 0;
while (recsDone++ <= chunks){
ip = (unsigned char *) buf+offset;
port = (unsigned short *) buf+offset+4;
printf("%s - %dn",inet_ntoa(*(in_addr*)ip),ntohs(*port));
offset+=6;
}
}
我得到这个作为输出:
80.94.76.226-1189242.106.46.116-12601
如果有效,请告诉我们。
我没有使用过这个协议,显示的代码有点令人困惑。例如,我不知道什么是peerIp。但是基于peer_ id中字段的描述,https://wiki.theory.org/BitTorrentSpecification#peer_id,我建议:
#include <arpa/inet.h>
#include <stdint.h>
#include <stdio.h>
uint32_t *peerIPAux;
struct sockaddr_in peer_data;
char str[INET_ADDRSTRLEN];
uint16_t *peer_port_ptr;
uint16_t peer_port;
//Here we need to test if IPv4 address, represented in an integer value is
//actually in network notation or not. First assume what documentation says
//is true (it is in network notation)
//INSIDE THE WHILE LOOP
peerIPAux = (uint32_t *)(buffer + counter);
peer_data.sin_addr.s_addr = ntohl(*peerIPAux); //If this doesn't work try without ntohl
//http://linux.die.net/man/3/inet_ntop
inet_ntop(AF_INET, &(peer_data.sin_addr), str, INET_ADDRSTRLEN); //check error code here
printf("%sn", str); // or std::cout << str << std::endl;
counter += 4;
peer_port_ptr = (uint16_t *) (buffer + counter);
peer_port = ntohs(*peer_port_ptr);
printf("PORT: %d", peer_port); // in C
....
相关文章:
- 使用移动调用对等构造函数unique_ptr默认构造函数
- 当客户端在 write() 期间终止连接时,由对等套接字错误重置连接
- curl_easy_perform() 失败:SSL 对等证书或 SSH 远程密钥不正常
- 从 NAT 后面建立对等连接
- iocp openssl 对等服务器在与 ConnectEx 连接后关闭连接
- 对等方断开连接后未释放 SSL 内存
- 它是否需要为webrtc本机应用程序中的每个对等体提供单独的rtc::Runnable线程和PeerConnection
- QtRO - 类 qremoteobjects - 如何连接 2 个和更多远程对等体 beetwen TCP
- 对等如何实际工作网络/端口明智
- Libtorrent通过IP添加对等
- TCP套接字:在发送之前检测对等方是否已关闭?(Linux)
- 发送大型数据时由对等方重置 Tcp 连接
- SSL握手成功后如何获得对等方的QSsl证书
- C++中的Bittorrent客户端,在非阻塞套接字上连接到对等端总是超时
- 有没有办法获取 OpenSSL X509 证书名称,该证书名称将在 C++ 中发送到对等方
- 使用 select() 使用 UDP 套接字从多个对等方接收,同时从 STDIN 获取用户输入
- C++中的BitTorrent对等线路消息
- 在不可靠的网络中,是否有任何框架可以将一个对等体上生成的数据与所有其他对等体同步
- Flash客户端c++服务器连接被对等方重置
- 如何解码跟踪器响应(Bittorent)中的对等值