blob: 72bf717d35ff4d6d9e9919b73625703731ac780f [file] [log] [blame]
Harald Weltea68d96e2011-02-10 09:49:46 +01001% MAP masquerading application
2
3% (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
4% (C) 2010-2011 by On-Waves
5%
6% All Rights Reserved
7%
8% This program is free software; you can redistribute it and/or modify
9% it under the terms of the GNU General Public License as published by
10% the Free Software Foundation; either version 2 of the License, or
11% (at your option) any later version.
12%
13% This program is distributed in the hope that it will be useful,
14% but WITHOUT ANY WARRANTY; without even the implied warranty of
15% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16% GNU General Public License for more details.
17%
18% You should have received a copy of the GNU General Public License along
19% with this program; if not, write to the Free Software Foundation, Inc.,
20% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22
23-module(map_masq).
24-author('Harald Welte <laforge@gnumonks.org>').
25%-compile(export_all).
26
Harald Weltef9629642011-02-10 20:44:46 +010027-export([mangle_map/2, config_update/0]).
Harald Weltea68d96e2011-02-10 09:49:46 +010028
29-include_lib("osmo_map/include/map.hrl").
Harald Weltef9629642011-02-10 20:44:46 +010030-include_lib("osmo_ss7/include/isup.hrl").
31
32% Use the MAP address translation table to alter an ISDN-Address-String
33patch_map_isdn_addr(asn1_NOVALUE, _Type) ->
34 asn1_NOVALUE;
35patch_map_isdn_addr(AddrIn, Type) when is_binary(AddrIn) ->
36 patch_map_isdn_addr(binary_to_list(AddrIn), Type);
37patch_map_isdn_addr(AddrIn, Type) when is_list(AddrIn) ->
38 % obtain some configuration data
39 {ok, Tbl} = application:get_env(map_rewrite_table),
40 {ok, IntPfx} = application:get_env(intern_pfx),
41 % Decode the list of octets into an party_number
42 AddrInDec = map_codec:parse_addr_string(AddrIn),
43 % First we always internationalize the address
Harald Welte6a33c1e2011-02-10 21:40:40 +010044 AddrInIntl = mgw_nat:isup_party_internationalize(AddrInDec, IntPfx),
Harald Weltef9629642011-02-10 20:44:46 +010045 % And then patch/replace the address digits
46 DigitsIn = AddrInIntl#party_number.phone_number,
47 DigitsOut = patch_map_isdn_digits(DigitsIn, Type, Tbl),
48 AddrOutIntl = AddrInIntl#party_number{phone_number = DigitsOut},
49 if AddrOutIntl == AddrInDec ->
50 ok;
51 true ->
52 io:format("Translating MAP-ISDN-Addess ~p ~p -> ~p~n",
53 [Type, AddrInDec, AddrOutIntl])
54 end,
Harald Welte62aa7c62011-02-10 22:52:01 +010055 map_codec:encode_addr_string(AddrOutIntl).
Harald Weltef9629642011-02-10 20:44:46 +010056
57patch_map_isdn_digits(AddrIn, _Type, []) ->
58 AddrIn;
59patch_map_isdn_digits(AddrIn, TypeIn, [Head|Tail]) ->
60 case Head of
61 {TypeIn, _,_, MscSide, StpSide} ->
62 if AddrIn == MscSide ->
63 StpSide;
64 AddrIn == StpSide ->
65 MscSide;
66 true ->
67 patch_map_isdn_digits(AddrIn, TypeIn, Tail)
68 end;
69 _ ->
70 patch_map_isdn_digits(AddrIn, TypeIn, Tail)
71 end.
Harald Weltea68d96e2011-02-10 09:49:46 +010072
73mangle_msisdn(from_stp, _Opcode, AddrIn) ->
74 {ok, IntPfx} = application:get_env(intern_pfx),
Harald Welte6a33c1e2011-02-10 21:40:40 +010075 mgw_nat:isup_party_internationalize(AddrIn, IntPfx).
Harald Weltea68d96e2011-02-10 09:49:46 +010076
Harald Welte449a3172011-02-10 15:06:27 +010077% Someobdy inquires on Routing Info for a MS (from HLR)
Harald Weltee78474e2011-02-10 21:04:52 +010078patch(#'SendRoutingInfoArg'{msisdn = Msisdn,'gmsc-OrGsmSCF-Address'=GmscAddr} = P) ->
79 % First Translate the MSISDN into international
Harald Weltea68d96e2011-02-10 09:49:46 +010080 AddrInDec = map_codec:parse_addr_string(Msisdn),
81 io:format("MSISDN IN = ~p~n", [AddrInDec]),
82 AddrOutDec = mangle_msisdn(from_stp, 22, AddrInDec),
83 io:format("MSISDN OUT = ~p~n", [AddrOutDec]),
84 AddrOutBin = map_codec:encode_addr_string(AddrOutDec),
Harald Weltee78474e2011-02-10 21:04:52 +010085 % Second, try to masquerade the G-MSC
86 GmscInDec = map_codec:parse_addr_string(GmscAddr),
87 case sccp_masq:lookup_masq_addr(orig, GmscInDec#party_number.phone_number) of
88 undef ->
89 GmscOut = GmscAddr;
90 GmscOutDigits ->
91 GmscOutDec = GmscInDec#party_number{phone_number = GmscOutDigits},
92 GmscOut = map_codec:encode_addr_string(GmscOutDec)
93 end,
94 P#'SendRoutingInfoArg'{msisdn = AddrOutBin, 'gmsc-OrGsmSCF-Address' = GmscOut};
Harald Weltea68d96e2011-02-10 09:49:46 +010095
Harald Welte449a3172011-02-10 15:06:27 +010096% HLR responds with Routing Info for a MS
97patch(#'SendRoutingInfoRes'{extendedRoutingInfo = ExtRoutInfo,
Holger Hans Peter Freyther016e3062011-02-21 22:04:38 +010098 subscriberInfo = SubscriberInfo,
Harald Welte449a3172011-02-10 15:06:27 +010099 'vmsc-Address' = VmscAddress} = P) ->
Harald Weltef9629642011-02-10 20:44:46 +0100100 VmscAddrOut = patch_map_isdn_addr(VmscAddress, msc),
Harald Welte449a3172011-02-10 15:06:27 +0100101 P#'SendRoutingInfoRes'{extendedRoutingInfo = patch(ExtRoutInfo),
Holger Hans Peter Freyther016e3062011-02-21 22:04:38 +0100102 'subscriberInfo' = patch(SubscriberInfo),
Harald Weltef9629642011-02-10 20:44:46 +0100103 'vmsc-Address' = VmscAddrOut};
Harald Welte449a3172011-02-10 15:06:27 +0100104patch(#'CamelRoutingInfo'{gmscCamelSubscriptionInfo = GmscCamelSI} = P) ->
105 P#'CamelRoutingInfo'{gmscCamelSubscriptionInfo = patch(GmscCamelSI)};
106patch({camelRoutingInfo, CRI}) ->
107 {camelRoutingInfo, patch(CRI)};
108patch({routingInfo, RI}) ->
109 {routingInfo, patch(RI)};
110
Harald Weltef9629642011-02-10 20:44:46 +0100111% HLR responds to inquiring MSC indicating the current serving MSC number
112patch(#'RoutingInfoForSM-Res'{locationInfoWithLMSI = LocInf} = P) ->
113 P#'RoutingInfoForSM-Res'{locationInfoWithLMSI = patch(LocInf)};
114patch(#'LocationInfoWithLMSI'{'networkNode-Number' = NetNodeNr} = P) ->
115 NetNodeNrOut = patch_map_isdn_addr(NetNodeNr, msc),
116 P#'LocationInfoWithLMSI'{'networkNode-Number' = NetNodeNrOut};
117
Harald Weltedf81bdd2011-02-10 17:21:26 +0100118% patch the roaming number as it is sent from HLR to G-MSC (SRI Resp)
119patch({roamingNumber, RoamNumTBCD}) ->
120 RoamNumIn = map_codec:parse_addr_string(RoamNumTBCD),
121 io:format("Roaming Number IN = ~p~n", [RoamNumIn]),
122 {ok, MsrnPfxStp} = application:get_env(msrn_pfx_stp),
123 {ok, MsrnPfxMsc} = application:get_env(msrn_pfx_msc),
Harald Welte6a33c1e2011-02-10 21:40:40 +0100124 RoamNumOut = mgw_nat:isup_party_replace_prefix(RoamNumIn, MsrnPfxMsc, MsrnPfxStp),
Harald Weltedf81bdd2011-02-10 17:21:26 +0100125 io:format("Roaming Number OUT = ~p~n", [RoamNumOut]),
126 RoamNumOutTBCD = map_codec:encode_addr_string(RoamNumOut),
127 {roamingNumber, RoamNumOutTBCD};
128
Harald Welte449a3172011-02-10 15:06:27 +0100129
Harald Weltea68d96e2011-02-10 09:49:46 +0100130% patch a UpdateGprsLocationArg and replace SGSN number and SGSN address
131% !!! TESTING ONLY !!!
Harald Weltef9629642011-02-10 20:44:46 +0100132patch(#'UpdateGprsLocationArg'{'sgsn-Number' = SgsnNum,
133 'sgsn-Address' = SgsnAddr} = P) ->
134 SgsnNumOut = patch_map_isdn_addr(SgsnNum, sgsn),
135 P#'UpdateGprsLocationArg'{'sgsn-Number'= SgsnNumOut,
136 'sgsn-Address' = SgsnAddr};
Harald Weltea68d96e2011-02-10 09:49:46 +0100137
138% Some other SGSN is sendingu us a GPRS location update. In the response,
139% we indicate teh HLR number, which we need to masquerade
Harald Weltef9629642011-02-10 20:44:46 +0100140patch(#'UpdateGprsLocationRes'{'hlr-Number' = HlrNum} = P) ->
141 HlrNumOut = patch_map_isdn_addr(HlrNum, hlr),
142 P#'UpdateGprsLocationRes'{'hlr-Number' = HlrNumOut};
Harald Weltea68d96e2011-02-10 09:49:46 +0100143
144% Some other MSC/VLR is sendingu us a GSM location update. In the response,
145% we indicate teh HLR number, which we need to masquerade
Harald Weltef9629642011-02-10 20:44:46 +0100146patch(#'UpdateLocationRes'{'hlr-Number' = HlrNum} = P) ->
147 HlrNumOut = patch_map_isdn_addr(HlrNum, hlr),
148 P#'UpdateLocationRes'{'hlr-Number' = HlrNumOut};
Harald Weltea68d96e2011-02-10 09:49:46 +0100149
150% HLR responds to VLR's MAP_RESTORE_REQ (i.e. it has lost information)
Harald Weltef9629642011-02-10 20:44:46 +0100151patch(#'RestoreDataRes'{'hlr-Number' = HlrNum} = P) ->
152 HlrNumOut = patch_map_isdn_addr(HlrNum, hlr),
153 P#'RestoreDataRes'{'hlr-Number' = HlrNumOut};
Harald Weltea68d96e2011-02-10 09:49:46 +0100154
155% HLR sends subscriber data to VLR/SGSN, including CAMEL info
156patch(#'InsertSubscriberDataArg'{'vlrCamelSubscriptionInfo'=VlrCamel,
157 'sgsn-CAMEL-SubscriptionInfo'=SgsnCamel} = Arg) ->
158 Arg#'InsertSubscriberDataArg'{'vlrCamelSubscriptionInfo'=patch(VlrCamel),
159 'sgsn-CAMEL-SubscriptionInfo'=patch(SgsnCamel)};
160
161% HLR sends subscriber data to gsmSCF
162patch(#'AnyTimeSubscriptionInterrogationRes'{'camel-SubscriptionInfo'=Csi} = P) ->
163 P#'AnyTimeSubscriptionInterrogationRes'{'camel-SubscriptionInfo'=patch(Csi)};
164
165patch(asn1_NOVALUE) ->
166 asn1_NOVALUE;
167
168% CAMEL related parsing
169
Harald Welte449a3172011-02-10 15:06:27 +0100170% this is part of the SRI Response (HLR->GMSC)
171patch(#'GmscCamelSubscriptionInfo'{'o-CSI'=Ocsi, 't-CSI'=Tcsi,
172 'd-csi'=Dcsi} = P) ->
173 P#'GmscCamelSubscriptionInfo'{'o-CSI'=patch(Ocsi),
174 't-CSI'=patch(Tcsi),
175 'd-csi'=patch(Dcsi)};
176
Harald Weltea68d96e2011-02-10 09:49:46 +0100177% this is part of the InsertSubscriberData HLR -> VLR
178patch(#'VlrCamelSubscriptionInfo'{'o-CSI'=Ocsi, 'mo-sms-CSI'=MoSmsCsi,
179 'mt-sms-CSI'=MtSmsCsi, 'ss-CSI'=SsCsi} = P) ->
180 P#'VlrCamelSubscriptionInfo'{'o-CSI'=patch(Ocsi),
181 'mo-sms-CSI'=patch(MoSmsCsi),
182 'mt-sms-CSI'=patch(MtSmsCsi),
183 'ss-CSI'=patch(SsCsi)};
184
185% this is part of the InsertSubscriberData HLR -> SGSN
186patch(#'SGSN-CAMEL-SubscriptionInfo'{'gprs-CSI'=GprsCsi,
187 'mo-sms-CSI'=MoSmsCsi,
188 'mt-sms-CSI'=MtSmsCsi} = P) ->
189 P#'SGSN-CAMEL-SubscriptionInfo'{'gprs-CSI'=patch(GprsCsi),
190 'mo-sms-CSI'=patch(MoSmsCsi),
191 'mt-sms-CSI'=patch(MtSmsCsi)};
192
193% this is part of the Anytime Subscription Interrogation Result HLR->gsmSCF
194patch(#'CAMEL-SubscriptionInfo'{'o-CSI'=Ocsi,
195 'd-CSI'=Dcsi,
196 't-CSI'=Tcsi,
197 'vt-CSI'=Vtcsi,
198 %'tif-CSI'=Tifcsi,
199 'gprs-CSI'=GprsCsi,
200 'mo-sms-CSI'=MoSmsCsi,
201 'ss-CSI'=SsCsi,
202 'm-CSI'=Mcsi,
203 'mt-sms-CSI'=MtSmsCsi,
204 'mg-csi'=MgCsi,
205 'o-IM-CSI'=OimCsi,
206 'd-IM-CSI'=DimCsi,
207 'vt-IM-CSI'=VtImCsi} = P) ->
208 P#'CAMEL-SubscriptionInfo'{'o-CSI'=patch(Ocsi),
209 'd-CSI'=patch(Dcsi),
210 't-CSI'=patch(Tcsi),
211 'vt-CSI'=patch(Vtcsi),
212 'gprs-CSI'=patch(GprsCsi),
213 'mo-sms-CSI'=patch(MoSmsCsi),
214 'ss-CSI'=patch(SsCsi),
215 'm-CSI'=patch(Mcsi),
216 'mt-sms-CSI'=patch(MtSmsCsi),
217 'mg-csi'=patch(MgCsi),
218 'o-IM-CSI'=patch(OimCsi),
219 'd-IM-CSI'=patch(DimCsi),
220 'vt-IM-CSI'=patch(VtImCsi)};
221
222patch(#'T-CSI'{'t-BcsmCamelTDPDataList'=TdpList} = P) ->
223 P#'T-CSI'{'t-BcsmCamelTDPDataList'=patch_tBcsmCamelTDPDataList(TdpList)};
224patch(#'M-CSI'{'gsmSCF-Address'=GsmScfAddr} = P) ->
Harald Weltef9629642011-02-10 20:44:46 +0100225 GsmScfAddrOut = patch_map_isdn_addr(GsmScfAddr, scf),
226 P#'M-CSI'{'gsmSCF-Address'=GsmScfAddrOut};
Harald Weltea68d96e2011-02-10 09:49:46 +0100227patch(#'MG-CSI'{'gsmSCF-Address'=GsmScfAddr} = P) ->
Harald Weltef9629642011-02-10 20:44:46 +0100228 GsmScfAddrOut = patch_map_isdn_addr(GsmScfAddr, scf),
229 P#'MG-CSI'{'gsmSCF-Address'=GsmScfAddrOut};
Harald Weltea68d96e2011-02-10 09:49:46 +0100230patch(#'O-CSI'{'o-BcsmCamelTDPDataList'=TdpList} = P) ->
231 P#'O-CSI'{'o-BcsmCamelTDPDataList'=patch_oBcsmCamelTDPDataList(TdpList)};
232patch(#'D-CSI'{'dp-AnalysedInfoCriteriaList'=List} = P) ->
233 P#'D-CSI'{'dp-AnalysedInfoCriteriaList'=patch_AnInfoCritList(List)};
234patch(#'SMS-CSI'{'sms-CAMEL-TDP-DataList'=TdpList} = P) ->
235 P#'SMS-CSI'{'sms-CAMEL-TDP-DataList'=patch_SmsCamelTDPDataList(TdpList)};
236patch(#'SS-CSI'{'ss-CamelData'=Sscd} = P) ->
237 P#'SS-CSI'{'ss-CamelData'=patch(Sscd)};
238patch(#'GPRS-CSI'{'gprs-CamelTDPDataList'=TdpList} = P) ->
239 P#'GPRS-CSI'{'gprs-CamelTDPDataList'=patch_GprsCamelTDPDataList(TdpList)};
240patch(#'SS-CamelData'{'gsmSCF-Address'=GsmScfAddr} = P) ->
Harald Weltef9629642011-02-10 20:44:46 +0100241 GsmScfAddrOut = patch_map_isdn_addr(GsmScfAddr, scf),
242 P#'SS-CamelData'{'gsmSCF-Address'=GsmScfAddrOut};
Harald Weltea68d96e2011-02-10 09:49:46 +0100243patch(#'O-BcsmCamelTDPData'{'gsmSCF-Address'=GsmScfAddr} = P) ->
Harald Weltef9629642011-02-10 20:44:46 +0100244 GsmScfAddrOut = patch_map_isdn_addr(GsmScfAddr, scf),
245 P#'O-BcsmCamelTDPData'{'gsmSCF-Address'=GsmScfAddrOut};
Holger Hans Peter Freyther60e0fb22011-02-21 21:02:08 +0100246patch(#'T-BcsmCamelTDPData'{'gsmSCF-Address'=GsmScfAddr} = P) ->
247 GsmScfAddrOut = patch_map_isdn_addr(GsmScfAddr, scf),
248 P#'T-BcsmCamelTDPData'{'gsmSCF-Address'=GsmScfAddrOut};
Harald Weltea68d96e2011-02-10 09:49:46 +0100249patch(#'SMS-CAMEL-TDP-Data'{'gsmSCF-Address'=GsmScfAddr} = P) ->
Harald Weltef9629642011-02-10 20:44:46 +0100250 GsmScfAddrOut = patch_map_isdn_addr(GsmScfAddr, scf),
251 P#'SMS-CAMEL-TDP-Data'{'gsmSCF-Address'=GsmScfAddrOut};
Harald Weltea68d96e2011-02-10 09:49:46 +0100252patch(#'GPRS-CamelTDPData'{'gsmSCF-Address'=GsmScfAddr} = P) ->
Harald Weltef9629642011-02-10 20:44:46 +0100253 GsmScfAddrOut = patch_map_isdn_addr(GsmScfAddr, scf),
254 P#'GPRS-CamelTDPData'{'gsmSCF-Address'=GsmScfAddrOut};
Harald Weltea68d96e2011-02-10 09:49:46 +0100255patch(#'DP-AnalysedInfoCriterium'{'gsmSCF-Address'=GsmScfAddr} = P) ->
Harald Weltef9629642011-02-10 20:44:46 +0100256 GsmScfAddrOut = patch_map_isdn_addr(GsmScfAddr, scf),
257 P#'DP-AnalysedInfoCriterium'{'gsmSCF-Address'=GsmScfAddrOut};
Holger Hans Peter Freyther016e3062011-02-21 22:04:38 +0100258patch(#'SubscriberInfo'{'locationInformation'=LocInformation} = P) ->
259 P#'SubscriberInfo'{'locationInformation'=patch(LocInformation)};
260patch(#'LocationInformation'{'vlr-number'=VlrNumber} = P) ->
261 VlrNumberOut = patch_map_isdn_addr(VlrNumber, vlr),
262 P#'LocationInformation'{'vlr-number'=VlrNumberOut};
263
Harald Weltec7523622011-02-10 14:42:31 +0100264patch(Default) ->
265 Default.
Harald Weltea68d96e2011-02-10 09:49:46 +0100266
267patch_oBcsmCamelTDPDataList(List) ->
268 % we reverse the origianl list, as the tail recursive _acc function
269 % will invert the order of components again
270 patch_oBcsmCamelTDPDataList_acc(lists:reverse(List), []).
271patch_oBcsmCamelTDPDataList_acc([], NewList) -> NewList;
272patch_oBcsmCamelTDPDataList_acc([TdpData|Tail], NewList) ->
273 NewTdpData = patch(TdpData#'O-BcsmCamelTDPData'{}),
274 patch_oBcsmCamelTDPDataList_acc(Tail, [NewTdpData|NewList]).
275
276patch_tBcsmCamelTDPDataList(List) ->
277 % we reverse the origianl list, as the tail recursive _acc function
278 % will invert the order of components again
279 patch_tBcsmCamelTDPDataList_acc(lists:reverse(List), []).
280patch_tBcsmCamelTDPDataList_acc([], NewList) -> NewList;
281patch_tBcsmCamelTDPDataList_acc([TdpData|Tail], NewList) ->
282 NewTdpData = patch(TdpData#'T-BcsmCamelTDPData'{}),
283 patch_tBcsmCamelTDPDataList_acc(Tail, [NewTdpData|NewList]).
284
285patch_AnInfoCritList(List) ->
286 % we reverse the origianl list, as the tail recursive _acc function
287 % will invert the order of components again
288 patch_AnInfoCritList_acc(lists:reverse(List), []).
289patch_AnInfoCritList_acc([], NewList) -> NewList;
290patch_AnInfoCritList_acc([Crit|Tail], NewList) ->
291 NewCrit = patch(Crit#'DP-AnalysedInfoCriterium'{}),
292 patch_AnInfoCritList_acc(Tail, [NewCrit|NewList]).
293
294patch_GprsCamelTDPDataList(List) ->
295 % we reverse the origianl list, as the tail recursive _acc function
296 % will invert the order of components again
297 patch_GprsCamelTDPDataList_acc(lists:reverse(List), []).
298patch_GprsCamelTDPDataList_acc([], NewList) -> NewList;
299patch_GprsCamelTDPDataList_acc([TdpData|Tail], NewList) ->
300 NewTdpData = patch(TdpData#'GPRS-CamelTDPData'{}),
301 patch_GprsCamelTDPDataList_acc(Tail, [NewTdpData|NewList]).
302
303patch_SmsCamelTDPDataList(List) ->
304 % we reverse the origianl list, as the tail recursive _acc function
305 % will invert the order of components again
306 patch_SmsCamelTDPDataList_acc(lists:reverse(List), []).
307patch_SmsCamelTDPDataList_acc([], NewList) -> NewList;
308patch_SmsCamelTDPDataList_acc([TdpData|Tail], NewList) ->
309 NewTdpData = patch(TdpData#'SMS-CAMEL-TDP-Data'{}),
310 patch_GprsCamelTDPDataList_acc(Tail, [NewTdpData|NewList]).
311
312
313
314% process the Argument of a particular MAP invocation
Harald Welte2cc831f2011-02-10 18:36:02 +0100315process_component_arg(_From, OpCode, Arg) ->
Harald Weltea68d96e2011-02-10 09:49:46 +0100316 case Arg of
317 asn1_NOVALUE -> Arg;
318 _ -> patch(Arg)
319 end.
320
321% recurse over all components
Harald Welte2cc831f2011-02-10 18:36:02 +0100322handle_tcap_components(_From, asn1_NOVALUE) ->
Harald Welte17f64742011-02-10 18:29:59 +0100323 asn1_NOVALUE;
Harald Welte2cc831f2011-02-10 18:36:02 +0100324handle_tcap_components(From, List) ->
Harald Weltea68d96e2011-02-10 09:49:46 +0100325 % we reverse the origianl list, as the tail recursive _acc function
326 % will invert the order of components again
Harald Welte2cc831f2011-02-10 18:36:02 +0100327 handle_tcap_components_acc(From, lists:reverse(List), []).
328handle_tcap_components_acc(_From, [], NewComponents) -> NewComponents;
329handle_tcap_components_acc(From, [Component|Tail], NewComponents) ->
Harald Weltea68d96e2011-02-10 09:49:46 +0100330 case Component of
331 {basicROS, {Primitive, Body}} ->
332 io:format("handle component ~p primitive ~n", [Component]),
333 case Body of
334 % BEGIN
335 #'MapSpecificPDUs_begin_components_SEQOF_basicROS_invoke'{opcode={local, OpCode},
336 argument=Arg} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100337 NewArg = process_component_arg(From, OpCode, Arg),
Harald Weltea68d96e2011-02-10 09:49:46 +0100338 NewBody = Body#'MapSpecificPDUs_begin_components_SEQOF_basicROS_invoke'{argument=NewArg};
Harald Welteea193142011-02-10 15:40:36 +0100339 #'MapSpecificPDUs_begin_components_SEQOF_basicROS_returnResult'{result=#'MapSpecificPDUs_begin_components_SEQOF_basicROS_returnResult_result'{opcode={local, OpCode}, result=Arg} = R} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100340 NewArg = process_component_arg(From, OpCode, Arg),
Harald Welteea193142011-02-10 15:40:36 +0100341 NewBody = Body#'MapSpecificPDUs_begin_components_SEQOF_basicROS_returnResult'{result=R#'MapSpecificPDUs_begin_components_SEQOF_basicROS_returnResult_result'{result=NewArg}};
342 #'MapSpecificPDUs_begin_components_SEQOF_returnResultNotLast'{result=#'MapSpecificPDUs_begin_components_SEQOF_returnResultNotLast_result'{opcode={local, OpCode}, result=Arg} = R} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100343 NewArg = process_component_arg(From, OpCode, Arg),
Harald Welteea193142011-02-10 15:40:36 +0100344 NewBody = Body#'MapSpecificPDUs_begin_components_SEQOF_returnResultNotLast'{result=R#'MapSpecificPDUs_begin_components_SEQOF_returnResultNotLast_result'{result=NewArg}};
Harald Weltea68d96e2011-02-10 09:49:46 +0100345 % END
346 #'MapSpecificPDUs_end_components_SEQOF_basicROS_invoke'{opcode={local, OpCode},
347 argument=Arg} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100348 NewArg = process_component_arg(From, OpCode, Arg),
Harald Weltea68d96e2011-02-10 09:49:46 +0100349 NewBody = Body#'MapSpecificPDUs_end_components_SEQOF_basicROS_invoke'{argument=NewArg};
Harald Welteea193142011-02-10 15:40:36 +0100350 #'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult'{result=#'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult_result'{opcode={local, OpCode}, result=Arg} = R} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100351 NewArg = process_component_arg(From, OpCode, Arg),
Harald Welteea193142011-02-10 15:40:36 +0100352 NewBody = Body#'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult'{result=R#'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult_result'{result=NewArg}};
353 #'MapSpecificPDUs_end_components_SEQOF_returnResultNotLast'{result=#'MapSpecificPDUs_end_components_SEQOF_returnResultNotLast_result'{opcode={local, OpCode}, result=Arg} = R} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100354 NewArg = process_component_arg(From, OpCode, Arg),
Harald Welteea193142011-02-10 15:40:36 +0100355 NewBody = Body#'MapSpecificPDUs_end_components_SEQOF_returnResultNotLast'{result=R#'MapSpecificPDUs_end_components_SEQOF_returnResultNotLast_result'{result=NewArg}};
Harald Weltea68d96e2011-02-10 09:49:46 +0100356 % CONTINUE
357 #'MapSpecificPDUs_continue_components_SEQOF_basicROS_invoke'{opcode={local, OpCode},
358 argument=Arg} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100359 NewArg = process_component_arg(From, OpCode, Arg),
Harald Weltea68d96e2011-02-10 09:49:46 +0100360 NewBody = Body#'MapSpecificPDUs_continue_components_SEQOF_basicROS_invoke'{argument=NewArg};
Harald Welteea193142011-02-10 15:40:36 +0100361 #'MapSpecificPDUs_continue_components_SEQOF_basicROS_returnResult'{result=#'MapSpecificPDUs_continue_components_SEQOF_basicROS_returnResult_result'{opcode={local, OpCode}, result=Arg} = R} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100362 NewArg = process_component_arg(From, OpCode, Arg),
Harald Welteea193142011-02-10 15:40:36 +0100363 NewBody = Body#'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult'{result=R#'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult_result'{result=NewArg}};
364 #'MapSpecificPDUs_continue_components_SEQOF_returnResultNotLast'{result=#'MapSpecificPDUs_continue_components_SEQOF_returnResultNotLast_result'{opcode={local, OpCode}, result=Arg} = R} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100365 NewArg = process_component_arg(From, OpCode, Arg),
Harald Welteea193142011-02-10 15:40:36 +0100366 NewBody = Body#'MapSpecificPDUs_continue_components_SEQOF_returnResultNotLast'{result=R#'MapSpecificPDUs_continue_components_SEQOF_returnResultNotLast_result'{result=NewArg}};
Harald Weltea68d96e2011-02-10 09:49:46 +0100367 _ ->
368 NewBody = Body
369 end,
370 %NewBody = setelement(5, Body, NewArg)
371 NewComponent = {basicROS, {Primitive, NewBody}};
372 _ ->
373 NewComponent = Component
374 end,
375 io:format("=> modified component ~p~n", [NewComponent]),
Harald Welte2cc831f2011-02-10 18:36:02 +0100376 handle_tcap_components_acc(From, Tail, [NewComponent|NewComponents]).
Harald Weltea68d96e2011-02-10 09:49:46 +0100377
378
Harald Welte9bc2e4a2011-02-10 12:40:31 +0100379% Erlang asn1rt has this strange property that all incoming EXTERNAL types are
380% converted from the 1990 version into the 1994 version. The latter does not
381% preserve the encoding (octet string, single ASN1 type, ...). During encoding,
382% it then uses the OCTTET-STRING encoding, which is different from the MAP
383% customary single-ASN1-type format.
Harald Weltec506fe52011-02-10 13:09:44 +0100384asn1_EXTERNAL1994_fixup({'EXTERNAL', DirRef, IndRef, Data}) when is_list(Data);is_binary(Data) ->
Harald Welte9bc2e4a2011-02-10 12:40:31 +0100385 % our trick is as follows: we simply convert back to 1990 format, and explicitly
386 % set the single-ASN1-type encoding. asn1rt:s 'enc_EXTERNAL'() will detect this
387 #'EXTERNAL'{'direct-reference' = DirRef, 'indirect-reference' = IndRef,
388 'encoding' = {'single-ASN1-type', Data}};
389asn1_EXTERNAL1994_fixup(Foo) ->
390 Foo.
391
392
Harald Welte2cc831f2011-02-10 18:36:02 +0100393handle_tcap_dialogue(_From, Foo = {'EXTERNAL', DirRef, IndRef, Data}) ->
Harald Welte9bc2e4a2011-02-10 12:40:31 +0100394 asn1_EXTERNAL1994_fixup(Foo);
Harald Welte2cc831f2011-02-10 18:36:02 +0100395handle_tcap_dialogue(_From, Foo) ->
Harald Welte9bc2e4a2011-02-10 12:40:31 +0100396 Foo.
397
Harald Weltea68d96e2011-02-10 09:49:46 +0100398
399%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400% Actual mangling of the decoded MAP messages
401%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Harald Welte2cc831f2011-02-10 18:36:02 +0100402mangle_map(From, {Type, TcapMsgDec}) ->
Harald Weltea68d96e2011-02-10 09:49:46 +0100403 case {Type, TcapMsgDec} of
404 {'unidirectional', #'MapSpecificPDUs_unidirectional'{dialoguePortion=Dialg,
405 components=Components}} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100406 NewDialg = handle_tcap_dialogue(From, Dialg),
407 NewComponents = handle_tcap_components(From, Components),
Harald Welte9bc2e4a2011-02-10 12:40:31 +0100408 NewTcapMsgDec = TcapMsgDec#'MapSpecificPDUs_unidirectional'{dialoguePortion=NewDialg, components=NewComponents};
Harald Welte29c5f402011-02-10 13:24:49 +0100409 {'begin', #'MapSpecificPDUs_begin'{dialoguePortion=Dialg, components=Components}} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100410 NewDialg = handle_tcap_dialogue(From, Dialg),
411 NewComponents = handle_tcap_components(From, Components),
Harald Welte29c5f402011-02-10 13:24:49 +0100412 NewTcapMsgDec = TcapMsgDec#'MapSpecificPDUs_begin'{dialoguePortion=NewDialg, components=NewComponents};
Harald Weltea68d96e2011-02-10 09:49:46 +0100413 {'continue', #'MapSpecificPDUs_continue'{dialoguePortion=Dialg, components=Components}} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100414 NewDialg = handle_tcap_dialogue(From, Dialg),
415 NewComponents = handle_tcap_components(From, Components),
Harald Welte9bc2e4a2011-02-10 12:40:31 +0100416 NewTcapMsgDec = TcapMsgDec#'MapSpecificPDUs_continue'{dialoguePortion=NewDialg, components=NewComponents};
Harald Welte29c5f402011-02-10 13:24:49 +0100417 {'end', #'MapSpecificPDUs_end'{dialoguePortion=Dialg, components=Components}} ->
Harald Welte2cc831f2011-02-10 18:36:02 +0100418 NewDialg = handle_tcap_dialogue(From, Dialg),
419 NewComponents = handle_tcap_components(From, Components),
Harald Welte29c5f402011-02-10 13:24:49 +0100420 NewTcapMsgDec = TcapMsgDec#'MapSpecificPDUs_end'{dialoguePortion=NewDialg, components=NewComponents};
421 %{_, #'Abort'{reason=Reason} ->
Harald Weltea68d96e2011-02-10 09:49:46 +0100422 _ ->
423 NewTcapMsgDec = TcapMsgDec
424 end,
Harald Welte29c5f402011-02-10 13:24:49 +0100425 io:format("new TcapMsgDec (Type=~p) ~p~n", [Type, NewTcapMsgDec]),
Harald Weltea68d96e2011-02-10 09:49:46 +0100426 {Type, NewTcapMsgDec}.
427
Harald Weltef9629642011-02-10 20:44:46 +0100428
429% Configuration file has changed, re-generate internal data structures
430config_update() ->
431 % (re-)generate the MAP Address rewrite table
Harald Welte6009f6e2011-02-10 21:41:50 +0100432 {ok, MapRewriteTbl} = application:get_env(mgw_nat, map_rewrite_table),
Harald Weltef9629642011-02-10 20:44:46 +0100433 MapRewriteTblOut = generate_rewrite_table(MapRewriteTbl),
Harald Welte6009f6e2011-02-10 21:41:50 +0100434 application:set_env(mgw_nat, map_rewrite_table, MapRewriteTblOut),
Harald Weltef9629642011-02-10 20:44:46 +0100435 %{ok, MsrnPfxStp} = application:get_env(msrn_pfx_stp),
436 %{ok, MsrnPfxMsc} = application:get_env(msrn_pfx_msc),
437 %{ok, IntPfx} = application:get_env(intern_pfx),
438 ok.
439
440% Generate the full MAP address rewrite table
441generate_rewrite_table(List) when is_list(List) ->
442 generate_rewrite_table(List, []).
443generate_rewrite_table([], OutList) ->
444 io:format("(Re)generated MAP ISDN-Address rewrite table: ~p~n", [OutList]),
445 OutList;
446generate_rewrite_table([Head|Tail], OutList) ->
447 NewItem = generate_rewrite_entry(Head),
448 generate_rewrite_table(Tail, [NewItem|OutList]).
449
450% Generate a MAP Address rewrite table entry
451generate_rewrite_entry({Name, MscSideInt, StpSideInt}) ->
452 MscSideList = osmo_util:int2digit_list(MscSideInt),
453 StpSideList = osmo_util:int2digit_list(StpSideInt),
454 {Name, MscSideInt, StpSideInt, MscSideList, StpSideList}.