Harald Welte | 1f6a035 | 2011-01-15 20:07:45 +0100 | [diff] [blame] | 1 | % RFC 3331 MTP2 User Adaption Layer coding / decoding |
| 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(m2ua_codec). |
| 21 | -author('Harald Welte <laforge@gnumonks.org>'). |
| 22 | -include("m2ua.hrl"). |
| 23 | |
| 24 | -export([parse_m2ua_msg/1, encode_m2ua_msg/1]). |
| 25 | |
Harald Welte | 9c9553c | 2011-01-18 18:24:09 +0100 | [diff] [blame] | 26 | % compute the number of pad bits required after a binary parameter |
| 27 | get_num_pad_bytes(BinLenBytes) -> |
| 28 | case BinLenBytes rem 4 of |
| 29 | 0 -> 0; |
| 30 | Val -> 4 - Val |
| 31 | end. |
| 32 | |
Harald Welte | 1f6a035 | 2011-01-15 20:07:45 +0100 | [diff] [blame] | 33 | % parse a binary chunk of options into an options proplist |
Harald Welte | 9c9553c | 2011-01-18 18:24:09 +0100 | [diff] [blame] | 34 | parse_m2ua_opts(<<>>, OptList) when is_list(OptList) -> |
Harald Welte | 1f6a035 | 2011-01-15 20:07:45 +0100 | [diff] [blame] | 35 | OptList; |
Harald Welte | 9c9553c | 2011-01-18 18:24:09 +0100 | [diff] [blame] | 36 | parse_m2ua_opts(OptBin, OptList) when is_list(OptList) -> |
Harald Welte | 1f6a035 | 2011-01-15 20:07:45 +0100 | [diff] [blame] | 37 | <<Tag:16/big, LengthIncHdr:16/big, Remain/binary>> = OptBin, |
| 38 | Length = LengthIncHdr - 4, |
Harald Welte | 9c9553c | 2011-01-18 18:24:09 +0100 | [diff] [blame] | 39 | PadLength = get_num_pad_bytes(Length), |
| 40 | %io:format("Tag ~w, LenInHdr ~w, Len ~w, PadLen ~w, Remain ~w(~p)~n", |
| 41 | % [Tag, LengthIncHdr, Length, PadLength, byte_size(Remain), Remain]), |
| 42 | <<Value:Length/binary, PadNextOpts/binary>> = Remain, |
| 43 | % this is ridiculous, we cannot use "<<Value:Length/binary, |
| 44 | % 0:PadLength, Remain/binary>>" as the last part would not match an |
| 45 | % empty binary <<>> anymore. Without the "0:PadLengh" this works |
| 46 | % perfectly fine. Now we need some complicated construct and check if |
| 47 | % the resulting list would be empty :(( |
| 48 | if |
| 49 | byte_size(PadNextOpts) > PadLength -> |
| 50 | <<0:PadLength/integer-unit:8, NextOpts/binary>> = PadNextOpts; |
| 51 | true -> |
| 52 | NextOpts = <<>> |
| 53 | end, |
Harald Welte | 1f6a035 | 2011-01-15 20:07:45 +0100 | [diff] [blame] | 54 | NewOpt = {Tag, {Length, Value}}, |
Harald Welte | 9c9553c | 2011-01-18 18:24:09 +0100 | [diff] [blame] | 55 | parse_m2ua_opts(NextOpts, OptList ++ [NewOpt]). |
Harald Welte | 1f6a035 | 2011-01-15 20:07:45 +0100 | [diff] [blame] | 56 | |
| 57 | % parse a single M2UA message |
| 58 | parse_m2ua_msgt(_, _, _, Remain) -> |
| 59 | parse_m2ua_opts(Remain, []). |
| 60 | |
| 61 | % parse a M2UA message binary into a record |
| 62 | parse_m2ua_msg(DataBin) when is_binary(DataBin) -> |
| 63 | <<1:8, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, Remain/binary>> = DataBin, |
| 64 | Parsed = parse_m2ua_msgt(MsgClass, MsgType, MsgLen, Remain), |
| 65 | {ok, #m2ua_msg{msg_class = MsgClass, msg_type = MsgType, parameters = Parsed}}. |
| 66 | |
| 67 | |
| 68 | |
| 69 | % encode a single option |
| 70 | encode_m2ua_opt({OptNum, {DataBinLen, DataBin}}) when is_integer(OptNum) -> |
| 71 | LengthIncHdr = DataBinLen + 4, |
Harald Welte | 9c9553c | 2011-01-18 18:24:09 +0100 | [diff] [blame] | 72 | PadLength = get_num_pad_bytes(DataBinLen), |
| 73 | case PadLength of |
| 74 | 0 -> <<OptNum:16/big, LengthIncHdr:16/big, DataBin/binary>>; |
| 75 | _ -> <<OptNum:16/big, LengthIncHdr:16/big, DataBin/binary, 0:PadLength/integer-unit:8>> |
| 76 | end. |
Harald Welte | 1f6a035 | 2011-01-15 20:07:45 +0100 | [diff] [blame] | 77 | |
| 78 | % encode a list of options |
| 79 | encode_m2ua_opts([], OptEnc) -> |
Harald Welte | 9c9553c | 2011-01-18 18:24:09 +0100 | [diff] [blame] | 80 | OptEnc; |
Harald Welte | 1f6a035 | 2011-01-15 20:07:45 +0100 | [diff] [blame] | 81 | encode_m2ua_opts([CurOpt|OptPropList], OptEnc) -> |
| 82 | CurOptEnc = encode_m2ua_opt(CurOpt), |
Harald Welte | 9c9553c | 2011-01-18 18:24:09 +0100 | [diff] [blame] | 83 | encode_m2ua_opts(OptPropList, <<OptEnc/binary, CurOptEnc/binary>>). |
Harald Welte | 1f6a035 | 2011-01-15 20:07:45 +0100 | [diff] [blame] | 84 | |
| 85 | |
| 86 | % encode a particular message type |
| 87 | encode_m2ua_msgt(MsgClass, MsgType, Params) -> |
| 88 | OptBin = encode_m2ua_opts(Params, <<>>), |
Harald Welte | 9c9553c | 2011-01-18 18:24:09 +0100 | [diff] [blame] | 89 | MsgLenIncHdr = 8 + byte_size(OptBin), |
Harald Welte | 1f6a035 | 2011-01-15 20:07:45 +0100 | [diff] [blame] | 90 | <<1:8, 0:8, MsgClass:8, MsgType:8, MsgLenIncHdr:32/big, OptBin/binary>>. |
| 91 | |
| 92 | % encode a message from record to binary |
| 93 | encode_m2ua_msg(#m2ua_msg{msg_class = MsgClass, msg_type = MsgType, parameters = Params}) -> |
| 94 | encode_m2ua_msgt(MsgClass, MsgType, Params). |