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 | |
| 196 | |
| 197 | /* Check if the given transaction ID is a pending CRCX. If yes, return true + remove */ |
| 198 | private function f_trans_id_was_pending(MgcpTransId trans_id) |
| 199 | runs on MGCP_Emulation_CT return boolean { |
| 200 | for (var integer i := 0; i < lengthof(MgcpPendingTrans); i := i+1) { |
| 201 | if (MgcpPendingTrans[i] == trans_id) { |
| 202 | /* Remove from list */ |
| 203 | var MgcpTransIds OldPendingTrans := MgcpPendingTrans; |
| 204 | MgcpPendingTrans := {} |
| 205 | for (var integer j := 0; j < lengthof(OldPendingTrans); j := j+1) { |
| 206 | if (j != i) { |
| 207 | MgcpPendingTrans := MgcpPendingTrans & {OldPendingTrans[j]}; |
| 208 | } |
| 209 | } |
| 210 | return true; |
| 211 | } |
| 212 | } |
| 213 | return false; |
| 214 | } |
| 215 | |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 216 | /* TODO: move this to MGCP_Types? */ |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 217 | function f_mgcp_ep(MgcpMessage msg) return MgcpEndpoint { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 218 | var MgcpParameterList params; |
| 219 | var integer i; |
| 220 | if (ischosen(msg.command)) { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 221 | return msg.command.line.ep; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 222 | } else { |
Harald Welte | 363cb0a | 2018-01-30 19:35:53 +0100 | [diff] [blame] | 223 | var MgcpEndpoint ep; |
| 224 | if (f_mgcp_find_param(msg, "Z", ep) == false) { |
| 225 | setverdict(fail, "No SpecificEndpointName in MGCP response", msg); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 226 | mtc.stop; |
Harald Welte | 363cb0a | 2018-01-30 19:35:53 +0100 | [diff] [blame] | 227 | } |
| 228 | return ep; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 229 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 232 | private function f_ep_table_init() |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 233 | runs on MGCP_Emulation_CT { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 234 | for (var integer i := 0; i < sizeof(MgcpEndpointTable); i := i+1) { |
| 235 | MgcpEndpointTable[i].comp_ref := null; |
| 236 | MgcpEndpointTable[i].endpoint := omit; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Pau Espin Pedrol | 1a026a5 | 2019-06-18 17:21:52 +0200 | [diff] [blame] | 240 | private function f_forward_to_client(MGCP_RecvFrom mrf, MGCP_ConnHdlr vc_conn) runs on MGCP_Emulation_CT { |
| 241 | if (g_pars.multi_conn_mode) { |
| 242 | MGCP_CLIENT_MULTI.send(mrf) to vc_conn; |
| 243 | } else { |
| 244 | MGCP_CLIENT.send(mrf.msg.command) to vc_conn; |
| 245 | } |
| 246 | } |
| 247 | |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 248 | 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] | 249 | var Result res; |
Pau Espin Pedrol | 1a026a5 | 2019-06-18 17:21:52 +0200 | [diff] [blame] | 250 | g_pars := p; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 251 | g_mgcp_id := id; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 252 | f_ep_table_init(); |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 253 | f_expect_table_init(); |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 254 | |
| 255 | map(self:MGCP, system:MGCP_CODEC_PT); |
Pau Espin Pedrol | 1a026a5 | 2019-06-18 17:21:52 +0200 | [diff] [blame] | 256 | if (p.multi_conn_mode or p.callagent_udp_port == -1) { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 257 | res := MGCP_CodecPort_CtrlFunct.f_IPL4_listen(MGCP, p.mgw_ip, p.mgw_udp_port, { udp:={} }); |
| 258 | } else { |
| 259 | res := MGCP_CodecPort_CtrlFunct.f_IPL4_connect(MGCP, p.callagent_ip, p.callagent_udp_port, p.mgw_ip, p.mgw_udp_port, -1, { udp:={} }); |
| 260 | } |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 261 | if (not ispresent(res.connId)) { |
| 262 | setverdict(fail, "Could not connect MGCP socket, check your configuration"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 263 | mtc.stop; |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 264 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 265 | g_mgcp_conn_id := res.connId; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 266 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 267 | while (true) { |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 268 | var MGCP_ConnHdlr vc_conn; |
| 269 | var ExpectCriteria crit; |
| 270 | var MGCP_RecvFrom mrf; |
Pau Espin Pedrol | 1a026a5 | 2019-06-18 17:21:52 +0200 | [diff] [blame] | 271 | var MGCP_SendTo mst; |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 272 | var MgcpMessage msg; |
| 273 | var MgcpCommand cmd; |
| 274 | var MgcpResponse resp; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 275 | var MgcpEndpoint ep; |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 276 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 277 | alt { |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 278 | /* MGCP from client */ |
Pau Espin Pedrol | 1a026a5 | 2019-06-18 17:21:52 +0200 | [diff] [blame] | 279 | [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] | 280 | msg := { |
| 281 | response := resp |
| 282 | }; |
| 283 | /* If this is the resposne to a pending CRCX, extract Endpoint and store in table */ |
| 284 | if (f_trans_id_was_pending(resp.line.trans_id)) { |
| 285 | f_ep_table_add(vc_conn, f_mgcp_ep(msg)); |
| 286 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 287 | /* Pass message through */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 288 | /* TODO: check which ConnectionID client has allocated + store in table? */ |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 289 | MGCP.send(t_MGCP_Send(g_mgcp_conn_id, msg)); |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 290 | } |
Pau Espin Pedrol | 1a026a5 | 2019-06-18 17:21:52 +0200 | [diff] [blame] | 291 | |
| 292 | /* MGCP from client in Multi Conn mode */ |
| 293 | [p.multi_conn_mode] MGCP_CLIENT_MULTI.receive(MGCP_SendTo:?) -> value mst sender vc_conn { |
| 294 | /* If this is the resposne to a pending CRCX, extract Endpoint and store in table */ |
| 295 | if (f_trans_id_was_pending(mst.msg.response.line.trans_id)) { |
| 296 | f_ep_table_add(vc_conn, f_mgcp_ep(mst.msg)); |
| 297 | } |
| 298 | /* Pass message through */ |
| 299 | /* TODO: check which ConnectionID client has allocated + store in table? */ |
| 300 | MGCP.send(mst); |
| 301 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 302 | [] MGCP.receive(tr_MGCP_RecvFrom_R(?)) -> value mrf { |
Pau Espin Pedrol | 1a026a5 | 2019-06-18 17:21:52 +0200 | [diff] [blame] | 303 | if (not p.multi_conn_mode and p.callagent_udp_port == -1) { |
| 304 | /* we aren't yet connected to the remote side |
| 305 | port, let's fix this. This way upper layers |
| 306 | can use Send/Recv without caring about UDP |
| 307 | src/dst addr + port */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 308 | p.callagent_udp_port := mrf.remPort; |
Harald Welte | 930d0a7 | 2018-03-22 22:08:40 +0100 | [diff] [blame] | 309 | 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] | 310 | if (not ispresent(res.connId)) { |
| 311 | setverdict(fail, "Could not connect MGCP socket, check your configuration"); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 312 | mtc.stop; |
Harald Welte | 9220f63 | 2018-05-23 20:27:02 +0200 | [diff] [blame] | 313 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 314 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 315 | if (ischosen(mrf.msg.command)) { |
| 316 | cmd := mrf.msg.command; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 317 | if (f_ep_known(cmd.line.ep)) { |
| 318 | vc_conn := f_comp_by_ep(cmd.line.ep); |
Pau Espin Pedrol | 1a026a5 | 2019-06-18 17:21:52 +0200 | [diff] [blame] | 319 | f_forward_to_client(mrf, vc_conn); |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 320 | } else { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 321 | if (cmd.line.verb == "CRCX") { |
| 322 | vc_conn := ops.create_cb.apply(cmd, id); |
Harald Welte | 363cb0a | 2018-01-30 19:35:53 +0100 | [diff] [blame] | 323 | if (not match(cmd.line.ep, t_MGCP_EP_wildcard)) { |
| 324 | /* non-wildcard EP, use directly */ |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 325 | f_ep_table_add(vc_conn, cmd.line.ep); |
| 326 | } else { |
| 327 | /* add this transaction to list of pending transactions */ |
| 328 | MgcpPendingTrans := MgcpPendingTrans & {cmd.line.trans_id}; |
| 329 | } |
Pau Espin Pedrol | 1a026a5 | 2019-06-18 17:21:52 +0200 | [diff] [blame] | 330 | f_forward_to_client(mrf, vc_conn); |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 331 | } else { |
| 332 | /* connectionless MGCP, i.e. messages without ConnectionId */ |
| 333 | var template MgcpMessage r := ops.unitdata_cb.apply(mrf.msg); |
| 334 | if (isvalue(r)) { |
Pau Espin Pedrol | 1a026a5 | 2019-06-18 17:21:52 +0200 | [diff] [blame] | 335 | MGCP.send(t_MGCP_SendToMrf(mrf, r)); |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 336 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 337 | } |
| 338 | } |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 339 | } else { |
| 340 | setverdict(fail, "Received unexpected MGCP response: ", mrf.msg.response); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 341 | mtc.stop; |
Daniel Willmann | 166bbb3 | 2018-01-17 15:28:04 +0100 | [diff] [blame] | 342 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 343 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 344 | [] MGCP_PROC.getcall(MGCPEM_register:{?,?}) -> param(crit, vc_conn) { |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 345 | f_create_expect(crit, vc_conn); |
Harald Welte | e32ad99 | 2018-05-31 22:17:46 +0200 | [diff] [blame] | 346 | MGCP_PROC.reply(MGCPEM_register:{crit, vc_conn}) to vc_conn; |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 347 | } |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 348 | [] MGCP_PROC.getcall(MGCPEM_delete_ep:{?,?}) -> param(ep, vc_conn) { |
| 349 | f_ep_table_del(vc_conn, ep); |
Harald Welte | e32ad99 | 2018-05-31 22:17:46 +0200 | [diff] [blame] | 350 | MGCP_PROC.reply(MGCPEM_delete_ep:{ep, vc_conn}) to vc_conn; |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 351 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 352 | } |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 353 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | /* "Expect" Handling */ |
| 358 | |
| 359 | /* */ |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 360 | type record ExpectCriteria { |
| 361 | MgcpConnectionId connid optional, |
| 362 | MgcpEndpoint endpoint optional, |
| 363 | MgcpTransId transid optional |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | type record ExpectData { |
| 367 | ExpectCriteria crit optional, |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 368 | MGCP_ConnHdlr vc_conn |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 371 | signature MGCPEM_register(in ExpectCriteria cmd, in MGCP_ConnHdlr hdlr); |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 372 | signature MGCPEM_delete_ep(in MgcpEndpoint ep, in MGCP_ConnHdlr hdlr); |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 373 | |
| 374 | type port MGCPEM_PROC_PT procedure { |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 375 | inout MGCPEM_register, MGCPEM_delete_ep; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 376 | } with { extension "internal" }; |
| 377 | |
| 378 | function f_get_mgcp_by_crit(ExpectCriteria crit) |
| 379 | return template MgcpCommand { |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 380 | var template MgcpCommand ret := { |
| 381 | line := { |
| 382 | verb := ?, |
| 383 | trans_id := ?, |
| 384 | ep := ?, |
| 385 | ver := ? |
| 386 | }, |
| 387 | params := *, |
| 388 | sdp := * |
| 389 | } |
| 390 | if (ispresent(crit.connid)) { |
| 391 | ret.params := { *, ts_MgcpParConnectionId(crit.connid), * }; |
| 392 | } |
| 393 | if (ispresent(crit.endpoint)) { |
| 394 | ret.line.ep := crit.endpoint; |
| 395 | } |
| 396 | if (ispresent(crit.transid)) { |
| 397 | ret.line.trans_id := crit.transid; |
| 398 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 399 | |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | /* Function that can be used as create_cb and will usse the expect table */ |
| 404 | function ExpectedCreateCallback(MgcpCommand cmd, charstring id) |
| 405 | runs on MGCP_Emulation_CT return MGCP_ConnHdlr { |
| 406 | var MGCP_ConnHdlr ret := null; |
| 407 | var template MgcpCommand mgcpcmd; |
| 408 | var integer i; |
| 409 | |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 410 | for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) { |
| 411 | if (not ispresent(MgcpExpectTable[i].crit)) { |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 412 | continue; |
| 413 | } |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 414 | /* FIXME: Ignore criteria for now */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 415 | mgcpcmd := f_get_mgcp_by_crit(MgcpExpectTable[i].crit); |
| 416 | if (match(cmd, mgcpcmd)) { |
| 417 | ret := MgcpExpectTable[i].vc_conn; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 418 | /* Release this entry */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 419 | MgcpExpectTable[i].crit := omit; |
| 420 | MgcpExpectTable[i].vc_conn := null; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 421 | log("Found Expect[", i, "] for ", cmd, " handled at ", ret); |
| 422 | return ret; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 423 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 424 | } |
| 425 | setverdict(fail, "Couldn't find Expect for CRCX", cmd); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 426 | mtc.stop; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | private function f_create_expect(ExpectCriteria crit, MGCP_ConnHdlr hdlr) |
| 430 | runs on MGCP_Emulation_CT { |
| 431 | var integer i; |
| 432 | |
| 433 | /* Check an entry like this is not already presnt */ |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 434 | for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) { |
| 435 | if (crit == MgcpExpectTable[i].crit) { |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 436 | setverdict(fail, "Crit already present", crit); |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 437 | mtc.stop; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 438 | } |
| 439 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 440 | for (i := 0; i < sizeof(MgcpExpectTable); i := i+1) { |
| 441 | if (not ispresent(MgcpExpectTable[i].crit)) { |
| 442 | MgcpExpectTable[i].crit := crit; |
| 443 | MgcpExpectTable[i].vc_conn := hdlr; |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 444 | log("Created Expect[", i, "] for ", crit, " to be handled at ", hdlr); |
| 445 | return; |
| 446 | } |
| 447 | } |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 448 | testcase.stop("No space left in MgcpExpectTable") |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 452 | function f_create_mgcp_expect(ExpectCriteria dest_number) runs on MGCP_ConnHdlr { |
| 453 | MGCP_PROC.call(MGCPEM_register:{dest_number, self}) { |
| 454 | [] MGCP_PROC.getreply(MGCPEM_register:{?,?}) {}; |
| 455 | } |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Harald Welte | 9601c81 | 2018-01-26 18:58:20 +0100 | [diff] [blame] | 458 | /* client/conn_hdlr side function to use procedure port to create expect in emulation */ |
| 459 | function f_create_mgcp_delete_ep(MgcpEndpoint ep) runs on MGCP_ConnHdlr { |
| 460 | MGCP_PROC.call(MGCPEM_delete_ep:{ep, self}) { |
| 461 | [] MGCP_PROC.getreply(MGCPEM_delete_ep:{?,?}) {}; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 466 | private function f_expect_table_init() |
| 467 | runs on MGCP_Emulation_CT { |
| 468 | var integer i; |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 469 | for (i := 0; i < sizeof(MgcpExpectTable); i := i + 1) { |
| 470 | MgcpExpectTable[i].crit := omit; |
Daniel Willmann | 955627a | 2018-01-17 15:22:32 +0100 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 474 | function DummyUnitdataCallback(MgcpMessage msg) |
| 475 | runs on MGCP_Emulation_CT return template MgcpMessage { |
| 476 | log("Ignoring MGCP ", msg); |
| 477 | return omit; |
| 478 | } |
| 479 | |
Philipp Maier | 11a5894 | 2018-06-25 16:40:48 +0200 | [diff] [blame] | 480 | /* Determine encoding name for a specified payload type number */ |
| 481 | function f_encoding_name_from_pt(SDP_FIELD_PayloadType pt) return charstring { |
| 482 | if (pt == PT_PCMU) { |
| 483 | return "PCMU"; |
| 484 | } else if (pt == PT_GSM) { |
| 485 | return "GSM"; |
| 486 | } else if (pt == PT_PCMA) { |
| 487 | return "PCMA"; |
| 488 | } else if (pt == PT_GSMEFR) { |
| 489 | return "GSM-EFR"; |
| 490 | } else if (pt == PT_GSMHR) { |
| 491 | return "GSM-HR-08"; |
| 492 | } else if (pt == PT_AMR) { |
| 493 | return "AMR"; |
| 494 | } else if (pt == PT_AMRWB) { |
| 495 | return "AMR-WB"; |
| 496 | } |
| 497 | |
Daniel Willmann | e4ff537 | 2018-07-05 17:35:03 +0200 | [diff] [blame] | 498 | setverdict(fail, "Unknown payload type ", pt); |
| 499 | mtc.stop; |
Philipp Maier | 11a5894 | 2018-06-25 16:40:48 +0200 | [diff] [blame] | 500 | } |
Harald Welte | bb5a121 | 2018-01-26 10:34:44 +0100 | [diff] [blame] | 501 | |
Daniel Willmann | fa870f5 | 2018-01-17 12:37:14 +0100 | [diff] [blame] | 502 | } |