Harald Welte | 365f4ed | 2017-11-23 00:00:43 +0100 | [diff] [blame^] | 1 | module BSSMAP_Emulation { |
| 2 | |
| 3 | import from SCCPasp_Types all; |
| 4 | import from BSSAP_Types all; |
| 5 | import from BSSMAP_Templates all; |
| 6 | //import from MSC_ConnectionHandler all; |
| 7 | |
| 8 | /* General "base class" component definition, of which specific implementations |
| 9 | * derive themselves by means of the "extends" feature */ |
| 10 | type component BSSAP_ConnHdlr { |
| 11 | /* port towards MSC Emulator core / SCCP connection dispatchar */ |
| 12 | port BSSAP_Conn_PT BSSAP; |
| 13 | } |
| 14 | |
| 15 | /* Auxiliary primitive that can happen on the port between per-connection client and this dispatcher */ |
| 16 | type enumerated BSSAP_Conn_Prim { |
| 17 | /* SCCP tell us that connection was released */ |
| 18 | MSC_CONN_PRIM_DISC_IND, |
| 19 | /* we tell SCCP to release connection */ |
| 20 | MSC_CONN_PRIM_DISC_REQ |
| 21 | } |
| 22 | |
| 23 | /* port between individual per-connection components and this dispatcher */ |
| 24 | type port BSSAP_Conn_PT message { |
| 25 | inout PDU_BSSAP; |
| 26 | inout BSSAP_Conn_Prim; |
| 27 | } with { extension "internal" }; |
| 28 | |
| 29 | |
| 30 | /* represents a single BSSAP connection over SCCP */ |
| 31 | type record ConnectionData { |
| 32 | /* reference to the instance of the per-connection component */ |
| 33 | BSSAP_ConnHdlr comp_ref, |
| 34 | integer sccp_conn_id |
| 35 | } |
| 36 | |
| 37 | type component BSSMAP_Emulation_CT { |
| 38 | /* SCCP port on the bottom side, using ASP primitives */ |
| 39 | port SCCPasp_PT SCCP; |
| 40 | /* BSSAP port to the per-connection clients */ |
| 41 | port BSSAP_Conn_PT CLIENT; |
| 42 | |
| 43 | /* use 16 as this is also the number of SCCP connections that SCCP_Emulation can handle */ |
| 44 | var ConnectionData ConnectionTable[16]; |
| 45 | }; |
| 46 | |
| 47 | |
| 48 | /* resolve component reference by connection ID */ |
| 49 | private function f_comp_by_conn_id(integer sccp_conn_id) |
| 50 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr { |
| 51 | var integer i; |
| 52 | for (i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 53 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) { |
| 54 | return ConnectionTable[i].comp_ref; |
| 55 | } |
| 56 | } |
| 57 | log("BSSMAP Connection table not found by SCCP Connection ID ", sccp_conn_id); |
| 58 | self.stop; |
| 59 | } |
| 60 | |
| 61 | /* resolve connection ID by component reference */ |
| 62 | private function f_conn_id_by_comp(BSSAP_ConnHdlr client) |
| 63 | runs on BSSMAP_Emulation_CT return integer { |
| 64 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 65 | if (ConnectionTable[i].comp_ref == client) { |
| 66 | return ConnectionTable[i].sccp_conn_id; |
| 67 | } |
| 68 | } |
| 69 | log("BSSMAP Connection table not found by component ", client); |
| 70 | self.stop; |
| 71 | } |
| 72 | |
| 73 | private function f_conn_table_init() |
| 74 | runs on BSSMAP_Emulation_CT { |
| 75 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 76 | ConnectionTable[i].comp_ref := null; |
| 77 | ConnectionTable[i].sccp_conn_id := -1; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private function f_conn_table_add(BSSAP_ConnHdlr comp_ref, integer sccp_conn_id) |
| 82 | runs on BSSMAP_Emulation_CT { |
| 83 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 84 | if (ConnectionTable[i].sccp_conn_id == -1) { |
| 85 | ConnectionTable[i].comp_ref := comp_ref; |
| 86 | ConnectionTable[i].sccp_conn_id := sccp_conn_id; |
| 87 | return; |
| 88 | } |
| 89 | } |
| 90 | log("BSSMAP Connection table full!"); |
| 91 | self.stop; |
| 92 | } |
| 93 | |
| 94 | private function f_conn_table_del(integer sccp_conn_id) |
| 95 | runs on BSSMAP_Emulation_CT { |
| 96 | for (var integer i := 0; i < sizeof(ConnectionTable); i := i+1) { |
| 97 | if (ConnectionTable[i].sccp_conn_id == sccp_conn_id) { |
| 98 | ConnectionTable[i].sccp_conn_id := -1; |
| 99 | ConnectionTable[i].comp_ref := null; |
| 100 | } |
| 101 | } |
| 102 | log("BSSMAP Connection table attempt to delete non-existant ", sccp_conn_id); |
| 103 | self.stop; |
| 104 | } |
| 105 | |
| 106 | /* handle (optional) userData portion of various primitives and dispatch it to the client */ |
| 107 | private function f_handle_userData(BSSAP_ConnHdlr client, template octetstring userdata) |
| 108 | runs on BSSMAP_Emulation_CT { |
| 109 | if (not isvalue(userdata)) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | /* decode + send decoded BSSAP to client */ |
| 114 | var PDU_BSSAP bssap := dec_PDU_BSSAP(valueof(userdata)); |
| 115 | CLIENT.send(bssap) to client; |
| 116 | } |
| 117 | |
| 118 | /* call-back type, to be provided by specific implementation; called when new SCCP connection |
| 119 | * arrives */ |
| 120 | type function BssmapCreateCallback(ASP_SCCP_N_CONNECT_ind conn_ind) |
| 121 | runs on BSSMAP_Emulation_CT return BSSAP_ConnHdlr; |
| 122 | |
| 123 | type function BssmapUnitdataCallback(PDU_BSSAP bssap) |
| 124 | runs on BSSMAP_Emulation_CT return template PDU_BSSAP; |
| 125 | |
| 126 | type record BssmapOps { |
| 127 | BssmapCreateCallback create_cb, |
| 128 | BssmapUnitdataCallback unitdata_cb |
| 129 | } |
| 130 | |
| 131 | function main(BssmapOps ops) runs on BSSMAP_Emulation_CT { |
| 132 | |
| 133 | f_conn_table_init(); |
| 134 | |
| 135 | while (true) { |
| 136 | var ASP_SCCP_N_UNITDATA_ind ud_ind; |
| 137 | var ASP_SCCP_N_CONNECT_ind conn_ind; |
| 138 | var ASP_SCCP_N_DATA_ind data_ind; |
| 139 | var ASP_SCCP_N_DISCONNECT_ind disc_ind; |
| 140 | var BSSAP_ConnHdlr vc_conn; |
| 141 | var PDU_BSSAP bssap; |
| 142 | |
| 143 | alt { |
| 144 | /* SCCP -> Client: UNIT-DATA (connectionless SCCP) from a BSC */ |
| 145 | [] SCCP.receive(ASP_SCCP_N_UNITDATA_ind:?) -> value ud_ind { |
| 146 | /* Connectionless Procedures like RESET */ |
| 147 | var template PDU_BSSAP resp; |
| 148 | bssap := dec_PDU_BSSAP(ud_ind.userData); |
| 149 | resp := ops.unitdata_cb.apply(bssap); |
| 150 | if (isvalue(resp)) { |
| 151 | var octetstring resp_ud := enc_PDU_BSSAP(valueof(resp)); |
| 152 | SCCP.send(t_ASP_N_UNITDATA_req(ud_ind.callingAddress, |
| 153 | ud_ind.calledAddress, omit, |
| 154 | omit, resp_ud, omit)); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* SCCP -> Client: new connection from BSC */ |
| 159 | [] SCCP.receive(ASP_SCCP_N_CONNECT_ind:?) -> value conn_ind { |
| 160 | vc_conn := ops.create_cb.apply(conn_ind); |
| 161 | /* store mapping between client components and SCCP connectionId */ |
| 162 | f_conn_table_add(vc_conn, conn_ind.connectionId); |
| 163 | /* handle user payload */ |
| 164 | f_handle_userData(vc_conn, conn_ind.userData); |
| 165 | /* confirm connection establishment */ |
| 166 | SCCP.send(t_ASP_N_CONNECT_res(omit, omit, omit, omit, conn_ind.connectionId, omit)); |
| 167 | } |
| 168 | |
| 169 | /* SCCP -> Client: connection-oriented data in existing connection */ |
| 170 | [] SCCP.receive(ASP_SCCP_N_DATA_ind:?) -> value data_ind { |
| 171 | vc_conn := f_comp_by_conn_id(data_ind.connectionId); |
| 172 | f_handle_userData(vc_conn, conn_ind.userData); |
| 173 | } |
| 174 | |
| 175 | /* SCCP -> Client: disconnect of an existing connection */ |
| 176 | [] SCCP.receive(ASP_SCCP_N_DISCONNECT_ind:?) -> value disc_ind { |
| 177 | vc_conn := f_comp_by_conn_id(disc_ind.connectionId); |
| 178 | f_handle_userData(vc_conn, disc_ind.userData); |
| 179 | /* notify client about termination */ |
| 180 | var BSSAP_Conn_Prim prim := MSC_CONN_PRIM_DISC_IND; |
| 181 | CLIENT.send(prim) to vc_conn; |
| 182 | f_conn_table_del(disc_ind.connectionId); |
| 183 | /* TOOD: return confirm to other side? */ |
| 184 | } |
| 185 | |
| 186 | /* Disconnect request client -> SCCP */ |
| 187 | [] CLIENT.receive(BSSAP_Conn_Prim:MSC_CONN_PRIM_DISC_REQ) -> sender vc_conn { |
| 188 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
| 189 | SCCP.send(t_ASP_N_DISCONNECT_req(omit, 0, omit, conn_id, omit)); |
| 190 | f_conn_table_del(conn_id); |
| 191 | } |
| 192 | |
| 193 | /* BSSAP from client -> SCCP */ |
| 194 | [] CLIENT.receive(PDU_BSSAP:?) -> value bssap sender vc_conn { |
| 195 | var integer conn_id := f_conn_id_by_comp(vc_conn); |
| 196 | /* encode + send to dispatcher */ |
| 197 | var octetstring userdata := enc_PDU_BSSAP(bssap); |
| 198 | SCCP.send(t_ASP_N_DATA_req(userdata, conn_id, omit)); |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | |
| 206 | } |