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