Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 1 | % ITU-T Q.71x SCCP Message coding / decoding |
| 2 | |
| 3 | % (C) 2010 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 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 |
| 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 Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 33 | |
| 34 | -module(sccp_codec). |
| 35 | -author('Harald Welte <laforge@gnumonks.org>'). |
| 36 | -include("sccp.hrl"). |
| 37 | |
Harald Welte | 9abbbad | 2011-04-21 12:19:41 +0200 | [diff] [blame] | 38 | -export([parse_sccp_msg/1, encode_sccp_msg/1, encode_sccp_msgt/2, |
| 39 | is_connectionless/1]). |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 40 | |
Harald Welte | 9baab6d | 2011-12-08 00:46:00 +0100 | [diff] [blame] | 41 | -export([gen_gt_helper/1, gen_addr_helper/2, gen_addr_helper/3]). |
| 42 | |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 43 | -compile(export_all). |
| 44 | |
Harald Welte | 2edaf55 | 2011-04-02 16:46:16 +0200 | [diff] [blame] | 45 | -compile({parse_transform, exprecs}). |
Harald Welte | 0f2f596 | 2011-04-04 15:59:49 +0200 | [diff] [blame] | 46 | -export_records([global_title, sccp_addr, sccp_msg]). |
Harald Welte | 2edaf55 | 2011-04-02 16:46:16 +0200 | [diff] [blame] | 47 | |
Harald Welte | d9c318f | 2011-12-10 22:17:11 +0100 | [diff] [blame] | 48 | binarify(In) when is_binary(In) -> |
| 49 | In; |
| 50 | binarify(In) when is_list(In) -> |
| 51 | list_to_binary(In). |
| 52 | |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 53 | parse_point_code(BinPC, PCind) when is_binary(BinPC) -> |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 54 | case PCind of |
| 55 | 1 -> |
Harald Welte | 9c3b1bb | 2011-10-12 17:00:34 +0200 | [diff] [blame] | 56 | <<PointCode:16/little, Remain/binary>> = BinPC; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 57 | _ -> |
| 58 | Remain = BinPC, |
Harald Welte | b5936ba | 2011-12-08 00:58:51 +0100 | [diff] [blame] | 59 | PointCode = undefined |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 60 | end, |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 61 | {Remain, PointCode}. |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 62 | |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 63 | parse_ssn(BinSSN, SSNind) -> |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 64 | case SSNind of |
| 65 | 1 -> |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 66 | <<SSN:8, Remain/binary>> = BinSSN; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 67 | _ -> |
| 68 | Remain = BinSSN, |
Harald Welte | b5936ba | 2011-12-08 00:58:51 +0100 | [diff] [blame] | 69 | SSN = undefined |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 70 | end, |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 71 | {Remain, SSN}. |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 72 | |
| 73 | enc_is_odd(Enc) -> |
| 74 | case Enc of |
| 75 | 1 -> 1; |
| 76 | _ -> 0 |
| 77 | end. |
| 78 | |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 79 | parse_gt(BinGT, GTind) -> |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 80 | case GTind of |
| 81 | ?SCCP_GTI_NO_GT -> |
Harald Welte | b5936ba | 2011-12-08 00:58:51 +0100 | [diff] [blame] | 82 | undefined; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 83 | ?SCCP_GTI_NAT_ONLY -> |
| 84 | % Figure 7/Q.713 |
| 85 | <<Odd:1, Nature:7, Digits/binary>> = BinGT, |
| 86 | PhoneNum = isup_codec:parse_isup_party(Digits, Odd), |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 87 | #global_title{gti = GTind, |
| 88 | nature_of_addr_ind = Nature, |
| 89 | phone_number = PhoneNum}; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 90 | ?SCCP_GTI_TT_ONLY -> |
| 91 | % Figure 9/Q.913 |
| 92 | <<TransType:8, Digits/binary>> = BinGT, |
| 93 | % Used in national interfaces only, we cannot parse Digits |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 94 | #global_title{gti = GTind, |
| 95 | trans_type = TransType, |
| 96 | phone_number = Digits}; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 97 | ?SCCP_GTI_TT_NP_ENC -> |
| 98 | % Figure 10/Q.713 |
| 99 | <<TransType:8, NumPlan:4, Enc:4, Digits/binary>> = BinGT, |
| 100 | PhoneNum = isup_codec:parse_isup_party(Digits, enc_is_odd(Enc)), |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 101 | #global_title{gti = GTind, |
Harald Welte | 2c67ac0 | 2012-01-18 08:49:45 +0100 | [diff] [blame] | 102 | trans_type = TransType, |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 103 | numbering_plan = NumPlan, |
| 104 | phone_number = PhoneNum}; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 105 | ?SCCP_GTI_TT_NP_ENC_NAT -> |
| 106 | % Figure 11/Q.713 |
| 107 | <<TransType:8, NumPlan:4, Enc:4, 0:1, Nature:7, Digits/binary>> = BinGT, |
| 108 | PhoneNum = isup_codec:parse_isup_party(Digits, enc_is_odd(Enc)), |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 109 | #global_title{gti = GTind, |
Harald Welte | 2c67ac0 | 2012-01-18 08:49:45 +0100 | [diff] [blame] | 110 | trans_type = TransType, |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 111 | numbering_plan = NumPlan, |
| 112 | nature_of_addr_ind = Nature, |
| 113 | phone_number = PhoneNum}; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 114 | _ -> |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 115 | BinGT |
| 116 | end. |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 117 | |
| 118 | % parse SCCP Address |
| 119 | parse_sccp_addr(BinAddr) when is_binary(BinAddr) -> |
| 120 | <<ResNatUse:1, RoutInd:1, GTind:4, SSNind:1, PCind:1, Remain/binary>> = BinAddr, |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 121 | {RemainPC, OptPC} = parse_point_code(Remain, PCind), |
| 122 | {RemainSSN, OptSSN} = parse_ssn(RemainPC, SSNind), |
| 123 | OptGT = parse_gt(RemainSSN, GTind), |
| 124 | #sccp_addr{res_nat_use = ResNatUse, route_on_ssn = RoutInd, |
| 125 | point_code = OptPC, ssn = OptSSN, global_title = OptGT}. |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 126 | |
| 127 | % parse SCCP Optional Part |
Harald Welte | 9dda4e1 | 2012-01-23 16:15:06 +0100 | [diff] [blame] | 128 | parse_sccp_opt(OptType, _OptLen, Content) -> |
| 129 | OptAtom = opt_to_atom(OptType), |
| 130 | {OptAtom, Content}. |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 131 | |
| 132 | parse_sccp_opts(<<>>, OptList) -> |
| 133 | % empty list |
| 134 | OptList; |
| 135 | parse_sccp_opts(<<0>>, OptList) -> |
| 136 | % end of options |
| 137 | OptList; |
| 138 | parse_sccp_opts(OptBin, OptList) -> |
| 139 | <<OptType, OptLen, Content:OptLen/binary, Remain/binary>> = OptBin, |
| 140 | NewOpt = parse_sccp_opt(OptType, OptLen, Content), |
| 141 | parse_sccp_opts(Remain, [NewOpt|OptList]). |
| 142 | |
Harald Welte | 9dda4e1 | 2012-01-23 16:15:06 +0100 | [diff] [blame] | 143 | |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 144 | % Parse incoming SCCP message, one function for every message type |
| 145 | parse_sccp_msgt(?SCCP_MSGT_CR, DataBin) -> |
| 146 | % first get the fixed part |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 147 | <<_:8, SrcLocalRef:24/big, PCOpt:4, ProtoClass:4, RemainVar/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 148 | % variable length fixed part |
| 149 | <<PtrVar:8, PtrOpt:8, _/binary>> = RemainVar, |
| 150 | CalledPartyLen = binary:at(RemainVar, PtrVar), |
| 151 | CalledParty = binary:part(RemainVar, PtrVar+1, CalledPartyLen), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 152 | CalledPartyDec = parse_sccp_addr(CalledParty), |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 153 | % optional part |
| 154 | OptBin = binary:part(RemainVar, 1 + PtrOpt, byte_size(RemainVar)-(1+PtrOpt)), |
| 155 | OptList = parse_sccp_opts(OptBin, []), |
| 156 | %OptList = [], |
| 157 | % build parsed list of message |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 158 | [{src_local_ref, SrcLocalRef},{protocol_class, {ProtoClass, PCOpt}}, |
| 159 | {called_party_addr, CalledPartyDec} | OptList]; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 160 | parse_sccp_msgt(?SCCP_MSGT_CC, DataBin) -> |
| 161 | % first get the fixed part |
Harald Welte | cbddf84 | 2012-01-24 22:58:01 +0100 | [diff] [blame] | 162 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big, PCOpt:4, ProtoClass:4, PtrOpt:8, Remain/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 163 | % optional part |
Harald Welte | cbddf84 | 2012-01-24 22:58:01 +0100 | [diff] [blame] | 164 | OptBin = binary:part(Remain, PtrOpt-1, byte_size(Remain)-(PtrOpt-1)), |
| 165 | OptList = parse_sccp_opts(OptBin, []), |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 166 | % build parsed list of message |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 167 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef}, |
| 168 | {protocol_class, {ProtoClass, PCOpt}} | OptList]; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 169 | parse_sccp_msgt(?SCCP_MSGT_CREF, DataBin) -> |
| 170 | % first get the fixed part |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 171 | <<_:8, DstLocalRef:24/big, RefusalCause:8, Remain/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 172 | % optional part |
| 173 | OptList = parse_sccp_opts(Remain, []), |
| 174 | % build parsed list of message |
| 175 | [{dst_local_ref, DstLocalRef},{refusal_cause, RefusalCause}|OptList]; |
| 176 | parse_sccp_msgt(?SCCP_MSGT_RLSD, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 177 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big, ReleaseCause:8, Remain/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 178 | % optional part |
| 179 | OptList = parse_sccp_opts(Remain, []), |
| 180 | % build parsed list of message |
| 181 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef},{release_cause, ReleaseCause}|OptList]; |
| 182 | parse_sccp_msgt(?SCCP_MSGT_RLC, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 183 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 184 | % build parsed list of message |
| 185 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef}]; |
| 186 | parse_sccp_msgt(?SCCP_MSGT_DT1, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 187 | <<_:8, DstLocalRef:24/big, SegmReass:8, DataPtr:8, Remain/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 188 | DataLen = binary:at(Remain, DataPtr-1), |
| 189 | UserData = binary:part(Remain, DataPtr-1+1, DataLen), |
| 190 | % build parsed list of message |
| 191 | [{dst_local_ref, DstLocalRef},{segm_reass, SegmReass},{user_data, UserData}]; |
| 192 | parse_sccp_msgt(?SCCP_MSGT_DT2, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 193 | <<_:8, DstLocalRef:24/big, SeqSegm:16, DataPtr:8, Remain/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 194 | DataLen = binary:at(Remain, DataPtr-1), |
| 195 | UserData = binary:part(Remain, DataPtr-1+1, DataLen), |
| 196 | % build parsed list of message |
| 197 | [{dst_local_ref, DstLocalRef},{seq_segm, SeqSegm},{user_data, UserData}]; |
| 198 | parse_sccp_msgt(?SCCP_MSGT_AK, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 199 | <<_:8, DstLocalRef:24/big, RxSeqnr:8, Credit:8>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 200 | [{dst_local_ref, DstLocalRef},{rx_seq_nr, RxSeqnr},{credit, Credit}]; |
| 201 | parse_sccp_msgt(?SCCP_MSGT_UDT, DataBin) -> |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 202 | <<_:8, PCOpt:4, ProtoClass:4, CalledPartyPtr:8, CallingPartyPtr:8, DataPtr:8, Remain/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 203 | % variable part |
| 204 | CalledPartyLen = binary:at(Remain, CalledPartyPtr-3), |
| 205 | CalledParty = binary:part(Remain, CalledPartyPtr-3+1, CalledPartyLen), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 206 | CalledPartyDec = parse_sccp_addr(CalledParty), |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 207 | CallingPartyLen = binary:at(Remain, CallingPartyPtr-2), |
| 208 | CallingParty = binary:part(Remain, CallingPartyPtr-2+1, CallingPartyLen), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 209 | CallingPartyDec = parse_sccp_addr(CallingParty), |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 210 | DataLen = binary:at(Remain, DataPtr-1), |
| 211 | UserData = binary:part(Remain, DataPtr-1+1, DataLen), |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 212 | [{protocol_class, {ProtoClass, PCOpt}},{called_party_addr, CalledPartyDec}, |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 213 | {calling_party_addr, CallingPartyDec},{user_data, UserData}]; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 214 | parse_sccp_msgt(?SCCP_MSGT_UDTS, DataBin) -> |
Harald Welte | 030f109 | 2011-03-11 19:08:07 +0100 | [diff] [blame] | 215 | <<_:8, ReturnCause:8, CalledPartyPtr:8, CallingPartyPtr:8, DataPtr:8, Remain/binary >> = DataBin, |
| 216 | % variable part |
| 217 | CalledPartyLen = binary:at(Remain, CalledPartyPtr-3), |
| 218 | CalledParty = binary:part(Remain, CalledPartyPtr-3+1, CalledPartyLen), |
| 219 | CalledPartyDec = parse_sccp_addr(CalledParty), |
| 220 | CallingPartyLen = binary:at(Remain, CallingPartyPtr-2), |
| 221 | CallingParty = binary:part(Remain, CallingPartyPtr-2+1, CallingPartyLen), |
| 222 | CallingPartyDec = parse_sccp_addr(CallingParty), |
| 223 | DataLen = binary:at(Remain, DataPtr-1), |
| 224 | UserData = binary:part(Remain, DataPtr-1+1, DataLen), |
| 225 | [{return_cause, ReturnCause},{called_party_addr, CalledPartyDec}, |
| 226 | {calling_party_addr, CallingPartyDec},{user_data, UserData}]; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 227 | parse_sccp_msgt(?SCCP_MSGT_ED, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 228 | <<_:8, DstLocalRef:24/big, DataPtr:8, Remain/binary>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 229 | DataLen = binary:at(Remain, DataPtr-1), |
| 230 | UserData = binary:part(Remain, DataPtr-1+1, DataLen), |
| 231 | [{dst_local_ref, DstLocalRef}, {user_data, UserData}]; |
| 232 | parse_sccp_msgt(?SCCP_MSGT_EA, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 233 | <<_:8, DstLocalRef:24/big>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 234 | [{dst_local_ref, DstLocalRef}]; |
| 235 | parse_sccp_msgt(?SCCP_MSGT_RSR, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 236 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big, ResetCause:8>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 237 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef},{reset_cause, ResetCause}]; |
| 238 | parse_sccp_msgt(?SCCP_MSGT_RSC, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 239 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 240 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef}]; |
| 241 | parse_sccp_msgt(?SCCP_MSGT_ERR, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 242 | <<_:8, DstLocalRef:24/big, ErrCause:8>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 243 | [{dst_local_ref, DstLocalRef},{error_cause, ErrCause}]; |
| 244 | parse_sccp_msgt(?SCCP_MSGT_IT, DataBin) -> |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 245 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big, PCOpt: 4, ProtoClass:4, SegmSeq:16, Credit:8>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 246 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef}, |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 247 | {protocol_class, {ProtoClass, PCOpt}},{seq_segm, SegmSeq},{credit, Credit}]. |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 248 | % FIXME: XUDT/XUDTS, LUDT/LUDTS |
| 249 | |
| 250 | % process one incoming SCCP message |
| 251 | parse_sccp_msg(DataBin) -> |
| 252 | MsgType = binary:first(DataBin), |
| 253 | Parsed = parse_sccp_msgt(MsgType, DataBin), |
| 254 | {ok, #sccp_msg{msg_type = MsgType, parameters = Parsed}}. |
| 255 | |
| 256 | % Encoding Part |
| 257 | |
Harald Welte | 0e1709c | 2011-02-06 22:17:53 +0100 | [diff] [blame] | 258 | gt_enc_by_odd(Odd) -> |
| 259 | if Odd == 1 -> |
| 260 | 1; |
| 261 | true -> |
| 262 | 2 |
| 263 | end. |
| 264 | |
Harald Welte | 5a1cf3c | 2011-04-14 21:55:13 +0200 | [diff] [blame] | 265 | encode_gt(undefined) -> |
| 266 | {?SCCP_GTI_NO_GT, <<>>}; |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 267 | encode_gt(#global_title{gti = GTind, phone_number = PhoneNum, |
| 268 | nature_of_addr_ind = Nature, |
Harald Welte | 2c67ac0 | 2012-01-18 08:49:45 +0100 | [diff] [blame] | 269 | trans_type = TransType, |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 270 | numbering_plan = NumPlan}) -> |
| 271 | case GTind of |
| 272 | ?SCCP_GTI_NO_GT -> |
| 273 | {GTind, <<>>}; |
| 274 | ?SCCP_GTI_NAT_ONLY -> |
| 275 | % Figure 7/Q.713 |
| 276 | {PhoneBin, OddEven} = isup_codec:encode_isup_party(PhoneNum), |
| 277 | {GTind, <<OddEven:1, Nature:7, PhoneBin/binary>>}; |
| 278 | ?SCCP_GTI_TT_ONLY -> |
| 279 | % Figure 9/Q.913 |
| 280 | % Used in national interfaces only, we cannot parse Digits |
| 281 | {GTind, <<TransType:8, PhoneNum/binary>>}; |
| 282 | ?SCCP_GTI_TT_NP_ENC -> |
| 283 | % Figure 10/Q.713 |
Harald Welte | 0e1709c | 2011-02-06 22:17:53 +0100 | [diff] [blame] | 284 | {PhoneBin, OddEven} = isup_codec:encode_isup_party(PhoneNum), |
| 285 | Enc = gt_enc_by_odd(OddEven), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 286 | {GTind, <<TransType:8, NumPlan:4, Enc:4, PhoneBin/binary>>}; |
| 287 | ?SCCP_GTI_TT_NP_ENC_NAT -> |
| 288 | % Figure 11/Q.713 |
Harald Welte | 0e1709c | 2011-02-06 22:17:53 +0100 | [diff] [blame] | 289 | {PhoneBin, OddEven} = isup_codec:encode_isup_party(PhoneNum), |
| 290 | Enc = gt_enc_by_odd(OddEven), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 291 | {GTind, <<TransType:8, NumPlan:4, Enc:4, 0:1, Nature:7, PhoneBin/binary>>} |
| 292 | end. |
| 293 | |
Harald Welte | c923a2a | 2012-01-23 14:13:05 +0100 | [diff] [blame] | 294 | encode_pc(undefined) -> |
| 295 | {0, <<>>}; |
Harald Welte | 683ed23 | 2011-12-08 00:56:54 +0100 | [diff] [blame] | 296 | encode_pc(PointCode) when is_integer(PointCode) -> |
Harald Welte | c923a2a | 2012-01-23 14:13:05 +0100 | [diff] [blame] | 297 | {1, <<PointCode:16/little>>}; |
Harald Welte | 683ed23 | 2011-12-08 00:56:54 +0100 | [diff] [blame] | 298 | encode_pc(PcRec) -> |
| 299 | PcInt = osmo_util:pointcode2int(PcRec), |
| 300 | encode_pc(PcInt). |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 301 | |
| 302 | encode_ssn(SSN) -> |
| 303 | case SSN of |
Harald Welte | b5936ba | 2011-12-08 00:58:51 +0100 | [diff] [blame] | 304 | undefined -> |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 305 | {0, <<>>}; |
| 306 | _ -> |
| 307 | {1, <<SSN:8>>} |
| 308 | end. |
| 309 | |
Harald Welte | 5a1cf3c | 2011-04-14 21:55:13 +0200 | [diff] [blame] | 310 | undef_or_true(Foo) -> |
| 311 | case Foo of |
| 312 | undefined -> 0; |
| 313 | 0 -> 0; |
| 314 | _ -> 1 |
| 315 | end. |
| 316 | |
| 317 | |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 318 | encode_sccp_addr(#sccp_addr{res_nat_use = ResNatUse, |
| 319 | route_on_ssn = RoutInd, |
| 320 | point_code = PointCode, |
| 321 | ssn = SSN, |
| 322 | global_title = GT}) -> |
| 323 | |
| 324 | {GTind, GTbin} = encode_gt(GT), |
| 325 | {SSNind, SSNbin} = encode_ssn(SSN), |
| 326 | {PCind, PCbin} = encode_pc(PointCode), |
Harald Welte | 5a1cf3c | 2011-04-14 21:55:13 +0200 | [diff] [blame] | 327 | ResNatOut = undef_or_true(ResNatUse), |
| 328 | RoutIndOut = undef_or_true(RoutInd), |
| 329 | <<ResNatOut:1, RoutIndOut:1, GTind:4, SSNind:1, PCind:1, PCbin/binary, SSNbin/binary, GTbin/binary>>. |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 330 | |
| 331 | |
Harald Welte | 9dda4e1 | 2012-01-23 16:15:06 +0100 | [diff] [blame] | 332 | encode_sccp_opt({AddrTag, AddrVal}) when AddrTag == ?SCCP_PNC_CALLED_PARTY_ADDRESS; |
| 333 | AddrTag == ?SCCP_PNC_CALLING_PARTY_ADDRESS -> |
| 334 | AddrEnc = encode_sccp_addr(AddrVal), |
| 335 | AddrLen = byte_size(AddrEnc), |
| 336 | <<AddrTag:8, AddrLen:8, AddrEnc/binary>>; |
| 337 | encode_sccp_opt({OptInt, DataBin}) when is_binary(DataBin), is_integer(OptInt) -> |
| 338 | DataBinLen = byte_size(DataBin), |
| 339 | <<OptInt:8, DataBinLen:8, DataBin/binary>>; |
| 340 | encode_sccp_opt({Opt, DataBin}) when is_atom(Opt) -> |
| 341 | OptNum = atom_to_opt(Opt), |
| 342 | encode_sccp_opt({OptNum, DataBin}); |
| 343 | encode_sccp_opt({Opt, DataInt}) when is_integer(DataInt), DataInt =< 255 -> |
| 344 | encode_sccp_opt({Opt, <<DataInt:8>>}); |
| 345 | encode_sccp_opt({Opt, DataList}) when is_list(DataList) -> |
| 346 | encode_sccp_opt({Opt, list_to_binary(DataList)}). |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 347 | |
Harald Welte | 9dda4e1 | 2012-01-23 16:15:06 +0100 | [diff] [blame] | 348 | encode_sccp_opts(OptList, Filter) -> |
| 349 | FilteredList = lists:filter(fun({Tag, _Val}) -> proplists:is_defined(opt_to_atom(Tag), Filter) end, OptList), |
| 350 | e_sccp_opts(FilteredList, []). |
| 351 | |
| 352 | e_sccp_opts([], OptEnc) -> |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 353 | % end of options + convert to binary |
| 354 | list_to_binary([OptEnc, ?SCCP_PNC_END_OF_OPTIONAL]); |
Harald Welte | 9dda4e1 | 2012-01-23 16:15:06 +0100 | [diff] [blame] | 355 | e_sccp_opts([CurOpt|OptPropList], OptEnc) -> |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 356 | CurOptEnc = encode_sccp_opt(CurOpt), |
Harald Welte | 9dda4e1 | 2012-01-23 16:15:06 +0100 | [diff] [blame] | 357 | e_sccp_opts(OptPropList, list_to_binary([OptEnc,CurOptEnc])). |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 358 | |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 359 | |
| 360 | encode_sccp_msgt(?SCCP_MSGT_CR, Params) -> |
| 361 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 362 | {ProtoClass, PCOpt} = proplists:get_value(protocol_class, Params), |
Harald Welte | 9dda4e1 | 2012-01-23 16:15:06 +0100 | [diff] [blame] | 363 | CalledParty = proplists:get_value(called_party_addr, Params), |
| 364 | CalledPartyEnc = encode_sccp_addr(CalledParty), |
| 365 | CalledPartyLen = byte_size(CalledPartyEnc), |
| 366 | PtrOpt = CalledPartyLen+1+1, |
| 367 | OptBin = encode_sccp_opts(Params, [credit, calling_party_addr, user_data, hop_counter, importance]), |
| 368 | <<?SCCP_MSGT_CR:8, SrcLocalRef:24/big, PCOpt:4, ProtoClass:4, 2:8, PtrOpt:8, CalledPartyLen:8, CalledPartyEnc/binary, OptBin/binary>>; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 369 | encode_sccp_msgt(?SCCP_MSGT_CC, Params) -> |
| 370 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
| 371 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 372 | {ProtoClass, PCOpt} = proplists:get_value(protocol_class, Params), |
Harald Welte | 9dda4e1 | 2012-01-23 16:15:06 +0100 | [diff] [blame] | 373 | OptBin = encode_sccp_opts(Params, [credit, called_party_addr, user_data, importance]), |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 374 | <<?SCCP_MSGT_CC:8, DstLocalRef:24/big, SrcLocalRef:24/big, PCOpt:4, ProtoClass:4, OptBin/binary>>; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 375 | encode_sccp_msgt(?SCCP_MSGT_CREF, Params) -> |
| 376 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 377 | RefusalCause = proplists:get_value(refusal_cause, Params), |
Harald Welte | 9dda4e1 | 2012-01-23 16:15:06 +0100 | [diff] [blame] | 378 | OptBin = encode_sccp_opts(Params, [called_party_addr, user_data, importance]), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 379 | <<?SCCP_MSGT_CREF:8, DstLocalRef:24/big, RefusalCause:8, OptBin/binary>>; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 380 | encode_sccp_msgt(?SCCP_MSGT_RLSD, Params) -> |
| 381 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
| 382 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 383 | ReleaseCause = proplists:get_value(release_cause, Params), |
Harald Welte | 9dda4e1 | 2012-01-23 16:15:06 +0100 | [diff] [blame] | 384 | OptBin = encode_sccp_opts(Params, [user_data, importance]), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 385 | <<?SCCP_MSGT_RLSD:8, DstLocalRef:24/big, SrcLocalRef:24/big, ReleaseCause:8, OptBin/binary>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 386 | encode_sccp_msgt(?SCCP_MSGT_RLC, Params) -> |
| 387 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
| 388 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 389 | <<?SCCP_MSGT_RLC:8, DstLocalRef:24/big, SrcLocalRef:24/big>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 390 | encode_sccp_msgt(?SCCP_MSGT_DT1, Params) -> |
| 391 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 392 | SegmReass = proplists:get_value(segm_reass, Params), |
Harald Welte | d9c318f | 2011-12-10 22:17:11 +0100 | [diff] [blame] | 393 | UserData = binarify(proplists:get_value(user_data, Params)), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 394 | UserDataLen = byte_size(UserData), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 395 | <<?SCCP_MSGT_DT1:8, DstLocalRef:24/big, SegmReass:8, 1:8, UserDataLen:8, UserData/binary>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 396 | encode_sccp_msgt(?SCCP_MSGT_DT2, Params) -> |
| 397 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 398 | SeqSegm = proplists:get_value(seq_segm, Params), |
Harald Welte | d9c318f | 2011-12-10 22:17:11 +0100 | [diff] [blame] | 399 | UserData = binarify(proplists:get_value(user_data, Params)), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 400 | UserDataLen = byte_size(UserData), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 401 | <<?SCCP_MSGT_DT2:8, DstLocalRef:24/big, SeqSegm:16, 1:8, UserDataLen:8, UserData/binary>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 402 | encode_sccp_msgt(?SCCP_MSGT_AK, Params) -> |
| 403 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 404 | RxSeqnr = proplists:get_value(rx_seqnr, Params), |
| 405 | Credit = proplists:get_value(credit, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 406 | <<?SCCP_MSGT_AK:8, DstLocalRef:24/big, RxSeqnr:8, Credit:8>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 407 | encode_sccp_msgt(?SCCP_MSGT_UDT, Params) -> |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 408 | {ProtoClass, PCOpt} = proplists:get_value(protocol_class, Params), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 409 | CalledParty = proplists:get_value(called_party_addr, Params), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 410 | CalledPartyEnc = encode_sccp_addr(CalledParty), |
| 411 | CalledPartyLen = byte_size(CalledPartyEnc), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 412 | CallingParty = proplists:get_value(calling_party_addr, Params), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 413 | CallingPartyEnc = encode_sccp_addr(CallingParty), |
| 414 | CallingPartyLen = byte_size(CallingPartyEnc), |
Harald Welte | d9c318f | 2011-12-10 22:17:11 +0100 | [diff] [blame] | 415 | UserData = binarify(proplists:get_value(user_data, Params)), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 416 | UserDataLen = byte_size(UserData), |
| 417 | % variable part |
| 418 | CalledPartyPtr = 3, |
| 419 | CallingPartyPtr = 2 + (1 + CalledPartyLen), |
| 420 | DataPtr = 1 + (1 + CalledPartyLen) + (1 + CallingPartyLen), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 421 | Remain = <<CalledPartyLen:8, CalledPartyEnc/binary, |
| 422 | CallingPartyLen:8, CallingPartyEnc/binary, |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 423 | UserDataLen:8, UserData/binary>>, |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 424 | <<?SCCP_MSGT_UDT:8, PCOpt:4, ProtoClass:4, CalledPartyPtr:8, CallingPartyPtr:8, DataPtr:8, Remain/binary>>; |
Harald Welte | 030f109 | 2011-03-11 19:08:07 +0100 | [diff] [blame] | 425 | encode_sccp_msgt(?SCCP_MSGT_UDTS, Params) -> |
| 426 | ReturnCause = proplists:get_value(return_cause, Params), |
| 427 | CalledParty = proplists:get_value(called_party_addr, Params), |
| 428 | CalledPartyEnc = encode_sccp_addr(CalledParty), |
| 429 | CalledPartyLen = byte_size(CalledPartyEnc), |
| 430 | CallingParty = proplists:get_value(calling_party_addr, Params), |
| 431 | CallingPartyEnc = encode_sccp_addr(CallingParty), |
| 432 | CallingPartyLen = byte_size(CallingPartyEnc), |
Harald Welte | d9c318f | 2011-12-10 22:17:11 +0100 | [diff] [blame] | 433 | UserData = binarify(proplists:get_value(user_data, Params)), |
Harald Welte | 030f109 | 2011-03-11 19:08:07 +0100 | [diff] [blame] | 434 | UserDataLen = byte_size(UserData), |
| 435 | % variable part |
| 436 | CalledPartyPtr = 3, |
| 437 | CallingPartyPtr = 2 + (1 + CalledPartyLen), |
| 438 | DataPtr = 1 + (1 + CalledPartyLen) + (1 + CallingPartyLen), |
| 439 | Remain = <<CalledPartyLen:8, CalledPartyEnc/binary, |
| 440 | CallingPartyLen:8, CallingPartyEnc/binary, |
| 441 | UserDataLen:8, UserData/binary>>, |
| 442 | <<?SCCP_MSGT_UDTS:8, ReturnCause:8, CalledPartyPtr:8, CallingPartyPtr:8, DataPtr:8, Remain/binary>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 443 | encode_sccp_msgt(?SCCP_MSGT_ED, Params) -> |
| 444 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
Harald Welte | d9c318f | 2011-12-10 22:17:11 +0100 | [diff] [blame] | 445 | UserData = binarify(proplists:get_value(user_data, Params)), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 446 | UserDataLen = byte_size(UserData), |
| 447 | DataPtr = 1, |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 448 | <<?SCCP_MSGT_ED:8, DstLocalRef:24/big, DataPtr:8, UserDataLen:8, UserData/binary>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 449 | encode_sccp_msgt(?SCCP_MSGT_EA, Params) -> |
| 450 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 451 | <<?SCCP_MSGT_EA:8, DstLocalRef:24/big>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 452 | encode_sccp_msgt(?SCCP_MSGT_RSR, Params) -> |
| 453 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 454 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
| 455 | ResetCause = proplists:get_value(reset_cause, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 456 | <<?SCCP_MSGT_RSR:8, DstLocalRef:24/big, SrcLocalRef:24/big, ResetCause:8>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 457 | encode_sccp_msgt(?SCCP_MSGT_RSC, Params) -> |
| 458 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 459 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 460 | <<?SCCP_MSGT_RSC:8, DstLocalRef:24/big, SrcLocalRef:24/big>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 461 | encode_sccp_msgt(?SCCP_MSGT_ERR, Params) -> |
| 462 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 463 | ErrCause = proplists:get_value(error_cause, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 464 | <<?SCCP_MSGT_ERR:8, DstLocalRef:24/big, ErrCause:8>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 465 | encode_sccp_msgt(?SCCP_MSGT_IT, Params) -> |
| 466 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 467 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 468 | {ProtoClass, PCOpt} = proplists:get_value(protocol_class, Params), |
Harald Welte | 09b4399 | 2010-12-20 12:21:03 +0100 | [diff] [blame] | 469 | SegmSeq = proplists:get_value(seq_segm, Params), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 470 | Credit = proplists:get_value(credit, Params), |
Harald Welte | 1156577 | 2011-04-15 10:37:57 +0200 | [diff] [blame] | 471 | <<?SCCP_MSGT_IT:8, DstLocalRef:24/big, SrcLocalRef:24/big, PCOpt:4, ProtoClass:4, SegmSeq:16, Credit:8>>. |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 472 | % FIXME: XUDT/XUDTS, LUDT/LUDTS |
| 473 | |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 474 | |
| 475 | % encode one sccp message data structure into the on-wire format |
| 476 | encode_sccp_msg(#sccp_msg{msg_type = MsgType, parameters = Params}) -> |
| 477 | encode_sccp_msgt(MsgType, Params). |
Harald Welte | 9abbbad | 2011-04-21 12:19:41 +0200 | [diff] [blame] | 478 | |
| 479 | % is the supplied message type a connectionless message? |
| 480 | is_connectionless(#sccp_msg{msg_type = MsgType}) -> |
| 481 | is_connectionless(MsgType); |
| 482 | is_connectionless(MsgType) -> |
| 483 | case MsgType of |
| 484 | ?SCCP_MSGT_UDT -> true; |
| 485 | ?SCCP_MSGT_UDTS -> true; |
| 486 | ?SCCP_MSGT_XUDT -> true; |
| 487 | ?SCCP_MSGT_XUDTS -> true; |
| 488 | ?SCCP_MSGT_LUDT -> true; |
| 489 | ?SCCP_MSGT_LUDTS -> true; |
| 490 | _ -> false |
| 491 | end. |
Harald Welte | 9baab6d | 2011-12-08 00:46:00 +0100 | [diff] [blame] | 492 | |
| 493 | |
| 494 | gen_gt_helper(Number) when is_list(Number) -> |
| 495 | #global_title{gti=?SCCP_GTI_NAT_ONLY, |
| 496 | nature_of_addr_ind=?SCCP_NAI_INTERNATIONAL, |
| 497 | phone_number = Number}. |
| 498 | |
| 499 | gen_addr_helper(Gt, Pc, Ssn) when is_record(Gt, global_title) -> |
| 500 | #sccp_addr{point_code=Pc, ssn=Ssn, global_title=Gt}; |
| 501 | gen_addr_helper(Number, Pc, Ssn) when is_list(Number) -> |
| 502 | Gt = gen_gt_helper(Number), |
| 503 | gen_addr_helper(Gt, Pc, Ssn). |
| 504 | |
| 505 | |
| 506 | gen_addr_helper(Gt, Pc) when is_record(Gt, global_title) -> |
| 507 | #sccp_addr{point_code=Pc, global_title=Gt}; |
| 508 | gen_addr_helper(Number, Pc) when is_list(Number) -> |
| 509 | Gt = gen_gt_helper(Number), |
| 510 | gen_addr_helper(Gt, Pc). |
Harald Welte | 9dda4e1 | 2012-01-23 16:15:06 +0100 | [diff] [blame] | 511 | |
| 512 | opt_to_atom(Num) -> |
| 513 | case Num of |
| 514 | ?SCCP_PNC_DESTINATION_LOCAL_REFERENCE -> dst_local_ref; |
| 515 | ?SCCP_PNC_SOURCE_LOCAL_REFERENCE -> src_local_ref; |
| 516 | ?SCCP_PNC_CALLED_PARTY_ADDRESS -> called_party_addr; |
| 517 | ?SCCP_PNC_CALLING_PARTY_ADDRESS -> calling_party_addr; |
| 518 | ?SCCP_PNC_PROTOCOL_CLASS -> protocol_class; |
| 519 | ?SCCP_PNC_SEGMENTING -> segmenting; |
| 520 | ?SCCP_PNC_RECEIVE_SEQ_NUMBER -> rx_seq_number; |
| 521 | ?SCCP_PNC_SEQUENCING -> seq_segm; |
| 522 | ?SCCP_PNC_CREDIT -> credit; |
| 523 | ?SCCP_PNC_RELEASE_CAUSE -> release_cause; |
| 524 | ?SCCP_PNC_RETURN_CAUSE -> return_cause; |
| 525 | ?SCCP_PNC_RESET_CAUSE -> reset_cause; |
| 526 | ?SCCP_PNC_ERROR_CAUSE -> error_cause; |
| 527 | ?SCCP_PNC_REFUSAL_CAUSE -> refusal_cause; |
| 528 | ?SCCP_PNC_DATA -> user_data; |
| 529 | ?SCCP_PNC_SEGMENTATION -> segmentation; |
| 530 | ?SCCP_PNC_HOP_COUNTER -> hop_counter; |
| 531 | ?SCCP_PNC_IMPORTANCE -> importance; |
| 532 | ?SCCP_PNC_LONG_DATA -> long_data; |
| 533 | Foo -> Foo |
| 534 | end. |
| 535 | |
| 536 | atom_to_opt(Atom) -> |
| 537 | case Atom of |
| 538 | dst_local_ref -> ?SCCP_PNC_DESTINATION_LOCAL_REFERENCE; |
| 539 | src_local_ref -> ?SCCP_PNC_SOURCE_LOCAL_REFERENCE; |
| 540 | called_party_addr -> ?SCCP_PNC_CALLED_PARTY_ADDRESS; |
| 541 | calling_party_addr -> ?SCCP_PNC_CALLING_PARTY_ADDRESS; |
| 542 | protocol_class -> ?SCCP_PNC_PROTOCOL_CLASS; |
| 543 | segmenting -> ?SCCP_PNC_SEGMENTING; |
| 544 | rx_seq_number -> ?SCCP_PNC_RECEIVE_SEQ_NUMBER; |
| 545 | seq_segm -> ?SCCP_PNC_SEQUENCING; |
| 546 | credit -> ?SCCP_PNC_CREDIT; |
| 547 | release_cause -> ?SCCP_PNC_RELEASE_CAUSE; |
| 548 | return_cause -> ?SCCP_PNC_RETURN_CAUSE; |
| 549 | reset_cause -> ?SCCP_PNC_RESET_CAUSE; |
| 550 | error_cause -> ?SCCP_PNC_ERROR_CAUSE; |
| 551 | refusal_cause -> ?SCCP_PNC_REFUSAL_CAUSE; |
| 552 | user_data -> ?SCCP_PNC_DATA; |
| 553 | segmentation -> ?SCCP_PNC_SEGMENTATION; |
| 554 | hop_counter -> ?SCCP_PNC_HOP_COUNTER; |
| 555 | importance -> ?SCCP_PNC_IMPORTANCE; |
| 556 | long_data -> ?SCCP_PNC_LONG_DATA; |
| 557 | Foo -> Foo |
| 558 | end. |