summaryrefslogtreecommitdiff
path: root/components/dlink_tcp
diff options
context:
space:
mode:
authorMagnus Feuer <mfeuer@jaguarlandrover.com>2015-06-09 12:30:09 -0700
committerMagnus Feuer <mfeuer@jaguarlandrover.com>2015-06-09 13:54:54 -0700
commitb70691a0908236dd411fc1fa167f7236d07482ab (patch)
treeae6ce0cf641e61f35f7e5572d6b06806d65ff54e /components/dlink_tcp
parent1e2990552256491df6661073012238db6b96b378 (diff)
downloadrvi_core-b70691a0908236dd411fc1fa167f7236d07482ab.tar.gz
Fixed crash if inbound socket is disconnected prior to authenticaiton
Diffstat (limited to 'components/dlink_tcp')
-rw-r--r--components/dlink_tcp/src/connection.erl22
-rw-r--r--components/dlink_tcp/src/dlink_tcp_rpc.erl3
-rw-r--r--components/dlink_tcp/src/listener.erl2
3 files changed, 19 insertions, 8 deletions
diff --git a/components/dlink_tcp/src/connection.erl b/components/dlink_tcp/src/connection.erl
index 9faa2c5..fbba9a6 100644
--- a/components/dlink_tcp/src/connection.erl
+++ b/components/dlink_tcp/src/connection.erl
@@ -55,6 +55,7 @@ setup(IP, Port, Sock, Mod, Fun, Arg) ->
case gen_server:start_link(connection, {IP, Port, Sock, Mod, Fun, Arg},[]) of
{ ok, GenSrvPid } = Res ->
gen_tcp:controlling_process(Sock, GenSrvPid),
+ gen_server:cast(GenSrvPid, {activate_socket, Sock}),
Res;
Err ->
@@ -63,7 +64,7 @@ setup(IP, Port, Sock, Mod, Fun, Arg) ->
send(Pid, Data) when is_pid(Pid) ->
gen_server:cast(Pid, {send, Data}).
-
+
send(IP, Port, Data) ->
case connection_manager:find_connection_by_address(IP, Port) of
{ok, Pid} ->
@@ -78,7 +79,7 @@ send(IP, Port, Data) ->
terminate_connection(Pid) when is_pid(Pid) ->
gen_server:call(Pid, terminate_connection).
-
+
terminate_connection(IP, Port) ->
case connection_manager:find_connection_by_address(IP, Port) of
{ok, Pid} ->
@@ -99,7 +100,7 @@ is_connection_up(IP, Port) ->
_Err ->
false
end.
-
+
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
@@ -130,7 +131,6 @@ init({IP, Port, Sock, Mod, Fun, Arg}) ->
?debug("connection:init(): Module: ~p", [Mod]),
?debug("connection:init(): Function: ~p", [Fun]),
?debug("connection:init(): Arg: ~p", [Arg]),
- inet:setopts(Sock, [{active, once}]),
%% Grab socket control
{ok, #st{
ip = IP,
@@ -138,7 +138,8 @@ init({IP, Port, Sock, Mod, Fun, Arg}) ->
sock = Sock,
mod = Mod,
func = Fun,
- args = Arg
+ args = Arg,
+ buffer = undefined
}}.
@@ -160,7 +161,7 @@ init({IP, Port, Sock, Mod, Fun, Arg}) ->
handle_call(terminate_connection, _From, St) ->
?debug("~p:handle_call(terminate_connection): Terminating: ~p",
- [ ?MODULE, {St#st.ip, St#st.port}]),
+ [ ?MODULE, {St#st.ip, St#st.port}]),
{stop, Reason, NSt} = handle_info({tcp_closed, St#st.sock}, St),
{stop, Reason, ok, NSt};
@@ -182,12 +183,18 @@ handle_call(_Request, _From, State) ->
%%--------------------------------------------------------------------
handle_cast({send, Data}, St) ->
?debug("~p:handle_call(send): Sending: ~p",
- [ ?MODULE, Data]),
+ [ ?MODULE, Data]),
gen_tcp:send(St#st.sock, Data),
{noreply, St};
+handle_cast({activate_socket, Sock}, State) ->
+ Res = inet:setopts(Sock, [{active, once}]),
+ ?debug("connection:activate_socket(): ~p", [Res]),
+ {noreply, State};
+
+
handle_cast(_Msg, State) ->
?warning("~p:handle_cast(): Unknown call: ~p", [ ?MODULE, _Msg]),
{noreply, State}.
@@ -208,6 +215,7 @@ handle_info({tcp, Sock, Data},
#st { ip = undefined } = St) ->
{ok, {IP, Port}} = inet:peername(Sock),
NSt = St#st { ip = inet_parse:ntoa(IP), port = Port },
+ ?warning("YESSSS"),
handle_info({tcp, Sock, Data}, NSt);
diff --git a/components/dlink_tcp/src/dlink_tcp_rpc.erl b/components/dlink_tcp/src/dlink_tcp_rpc.erl
index 929cffe..c60a8a8 100644
--- a/components/dlink_tcp/src/dlink_tcp_rpc.erl
+++ b/components/dlink_tcp/src/dlink_tcp_rpc.erl
@@ -422,6 +422,9 @@ process_data(_FromPid,
%% We lost the socket connection.
%% Unregister all services that were routed to the remote end that just died.
+handle_socket(FromPid, undefined, SetupPort, closed, Arg) ->
+ handle_socket(FromPid, "0.0.0.0", SetupPort, closed, Arg);
+
handle_socket(FromPid, SetupIP, SetupPort, closed, [CompSpec]) ->
?info("dlink_tcp:closed(): SetupAddress: {~p, ~p}", [ SetupIP, SetupPort ]),
diff --git a/components/dlink_tcp/src/listener.erl b/components/dlink_tcp/src/listener.erl
index f2386d5..41a272a 100644
--- a/components/dlink_tcp/src/listener.erl
+++ b/components/dlink_tcp/src/listener.erl
@@ -63,7 +63,7 @@ terminate(_Reason, _State) ->
ok.
sock_opts() ->
- [list, {active, once}, {packet, 4}].
+ [list, {active, once}, {packet, 0}].
new_connection(IP, Port, Sock, State) ->
?debug("listener:new_connection(): Peer IP: ~p (ignored)", [IP]),