blob: 9e4d5c3e8d230e47503269678672f3cc1c3be49b [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 */
148private function f_handle_userData(BSSAP_ConnHdlr client, template octetstring userdata)
149runs on BSSMAP_Emulation_CT {
150 if (not isvalue(userdata)) {
151 return;
152 }
153
154 /* decode + send decoded BSSAP to client */
155 var PDU_BSSAP bssap := dec_PDU_BSSAP(valueof(userdata));
156 CLIENT.send(bssap) to client;
157}
158
159/* call-back type, to be provided by specific implementation; called when new SCCP connection
160 * arrives */
161type function BssmapCreateCallback(ASP_SCCP_N_CONNECT_ind conn_ind)
162runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr;
163
164type function BssmapUnitdataCallback(PDU_BSSAP bssap)
165runs on BSSMAP_Emulation_CT return template PDU_BSSAP;
166
167type record BssmapOps {
168 BssmapCreateCallback create_cb,
169 BssmapUnitdataCallback unitdata_cb
170}
171
172function main(BssmapOps ops) runs on BSSMAP_Emulation_CT {
173
174 f_conn_table_init();
175
176 while (true) {
177 var ASP_SCCP_N_UNITDATA_ind ud_ind;
178 var ASP_SCCP_N_CONNECT_ind conn_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100179 var ASP_SCCP_N_CONNECT_cfm conn_cfm;
Harald Welte365f4ed2017-11-23 00:00:43 +0100180 var ASP_SCCP_N_DATA_ind data_ind;
181 var ASP_SCCP_N_DISCONNECT_ind disc_ind;
Harald Welteb3414b22017-11-23 18:22:10 +0100182 var BSSAP_Conn_Req creq;
Harald Welte365f4ed2017-11-23 00:00:43 +0100183 var BSSAP_ConnHdlr vc_conn;
184 var PDU_BSSAP bssap;
185
186 alt {
187 /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */
188 [] SCCP.receive(ASP_SCCP_N_UNITDATA_ind:?) -> value ud_ind {
189 /* Connectionless Procedures like RESET */
190 var template PDU_BSSAP resp;
191 bssap := dec_PDU_BSSAP(ud_ind.userData);
192 resp := ops.unitdata_cb.apply(bssap);
193 if (isvalue(resp)) {
194 var octetstring resp_ud := enc_PDU_BSSAP(valueof(resp));
195 SCCP.send(t_ASP_N_UNITDATA_req(ud_ind.callingAddress,
196 ud_ind.calledAddress, omit,
197 omit, resp_ud, omit));
198 }
199 }
200
201 /* SCCP -> Client: new connection from BSC */
202 [] SCCP.receive(ASP_SCCP_N_CONNECT_ind:?) -> value conn_ind {
203 vc_conn := ops.create_cb.apply(conn_ind);
204 /* store mapping between client components and SCCP connectionId */
205 f_conn_table_add(vc_conn, conn_ind.connectionId);
206 /* handle user payload */
207 f_handle_userData(vc_conn, conn_ind.userData);
208 /* confirm connection establishment */
209 SCCP.send(t_ASP_N_CONNECT_res(omit, omit, omit, omit, conn_ind.connectionId, omit));
210 }
211
212 /* SCCP -> Client: connection-oriented data in existing connection */
213 [] SCCP.receive(ASP_SCCP_N_DATA_ind:?) -> value data_ind {
214 vc_conn := f_comp_by_conn_id(data_ind.connectionId);
215 f_handle_userData(vc_conn, conn_ind.userData);
216 }
217
218 /* SCCP -> Client: disconnect of an existing connection */
219 [] SCCP.receive(ASP_SCCP_N_DISCONNECT_ind:?) -> value disc_ind {
220 vc_conn := f_comp_by_conn_id(disc_ind.connectionId);
221 f_handle_userData(vc_conn, disc_ind.userData);
222 /* 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 */
232 f_handle_userData(vc_conn, conn_cfm.userData);
233 }
234
Harald Welte365f4ed2017-11-23 00:00:43 +0100235 /* Disconnect request client -> SCCP */
236 [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn {
237 var integer conn_id := f_conn_id_by_comp(vc_conn);
238 SCCP.send(t_ASP_N_DISCONNECT_req(omit, 0, omit, conn_id, omit));
239 f_conn_table_del(conn_id);
240 }
241
242 /* BSSAP from client -> SCCP */
Harald Welteb3414b22017-11-23 18:22:10 +0100243 [] CLIENT.receive(BSSAP_Conn_Req:?) -> value creq sender vc_conn {
244 var integer conn_id;
245 /* encode + send to dispatcher */
246 var octetstring userdata := enc_PDU_BSSAP(creq.bssap);
247
248 if (f_comp_known(vc_conn) == false) {
249 /* unknown client, create new connection */
250 conn_id := f_gen_conn_id();
251
252 /* store mapping between client components and SCCP connectionId */
253 f_conn_table_add(vc_conn, conn_id);
254
255 SCCP.send(t_ASP_N_CONNECT_req(creq.addr_peer, creq.addr_own, omit, omit,
256 userdata, conn_id, omit));
257 } else {
258 /* known client, send via existing connection */
259 conn_id := f_conn_id_by_comp(vc_conn);
260 SCCP.send(t_ASP_N_DATA_req(userdata, conn_id, omit));
261 }
262
263 }
264
Harald Welte365f4ed2017-11-23 00:00:43 +0100265 [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn {
266 var integer conn_id := f_conn_id_by_comp(vc_conn);
Harald Welteb3414b22017-11-23 18:22:10 +0100267 /* encode + send it to dispatcher */
Harald Welte365f4ed2017-11-23 00:00:43 +0100268 var octetstring userdata := enc_PDU_BSSAP(bssap);
269 SCCP.send(t_ASP_N_DATA_req(userdata, conn_id, omit));
270 }
271
272 }
273 }
274}
275
276
277}