blob: adf69d61d51921ff5341fb6b3b55c7b36b36b45a [file] [log] [blame]
Harald Welte3bf7cb62011-04-03 00:25:34 +02001% M3UA in accordance with RFC4666 (http://tools.ietf.org/html/rfc4666)
2
3% (C) 2011 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(m3ua_codec).
21-author('Harald Welte <laforge@gnumonks.org>').
22-include("m3ua.hrl").
23
24-export([parse_m3ua_msg/1, encode_m3ua_msg/1]).
25
26%-compile(export_all).
27
28-compile({parse_transform, exprecs}).
29-export_records([m3ua_msg]).
30
31% compute the number of pad bits required after a binary parameter
32get_num_pad_bytes(BinLenBytes) ->
33 case BinLenBytes rem 4 of
34 0 -> 0;
35 Val -> 4 - Val
36 end.
37
38parse_m3ua_msg(DataBin) when is_binary(DataBin) ->
39 <<Version:8, _Reserved:8, MsgClass:8, MsgType:8, MsgLen:32/big, Remain/binary>> = DataBin,
40 OptList = parse_m3ua_opts(Remain),
41 #m3ua_msg{version = Version, msg_class = MsgClass, msg_type = MsgType,
42 msg_length = MsgLen-4, payload = OptList};
43parse_m3ua_msg(Data) when is_list(Data) ->
44 parse_m3ua_msg(list_to_binary(Data)).
45
46parse_m3ua_opts(OptBin) when is_binary(OptBin) ->
47 parse_m3ua_opts(OptBin, []).
48
Harald Welte18dcab02011-04-03 01:01:58 +020049parse_m3ua_opts(<<>>, OptList) when is_list(OptList) ->
50 OptList;
Harald Welte3bf7cb62011-04-03 00:25:34 +020051parse_m3ua_opts(OptBin, OptList) when is_binary(OptBin), is_list(OptList) ->
52 <<Tag:16/big, Length:16/big, Remain/binary>> = OptBin,
53 PadLen = get_num_pad_bytes(Length),
54 LengthNet = Length - 4,
55 <<CurOpt:LengthNet/binary, 0:PadLen/integer-unit:8, Remain2/binary>> = Remain,
56 parse_m3ua_opts(Remain2, OptList ++ [{Tag, CurOpt}]).
57
58encode_m3ua_msg(#m3ua_msg{version = Version, msg_class = MsgClass,
59 msg_type = MsgType, payload = OptList}) ->
60 OptBin = encode_m3ua_opts(OptList),
61 MsgLen = length(OptBin) + 8,
62 <<Version:4, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, OptBin/binary>>.
63
64encode_m3ua_opts(OptList) when is_list(OptList) ->
65 encode_m3ua_opts(OptList, <<>>).
66
67encode_m3ua_opts([], Bin) ->
68 Bin;
69encode_m3ua_opts([Head|Tail], Bin) ->
70 OptBin = encode_m3ua_opt(Head),
71 encode_m3ua_opts(Tail, <<Bin/binary, OptBin/binary>>).
72
73encode_m3ua_opt({Iei, Data}) when is_integer(Iei), is_binary(Data) ->
74 Length = length(Data) + 4,
75 PadLen = get_num_pad_bytes(Length),
76 <<Iei:16/big, Length:16/big, 0:PadLen/integer-unit:8, Data/binary>>.