blob: 7fb3103554cc798bf76dc1aef3f6ff75b0f941b2 [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/>.
Harald Weltef8bf0322012-04-16 13:10:47 +020019%
20% Additional Permission under GNU AGPL version 3 section 7:
21%
22% If you modify this Program, or any covered work, by linking or
23% combining it with runtime libraries of Erlang/OTP as released by
24% Ericsson on http://www.erlang.org (or a modified version of these
25% libraries), containing parts covered by the terms of the Erlang Public
26% License (http://www.erlang.org/EPLICENSE), the licensors of this
27% Program grant you additional permission to convey the resulting work
28% without the need to license the runtime libraries of Erlang/OTP under
29% the GNU Affero General Public License. Corresponding Source for a
30% non-source form of such a combination shall include the source code
31% for the parts of the runtime libraries of Erlang/OTP used as well as
32% that of the covered work.
Harald Welte26bdef22012-01-16 22:22:17 +010033
Harald Welte92e783d2012-04-01 19:52:01 +020034-module(xua_codec).
Harald Welte26bdef22012-01-16 22:22:17 +010035-author('Harald Welte <laforge@gnumonks.org>').
Harald Welte92e783d2012-04-01 19:52:01 +020036-include("xua.hrl").
Harald Welte26bdef22012-01-16 22:22:17 +010037
Harald Welte50dfc192012-01-17 15:11:37 +010038-export([parse_msg/1, encode_msg/1, parse_xua_opts/1, encode_xua_opts/1]).
Harald Welte26bdef22012-01-16 22:22:17 +010039
40parse_msg(DataBin) when is_binary(DataBin) ->
41 <<Version:8, _Reserved:8, MsgClass:8, MsgType:8, MsgLen:32/big, Remain/binary>> = DataBin,
Harald Welte4d610992012-01-18 14:10:35 +010042 RemainLen = MsgLen - 4,
Harald Welte50dfc192012-01-17 15:11:37 +010043 OptList = parse_xua_opts(Remain),
Harald Welte92e783d2012-04-01 19:52:01 +020044 #xua_msg{version = Version, msg_class = MsgClass, msg_type = MsgType,
Harald Welte4d610992012-01-18 14:10:35 +010045 payload = OptList};
Harald Welte26bdef22012-01-16 22:22:17 +010046parse_msg(Data) when is_list(Data) ->
47 parse_msg(list_to_binary(Data)).
48
Harald Welte50dfc192012-01-17 15:11:37 +010049parse_xua_opts(OptBin) when is_binary(OptBin) ->
50 parse_xua_opts(OptBin, []).
Harald Welte26bdef22012-01-16 22:22:17 +010051
Harald Welte50dfc192012-01-17 15:11:37 +010052parse_xua_opts(<<>>, OptList) when is_list(OptList) ->
Harald Welte26bdef22012-01-16 22:22:17 +010053 OptList;
Harald Welte50dfc192012-01-17 15:11:37 +010054parse_xua_opts(OptBin, OptList) when is_binary(OptBin), is_list(OptList) ->
Harald Welte26bdef22012-01-16 22:22:17 +010055 <<Tag:16/big, LengthIncHdr:16/big, Remain/binary>> = OptBin,
56 Length = LengthIncHdr - 4,
57 PadLength = get_num_pad_bytes(Length),
58 %io:format("Tag ~w, LenInHdr ~w, Len ~w, PadLen ~w, Remain ~w(~p)~n",
59 % [Tag, LengthIncHdr, Length, PadLength, byte_size(Remain), Remain]),
60 <<Value:Length/binary, PadNextOpts/binary>> = Remain,
61 % this is ridiculous, we cannot use "<<Value:Length/binary,
62 % 0:PadLength, Remain/binary>>" as the last part would not match an
63 % empty binary <<>> anymore. Without the "0:PadLengh" this works
64 % perfectly fine. Now we need some complicated construct and check if
65 % the resulting list would be empty :((
66 if
67 byte_size(PadNextOpts) > PadLength ->
68 <<0:PadLength/integer-unit:8, NextOpts/binary>> = PadNextOpts;
69 true ->
70 NextOpts = <<>>
71 end,
72 NewOpt = {Tag, {Length, Value}},
Harald Welte50dfc192012-01-17 15:11:37 +010073 parse_xua_opts(NextOpts, OptList ++ [NewOpt]).
Harald Welte26bdef22012-01-16 22:22:17 +010074
Harald Welte26bdef22012-01-16 22:22:17 +010075
76
Harald Welte92e783d2012-04-01 19:52:01 +020077encode_msg(#xua_msg{version = Version, msg_class = MsgClass,
Harald Welte26bdef22012-01-16 22:22:17 +010078 msg_type = MsgType, payload = OptList}) ->
Harald Welte50dfc192012-01-17 15:11:37 +010079 OptBin = encode_xua_opts(OptList),
Harald Welte26bdef22012-01-16 22:22:17 +010080 MsgLen = byte_size(OptBin) + 8,
81 <<Version:8, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, OptBin/binary>>.
82
Harald Welte50dfc192012-01-17 15:11:37 +010083encode_xua_opts(OptList) when is_list(OptList) ->
84 encode_xua_opts(OptList, <<>>).
Harald Welte26bdef22012-01-16 22:22:17 +010085
Harald Welte50dfc192012-01-17 15:11:37 +010086encode_xua_opts([], Bin) ->
Harald Welte26bdef22012-01-16 22:22:17 +010087 Bin;
Harald Welte50dfc192012-01-17 15:11:37 +010088encode_xua_opts([{Iei, Attr}|Tail], Bin) ->
Harald Welte7aff8fb2012-04-01 19:03:31 +020089 OptBin = encode_xua_opt(Iei, Attr),
Harald Welte50dfc192012-01-17 15:11:37 +010090 encode_xua_opts(Tail, <<Bin/binary, OptBin/binary>>).
Harald Welte26bdef22012-01-16 22:22:17 +010091
Harald Welte7aff8fb2012-04-01 19:03:31 +020092encode_xua_opt(Iei, {LenIn, Data}) when is_integer(Iei), is_binary(Data) ->
Harald Welte8ed441d2012-01-18 00:25:05 +010093 Length = LenIn + 4,
94 PadLen = get_num_pad_bytes(Length),
95 <<Iei:16/big, Length:16/big, Data/binary, 0:PadLen/integer-unit:8>>;
Harald Welte7aff8fb2012-04-01 19:03:31 +020096encode_xua_opt(Iei, Data) when is_integer(Iei), is_binary(Data) ->
Harald Welte26bdef22012-01-16 22:22:17 +010097 Length = byte_size(Data) + 4,
98 PadLen = get_num_pad_bytes(Length),
99 <<Iei:16/big, Length:16/big, Data/binary, 0:PadLen/integer-unit:8>>.
100
101% compute the number of pad bits required after a binary parameter
102get_num_pad_bytes(BinLenBytes) ->
103 case BinLenBytes rem 4 of
104 0 -> 0;
105 Val -> 4 - Val
106 end.