如何解码跟踪器响应中的对等值 (Bittorrent)
How can I decode the peers value in the tracker response (Bittorent)
我正在尝试自己实现 bittorent 协议,但我在使用 C++ 解码跟踪器响应中的 "Peers" 值时遇到问题。
根据 bittorent 协议文档:
peers: (binary model) Instead of using the dictionary model described above, the peers value may be a string consisting of multiples of 6 bytes. First 4 bytes are the IP address and last 2 bytes are the port number. All in network (big endian) notation.
如何使用 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) = '[=10=]';
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
我没有使用过这个协议,显示的代码有点混乱。例如,我不知道什么是 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("%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
....
我试图在你的缓冲区上使用这段代码来解码 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;
}
}
我得到这个作为输出:
80.94.76.226 - 11892
42.106.46.116 - 12601
如果可行,请告诉我们。
我正在尝试自己实现 bittorent 协议,但我在使用 C++ 解码跟踪器响应中的 "Peers" 值时遇到问题。
根据 bittorent 协议文档:
peers: (binary model) Instead of using the dictionary model described above, the peers value may be a string consisting of multiples of 6 bytes. First 4 bytes are the IP address and last 2 bytes are the port number. All in network (big endian) notation.
如何使用 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) = '[=10=]';
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
我没有使用过这个协议,显示的代码有点混乱。例如,我不知道什么是 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("%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
....
我试图在你的缓冲区上使用这段代码来解码 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;
}
}
我得到这个作为输出:
80.94.76.226 - 11892 42.106.46.116 - 12601
如果可行,请告诉我们。