I try to use PHP to do a DHT crawler, because the transmission of data using Bencode, this is not a problem, can be correctly encoded decoding, but if you send find_node data, the data obtained there is a nodes, This is a binary stream encoded data using a struct structure, according to the DHT protocol, is: 20 bytes of Node ID + 4 bytes of IP + 2 bytes of port, I have seen in Python encoding decoding method, but can not be converted to PHP, in Python this translates:
def decodenodes (nodes): "" "Turn the received nodes to the precedent table. Data format: [(Node ID, IP, port), (node ID, IP, port), (node ID, IP, port) ....] "" " n = [] nrnodes = len (nodes)/ nodes = Unpack ("!" + "20sIH" * nrnodes, nodes) for i in Xrange (nrnodes): Nid, IP, port = nodes[i * 3], Numtodottedquad (nodes[i * 3 + 1]), Nodes[i * 3 + 2]print nid N.append (NID, IP, port)
return ndef Encodenodes (nodes): "" "" " Encode" nodes with Decodenodes "" " n = [] for node in nodes : n.extend ([Node.nid, Dottedquadtonum (NODE.IP), Node.port]) return Pack ("!" + "20sIH" * len (nodes), *n)
How can I change this in PHP?
Reply to discussion (solution)
20 bytes of node id:a20 or A20 if it is filled with blanks, use the latter
4-byte ip:l IP address converted to unsigned long-shaped number with Ip2long
2-byte port: S
Packaged
$s = pack (' A20ls ', $nodeid, Ip2logn ($IP), 456);
Unpack
$d = Unpack (' A20nodeid/lip/sport ', $s);
$d [' ip '] = logn2ip ($d [' IP ']);
Print_r ($d); It's clear.
20 bytes of node id:a20 or A20 if it is filled with blanks, use the latter
4-byte ip:l IP address converted to unsigned long-shaped number with Ip2long
2-byte port: S
Packaged
$s = pack (' A20ls ', $nodeid, Ip2logn ($IP), 456);
Unpack
$d = Unpack (' A20nodeid/lip/sport ', $s);
$d [' ip '] = logn2ip ($d [' IP ']);
Print_r ($d); It's clear.
Thank you very much, it's settled.