Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alehander92 committed Dec 30, 2014
0 parents commit 9118bd9
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/_build
/deps
erl_crash.dump
*.ez
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: erlang
otp_release:
- 17.0
before_install:
- git clone https://github.com/elixir-lang/elixir
- cd elixir
- git checkout v1.0.0
- cd ..
- make -C elixir
before_script:
- export PATH="`pwd`/elixir/bin:$PATH"
- mix local.hex --force
script: "MIX_ENV=test mix do deps.get, test"
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2014 Alexander Ivanov

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Wire
====

[![Build Status](https://travis-ci.org/alehander42/wire.svg)](https://travis-ci.org/alehander42/wire/)

Wire is an elixir package for decoding and encoding
bittorrent peer wire protocol messages.

A message is represented in the library as a keyword list with
`:type`: the type of the message (`:keep_alive`, :`not_interested`, etc)
and the other fields of the message, e.g.

```elixir
h = [type: :have, piece_index: 4]
```

```elixir

Wire.encode [type: :interested] # <<0, 0, 0, 1, 2>>
Wire.encode [type: :bitfield, field: <<0, 4>>] # <<0, 0, 0, 3, 5, 0, 4>>
```

`decode_messages` decodes a binary containing 0 or more messages and
returns a list of messages and the remaining bytes

```elixir


Wire.decode_messages(<<0, 0, 0, 6, 5, 0, 2, 4, 3, 1>>)
# {[[type: :bitfield, field: <<0, 2, 4, 3, 1>>]], <<>>}
Wire.decode_messages(<< 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0xf, 5 >>)
# {[[type: :keep_alive], [type: :interested]], <<0, 0, 0, 0xf, 5>>})


```

24 changes: 24 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config

# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for third-
# party users, it should be done in your mix.exs file.

# Sample configuration:
#
# config :logger, :console,
# level: :info,
# format: "$date $time [$level] $metadata$message\n",
# metadata: [:user_id]

# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
7 changes: 7 additions & 0 deletions lib/wire.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Wire do
defdelegate encode(message), to: Wire.Encoder
defdelegate decode_messages(message), to: Wire.Decoder
defdelegate decode_message(message), to: Wire.Decoder
end


77 changes: 77 additions & 0 deletions lib/wire/decoder.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
defmodule Wire.Decoder do

@doc ~S"""
Parses a binary containing 0 or more messages and
returns a list with messages and the unparsed part of the message
"""
@spec decode_messages(binary) :: {List.Keyword.t, binary}
def decode_messages(binary) do
<< l :: 32-integer-big-unsigned, rest :: binary >> = binary
cond do
byte_size(rest) < l ->
{[], binary}
true ->
decode_messages(binary, [])
end
end

def decode_messages(s, acc) do
<< l :: 32-integer-big-unsigned, rest :: binary >> = s
if byte_size(rest) < l do
{ acc, s }
else
{ message, rest } = decode_message(s)
if byte_size(rest) > 0 do
decode_messages(rest, acc ++ [message])
else
{ acc ++ [message], rest }
end
end
end

def decode_message(message) do
<< l :: 32-integer-big-unsigned, rest :: binary >> = message

if l == 0 do
{[type: :keep_alive], rest}
else
<< id, rest :: binary >> = rest

case id do
9 ->
<< port :: 16-integer-big-unsigned, rest :: binary >> = rest
{[type: :port, listen_port: port], rest}
7 ->
l2 = l - 9
<< index :: 32-integer-big-unsigned, begin :: 32-integer-big-unsigned,
block :: binary-size(l2), rest :: binary >> = rest
{[type: :piece, index: index, begin: begin, block: block], rest}
id when id in [8, 6] ->
<< index :: 32-integer-big-unsigned,
begin :: 32-integer-big-unsigned,
length :: 32-integer-big-unsigned,
rest :: binary >> = rest
type = if id == 8 do :cancel else :request end
{[type: type, index: index, begin: begin, length: length], rest}

5 ->
l2 = l - 1
<< field :: binary-size(l2), rest :: binary >> = rest
{[type: :bitfield, field: field], rest}
4 ->
<< piece_index :: 32-integer-big-unsigned, rest :: binary >> = rest
{[type: :have, piece_index: piece_index], rest}
3 ->
{[type: :not_interested], rest}
2 ->
{[type: :interested], rest}
1 ->
{[type: :unchoke], rest}
0 ->
{[type: :choke], rest}

end
end
end
end
47 changes: 47 additions & 0 deletions lib/wire/encoder.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
defmodule Wire.Encoder do
def encode(type: :port, listen_port: listen_port) do
<< 0, 0, 0, 3, listen_port :: 16-integer-big-unsigned, 0 >>
end

def encode(type: request_or_cancel, index: index, begin: begin, length: length) when request_or_cancel in [:cancel, :request] do
a = if request_or_cancel == :cancel do 8 else 6 end

<< 0, 0, 0, 0xd, a,
index :: 32-integer-big-unsigned,
begin :: 32-integer-big-unsigned,
length :: 32-integer-big-unsigned >>
end

def encode(type: :piece, index: index, begin: begin, block: block) do
<< (9 + byte_size(block)) :: 32-integer-big-unsigned, 8, index :: 32-integer-big-unsigned, begin :: 32-integer-big-unsigned,
block :: binary >>
end

def encode(type: :bitfield, field: field) do
<< (1 + byte_size(field)) :: 32-integer-big-unsigned, 5, field :: binary>>
end

def encode(type: :have, piece_index: piece_index) do
<< 0, 0, 0, 5, 4, piece_index :: 32-integer-big-unsigned >>
end

def encode(type: :not_interested) do
<< 0, 0, 0, 1, 3 >>
end

def encode(type: :interested) do
<< 0, 0, 0, 1, 2 >>
end

def encode(type: :unchoke) do
<< 0, 0, 0, 1, 1 >>
end

def encode(type: :choke) do
<< 0, 0, 0, 1, 0 >>
end

def encode(type: :keep_alive) do
<< 0, 0, 0, 0 >>
end
end
26 changes: 26 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Wire.Mixfile do
use Mix.Project

def project do
[app: :wire,
version: "0.0.8",
elixir: "~> 1.0.0",
description: "Encode and decode bittorrent peer wire protocol messages",
package: package,
deps: deps]
end

defp package do
[ contributors: ["alehander42"],
licenses: ["MIT"],
links: %{"Github" => "https://github.com/alehander42/wire"}]
end

def application do
[applications: [:logger]]
end

defp deps do
[]
end
end
47 changes: 47 additions & 0 deletions test/decoder_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Code.require_file "test_helper.exs", __DIR__

defmodule WireDecoderTest do
use ExUnit.Case, async: True

import Wire.Decoder, only: [decode_message: 1, decode_messages: 1, decode_messages: 2]

test "parses keep_alive correctly" do
assert decode_message(<< 0, 0, 0, 0 >>) == {[type: :keep_alive], <<>>}
end

test "parses have correctly" do
assert decode_message(<< 0, 0, 0, 5, 4, 0, 0, 0, 96, 0, 96, 3, 5>>) == {[type: :have, piece_index: 96], <<0, 96, 3, 5>>}
end

test "parses interested correctly" do
assert decode_message(<< 0, 0, 0, 1, 2 >>) == {[type: :interested], <<>>}
end

test "parses bitfield correctly" do
assert decode_message(<< 0, 0, 0, 6, 5, 0, 2, 4, 0, 2 >>) ==
{[type: :bitfield, field: <<0, 2, 4, 0, 2>>], <<>>}
end

test "parses unchoke messages" do
assert decode_messages(<< 0, 0, 0, 1, 1 >>) == {[[type: :unchoke]], <<>>}
end

test "parses several messages" do
assert decode_messages(<< 0, 0, 0, 0, 0, 0, 0, 1, 2 >>) ==
{[[type: :keep_alive], [type: :interested]], <<>>}
end

test "parses a part of a message as rest" do
assert decode_messages(<< 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0xf, 5 >>) ==
{[[type: :keep_alive], [type: :interested]], <<0, 0, 0, 0xf, 5>>}
assert decode_messages(<< 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 >>) ==
{[[type: :keep_alive], [type: :keep_alive]], <<0, 0, 0, 1>>}
end

test "parses several messages with acc" do
assert decode_messages(<< 0, 0, 0, 0, 0, 0, 0, 1>>, []) ==
{[[type: :keep_alive]], <<0, 0, 0, 1>>}
assert decode_messages(<< 0, 0, 0, 1, 2, 0, 0, 0, 5>>, [[type: :keep_alive]]) ==
{[[type: :keep_alive], [type: :interested]], <<0, 0, 0, 5>>}
end
end
37 changes: 37 additions & 0 deletions test/encode_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Code.require_file "test_helper.exs", __DIR__

defmodule WireEncoderTest do
use ExUnit.Case, async: True

import Wire.Encoder, only: [encode: 1]

test "converts port correctly" do
assert encode(type: :port, listen_port: 80) ==
<< 0, 0, 0, 3, 0, 80, 0 >>
end

test "converts cancel correctly" do
assert encode(type: :cancel, index: 2, begin: 0, length: 4) ==
<< 0, 0, 0, 0xd, 8, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 4 >>
end

test "converts request correctly" do
assert encode(type: :request, index: 2, begin: 0, length: 4) ==
<< 0, 0, 0, 0xd, 6, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 4 >>
end

test "converts bitfield correctly" do
assert encode(type: :bitfield, field: << 0 >>) ==
<< 0, 0, 0, 2, 5, 0 >>
end

test "converts have correctly" do
assert encode(type: :have, piece_index: 22) ==
<< 0, 0, 0, 5, 4, 0, 0, 0, 22 >>
end

test "converts interested correctly" do
assert encode(type: :interested) == << 0, 0, 0, 1, 2 >>
end
end

1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit 9118bd9

Please sign in to comment.