Skip to content

KRPCProtocol is an elixir package for decoding and encoding mainline DHT messages.

License

Notifications You must be signed in to change notification settings

faried/krpc_protocol

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KRPCProtocol

Build Status

KRPCProtocol is an elixir package for decoding and encoding mainline DHT messages.

Installation

First, add krpc_protocol to your mix.exs dependencies:

def deps do
    [{:krpc_protocol, "~> 0.0.4"}]
end

Then, update your dependencies:

$ mix deps.get

Usage

Encoding

ping message

iex> KRPCProtocol.encode(:ping, tid: "aa", node_id: "aa")
"d1:ad2:id2:aae1:q4:ping1:t2:aa1:y1:qe"

find node query

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"

get_peers query

iex> KRPCProtocol.encode(:get_peers, node_id: "aa", info_hash: "bb", want: "n6", scrape: true, tid: "aa")
"d1:ad2:id2:aa9:info_hash2:bb6:scrapei1e4:want2:n6e1:q9:get_peers1:t2:aa1:y1:qe"

announce

iex(5)> KRPCProtocol.encode(:announce_peer, node_id: "aa", info_hash: "bb", token: "aoeusnth", tid: "cc", port: 2342)
"d1:ad2:id2:aa9:info_hash2:bb4:porti2342e5:token8:aoeusnthe1:q13:announce_peer1:t2:cc1:y1:qe"

Decoding

iex> KRPCProtocol.decode("d1:ad2:id20:aaaaaaaaaaaaaaaaaaaae1:q4:ping1:t2:aa1:y1:qe")
{:ping, %{node_id: "aaaaaaaaaaaaaaaaaaaa", tid: "aa"}}

Errors

The decode function returns {:invalid, error_msg} if an error occurs during the decoding process such as an unknown message type or invalid bencoded payload.

iex> KRPCProtocol.decode("abc")
{:invalid, "Invalid becoded payload: \"abc\""}

About

KRPCProtocol is an elixir package for decoding and encoding mainline DHT messages.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Elixir 100.0%