aboutsummaryrefslogtreecommitdiffstats
path: root/src/tcap_udp_server.erl
blob: 9abc03ffa2928de5bad7d0d3db4e0db751d7ec81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
% MAP masquerading application

% (C) 2010 by Harald Welte <laforge@gnumonks.org>
% (C) 2010 by On-Waves
%
% All Rights Reserved
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License along
% with this program; if not, write to the Free Software Foundation, Inc.,
% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 

-module(tcap_udp_server).
-compile(export_all).

-define(PATCH_HLR_NUMBER, [1]).
-define(PATCH_SGSN_NUMBER, [2]).
-define(PATCH_SGSN_ADDRESS, [3]).
-define(PATCH_VMSC_ADDRESS, [4]).
-define(PATCH_GSMSCF_ADDRESS, [5]).

-include("MAP.hrl").

init(ServerPort) ->
	{ok, Sock} = gen_udp:open(ServerPort, [binary, inet]),
	loop().

% patch a UpdateGprsLocationArg and replace SGSN number and SGSN address
% !!! TESTING ONLY !!!
patch_UpdateGprsLocationArg(Arg) ->
	Arg1 = Arg#'UpdateGprsLocationArg'{'sgsn-Number' = ?PATCH_SGSN_NUMBER},
	Arg1#'UpdateGprsLocationArg'{'sgsn-Address' = ?PATCH_SGSN_ADDRESS}.


% Some other SGSN is sendingu us a GPRS location update.  In the response,
% we indicate teh HLR number, which we need to masquerade
patch_UpdateGprsLocationRes(Arg) ->
	Arg#'UpdateGprsLocationRes'{'hlr-Number' = ?PATCH_HLR_NUMBER}.

% Some other MSC/VLR is sendingu us a GSM location update.  In the response,
% we indicate teh HLR number, which we need to masquerade
patch_UpdateLocationRes(Arg) ->
	Arg#'UpdateLocationRes'{'hlr-Number' = ?PATCH_HLR_NUMBER}.

% HLR responds to VLR's MAP_RESTORE_REQ (i.e. it has lost information)
patch_RestoreDataRes(Arg) ->
	Arg#'RestoreDataRes'{'hlr-Number' = ?PATCH_HLR_NUMBER}.

% HLR sends subscriber data to VLR/SGSN, including CAMEL info
patch_InsertSubscriberDataArg(Arg) ->
	VlrCamel = Arg#'InsertSubscriberDataArg'.'vlrCamelSubscriptionInfo',
	Arg1 = Arg#'InsertSubscriberDataArg'{'vlrCamelSubscriptionInfo' = patch_vlrCamelSubscrInfo(VlrCamel)},
	%SgsnCamel = Arg#'InsertSubscriberDataArg'.'sgsn-CAMEL-SubscriptionInfo',
	%Arg#'InsertSubscriberDataArg'{'sgsn-CAMEL-SubscriptionInfo' = patch_sgsnCamelSubscrInfo(SgsnCamel)},
	Arg1.


% CAMEL related parsing

patch_oBcsmCamelTDPDataList(List) ->
	% we reverse the origianl list, as the tail recursive _acc function
	% will invert the order of components again
	patch_oBcsmCamelTDPDataList_acc(lists:reverse(List), []).
patch_oBcsmCamelTDPDataList_acc([], NewList) -> NewList;
patch_oBcsmCamelTDPDataList_acc([TdpData|Tail], NewList) ->
	case TdpData of
		#'O-BcsmCamelTDPData'{'gsmSCF-Address'=ScfAddr} ->
			NewTdpData = TdpData#'O-BcsmCamelTDPData'{'gsmSCF-Address'=?PATCH_GSMSCF_ADDRESS};
		_ ->
			NewTdpData = TdpData
	end,
	patch_oBcsmCamelTDPDataList_acc(Tail, [NewTdpData|NewList]).


patch_GprsCamelTDPDataList(List) ->
	% we reverse the origianl list, as the tail recursive _acc function
	% will invert the order of components again
	patch_GprsCamelTDPDataList_acc(lists:reverse(List), []).
patch_GprsCamelTDPDataList_acc([], NewList) -> NewList;
patch_GprsCamelTDPDataList_acc([TdpData|Tail], NewList) ->
	case TdpData of
		#'GPRS-CamelTDPData'{'gsmSCF-Address'=ScfAddr} ->
			NewTdpData = TdpData#'GPRS-CamelTDPData'{'gsmSCF-Address'=?PATCH_GSMSCF_ADDRESS};
		_ ->
			NewTdpData = TdpData
	end,
	patch_GprsCamelTDPDataList_acc(Tail, [NewTdpData|NewList]).


