简体   繁体   English

将6字节IP /端口字符串解析为c中的in_addr结构

[英]Parse 6-byte IP/Port string to in_addr structure in c

I'm trying to build a little bittorrent client to store data in the bittorrent dht. 我正在尝试构建一个小小的bittorrent客户端,以将数据存储在bittorrent dht中。 Since much of the bittorrent functionality is not needed I'm trying to stay as simple as possible, since it only needs to function in a couple of limited use cases. 由于不需要很多bittorrent功能,因此我试图保持尽可能的简单,因为它只需要在几个有限的用例中运行即可。

I'm trying to parse information about new nodes from a message, which I get from a bootstrap node. 我正在尝试从消息中解析有关新节点的信息,该消息是从引导节点获得的。 The information about each node is 26-bytes long. 有关每个节点的信息为26个字节长。 20-bytes is node ID, 4 bytes IP address and 2 bytes port number. 节点ID为20字节,IP地址为4字节,端口号为2字节。 All the info is in binary. 所有信息都是二进制的。 I want to store the ip address and port number in a struct (myNode) which has got an in_addr parameter for the IP and unsinged short for the port. 我想将ip地址和端口号存储在一个结构(myNode)中,该结构具有IP的in_addr参数,而该端口的未命名简称。

typedef struct myNode {
   unsigned char id[21];
   struct in_addr ip;
   unsigned short port;
} myNode;

The parsing of the ID works fine. ID的解析工作正常。 But the parsing of the IP and port fails. 但是IP和端口的解析失败。 Sometimes it works, mostly it does not and never both work at the same time. 有时它会起作用,但大多数情况下它不会并且永远不会同时起作用。 I think there might be something wrong with the bitwise shift I'm doing, but I don't know what. 我认为我进行的按位转换可能有问题,但我不知道该怎么办。 When I printf the IP in hex after parsing I mostly get something like this: 当我在解析后以十六进制形式打印IP时,我通常会得到以下内容:

0xffffed3a 0xffffed3a

So it seems like the first to shifts are not working. 因此,似乎第一个转变没有奏效。 The other bits are correct. 其他位是正确的。 What am I doing wrong? 我究竟做错了什么?

int get_nodes_from_find_node(char *msg, myNode *setme) {
  char *fnstr = "nodes";
  char *pos, *ptr;
  long lenNodes;
  int nrNodes, i = 0, k = 0;

  pos = strstr(msg, fnstr);           // Find nodes section.
  if(pos == NULL) { return -1; }
  pos += strlen(fnstr);

  lenNodes = strtoul(pos, &ptr, 10);  // Save the length of the nodes section in lenNodes
  nrNodes = lenNodes / 26;            // Get number of nodes, about which info is delivered.
  pos = ptr+1;                        // Set pos to first node.

  while(nrNodes > 0) {
    for(k=0;k<20;k++,pos++) {       // First 20 bytes are ID.
        setme[i].id[k] = *pos;
    }
    setme[i].id[20] = '\0';         // Make ID a Null-terminated string.

    setme[i].ip.s_addr = (*pos << 24) | (*(pos+1) << 16) | (*(pos+2) << 8) | *(pos+3);   // Next 4 bytes are IP in network byte order.
    pos += 4;

    setme[i].port = (*pos << 8) | *(pos+1);  // Last 2 bytes are port.
    pos += 2;

    nrNodes--;
    i++;
  }     
  return 0;
}

Your problem is in (*pos << 24) | (*(pos+1) << 16) | (*(pos+2) << 8) | *(pos+3) 您的问题出在(*pos << 24) | (*(pos+1) << 16) | (*(pos+2) << 8) | *(pos+3) (*pos << 24) | (*(pos+1) << 16) | (*(pos+2) << 8) | *(pos+3) (*pos << 24) | (*(pos+1) << 16) | (*(pos+2) << 8) | *(pos+3) . (*pos << 24) | (*(pos+1) << 16) | (*(pos+2) << 8) | *(pos+3)

If any of the values in pos[0,...,3] is negative, then the entire expression is negative. 如果pos[0,...,3]值为负,则整个表达式为负。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM