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