patch_SmsCamelTDPDataList(List) ->
	% we reverse the origianl list, as the tail recursive _acc function
	% will invert the order of components again
	patch_SmsCamelTDPDataList_acc(lists:reverse(List), []).
patch_SmsCamelTDPDataList_acc([], NewList) -> NewList;
patch_SmsCamelTDPDataList_acc([TdpData|Tail], NewList) ->
	case TdpData of
		#'SMS-CAMEL-TDP-Data'{'gsmSCF-Address'=ScfAddr} ->
			NewTdpData = TdpData#'SMS-CAMEL-TDP-Data'{'gsmSCF-Address'=?PATCH_GSMSCF_ADDRESS};
		_ ->
			NewTdpData = TdpData
	end,
	patch_GprsCamelTDPDataList_acc(Tail, [NewTdpData|NewList]).


patch_vlrCamelSubscrInfo(VlrCamel) ->
	case VlrCamel of
		asn1_NOVALUE ->
			% return the original (empty) VlrCamelSubscrInfo
			VlrCamel;
		#'VlrCamelSubscriptionInfo'{} ->
			case VlrCamel of
				#'VlrCamelSubscriptionInfo'{'o-CSI'={#'O-CSI'{'o-BcsmCamelTDPDataList'=TdpList}}} ->
					NewTdpList = patch_oBcsmCamelTDPDataList(TdpList),
					VlrCamel1 = VlrCamel#'VlrCamelSubscriptionInfo'{'o-CSI'={#'O-CSI'{'o-BcsmCamelTDPDataList'=NewTdpList}}};
				_ ->
					VlrCamel1 = VlrCamel
			end,
			case VlrCamel1 of
				#'VlrCamelSubscriptionInfo'{'mo-sms-CSI'={#'SMS-CSI'{'sms-CAMEL-TDP-DataList'=MoSmsTdpList}}} ->
					NewMoSmsTdpList = patch_SmsCamelTDPDataList(MoSmsTdpList),
					VlrCamel2 = VlrCamel1#'VlrCamelSubscriptionInfo'{'mo-sms-CSI'={#'SMS-CSI'{'sms-CAMEL-TDP-DataList'=NewMoSmsTdpList}}};
				_ ->
					VlrCamel2 = VlrCamel1
			end,
			case VlrCamel2 of
				#'VlrCamelSubscriptionInfo'{'mt-sms-CSI'={#'SMS-CSI'{'sms-CAMEL-TDP-DataList'=MtSmsTdpList}}} ->
					NewMtSmsTdpList = patch_SmsCamelTDPDataList(MtSmsTdpList),
					VlrCamel3 = VlrCamel2#'VlrCamelSubscriptionInfo'{'mt-sms-CSI'={#'SMS-CSI'{'sms-CAMEL-TDP-DataList'=NewMtSmsTdpList}}};
				_ ->
					VlrCamel3 = VlrCamel2
			end,
			case VlrCamel3 of
				#'VlrCamelSubscriptionInfo'{'ss-CSI'={#'SS-CSI'{'ss-CamelData'=#'SS-CamelData'{'gsmSCF-Address'=SSgsmSCF}}}} ->
					VlrCamel4 = #'VlrCamelSubscriptionInfo'{'ss-CSI'={#'SS-CSI'{'ss-CamelData'=#'SS-CamelData'{'gsmSCF-Address'=?PATCH_GSMSCF_ADDRESS}}}};
				_ ->
					VlrCamel4 = VlrCamel3
			end,
			% return the VlrCamelSubscrInfo with replaced TdpList
			VlrCamel4;
		_ ->
			% return the original VlrCamelSubscrInfo without TdpList
			VlrCamel
	end.


