Skip to content
Browse files

Protect the code against wrong packet data.

If the packet data is wrong, we should not use the packet but just log
we got a bad packet. This patch set fixes that.
  • Loading branch information...
1 parent c1b8150 commit 3a3a20bc40c7918a603f7e9bca54cc2b6fe9928a @jlouis committed Aug 24, 2011
Showing with 38 additions and 20 deletions.
  1. +25 −19 apps/utp/src/gen_utp_decoder.erl
  2. +13 −1 apps/utp/src/utp_proto.erl
View
44 apps/utp/src/gen_utp_decoder.erl
@@ -45,30 +45,36 @@ handle_call(_Request, _From, State) ->
%% @private
handle_cast({packet, P, Addr, Port}, S) ->
- {#packet { conn_id = CID,
- ty = PTy } = Packet, TS, TSDiff, RecvTime} = utp_proto:decode(P),
- case PTy of
- st_reset ->
- case gen_utp:lookup_registrar(CID, Addr, Port) of
- {ok, Pid} ->
- gen_utp_worker:incoming(Pid, Packet, {TS, TSDiff, RecvTime});
- not_found ->
- case gen_utp:lookup_registrar(CID+1, Addr, Port) of
+ case utp_proto:decode(P) of
+ {ok,
+ {#packet { conn_id = CID,
+ ty = PTy } = Packet, TS, TSDiff, RecvTime}} ->
+ case PTy of
+ st_reset ->
+ case gen_utp:lookup_registrar(CID, Addr, Port) of
+ {ok, Pid} ->
+ gen_utp_worker:incoming(Pid, Packet, {TS, TSDiff, RecvTime});
+ not_found ->
+ case gen_utp:lookup_registrar(CID+1, Addr, Port) of
+ {ok, Pid} ->
+ gen_utp_worker:incoming(Pid, Packet, {TS, TSDiff, RecvTime});
+ not_found ->
+ gen_utp:incoming_unknown(Packet, Addr, Port)
+ end
+ end;
+ _OtherState ->
+ case gen_utp:lookup_registrar(CID, Addr, Port) of
{ok, Pid} ->
gen_utp_worker:incoming(Pid, Packet, {TS, TSDiff, RecvTime});
not_found ->
gen_utp:incoming_unknown(Packet, Addr, Port)
end
- end;
- _OtherState ->
- case gen_utp:lookup_registrar(CID, Addr, Port) of
- {ok, Pid} ->
- gen_utp_worker:incoming(Pid, Packet, {TS, TSDiff, RecvTime});
- not_found ->
- gen_utp:incoming_unknown(Packet, Addr, Port)
- end
- end,
- {noreply, S};
+ end,
+ {noreply, S};
+ {error, Reason} ->
+ error_logger:info_report([decoder_error, Reason]),
+ {noreply, S}
+ end;
handle_cast(_Msg, State) ->
{noreply, State}.
View
14 apps/utp/src/utp_proto.erl
@@ -85,8 +85,20 @@ encode(#packet { ty = Type,
ExtBin/binary,
Payload/binary>>.
--spec decode(binary()) -> {packet(), timestamp(), timestamp(), timestamp()}.
+
+-spec decode(binary()) -> {ok, {packet(), timestamp(), timestamp(), timestamp()}}
+ | {error, term()}.
decode(Packet) ->
+ try
+ P = decode_packet(Packet),
+ {ok, P}
+ catch
+ error:Reason ->
+ {error, Reason}
+ end.
+
+-spec decode_packet(binary()) -> {packet(), timestamp(), timestamp(), timestamp()}.
+decode_packet(Packet) ->
TS = current_time_us(),
%% Decode packet

0 comments on commit 3a3a20b

Please sign in to comment.
Something went wrong with that request. Please try again.