Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3 from ates/master

Add some specs
  • Loading branch information...
commit df39947d2115a2c385723751bb52ae53830a1744 2 parents 2a8d511 + a5368d4
@msantos authored
Showing with 27 additions and 15 deletions.
  1. +1 −0  .gitignore
  2. +26 −15 src/pkt.erl
View
1  .gitignore
@@ -1,3 +1,4 @@
ebin
.test
+.eunit
*.swp
View
41 src/pkt.erl
@@ -352,33 +352,44 @@ options(Offset, Payload) ->
%%
%% SCTP
%%
+-spec sctp(binary()) -> {#sctp{}, []}.
sctp(<<SPort:16, DPort:16, VTag:32, Sum:32, Payload/binary>>) ->
- {#sctp{sport = SPort, dport = DPort, vtag = VTag, sum = Sum,
- chunks = sctp_decode_chunks(Payload)}, []}.
+ SCTP = #sctp{
+ 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(<<>>, 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) ->
L = case Length rem 4 of
- 0 -> Length - 4;
- Pad -> Length + (4 - Pad) - 4
+ 0 -> % No padding bytes
+ Length - 4;
+ Pad ->
+ Length + (4 - Pad) - 4
end,
- case Rest of
- <<>> ->
- Acc;
- _ ->
- <<Payload:L/binary-unit:8, Tail/binary>> = Rest,
- sctp_decode_chunks(Tail, [sctp_chunk(Type, Flags, L, Payload) | Acc])
- end.
+ <<Payload:L/binary-unit:8, Tail/binary>> = Rest,
+ sctp_decode_chunks(Tail, [sctp_chunk(Type, Flags, Length, Payload) | Acc]);
+sctp_decode_chunks(_, Acc) -> Acc.
+-spec sctp_chunk(byte(), byte(), non_neg_integer(), binary()) -> #sctp_chunk{}.
sctp_chunk(Ctype, Cflags, Clen, Payload) ->
- #sctp_chunk{type=Ctype, flags=Cflags, len = Clen-4,
- payload=sctp_chunk_payload(Ctype, Payload)}.
+ #sctp_chunk{
+ 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_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) ->
Data.
Please sign in to comment.
Something went wrong with that request. Please try again.