patch_sgsnCamelSubscrInfo(SgsnCamel) ->
	case SgsnCamel of
		asn1_NOVALUE ->
			% return the original (empty) SgsnCamelSubscrInfo
			SgsnCamel;
		#'SGSN-CAMEL-SubscriptionInfo'{} ->
			case SgsnCamel of #'SGSN-CAMEL-SubscriptionInfo'{'gprs-CSI'={#'GPRS-CSI'{'gprs-CamelTDPDataList'=GprsTdpList}}} ->
				NewGprsTdpList = patch_GprsCamelTDPDataList(GprsTdpList),
				SgsnCamel1 = SgsnCamel#'SGSN-CAMEL-SubscriptionInfo'{'gprs-CSI'={#'GPRS-CSI'{'gprs-CamelTDPDataList'=NewGprsTdpList}}};
			_ ->
				SgsnCamel1 = SgsnCamel
			end,
			case SgsnCamel1 of #'SGSN-CAMEL-SubscriptionInfo'{'mo-sms-CSI'={#'SMS-CSI'{'sms-CAMEL-TDP-DataList'=MoSmsTdpList}}} ->
				NewMoSmsTdpList = patch_SmsCamelTDPDataList(MoSmsTdpList),
				SgsnCamel2 = SgsnCamel1#'SGSN-CAMEL-SubscriptionInfo'{'mo-sms-CSI'={#'SMS-CSI'{'sms-CAMEL-TDP-DataList'=NewMoSmsTdpList}}};
			_ ->
				SgsnCamel2 = SgsnCamel1
			end,
			case SgsnCamel1 of #'SGSN-CAMEL-SubscriptionInfo'{'mt-sms-CSI'={#'SMS-CSI'{'sms-CAMEL-TDP-DataList'=MtSmsTdpList}}} ->
				NewMtSmsTdpList = patch_SmsCamelTDPDataList(MtSmsTdpList),
				SgsnCamel3 = SgsnCamel2#'SGSN-CAMEL-SubscriptionInfo'{'mt-sms-CSI'={#'SMS-CSI'{'sms-CAMEL-TDP-DataList'=NewMtSmsTdpList}}};
			_ ->
				SgsnCamel3 = SgsnCamel2
			end,
			SgsnCamel3;
		_ ->
			% return the original VlrCamelSubscrInfo without TdpList
			SgsnCamel
	end.


% process the Argument of a particular MAP invocation
process_component_arg(OpCode, Arg) ->
	case Arg of
		asn1_NOVALUE -> Arg;
		_ -> patch_UpdateGprsLocationArg(Arg)
	end.

% recurse over all components
handle_tcap_components(List) ->
	% we reverse the origianl list, as the tail recursive _acc function
	% will invert the order of components again
	handle_tcap_components_acc(lists:reverse(List), []).
