Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Whitespace...

  • Loading branch information...
commit 88febcc825c9f57eec04e33685c926699fb71560 1 parent f6937bb
@jlouis authored
View
86 apps/etorrent/src/etorrent_acceptor.erl
@@ -48,8 +48,8 @@ handle_cast(_Msg, State) ->
handle_info(timeout, #state { our_peer_id = PeerId } = S) ->
case gen_tcp:accept(S#state.listen_socket) of
{ok, Socket} ->
- {ok, _Pid} = etorrent_listen_sup:start_child(),
- handshake(Socket, PeerId);
+ {ok, _Pid} = etorrent_listen_sup:start_child(),
+ handshake(Socket, PeerId);
{error, closed} -> ok;
{error, econnaborted} -> ok;
{error, enotconn} -> ok;
@@ -72,49 +72,49 @@ handshake(Socket, PeerId) ->
%% if the check fails. Finally, control is handed over to the control
%% pid or we handle an error by closing down.
try
- {ok, {IP, Port}} = inet:peername(Socket),
- {ok, Caps, InfoHash, HisPeerId} = receive_handshake(Socket),
- ok = check_infohash(InfoHash),
- ok = check_peer(IP, Port, InfoHash, HisPeerId, PeerId),
- ok = check_peer_count(),
- {ok, RecvPid, ControlPid} =
- check_torrent_state(Socket, Caps, IP, Port, InfoHash, PeerId),
- ok = handover_control(Socket, RecvPid, ControlPid)
+ {ok, {IP, Port}} = inet:peername(Socket),
+ {ok, Caps, InfoHash, HisPeerId} = receive_handshake(Socket),
+ ok = check_infohash(InfoHash),
+ ok = check_peer(IP, Port, InfoHash, HisPeerId, PeerId),
+ ok = check_peer_count(),
+ {ok, RecvPid, ControlPid} =
+ check_torrent_state(Socket, Caps, IP, Port, InfoHash, PeerId),
+ ok = handover_control(Socket, RecvPid, ControlPid)
catch
error:{badmatch, {error, enotconn}} ->
ok;
- throw:{error, _Reason} ->
- gen_tcp:close(Socket),
- ok;
- throw:{bad_peer, HisPId} ->
- ?INFO([peer_id_is_bad, HisPId]),
- gen_tcp:close(Socket),
- ok
+ throw:{error, _Reason} ->
+ gen_tcp:close(Socket),
+ ok;
+ throw:{bad_peer, HisPId} ->
+ ?INFO([peer_id_is_bad, HisPId]),
+ gen_tcp:close(Socket),
+ ok
end.
receive_handshake(Socket) ->
case etorrent_proto_wire:receive_handshake(Socket) of
{ok, Caps, InfoHash, HisPeerId} ->
- {ok, Caps, InfoHash, HisPeerId};
- {error, Reason} ->
- throw({error, Reason})
+ {ok, Caps, InfoHash, HisPeerId};
+ {error, Reason} ->
+ throw({error, Reason})
end.
check_infohash(InfoHash) ->
case etorrent_table:get_torrent({infohash, InfoHash}) of
- {value, _} ->
- ok;
- not_found ->
- throw({error, infohash_not_found})
+ {value, _} ->
+ ok;
+ not_found ->
+ throw({error, infohash_not_found})
end.
handover_control(Socket, RPid, CPid) ->
case gen_tcp:controlling_process(Socket, RPid) of
- ok -> etorrent_peer_control:initialize(CPid, incoming),
- ok;
- {error, enotconn} ->
- etorrent_peer_control:stop(CPid),
- throw({error, enotconn})
+ ok -> etorrent_peer_control:initialize(CPid, incoming),
+ ok;
+ {error, enotconn} ->
+ etorrent_peer_control:stop(CPid),
+ throw({error, enotconn})
end.
check_peer(_IP, _Port, _InfoHash, PeerId, PeerId) ->
@@ -123,27 +123,27 @@ check_peer(IP, Port, InfoHash, HisPeerId, _OurPeerId) ->
{value, PL} = etorrent_table:get_torrent({infohash, InfoHash}),
case etorrent_peer_mgr:is_bad_peer(IP, Port) of
true ->
- throw({bad_peer, HisPeerId});
+ throw({bad_peer, HisPeerId});
false ->
- ok
+ ok
end,
case etorrent_table:connected_peer(IP, Port, proplists:get_value(id, PL)) of
- true -> throw({error, already_connected});
- false -> ok
+ true -> throw({error, already_connected});
+ false -> ok
end.
check_torrent_state(Socket, Caps, IP, Port, InfoHash, OurPeerId) ->
{value, PL} = etorrent_table:get_torrent({infohash, InfoHash}),
case proplists:get_value(state, PL) of
- started ->
- etorrent_peer_pool:start_child(
- OurPeerId,
- InfoHash,
- proplists:get_value(id, PL),
- {IP, Port},
- Caps,
- Socket);
- _ -> throw({error, not_ready_for_connections})
+ started ->
+ etorrent_peer_pool:start_child(
+ OurPeerId,
+ InfoHash,
+ proplists:get_value(id, PL),
+ {IP, Port},
+ Caps,
+ Socket);
+ _ -> throw({error, not_ready_for_connections})
end.
check_peer_count() ->
@@ -151,5 +151,3 @@ check_peer_count() ->
{value, 0} -> throw({error, already_enough_connections});
{value, K} when is_integer(K) -> ok
end.
-
-
View
3  apps/etorrent/src/etorrent_allowed_fast.erl
@@ -23,7 +23,8 @@
%% access to always when it wants to request.</p>
%% @end
-type ip() :: {integer(), integer(), integer(), integer()}.
--spec allowed_fast(integer(), ip() | binary(), integer(), binary()) -> {value, set()}.
+-spec allowed_fast(integer(), ip() | binary(), integer(), binary()) ->
+ {value, set()}.
allowed_fast(Sz, {B1, B2, B3, B4}, K, InfoHash) ->
B = <<B1:8/integer, B2:8/integer, B3:8/integer, B4:8/integer>>,
allowed_fast(Sz, B, K, InfoHash);
View
11 apps/etorrent/src/etorrent_app.erl
@@ -87,11 +87,12 @@ stop(_State) ->
start_webui() ->
Dispatch = [ {'_', [{'_', etorrent_cowboy_handler, []}]} ],
- {ok, _Pid} = cowboy:start_listener(http, 10,
- cowboy_tcp_transport, [{port, 8080}],
- cowboy_http_protocol, [{dispatch, Dispatch}]
- ).
-
+ {ok, _Pid} =
+ cowboy:start_listener(http, 10,
+ cowboy_tcp_transport, [{port, 8080}],
+ cowboy_http_protocol, [{dispatch, Dispatch}]
+ ).
+
%% @doc Generate a random peer id for use
%% @end
generate_peer_id() ->
View
16 apps/etorrent/src/etorrent_bcoding.erl
@@ -29,7 +29,7 @@
% Retrieval
-export([get_value/2, get_value/3, get_info_value/2, get_info_value/3,
get_binary_value/2, get_binary_value/3,
- get_string_value/2, get_string_value/3]).
+ get_string_value/2, get_string_value/3]).
-type bcode() :: etorrent_types:bcode().
@@ -110,8 +110,8 @@ get_binary_value(Key, PL) ->
%% @end
get_binary_value(Key, PL, Default) ->
case get_value(Key, PL) of
- undefined -> Default;
- B when is_binary(B) -> B
+ undefined -> Default;
+ B when is_binary(B) -> B
end.
%% @doc Read a string Value indexed by Key from the PL dict
@@ -123,8 +123,8 @@ get_string_value(Key, PL) -> binary_to_list(get_binary_value(Key, PL)).
%% @end
get_string_value(Key, PL, Default) ->
case get_value(Key, PL) of
- undefined -> Default;
- V when is_binary(V) -> binary_to_list(V)
+ undefined -> Default;
+ V when is_binary(V) -> binary_to_list(V)
end.
%% @doc Parse a file into a Torrent structure.
@@ -198,9 +198,9 @@ decode_dict_items(String, Accum) ->
prop_inv() ->
?FORALL(BC, bcode(),
begin
- Enc = iolist_to_binary(encode(BC)),
- {ok, Dec} = decode(Enc),
- encode(BC) =:= encode(Dec)
+ Enc = iolist_to_binary(encode(BC)),
+ {ok, Dec} = decode(Enc),
+ encode(BC) =:= encode(Dec)
end).
eqc_test() ->
View
257 apps/etorrent/src/etorrent_tracker_communication.erl
@@ -47,16 +47,16 @@
%% information. The `ControlPid' refers to the Pid of the controller
%% process. The `UrlTiers' are a list of lists of string() parameters,
%% each a URL. Next comes the `Infohash' as a binary(), the `PeerId'
-%% parameter and finally the `TorrentId': the identifier of the torrent.</p>
+%% parameter and finally the `TorrentId': the identifier of the torrent.</p>
%% @end
%% @todo What module, precisely do the control pid come from?
-spec start_link(pid(), [tier()], binary(), binary(), integer()) ->
- ignore | {ok, pid()} | {error, term()}.
+ ignore | {ok, pid()} | {error, term()}.
start_link(ControlPid, UrlTiers, InfoHash, PeerId, TorrentId)
when is_binary(PeerId) ->
gen_server:start_link(?MODULE,
[ControlPid,
- UrlTiers, InfoHash, PeerId, TorrentId],
+ UrlTiers, InfoHash, PeerId, TorrentId],
[]).
%% @doc Prod the tracker and tell it we completed to torrent
@@ -72,9 +72,10 @@ init([ControlPid, UrlTiers, InfoHash, PeerId, TorrentId]) ->
process_flag(trap_exit, true),
random:seed(now()),
HardRef = erlang:send_after(0, self(), hard_timeout),
- SoftRef = erlang:send_after(timer:seconds(?DEFAULT_CONNECTION_TIMEOUT_INTERVAL),
- self(),
- soft_timeout),
+ SoftRef = erlang:send_after(
+ timer:seconds(?DEFAULT_CONNECTION_TIMEOUT_INTERVAL),
+ self(),
+ soft_timeout),
Url = swap_urls(shuffle_tiers(UrlTiers)),
{ok, #state{control_pid = ControlPid,
torrent_id = TorrentId,
@@ -105,7 +106,7 @@ handle_cast(Msg, S) ->
%% @private
handle_info(hard_timeout,
- #state { queued_message = none } = S) ->
+ #state { queued_message = none } = S) ->
%% There is nothing to do with the hard_timer, just ignore this
{noreply, S#state { hard_timer = none }};
handle_info(hard_timeout, S) ->
@@ -139,10 +140,10 @@ contact_tracker(S) ->
contact_tracker(Event, #state { url = Tiers } = S) ->
case contact_tracker(Tiers, Event, S) of
- {none, NS} ->
- NS;
- {ok, NS} ->
- NS
+ {none, NS} ->
+ NS;
+ {ok, NS} ->
+ NS
end.
contact_tracker(Tiers, Event, S) ->
@@ -152,10 +153,10 @@ contact_tracker([], _Event, S, _Acc) ->
{none, handle_timeout(S)};
contact_tracker([Tier | NextTier], Event, S, Acc) ->
case contact_tracker_tier(Tier, Event, S) of
- {ok, NS, NewTier} ->
- {ok, NS#state { url = lists:reverse(Acc) ++ [NewTier | NextTier] }};
- none ->
- contact_tracker(NextTier, Event, S, [Tier | Acc])
+ {ok, NS, NewTier} ->
+ {ok, NS#state { url = lists:reverse(Acc) ++ [NewTier | NextTier] }};
+ none ->
+ contact_tracker(NextTier, Event, S, [Tier | Acc])
end.
contact_tracker_tier(Tier, S, Event) ->
@@ -165,35 +166,35 @@ contact_tracker_tier([], _Event, _S, _Acc) ->
none;
contact_tracker_tier([Url | Next], Event, S, Acc) ->
case
- case identify_url_type(Url) of
- http -> contact_tracker_http(Url, Event, S);
- {udp, IP, Port} -> contact_tracker_udp(Url, IP, Port, Event, S)
- end
+ case identify_url_type(Url) of
+ http -> contact_tracker_http(Url, Event, S);
+ {udp, IP, Port} -> contact_tracker_udp(Url, IP, Port, Event, S)
+ end
of
- {ok, NS} ->
- {ok, NS, [Url] ++ lists:reverse(Acc) ++ Next};
- error ->
- %% For private torrent (BEP 27), disconnect all peers coming
- %% from the tracker before switching to another one
- case etorrent_torrent:is_private(S#state.torrent_id) of
- true -> disconnect_tracker(Url);
- _ -> ok
- end,
- contact_tracker_tier(Next, Event, S, [Url | Acc])
+ {ok, NS} ->
+ {ok, NS, [Url] ++ lists:reverse(Acc) ++ Next};
+ error ->
+ %% For private torrent (BEP 27), disconnect all peers coming
+ %% from the tracker before switching to another one
+ case etorrent_torrent:is_private(S#state.torrent_id) of
+ true -> disconnect_tracker(Url);
+ _ -> ok
+ end,
+ contact_tracker_tier(Next, Event, S, [Url | Acc])
end.
identify_url_type(Url) ->
case etorrent_http_uri:parse(Url) of
- {S1, _UserInfo, Host, Port, _Path, _Query} ->
- case S1 of
- http ->
- http;
- udp ->
- {udp, Host, Port}
- end;
- {error, Reason} ->
- ?WARN([Reason, Url]),
- exit(identify_url_type)
+ {S1, _UserInfo, Host, Port, _Path, _Query} ->
+ case S1 of
+ http ->
+ http;
+ udp ->
+ {udp, Host, Port}
+ end;
+ {error, Reason} ->
+ ?WARN([Reason, Url]),
+ exit(identify_url_type)
end.
@@ -205,73 +206,76 @@ identify_url_type(Url) ->
-spec disconnect_tracker(string()) -> ok.
disconnect_tracker(Url) ->
F = fun(P) ->
- etorrent_peer_control:stop(P)
- end,
+ etorrent_peer_control:stop(P)
+ end,
etorrent_table:foreach_peer_of_tracker(Url, F),
ok.
contact_tracker_udp(Url, IP, Port, Event,
- #state { torrent_id = Id,
- info_hash = InfoHash,
- peer_id = PeerId } = S) ->
+ #state { torrent_id = Id,
+ info_hash = InfoHash,
+ peer_id = PeerId } = S) ->
{value, PL} = etorrent_torrent:lookup(Id),
Uploaded = proplists:get_value(uploaded, PL),
Downloaded = proplists:get_value(downloaded, PL),
Left = proplists:get_value(left, PL),
PropList = [{info_hash, InfoHash},
- {peer_id, PeerId},
- {up, Uploaded},
- {down, Downloaded},
- {left, Left},
- {port, Port},
- {key, 0}, %% @todo: Actually process the key correctly for private tracking
- {event, Event}],
+ {peer_id, PeerId},
+ {up, Uploaded},
+ {down, Downloaded},
+ {left, Left},
+ {port, Port},
+ %% @todo: Actually process the key correctly for
+ %% private tracking
+ {key, 0},
+ {event, Event}],
?INFO([announcing_via_udp]),
- case etorrent_udp_tracker_mgr:announce({IP, Port}, PropList, timer:seconds(60)) of
- {ok, {announce, Peers, Status}} ->
- ?INFO([udp_reply_handled]),
- {I, MI} = handle_udp_response(Url, Id, Peers, Status),
- {ok, handle_timeout(I, MI, S)};
- timeout ->
- error
+ case etorrent_udp_tracker_mgr:announce(
+ {IP, Port}, PropList, timer:seconds(60)) of
+ {ok, {announce, Peers, Status}} ->
+ ?INFO([udp_reply_handled]),
+ {I, MI} = handle_udp_response(Url, Id, Peers, Status),
+ {ok, handle_timeout(I, MI, S)};
+ timeout ->
+ error
end.
%% @todo: consider not passing around the state here!
contact_tracker_http(Url, Event, S) ->
RequestUrl = build_tracker_url(Url, Event, S),
case etorrent_http:request(RequestUrl) of
- {ok, {{200, _}, _, Body}} ->
- case etorrent_bcoding:decode(Body) of
- {ok, BC} -> {ok, handle_tracker_response(Url, BC, S)};
- {error, _} ->
- etorrent_event:notify({malformed_tracker_response, Body}),
+ {ok, {{200, _}, _, Body}} ->
+ case etorrent_bcoding:decode(Body) of
+ {ok, BC} -> {ok, handle_tracker_response(Url, BC, S)};
+ {error, _} ->
+ etorrent_event:notify({malformed_tracker_response, Body}),
+ error
+ end;
+ {error, Type} ->
+ case Type of
+ etimedout -> ignore;
+ econnrefused -> ignore;
+ session_remotly_closed -> ignore;
+ Err ->
+ Msg = {error, [{contact_tracker, Err},
+ {id, S#state.torrent_id}]},
+ etorrent_event:notify(Msg),
+ ?INFO([Msg]),
+ ignore
+ end,
error
- end;
- {error, Type} ->
- case Type of
- etimedout -> ignore;
- econnrefused -> ignore;
- session_remotly_closed -> ignore;
- Err ->
- Msg = {error, [{contact_tracker, Err},
- {id, S#state.torrent_id}]},
- etorrent_event:notify(Msg),
- ?INFO([Msg]),
- ignore
- end,
- error
end.
handle_tracker_response(Url, BC, S) ->
case etorrent_bcoding:get_string_value("failure reason", BC, none) of
- none ->
- report_warning(
- S#state.torrent_id,
- etorrent_bcoding:get_string_value("warning message", BC, none)),
- handle_tracker_bcoding(Url, BC, S);
- Err ->
- etorrent_event:notify({tracker_error, S#state.torrent_id, Err}),
- handle_timeout(BC, S)
+ none ->
+ report_warning(
+ S#state.torrent_id,
+ etorrent_bcoding:get_string_value("warning message", BC, none)),
+ handle_tracker_bcoding(Url, BC, S);
+ Err ->
+ etorrent_event:notify({tracker_error, S#state.torrent_id, Err}),
+ handle_timeout(BC, S)
end.
report_warning(_Id, none) -> ok;
@@ -285,20 +289,21 @@ handle_tracker_bcoding(Url, BC, S) ->
response_ips(BC)),
%% Update the state of the torrent
ok = etorrent_torrent:statechange(
- S#state.torrent_id,
- [{tracker_report,
- etorrent_bcoding:get_value("complete", BC, 0),
- etorrent_bcoding:get_value("incomplete", BC, 0)}]),
+ S#state.torrent_id,
+ [{tracker_report,
+ etorrent_bcoding:get_value("complete", BC, 0),
+ etorrent_bcoding:get_value("incomplete", BC, 0)}]),
%% Timeout
handle_timeout(BC, S).
handle_udp_response(Url, Id, Peers, Status) ->
etorrent_peer_mgr:add_peers(Url, Id, Peers),
etorrent_torrent:statechange(Id,
- [{tracker_report,
- proplists:get_value(seeders, Status, 0),
- proplists:get_value(leechers, Status, 0)}]),
- {proplists:get_value(interval, Status), ?DEFAULT_CONNECTION_TIMEOUT_MIN_INTERVAL}.
+ [{tracker_report,
+ proplists:get_value(seeders, Status, 0),
+ proplists:get_value(leechers, Status, 0)}]),
+ {proplists:get_value(interval, Status),
+ ?DEFAULT_CONNECTION_TIMEOUT_MIN_INTERVAL}.
handle_timeout(S) ->
Interval = ?DEFAULT_CONNECTION_TIMEOUT_INTERVAL,
@@ -306,7 +311,8 @@ handle_timeout(S) ->
handle_timeout(Interval, MinInterval, S).
handle_timeout(BC, S) ->
- Interval = etorrent_bcoding:get_value("interval", BC, ?DEFAULT_REQUEST_TIMEOUT),
+ Interval =
+ etorrent_bcoding:get_value("interval", BC, ?DEFAULT_REQUEST_TIMEOUT),
MinInterval = etorrent_bcoding:get_value("min interval", BC, none),
handle_timeout(Interval, MinInterval, S).
@@ -325,9 +331,9 @@ cancel_timer(none) -> ok;
cancel_timer(TRef) -> erlang:cancel_timer(TRef).
build_tracker_url(Url, Event,
- #state { torrent_id = Id,
- info_hash = InfoHash,
- peer_id = PeerId }) ->
+ #state { torrent_id = Id,
+ info_hash = InfoHash,
+ peer_id = PeerId }) ->
{value, PL} = etorrent_torrent:lookup(Id),
Uploaded = proplists:get_value(uploaded, PL),
Downloaded = proplists:get_value(downloaded, PL),
@@ -349,24 +355,22 @@ build_tracker_url(Url, Event,
completed -> [{"event", "completed"} | Request]
end,
- % Url can already has `?'.
- % For example,
- % /announce.php?passkey=43c08a5dd9e70a19f62adfd0ad76dw04
- FlatUrl = lists:flatten(Url),
- Delim = case lists:member($?, FlatUrl) of
- true -> "&";
- false -> "?"
- end,
-
- lists:concat([Url, Delim, etorrent_http:mk_header(EReq)]).
-
+ %% Url can already has `?'.
+ %% For example,
+ %% /announce.php?passkey=43c08a5dd9e70a19f62adfd0ad76dw04
+ FlatUrl = lists:flatten(Url),
+ Delim = case lists:member($?, FlatUrl) of
+ true -> "&";
+ false -> "?"
+ end,
+ lists:concat([Url, Delim, etorrent_http:mk_header(EReq)]).
%%% Tracker response lookup functions
response_ips(BC) ->
case etorrent_bcoding:get_value("peers", BC, none) of
- none -> [];
- IPs -> etorrent_utils:decode_ips(IPs)
+ none -> [];
+ IPs -> etorrent_utils:decode_ips(IPs)
end.
@@ -396,14 +400,15 @@ swap([Url | R]) ->
swap_in_tier(Url, [], Acc) -> {Url, lists:reverse(Acc)};
swap_in_tier(Url, [H | T], Acc) ->
case should_swap_for(Url, H) of
- true ->
- {H, lists:reverse(Acc) ++ [Url | T]};
- false ->
- swap_in_tier(Url, T, [H | Acc])
+ true ->
+ {H, lists:reverse(Acc) ++ [Url | T]};
+ false ->
+ swap_in_tier(Url, T, [H | Acc])
end.
should_swap_for(Url1, Url2) ->
- {S1, _UserInfo, Host1, _Port, _Path, _Query} = etorrent_http_uri:parse(Url1),
+ {S1, _UserInfo, Host1, _Port, _Path, _Query} =
+ etorrent_http_uri:parse(Url1),
{_S2, _, Host2, _, _, _} = etorrent_http_uri:parse(Url2),
Host1 == Host2 andalso S1 == http.
@@ -422,19 +427,21 @@ splice_test() ->
swap_test() ->
Swapped = swap(lists:concat(tier())),
?assertEqual(["udp://one.com", "udp://two.com", "http://one.com",
- "udp://four.com", "http://two.com", "http://three.com"],
- Swapped).
+ "udp://four.com", "http://two.com", "http://three.com"],
+ Swapped).
swap_urls_test() ->
Swapped = swap_urls(tier()),
?assertEqual([["udp://one.com", "udp://two.com", "http://one.com"],
- ["udp://four.com", "http://two.com", "http://three.com"]],
- Swapped).
+ ["udp://four.com", "http://two.com", "http://three.com"]],
+ Swapped).
should_swap_test() ->
?assertEqual(true, should_swap_for("http://foo.com", "udp://foo.com")),
?assertEqual(false, should_swap_for("http://foo.com", "udp://bar.com")),
- ?assertEqual(true, should_swap_for("http://foo.com", "udp://foo.com/something/more")).
+ ?assertEqual(true,
+ should_swap_for("http://foo.com",
+ "udp://foo.com/something/more")).
-ifdef(PROPER).
@@ -444,17 +451,17 @@ host() -> oneof(["one.com", "two.com", "three.com", "four.com", "five.com"]).
url() ->
?LET({Scheme, Host}, {scheme(), host()},
- atom_to_list(Scheme) ++ "://" ++ Host).
+ atom_to_list(Scheme) ++ "://" ++ Host).
g_tier() -> list(url()).
g_tiers() -> list(g_tier()).
prop_splice_unsplice_inv() ->
?FORALL(In, g_tiers(),
- begin
- {K, Spliced} = splice(In),
- In =:= unsplice(K, Spliced)
- end).
+ begin
+ {K, Spliced} = splice(In),
+ In =:= unsplice(K, Spliced)
+ end).
eqc_test() ->
?assert(proper:quickcheck(prop_splice_unsplice_inv())).
Please sign in to comment.
Something went wrong with that request. Please try again.