dht find_node没有回复

时间:2018-05-14 23:27:44

标签: java udp bittorrent dht

我正在编写dht服务器并遇到一些问题。 我将 find_node 请求发送到bootstraps,然后他们返回一些紧凑的节点信息(416bytes),其中包含16个节点信息 然后我bdecode并存储IP地址和端口,继续向这些节点发送 find_node 请求,但我没有得到任何响应。

此处的服务器代码(接收数据包并启动新线程以将节点添加到列表中)

public static LinkedList<Node> bdecode_find_node(DatagramPacket recvPacket) throws IOException {

    byte[]nodes_compact_info = (byte[]) ((Map) new BDecoder().decodeByteArray(recvPacket.getData()).get("r")).get("nodes");

    byte[]node_info = new byte[26];
    byte[]node_id = new byte[20];
    byte[]ip_byte = new byte[4];
    byte[]port_byte = new byte[2];
    int port;


    LinkedList<Node> nodes = new LinkedList<>();
    //System.out.println("nodes_compact_info.length"+nodes_compact_info.length);
    for (int i = 0;i<nodes_compact_info.length/26;i++) { //416 = 16 * 26byte_per_node 
        //0-19 id 20 21 22 23 ip 24 25 port

        node_info = Utils.subArray(nodes_compact_info, i*26, 26);           
        node_id=Utils.subArray(node_info, 0, 20);           
        ip_byte = Utils.subArray(node_info, 20, 4); 

        String ip = "";
        for (int j = 0;j<ip_byte.length;j++) {
            ip+=Utils.byteToUnsignedInt(node_info[j]);
            ip+=".";
        }
        ip = ip.substring(0, ip.length()-1);
        //System.out.println(ip);
        port_byte = Utils.subArray(node_info, 24, 2);
        port = Utils.byteArrayToInt(port_byte);

        nodes.add(new Node(node_id, ip, port, new Date()));         
    }
    return nodes;
}

我从sourceforge下载jbittorrent api并将其用于bdecode

public void get_neighbor() {

    ArrayList<Node> nodes = table.get_all_nodes();
    System.out.println("*********get_neighbor!!!");

    for (Node n : nodes) {
        try {

            byte[] find_node_query = BEncode.find_node(this.id, this.id);
            DatagramPacket sendPacket = new DatagramPacket(find_node_query, find_node_query.length,
                    InetAddress.getByName(n.getIp()), n.getPort());
            for (int i = 0;i<5;i++)
                socket.send(sendPacket);
            System.out.println("packet send to " + n.getIp() + ":" + n.getPort());
        } catch (UnknownHostException e1) {
            // TODO Auto-generated catch block
            System.out.println("UnknownHostException" + n.getIp());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("cant send packet");
        }
    }
}

最后,我将 find_node 请求发送到列表中的每个节点

for(var i=0; i<=1; i++) { eval("console.log(s" + i + ")"); }

我已经在那里停留了几天,并尝试了很多方法来找出哪里出错了,请帮帮我。

node compact info

这是从bootstrap和bdecoding node compact info

之后返回的

capture

send find_node to bootstrap

response from bootstrap

udp packet to node

.pcap file

2 个答案:

答案 0 :(得分:0)

  

我刚发现了一些东西。我在端口6881上修复了套接字,但是当我发送数据包时,端口会改变

是的,这是一个问题。您应绑定单个UDP套接字,然后继续重用该套接字。根据{{​​3}},它也应绑定到特定地址。

  

.pcap文件

数据包#2,来自引导节点的响应包含16个联系人。第一次联系是id: 0f9c86a1085677f9f1edb519bfc16b626f19fb11, IPv4/Port: 92.255.216.47:29501

数据包#18尝试联系47.216.255.92:29501

因此,您在解码IP地址时交换字节。 BEP 45Contacts编码,即大端。

你不需要使用字符串处理,network byte order有一个带字节数组的重载

答案 1 :(得分:0)

通过查看.pcap文件中的这四个数据包,我看到了一些问题

