Query DHT Server
Asked Answered
A

2

1

I'm trying to make a simple query to a DHT server.

I'm trying to make a simple simple example to test queries to the servers, but I don't get a response from the server in any form...

Little example:

$socket = fsockopen("udp://router.bittorrent.com", 6881, $errno, $errstr, 3);
fwrite($socket, 'p'); $Head = fread($socket, 4);
$CheckStatus = socket_get_status($socket);      
if($CheckStatus["unread_bytes"] == 0)
{
    return 0;
}   
$do = 1;
while($do)
{
    $str = fread($socket,1);
    $stats .= $str;
    $status = socket_get_status($socket);
    if($status["unread_bytes"] == 0)
    {
        $do = 0;
    }
}
fclose($socket);

The info about the queries in DHT server is here: http://www.bittorrent.org/beps/bep_0005.html#dht-queries

But I don't understand how make this with PHP. Is this possible? What's the problem with my code?

Augusto answered 18/2, 2012 at 9:57 Comment(2)
Any DHT Torrent querry example here?Augusto
I would advise against using PHP for this kind of thing, it's not well-suited for juggling the binary data used in the bittorrent kademlia implementation. As for your query, p is not a valid bencoded DHT requestWashedout
F
1

As the8472 mentions, your client is sending p, which is not a valid query. To see valid query formats, look here: bep 005

Fleshy answered 20/8, 2012 at 6:35 Comment(0)
B
0

First off you need to specify if your trying to do a find_node or get_peers. Assuming its find_node your request should look like this: {"t":"aa", "y":"q", "q":"find_node", "a": {"id":"abcdefghij0123456789", "target":"mnopqrstuvwxyz123456"}}.

  • t = TID or random ID for packet

  • y = query

  • a = the request object

  • target = being the node list your requesting

  • id = your node ID - this is a CRC32C hash of your IP + a random number. (your IP should be 127.0.0.1 when creating if you do not know it yet.)

  • make sure this is in bencode format not json.

Exmple of making the ID:

    fn derive_uid(&mut self) {
        let mut ip: Vec<u8> = match self.consensus_external_address {
            IpAddr::V4(v4) => v4.octets().to_vec(),
            IpAddr::V6(v6) => v6.octets().to_vec(),
        };

        let mask: Vec<u8> = if ip.len() == 4 {
            V4_MASK.to_vec()
        } else {
            V6_MASK.to_vec()
        };

        for i in 0..mask.len() {
            ip[i] &= mask[i];
        }

        //let mut rng = rand::thread_rng();
        let rand: u8 = /*rng.gen::<u8>()*/utils::random::gen::<u8>() & 0xFF;
        let r = rand & 0x7;

        ip[0] |= r << 5;

        let mut c = CRC32c::new();
        c.update(&ip, 0, ip.len());
        let crc = c.get_value();

        let mut bid = [0u8; ID_LENGTH];
        bid[0] = (crc >> 24) as u8;
        bid[1] = (crc >> 16) as u8;
        bid[2] = ((crc >> 8) as u8 & 0xF8) | (/*rng.gen::<u8>()*/utils::random::gen::<u8>() & 0x7);

        for i in 3..19 {
            bid[i] = /*rng.gen::<u8>()*/utils::random::gen::<u8>() & 0xFF;
        }

        bid[19] = rand & 0xFF;

        self.uid = Some(UID::from(bid));
    }
  • Another thing to note, router.bittorrent.com has not been replying very often lately, in fact not at all for 2 months. I recommend trying this node: 87.98.162.88 - this is the second DNS resolve of dht.transmissionbt.com. For some reason none of the major bootstrap nodes are replying.

  • I'm pretty sure you cant do this in PHP, I don't think UDP is supported, I may be wrong. Your other problem would be async factors with PHP.

Broadbill answered 16/10 at 5:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.