blob: 9e2e203556deaa78c07ab6d2383acdba23d1d30d [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
Harald Welte50dfc192012-01-17 15:11:37 +010024-export([parse_msg/1, encode_msg/1, parse_xua_opts/1, encode_xua_opts/1]).
Harald Welte26bdef22012-01-16 22:22:17 +010025
26parse_msg(DataBin) when is_binary(DataBin) ->
27 <<Version:8, _Reserved:8, MsgClass:8, MsgType:8, MsgLen:32/big, Remain/binary>> = DataBin,
Harald Welte4d610992012-01-18 14:10:35 +010028 RemainLen = MsgLen - 4,
Harald Welte50dfc192012-01-17 15:11:37 +010029 OptList = parse_xua_opts(Remain),
Harald Welte26bdef22012-01-16 22:22:17 +010030 #sua_msg{version = Version, msg_class = MsgClass, msg_type = MsgType,
Harald Welte4d610992012-01-18 14:10:35 +010031 payload = OptList};
Harald Welte26bdef22012-01-16 22:22:17 +010032parse_msg(Data) when is_list(Data) ->
33 parse_msg(list_to_binary(Data)).
34
Harald Welte50dfc192012-01-17 15:11:37 +010035parse_xua_opts(OptBin) when is_binary(OptBin) ->
36 parse_xua_opts(OptBin, []).
Harald Welte26bdef22012-01-16 22:22:17 +010037
Harald Welte50dfc192012-01-17 15:11:37 +010038parse_xua_opts(<<>>, OptList) when is_list(OptList) ->
Harald Welte26bdef22012-01-16 22:22:17 +010039 OptList;
Harald Welte50dfc192012-01-17 15:11:37 +010040parse_xua_opts(OptBin, OptList) when is_binary(OptBin), is_list(OptList) ->
Harald Welte26bdef22012-01-16 22:22:17 +010041 <<Tag:16/big, LengthIncHdr:16/big, Remain/binary>> = OptBin,
42 Length = LengthIncHdr - 4,
43 PadLength = get_num_pad_bytes(Length),
44 %io:format("Tag ~w, LenInHdr ~w, Len ~w, PadLen ~w, Remain ~w(~p)~n",
45 % [Tag, LengthIncHdr, Length, PadLength, byte_size(Remain), Remain]),
46 <<Value:Length/binary, PadNextOpts/binary>> = Remain,
47 % this is ridiculous, we cannot use "<<Value:Length/binary,
48 % 0:PadLength, Remain/binary>>" as the last part would not match an
49 % empty binary <<>> anymore. Without the "0:PadLengh" this works
50 % perfectly fine. Now we need some complicated construct and check if
51 % the resulting list would be empty :((
52 if
53 byte_size(PadNextOpts) > PadLength ->
54 <<0:PadLength/integer-unit:8, NextOpts/binary>> = PadNextOpts;
55 true ->
56 NextOpts = <<>>
57 end,
58 NewOpt = {Tag, {Length, Value}},
Harald Welte50dfc192012-01-17 15:11:37 +010059 parse_xua_opts(NextOpts, OptList ++ [NewOpt]).
Harald Welte26bdef22012-01-16 22:22:17 +010060
61parse_sua_opt(Opt, Msg) ->
62 {Opt, Msg}.
63
64
65encode_msg(#sua_msg{version = Version, msg_class = MsgClass,
66 msg_type = MsgType, payload = OptList}) ->
Harald Welte50dfc192012-01-17 15:11:37 +010067 OptBin = encode_xua_opts(OptList),
Harald Welte26bdef22012-01-16 22:22:17 +010068 MsgLen = byte_size(OptBin) + 8,
69 <<Version:8, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, OptBin/binary>>.
70
Harald Welte50dfc192012-01-17 15:11:37 +010071encode_xua_opts(OptList) when is_list(OptList) ->
72 encode_xua_opts(OptList, <<>>).
Harald Welte26bdef22012-01-16 22:22:17 +010073
Harald Welte50dfc192012-01-17 15:11:37 +010074encode_xua_opts([], Bin) ->
Harald Welte26bdef22012-01-16 22:22:17 +010075 Bin;
Harald Welte50dfc192012-01-17 15:11:37 +010076encode_xua_opts([{Iei, Attr}|Tail], Bin) ->
Harald Welte26bdef22012-01-16 22:22:17 +010077 OptBin = encode_sua_opt(Iei, Attr),
Harald Welte50dfc192012-01-17 15:11:37 +010078 encode_xua_opts(Tail, <<Bin/binary, OptBin/binary>>).
Harald Welte26bdef22012-01-16 22:22:17 +010079
Harald Welte8ed441d2012-01-18 00:25:05 +010080encode_sua_opt(Iei, {LenIn, Data}) when is_integer(Iei), is_binary(Data) ->
81 Length = LenIn + 4,
82 PadLen = get_num_pad_bytes(Length),
83 <<Iei:16/big, Length:16/big, Data/binary, 0:PadLen/integer-unit:8>>;
Harald Welte26bdef22012-01-16 22:22:17 +010084encode_sua_opt(Iei, Data) when is_integer(Iei), is_binary(Data) ->
85 Length = byte_size(Data) + 4,
86 PadLen = get_num_pad_bytes(Length),
87 <<Iei:16/big, Length:16/big, Data/binary, 0:PadLen/integer-unit:8>>.
88
89% compute the number of pad bits required after a binary parameter
90get_num_pad_bytes(BinLenBytes) ->
91 case BinLenBytes rem 4 of
92 0 -> 0;
93 Val -> 4 - Val
94 end.