summaryrefslogtreecommitdiffstats
path: root/library/SMPP_Emulation.ttcn
blob: 27876dc0fe29492bf0ed461e10975e45d68a18f5 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
module SMPP_Emulation {

/* SMPP Emulation layer, sitting on top of SMPP_CodecPort.
 *
 * (C) 2018 by Harald Welte <laforge@gnumonks.org>
 * All rights reserved.
 *
 * Released under the terms of GNU General Public License, Version 2 or
 * (at your option) any later version.
 */


import from Osmocom_Types all;
import from General_Types all;
import from SMPP_Types all;
import from SMPP_Templates all;
import from SMPP_CodecPort all;
import from SMPP_CodecPort_CtrlFunct all;
import from IPL4asp_Types all;
import from IPL4asp_PortType all;
import from Socket_API_Definitions all;

/* general "base class" component definition, of which specific implementations
 * derive themselves by menas of the "extends" feature */
type component SMPP_ConnHdlr {
	/* port towards SPPP_Emulation_CT */
	port SMPP_Conn_PT SMPP;
	port SMPPEM_PROC_PT SMPP_PROC;
}


type component SMPP_Emulation_CT {
	/* down-facing port to SMPP Codec port */
	port SMPP_CODEC_PT SMPP_PORT;
	var IPL4asp_Types.ConnectionId g_smpp_conn_id := -1;

	var integer g_seq := 1;

	/* up-facing port to Clients */
	port SMPP_Conn_PT SMPP_CLIENT;
	port SMPPEM_PROC_PT SMPP_PROC;

	var TransactionData TransactionTable[32];
	var ExpectData ExpectTable[32];
}

type port SMPP_Conn_PT message {
	inout SMPP_PDU;
} with { extension "internal" };

type record TransactionData {
	uint32_t	tid optional,
	SMPP_ConnHdlr	vc_conn
}

type record ExpectData {
	SMPP_TON	dst_ton optional,
	SMPP_NPI	dst_npi optional,
	charstring	dst_addr,
	SMPP_ConnHdlr	vc_conn
}

private function f_trans_id_known(uint32_t tid)
runs on SMPP_Emulation_CT return boolean {
	for (var integer i := 0; i < sizeof(TransactionTable); i := i+1) {
		if (TransactionTable[i].tid == tid) {
			return true;
		}
	}
	return false;
}

private function f_comp_known(SMPP_ConnHdlr client)
runs on SMPP_Emulation_CT return boolean {
	for (var integer i := 0; i < sizeof(TransactionTable); i := i+1) {
		if (TransactionTable[i].vc_conn == client) {
			return true;
		}
	}
	return false;
}

private function f_comp_by_trans_id(uint32_t tid)
runs on SMPP_Emulation_CT return SMPP_ConnHdlr {
	for (var integer i := 0; i < sizeof(TransactionTable); i := i+1) {
		if (TransactionTable[i].tid == tid) {
			return TransactionTable[i].vc_conn;
		}
	}
	setverdict(fail, "No componten for SMPP TID ", tid);
	self.stop;
}


private function f_trans_table_init()
runs on SMPP_Emulation_CT {
	for (var integer i := 0; i < sizeof(TransactionTable); i := i+1) {
		TransactionTable[i].vc_conn := null;
		TransactionTable[i].tid := omit;
	}
}

private function f_trans_table_add(SMPP_ConnHdlr vc_conn, uint32_t trans_id)
runs on SMPP_Emulation_CT {
	for (var integer i := 0; i < sizeof(TransactionTable); i := i+1) {
		if (TransactionTable[i].vc_conn == null) {
			TransactionTable[i].vc_conn := vc_conn;
			TransactionTable[i].tid := trans_id;
			return;
		}
	}
	setverdict(fail, "SMPP Trans table full!");
	self.stop;
}

private function f_trans_table_del(uint32_t trans_id)
runs on SMPP_Emulation_CT {
	for (var integer i := 0; i < sizeof(TransactionTable); i := i+1) {
		if (TransactionTable[i].tid == trans_id) {
			TransactionTable[i].vc_conn := null;
			TransactionTable[i].tid := omit;
			return;
		}
	}
	setverdict(fail, "SMPP Trans table attempt to delete non-existant ", trans_id);
	self.stop;
}



function f_connect(charstring remote_host, IPL4asp_Types.PortNumber remote_port,
		   charstring local_host, IPL4asp_Types.PortNumber local_port)
runs on SMPP_Emulation_CT {
	var IPL4asp_Types.Result res;
	res := SMPP_CodecPort_CtrlFunct.f_IPL4_connect(SMPP_PORT, remote_host, remote_port,
							local_host, local_port, 0, { tcp :={} });
	if (not ispresent(res.connId)) {
		setverdict(fail, "Could not connect to SMPP port, check your configuration");
		self.stop;
	}
	g_smpp_conn_id := res.connId;
}

/* Function to use to bind to a local port as IPA server, accepting remote clients */
function f_bind(charstring local_host, IPL4asp_Types.PortNumber local_port)
runs on SMPP_Emulation_CT {
	var IPL4asp_Types.Result res;
	res := SMPP_CodecPort_CtrlFunct.f_IPL4_listen(SMPP_PORT, local_host, local_port, { tcp:={} });
	g_smpp_conn_id := res.connId;
}


function main_server(EsmePars pars, charstring local_host, integer local_port)
runs on SMPP_Emulation_CT {
	f_bind(local_host, local_port);
	f_mainloop(pars);
}

function main_client(EsmePars pars, charstring remote_host, integer remote_port,
			charstring local_host, integer local_port)
runs on SMPP_Emulation_CT {
	f_connect(remote_host, remote_port, local_host, local_port);
	f_mainloop(pars);
}

type enumerated EsmeMode {
	MODE_TRANSMITTER,
	MODE_RECEIVER,
	MODE_TRANSCEIVER
}
type record EsmePars {
	EsmeMode	mode,
	SMPP_Bind	bind,
	boolean		esme_role
}

private function f_tx_smpp(template (value) SMPP_PDU pdu) runs on SMPP_Emulation_CT {
	pdu.header.seq_num := g_seq;
	SMPP_PORT.send(ts_SMPP_Send(g_smpp_conn_id, pdu));
	g_seq := g_seq+1;
}

private function f_rx_smpp(template SMPP_PDU pdu) runs on SMPP_Emulation_CT {
	timer T_wait := 3.0;
	T_wait.start;
	alt {
	[] SMPP_PORT.receive(tr_SMPP_Recv(g_smpp_conn_id, pdu)) { }
	[] T_wait.timeout {
		setverdict(fail, "Timeout waiting for ", pdu);
		self.stop;
		}
	}
}

/* default altstep which we use throughout */
private altstep as_smpp() runs on SMPP_Emulation_CT {
	var SMPP_ConnHdlr vc_conn;
	var SMPP_RecvFrom smpp_rf;
	/* Answer to ENQUIRE LINK */
	[] SMPP_PORT.receive(tr_SMPP_Recv(g_smpp_conn_id,
			     tr_SMPP(c_SMPP_command_id_enquire_link, ESME_ROK))) {
		f_tx_smpp(ts_SMPP_ENQ_LINK_resp);
		}
	[] SMPP_PORT.receive(tr_SMPP_Recv(g_smpp_conn_id,
			     tr_SMPP(c_SMPP_command_id_alert_notification, ESME_ROK))) -> value smpp_rf {
		/* TODO: dispatch to ConnHdlr based on some kind of expect mechanism? */
		vc_conn := f_exp_lookup(smpp_rf.msg.body.alert_notif.source_addr_ton,
					smpp_rf.msg.body.alert_notif.source_addr_npi,
					smpp_rf.msg.body.alert_notif.source_addr);
		SMPP_CLIENT.send(smpp_rf.msg) to vc_conn;
		}
	[] SMPP_PORT.receive {
		setverdict(fail, "Unexpected SMPP from peer");
		self.stop;
		}
}

function f_mainloop(EsmePars pars)
runs on SMPP_Emulation_CT {

	/* Set function for dissecting the binary stream into packets */
	var f_IPL4_getMsgLen vl_f := refers(f_IPL4_fixedMsgLen);
	/* Offset: 0, size of length: 4, delta: 0, multiplier: 1, big-endian */
	SMPP_CodecPort_CtrlFunct.f_IPL4_setGetMsgLen(SMPP_PORT, g_smpp_conn_id, vl_f, {0, 4, 0, 1, 0});

	f_trans_table_init();
	f_expect_table_init();

	/* activate default altstep */
	var default d := activate(as_smpp());

	if (pars.esme_role) {
		/* BIND to SMSC */
		select (pars.mode) {
		case (MODE_TRANSMITTER) {
			f_tx_smpp(ts_SMPP_BIND_TX(pars.bind));
			/* FIXME: do we have to check for SEQ? */
			f_rx_smpp(tr_SMPP(c_SMPP_command_id_bind_transmitter_resp, ESME_ROK, g_seq));
			}
		case (MODE_RECEIVER) {
			f_tx_smpp(ts_SMPP_BIND_RX(pars.bind));
			/* FIXME: do we have to check for SEQ? */
			f_rx_smpp(tr_SMPP(c_SMPP_command_id_bind_receiver_resp, ESME_ROK, g_seq));
			}
		case (MODE_TRANSCEIVER) {
			f_tx_smpp(ts_SMPP_BIND_TRX(pars.bind));
			/* FIXME: do we have to check for SEQ? */
			f_rx_smpp(tr_SMPP(c_SMPP_command_id_bind_transceiver_resp, ESME_ROK));
			}
		}
	} else {
		var SMPP_Bind_resp bresp := {
			system_id := pars.bind.system_id,
			opt_pars := {}
		}
		/* Expect bind from ESME */
		select (pars.mode) {
		case (MODE_TRANSMITTER) {
			f_rx_smpp(tr_SMPP_BIND_TX(pars.bind));
			/* FIXME: do we have to check for SEQ? */
			f_tx_smpp(ts_SMPP_BIND_TX_resp(ESME_ROK, bresp));
			}
		case (MODE_RECEIVER) {
			f_rx_smpp(tr_SMPP_BIND_RX(pars.bind));
			/* FIXME: do we have to check for SEQ? */
			f_tx_smpp(ts_SMPP_BIND_RX_resp(ESME_ROK, bresp));
			}
		case (MODE_TRANSCEIVER) {
			f_rx_smpp(tr_SMPP_BIND_TRX(pars.bind));
			/* FIXME: do we have to check for SEQ? */
			f_tx_smpp(ts_SMPP_BIND_TRX_resp(ESME_ROK, bresp));
			}
		}
	}

	while (true) {
		var SMPP_ConnHdlr vc_conn;
		var SMPP_RecvFrom smpp_rf;
		var SMPP_PDU smpp;
		var charstring dest_addr;
		alt {
		/* SMSC -> CLIENT: response, map by seq_nr */
		[pars.esme_role] SMPP_PORT.receive(tr_SMPP_Recv(g_smpp_conn_id,
							tr_SMPP_esme_resp)) -> value smpp_rf {
			var uint32_t trans_id := smpp_rf.msg.header.seq_num;
			if (f_trans_id_known(trans_id)) {
				vc_conn := f_comp_by_trans_id(trans_id);
				SMPP_CLIENT.send(smpp_rf.msg) to vc_conn;
				f_trans_table_del(trans_id);
			} else {
				log("Received SMPP response for unknown trans_id ", smpp_rf);
				/* FIXME */
			}
			}
		/* SMSC -> CLIENT: DELIVER-SM.req */
		[pars.esme_role] SMPP_PORT.receive(tr_SMPP_Recv(g_smpp_conn_id,
				tr_SMPP(c_SMPP_command_id_deliver_sm, ESME_ROK))) -> value smpp_rf {
			vc_conn := f_exp_lookup(smpp_rf.msg.body.deliver_sm.dest_addr_ton,
						smpp_rf.msg.body.deliver_sm.dest_addr_npi,
						smpp_rf.msg.body.deliver_sm.destination_addr);
			SMPP_CLIENT.send(smpp_rf.msg) to vc_conn;
			}

		/* record seq_nr for commands from CLIENT -> SMSC */
		[pars.esme_role] SMPP_CLIENT.receive(tr_SMPP_esme_req) -> value smpp sender vc_conn {
			/* register current seq_nr/trans_id */
			f_trans_table_add(vc_conn, g_seq);
			f_tx_smpp(smpp);
			}
		/* pass responses 1:1 through from CLIENT -> SMSC */
		[pars.esme_role] SMPP_CLIENT.receive(tr_SMPP_smsc_resp) -> value smpp sender vc_conn {
			SMPP_PORT.send(ts_SMPP_Send(g_smpp_conn_id, smpp));
			}

		[] SMPP_PROC.getcall(SMPPEM_register:{?,?}) -> param(dest_addr, vc_conn) {
			f_create_expect(dest_addr, vc_conn);
			SMPP_PROC.reply(SMPPEM_register:{dest_addr, vc_conn}) to vc_conn;
			}
		}
	}
}

/* Requests from ESME -> SMSC */
template OCT4 SMPP_esme_req := (
	c_SMPP_command_id_submit_sm,
	c_SMPP_command_id_replace_sm,
	c_SMPP_command_id_cancel_sm,
	c_SMPP_command_id_submit_multi
);
template SMPP_PDU tr_SMPP_esme_req := tr_SMPP(SMPP_esme_req, ?);

/* Responses from ESME -> SMSC */
template OCT4 SMPP_esme_resp := (
	c_SMPP_command_id_submit_sm_resp,
	c_SMPP_command_id_replace_sm_resp,
	c_SMPP_command_id_cancel_sm_resp,
	c_SMPP_command_id_submit_multi_resp
);
template SMPP_PDU tr_SMPP_esme_resp := tr_SMPP(SMPP_esme_resp, ?);

/* Requests from SMSC -> ESME */
template OCT4 SMPP_smsc_req := (
	c_SMPP_command_id_deliver_sm
);
template SMPP_PDU tr_SMPP_smsc_req := tr_SMPP(SMPP_smsc_req, ?);

/* Responses from SMSC -> ESME */
template OCT4 SMPP_smsc_resp := (
	c_SMPP_command_id_deliver_sm_resp
);
template SMPP_PDU tr_SMPP_smsc_resp := tr_SMPP(SMPP_smsc_resp, ?);



signature SMPPEM_register(charstring dst_addr, SMPP_ConnHdlr hdlr);

type port SMPPEM_PROC_PT procedure {
	inout SMPPEM_register;
} with { extension "internal" };

private function f_create_expect(charstring dest_number, SMPP_ConnHdlr hdlr)
runs on SMPP_Emulation_CT {
	for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
		if (ExpectTable[i].vc_conn == null) {
			ExpectTable[i] := {
				dst_ton := omit,
				dst_npi := omit,
				dst_addr := dest_number,
				vc_conn := hdlr
			}
			return;
		}
	}
	setverdict(fail, "No space left in SmppExpectTable");
	self.stop;
}

private function f_exp_lookup(SMPP_TON ton, SMPP_NPI npi, charstring dst)
runs on SMPP_Emulation_CT return SMPP_ConnHdlr {
	for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
		if (ExpectTable[i].vc_conn != null and ExpectTable[i].dst_addr == dst) {
			if (ispresent(ExpectTable[i].dst_ton) and ExpectTable[i].dst_ton != ton) {
				continue;
			}
			if (ispresent(ExpectTable[i].dst_npi) and ExpectTable[i].dst_npi != npi) {
				continue;
			}
			return ExpectTable[i].vc_conn;
		}
	}
	return null;
}
private function f_expect_table_init()
runs on SMPP_Emulation_CT {
	for (var integer i := 0; i < sizeof(ExpectTable); i := i+1) {
		ExpectTable[i] := {
			dst_ton := omit,
			dst_npi := omit,
			dst_addr := "",
			vc_conn := null
		};
	}
}





/* client/conn_hdlr side function to use procedure port to create expect in emulation */
function f_create_smpp_expect(charstring dest_number) runs on SMPP_ConnHdlr {
	SMPP_PROC.call(SMPPEM_register:{dest_number, self}) {
		[] SMPP_PROC.getreply(SMPPEM_register:{?,?}) {};
	}
}


}