Skip to content

Commit

Permalink
Rename pkt_tcp:tcp_options/1 to options/1
Browse files Browse the repository at this point in the history
Also remove pkt:tcp_options/1 for now. Modify the test to call
pkt_tcp:options/1 directly.
  • Loading branch information
msantos committed Oct 21, 2013
1 parent 73e0535 commit 304fe58
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 51 deletions.
4 changes: 0 additions & 4 deletions src/pkt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
ipv6/1,
proto/1,
tcp/1,
tcp_options/1,
udp/1,
sctp/1,
dlt/1
Expand Down Expand Up @@ -186,9 +185,6 @@ gre(N) ->
tcp(N) ->
pkt_tcp:codec(N).

tcp_options(N) ->
pkt_tcp:tcp_options(N).

%%
%% SCTP
%%
Expand Down
8 changes: 2 additions & 6 deletions src/pkt_ipv4.erl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ codec(
DA1:8, DA2:8, DA3:8, DA4:8,
Rest/binary>>
) when HL >= 5 ->
{Opt, Payload} = options(HL, Rest),
OptLen = (HL - 5) * 4,
<<Opt:OptLen/binary, Payload/binary>> = Rest,
{#ipv4{
hl = HL, tos = ToS, len = Len,
id = Id, df = DF, mf = MF,
Expand All @@ -67,8 +68,3 @@ codec(#ipv4{
SA1:8, SA2:8, SA3:8, SA4:8,
DA1:8, DA2:8, DA3:8, DA4:8,
Opt/binary, 0:Pad>>.

options(Offset, Binary) ->
Length = (Offset - 5) * 4,
<<Options:Length/binary, Payload/binary>> = Binary,
{Options, Payload}.
76 changes: 36 additions & 40 deletions src/pkt_tcp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

-export([
codec/1,
tcp_options/1
options/1
]).

codec(
Expand All @@ -46,7 +46,8 @@ codec(
Sum:16, Urp:16,
Rest/binary>>
) when Off >= 5 ->
{Opt, Payload} = options(Off, Rest),
OptLen = (Off - 5) * 4,
<<Opt:OptLen/binary, Payload/binary>> = Rest,
{#tcp{
sport = SPort, dport = DPort,
seqno = SeqNo,
Expand Down Expand Up @@ -74,61 +75,56 @@ codec(#tcp{
Sum:16, Urp:16,
Opt/binary, 0:Pad>>.

options(Offset, Binary) ->
Length = (Offset - 5) * 4,
<<Options:Length/binary, Payload/binary>> = Binary,
{Options, Payload}.

%% @doc Used to decoding or encoding the TCP options.
-spec tcp_options(binary()) -> [proplists:property()].
tcp_options(Options) ->
tcp_options(Options, []).
-spec options(binary()) -> [proplists:property()].
options(Options) ->
options(Options, []).

%% Decoding routines
tcp_options(<<>>, Acc) ->
options(<<>>, Acc) ->
lists:reverse(Acc); % Return list of the options in the correct order
%% Kind - 0, End of Option List (RFC 793)
tcp_options(<<0:8, _Rest/binary>>, Acc) ->
tcp_options(<<>>, [{eol, []} | Acc]);
options(<<0:8, _Rest/binary>>, Acc) ->
options(<<>>, [{eol, []} | Acc]);
%% Kind - 1, No-Operation (RFC 793)
tcp_options(<<1:8, Rest/binary>>, Acc) ->
tcp_options(Rest, [{nop, []} | Acc]);
options(<<1:8, Rest/binary>>, Acc) ->
options(Rest, [{nop, []} | Acc]);
%% Kind - 2, Length - 4, Maximum Segment Size (RFC 793)
tcp_options(<<2:8, 4:8, MSSValue:16, Rest/binary>>, Acc) ->
tcp_options(Rest, [{maximum_segment_size, MSSValue} | Acc]);
options(<<2:8, 4:8, MSSValue:16, Rest/binary>>, Acc) ->
options(Rest, [{maximum_segment_size, MSSValue} | Acc]);
%% Kind - 3, Length - 3, WSOPT - Window Scale (RFC 1323)
%% Multiplier is calculated as 1 bsl ShiftCount
tcp_options(<<3:8, 3:8, ShiftCount:8, Rest/binary>>, Acc) ->
tcp_options(Rest, [{window_scale, ShiftCount} | Acc]);
options(<<3:8, 3:8, ShiftCount:8, Rest/binary>>, Acc) ->
options(Rest, [{window_scale, ShiftCount} | Acc]);
%% Kind - 4, Length - 2, SACK Permitted (RFC 2018)
tcp_options(<<4:8, 2:8, Rest/binary>>, Acc) ->
tcp_options(Rest, [{sack_permitted, true} | Acc]);
options(<<4:8, 2:8, Rest/binary>>, Acc) ->
options(Rest, [{sack_permitted, true} | Acc]);
%% Kind - 5, Length - variable, SACK (RFC 2018)
tcp_options(<<5:8, Len:8, Rest/binary>>, Acc) ->
options(<<5:8, Len:8, Rest/binary>>, Acc) ->
Length = Len - 2,
<<Values:Length/binary, Rest1/binary>> = Rest,
Edges = [{{left_edge, Left}, {right_edge, Right}} || <<Left:32, Right:32>> <= Values],
tcp_options(Rest1, [{sack, Edges} | Acc]);
options(Rest1, [{sack, Edges} | Acc]);
%% Kind - 8, Length - 10, TSOPT - Time Stamp Option (RFC 1072, RFC 6247)
tcp_options(<<8:8, 10:8, Timestamp:32, TimestampEchoReply:32, Rest/binary>>, Acc) ->
tcp_options(Rest, [{tsopt, [{timestamp, Timestamp}, {timestamp_echo_reply, TimestampEchoReply}]} | Acc]);
options(<<8:8, 10:8, Timestamp:32, TimestampEchoReply:32, Rest/binary>>, Acc) ->
options(Rest, [{tsopt, [{timestamp, Timestamp}, {timestamp_echo_reply, TimestampEchoReply}]} | Acc]);

%% Encoding routines
tcp_options([], Acc) ->
options([], Acc) ->
list_to_binary(lists:reverse(Acc));
tcp_options([{eol, []} | _Rest], Acc) ->
tcp_options([], [<<0:8>> | Acc]); % No more options after EOL must be
tcp_options([{nop, []} | Rest], Acc) ->
tcp_options(Rest, [<<1:8>> | Acc]);
tcp_options([{maximum_segment_size, MSSValue} | Rest], Acc) ->
tcp_options(Rest, [<<2:8, 4:8, MSSValue:16>> | Acc]);
tcp_options([{window_scale, ShiftCount} | Rest], Acc) ->
tcp_options(Rest, [<<3:8, 3:8, ShiftCount:8>> | Acc]);
tcp_options([{sack_permitted, true} | Rest], Acc) ->
tcp_options(Rest, [<<4:8, 2:8>> | Acc]);
tcp_options([{sack, Values} | Rest], Acc) ->
options([{eol, []} | _Rest], Acc) ->
options([], [<<0:8>> | Acc]); % No more options after EOL must be
options([{nop, []} | Rest], Acc) ->
options(Rest, [<<1:8>> | Acc]);
options([{maximum_segment_size, MSSValue} | Rest], Acc) ->
options(Rest, [<<2:8, 4:8, MSSValue:16>> | Acc]);
options([{window_scale, ShiftCount} | Rest], Acc) ->
options(Rest, [<<3:8, 3:8, ShiftCount:8>> | Acc]);
options([{sack_permitted, true} | Rest], Acc) ->
options(Rest, [<<4:8, 2:8>> | Acc]);
options([{sack, Values} | Rest], Acc) ->
Edges = list_to_binary([<<Left:32, Right:32>> || {{left_edge, Left}, {right_edge, Right}} <- Values]),
Length = byte_size(Edges) + 2,
tcp_options(Rest, [<<5:8, Length:8, Edges/binary>> | Acc]);
tcp_options([{tsopt, [{timestamp, Timestamp}, {timestamp_echo_reply, TimestampEchoReply}]} | Rest], Acc) ->
tcp_options(Rest, [<<8:8, 10:8, Timestamp:32, TimestampEchoReply:32>> | Acc]).
options(Rest, [<<5:8, Length:8, Edges/binary>> | Acc]);
options([{tsopt, [{timestamp, Timestamp}, {timestamp_echo_reply, TimestampEchoReply}]} | Rest], Acc) ->
options(Rest, [<<8:8, 10:8, Timestamp:32, TimestampEchoReply:32>> | Acc]).
2 changes: 1 addition & 1 deletion test/pkt_tcp_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tcp_decode_encode() ->
Packet = <<0,80,217,184,222,13,22,43,241,75,9,12,176,18,17,4,140,86,
0,0,2,4,5,172,1, 3,3,0,1,1,8,10,190,15,172,236,0,64,161,73,4,2,0,0>>,
{TCP, <<>>} = pkt:tcp(Packet),
TCP1 = TCP#tcp{opt = pkt:tcp_options(pkt:tcp_options(TCP#tcp.opt))},
TCP1 = TCP#tcp{opt = pkt_tcp:options(pkt_tcp:options(TCP#tcp.opt))},
?_assertEqual(Packet, pkt:tcp(TCP1)).

tcp_checksum4() ->
Expand Down

0 comments on commit 304fe58

Please sign in to comment.