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 | |
| 24 | -export([parse_sccp_msg/1, encode_sccp_msg/1, encode_sccp_msgt/2]). |
| 25 | |
| 26 | -compile(export_all). |
| 27 | |
Harald Welte | 2edaf55 | 2011-04-02 16:46:16 +0200 | [diff] [blame] | 28 | -compile({parse_transform, exprecs}). |
Harald Welte | 0f2f596 | 2011-04-04 15:59:49 +0200 | [diff] [blame] | 29 | -export_records([global_title, sccp_addr, sccp_msg]). |
Harald Welte | 2edaf55 | 2011-04-02 16:46:16 +0200 | [diff] [blame] | 30 | |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 31 | parse_point_code(BinPC, PCind) when is_binary(BinPC) -> |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 32 | case PCind of |
| 33 | 1 -> |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 34 | <<PointCode:16/big, Remain/binary>> = BinPC; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 35 | _ -> |
| 36 | Remain = BinPC, |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 37 | PointCode = undef |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 38 | end, |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 39 | {Remain, PointCode}. |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 40 | |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 41 | parse_ssn(BinSSN, SSNind) -> |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 42 | case SSNind of |
| 43 | 1 -> |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 44 | <<SSN:8, Remain/binary>> = BinSSN; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 45 | _ -> |
| 46 | Remain = BinSSN, |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 47 | SSN = undef |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 48 | end, |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 49 | {Remain, SSN}. |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 50 | |
| 51 | enc_is_odd(Enc) -> |
| 52 | case Enc of |
| 53 | 1 -> 1; |
| 54 | _ -> 0 |
| 55 | end. |
| 56 | |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 57 | parse_gt(BinGT, GTind) -> |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 58 | case GTind of |
| 59 | ?SCCP_GTI_NO_GT -> |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 60 | undef; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 61 | ?SCCP_GTI_NAT_ONLY -> |
| 62 | % Figure 7/Q.713 |
| 63 | <<Odd:1, Nature:7, Digits/binary>> = BinGT, |
| 64 | PhoneNum = isup_codec:parse_isup_party(Digits, Odd), |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 65 | #global_title{gti = GTind, |
| 66 | nature_of_addr_ind = Nature, |
| 67 | phone_number = PhoneNum}; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 68 | ?SCCP_GTI_TT_ONLY -> |
| 69 | % Figure 9/Q.913 |
| 70 | <<TransType:8, Digits/binary>> = BinGT, |
| 71 | % Used in national interfaces only, we cannot parse Digits |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 72 | #global_title{gti = GTind, |
| 73 | trans_type = TransType, |
| 74 | phone_number = Digits}; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 75 | ?SCCP_GTI_TT_NP_ENC -> |
| 76 | % Figure 10/Q.713 |
| 77 | <<TransType:8, NumPlan:4, Enc:4, Digits/binary>> = BinGT, |
| 78 | PhoneNum = isup_codec:parse_isup_party(Digits, enc_is_odd(Enc)), |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 79 | #global_title{gti = GTind, |
| 80 | trans_type = TransType, encoding = Enc, |
| 81 | numbering_plan = NumPlan, |
| 82 | phone_number = PhoneNum}; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 83 | ?SCCP_GTI_TT_NP_ENC_NAT -> |
| 84 | % Figure 11/Q.713 |
| 85 | <<TransType:8, NumPlan:4, Enc:4, 0:1, Nature:7, 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, |
| 88 | trans_type = TransType, encoding = Enc, |
| 89 | numbering_plan = NumPlan, |
| 90 | nature_of_addr_ind = Nature, |
| 91 | phone_number = PhoneNum}; |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 92 | _ -> |
Harald Welte | 2b4b267 | 2011-02-03 12:50:41 +0100 | [diff] [blame] | 93 | BinGT |
| 94 | end. |
Harald Welte | ba6fdbb | 2011-01-23 22:04:39 +0100 | [diff] [blame] | 95 | |
| 96 | % parse SCCP Address |
| 97 | parse_sccp_addr(BinAddr) when is_binary(BinAddr) -> |
| 98 | <<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] | 99 | {RemainPC, OptPC} = parse_point_code(Remain, PCind), |
| 100 | {RemainSSN, OptSSN} = parse_ssn(RemainPC, SSNind), |
| 101 | OptGT = parse_gt(RemainSSN, GTind), |
| 102 | #sccp_addr{res_nat_use = ResNatUse, route_on_ssn = RoutInd, |
| 103 | point_code = OptPC, ssn = OptSSN, global_title = OptGT}. |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 104 | |
| 105 | % parse SCCP Optional Part |
| 106 | parse_sccp_opt(OptType, OptLen, Content) -> |
| 107 | {OptType, {OptLen, Content}}. |
| 108 | |
| 109 | parse_sccp_opts(<<>>, OptList) -> |
| 110 | % empty list |
| 111 | OptList; |
| 112 | parse_sccp_opts(<<0>>, OptList) -> |
| 113 | % end of options |
| 114 | OptList; |
| 115 | parse_sccp_opts(OptBin, OptList) -> |
| 116 | <<OptType, OptLen, Content:OptLen/binary, Remain/binary>> = OptBin, |
| 117 | NewOpt = parse_sccp_opt(OptType, OptLen, Content), |
| 118 | parse_sccp_opts(Remain, [NewOpt|OptList]). |
| 119 | |
| 120 | % Parse incoming SCCP message, one function for every message type |
| 121 | parse_sccp_msgt(?SCCP_MSGT_CR, DataBin) -> |
| 122 | % first get the fixed part |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 123 | <<_:8, SrcLocalRef:24/big, ProtoClass:8, RemainVar/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 124 | % variable length fixed part |
| 125 | <<PtrVar:8, PtrOpt:8, _/binary>> = RemainVar, |
| 126 | CalledPartyLen = binary:at(RemainVar, PtrVar), |
| 127 | CalledParty = binary:part(RemainVar, PtrVar+1, CalledPartyLen), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 128 | CalledPartyDec = parse_sccp_addr(CalledParty), |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 129 | % optional part |
| 130 | OptBin = binary:part(RemainVar, 1 + PtrOpt, byte_size(RemainVar)-(1+PtrOpt)), |
| 131 | OptList = parse_sccp_opts(OptBin, []), |
| 132 | %OptList = [], |
| 133 | % build parsed list of message |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 134 | [{src_local_ref, SrcLocalRef},{protocol_class, ProtoClass},{called_party_addr, CalledPartyDec}|OptList]; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 135 | parse_sccp_msgt(?SCCP_MSGT_CC, DataBin) -> |
| 136 | % first get the fixed part |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 137 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big, ProtoClass:8, Remain/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 138 | % optional part |
| 139 | OptList = parse_sccp_opts(Remain, []), |
| 140 | % build parsed list of message |
| 141 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef},{protocol_class, ProtoClass}|OptList]; |
| 142 | parse_sccp_msgt(?SCCP_MSGT_CREF, DataBin) -> |
| 143 | % first get the fixed part |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 144 | <<_:8, DstLocalRef:24/big, RefusalCause:8, Remain/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 145 | % optional part |
| 146 | OptList = parse_sccp_opts(Remain, []), |
| 147 | % build parsed list of message |
| 148 | [{dst_local_ref, DstLocalRef},{refusal_cause, RefusalCause}|OptList]; |
| 149 | parse_sccp_msgt(?SCCP_MSGT_RLSD, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 150 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big, ReleaseCause:8, Remain/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 151 | % optional part |
| 152 | OptList = parse_sccp_opts(Remain, []), |
| 153 | % build parsed list of message |
| 154 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef},{release_cause, ReleaseCause}|OptList]; |
| 155 | parse_sccp_msgt(?SCCP_MSGT_RLC, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 156 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 157 | % build parsed list of message |
| 158 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef}]; |
| 159 | parse_sccp_msgt(?SCCP_MSGT_DT1, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 160 | <<_:8, DstLocalRef:24/big, SegmReass:8, DataPtr:8, Remain/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 161 | DataLen = binary:at(Remain, DataPtr-1), |
| 162 | UserData = binary:part(Remain, DataPtr-1+1, DataLen), |
| 163 | % build parsed list of message |
| 164 | [{dst_local_ref, DstLocalRef},{segm_reass, SegmReass},{user_data, UserData}]; |
| 165 | parse_sccp_msgt(?SCCP_MSGT_DT2, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 166 | <<_:8, DstLocalRef:24/big, SeqSegm:16, DataPtr:8, Remain/binary >> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 167 | DataLen = binary:at(Remain, DataPtr-1), |
| 168 | UserData = binary:part(Remain, DataPtr-1+1, DataLen), |
| 169 | % build parsed list of message |
| 170 | [{dst_local_ref, DstLocalRef},{seq_segm, SeqSegm},{user_data, UserData}]; |
| 171 | parse_sccp_msgt(?SCCP_MSGT_AK, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 172 | <<_:8, DstLocalRef:24/big, RxSeqnr:8, Credit:8>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 173 | [{dst_local_ref, DstLocalRef},{rx_seq_nr, RxSeqnr},{credit, Credit}]; |
| 174 | parse_sccp_msgt(?SCCP_MSGT_UDT, DataBin) -> |
| 175 | <<_:8, ProtoClass:8, CalledPartyPtr:8, CallingPartyPtr:8, DataPtr:8, Remain/binary >> = DataBin, |
| 176 | % variable part |
| 177 | CalledPartyLen = binary:at(Remain, CalledPartyPtr-3), |
| 178 | CalledParty = binary:part(Remain, CalledPartyPtr-3+1, CalledPartyLen), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 179 | CalledPartyDec = parse_sccp_addr(CalledParty), |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 180 | CallingPartyLen = binary:at(Remain, CallingPartyPtr-2), |
| 181 | CallingParty = binary:part(Remain, CallingPartyPtr-2+1, CallingPartyLen), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 182 | CallingPartyDec = parse_sccp_addr(CallingParty), |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 183 | DataLen = binary:at(Remain, DataPtr-1), |
| 184 | UserData = binary:part(Remain, DataPtr-1+1, DataLen), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 185 | [{protocol_class, ProtoClass},{called_party_addr, CalledPartyDec}, |
| 186 | {calling_party_addr, CallingPartyDec},{user_data, UserData}]; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 187 | parse_sccp_msgt(?SCCP_MSGT_UDTS, DataBin) -> |
Harald Welte | 030f109 | 2011-03-11 19:08:07 +0100 | [diff] [blame] | 188 | <<_:8, ReturnCause:8, CalledPartyPtr:8, CallingPartyPtr:8, DataPtr:8, Remain/binary >> = DataBin, |
| 189 | % variable part |
| 190 | CalledPartyLen = binary:at(Remain, CalledPartyPtr-3), |
| 191 | CalledParty = binary:part(Remain, CalledPartyPtr-3+1, CalledPartyLen), |
| 192 | CalledPartyDec = parse_sccp_addr(CalledParty), |
| 193 | CallingPartyLen = binary:at(Remain, CallingPartyPtr-2), |
| 194 | CallingParty = binary:part(Remain, CallingPartyPtr-2+1, CallingPartyLen), |
| 195 | CallingPartyDec = parse_sccp_addr(CallingParty), |
| 196 | DataLen = binary:at(Remain, DataPtr-1), |
| 197 | UserData = binary:part(Remain, DataPtr-1+1, DataLen), |
| 198 | [{return_cause, ReturnCause},{called_party_addr, CalledPartyDec}, |
| 199 | {calling_party_addr, CallingPartyDec},{user_data, UserData}]; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 200 | parse_sccp_msgt(?SCCP_MSGT_ED, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 201 | <<_:8, DstLocalRef:24/big, DataPtr:8, Remain/binary>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 202 | DataLen = binary:at(Remain, DataPtr-1), |
| 203 | UserData = binary:part(Remain, DataPtr-1+1, DataLen), |
| 204 | [{dst_local_ref, DstLocalRef}, {user_data, UserData}]; |
| 205 | parse_sccp_msgt(?SCCP_MSGT_EA, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 206 | <<_:8, DstLocalRef:24/big>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 207 | [{dst_local_ref, DstLocalRef}]; |
| 208 | parse_sccp_msgt(?SCCP_MSGT_RSR, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 209 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big, ResetCause:8>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 210 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef},{reset_cause, ResetCause}]; |
| 211 | parse_sccp_msgt(?SCCP_MSGT_RSC, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 212 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 213 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef}]; |
| 214 | parse_sccp_msgt(?SCCP_MSGT_ERR, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 215 | <<_:8, DstLocalRef:24/big, ErrCause:8>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 216 | [{dst_local_ref, DstLocalRef},{error_cause, ErrCause}]; |
| 217 | parse_sccp_msgt(?SCCP_MSGT_IT, DataBin) -> |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 218 | <<_:8, DstLocalRef:24/big, SrcLocalRef:24/big, ProtoClass:8, SegmSeq:16, Credit:8>> = DataBin, |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 219 | [{dst_local_ref, DstLocalRef},{src_local_ref, SrcLocalRef}, |
| 220 | {protocol_class, ProtoClass},{seq_segm, SegmSeq},{credit, Credit}]. |
| 221 | % FIXME: XUDT/XUDTS, LUDT/LUDTS |
| 222 | |
| 223 | % process one incoming SCCP message |
| 224 | parse_sccp_msg(DataBin) -> |
| 225 | MsgType = binary:first(DataBin), |
| 226 | Parsed = parse_sccp_msgt(MsgType, DataBin), |
| 227 | {ok, #sccp_msg{msg_type = MsgType, parameters = Parsed}}. |
| 228 | |
| 229 | % Encoding Part |
| 230 | |
Harald Welte | 0e1709c | 2011-02-06 22:17:53 +0100 | [diff] [blame] | 231 | gt_enc_by_odd(Odd) -> |
| 232 | if Odd == 1 -> |
| 233 | 1; |
| 234 | true -> |
| 235 | 2 |
| 236 | end. |
| 237 | |
Harald Welte | 5a1cf3c | 2011-04-14 21:55:13 +0200 | [diff] [blame^] | 238 | encode_gt(undefined) -> |
| 239 | {?SCCP_GTI_NO_GT, <<>>}; |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 240 | encode_gt(#global_title{gti = GTind, phone_number = PhoneNum, |
| 241 | nature_of_addr_ind = Nature, |
Harald Welte | 0e1709c | 2011-02-06 22:17:53 +0100 | [diff] [blame] | 242 | trans_type = TransType, encoding = _EncOrig, |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 243 | numbering_plan = NumPlan}) -> |
| 244 | case GTind of |
| 245 | ?SCCP_GTI_NO_GT -> |
| 246 | {GTind, <<>>}; |
| 247 | ?SCCP_GTI_NAT_ONLY -> |
| 248 | % Figure 7/Q.713 |
| 249 | {PhoneBin, OddEven} = isup_codec:encode_isup_party(PhoneNum), |
| 250 | {GTind, <<OddEven:1, Nature:7, PhoneBin/binary>>}; |
| 251 | ?SCCP_GTI_TT_ONLY -> |
| 252 | % Figure 9/Q.913 |
| 253 | % Used in national interfaces only, we cannot parse Digits |
| 254 | {GTind, <<TransType:8, PhoneNum/binary>>}; |
| 255 | ?SCCP_GTI_TT_NP_ENC -> |
| 256 | % Figure 10/Q.713 |
Harald Welte | 0e1709c | 2011-02-06 22:17:53 +0100 | [diff] [blame] | 257 | {PhoneBin, OddEven} = isup_codec:encode_isup_party(PhoneNum), |
| 258 | Enc = gt_enc_by_odd(OddEven), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 259 | {GTind, <<TransType:8, NumPlan:4, Enc:4, PhoneBin/binary>>}; |
| 260 | ?SCCP_GTI_TT_NP_ENC_NAT -> |
| 261 | % Figure 11/Q.713 |
Harald Welte | 0e1709c | 2011-02-06 22:17:53 +0100 | [diff] [blame] | 262 | {PhoneBin, OddEven} = isup_codec:encode_isup_party(PhoneNum), |
| 263 | Enc = gt_enc_by_odd(OddEven), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 264 | {GTind, <<TransType:8, NumPlan:4, Enc:4, 0:1, Nature:7, PhoneBin/binary>>} |
| 265 | end. |
| 266 | |
| 267 | encode_pc(PointCode) -> |
| 268 | case PointCode of |
| 269 | undef -> |
| 270 | {0, <<>>}; |
| 271 | _ -> |
| 272 | {1, <<PointCode:16/big>>} |
| 273 | end. |
| 274 | |
| 275 | encode_ssn(SSN) -> |
| 276 | case SSN of |
| 277 | undef -> |
| 278 | {0, <<>>}; |
| 279 | _ -> |
| 280 | {1, <<SSN:8>>} |
| 281 | end. |
| 282 | |
Harald Welte | 5a1cf3c | 2011-04-14 21:55:13 +0200 | [diff] [blame^] | 283 | undef_or_true(Foo) -> |
| 284 | case Foo of |
| 285 | undefined -> 0; |
| 286 | 0 -> 0; |
| 287 | _ -> 1 |
| 288 | end. |
| 289 | |
| 290 | |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 291 | encode_sccp_addr(#sccp_addr{res_nat_use = ResNatUse, |
| 292 | route_on_ssn = RoutInd, |
| 293 | point_code = PointCode, |
| 294 | ssn = SSN, |
| 295 | global_title = GT}) -> |
| 296 | |
| 297 | {GTind, GTbin} = encode_gt(GT), |
| 298 | {SSNind, SSNbin} = encode_ssn(SSN), |
| 299 | {PCind, PCbin} = encode_pc(PointCode), |
Harald Welte | 5a1cf3c | 2011-04-14 21:55:13 +0200 | [diff] [blame^] | 300 | ResNatOut = undef_or_true(ResNatUse), |
| 301 | RoutIndOut = undef_or_true(RoutInd), |
| 302 | <<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] | 303 | |
| 304 | |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 305 | encode_sccp_opt({OptNum, {DataBinLen, DataBin}}) when is_integer(OptNum) -> |
| 306 | DataBinLen8 = DataBinLen*8, |
| 307 | <<OptNum:8, DataBinLen:8, DataBin:DataBinLen8>>; |
| 308 | encode_sccp_opt({OptAtom,_}) when is_atom(OptAtom) -> |
| 309 | <<>>. |
| 310 | |
| 311 | encode_sccp_opts([], OptEnc) -> |
| 312 | % end of options + convert to binary |
| 313 | list_to_binary([OptEnc, ?SCCP_PNC_END_OF_OPTIONAL]); |
| 314 | encode_sccp_opts([CurOpt|OptPropList], OptEnc) -> |
| 315 | CurOptEnc = encode_sccp_opt(CurOpt), |
| 316 | encode_sccp_opts(OptPropList, list_to_binary([OptEnc,CurOptEnc])). |
| 317 | |
| 318 | |
| 319 | |
| 320 | encode_sccp_msgt(?SCCP_MSGT_CR, Params) -> |
| 321 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
| 322 | ProtoClass = proplists:get_value(protocol_class, Params), |
| 323 | OptBin = encode_sccp_opts(Params, []), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 324 | <<?SCCP_MSGT_CR:8, SrcLocalRef:24/big, ProtoClass:8, OptBin/binary>>; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 325 | encode_sccp_msgt(?SCCP_MSGT_CC, Params) -> |
| 326 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
| 327 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 328 | ProtoClass = proplists:get_value(protocol_class, Params), |
| 329 | OptBin = encode_sccp_opts(Params, []), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 330 | <<?SCCP_MSGT_CC:8, DstLocalRef:24/big, SrcLocalRef:24/big, ProtoClass:8, OptBin/binary>>; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 331 | encode_sccp_msgt(?SCCP_MSGT_CREF, Params) -> |
| 332 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 333 | RefusalCause = proplists:get_value(refusal_cause, Params), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 334 | OptBin = encode_sccp_opts(Params, []), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 335 | <<?SCCP_MSGT_CREF:8, DstLocalRef:24/big, RefusalCause:8, OptBin/binary>>; |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 336 | encode_sccp_msgt(?SCCP_MSGT_RLSD, Params) -> |
| 337 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
| 338 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 339 | ReleaseCause = proplists:get_value(release_cause, Params), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 340 | OptBin = encode_sccp_opts(Params, []), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 341 | <<?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] | 342 | encode_sccp_msgt(?SCCP_MSGT_RLC, Params) -> |
| 343 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
| 344 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 345 | <<?SCCP_MSGT_RLC:8, DstLocalRef:24/big, SrcLocalRef:24/big>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 346 | encode_sccp_msgt(?SCCP_MSGT_DT1, Params) -> |
| 347 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 348 | SegmReass = proplists:get_value(segm_reass, Params), |
| 349 | UserData = proplists:get_value(user_data, Params), |
| 350 | UserDataLen = byte_size(UserData), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 351 | <<?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] | 352 | encode_sccp_msgt(?SCCP_MSGT_DT2, Params) -> |
| 353 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 354 | SeqSegm = proplists:get_value(seq_segm, Params), |
| 355 | UserData = proplists:get_value(user_data, Params), |
| 356 | UserDataLen = byte_size(UserData), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 357 | <<?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] | 358 | encode_sccp_msgt(?SCCP_MSGT_AK, Params) -> |
| 359 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 360 | RxSeqnr = proplists:get_value(rx_seqnr, Params), |
| 361 | Credit = proplists:get_value(credit, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 362 | <<?SCCP_MSGT_AK:8, DstLocalRef:24/big, RxSeqnr:8, Credit:8>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 363 | encode_sccp_msgt(?SCCP_MSGT_UDT, Params) -> |
| 364 | ProtoClass = proplists:get_value(protocol_class, Params), |
| 365 | CalledParty = proplists:get_value(called_party_addr, Params), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 366 | CalledPartyEnc = encode_sccp_addr(CalledParty), |
| 367 | CalledPartyLen = byte_size(CalledPartyEnc), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 368 | CallingParty = proplists:get_value(calling_party_addr, Params), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 369 | CallingPartyEnc = encode_sccp_addr(CallingParty), |
| 370 | CallingPartyLen = byte_size(CallingPartyEnc), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 371 | UserData = proplists:get_value(user_data, Params), |
| 372 | UserDataLen = byte_size(UserData), |
| 373 | % variable part |
| 374 | CalledPartyPtr = 3, |
| 375 | CallingPartyPtr = 2 + (1 + CalledPartyLen), |
| 376 | DataPtr = 1 + (1 + CalledPartyLen) + (1 + CallingPartyLen), |
Harald Welte | 234c956 | 2011-02-03 13:51:12 +0100 | [diff] [blame] | 377 | Remain = <<CalledPartyLen:8, CalledPartyEnc/binary, |
| 378 | CallingPartyLen:8, CallingPartyEnc/binary, |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 379 | UserDataLen:8, UserData/binary>>, |
| 380 | <<?SCCP_MSGT_UDT:8, ProtoClass:8, CalledPartyPtr:8, CallingPartyPtr:8, DataPtr:8, Remain/binary>>; |
Harald Welte | 030f109 | 2011-03-11 19:08:07 +0100 | [diff] [blame] | 381 | encode_sccp_msgt(?SCCP_MSGT_UDTS, Params) -> |
| 382 | ReturnCause = proplists:get_value(return_cause, Params), |
| 383 | CalledParty = proplists:get_value(called_party_addr, Params), |
| 384 | CalledPartyEnc = encode_sccp_addr(CalledParty), |
| 385 | CalledPartyLen = byte_size(CalledPartyEnc), |
| 386 | CallingParty = proplists:get_value(calling_party_addr, Params), |
| 387 | CallingPartyEnc = encode_sccp_addr(CallingParty), |
| 388 | CallingPartyLen = byte_size(CallingPartyEnc), |
| 389 | UserData = proplists:get_value(user_data, Params), |
| 390 | UserDataLen = byte_size(UserData), |
| 391 | % variable part |
| 392 | CalledPartyPtr = 3, |
| 393 | CallingPartyPtr = 2 + (1 + CalledPartyLen), |
| 394 | DataPtr = 1 + (1 + CalledPartyLen) + (1 + CallingPartyLen), |
| 395 | Remain = <<CalledPartyLen:8, CalledPartyEnc/binary, |
| 396 | CallingPartyLen:8, CallingPartyEnc/binary, |
| 397 | UserDataLen:8, UserData/binary>>, |
| 398 | <<?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] | 399 | encode_sccp_msgt(?SCCP_MSGT_ED, Params) -> |
| 400 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 401 | UserData = proplists:get_value(user_data, Params), |
| 402 | UserDataLen = byte_size(UserData), |
| 403 | DataPtr = 1, |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 404 | <<?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] | 405 | encode_sccp_msgt(?SCCP_MSGT_EA, Params) -> |
| 406 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 407 | <<?SCCP_MSGT_EA:8, DstLocalRef:24/big>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 408 | encode_sccp_msgt(?SCCP_MSGT_RSR, Params) -> |
| 409 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 410 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
| 411 | ResetCause = proplists:get_value(reset_cause, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 412 | <<?SCCP_MSGT_RSR:8, DstLocalRef:24/big, SrcLocalRef:24/big, ResetCause:8>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 413 | encode_sccp_msgt(?SCCP_MSGT_RSC, Params) -> |
| 414 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 415 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 416 | <<?SCCP_MSGT_RSC:8, DstLocalRef:24/big, SrcLocalRef:24/big>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 417 | encode_sccp_msgt(?SCCP_MSGT_ERR, Params) -> |
| 418 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 419 | ErrCause = proplists:get_value(error_cause, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 420 | <<?SCCP_MSGT_ERR:8, DstLocalRef:24/big, ErrCause:8>>; |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 421 | encode_sccp_msgt(?SCCP_MSGT_IT, Params) -> |
| 422 | DstLocalRef = proplists:get_value(dst_local_ref, Params), |
| 423 | SrcLocalRef = proplists:get_value(src_local_ref, Params), |
| 424 | ProtoClass = proplists:get_value(protocol_class, Params), |
Harald Welte | 09b4399 | 2010-12-20 12:21:03 +0100 | [diff] [blame] | 425 | SegmSeq = proplists:get_value(seq_segm, Params), |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 426 | Credit = proplists:get_value(credit, Params), |
Harald Welte | 56ee7a6 | 2010-12-20 13:34:32 +0100 | [diff] [blame] | 427 | <<?SCCP_MSGT_IT:8, DstLocalRef:24/big, SrcLocalRef:24/big, ProtoClass:8, SegmSeq:16, Credit:8>>. |
Harald Welte | c0696b0 | 2010-12-20 00:09:37 +0100 | [diff] [blame] | 428 | % FIXME: XUDT/XUDTS, LUDT/LUDTS |
| 429 | |
Harald Welte | 033cef0 | 2010-12-19 22:47:14 +0100 | [diff] [blame] | 430 | |
| 431 | % encode one sccp message data structure into the on-wire format |
| 432 | encode_sccp_msg(#sccp_msg{msg_type = MsgType, parameters = Params}) -> |
| 433 | encode_sccp_msgt(MsgType, Params). |