Introduce generic enoise:handshake and implement tcp in terms of it
This commit is contained in:
parent
7805b983b2
commit
2f220d599c
137
src/enoise.erl
137
src/enoise.erl
@ -4,15 +4,19 @@
|
|||||||
%%% @doc Module is an interface to the Noise protocol
|
%%% @doc Module is an interface to the Noise protocol
|
||||||
%%% [https://noiseprotocol.org]
|
%%% [https://noiseprotocol.org]
|
||||||
%%%
|
%%%
|
||||||
%%% The module implements Noise over TCP (i.e. `gen_tcp') and after "upgrading"
|
%%% The module implements Noise handshake in `handshake/3'.
|
||||||
%%% a `gen_tcp'-socket into a `enoise'-socket it has a similar API as
|
|
||||||
%%% `gen_tcp'.
|
|
||||||
%%%
|
%%%
|
||||||
%%% @end
|
%%% For convenience there is also an API to use Noise over TCP (i.e. `gen_tcp')
|
||||||
%%% ------------------------------------------------------------------
|
%%% and after "upgrading" a `gen_tcp'-socket into a `enoise'-socket it has a
|
||||||
|
%%% similar API as `gen_tcp'.
|
||||||
|
%%%
|
||||||
|
%%% @end ------------------------------------------------------------------
|
||||||
|
|
||||||
-module(enoise).
|
-module(enoise).
|
||||||
|
|
||||||
|
%% Main function with generic Noise handshake
|
||||||
|
-export([handshake/3]).
|
||||||
|
|
||||||
%% API exports - Mainly mimicing gen_tcp
|
%% API exports - Mainly mimicing gen_tcp
|
||||||
-export([ accept/2
|
-export([ accept/2
|
||||||
, close/1
|
, close/1
|
||||||
@ -56,6 +60,28 @@ binary().
|
|||||||
%% API functions
|
%% API functions
|
||||||
%%====================================================================
|
%%====================================================================
|
||||||
|
|
||||||
|
%% @doc The main function - performs a Noise handshake
|
||||||
|
handshake(Options, Role, ComState) ->
|
||||||
|
Prologue = proplists:get_value(prologue, Options, <<>>),
|
||||||
|
NoiseProtocol0 = proplists:get_value(noise, Options),
|
||||||
|
|
||||||
|
NoiseProtocol =
|
||||||
|
case NoiseProtocol0 of
|
||||||
|
X when is_binary(X); is_list(X) ->
|
||||||
|
enoise_protocol:from_name(X);
|
||||||
|
_ -> NoiseProtocol0
|
||||||
|
end,
|
||||||
|
|
||||||
|
S = proplists:get_value(s, Options, undefined),
|
||||||
|
E = proplists:get_value(e, Options, undefined),
|
||||||
|
RS = proplists:get_value(rs, Options, undefined),
|
||||||
|
RE = proplists:get_value(re, Options, undefined),
|
||||||
|
|
||||||
|
HSState = enoise_hs_state:init(NoiseProtocol, Role,
|
||||||
|
Prologue, {S, E, RS, RE}),
|
||||||
|
do_handshake(HSState, ComState).
|
||||||
|
|
||||||
|
|
||||||
%% @doc Upgrades a gen_tcp, or equivalent, connected socket to a Noise socket,
|
%% @doc Upgrades a gen_tcp, or equivalent, connected socket to a Noise socket,
|
||||||
%% that is, performs the client-side noise handshake.
|
%% that is, performs the client-side noise handshake.
|
||||||
%%
|
%%
|
||||||
@ -65,7 +91,7 @@ binary().
|
|||||||
Options :: noise_options()) ->
|
Options :: noise_options()) ->
|
||||||
{ok, noise_socket()} | {error, term()}.
|
{ok, noise_socket()} | {error, term()}.
|
||||||
connect(TcpSock, Options) ->
|
connect(TcpSock, Options) ->
|
||||||
start_handshake(TcpSock, initiator, Options).
|
tcp_handshake(TcpSock, initiator, Options).
|
||||||
|
|
||||||
%% @doc Upgrades a gen_tcp, or equivalent, connected socket to a Noise socket,
|
%% @doc Upgrades a gen_tcp, or equivalent, connected socket to a Noise socket,
|
||||||
%% that is, performs the server-side noise handshake.
|
%% that is, performs the server-side noise handshake.
|
||||||
@ -76,7 +102,7 @@ connect(TcpSock, Options) ->
|
|||||||
Options :: noise_options()) ->
|
Options :: noise_options()) ->
|
||||||
{ok, noise_socket()} | {error, term()}.
|
{ok, noise_socket()} | {error, term()}.
|
||||||
accept(TcpSock, Options) ->
|
accept(TcpSock, Options) ->
|
||||||
start_handshake(TcpSock, responder, Options).
|
tcp_handshake(TcpSock, responder, Options).
|
||||||
|
|
||||||
%% @doc Writes `Data' to `Socket'
|
%% @doc Writes `Data' to `Socket'
|
||||||
%% @end
|
%% @end
|
||||||
@ -125,69 +151,94 @@ controlling_process(#enoise{ pid = Pid }, NewPid) ->
|
|||||||
%%====================================================================
|
%%====================================================================
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%====================================================================
|
%%====================================================================
|
||||||
start_handshake(TcpSock, Role, Options) ->
|
tcp_handshake(TcpSock, Role, Options) ->
|
||||||
case check_tcp(TcpSock) of
|
case check_gen_tcp(TcpSock) of
|
||||||
{ok, WasActive} ->
|
ok ->
|
||||||
inet:setopts(TcpSock, [{active, false}]), %% False for handshake
|
{ok, [{active, Active}]} = inet:getopts(TcpSock, [active]),
|
||||||
Prologue = proplists:get_value(prologue, Options, <<>>),
|
ComState = #{ recv_msg => fun gen_tcp_rcv_msg/1,
|
||||||
NoiseProtocol = proplists:get_value(noise, Options),
|
send_msg => fun gen_tcp_snd_msg/2,
|
||||||
|
state => {TcpSock, Active, <<>>} },
|
||||||
|
|
||||||
S = proplists:get_value(s, Options, undefined),
|
case handshake(Options, Role, ComState) of
|
||||||
E = proplists:get_value(e, Options, undefined),
|
{ok, #{ rx := Rx, tx := Tx }, #{ state := {_, _, Buf} }} ->
|
||||||
RS = proplists:get_value(rs, Options, undefined),
|
{ok, Pid} = enoise_connection:start_link(TcpSock, Rx, Tx, self(), {Active, Buf}),
|
||||||
RE = proplists:get_value(re, Options, undefined),
|
{ok, #enoise{ pid = Pid }};
|
||||||
|
Err = {error, _} ->
|
||||||
HSState = enoise_hs_state:init(NoiseProtocol, Role,
|
Err
|
||||||
Prologue, {S, E, RS, RE}),
|
end;
|
||||||
|
|
||||||
do_handshake(TcpSock, HSState, WasActive);
|
|
||||||
Err = {error, _} ->
|
Err = {error, _} ->
|
||||||
Err
|
Err
|
||||||
end.
|
end.
|
||||||
|
|
||||||
do_handshake(TcpSock, HState, WasActive) ->
|
do_handshake(HState, ComState) ->
|
||||||
case enoise_hs_state:next_message(HState) of
|
case enoise_hs_state:next_message(HState) of
|
||||||
in ->
|
in ->
|
||||||
case hs_recv(TcpSock) of
|
case hs_recv_msg(ComState) of
|
||||||
{ok, Data} ->
|
{ok, Data, ComState1} ->
|
||||||
{ok, HState1, _Msg} = enoise_hs_state:read_message(HState, Data),
|
{ok, HState1, _Msg} = enoise_hs_state:read_message(HState, Data),
|
||||||
do_handshake(TcpSock, HState1, WasActive);
|
do_handshake(HState1, ComState1);
|
||||||
Err = {error, _} ->
|
Err = {error, _} ->
|
||||||
Err
|
Err
|
||||||
end;
|
end;
|
||||||
out ->
|
out ->
|
||||||
{ok, HState1, Msg} = enoise_hs_state:write_message(HState, <<>>),
|
{ok, HState1, Msg} = enoise_hs_state:write_message(HState, <<>>),
|
||||||
hs_send(TcpSock, Msg),
|
{ok, ComState1} = hs_send_msg(ComState, Msg),
|
||||||
do_handshake(TcpSock, HState1, WasActive);
|
do_handshake(HState1, ComState1);
|
||||||
done ->
|
done ->
|
||||||
{ok, #{ rx := Rx, tx := Tx }} = enoise_hs_state:finalize(HState),
|
{ok, Res} = enoise_hs_state:finalize(HState),
|
||||||
{ok, Pid} = enoise_connection:start_link(TcpSock, Rx, Tx, self(), WasActive),
|
{ok, Res, ComState}
|
||||||
{ok, #enoise{ pid = Pid }}
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
check_tcp(TcpSock) ->
|
hs_recv_msg(CS = #{ recv_msg := Recv, state := S }) ->
|
||||||
{ok, TcpOpts} = inet:getopts(TcpSock, [mode, packet, active, header, packet_size]),
|
case Recv(S) of
|
||||||
|
{ok, Data, S1} -> {ok, Data, CS#{ state := S1 }};
|
||||||
|
Err = {error, _} -> Err
|
||||||
|
end.
|
||||||
|
|
||||||
|
hs_send_msg(CS = #{ send_msg := Send, state := S }, Data) ->
|
||||||
|
case Send(S, Data) of
|
||||||
|
{ok, S1} -> {ok, CS#{ state := S1 }};
|
||||||
|
Err = {error, _} -> Err
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
%% -- gen_tcp specific functions ---------------------------------------------
|
||||||
|
|
||||||
|
check_gen_tcp(TcpSock) ->
|
||||||
|
{ok, TcpOpts} = inet:getopts(TcpSock, [mode, packet, header, packet_size]),
|
||||||
Packet = proplists:get_value(packet, TcpOpts, 0),
|
Packet = proplists:get_value(packet, TcpOpts, 0),
|
||||||
Header = proplists:get_value(header, TcpOpts, 0),
|
Header = proplists:get_value(header, TcpOpts, 0),
|
||||||
Active = proplists:get_value(active, TcpOpts, true),
|
|
||||||
PSize = proplists:get_value(packet_size, TcpOpts, undefined),
|
PSize = proplists:get_value(packet_size, TcpOpts, undefined),
|
||||||
Mode = proplists:get_value(mode, TcpOpts, binary),
|
Mode = proplists:get_value(mode, TcpOpts, binary),
|
||||||
case (Packet == 0 orelse Packet == raw)
|
case (Packet == 0 orelse Packet == raw)
|
||||||
andalso Header == 0 andalso PSize == 0 andalso Mode == binary of
|
andalso Header == 0 andalso PSize == 0 andalso Mode == binary of
|
||||||
true ->
|
true ->
|
||||||
case gen_tcp:controlling_process(TcpSock, self()) of
|
gen_tcp:controlling_process(TcpSock, self());
|
||||||
ok -> {ok, Active};
|
|
||||||
Err = {error, _} -> Err
|
|
||||||
end;
|
|
||||||
false ->
|
false ->
|
||||||
{error, {invalid_tcp_options, proplists:delete(active, TcpOpts)}}
|
{error, {invalid_tcp_options, TcpOpts}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
hs_send(TcpSock, Msg) ->
|
gen_tcp_snd_msg(S = {TcpSock, _, _}, Msg) ->
|
||||||
Len = byte_size(Msg),
|
Len = byte_size(Msg),
|
||||||
gen_tcp:send(TcpSock, <<Len:16, Msg/binary>>).
|
ok = gen_tcp:send(TcpSock, <<Len:16, Msg/binary>>),
|
||||||
|
{ok, S}.
|
||||||
|
|
||||||
hs_recv(TcpSock) ->
|
gen_tcp_rcv_msg({TcpSock, true, Buf}) ->
|
||||||
|
receive {tcp, TcpSock, Data} ->
|
||||||
|
case <<Buf/binary, Data/binary>> of
|
||||||
|
Buf1 = <<Len:16, Rest/binary>> when byte_size(Rest) < Len ->
|
||||||
|
gen_tcp_rcv_msg({TcpSock, true, Buf1});
|
||||||
|
<<Len:16, Rest/binary>> ->
|
||||||
|
<<Data1:Len/binary, Buf1/binary>> = Rest,
|
||||||
|
{ok, Data1, {TcpSock, true, Buf1}}
|
||||||
|
end
|
||||||
|
after 1000 ->
|
||||||
|
{error, timeout}
|
||||||
|
end;
|
||||||
|
gen_tcp_rcv_msg(S = {TcpSock, false, <<>>}) ->
|
||||||
{ok, <<Len:16>>} = gen_tcp:recv(TcpSock, 2, 1000),
|
{ok, <<Len:16>>} = gen_tcp:recv(TcpSock, 2, 1000),
|
||||||
gen_tcp:recv(TcpSock, Len, 1000).
|
case gen_tcp:recv(TcpSock, Len, 1000) of
|
||||||
|
{ok, Data} -> {ok, Data, S};
|
||||||
|
Err = {error, _} -> Err
|
||||||
|
end.
|
||||||
|
|
||||||
|
@ -2,7 +2,10 @@
|
|||||||
%%% @copyright 2018, Aeternity Anstalt
|
%%% @copyright 2018, Aeternity Anstalt
|
||||||
%%%
|
%%%
|
||||||
%%% @doc Module implementing a gen_server for holding a handshaked
|
%%% @doc Module implementing a gen_server for holding a handshaked
|
||||||
%%% Noise connection.
|
%%% Noise connection over gen_tcp.
|
||||||
|
%%%
|
||||||
|
%%% Some care is needed since the underlying transmission is broken up
|
||||||
|
%%% into Noise packets, so we need some buffering.
|
||||||
%%%
|
%%%
|
||||||
%%% @end
|
%%% @end
|
||||||
%%% ------------------------------------------------------------------
|
%%% ------------------------------------------------------------------
|
||||||
@ -25,13 +28,20 @@
|
|||||||
-record(state, {rx, tx, owner, tcp_sock, active, buf = <<>>, rawbuf = <<>>}).
|
-record(state, {rx, tx, owner, tcp_sock, active, buf = <<>>, rawbuf = <<>>}).
|
||||||
|
|
||||||
%% -- API --------------------------------------------------------------------
|
%% -- API --------------------------------------------------------------------
|
||||||
start_link(TcpSock, Rx, Tx, Owner, Active) ->
|
start_link(TcpSock, Rx, Tx, Owner, {Active, Buf}) ->
|
||||||
inet:setopts(TcpSock, [{active, Active}]),
|
State0 = #state{ rx = Rx, tx = Tx, owner = Owner,
|
||||||
State = #state{ rx = Rx, tx = Tx, owner = Owner,
|
|
||||||
tcp_sock = TcpSock, active = Active },
|
tcp_sock = TcpSock, active = Active },
|
||||||
|
State = case Active of
|
||||||
|
true -> State0;
|
||||||
|
false -> State0#state{ rawbuf = Buf }
|
||||||
|
end,
|
||||||
case gen_server:start_link(?MODULE, [State], []) of
|
case gen_server:start_link(?MODULE, [State], []) of
|
||||||
{ok, Pid} ->
|
{ok, Pid} ->
|
||||||
ok = gen_tcp:controlling_process(TcpSock, Pid),
|
ok = gen_tcp:controlling_process(TcpSock, Pid),
|
||||||
|
%% Changing controlling process if active requires a bit
|
||||||
|
%% of fiddling with already received content...
|
||||||
|
[ Pid ! {tcp, TcpSock, Buf} || Buf /= <<>>, Active ],
|
||||||
|
flush_tcp(Active, Pid, TcpSock),
|
||||||
{ok, Pid};
|
{ok, Pid};
|
||||||
Err = {error, _} ->
|
Err = {error, _} ->
|
||||||
Err
|
Err
|
||||||
@ -55,6 +65,10 @@ controlling_process(Noise, NewPid) ->
|
|||||||
init([State]) ->
|
init([State]) ->
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
|
handle_call(close, _From, S) ->
|
||||||
|
{stop, normal, ok, S};
|
||||||
|
handle_call(_Call, _From, S = #state{ tcp_sock = closed }) ->
|
||||||
|
{reply, {error, closed}, S};
|
||||||
handle_call({send, Data}, _From, S) ->
|
handle_call({send, Data}, _From, S) ->
|
||||||
{Res, S1} = handle_send(S, Data),
|
{Res, S1} = handle_send(S, Data),
|
||||||
{reply, Res, S1};
|
{reply, Res, S1};
|
||||||
@ -65,9 +79,7 @@ handle_call({recv, Length, Timeout}, _From, S) ->
|
|||||||
{reply, Res, S1};
|
{reply, Res, S1};
|
||||||
handle_call({controlling_process, OldPid, NewPid}, _From, S) ->
|
handle_call({controlling_process, OldPid, NewPid}, _From, S) ->
|
||||||
{Res, S1} = handle_control_change(S, OldPid, NewPid),
|
{Res, S1} = handle_control_change(S, OldPid, NewPid),
|
||||||
{reply, Res, S1};
|
{reply, Res, S1}.
|
||||||
handle_call(close, _From, S) ->
|
|
||||||
{stop, normal, ok, S}.
|
|
||||||
|
|
||||||
handle_cast(_Msg, S) ->
|
handle_cast(_Msg, S) ->
|
||||||
{noreply, S}.
|
{noreply, S}.
|
||||||
@ -78,13 +90,13 @@ handle_info({tcp, TS, Data}, S = #state{ tcp_sock = TS }) ->
|
|||||||
{noreply, S2};
|
{noreply, S2};
|
||||||
handle_info({tcp_closed, TS}, S = #state{ tcp_sock = TS, active = A, owner = O }) ->
|
handle_info({tcp_closed, TS}, S = #state{ tcp_sock = TS, active = A, owner = O }) ->
|
||||||
[ O ! {tcp_closed, TS} || A ],
|
[ O ! {tcp_closed, TS} || A ],
|
||||||
{stop, normal, S#state{ tcp_sock = undefined }};
|
{noreply, S#state{ tcp_sock = closed }};
|
||||||
handle_info(Msg, S) ->
|
handle_info(Msg, S) ->
|
||||||
io:format("Unexpected info: ~p\n", [Msg]),
|
io:format("Unexpected info: ~p\n", [Msg]),
|
||||||
{noreply, S}.
|
{noreply, S}.
|
||||||
|
|
||||||
terminate(_Reason, #state{ tcp_sock = TcpSock }) ->
|
terminate(_Reason, #state{ tcp_sock = TcpSock }) ->
|
||||||
[ gen_tcp:close(TcpSock) || TcpSock /= undefined ],
|
[ gen_tcp:close(TcpSock) || TcpSock /= closed ],
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
code_change(_OldVsn, State, _Extra) ->
|
code_change(_OldVsn, State, _Extra) ->
|
||||||
@ -102,7 +114,7 @@ handle_control_change(S, _OldPid, _NewPid) ->
|
|||||||
|
|
||||||
handle_data(S = #state{ rawbuf = Buf, rx = Rx }, Data) ->
|
handle_data(S = #state{ rawbuf = Buf, rx = Rx }, Data) ->
|
||||||
case <<Buf/binary, Data/binary>> of
|
case <<Buf/binary, Data/binary>> of
|
||||||
B = <<Len:16, Rest/binary>> when Len < byte_size(Rest) ->
|
B = <<Len:16, Rest/binary>> when Len > byte_size(Rest) ->
|
||||||
{S#state{ rawbuf = B }, []}; %% Not a full message - save it
|
{S#state{ rawbuf = B }, []}; %% Not a full message - save it
|
||||||
<<Len:16, Rest/binary>> ->
|
<<Len:16, Rest/binary>> ->
|
||||||
<<Msg:Len/binary, Rest2/binary>> = Rest,
|
<<Msg:Len/binary, Rest2/binary>> = Rest,
|
||||||
@ -211,3 +223,12 @@ timed_recv(TcpSock, Len, TO) ->
|
|||||||
Err = {error, _} ->
|
Err = {error, _} ->
|
||||||
Err
|
Err
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
flush_tcp(false, _Pid, _TcpSock) ->
|
||||||
|
ok;
|
||||||
|
flush_tcp(true, Pid, TcpSock) ->
|
||||||
|
receive {tcp, TcpSock, Data} ->
|
||||||
|
Pid ! {tcp, TcpSock, Data},
|
||||||
|
flush_tcp(true, Pid, TcpSock)
|
||||||
|
after 1 -> ok
|
||||||
|
end.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user