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