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. |
Harald Welte | 34b5a95 | 2019-05-27 11:54:11 +0200 | [diff] [blame^] | 28 | * |
| 29 | * SPDX-License-Identifier: GPL-2.0-or-later |
Harald Welte | a02515f | 2018-01-26 10:33:23 +0100 | [diff] [blame] | 30 | */ |
| 31 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 32 | import from MGCP_CodecPort all; |
| 33 | import from MGCP_CodecPort_CtrlFunct all; |
| 34 | import from MGCP_Types all; |
| 35 | import from MGCP_Templates all; |
| 36 | import from Osmocom_Types all; |
| 37 | import from IPL4asp_Types all; |
| 38 | |
| 39 | type component MGCP_ConnHdlr { |
| 40 | port MGCP_Conn_PT MGCP; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 41 | /* procedure based port to register for incoming connections */ |
| 42 | port MGCPEM_PROC_PT MGCP_PROC; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* port between individual per-connection components and this dispatcher */ |
| 46 | type port MGCP_Conn_PT message { |
| 47 | inout MgcpCommand, MgcpResponse; |
| 48 | } with { extension "internal" }; |
| 49 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 50 | /* represents a single MGCP Endpoint */ |
| 51 | type record EndpointData { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 52 | MGCP_ConnHdlr comp_ref, |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 53 | MgcpEndpoint endpoint optional |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 54 | }; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 55 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 56 | /* pending CRCX with their transaction ID */ |
| 57 | type set of MgcpTransId MgcpTransIds; |
| 58 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 59 | type component MGCP_Emulation_CT { |
| 60 | /* Port facing to the UDP SUT */ |
| 61 | port MGCP_CODEC_PT MGCP; |
| 62 | /* All MGCP_ConnHdlr MGCP ports connect here |
| 63 | * MGCP_Emulation_CT.main needs to figure out what messages |
| 64 | * to send where with CLIENT.send() to vc_conn */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 65 | port MGCP_Conn_PT MGCP_CLIENT; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 66 | /* currently tracked connections */ |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 67 | var EndpointData MgcpEndpointTable[16]; |
| 68 | var MgcpTransIds MgcpPendingTrans := {}; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 69 | /* pending expected CRCX */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 70 | var ExpectData MgcpExpectTable[8]; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 71 | /* procedure based port to register for incoming connections */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 72 | port MGCPEM_PROC_PT MGCP_PROC; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 73 | |
| 74 | var charstring g_mgcp_id; |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 75 | var integer g_mgcp_conn_id := -1; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | type function MGCPCreateCallback(MgcpCommand cmd, charstring id) |
| 79 | runs on MGCP_Emulation_CT return MGCP_ConnHdlr; |
| 80 | |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 81 | type function MGCPUnitdataCallback(MgcpMessage msg) |
| 82 | runs on MGCP_Emulation_CT return template MgcpMessage; |
| 83 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 84 | type record MGCPOps { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 85 | MGCPCreateCallback create_cb, |
| 86 | MGCPUnitdataCallback unitdata_cb |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | type record MGCP_conn_parameters { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 90 | HostName callagent_ip, |
| 91 | PortNumber callagent_udp_port, |
| 92 | HostName mgw_ip, |
| 93 | PortNumber mgw_udp_port |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 96 | function tr_MGCP_RecvFrom_R(template MgcpMessage msg) |
| 97 | runs on MGCP_Emulation_CT return template MGCP_RecvFrom { |
| 98 | var template MGCP_RecvFrom mrf := { |
| 99 | connId := g_mgcp_conn_id, |
| 100 | remName := ?, |
| 101 | remPort := ?, |
| 102 | locName := ?, |
| 103 | locPort := ?, |
| 104 | msg := msg |
| 105 | } |
| 106 | return mrf; |
| 107 | } |
| 108 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 109 | private function f_ep_known(MgcpEndpoint ep) |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 110 | runs on MGCP_Emulation_CT return boolean { |
| 111 | var integer i; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 112 | for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 113 | if (MgcpEndpointTable[i].endpoint == ep) { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | private function f_comp_known(MGCP_ConnHdlr client) |
| 121 | runs on MGCP_Emulation_CT return boolean { |
| 122 | var integer i; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 123 | for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 124 | if (MgcpEndpointTable[i].comp_ref == client) { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 131 | private function f_comp_by_ep(MgcpEndpoint ep) |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 132 | runs on MGCP_Emulation_CT return MGCP_ConnHdlr { |
| 133 | var integer i; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 134 | for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 135 | if (MgcpEndpointTable[i].endpoint == ep) { |
| 136 | return MgcpEndpointTable[i].comp_ref; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 137 | } |
| 138 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 139 | setverdict(fail, "MGCP Endpoint Table not found by Endpoint", ep); |
| 140 | mtc.stop; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 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 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 151 | setverdict(fail, "MGCP Endpoint Table not found by component ", client); |
| 152 | mtc.stop; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 155 | private function f_ep_table_add(MGCP_ConnHdlr comp_ref, MgcpEndpoint ep) |
| 156 | runs on MGCP_Emulation_CT { |
| 157 | var integer i; |
| 158 | for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 159 | if (not isvalue(MgcpEndpointTable[i].endpoint)) { |
| 160 | MgcpEndpointTable[i].endpoint := ep; |
| 161 | MgcpEndpointTable[i].comp_ref := comp_ref; |
| 162 | return; |
| 163 | } |
| 164 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 165 | testcase.stop("MGCP Endpoint Table full!"); |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | private function f_ep_table_del(MGCP_ConnHdlr comp_ref, MgcpEndpoint ep) |
| 169 | runs on MGCP_Emulation_CT { |
| 170 | var integer i; |
| 171 | for (i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 172 | if (MgcpEndpointTable[i].comp_ref == comp_ref and |
| 173 | MgcpEndpointTable[i].endpoint == ep) { |
| 174 | MgcpEndpointTable[i].endpoint := omit; |
| 175 | MgcpEndpointTable[i].comp_ref := null; |
| 176 | return; |
| 177 | } |
| 178 | } |
| 179 | setverdict(fail, "MGCP Endpoint Table: Couldn't find to-be-deleted entry!"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 180 | mtc.stop; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | |
| 184 | /* Check if the given transaction ID is a pending CRCX. If yes, return true + remove */ |
| 185 | private function f_trans_id_was_pending(MgcpTransId trans_id) |
| 186 | runs on MGCP_Emulation_CT return boolean { |
| 187 | for (var integer i := 0; i < lengthof(MgcpPendingTrans); i := i+1) { |
| 188 | if (MgcpPendingTrans[i] == trans_id) { |
| 189 | /* Remove from list */ |
| 190 | var MgcpTransIds OldPendingTrans := MgcpPendingTrans; |
| 191 | MgcpPendingTrans := {} |
| 192 | for (var integer j := 0; j < lengthof(OldPendingTrans); j := j+1) { |
| 193 | if (j != i) { |
| 194 | MgcpPendingTrans := MgcpPendingTrans & {OldPendingTrans[j]}; |
| 195 | } |
| 196 | } |
| 197 | return true; |
| 198 | } |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 203 | /* TODO: move this to MGCP_Types? */ |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 204 | function f_mgcp_ep(MgcpMessage msg) return MgcpEndpoint { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 205 | var MgcpParameterList params; |
| 206 | var integer i; |
| 207 | if (ischosen(msg.command)) { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 208 | return msg.command.line.ep; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 209 | } else { |
Harald Welte | 363cb0a | 2018-01-30 19:35:53 +0100 | [diff] [blame] | 210 | var MgcpEndpoint ep; |
| 211 | if (f_mgcp_find_param(msg, "Z", ep) == false) { |
| 212 | setverdict(fail, "No SpecificEndpointName in MGCP response", msg); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 213 | mtc.stop; |
Harald Welte | 363cb0a | 2018-01-30 19:35:53 +0100 | [diff] [blame] | 214 | } |
| 215 | return ep; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 216 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 219 | private function f_ep_table_init() |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 220 | runs on MGCP_Emulation_CT { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 221 | for (var integer i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 222 | MgcpEndpointTable[i].comp_ref := null; |
| 223 | MgcpEndpointTable[i].endpoint := omit; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 227 | 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] | 228 | var Result res; |
| 229 | g_mgcp_id := id; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 230 | f_ep_table_init(); |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 231 | f_expect_table_init(); |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 232 | |
| 233 | map(self:MGCP, system:MGCP_CODEC_PT); |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 234 | if (p.callagent_udp_port == -1) { |
| 235 | res := MGCP_CodecPort_CtrlFunct.f_IPL4_listen(MGCP, p.mgw_ip, p.mgw_udp_port, { udp:={} }); |
| 236 | } else { |
| 237 | res := MGCP_CodecPort_CtrlFunct.f_IPL4_connect(MGCP, p.callagent_ip, p.callagent_udp_port, p.mgw_ip, p.mgw_udp_port, -1, { udp:={} }); |
| 238 | } |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 239 | if (not ispresent(res.connId)) { |
| 240 | setverdict(fail, "Could not connect MGCP socket, check your configuration"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 241 | mtc.stop; |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 242 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 243 | g_mgcp_conn_id := res.connId; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 244 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 245 | while (true) { |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 246 | var MGCP_ConnHdlr vc_conn; |
| 247 | var ExpectCriteria crit; |
| 248 | var MGCP_RecvFrom mrf; |
| 249 | var MgcpMessage msg; |
| 250 | var MgcpCommand cmd; |
| 251 | var MgcpResponse resp; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 252 | var MgcpEndpoint ep; |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 253 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 254 | alt { |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 255 | /* MGCP from client */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 256 | [] MGCP_CLIENT.receive(MgcpResponse:?) -> value resp sender vc_conn { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 257 | msg := { |
| 258 | response := resp |
| 259 | }; |
| 260 | /* If this is the resposne to a pending CRCX, extract Endpoint and store in table */ |
| 261 | if (f_trans_id_was_pending(resp.line.trans_id)) { |
| 262 | f_ep_table_add(vc_conn, f_mgcp_ep(msg)); |
| 263 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 264 | /* Pass message through */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 265 | /* TODO: check which ConnectionID client has allocated + store in table? */ |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 266 | MGCP.send(t_MGCP_Send(g_mgcp_conn_id, msg)); |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 267 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 268 | [] MGCP.receive(tr_MGCP_RecvFrom_R(?)) -> value mrf { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 269 | if (p.callagent_udp_port == -1) { |
| 270 | /* we aren't yet connected to the remote side port, let's fix this */ |
| 271 | p.callagent_udp_port := mrf.remPort; |
Harald Welte | 930d0a7 | 2018-03-22 22:08:40 +0100 | [diff] [blame] | 272 | res := 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:={} }); |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 273 | if (not ispresent(res.connId)) { |
| 274 | setverdict(fail, "Could not connect MGCP socket, check your configuration"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 275 | mtc.stop; |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 276 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 277 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 278 | if (ischosen(mrf.msg.command)) { |
| 279 | cmd := mrf.msg.command; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 280 | if (f_ep_known(cmd.line.ep)) { |
| 281 | vc_conn := f_comp_by_ep(cmd.line.ep); |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 282 | MGCP_CLIENT.send(cmd) to vc_conn; |
| 283 | } else { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 284 | if (cmd.line.verb == "CRCX") { |
| 285 | vc_conn := ops.create_cb.apply(cmd, id); |
Harald Welte | 363cb0a | 2018-01-30 19:35:53 +0100 | [diff] [blame] | 286 | if (not match(cmd.line.ep, t_MGCP_EP_wildcard)) { |
| 287 | /* non-wildcard EP, use directly */ |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 288 | f_ep_table_add(vc_conn, cmd.line.ep); |
| 289 | } else { |
| 290 | /* add this transaction to list of pending transactions */ |
| 291 | MgcpPendingTrans := MgcpPendingTrans & {cmd.line.trans_id}; |
| 292 | } |
| 293 | MGCP_CLIENT.send(cmd) to vc_conn; |
| 294 | } else { |
| 295 | /* connectionless MGCP, i.e. messages without ConnectionId */ |
| 296 | var template MgcpMessage r := ops.unitdata_cb.apply(mrf.msg); |
| 297 | if (isvalue(r)) { |
| 298 | MGCP.send(t_MGCP_Send(g_mgcp_conn_id, r)); |
| 299 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 300 | } |
| 301 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 302 | } else { |
| 303 | setverdict(fail, "Received unexpected MGCP response: ", mrf.msg.response); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 304 | mtc.stop; |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 305 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 306 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 307 | [] MGCP_PROC.getcall(MGCPEM_register:{?,?}) -> param(crit, vc_conn) { |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 308 | f_create_expect(crit, vc_conn); |
Harald Welte | e32ad99 | 2018-05-31 22:17:46 +0200 | [diff] [blame] | 309 | MGCP_PROC.reply(MGCPEM_register:{crit, vc_conn}) to vc_conn; |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 310 | } |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 311 | [] MGCP_PROC.getcall(MGCPEM_delete_ep:{?,?}) -> param(ep, vc_conn) { |
| 312 | f_ep_table_del(vc_conn, ep); |
Harald Welte | e32ad99 | 2018-05-31 22:17:46 +0200 | [diff] [blame] | 313 | MGCP_PROC.reply(MGCPEM_delete_ep:{ep, vc_conn}) to vc_conn; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 314 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 315 | } |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 316 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| 320 | /* "Expect" Handling */ |
| 321 | |
| 322 | /* */ |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 323 | type record ExpectCriteria { |
| 324 | MgcpConnectionId connid optional, |
| 325 | MgcpEndpoint endpoint optional, |
| 326 | MgcpTransId transid optional |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | type record ExpectData { |
| 330 | ExpectCriteria crit optional, |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 331 | MGCP_ConnHdlr vc_conn |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 334 | signature MGCPEM_register(in ExpectCriteria cmd, in MGCP_ConnHdlr hdlr); |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 335 | signature MGCPEM_delete_ep(in MgcpEndpoint ep, in MGCP_ConnHdlr hdlr); |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 336 | |
| 337 | type port MGCPEM_PROC_PT procedure { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 338 | inout MGCPEM_register, MGCPEM_delete_ep; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 339 | } with { extension "internal" }; |
| 340 | |
| 341 | function f_get_mgcp_by_crit(ExpectCriteria crit) |
| 342 | return template MgcpCommand { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 343 | var template MgcpCommand ret := { |
| 344 | line := { |
| 345 | verb := ?, |
| 346 | trans_id := ?, |
| 347 | ep := ?, |
| 348 | ver := ? |
| 349 | }, |
| 350 | params := *, |
| 351 | sdp := * |
| 352 | } |
| 353 | if (ispresent(crit.connid)) { |
| 354 | ret.params := { *, ts_MgcpParConnectionId(crit.connid), * }; |
| 355 | } |
| 356 | if (ispresent(crit.endpoint)) { |
| 357 | ret.line.ep := crit.endpoint; |
| 358 | } |
| 359 | if (ispresent(crit.transid)) { |
| 360 | ret.line.trans_id := crit.transid; |
| 361 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | /* Function that can be used as create_cb and will usse the expect table */ |
| 367 | function ExpectedCreateCallback(MgcpCommand cmd, charstring id) |
| 368 | runs on MGCP_Emulation_CT return MGCP_ConnHdlr { |
| 369 | var MGCP_ConnHdlr ret := null; |
| 370 | var template MgcpCommand mgcpcmd; |
| 371 | var integer i; |
| 372 | |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 373 | for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) { |
| 374 | if (not ispresent(MgcpExpectTable[i].crit)) { |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 375 | continue; |
| 376 | } |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 377 | /* FIXME: Ignore criteria for now */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 378 | mgcpcmd := f_get_mgcp_by_crit(MgcpExpectTable[i].crit); |
| 379 | if (match(cmd, mgcpcmd)) { |
| 380 | ret := MgcpExpectTable[i].vc_conn; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 381 | /* Release this entry */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 382 | MgcpExpectTable[i].crit := omit; |
| 383 | MgcpExpectTable[i].vc_conn := null; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 384 | log("Found Expect[", i, "] for ", cmd, " handled at ", ret); |
| 385 | return ret; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 386 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 387 | } |
| 388 | setverdict(fail, "Couldn't find Expect for CRCX", cmd); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 389 | mtc.stop; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private function f_create_expect(ExpectCriteria crit, MGCP_ConnHdlr hdlr) |
| 393 | runs on MGCP_Emulation_CT { |
| 394 | var integer i; |
| 395 | |
| 396 | /* Check an entry like this is not already presnt */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 397 | for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) { |
| 398 | if (crit == MgcpExpectTable[i].crit) { |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 399 | setverdict(fail, "Crit already present", crit); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 400 | mtc.stop; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 401 | } |
| 402 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 403 | for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) { |
| 404 | if (not ispresent(MgcpExpectTable[i].crit)) { |
| 405 | MgcpExpectTable[i].crit := crit; |
| 406 | MgcpExpectTable[i].vc_conn := hdlr; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 407 | log("Created Expect[", i, "] for ", crit, " to be handled at ", hdlr); |
| 408 | return; |
| 409 | } |
| 410 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 411 | testcase.stop("No space left in MgcpExpectTable") |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 415 | function f_create_mgcp_expect(ExpectCriteria dest_number) runs on MGCP_ConnHdlr { |
| 416 | MGCP_PROC.call(MGCPEM_register:{dest_number, self}) { |
| 417 | [] MGCP_PROC.getreply(MGCPEM_register:{?,?}) {}; |
| 418 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 421 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 422 | function f_create_mgcp_delete_ep(MgcpEndpoint ep) runs on MGCP_ConnHdlr { |
| 423 | MGCP_PROC.call(MGCPEM_delete_ep:{ep, self}) { |
| 424 | [] MGCP_PROC.getreply(MGCPEM_delete_ep:{?,?}) {}; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 429 | private function f_expect_table_init() |
| 430 | runs on MGCP_Emulation_CT { |
| 431 | var integer i; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 432 | for (i := 0; i < sizeof(MgcpExpectTable); i := i + 1) { |
| 433 | MgcpExpectTable[i].crit := omit; |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 437 | function DummyUnitdataCallback(MgcpMessage msg) |
| 438 | runs on MGCP_Emulation_CT return template MgcpMessage { |
| 439 | log("Ignoring MGCP ", msg); |
| 440 | return omit; |
| 441 | } |
| 442 | |
Philipp Maier | 11a5894 | 2018-06-25 16:40:48 +0200 | [diff] [blame] | 443 | /* Determine encoding name for a specified payload type number */ |
| 444 | function f_encoding_name_from_pt(SDP_FIELD_PayloadType pt) return charstring { |
| 445 | if (pt == PT_PCMU) { |
| 446 | return "PCMU"; |
| 447 | } else if (pt == PT_GSM) { |
| 448 | return "GSM"; |
| 449 | } else if (pt == PT_PCMA) { |
| 450 | return "PCMA"; |
| 451 | } else if (pt == PT_GSMEFR) { |
| 452 | return "GSM-EFR"; |
| 453 | } else if (pt == PT_GSMHR) { |
| 454 | return "GSM-HR-08"; |
| 455 | } else if (pt == PT_AMR) { |
| 456 | return "AMR"; |
| 457 | } else if (pt == PT_AMRWB) { |
| 458 | return "AMR-WB"; |
| 459 | } |
| 460 | |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 461 | setverdict(fail, "Unknown payload type ", pt); |
| 462 | mtc.stop; |
Philipp Maier | 11a5894 | 2018-06-25 16:40:48 +0200 | [diff] [blame] | 463 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 464 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 465 | } |