krpc_protocol v0.0.4 KRPCProtocol.Encoder
KRPCProtocol.Encoder provides functions to encode mainline DHT messages.
Summary
Types
A node_id is a bitstring with a size of 160 bit which uniquely identifies every node
A transaction ID (t_id) is a bitstring with a size of 16 bit which correlates multiple queries to the same node
A target also belongs to the same node_id space and is a bitstring with a size of 160 bit
Functions
This function returns a bencoded Mainline DHT message
This function generates a 16 bit (2 byte) random transaction ID and converts it to a binary and returns it. This transaction ID is echoed in the response
Types
node_id :: <<_::160>>
A node_id is a bitstring with a size of 160 bit which uniquely identifies every node.
t_id :: <<_::16>>
A transaction ID (t_id) is a bitstring with a size of 16 bit which correlates multiple queries to the same node.
target :: <<_::160>>
A target also belongs to the same node_id space and is a bitstring with a size of 160 bit.
Functions
This function returns a bencoded Mainline DHT message.
error
When the first argument is :error
, the function encodes an error message.
Example
iex> KRPCProtocol.encode(:error, code: 202, msg: "Server Error", tid: "aa")
"d1:eli202e12:Server Errore1:t2:aa1:y1:ee"
ping
The purpose of a ping query is check if another node is available. When the
first argument is :ping
, the function encodes a ping message. The function
needs the node_id
and a t_id
.
Example
iex> KRPCProtocol.encode(:ping, tid: "aa", node_id: "bb")
"d1:ad2:id2:bbe1:q4:ping1:t2:aa1:y1:qe"
find_node query
A find_node query is used to find more nodes for a given target. When the
first argument is :find_node
, the function encodes a find_node message. The
following arguments are required: transaction id, node id and a target. The
‘want’ argument is optional and can be set to “n6” if you are only intrested
in nodes that can provide an IPv6 address.
Examples
iex> KRPCProtocol.encode(:find_node, tid: "aa", node_id: "bb", target: "cc")
"d1:ad2:id2:bb6:target2:cc4:want2:n4e1:q9:find_node1:t2:aa1:y1:qe"
iex> KRPCProtocol.encode(:find_node, tid: "aa", node_id: "bb", target: "cc", want: "n6")
"d1:ad2:id2:bb6:target2:cc4:want2:n6e1:q9:find_node1:t2:aa1:y1:qe"
get_peers query
A get_peers query is used to find nodes associated with a info_hash. When the
first argument is :get_peers
, the function encodes a get_peers message. The
required arguments are node_id and info_hash. The optional arguments are
scrape
, noseed
, and want
. Please take a look at
BEP0005 and
BEP0033 for more information
about these arguments.
Examples
iex> KRPCProtocol.encode(:get_peers, node_id: "aa", info_hash: "bb" , tid: "cc")
"d1:ad2:id2:aa9:info_hash2:bbe1:q9:get_peers1:t2:cc1:y1:qe"
iex> KRPCProtocol.encode(:get_peers, node_id: "aa", info_hash: "bb" , tid: "cc", scrape: true)
"d1:ad2:id2:aa9:info_hash2:bb6:scrapei1ee1:q9:get_peers1:t2:cc1:y1:qe"
announce_peer
With the announce_peer message, a peer announces that it is downloading a
torrent on a specific port. When the first argument is :announce_peer
, the
function encodes a announce_peer message. Required arguments are nod_id,
info_hash and tid. Optional arguments are port, implied_port and token.
Examples
iex> KRPCProtocol.encode(:announce_peer, node_id: "aa", info_hash: "bb", tid: "dd", port: 2342)
"d1:ad2:id2:aa9:info_hash2:bb4:porti2342ee1:q13:announce_peer1:t2:dd1:y1:qe"
iex> KRPCProtocol.encode(:announce_peer, node_id: "aa", info_hash: "bb", tid: "dd", implied_port: true)
"d1:ad2:id2:aa12:implied_porti1e9:info_hash2:bbe1:q13:announce_peer1:t2:dd1:y1:qe"
ping reply
The answer to a ping message is a ping reply message.
Example
iex> KRPCProtocol.encode(:ping_reply, tid: "aa", node_id: "bb")
"d1:rd2:id2:bbe1:t2:aa1:y1:re"
find_node reply
A find_node reply contains contact information for the requested target. The
contact information must be a list which contains node information in the
following tuple: {"nodeid", {97, 98, 99, 100}, 9797}
. This works also for
IPv6 addresses.
Example
iex> KRPCProtocol.encode(:find_node_reply, node_id: "bb", nodes: [{"nodeid", {97, 98, 99, 100}, 9797}], tid: "aa")
"d1:rd2:id2:bb5:nodes12:nodeidabcd&Ee1:t2:aa1:y1:re"
get_peers reply
A get_peers reply contains contact information for a get_peers request. The
contact information must be a list which contains node information in the
following tuple: {{97, 98, 99, 100}, 9797}
. This works also for IPv6
addresses.
Example
iex>KRPCProtocol.encode(:get_peers_reply, node_id: "bb", values: [{{97, 98, 99, 100}, 9797}], tid: "aa", token: "b")
"d1:rd2:id2:bb5:token1:b6:values6:abcd&Ee1:t2:aa1:y1:re"
Specs
gen_tid :: t_id
This function generates a 16 bit (2 byte) random transaction ID and converts it to a binary and returns it. This transaction ID is echoed in the response.