Skip to content

Commit

Permalink
Merge pull request #3 from ates/master
Browse files Browse the repository at this point in the history
Add some specs
  • Loading branch information
msantos committed Oct 2, 2012
2 parents 2a8d511 + a5368d4 commit df39947
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,4 @@
ebin ebin
.test .test
.eunit
*.swp *.swp
41 changes: 26 additions & 15 deletions src/pkt.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -352,33 +352,44 @@ options(Offset, Payload) ->
%% %%
%% SCTP %% SCTP
%% %%
-spec sctp(binary()) -> {#sctp{}, []}.
sctp(<<SPort:16, DPort:16, VTag:32, Sum:32, Payload/binary>>) -> sctp(<<SPort:16, DPort:16, VTag:32, Sum:32, Payload/binary>>) ->
{#sctp{sport = SPort, dport = DPort, vtag = VTag, sum = Sum, SCTP = #sctp{
chunks = sctp_decode_chunks(Payload)}, []}. sport = SPort, dport = DPort, vtag = VTag,
sum = Sum, chunks = sctp_decode_chunks(Payload)
},
{SCTP, []}.


-spec sctp_decode_chunks(binary()) -> [#sctp_chunk{}].
sctp_decode_chunks(Chunks) -> sctp_decode_chunks(Chunks) ->
sctp_decode_chunks(Chunks, []). sctp_decode_chunks(Chunks, []).


sctp_decode_chunks(<<>>, Acc) -> Acc; -spec sctp_decode_chunks(binary(), list()) -> [#sctp_chunk{}].
sctp_decode_chunks(<<_Type:8, _Flags:8, Length:16, Rest/binary>>, Acc)
when Length =< 4 ->
sctp_decode_chunks(Rest, Acc);

sctp_decode_chunks(<<Type:8, Flags:8, Length:16, Rest/binary>>, Acc) -> sctp_decode_chunks(<<Type:8, Flags:8, Length:16, Rest/binary>>, Acc) ->
L = case Length rem 4 of L = case Length rem 4 of
0 -> Length - 4; 0 -> % No padding bytes
Pad -> Length + (4 - Pad) - 4 Length - 4;
Pad ->
Length + (4 - Pad) - 4
end, end,
case Rest of <<Payload:L/binary-unit:8, Tail/binary>> = Rest,
<<>> -> sctp_decode_chunks(Tail, [sctp_chunk(Type, Flags, Length, Payload) | Acc]);
Acc; sctp_decode_chunks(_, Acc) -> Acc.
_ ->
<<Payload:L/binary-unit:8, Tail/binary>> = Rest,
sctp_decode_chunks(Tail, [sctp_chunk(Type, Flags, L, Payload) | Acc])
end.


-spec sctp_chunk(byte(), byte(), non_neg_integer(), binary()) -> #sctp_chunk{}.
sctp_chunk(Ctype, Cflags, Clen, Payload) -> sctp_chunk(Ctype, Cflags, Clen, Payload) ->
#sctp_chunk{type=Ctype, flags=Cflags, len = Clen-4, #sctp_chunk{
payload=sctp_chunk_payload(Ctype, Payload)}. type = Ctype, flags = Cflags, len = Clen - 4,
payload = sctp_chunk_payload(Ctype, Payload)
}.


-spec sctp_chunk_payload(non_neg_integer(), binary()) -> #sctp_chunk_data{} | binary().
sctp_chunk_payload(0, <<Tsn:32, Sid:16, Ssn:16, Ppi:32, Data/binary>>) -> sctp_chunk_payload(0, <<Tsn:32, Sid:16, Ssn:16, Ppi:32, Data/binary>>) ->
#sctp_chunk_data{tsn=Tsn, sid=Sid, ssn=Ssn, ppi=Ppi, data=Data}; #sctp_chunk_data{tsn = Tsn, sid = Sid, ssn = Ssn, ppi = Ppi, data = Data};
sctp_chunk_payload(_, Data) -> sctp_chunk_payload(_, Data) ->
Data. Data.


Expand Down

0 comments on commit df39947

Please sign in to comment.