Harald Welte | ca80443 | 2024-06-05 16:41:15 +0200 | [diff] [blame^] | 1 | % M3UA in accordance with RFC4666 (https://tools.ietf.org/html/rfc4666) |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 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 |
Harald Welte | ca80443 | 2024-06-05 16:41:15 +0200 | [diff] [blame^] | 18 | % along with this program. If not, see <https://www.gnu.org/licenses/>. |
Harald Welte | f8bf032 | 2012-04-16 13:10:47 +0200 | [diff] [blame] | 19 | % |
| 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 |
Harald Welte | ca80443 | 2024-06-05 16:41:15 +0200 | [diff] [blame^] | 24 | % Ericsson on https://www.erlang.org (or a modified version of these |
Harald Welte | f8bf032 | 2012-04-16 13:10:47 +0200 | [diff] [blame] | 25 | % libraries), containing parts covered by the terms of the Erlang Public |
Harald Welte | ca80443 | 2024-06-05 16:41:15 +0200 | [diff] [blame^] | 26 | % License (https://www.erlang.org/EPLICENSE), the licensors of this |
Harald Welte | f8bf032 | 2012-04-16 13:10:47 +0200 | [diff] [blame] | 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 Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 33 | |
| 34 | -module(m3ua_codec). |
| 35 | -author('Harald Welte <laforge@gnumonks.org>'). |
| 36 | -include("m3ua.hrl"). |
Harald Welte | f2e7cb5 | 2011-04-03 01:13:48 +0200 | [diff] [blame] | 37 | -include("mtp3.hrl"). |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 38 | |
| 39 | -export([parse_m3ua_msg/1, encode_m3ua_msg/1]). |
| 40 | |
| 41 | %-compile(export_all). |
| 42 | |
| 43 | -compile({parse_transform, exprecs}). |
| 44 | -export_records([m3ua_msg]). |
| 45 | |
| 46 | % compute the number of pad bits required after a binary parameter |
| 47 | get_num_pad_bytes(BinLenBytes) -> |
| 48 | case BinLenBytes rem 4 of |
| 49 | 0 -> 0; |
| 50 | Val -> 4 - Val |
| 51 | end. |
| 52 | |
| 53 | parse_m3ua_msg(DataBin) when is_binary(DataBin) -> |
| 54 | <<Version:8, _Reserved:8, MsgClass:8, MsgType:8, MsgLen:32/big, Remain/binary>> = DataBin, |
| 55 | OptList = parse_m3ua_opts(Remain), |
| 56 | #m3ua_msg{version = Version, msg_class = MsgClass, msg_type = MsgType, |
| 57 | msg_length = MsgLen-4, payload = OptList}; |
| 58 | parse_m3ua_msg(Data) when is_list(Data) -> |
| 59 | parse_m3ua_msg(list_to_binary(Data)). |
| 60 | |
| 61 | parse_m3ua_opts(OptBin) when is_binary(OptBin) -> |
| 62 | parse_m3ua_opts(OptBin, []). |
| 63 | |
Harald Welte | 18dcab0 | 2011-04-03 01:01:58 +0200 | [diff] [blame] | 64 | parse_m3ua_opts(<<>>, OptList) when is_list(OptList) -> |
| 65 | OptList; |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 66 | parse_m3ua_opts(OptBin, OptList) when is_binary(OptBin), is_list(OptList) -> |
| 67 | <<Tag:16/big, Length:16/big, Remain/binary>> = OptBin, |
| 68 | PadLen = get_num_pad_bytes(Length), |
| 69 | LengthNet = Length - 4, |
| 70 | <<CurOpt:LengthNet/binary, 0:PadLen/integer-unit:8, Remain2/binary>> = Remain, |
Harald Welte | f2e7cb5 | 2011-04-03 01:13:48 +0200 | [diff] [blame] | 71 | NewOpt = parse_m3ua_opt(Tag, CurOpt), |
| 72 | parse_m3ua_opts(Remain2, OptList ++ [NewOpt]). |
| 73 | |
| 74 | parse_m3ua_opt(Opt = ?M3UA_IEI_PROTOCOL_DATA, MsgBin) when is_binary(MsgBin) -> |
| 75 | <<Opc:32/big, Dpc:32/big, Si:8, Ni:8, Mp:8, Sls:8, Payload/binary>> = MsgBin, |
Harald Welte | ce070ef | 2011-04-14 21:52:01 +0200 | [diff] [blame] | 76 | % The idea is to hand back a #mtp3_msg{} to make upper layers beyond |
| 77 | % MTP-TRANSFR.{ind,req} unaware of a MTP3 or M3UA lower layer |
Harald Welte | f2e7cb5 | 2011-04-03 01:13:48 +0200 | [diff] [blame] | 78 | {Opt, #mtp3_msg{network_ind = Ni, service_ind = Si, |
| 79 | routing_label = #mtp3_routing_label{sig_link_sel = Sls, |
| 80 | origin_pc = Opc, |
| 81 | dest_pc = Dpc}, |
Harald Welte | 9586f95 | 2011-04-03 01:43:38 +0200 | [diff] [blame] | 82 | payload = Payload, m3ua_mp = Mp}}; |
Harald Welte | f2e7cb5 | 2011-04-03 01:13:48 +0200 | [diff] [blame] | 83 | parse_m3ua_opt(Opt, Msg) -> |
| 84 | {Opt, Msg}. |
| 85 | |
| 86 | |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 87 | |
| 88 | encode_m3ua_msg(#m3ua_msg{version = Version, msg_class = MsgClass, |
| 89 | msg_type = MsgType, payload = OptList}) -> |
| 90 | OptBin = encode_m3ua_opts(OptList), |
Harald Welte | d298d0b | 2011-04-03 01:17:40 +0200 | [diff] [blame] | 91 | MsgLen = byte_size(OptBin) + 8, |
Harald Welte | 0dc999f | 2011-04-03 01:36:37 +0200 | [diff] [blame] | 92 | <<Version:8, 0:8, MsgClass:8, MsgType:8, MsgLen:32/big, OptBin/binary>>. |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 93 | |
| 94 | encode_m3ua_opts(OptList) when is_list(OptList) -> |
| 95 | encode_m3ua_opts(OptList, <<>>). |
| 96 | |
| 97 | encode_m3ua_opts([], Bin) -> |
| 98 | Bin; |
Harald Welte | 0dc999f | 2011-04-03 01:36:37 +0200 | [diff] [blame] | 99 | encode_m3ua_opts([{Iei, Attr}|Tail], Bin) -> |
| 100 | OptBin = encode_m3ua_opt(Iei, Attr), |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 101 | encode_m3ua_opts(Tail, <<Bin/binary, OptBin/binary>>). |
| 102 | |
Harald Welte | 0dc999f | 2011-04-03 01:36:37 +0200 | [diff] [blame] | 103 | encode_m3ua_opt(?M3UA_IEI_PROTOCOL_DATA, Mtp3) when is_record(Mtp3, mtp3_msg) -> |
Harald Welte | f2e7cb5 | 2011-04-03 01:13:48 +0200 | [diff] [blame] | 104 | #mtp3_msg{network_ind = Ni, service_ind = Si, |
| 105 | routing_label = #mtp3_routing_label{sig_link_sel = Sls, |
Harald Welte | eecba25 | 2011-12-08 12:02:02 +0100 | [diff] [blame] | 106 | origin_pc = OpcIn, |
| 107 | dest_pc = DpcIn}, |
Harald Welte | 9586f95 | 2011-04-03 01:43:38 +0200 | [diff] [blame] | 108 | payload = Payload, m3ua_mp = Mp} = Mtp3, |
Harald Welte | eecba25 | 2011-12-08 12:02:02 +0100 | [diff] [blame] | 109 | Opc = osmo_util:pointcode2int(OpcIn), |
| 110 | Dpc = osmo_util:pointcode2int(DpcIn), |
Harald Welte | ce070ef | 2011-04-14 21:52:01 +0200 | [diff] [blame] | 111 | case Mp of |
| 112 | undefined -> MpD = 0; |
| 113 | _ -> MpD = Mp |
| 114 | end, |
| 115 | PayBin = <<Opc:32/big, Dpc:32/big, Si:8, Ni:8, MpD:8, Sls:8, Payload/binary>>, |
Harald Welte | 0dc999f | 2011-04-03 01:36:37 +0200 | [diff] [blame] | 116 | encode_m3ua_opt(?M3UA_IEI_PROTOCOL_DATA, PayBin); |
| 117 | encode_m3ua_opt(Iei, Data) when is_integer(Iei), is_binary(Data) -> |
Harald Welte | d298d0b | 2011-04-03 01:17:40 +0200 | [diff] [blame] | 118 | Length = byte_size(Data) + 4, |
Harald Welte | 3bf7cb6 | 2011-04-03 00:25:34 +0200 | [diff] [blame] | 119 | PadLen = get_num_pad_bytes(Length), |
Harald Welte | 0dc999f | 2011-04-03 01:36:37 +0200 | [diff] [blame] | 120 | <<Iei:16/big, Length:16/big, Data/binary, 0:PadLen/integer-unit:8>>. |