Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

sniff: support the snaplen option

For testing pkt:decode/2, allow the epcap snaplen option. To use
decode/2 with whole frames, set snaplen to 16#ffff.
  • Loading branch information...
commit d2fc04e2f10dabb7126cf48437091443d92d357a 1 parent 51029be
@msantos authored
Showing with 15 additions and 2 deletions.
  1. +15 −2 examples/sniff.erl
View
17 examples/sniff.erl
@@ -46,6 +46,7 @@
-record(state, {
pid,
+ crash = true,
format = [] % full packet dump: binary, hex
}).
@@ -85,7 +86,7 @@ handle_sync_event(_Event, _From, StateName, State) ->
%%
handle_info({packet, DLT, Time, Len, Data}, sniffing,
#state{format = Format} = State) ->
- Packet = pkt:decapsulate({pkt:dlt(DLT), Data}),
+ Packet = decode(DLT, Data, State),
Headers = header(Packet),
error_logger:info_report([
@@ -120,10 +121,12 @@ code_change(_OldVsn, StateName, State, _Extra) ->
%%--------------------------------------------------------------------
waiting({start, Opt}, State) ->
Format = proplists:get_value(format, Opt, []),
+ Snaplen = proplists:get_value(snaplen, Opt),
{ok, Pid} = epcap:start(Opt),
{next_state, sniffing, State#state{
pid = Pid,
- format = Format
+ format = Format,
+ crash = Snaplen =:= undefined
}}.
sniffing({start, Opt}, #state{pid = Pid} = State) ->
@@ -138,6 +141,16 @@ sniffing(stop, #state{pid = Pid} = State) ->
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
+decode(DLT, Data, #state{crash = true}) ->
+ pkt:decapsulate({pkt:dlt(DLT), Data});
+decode(DLT, Data, #state{crash = false}) ->
+ case pkt:decode(pkt:dlt(DLT), Data) of
+ {ok, {Headers, Payload}} ->
+ Headers ++ [Payload];
+ {error, SoFar, _Failed} ->
+ SoFar
+ end.
+
header(Payload) ->
header(Payload, []).

0 comments on commit d2fc04e

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