Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 1 | module MGCP_Emulation { |
| 2 | |
Harald Welte | a02515f | 2018-01-26 10:33:23 +0100 | [diff] [blame] | 3 | /* MGCP Emulation, runs on top of MGCP_CodecPort. It multiplexes/demultiplexes |
| 4 | * the individual connections, so there can be separate TTCN-3 components handling |
| 5 | * each of the connections. |
| 6 | * |
| 7 | * The MGCP_Emulation.main() function processes MGCP primitives from the MGCP |
| 8 | * socket via the MGCP_CodecPort, and dispatches them to the per-connection components. |
| 9 | * |
| 10 | * For each new inbound connection, the MgcpOps.create_cb() is called. It can create |
| 11 | * or resolve a TTCN-3 component, and returns a component reference to which that inbound |
| 12 | * connection is routed/dispatched. |
| 13 | * |
| 14 | * If a pre-existing component wants to register to handle a future inbound call, it can |
| 15 | * do so by registering an "expect" with the expected destination phone number. This is e.g. useful |
| 16 | * if you are simulating BSC + MGCP, and first trigger a connection from BSC side in a |
| 17 | * component which then subsequently should also handle the MGCP emulation. |
| 18 | * |
| 19 | * Inbound Unit Data messages (such as are dispatched to the MgcpOps.unitdata_cb() callback, |
| 20 | * which is registered with an argument to the main() function below. |
| 21 | * |
| 22 | * (C) 2017-2018 by Harald Welte <laforge@gnumonks.org> |
| 23 | * (C) 2018 by sysmocom - s.f.m.c. GmbH, Author: Daniel Willmann |
| 24 | * All rights reserved. |
| 25 | * |
| 26 | * Released under the terms of GNU General Public License, Version 2 or |
| 27 | * (at your option) any later version. |
| 28 | */ |
| 29 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 30 | import from MGCP_CodecPort all; |
| 31 | import from MGCP_CodecPort_CtrlFunct all; |
| 32 | import from MGCP_Types all; |
| 33 | import from MGCP_Templates all; |
| 34 | import from Osmocom_Types all; |
| 35 | import from IPL4asp_Types all; |
| 36 | |
| 37 | type component MGCP_ConnHdlr { |
| 38 | port MGCP_Conn_PT MGCP; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 39 | /* procedure based port to register for incoming connections */ |
| 40 | port MGCPEM_PROC_PT MGCP_PROC; |
Daniel Willmann | aeea76f | 2018-01-17 15:23:45 +0100 | [diff] [blame] | 41 | var MgcpConnectionId mgcp_conn_id; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /* port between individual per-connection components and this dispatcher */ |
| 45 | type port MGCP_Conn_PT message { |
| 46 | inout MgcpCommand, MgcpResponse; |
| 47 | } with { extension "internal" }; |
| 48 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 49 | /* represents a single MGCP Endpoint */ |
| 50 | type record EndpointData { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 51 | MGCP_ConnHdlr comp_ref, |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 52 | MgcpEndpoint endpoint optional |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 53 | }; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 54 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 55 | /* pending CRCX with their transaction ID */ |
| 56 | type set of MgcpTransId MgcpTransIds; |
| 57 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 58 | type component MGCP_Emulation_CT { |
| 59 | /* Port facing to the UDP SUT */ |
| 60 | port MGCP_CODEC_PT MGCP; |
| 61 | /* All MGCP_ConnHdlr MGCP ports connect here |
| 62 | * MGCP_Emulation_CT.main needs to figure out what messages |
| 63 | * to send where with CLIENT.send() to vc_conn */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 64 | port MGCP_Conn_PT MGCP_CLIENT; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 65 | /* currently tracked connections */ |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 66 | var EndpointData MgcpEndpointTable[16]; |
| 67 | var MgcpTransIds MgcpPendingTrans := {}; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 68 | /* pending expected CRCX */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 69 | var ExpectData MgcpExpectTable[8]; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 70 | /* procedure based port to register for incoming connections */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 71 | port MGCPEM_PROC_PT MGCP_PROC; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 72 | |
| 73 | var charstring g_mgcp_id; |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 74 | var integer g_mgcp_conn_id := -1; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | type function MGCPCreateCallback(MgcpCommand cmd, charstring id) |
| 78 | runs on MGCP_Emulation_CT return MGCP_ConnHdlr; |
| 79 | |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 80 | type function MGCPUnitdataCallback(MgcpMessage msg) |
| 81 | runs on MGCP_Emulation_CT return template MgcpMessage; |
| 82 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 83 | type record MGCPOps { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 84 | MGCPCreateCallback create_cb, |
| 85 | MGCPUnitdataCallback unitdata_cb |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | type record MGCP_conn_parameters { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 89 | HostName callagent_ip, |
| 90 | PortNumber callagent_udp_port, |
| 91 | HostName mgw_ip, |
| 92 | PortNumber mgw_udp_port |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 95 | function tr_MGCP_RecvFrom_R(template MgcpMessage msg) |
| 96 | runs on MGCP_Emulation_CT return template MGCP_RecvFrom { |
| 97 | var template MGCP_RecvFrom mrf := { |
| 98 | connId := g_mgcp_conn_id, |
| 99 | remName := ?, |
| 100 | remPort := ?, |
| 101 | locName := ?, |
| 102 | locPort := ?, |
| 103 | msg := msg |
| 104 | } |
| 105 | return mrf; |
| 106 | } |
| 107 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 108 | private function f_ep_known(MgcpEndpoint ep) |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 109 | runs on MGCP_Emulation_CT return boolean { |
| 110 | var integer i; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 111 | for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 112 | if (MgcpEndpointTable[i].endpoint == ep) { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | private function f_comp_known(MGCP_ConnHdlr client) |
| 120 | runs on MGCP_Emulation_CT return boolean { |
| 121 | var integer i; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 122 | for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 123 | if (MgcpEndpointTable[i].comp_ref == client) { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 124 | return true; |
| 125 | } |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 130 | private function f_comp_by_ep(MgcpEndpoint ep) |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 131 | runs on MGCP_Emulation_CT return MGCP_ConnHdlr { |
| 132 | var integer i; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 133 | for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 134 | if (MgcpEndpointTable[i].endpoint == ep) { |
| 135 | return MgcpEndpointTable[i].comp_ref; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 136 | } |
| 137 | } |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 138 | log("MGCP Endpoint Table not found by Endpoint", ep); |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 139 | setverdict(fail); |
| 140 | self.stop; |
| 141 | } |
| 142 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 143 | private function f_ep_by_comp(MGCP_ConnHdlr client) |
| 144 | runs on MGCP_Emulation_CT return MgcpEndpoint { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 145 | var integer i; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 146 | for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 147 | if (MgcpEndpointTable[i].comp_ref == client) { |
| 148 | return MgcpEndpointTable[i].endpoint; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 151 | log("MGCP Endpoint Table not found by component ", client); |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 152 | setverdict(fail); |
| 153 | self.stop; |
| 154 | } |
| 155 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 156 | private function f_ep_table_add(MGCP_ConnHdlr comp_ref, MgcpEndpoint ep) |
| 157 | runs on MGCP_Emulation_CT { |
| 158 | var integer i; |
| 159 | for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 160 | if (not isvalue(MgcpEndpointTable[i].endpoint)) { |
| 161 | MgcpEndpointTable[i].endpoint := ep; |
| 162 | MgcpEndpointTable[i].comp_ref := comp_ref; |
| 163 | return; |
| 164 | } |
| 165 | } |
| 166 | setverdict(fail, "MGCP Endpoint Table full!"); |
| 167 | self.stop; |
| 168 | } |
| 169 | |
| 170 | private function f_ep_table_del(MGCP_ConnHdlr comp_ref, MgcpEndpoint ep) |
| 171 | runs on MGCP_Emulation_CT { |
| 172 | var integer i; |
| 173 | for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 174 | if (MgcpEndpointTable[i].comp_ref == comp_ref and |
| 175 | MgcpEndpointTable[i].endpoint == ep) { |
| 176 | MgcpEndpointTable[i].endpoint := omit; |
| 177 | MgcpEndpointTable[i].comp_ref := null; |
| 178 | return; |
| 179 | } |
| 180 | } |
| 181 | setverdict(fail, "MGCP Endpoint Table: Couldn't find to-be-deleted entry!"); |
| 182 | self.stop; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /* Check if the given transaction ID is a pending CRCX. If yes, return true + remove */ |
| 187 | private function f_trans_id_was_pending(MgcpTransId trans_id) |
| 188 | runs on MGCP_Emulation_CT return boolean { |
| 189 | for (var integer i := 0; i < lengthof(MgcpPendingTrans); i := i+1) { |
| 190 | if (MgcpPendingTrans[i] == trans_id) { |
| 191 | /* Remove from list */ |
| 192 | var MgcpTransIds OldPendingTrans := MgcpPendingTrans; |
| 193 | MgcpPendingTrans := {} |
| 194 | for (var integer j := 0; j < lengthof(OldPendingTrans); j := j+1) { |
| 195 | if (j != i) { |
| 196 | MgcpPendingTrans := MgcpPendingTrans & {OldPendingTrans[j]}; |
| 197 | } |
| 198 | } |
| 199 | return true; |
| 200 | } |
| 201 | } |
| 202 | return false; |
| 203 | } |
| 204 | |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 205 | /* TODO: move this to MGCP_Types? */ |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 206 | function f_mgcp_ep(MgcpMessage msg) return MgcpEndpoint { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 207 | var MgcpParameterList params; |
| 208 | var integer i; |
| 209 | if (ischosen(msg.command)) { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 210 | return msg.command.line.ep; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 211 | } else { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 212 | /* FIXME */ |
| 213 | return "null@none"; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 214 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 217 | private function f_ep_table_init() |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 218 | runs on MGCP_Emulation_CT { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 219 | for (var integer i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 220 | MgcpEndpointTable[i].comp_ref := null; |
| 221 | MgcpEndpointTable[i].endpoint := omit; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 225 | function main(MGCPOps ops, MGCP_conn_parameters p, charstring id) runs on MGCP_Emulation_CT { |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 226 | var Result res; |
| 227 | g_mgcp_id := id; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 228 | f_ep_table_init(); |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 229 | f_expect_table_init(); |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 230 | |
| 231 | map(self:MGCP, system:MGCP_CODEC_PT); |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 232 | if (p.callagent_udp_port == -1) { |
| 233 | res := MGCP_CodecPort_CtrlFunct.f_IPL4_listen(MGCP, p.mgw_ip, p.mgw_udp_port, { udp:={} }); |
| 234 | } else { |
| 235 | res := MGCP_CodecPort_CtrlFunct.f_IPL4_connect(MGCP, p.callagent_ip, p.callagent_udp_port, p.mgw_ip, p.mgw_udp_port, -1, { udp:={} }); |
| 236 | } |
| 237 | |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 238 | g_mgcp_conn_id := res.connId; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 239 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 240 | while (true) { |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 241 | var MGCP_ConnHdlr vc_conn; |
| 242 | var ExpectCriteria crit; |
| 243 | var MGCP_RecvFrom mrf; |
| 244 | var MgcpMessage msg; |
| 245 | var MgcpCommand cmd; |
| 246 | var MgcpResponse resp; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 247 | var MgcpEndpoint ep; |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 248 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 249 | alt { |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 250 | /* MGCP from client */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 251 | [] MGCP_CLIENT.receive(MgcpResponse:?) -> value resp sender vc_conn { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 252 | msg := { |
| 253 | response := resp |
| 254 | }; |
| 255 | /* If this is the resposne to a pending CRCX, extract Endpoint and store in table */ |
| 256 | if (f_trans_id_was_pending(resp.line.trans_id)) { |
| 257 | f_ep_table_add(vc_conn, f_mgcp_ep(msg)); |
| 258 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 259 | /* Pass message through */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 260 | /* TODO: check which ConnectionID client has allocated + store in table? */ |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 261 | MGCP.send(t_MGCP_Send(g_mgcp_conn_id, msg)); |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 262 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 263 | [] MGCP.receive(tr_MGCP_RecvFrom_R(?)) -> value mrf { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 264 | if (p.callagent_udp_port == -1) { |
| 265 | /* we aren't yet connected to the remote side port, let's fix this */ |
| 266 | p.callagent_udp_port := mrf.remPort; |
| 267 | MGCP_CodecPort_CtrlFunct.f_IPL4_connect(MGCP, p.callagent_ip, p.callagent_udp_port, p.mgw_ip, p.mgw_udp_port, g_mgcp_conn_id, { udp:={} }); |
| 268 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 269 | if (ischosen(mrf.msg.command)) { |
| 270 | cmd := mrf.msg.command; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 271 | if (f_ep_known(cmd.line.ep)) { |
| 272 | vc_conn := f_comp_by_ep(cmd.line.ep); |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 273 | MGCP_CLIENT.send(cmd) to vc_conn; |
| 274 | } else { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 275 | if (cmd.line.verb == "CRCX") { |
| 276 | vc_conn := ops.create_cb.apply(cmd, id); |
| 277 | if (true /* non-wildcard EP */) { |
| 278 | f_ep_table_add(vc_conn, cmd.line.ep); |
| 279 | } else { |
| 280 | /* add this transaction to list of pending transactions */ |
| 281 | MgcpPendingTrans := MgcpPendingTrans & {cmd.line.trans_id}; |
| 282 | } |
| 283 | MGCP_CLIENT.send(cmd) to vc_conn; |
| 284 | } else { |
| 285 | /* connectionless MGCP, i.e. messages without ConnectionId */ |
| 286 | var template MgcpMessage r := ops.unitdata_cb.apply(mrf.msg); |
| 287 | if (isvalue(r)) { |
| 288 | MGCP.send(t_MGCP_Send(g_mgcp_conn_id, r)); |
| 289 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 290 | } |
| 291 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 292 | } else { |
| 293 | setverdict(fail, "Received unexpected MGCP response: ", mrf.msg.response); |
| 294 | self.stop; |
| 295 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 296 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 297 | [] MGCP_PROC.getcall(MGCPEM_register:{?,?}) -> param(crit, vc_conn) { |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 298 | f_create_expect(crit, vc_conn); |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 299 | MGCP_PROC.reply(MGCPEM_register:{crit, vc_conn}); |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 300 | } |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 301 | [] MGCP_PROC.getcall(MGCPEM_delete_ep:{?,?}) -> param(ep, vc_conn) { |
| 302 | f_ep_table_del(vc_conn, ep); |
| 303 | MGCP_PROC.reply(MGCPEM_delete_ep:{ep, vc_conn}); |
| 304 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 305 | } |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 306 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | /* "Expect" Handling */ |
| 311 | |
| 312 | /* */ |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 313 | type record ExpectCriteria { |
| 314 | MgcpConnectionId connid optional, |
| 315 | MgcpEndpoint endpoint optional, |
| 316 | MgcpTransId transid optional |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | type record ExpectData { |
| 320 | ExpectCriteria crit optional, |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 321 | MGCP_ConnHdlr vc_conn |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 324 | signature MGCPEM_register(in ExpectCriteria cmd, in MGCP_ConnHdlr hdlr); |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 325 | signature MGCPEM_delete_ep(in MgcpEndpoint ep, in MGCP_ConnHdlr hdlr); |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 326 | |
| 327 | type port MGCPEM_PROC_PT procedure { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 328 | inout MGCPEM_register, MGCPEM_delete_ep; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 329 | } with { extension "internal" }; |
| 330 | |
| 331 | function f_get_mgcp_by_crit(ExpectCriteria crit) |
| 332 | return template MgcpCommand { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 333 | var template MgcpCommand ret := { |
| 334 | line := { |
| 335 | verb := ?, |
| 336 | trans_id := ?, |
| 337 | ep := ?, |
| 338 | ver := ? |
| 339 | }, |
| 340 | params := *, |
| 341 | sdp := * |
| 342 | } |
| 343 | if (ispresent(crit.connid)) { |
| 344 | ret.params := { *, ts_MgcpParConnectionId(crit.connid), * }; |
| 345 | } |
| 346 | if (ispresent(crit.endpoint)) { |
| 347 | ret.line.ep := crit.endpoint; |
| 348 | } |
| 349 | if (ispresent(crit.transid)) { |
| 350 | ret.line.trans_id := crit.transid; |
| 351 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 352 | |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | /* Function that can be used as create_cb and will usse the expect table */ |
| 357 | function ExpectedCreateCallback(MgcpCommand cmd, charstring id) |
| 358 | runs on MGCP_Emulation_CT return MGCP_ConnHdlr { |
| 359 | var MGCP_ConnHdlr ret := null; |
| 360 | var template MgcpCommand mgcpcmd; |
| 361 | var integer i; |
| 362 | |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 363 | for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) { |
| 364 | if (not ispresent(MgcpExpectTable[i].crit)) { |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 365 | continue; |
| 366 | } |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 367 | /* FIXME: Ignore criteria for now */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 368 | mgcpcmd := f_get_mgcp_by_crit(MgcpExpectTable[i].crit); |
| 369 | if (match(cmd, mgcpcmd)) { |
| 370 | ret := MgcpExpectTable[i].vc_conn; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 371 | /* Release this entry */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 372 | MgcpExpectTable[i].crit := omit; |
| 373 | MgcpExpectTable[i].vc_conn := null; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 374 | log("Found Expect[", i, "] for ", cmd, " handled at ", ret); |
| 375 | return ret; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 376 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 377 | } |
| 378 | setverdict(fail, "Couldn't find Expect for CRCX", cmd); |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | private function f_create_expect(ExpectCriteria crit, MGCP_ConnHdlr hdlr) |
| 383 | runs on MGCP_Emulation_CT { |
| 384 | var integer i; |
| 385 | |
| 386 | /* Check an entry like this is not already presnt */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 387 | for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) { |
| 388 | if (crit == MgcpExpectTable[i].crit) { |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 389 | setverdict(fail, "Crit already present", crit); |
| 390 | self.stop; |
| 391 | } |
| 392 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 393 | for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) { |
| 394 | if (not ispresent(MgcpExpectTable[i].crit)) { |
| 395 | MgcpExpectTable[i].crit := crit; |
| 396 | MgcpExpectTable[i].vc_conn := hdlr; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 397 | log("Created Expect[", i, "] for ", crit, " to be handled at ", hdlr); |
| 398 | return; |
| 399 | } |
| 400 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 401 | setverdict(fail, "No space left in MgcpExpectTable") |
| 402 | } |
| 403 | |
| 404 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 405 | function f_create_mgcp_expect(ExpectCriteria dest_number) runs on MGCP_ConnHdlr { |
| 406 | MGCP_PROC.call(MGCPEM_register:{dest_number, self}) { |
| 407 | [] MGCP_PROC.getreply(MGCPEM_register:{?,?}) {}; |
| 408 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 411 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 412 | function f_create_mgcp_delete_ep(MgcpEndpoint ep) runs on MGCP_ConnHdlr { |
| 413 | MGCP_PROC.call(MGCPEM_delete_ep:{ep, self}) { |
| 414 | [] MGCP_PROC.getreply(MGCPEM_delete_ep:{?,?}) {}; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 419 | private function f_expect_table_init() |
| 420 | runs on MGCP_Emulation_CT { |
| 421 | var integer i; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 422 | for (i := 0; i < sizeof(MgcpExpectTable); i := i + 1) { |
| 423 | MgcpExpectTable[i].crit := omit; |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 427 | function DummyUnitdataCallback(MgcpMessage msg) |
| 428 | runs on MGCP_Emulation_CT return template MgcpMessage { |
| 429 | log("Ignoring MGCP ", msg); |
| 430 | return omit; |
| 431 | } |
| 432 | |
| 433 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 434 | } |