blob: 5cf5342364771851f4764abcbd481937c27ede8f [file] [log] [blame]
Harald Welte365f4ed2017-11-23 00:00:43 +01001module BSSMAP_Emulation {
2
Harald Welteb3414b22017-11-23 18:22:10 +01003import from SCCP_Emulation all;
Harald Welte365f4ed2017-11-23 00:00:43 +01004import from SCCPasp_Types all;
5import from BSSAP_Types all;
6import from BSSMAP_Templates all;
7//import from MSC_ConnectionHandler all;
8
9/* General "base class" component definition, of which specific implementations
10 * derive themselves by means of the "extends" feature */
11type component BSSAP_ConnHdlr {
12 /* port towards MSC Emulator core / SCCP connection dispatchar */
13 port BSSAP_Conn_PT BSSAP;
14}
15
16/* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */
17type enumerated BSSAP_Conn_Prim {
18 /* SCCP tell us that connection was released */
19 MSC_CONN_PRIM_DISC_IND,
20 /* we tell SCCP to release connection */
21 MSC_CONN_PRIM_DISC_REQ
22}
23
Harald Welteb3414b22017-11-23 18:22:10 +010024type record BSSAP_Conn_Req {
25 SCCP_PAR_Address addr_peer,
26 SCCP_PAR_Address addr_own,
27 PDU_BSSAP bssap
28}
29
Harald Welte365f4ed2017-11-23 00:00:43 +010030/* port between individual per-connection components and this dispatcher */
31type port BSSAP_Conn_PT message {
Harald Welteb3414b22017-11-23 18:22:10 +010032 inout PDU_BSSAP, BSSAP_Conn_Prim, BSSAP_Conn_Req;
Harald Welte365f4ed2017-11-23 00:00:43 +010033} with { extension "internal" };
34
35
36/* represents a single BSSAP connection over SCCP */
37type record ConnectionData {
38 /* reference to the instance of the per-connection component */
39 BSSAP_ConnHdlr comp_ref,
40 integer sccp_conn_id
41}
42
43type component BSSMAP_Emulation_CT {
44 /* SCCP port on the bottom side, using ASP primitives */
45 port SCCPasp_PT SCCP;
46 /* BSSAP port to the per-connection clients */
47 port BSSAP_Conn_PT CLIENT;
48
49 /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */
50 var ConnectionData ConnectionTable[16];
51};
52
Harald Welteb3414b22017-11-23 18:22:10 +010053private function f_conn_id_known(integer sccp_conn_id)
54runs on BSSMAP_Emulation_CT return boolean {
55 var integer i;
56 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
57 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id){
58 return true;
59 }
60 }
61 return false;
62}
63
64private function f_comp_known(BSSAP_ConnHdlr client)
65runs on BSSMAP_Emulation_CT return boolean {
66 var integer i;
67 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
68 if (ConnectionTable[i].comp_ref == client) {
69 return true;
70 }
71 }
72 return false;
73}
Harald Welte365f4ed2017-11-23 00:00:43 +010074
75/* resolve component reference by connection ID */
76private function f_comp_by_conn_id(integer sccp_conn_id)
77runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr {
78 var integer i;
79 for (i := 0; i < sizeof(ConnectionTable); i := i+1) {
80 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
81 return ConnectionTable[i].comp_ref;
82 }
83 }
84 log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id);
85 self.stop;
86}
87
88/* resolve connection ID by component reference */
89private function f_conn_id_by_comp(BSSAP_ConnHdlr client)
90runs on BSSMAP_Emulation_CT return integer {
91 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
92 if (ConnectionTable[i].comp_ref == client) {
93 return ConnectionTable[i].sccp_conn_id;
94 }
95 }
96 log("BSSMAP Connection table not found by component ", client);
97 self.stop;
98}
99
Harald Welteb3414b22017-11-23 18:22:10 +0100100private function f_gen_conn_id()
101runs on BSSMAP_Emulation_CT return integer {
102 var integer conn_id;
103
104 do {
105 conn_id := float2int(rnd()*SCCP_Emulation.tsp_max_ConnectionId);
106 } while (f_conn_id_known(conn_id) == true);
107
108 return conn_id;
109}
110
Harald Welte365f4ed2017-11-23 00:00:43 +0100111private function f_conn_table_init()
112runs on BSSMAP_Emulation_CT {
113 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
114 ConnectionTable[i].comp_ref := null;
115 ConnectionTable[i].sccp_conn_id := -1;
116 }
117}
118
119private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id)
120runs on BSSMAP_Emulation_CT {
121 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
122 if (ConnectionTable[i].sccp_conn_id == -1) {
123 ConnectionTable[i].comp_ref := comp_ref;
124 ConnectionTable[i].sccp_conn_id := sccp_conn_id;
Harald Welteb3414b22017-11-23 18:22:10 +0100125 log("Added conn table entry ", i, comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100126 return;
127 }
128 }
129 log("BSSMAP Connection table full!");
130 self.stop;
131}
132
133private function f_conn_table_del(integer sccp_conn_id)
134runs on BSSMAP_Emulation_CT {
135 for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) {
136 if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) {
Harald Welteb3414b22017-11-23 18:22:10 +0100137 log("Deleted conn table entry ", i,
138 ConnectionTable[i].comp_ref, sccp_conn_id);
Harald Welte365f4ed2017-11-23 00:00:43 +0100139 ConnectionTable[i].sccp_conn_id := -1;
140 ConnectionTable[i].comp_ref := null;
141 }
142 }
143 log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id);
144 self.stop;
145}
146
147/* handle (optional) userData portion of various primitives and dispatch it to the client */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100148private function f_handle_userData(BSSAP_ConnHdlr client, octetstring userdata)
Harald Welte365f4ed2017-11-23 00:00:43 +0100149runs on BSSMAP_Emulation_CT {
Harald Welte365f4ed2017-11-23 00:00:43 +0100150 /* decode + send decoded BSSAP to client */
151 var PDU_BSSAP bssap := dec_PDU_BSSAP(valueof(userdata));
152 CLIENT.send(bssap) to client;
153}
154
155/* call-back type, to be provided by specific implementation; called when new SCCP connection
156 * arrives */
157type function BssmapCreateCallback(ASP_SCCP_N_CONNECT_ind conn_ind)
158runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
159
160type function BssmapUnitdataCallback(PDU_BSSAP bssap)
161runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
162
163type record BssmapOps {
164 BssmapCreateCallback create_cb,
165 BssmapUnitdataCallback unitdata_cb
166}
167
168function main(BssmapOps ops) runs on BSSMAP_Emulation_CT {
169
170 f_conn_table_init();
171
172 while (true) {
173 var ASP_SCCP_N_UNITDATA_ind ud_ind;
174 var ASP_SCCP_N_CONNECT_ind conn_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100175 var ASP_SCCP_N_CONNECT_cfm conn_cfm;
Harald Welte365f4ed2017-11-23 00:00:43 +0100176 var ASP_SCCP_N_DATA_ind data_ind;
177 var ASP_SCCP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100178 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100179 var BSSAP_ConnHdlr vc_conn;
180 var PDU_BSSAP bssap;
181
182 alt {
183 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
184 [] SCCP.receive(ASP_SCCP_N_UNITDATA_ind:?) -> value ud_ind {
185 /* Connectionless Procedures like RESET */
186 var template PDU_BSSAP resp;
187 bssap := dec_PDU_BSSAP(ud_ind.userData);
188 resp := ops.unitdata_cb.apply(bssap);
189 if (isvalue(resp)) {
190 var octetstring resp_ud := enc_PDU_BSSAP(valueof(resp));
191 SCCP.send(t_ASP_N_UNITDATA_req(ud_ind.callingAddress,
192 ud_ind.calledAddress, omit,
193 omit, resp_ud, omit));
194 }
195 }
196
197 /* SCCP -> Client: new connection from BSC */
198 [] SCCP.receive(ASP_SCCP_N_CONNECT_ind:?) -> value conn_ind {
199 vc_conn := ops.create_cb.apply(conn_ind);
200 /* store mapping between client components and SCCP connectionId */
201 f_conn_table_add(vc_conn, conn_ind.connectionId);
202 /* handle user payload */
203 f_handle_userData(vc_conn, conn_ind.userData);
204 /* confirm connection establishment */
205 SCCP.send(t_ASP_N_CONNECT_res(omit, omit, omit, omit, conn_ind.connectionId, omit));
206 }
207
208 /* SCCP -> Client: connection-oriented data in existing connection */
209 [] SCCP.receive(ASP_SCCP_N_DATA_ind:?) -> value data_ind {
210 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100211 if (ispresent(data_ind.userData)) {
212 f_handle_userData(vc_conn, data_ind.userData);
213 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100214 }
215
216 /* SCCP -> Client: disconnect of an existing connection */
217 [] SCCP.receive(ASP_SCCP_N_DISCONNECT_ind:?) -> value disc_ind {
218 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
Harald Welte5cc4aa22017-11-23 18:51:28 +0100219 if (ispresent(disc_ind.userData)) {
220 f_handle_userData(vc_conn, disc_ind.userData);
221 }
Harald Welte365f4ed2017-11-23 00:00:43 +0100222 /* notify client about termination */
223 var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND;
224 CLIENT.send(prim) to vc_conn;
225 f_conn_table_del(disc_ind.connectionId);
226 /* TOOD: return confirm to other side? */
227 }
228
Harald Welteb3414b22017-11-23 18:22:10 +0100229 /* SCCP -> Client: connection confirm for outbound connection */
230 [] SCCP.receive(ASP_SCCP_N_CONNECT_cfm:?) -> value conn_cfm {
231 /* handle user payload */
Harald Welte5cc4aa22017-11-23 18:51:28 +0100232 if (ispresent(conn_cfm.userData)) {
233 f_handle_userData(vc_conn, conn_cfm.userData);
234 }
Harald Welteb3414b22017-11-23 18:22:10 +0100235 }
236
Harald Welte365f4ed2017-11-23 00:00:43 +0100237 /* Disconnect request client -> SCCP */
238 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
239 var integer conn_id := f_conn_id_by_comp(vc_conn);
240 SCCP.send(t_ASP_N_DISCONNECT_req(omit, 0, omit, conn_id, omit));
241 f_conn_table_del(conn_id);
242 }
243
244 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100245 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
246 var integer conn_id;
247 /* encode + send to dispatcher */
248 var octetstring userdata := enc_PDU_BSSAP(creq.bssap);
249
250 if (f_comp_known(vc_conn) == false) {
251 /* unknown client, create new connection */
252 conn_id := f_gen_conn_id();
253
254 /* store mapping between client components and SCCP connectionId */
255 f_conn_table_add(vc_conn, conn_id);
256
257 SCCP.send(t_ASP_N_CONNECT_req(creq.addr_peer, creq.addr_own, omit, omit,
258 userdata, conn_id, omit));
259 } else {
260 /* known client, send via existing connection */
261 conn_id := f_conn_id_by_comp(vc_conn);
262 SCCP.send(t_ASP_N_DATA_req(userdata, conn_id, omit));
263 }
264
265 }
266
Harald Welte365f4ed2017-11-23 00:00:43 +0100267 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
268 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welteb3414b22017-11-23 18:22:10 +0100269 /* encode + send it to dispatcher */
Harald Welte365f4ed2017-11-23 00:00:43 +0100270 var octetstring userdata := enc_PDU_BSSAP(bssap);
271 SCCP.send(t_ASP_N_DATA_req(userdata, conn_id, omit));
272 }
273
274 }
275 }
276}
277
278
279}