Permalink
Browse files

Add listen_ip() as an optional parameter.

  • Loading branch information...
1 parent cda58ee commit fad124dec283a68f2de90ed08eec71164834d652 @arcusfelis arcusfelis committed Feb 15, 2013
View
@@ -1,7 +1,6 @@
{etorrent_common_config, [{dirwatch_interval, 20 },
{use_upnp, false},
- {dht, false },
- {dht_port, 6882 },
+ {dht, true },
{max_peers, 200},
{max_download_rate, 2000},
{max_upload_rate, 2000},
View
@@ -16,6 +16,7 @@
download_dir/0,
fast_resume_file/0,
listen_port/0,
+ listen_ip/0,
logger_dir/0,
logger_file/0,
log_settings/0,
@@ -63,6 +64,7 @@ configuration_specification() ->
required(port),
required(logger_dir),
required(logger_fname),
+ optional(listen_ip, all),
optional(dht_port, 6882),
optional(dht_state, "etorrent_dht_state"),
optional(log_settings, [])].
@@ -142,6 +144,9 @@ max_download_rate() -> call(max_download_rate).
-spec listen_port() -> pos_integer().
listen_port() -> call(port).
+-spec listen_ip() -> inet:ip_address().
+listen_ip() -> call(listen_ip).
+
-spec logger_dir() -> file_path().
logger_dir() -> call(logger_dir).
View
@@ -13,14 +13,17 @@
-export([start_link/1,
start/1, start/2, stop/1,
- check/1, pause/1, continue/1]).
+ check/1, pause/1, continue/1,
+ local_peer_id/0]).
-export([handle_cast/2, handle_call/3, init/1, terminate/2]).
-export([handle_info/2, code_change/3]).
-define(SERVER, ?MODULE).
-type bcode() :: etorrent_types:bcode().
+-type peerid() :: <<_:160>>.
+
-record(state, {local_peer_id :: binary() }).
%% API
@@ -71,6 +74,16 @@ continue(Id) ->
stop(File) ->
gen_server:cast(?SERVER, {stop, File}).
+%% @doc Get a local peer id.
+%%
+%% Most of the code don't need this function, because the peer id is usually
+%% passed as a parameter of the `start_link' function.
+%%
+%% This function can be used for debugging and inside tests.
+-spec local_peer_id() -> peerid().
+local_peer_id() ->
+ gen_server:call(?SERVER, local_peer_id).
+
%% =======================================================================
%% @private
@@ -124,8 +137,8 @@ handle_call({start, F, CallBack}, _From, S) ->
handle_call(stop_all, _From, S) ->
stop_all(),
{reply, ok, S};
-handle_call(_A, _B, S) ->
- {noreply, S}.
+handle_call(local_peer_id, _From, S) ->
+ {reply, S#state.local_peer_id, S}.
%% @private
handle_info(Info, State) ->
View
@@ -529,6 +529,7 @@ handle_query('get_peers', Params, IP, Port, MsgID, Self, Tokens) ->
handle_query('announce', Params, IP, Port, MsgID, Self, Tokens) ->
InfoHash = etorrent_dht:integer_id(etorrent_bcoding:get_value(<<"info_hash">>, Params)),
+ lager:info("Announce ~s from ~p:~p~n", [integer_hash_to_literal(InfoHash), IP, Port]),
BTPort = etorrent_bcoding:get_value(<<"port">>, Params),
Token = get_string(<<"token">>, Params),
case is_valid_token(Token, IP, Port, Tokens) of
@@ -898,3 +899,7 @@ qc(Gen) ->
-endif. %% EQC
-endif.
+
+
+integer_hash_to_literal(InfoHashInt) when is_integer(InfoHashInt) ->
+ io:format("~40.16.0B", [InfoHashInt]).
View
@@ -107,7 +107,7 @@ base_path(Infohash) ->
filename:join([etorrent_config:dotdir(), Infohash]).
-%% @doc Get the info hash of a torrent file.
+%% @doc Get the literal info hash of a torrent file.
%% @end
-spec info_hash(Torrentfile::string()) -> {ok, [byte()]} | {error, _}.
info_hash(Torrentfile) ->
View
@@ -30,7 +30,9 @@ request(URL) ->
{<<"Host">>, decode_host(URL)},
{<<"Accept">>, "*/*"},
{<<"Accept-Encoding">>, "gzip, identity"}],
- Options = [{pool, default}, {recv_timeout, 15000}],
+ Ip = etorrent_config:listen_ip(),
+ Options = [{pool, default}, {recv_timeout, 15000},
+ {connect_options, case Ip of all -> []; _ -> [{ip, Ip}] end}],
case hackney:request(get, URL, Headers, <<>>, Options) of
{ok, Status, RespHeaders, Client} ->
case hackney:body(Client) of
@@ -28,8 +28,9 @@ start_child() ->
init([PeerId]) when is_binary(PeerId) ->
Port = etorrent_config:listen_port(),
- ListenOpts = [binary, inet, {active, false},
- {reuseaddr, true}],
+ Ip = etorrent_config:listen_ip(),
+ ListenOpts = [binary, inet, {active, false}, {reuseaddr, true}]
+ ++ case Ip of all -> []; _ -> [{ip, Ip}] end,
case gen_tcp:listen(Port, ListenOpts) of
{ok, LSock} ->
AcceptChild =
@@ -21,7 +21,6 @@
%% Metainfo
-export([get_piece_length/1, get_length/1, get_pieces/1, get_url/1,
get_infohash/1,
- literal_infohash/1,
file_paths/1,
file_path_len/1,
get_files/1, get_name/1,
@@ -219,10 +218,3 @@ valid_path(Path) when is_list(Path) ->
{match, _} -> true;
nomatch -> false
end.
-
-
-
-
--spec literal_infohash(infohash()) -> string().
-literal_infohash(InfohashInt) when is_integer(InfohashInt) ->
- integer_to_list(InfohashInt, 16).
@@ -87,6 +87,7 @@ handle_call(_Request, _From, State) ->
{reply, Reply, State}.
handle_cast({add_peers, TrackerUrl, IPList}, S) ->
+ lager:debug("Add peers ~p.", [IPList]),
NS = start_new_peers(TrackerUrl, IPList, S),
{noreply, NS};
handle_cast({enter_bad_peer, IP, Port, PeerId}, S) ->
@@ -381,6 +381,7 @@ build_tracker_url(Url, Event,
Downloaded = proplists:get_value(downloaded, PL),
Left = proplists:get_value(left, PL),
Port = etorrent_config:listen_port(),
+ Ip = etorrent_config:listen_ip(),
Request = [{"info_hash",
etorrent_http:build_encoded_form_rfc1738(InfoHash)},
{"peer_id",
@@ -389,7 +390,8 @@ build_tracker_url(Url, Event,
{"downloaded", Downloaded},
{"left", Left},
{"port", Port},
- {"compact", 1}],
+ {"compact", 1}]
+ ++ case Ip of all -> []; _ -> [{"ip", stringify_ip(Ip)}] end,
EReq = case Event of
none -> Request;
started -> [{"event", "started"} | Request];
@@ -516,3 +518,8 @@ eqc_test() ->
-endif.
-endif.
+
+
+
+stringify_ip({A,B,C,D}) ->
+ binary_to_list(iolist_to_binary(io_lib:format("~B.~B.~B.~B", [A,B,C,D]))).
View
@@ -71,6 +71,9 @@
% by DHT. DHT subsystem also uses it as node id and computes nodes
% distance by 'xor' their ids. Xor can not be applied on binaries.
% Thus the distinction.
+%
+% The other form is a literal (readable) string.
+% For example, `etorrent_dotdir:info_hash/1' returns infohash in this form.
-type infohash() :: pos_integer().
-type nodeid() :: pos_integer().
-type nodeinfo() :: {nodeid(), ipaddr(), portnum()}.
Oops, something went wrong.

0 comments on commit fad124d

Please sign in to comment.