handle_tcap_components_acc([], NewComponents) -> NewComponents;
handle_tcap_components_acc([Component|Tail], NewComponents) ->
	case Component of
		{basicROS, {Primitive, Body}} ->
			io:format("handle component ~p primitive ~n", [Component]),
			case Body of
				% BEGIN
				#'MapSpecificPDUs_begin_components_SEQOF_basicROS_invoke'{opcode={local, OpCode},
											  argument=Arg} ->
					NewArg = process_component_arg(OpCode, Arg),
					NewBody = Body#'MapSpecificPDUs_begin_components_SEQOF_basicROS_invoke'{argument=NewArg};
				#'MapSpecificPDUs_begin_components_SEQOF_basicROS_returnResult'{result=#'MapSpecificPDUs_begin_components_SEQOF_basicROS_returnResult_result'{opcode={local, OpCode}, result=Arg}} ->
					NewArg = process_component_arg(OpCode, Arg),
					NewBody = Body#'MapSpecificPDUs_begin_components_SEQOF_basicROS_returnResult'{result=#'MapSpecificPDUs_begin_components_SEQOF_basicROS_returnResult_result'{result=NewArg}};
				#'MapSpecificPDUs_begin_components_SEQOF_returnResultNotLast'{result=#'MapSpecificPDUs_begin_components_SEQOF_returnResultNotLast_result'{opcode={local, OpCode}, result=Arg}} ->
					NewArg = process_component_arg(OpCode, Arg),
					NewBody = Body#'MapSpecificPDUs_begin_components_SEQOF_returnResultNotLast'{result=#'MapSpecificPDUs_begin_components_SEQOF_returnResultNotLast_result'{result=NewArg}};
				% END
				#'MapSpecificPDUs_end_components_SEQOF_basicROS_invoke'{opcode={local, OpCode},
											argument=Arg} ->
					NewArg = process_component_arg(OpCode, Arg),
					NewBody = Body#'MapSpecificPDUs_end_components_SEQOF_basicROS_invoke'{argument=NewArg};
				#'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult'{result=#'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult_result'{opcode={local, OpCode}, result=Arg}} ->
					NewArg = process_component_arg(OpCode, Arg),
					NewBody = Body#'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult'{result=#'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult_result'{result=NewArg}};
				#'MapSpecificPDUs_end_components_SEQOF_returnResultNotLast'{result=#'MapSpecificPDUs_end_components_SEQOF_returnResultNotLast_result'{opcode={local, OpCode}, result=Arg}} ->
					NewArg = process_component_arg(OpCode, Arg),
					NewBody = Body#'MapSpecificPDUs_end_components_SEQOF_returnResultNotLast'{result=#'MapSpecificPDUs_end_components_SEQOF_returnResultNotLast_result'{result=NewArg}};
				% CONTINUE
				#'MapSpecificPDUs_continue_components_SEQOF_basicROS_invoke'{opcode={local, OpCode},
											     argument=Arg} ->
					NewArg = process_component_arg(OpCode, Arg),
					NewBody = Body#'MapSpecificPDUs_continue_components_SEQOF_basicROS_invoke'{argument=NewArg};
				#'MapSpecificPDUs_continue_components_SEQOF_basicROS_returnResult'{result=#'MapSpecificPDUs_continue_components_SEQOF_basicROS_returnResult_result'{opcode={local, OpCode}, result=Arg}} ->
					NewArg = process_component_arg(OpCode, Arg),
					NewBody = Body#'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult'{result=#'MapSpecificPDUs_end_components_SEQOF_basicROS_returnResult_result'{result=NewArg}};
				#'MapSpecificPDUs_continue_components_SEQOF_returnResultNotLast'{result=#'MapSpecificPDUs_continue_components_SEQOF_returnResultNotLast_result'{opcode={local, OpCode}, result=Arg}} ->
					NewArg = process_component_arg(OpCode, Arg),
					NewBody = Body#'MapSpecificPDUs_continue_components_SEQOF_returnResultNotLast'{result=#'MapSpecificPDUs_continue_components_SEQOF_returnResultNotLast_result'{result=NewArg}};
				_ ->
					NewBody = Body
			end,
			%NewBody = setelement(5, Body, NewArg)
			NewComponent = {basicROS, {Primitive, NewBody}};
		_ ->
			NewComponent = Component
	end,
	io:format("=> modified component ~p~n", [NewComponent]),
	handle_tcap_components_acc(Tail, [NewComponent|NewComponents]).
	

handle_tcap_msg_dec(Type, TcapMsgDec) ->
	case {Type, TcapMsgDec} of
		{'unidirectional', #'MapSpecificPDUs_unidirectional'{dialoguePortion=Dialg,
								     components=Components}} ->
			NewComponents = handle_tcap_components(Components),
			NewTcapMsgDec = TcapMsgDec#'MapSpecificPDUs_unidirectional'{components=NewComponents};
		{'begin', #'MapSpecificPDUs_begin'{components=Components}} ->
			NewComponents = handle_tcap_components(Components),
			NewTcapMsgDec = TcapMsgDec#'MapSpecificPDUs_begin'{components=NewComponents};
		{'continue', #'MapSpecificPDUs_continue'{dialoguePortion=Dialg, components=Components}} ->
			NewComponents = handle_tcap_components(Components),
			NewTcapMsgDec = TcapMsgDec#'MapSpecificPDUs_continue'{components=NewComponents};
		{'end', #'MapSpecificPDUs_end'{components=Components}} ->
			NewComponents = handle_tcap_components(Components),
			NewTcapMsgDec = TcapMsgDec#'MapSpecificPDUs_end'{components=NewComponents};
		_ ->
			NewTcapMsgDec = TcapMsgDec
	end,
	io:format("=> new TcapMsgDec ~p~n", [NewTcapMsgDec]),
	{Type, NewTcapMsgDec}.

handle_tcap_msg(PayloadL) ->
	io:format("aboout to tcap_msg_dec..."),
	case asn1rt:decode('MAP', 'MapSpecificPDUs', PayloadL) of
		{ok, {Type, TcapMsgDec}} ->
			io:format("success!~n"),
			handle_tcap_msg_dec(Type, TcapMsgDec);
		Error -> Error
	end.

loop() ->
	io:format("udp_server_loop~n"),
	receive {udp, Sock, ClientIP, ClientPort, Packet} ->
		io:format("UDP received from ~p:~p ==> ~p~n", [ClientIP, ClientPort, Packet]),
		handle_tcap_msg(binary_to_list(Packet))%,
		%loop()
	end.