110 48.444854   10.3.157.234    82.221.103.244  BT-DHT  134 
...
113 48.844328   82.221.103.244  10.3.157.234    BT-DHT  528 reply=16 nodes 
114 48.911670   10.3.157.234    47.152.12.43    BT-DHT  134 
115 48.912589   10.3.157.234    181.39.146.92   BT-DHT  134 

这是发送到引导程序节点的 find_node 请求:router.utorrent.com [82.221.103.244]

Frame 110: 134 bytes on wire (1072 bits), 134 bytes captured (1072 bits)
Ethernet II, Src: IntelCor_1d:89:36 (5c:e0:c5:1d:89:36), Dst: Hangzhou_bf:8e:b0 (d4:61:fe:bf:8e:b0)
Internet Protocol Version 4, Src: 10.3.157.234, Dst: 82.221.103.244
User Datagram Protocol, Src Port: 6883, Dst Port: 6881
BitTorrent DHT Protocol
    Transaction ID: 7473
        Key: t
        Value: 7473
    Message type: Request
        Key: y
        Value: q
    Request type: find_node
        Key: q
        Value: find_node
    Request arguments: Dictionary...
        Key: a
        Value: Dictionary...
            id: ef417ba2c490541a8981d7905036c814364750a6
                Key: id
                Value: ef417ba2c490541a8981d7905036c814364750a6
            target: ef417ba2c490541a8981d7905036c814364750a6
                Key: target
                Value: ef417ba2c490541a8981d7905036c814364750a6

节点已将idtarget设置为值ef417ba2c490541a8981d7905036c814364750a6,因此应为 node_id
这有一个小问题,因为必须对bencoded字典中的键进行排序,但大多数实现仍会接受请求。
d1:t2:ts1:y1:q1:q9:find_node1:ad2:id20:.A....T.....P6..6GP.6:target20:.A....T.....P6..6GP.ee
应该是:
d1:ad2:id20:.A....T.....P6..6GP.6:target20:.A....T.....P6..6GP.e1:q9:find_node1:t2:ts1:y1:qe

这是来自bootstrap节点的响应:

Frame 113: 528 bytes on wire (4224 bits), 528 bytes captured (4224 bits)
Ethernet II, Src: Hangzhou_bf:8e:b0 (d4:61:fe:bf:8e:b0), Dst: IntelCor_1d:89:36 (5c:e0:c5:1d:89:36)
Internet Protocol Version 4, Src: 82.221.103.244, Dst: 10.3.157.234
User Datagram Protocol, Src Port: 6881, Dst Port: 6883
BitTorrent DHT Protocol
    ip: def983017ed6
        Key: ip
        Value: def983017ed6
    Response values: Dictionary...
        Key: r
        Value: Dictionary...
            id: ebff36697351ff4aec29cdbaabf2fbe3467cc267
                Key: id
                Value: ebff36697351ff4aec29cdbaabf2fbe3467cc267
            nodes: 16
                Key: nodes
                Value: 16 nodes
                Node 1 (id: 2f980c2befa8ae85856bfabd44fb76137c50e39e, IP/Port: 184.148.185.216:43611)
                Node 2 (id: b527925cd9b0f4610904dd2d75aa8c5aa6c900e3, IP/Port: 201.92.140.89:63505)
                Node 3 (id: 454d423032343241433131303030325858587a63, IP/Port: 119.136.154.198:23493)
                Node 4 (id: 4dbe732bad993048936a71ae2c7be49dc9ab24f9, IP/Port: 27.209.68.82:16001)
                Node 5 (id: 39a36bc7f75c494b90395113ab2fd5833c6d684c, IP/Port: 171.239.30.38:1035)
                Node 6 (id: a5ac55cdfdf231bf35e4e68e3b89b5430f0eedd7, IP/Port: 109.154.41.107:58580)
                Node 7 (id: 8f05d55f2129e53c4a1f87b13199b598c479b0aa, IP/Port: 189.78.63.188:27070)
                Node 8 (id: d1bd0a25de085c18d8c45c60f681f7074b9e0458, IP/Port: 95.81.104.30:27196)
                Node 9 (id: 30f0086f8618d7aa5a53b8ac0e3d5b080351fb9a, IP/Port: 70.118.138.155:26085)
                Node 10 (id: dd4797facea363d908b629ce93183c241f08f478, IP/Port: 189.83.128.160:45381)
                Node 11 (id: 0801a40c0e72581bc76e00262350243fc38d21af, IP/Port: 218.210.40.82:31021)
                Node 12 (id: cacf92f943a69abcc24891919d80953b3e70526d, IP/Port: 120.148.25.109:59348)
                Node 13 (id: 3f3834ab54f4215248f0f47d0d0265317d320b48, IP/Port: 189.62.200.109:49307)
                Node 14 (id: fd81b802855925bee2b2ed89134788babe2328bb, IP/Port: 189.152.39.89:43529)
                Node 15 (id: cef348726b9c1278675db1b1da4b605016aab4f8, IP/Port: 189.163.183.249:49937)
                Node 16 (id: 96f24f9a50ee407836fd124932f69e7d49dcad32, IP/Port: 178.77.35.128:34304)
    Transaction ID: 7473
        Key: t
        Value: 7473
    Message type: Response
        Key: y
        Value: r

