blob: bec88a567c856f997062f4e48f0a5a415c14e115 [file] [log] [blame]
Harald Welte26bdef22012-01-16 22:22:17 +01001% RFC 3868 SUA SCCP Adaption Layer coding / decoding
2
3% (C) 2012 by Harald Welte <laforge@gnumonks.org>
4%
5% All Rights Reserved
6%
7% This program is free software; you can redistribute it and/or modify
8% it under the terms of the GNU Affero General Public License as
9% published by the Free Software Foundation; either version 3 of the
10% License, or (at your option) any later version.
11%
12% This program is distributed in the hope that it will be useful,
13% but WITHOUT ANY WARRANTY; without even the implied warranty of
14% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15% GNU General Public License for more details.
16%
17% You should have received a copy of the GNU Affero General Public License
18% along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20-module(sua_codec).
21-author('Harald Welte <laforge@gnumonks.org>').
22-include("sua.hrl").
23
24-export([parse_msg/1, encode_msg/1]).
25
26parse_msg(DataBin) when is_binary(DataBin) ->
27 <<Version:8, _Reserved:8, MsgClass:8, MsgType:8, MsgLen:32/big, Remain/binary>> = DataBin,
28 OptList = parse_sua_opts(Remain),
29 #sua_msg{version = Version, msg_class = MsgClass, msg_type = MsgType,
30 msg_length = MsgLen-4, payload = OptList};
31parse_msg(Data) when is_list(Data) ->
32 parse_msg(list_to_binary(Data)).
33
34parse_sua_opts(OptBin) when is_binary(OptBin) ->
35 parse_sua_opts(OptBin, []).
36
37parse_sua_opts(<<>>, OptList) when is_list(OptList) ->
38 OptList;
39parse_sua_opts(OptBin, OptList) when is_binary(OptBin), is_list(OptList) ->
40 <<Tag:16/big, LengthIncHdr:16/big, Remain/binary>> = OptBin,
41 Length = LengthIncHdr - 4,
42 PadLength = get_num_pad_bytes(Length),
43 %io:format("Tag ~w, LenInHdr ~w, Len ~w, PadLen ~w, Remain ~w(~p)~n",
44 % [Tag, LengthIncHdr, Length, PadLength, byte_size(Remain), Remain]),
45 <<Value:Length/binary, PadNextOpts/binary>> = Remain,
46 % this is ridiculous, we cannot use "<<Value:Length/binary,
47 % 0:PadLength, Remain/binary>>" as the last part would not match an
48 % empty binary <<>> anymore. Without the "0:PadLengh" this works
49 % perfectly fine. Now we need some complicated construct and check if
50 % the resulting list would be empty :((
51 if
52 byte_size(PadNextOpts) > PadLength ->
53 <<0:PadLength/integer-unit:8, NextOpts/binary>> = PadNextOpts;
54 true ->
55 NextOpts = <<>>
56 end,
57 NewOpt = {Tag, {Length, Value}},
58 parse_sua_opts(NextOpts, OptList ++ [NewOpt]).
59
60parse_sua_opt(Opt, Msg) ->
61 {Opt, Msg}.
62
63
64encode_msg(#sua_msg{version = Version, msg_class = MsgClass,
65 msg_type = MsgType, payload = OptList}) ->
66 OptBin = encode_sua_opts(OptList),
67 MsgLen = byte_size(OptBin) + 8,
68 <<Version:8, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, OptBin/binary>>.
69
70encode_sua_opts(OptList) when is_list(OptList) ->
71 encode_sua_opts(OptList, <<>>).
72
73encode_sua_opts([], Bin) ->
74 Bin;
75encode_sua_opts([{Iei, Attr}|Tail], Bin) ->
76 OptBin = encode_sua_opt(Iei, Attr),
77 encode_sua_opts(Tail, <<Bin/binary, OptBin/binary>>).
78
79encode_sua_opt(Iei, Data) when is_integer(Iei), is_binary(Data) ->
80 Length = byte_size(Data) + 4,
81 PadLen = get_num_pad_bytes(Length),
82 <<Iei:16/big, Length:16/big, Data/binary, 0:PadLen/integer-unit:8>>.
83
84% compute the number of pad bits required after a binary parameter
85get_num_pad_bytes(BinLenBytes) ->
86 case BinLenBytes rem 4 of
87 0 -> 0;
88 Val -> 4 - Val
89 end.