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