包含16个节点的列表。

这是发送到列表中第一个节点的 find_node 请求:
Node 1 (id: 2f980c2befa8ae85856bfabd44fb76137c50e39e, IP/Port: 184.148.185.216:43611)

Frame 114: 134 bytes on wire (1072 bits), 134 bytes captured (1072 bits)
Ethernet II, Src: IntelCor_1d:89:36 (5c:e0:c5:1d:89:36), Dst: Hangzhou_bf:8e:b0 (d4:61:fe:bf:8e:b0)
Internet Protocol Version 4, Src: 10.3.157.234, Dst: 47.152.12.43
User Datagram Protocol, Src Port: 6883, Dst Port: 43611
BitTorrent DHT Protocol
    Transaction ID: 6476
        Key: t
        Value: 6476
    Message type: Request
        Key: y
        Value: q
    Request type: find_node
        Key: q
        Value: find_node
    Request arguments: Dictionary...
        Key: a
        Value: Dictionary...
            id: 2f980c2befa8ae85856bfabd44fb7614364750a6
                Key: id
                Value: 2f980c2befa8ae85856bfabd44fb7614364750a6
            target: ef417ba2c490541a8981d7905036c814364750a6
                Key: target
                Value: ef417ba2c490541a8981d7905036c814364750a6

以下是更多问题:porttarget是正确的,
id错了。它是查询的节点id = 2f980c2befa8ae85856bfabd44fb7614364750a6
而不是查询节点id = ef417ba2c490541a8981d7905036c814364750a6应该是 节点可能不会响应'id thieves'  IP也是错误的,而不是184.148.185.216它是47.152.12.43 = 0x2f980c2b,它是查询节点id 中的四个第一个字节。发送错误的IP当然是一个致命的问题 bencoded键未分类。

这是发送到列表中第二个节点的 find_node 请求:
Node 2 (id: b527925cd9b0f4610904dd2d75aa8c5aa6c900e3, IP/Port: 201.92.140.89:63505)

Frame 115: 134 bytes on wire (1072 bits), 134 bytes captured (1072 bits)
Ethernet II, Src: IntelCor_1d:89:36 (5c:e0:c5:1d:89:36), Dst: Hangzhou_bf:8e:b0 (d4:61:fe:bf:8e:b0)
Internet Protocol Version 4, Src: 10.3.157.234, Dst: 181.39.146.92
User Datagram Protocol, Src Port: 6883, Dst Port: 63505
BitTorrent DHT Protocol
    Transaction ID: 6f69
        Key: t
        Value: 6f69
    Message type: Request
        Key: y
        Value: q
    Request type: find_node
        Key: q
        Value: find_node
    Request arguments: Dictionary...
        Key: a
        Value: Dictionary...
            id: b527925cd9b0f4610904dd2d75aa8c14364750a6
                Key: id
                Value: b527925cd9b0f4610904dd2d75aa8c14364750a6
            target: ef417ba2c490541a8981d7905036c814364750a6
                Key: target
                Value: ef417ba2c490541a8981d7905036c814364750a6

与第一个节点存在相同的问题。以下所有请求都有相同的问题。