summaryrefslogtreecommitdiff
path: root/p11p-daemon/src/p11p_server.erl
diff options
context:
space:
mode:
Diffstat (limited to 'p11p-daemon/src/p11p_server.erl')
-rw-r--r--p11p-daemon/src/p11p_server.erl70
1 files changed, 43 insertions, 27 deletions
diff --git a/p11p-daemon/src/p11p_server.erl b/p11p-daemon/src/p11p_server.erl
index ff1a8df..b3ffa5c 100644
--- a/p11p-daemon/src/p11p_server.erl
+++ b/p11p-daemon/src/p11p_server.erl
@@ -11,7 +11,7 @@
%% API.
-export([start_link/1]).
--export([add_to_clientbuf/2, reply/2]).
+-export([reply/2]).
%% Genserver callbacks.
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
@@ -23,7 +23,9 @@
remote :: pid() | undefined,
socket :: gen_tcp:socket(),
msg :: p11rpc_msg() | undefined,
- clientbuf = <<>> :: binary()
+ recv_count = 0 :: non_neg_integer(),
+ send_count = 0 :: non_neg_integer()
+ %%clientbuf = <<>> :: binary()
}).
%% API.
@@ -31,10 +33,6 @@
start_link(Args) ->
gen_server:start_link(?MODULE, Args, []).
--spec add_to_clientbuf(pid(), binary()) -> {ok, non_neg_integer()}.
-add_to_clientbuf(Pid, Data) ->
- gen_server:call(Pid, {add_to_clientbuf, Data}).
-
-spec reply(pid(), p11rpc_msg()) -> {ok, non_neg_integer()}.
reply(Pid, Response) ->
gen_server:call(Pid, {respond, Response}).
@@ -46,19 +44,21 @@ init([Token, Socket]) ->
gen_server:cast(self(), accept), % Invoke accept, returning a socket in state.
{ok, #state{tokname = Token, socket = Socket}}.
-handle_call({add_to_clientbuf, Data}, _, #state{clientbuf = B} = S) ->
- Buf = <<B/binary, Data/binary>>,
- {reply, {ok, size(Buf)}, S#state{clientbuf = Buf}};
-handle_call({respond, R}, _, #state{socket = Client, clientbuf = B} = S) ->
- Data = p11p_rpc:serialise(R),
- Buf = <<B/binary, Data/binary>>,
- %%lager:debug("~p: sending ~B octets to client as response", [self(), size(Buf)]),
- ok = gen_tcp:send(Client, Buf), % TODO: what about short writes?
- {reply, {ok, size(Buf)}, S#state{clientbuf = <<>>}};
+handle_call({respond, R}, _, #state{socket = Sock, send_count = Sent} = S) ->
+ D = p11p_rpc:serialise(R),
+ Buf = case Sent of
+ 0 -> <<?RPC_VERSION:8, D/binary>>;
+ _ -> D
+ end,
+ %%lager:debug("~p: sending ~B octets as response", [self(), size(Buf)]),
+ ok = gen_tcp:send(Sock, Buf), % TODO: what about short writes?
+ {reply, {ok, size(Buf)}, S#state{send_count = Sent + 1}};
+
handle_call(Call, _, S) ->
lager:debug("~p: Unhandled call: ~p~n", [self(), Call]),
{reply, unhandled, S}.
+%% Wait for new connection.
handle_cast(accept, State = #state{tokname = TokName, socket = ListenSocket}) ->
%% Blocking until client connects or timeout fires.
%% Without a timeout our supervisor cannot terminate us.
@@ -76,24 +76,38 @@ handle_cast(accept, State = #state{tokname = TokName, socket = ListenSocket}) ->
lager:debug("~p: listening socket closed", [self()]),
{stop, normal, State}
end;
+
handle_cast(Cast, State) ->
lager:debug("~p: Unhandled cast: ~p~n", [self(), Cast]),
{noreply, State}.
-handle_info({tcp, _Port, DataIn}, #state{tokname = TokName} = S)
+%% First packet from P11 client.
+handle_info({tcp, Port, DataIn}, #state{tokname = TokName} = S)
when S#state.remote == undefined ->
%%lager:debug("~p: received ~B octets from client on socket ~p, from new client", [self(), size(Data), Port]),
- <<Version:8, Data/binary>> = DataIn,
- Remote = p11p_remote_manager:remote_for_token(TokName),
- p11p_remote:add_to_outbuf(Remote, <<Version>>),
- State = S#state{remote = Remote},
- {noreply, handle_client_data(State, p11p_rpc:new(), Data)};
+ <<RPCVersion:8, Data/binary>> = DataIn,
+ case RPCVersion of
+ ?RPC_VERSION ->
+ {noreply,
+ p11_client_data(
+ S#state{remote = p11p_remote_manager:remote_for_token(TokName)},
+ p11p_rpc:new(),
+ Data)};
+ BadVersion ->
+ lager:info("~p: ~p: invalid RPC version: ~p", [self(), Port,
+ BadVersion]),
+ {stop, bad_proto, S}
+ end;
+
+%% Subsequent packages from P11 client.
handle_info({tcp, _Port, DataIn}, #state{msg = Msg} = S) ->
%%lager:debug("~p: received ~B octets from client on socket ~p, with ~B octets already in buffer", [self(), size(Data), Port, size(Msg#p11rpc_msg.buffer)]),
- {noreply, handle_client_data(S, Msg, DataIn)};
+ {noreply, p11_client_data(S, Msg, DataIn)};
+
handle_info({tcp_closed, Port}, S) ->
lager:debug("~p: socket ~p closed", [self(), Port]),
{stop, normal, S};
+
handle_info(Info, S) ->
lager:debug("~p: Unhandled info: ~p~n", [self(), Info]),
{noreply, S}.
@@ -108,11 +122,13 @@ code_change(_OldVersion, State, _Extra) ->
{ok, State}.
%% Private functions.
-handle_client_data(#state{remote = Remote} = S, MsgIn, DataIn) ->
+p11_client_data(#state{remote = Remote, recv_count = Recv} = S, MsgIn,
+ DataIn) ->
case p11p_rpc:parse(MsgIn, DataIn) of
- {done, Msg} ->
- ok = p11p_remote:request(Remote, Msg),
- S#state{msg = p11p_rpc:new(Msg#p11rpc_msg.buffer)};
{needmore, Msg} ->
- S#state{msg = Msg}
+ S#state{msg = Msg};
+ {done, Msg} ->
+ {ok, _BytesSent} = p11p_remote:request(Remote, Msg),
+ S#state{msg = p11p_rpc:new(Msg#p11rpc_msg.buffer),
+ recv_count = Recv + 1}
end.