Fix decoding of SCTP chunks #1

Merged
merged 5 commits into from Sep 7, 2012
View
@@ -1,2 +1,3 @@
-*.[oa]
-*.beam
+ebin
+.test
+*.swp
View
@@ -1,4 +1,3 @@
-
REBAR=$(shell which rebar || echo ./rebar)
all: compile
@@ -15,3 +14,7 @@ compile: $(REBAR)
clean: $(REBAR)
@$(REBAR) clean
+test:
+ @$(REBAR) xref eunit
+
+.PHONY: test
View
@@ -1,13 +0,0 @@
-{application, pkt,
- [
- {description, "Network packet parsing library"},
- {vsn, "0.03"},
- {modules, [
- pkt
- ]},
- {applications, [
- kernel,
- stdlib
- ]},
- {env, []}
- ]}.
View
@@ -0,0 +1,3 @@
+{xref_checks, [undefined_function_calls]}.
+
+{eunit_opts, [verbose, {report, {eunit_surefire, [{dir, "."}]}}]}.
View
@@ -0,0 +1,5 @@
+{application, pkt, [
+ {description, "Network packet parsing library"},
+ {vsn, "0.03"},
+ {applications, [kernel, stdlib]}
+]}.
View
@@ -60,14 +60,13 @@
proto/1,
tcp/1,
udp/1,
+ sctp/1,
dlt/1
]).
-
decapsulate_dlt(Dlt, Data) ->
decapsulate({link_type(Dlt), Data}, []).
-
decapsulate({DLT, Data}) when is_integer(DLT) ->
decapsulate({link_type(DLT), Data}, []);
decapsulate({DLT, Data}) when is_atom(DLT) ->
@@ -355,27 +354,19 @@ options(Offset, Payload) ->
%%
sctp(<<SPort:16, DPort:16, VTag:32, Sum:32, Payload/binary>>) ->
{#sctp{sport = SPort, dport = DPort, vtag = VTag, sum = Sum,
- chunks=sctp_chunk_list_gen(Payload)}, []}.
-
-sctp_chunk_list_gen(Payload) ->
- sctp_chunk_list_gen(Payload, []).
-
-sctp_chunk_list_gen(Payload, List) ->
- % chop the first chunk off the payload
- case sctp_chunk_chop(Payload) of
- {Chunk, Remainder} ->
- % loop
- sctp_chunk_list_gen(Remainder, [Chunk|List]);
- [] ->
- List
- end.
-
-sctp_chunk_chop(<<>>) ->
- [];
-sctp_chunk_chop(<<Ctype:8, Cflags:8, Clen:16, Remainder/binary>>) ->
- Payload = binary:part(Remainder, 0, Clen-4),
- Tail = binary:part(Remainder, Clen-4, byte_size(Remainder)-(Clen-4)),
- {sctp_chunk(Ctype, Cflags, Clen, Payload), Tail}.
+ chunks = sctp_decode_chunks(Payload)}, []}.
+
+sctp_decode_chunks(Chunks) ->
+ sctp_decode_chunks(Chunks, []).
+
+sctp_decode_chunks(<<>>, Acc) -> Acc;
+sctp_decode_chunks(<<Type:8, Flags:8, Length:16, Rest/binary>>, Acc) ->
+ L = case Length rem 4 of
+ 0 -> Length - 4;
+ Pad -> Length + (4 - Pad) - 4
+ end,
+ <<Payload:L/binary-unit:8, Tail/binary>> = Rest,
+ sctp_decode_chunks(Tail, [sctp_chunk(Type, Flags, L, Payload) | Acc]).
sctp_chunk(Ctype, Cflags, Clen, Payload) ->
#sctp_chunk{type=Ctype, flags=Cflags, len = Clen-4,
@@ -386,7 +377,6 @@ sctp_chunk_payload(0, <<Tsn:32, Sid:16, Ssn:16, Ppi:32, Data/binary>>) ->
sctp_chunk_payload(_, Data) ->
Data.
-
%%
%% UDP
%%
View
@@ -0,0 +1,17 @@
+-module(pkt_tests).
+
+-include("pkt.hrl").
+-include("pkt_tests.hrl").
+-include_lib("eunit/include/eunit.hrl").
+
+sctp_test() ->
+ {SCTP, []} = pkt:sctp(?SCTP_PACKET),
+ {SCTP1, []} = pkt:sctp(?SCTP_PACKET_WITH_PADDING),
+ Chunk = erlang:hd(SCTP#sctp.chunks),
+ Chunk1 = erlang:hd(SCTP1#sctp.chunks),
+ ?assertEqual(SCTP#sctp.sport, 7),
+ ?assertEqual(SCTP1#sctp.dport, 32837),
+ ?assertEqual(Chunk#sctp_chunk.type, 1),
+ ?assertEqual(byte_size(Chunk#sctp_chunk.payload), 28),
+ ?assertEqual(length(SCTP1#sctp.chunks), 1),
+ ?assertEqual(Chunk1#sctp_chunk.payload#sctp_chunk_data.tsn, 2860946939).
View
@@ -0,0 +1,20 @@
+-define(SCTP_PACKET,
+ <<0,7,0,7,0,0,0,0,55,97,167,70,1,0,0,32,67,35,37,68,0,0,255,255,0,17,0,17,
+ 92,254,55,159,192,0,0,4,0,12,0,6,0,5,0,0>>).
+
+-define(SCTP_PACKET_WITH_PADDING,
+ <<0,80,128,69,233,183,59,195,103,102,136,252,0,3,1,65,170,134,149,251,0,0,0,
+ 0,0,0,0,0,72,84,84,80,47,49,46,49,32,50,48,48,32,79,75,13,10,68,97,116,101,
+ 58,32,84,104,117,44,32,49,50,32,74,97,110,32,50,48,48,54,32,48,50,58,53,56,
+ 58,48,48,32,71,77,84,13,10,83,101,114,118,101,114,58,32,65,112,97,99,104,
+ 101,47,50,46,48,46,53,52,32,40,85,110,105,120,41,32,68,65,86,47,50,32,80,
+ 72,80,47,52,46,52,46,49,13,10,76,97,115,116,45,77,111,100,105,102,105,101,
+ 100,58,32,84,104,117,44,32,48,53,32,74,97,110,32,50,48,48,54,32,48,50,58,
+ 48,53,58,48,55,32,71,77,84,13,10,69,84,97,103,58,32,34,52,50,48,51,51,98,
+ 45,52,97,55,102,45,102,54,50,102,49,97,99,48,34,13,10,65,99,99,101,112,116,
+ 45,82,97,110,103,101,115,58,32,98,121,116,101,115,13,10,67,111,110,116,101,
+ 110,116,45,76,101,110,103,116,104,58,32,49,57,48,55,49,13,10,75,101,101,
+ 112,45,65,108,105,118,101,58,32,116,105,109,101,111,117,116,61,49,53,44,32,
+ 109,97,120,61,49,48,48,13,10,67,111,110,110,101,99,116,105,111,110,58,32,
+ 75,101,101,112,45,65,108,105,118,101,13,10,67,111,110,116,101,110,116,45,
+ 84,121,112,101,58,32,105,109,97,103,101,47,103,105,102,13,10,13,10,0,0,0>>).