如何解码跟踪器响应(Bittorent)中的对等值 我尝试自己实现BITTRONTRONTL,并且用C++ + /P>对跟踪器响应中的“对等”值进行解码是有问题的。
根据bittorent协议文档: 对等方:(二进制模型)对等方值可以是由6个字节的倍数组成的字符串,而不是使用上述字典模型。前4个字节是IP地址,后2个字节是端口号。全网络(大端)表示法 怎样用C+++</p >解码这些IP和端口号? 我已经修改了这段代码,但它不正确:如何解码跟踪器响应(Bittorent)中的对等值 我尝试自己实现BITTRONTRONTL,并且用C++ + /P>对跟踪器响应中的“对等”值进行解码是有问题的。,c++,protocols,p2p,network-protocols,bittorrent,C++,Protocols,P2p,Network Protocols,Bittorrent,根据bittorent协议文档: 对等方:(二进制模型)对等方值可以是由6个字节的倍数组成的字符串,而不是使用上述字典模型。前4个字节是IP地址,后2个字节是端口号。全网络(大端)表示法 怎样用C+++解码这些IP和端口号? 我已经修改了这段代码,但它不正确: void DecodePeers(OrderedMap<std::string, int> &map, const char * buffer, int i) { int counter = 0;
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) = '\0';
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);
}
}
我没有使用过这个协议,显示的代码有点混乱。例如,我不知道什么是peerIp。但根据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("%s\n", 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
....
#包括
#包括
#包括
uint32_t*peerIPAux;
对等数据中的结构sockaddr_;
char str[INET_ADDRSTRLEN];
uint16\u t*对等端口\u ptr;
uint16对等端口;
//这里我们需要测试以整数值表示的IPv4地址是否为
//实际上是网络符号还是非网络符号。首先假设文档中说了什么
//为真(以网络表示法表示)
//在WHILE循环中
peerIPAux=(uint32_t*)(缓冲区+计数器);
peer_data.sin_addr.s_addr=ntohl(*peerIPAux)//如果这不起作用,试着不用ntohl
//http://linux.die.net/man/3/inet_ntop
inet_ntop(AF_inet,&(peer_data.sin_addr)、str、inet_ADDRSTRLEN)//在这里检查错误代码
printf(“%s\n”,str);//或者std::cout我试图在您的缓冲区中使用此代码来解码bittorrent协议
#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 - %d\n",inet_ntoa(*(in_addr*)ip),ntohs(*port));
offset+=6;
}
}
#包括
int main(int argc,char*argv[])
{
char*buf=“P^L♠*j、 t╤U→πe199711”;
int chunks=strlen(buf)/6;
整数偏移=0;
无符号字符*ip;
无符号短*端口;
int recsDone=0;
while(recsDone++)你知道这个数字应该产生哪个ip吗?我得到这些80.94.76.226 29742 42.106.46.116 14641也许它是正确的。你是如何得到它的?你没有回答,所以我发布了答案,代码已经在这里了。非常感谢!代码工作正常,但请注意你放了“sizeof(buf)”/sizeof返回变量的大小,因为buf是指针,所以它将始终返回4,而不是sizeof,我将“strlen(buf)/6,它成功了!非常感谢!:)不客气,您的长度是对的,我已经修复了它。
#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 - %d\n",inet_ntoa(*(in_addr*)ip),ntohs(*port));
offset+=6;
}
}