Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 1 | /* (C) 2018 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 2 | * All Rights Reserved |
| 3 | * |
| 4 | * Author: Philipp Maier |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <osmocom/mgcp_client/mgcp_client.h> |
| 22 | #include <osmocom/mgcp_client/mgcp_client_fsm.h> |
| 23 | #include <osmocom/core/utils.h> |
| 24 | #include <osmocom/core/fsm.h> |
| 25 | #include <osmocom/core/byteswap.h> |
| 26 | #include <arpa/inet.h> |
| 27 | #include <osmocom/core/logging.h> |
| 28 | |
| 29 | /* Context information, this is attached to the priv pointer of the FSM and |
| 30 | * is also handed back when dispatcheing events to the parent FSM. This is |
| 31 | * purly intened and not meant to be accessible for the API user */ |
| 32 | struct mgcp_ctx { |
| 33 | /* MGCP client instance that is used to interact with the MGW */ |
| 34 | struct mgcp_client *mgcp; |
| 35 | |
| 36 | /* The ID of the last pending transaction. This is used internally |
| 37 | * to cancel the transaction in case of an error */ |
| 38 | mgcp_trans_id_t mgw_pending_trans; |
| 39 | |
| 40 | /* Flag to mark that there is a pending transaction */ |
| 41 | bool mgw_trans_pending; |
| 42 | |
| 43 | /* Connection ID which has been assigned by he MGW */ |
Neels Hofmeyr | 55e0dcf | 2018-09-03 21:36:56 +0200 | [diff] [blame] | 44 | char conn_id[MGCP_CONN_ID_MAXLEN]; |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 45 | |
| 46 | /* Local RTP connection info, the MGW will send outgoing traffic to the |
| 47 | * ip/port specified here. The Address does not have to be choosen right |
| 48 | * on the creation of a connection. It can always be modified later by |
| 49 | * the user. */ |
| 50 | struct mgcp_conn_peer conn_peer_local; |
| 51 | |
| 52 | /* Remote RTP connection info, the ip/port specified here is the address |
| 53 | * where the MGW expects the RTP data to be sent. This address is |
| 54 | * defined by soly by the MGW and can not be influenced by the user. */ |
| 55 | struct mgcp_conn_peer conn_peer_remote; |
| 56 | |
| 57 | /* The terminate flag is a way to handle cornercase sitations that |
| 58 | * might occur when the user runs into an error situation and sends |
| 59 | * a DLCX command while the FSM is waiting for a response. In this |
| 60 | * case the DLCX command is not executed immediately. Instead the |
| 61 | * terminate flag is set. When the response to from the previous |
| 62 | * operation is received, we know that there is a DLCX event is |
| 63 | * pending. The FSM then generates the EV_DLCX by itsself before |
| 64 | * it enters ST_READY to cause the immediate execution of the |
| 65 | * DLCX procedure. (If normal operations are executed too fast, |
| 66 | * the API functions will return an error. In general, the user |
| 67 | * should synchronize using the callback events) */ |
| 68 | bool terminate; |
| 69 | |
| 70 | /* Event that is sent when the current operation is completed (except |
| 71 | * for DLCX, there the specified parent_term_evt is sent instead) */ |
| 72 | uint32_t parent_evt; |
| 73 | }; |
| 74 | |
| 75 | #define S(x) (1 << (x)) |
| 76 | |
| 77 | #define MGCP_MGW_TIMEOUT 4 /* in seconds */ |
| 78 | #define MGCP_MGW_TIMEOUT_TIMER_NR 1 |
| 79 | |
Philipp Maier | 01f0395 | 2018-02-26 14:33:25 +0100 | [diff] [blame] | 80 | enum fsm_mgcp_client_states { |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 81 | ST_CRCX, |
| 82 | ST_CRCX_RESP, |
| 83 | ST_READY, |
| 84 | ST_MDCX_RESP, |
| 85 | ST_DLCX_RESP, |
| 86 | }; |
| 87 | |
Philipp Maier | 01f0395 | 2018-02-26 14:33:25 +0100 | [diff] [blame] | 88 | enum fsm_mgcp_client_evt { |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 89 | EV_CRCX, |
| 90 | EV_CRCX_RESP, |
| 91 | EV_MDCX, |
| 92 | EV_MDCX_RESP, |
| 93 | EV_DLCX, |
| 94 | EV_DLCX_RESP, |
| 95 | }; |
| 96 | |
Philipp Maier | d2e3a52 | 2018-02-26 14:29:01 +0100 | [diff] [blame] | 97 | static const struct value_string fsm_mgcp_client_evt_names[] = { |
| 98 | OSMO_VALUE_STRING(EV_CRCX), |
| 99 | OSMO_VALUE_STRING(EV_CRCX_RESP), |
| 100 | OSMO_VALUE_STRING(EV_MDCX), |
| 101 | OSMO_VALUE_STRING(EV_MDCX_RESP), |
| 102 | OSMO_VALUE_STRING(EV_DLCX), |
| 103 | OSMO_VALUE_STRING(EV_DLCX_RESP), |
| 104 | {0, NULL} |
| 105 | }; |
| 106 | |
Neels Hofmeyr | 475f868 | 2018-08-23 16:38:59 +0200 | [diff] [blame] | 107 | static void make_crcx_msg(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info) |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 108 | { |
Neels Hofmeyr | 475f868 | 2018-08-23 16:38:59 +0200 | [diff] [blame] | 109 | *mgcp_msg = (struct mgcp_msg) { |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 110 | .verb = MGCP_VERB_CRCX, |
Neels Hofmeyr | 475f868 | 2018-08-23 16:38:59 +0200 | [diff] [blame] | 111 | .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID |
| 112 | | MGCP_MSG_PRESENCE_CONN_MODE), |
| 113 | .call_id = info->call_id, |
Philipp Maier | 54eb0e1 | 2018-05-29 09:49:52 +0200 | [diff] [blame] | 114 | .conn_mode = MGCP_CONN_RECV_ONLY, |
Neels Hofmeyr | 475f868 | 2018-08-23 16:38:59 +0200 | [diff] [blame] | 115 | .ptime = info->ptime, |
| 116 | .codecs_len = info->codecs_len, |
| 117 | .ptmap_len = info->ptmap_len |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 118 | }; |
Neels Hofmeyr | 475f868 | 2018-08-23 16:38:59 +0200 | [diff] [blame] | 119 | osmo_strlcpy(mgcp_msg->endpoint, info->endpoint, MGCP_ENDPOINT_MAXLEN); |
| 120 | memcpy(mgcp_msg->codecs, info->codecs, sizeof(mgcp_msg->codecs)); |
| 121 | memcpy(mgcp_msg->ptmap, info->ptmap, sizeof(mgcp_msg->ptmap)); |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 122 | |
| 123 | if (info->x_osmo_ign) { |
| 124 | mgcp_msg->x_osmo_ign = info->x_osmo_ign; |
| 125 | mgcp_msg->presence |= MGCP_MSG_PRESENCE_X_OSMO_IGN; |
| 126 | } |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Neels Hofmeyr | 475f868 | 2018-08-23 16:38:59 +0200 | [diff] [blame] | 129 | static void add_audio(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info) |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 130 | { |
Neels Hofmeyr | 475f868 | 2018-08-23 16:38:59 +0200 | [diff] [blame] | 131 | mgcp_msg->presence |= MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT; |
| 132 | mgcp_msg->audio_ip = info->addr; |
| 133 | mgcp_msg->audio_port = info->port; |
Philipp Maier | a5e0cf0 | 2018-08-29 14:33:01 +0200 | [diff] [blame] | 134 | mgcp_msg->conn_mode = MGCP_CONN_RECV_SEND; |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static struct msgb *make_mdcx_msg(struct mgcp_ctx *mgcp_ctx) |
| 138 | { |
| 139 | struct mgcp_msg mgcp_msg; |
| 140 | |
| 141 | mgcp_msg = (struct mgcp_msg) { |
| 142 | .verb = MGCP_VERB_MDCX, |
| 143 | .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID | |
| 144 | MGCP_MSG_PRESENCE_CONN_MODE | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT), |
| 145 | .call_id = mgcp_ctx->conn_peer_remote.call_id, |
| 146 | .conn_id = mgcp_ctx->conn_id, |
| 147 | .conn_mode = MGCP_CONN_RECV_SEND, |
| 148 | .audio_ip = mgcp_ctx->conn_peer_local.addr, |
| 149 | .audio_port = mgcp_ctx->conn_peer_local.port, |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 150 | .ptime = mgcp_ctx->conn_peer_local.ptime, |
Philipp Maier | 544448a | 2018-07-26 11:37:44 +0200 | [diff] [blame] | 151 | .codecs_len = mgcp_ctx->conn_peer_local.codecs_len, |
| 152 | .ptmap_len = mgcp_ctx->conn_peer_local.ptmap_len |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 153 | }; |
| 154 | osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 155 | memcpy(mgcp_msg.codecs, mgcp_ctx->conn_peer_local.codecs, sizeof(mgcp_msg.codecs)); |
Philipp Maier | 544448a | 2018-07-26 11:37:44 +0200 | [diff] [blame] | 156 | memcpy(mgcp_msg.ptmap, mgcp_ctx->conn_peer_local.ptmap, sizeof(mgcp_msg.ptmap)); |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 157 | |
| 158 | /* Note: We take the endpoint and the call_id from the remote |
| 159 | * connection info, because we can be confident that the |
| 160 | * information there is valid. For the local info, we explicitly |
| 161 | * allow endpoint and call_id to be optional */ |
| 162 | return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg); |
| 163 | } |
| 164 | |
| 165 | struct msgb *make_dlcx_msg(struct mgcp_ctx *mgcp_ctx) |
| 166 | { |
| 167 | struct mgcp_msg mgcp_msg; |
| 168 | |
| 169 | mgcp_msg = (struct mgcp_msg) { |
| 170 | .verb = MGCP_VERB_DLCX, |
| 171 | .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID), |
| 172 | .call_id = mgcp_ctx->conn_peer_remote.call_id, |
| 173 | .conn_id = mgcp_ctx->conn_id, |
| 174 | }; |
| 175 | osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN); |
| 176 | |
| 177 | return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg); |
| 178 | } |
| 179 | |
| 180 | static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv); |
| 181 | |
| 182 | static void fsm_crcx_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 183 | { |
| 184 | struct mgcp_ctx *mgcp_ctx = data; |
| 185 | struct mgcp_client *mgcp; |
Neels Hofmeyr | 475f868 | 2018-08-23 16:38:59 +0200 | [diff] [blame] | 186 | struct mgcp_msg mgcp_msg; |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 187 | struct msgb *msg; |
| 188 | int rc; |
| 189 | |
| 190 | OSMO_ASSERT(mgcp_ctx); |
| 191 | mgcp = mgcp_ctx->mgcp; |
| 192 | OSMO_ASSERT(mgcp); |
| 193 | |
| 194 | switch (event) { |
| 195 | case EV_CRCX: |
| 196 | LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: creating connection on MGW endpoint:%s...\n", |
| 197 | mgcp_ctx->conn_peer_local.endpoint); |
| 198 | |
Neels Hofmeyr | 475f868 | 2018-08-23 16:38:59 +0200 | [diff] [blame] | 199 | make_crcx_msg(&mgcp_msg, &mgcp_ctx->conn_peer_local); |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 200 | if (mgcp_ctx->conn_peer_local.port) |
Neels Hofmeyr | 475f868 | 2018-08-23 16:38:59 +0200 | [diff] [blame] | 201 | add_audio(&mgcp_msg, &mgcp_ctx->conn_peer_local); |
| 202 | msg = mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg); |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 203 | OSMO_ASSERT(msg); |
| 204 | |
| 205 | mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg); |
| 206 | mgcp_ctx->mgw_trans_pending = true; |
| 207 | rc = mgcp_client_tx(mgcp, msg, mgw_crcx_resp_cb, fi); |
| 208 | if (rc < 0) { |
| 209 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | osmo_fsm_inst_state_chg(fi, ST_CRCX_RESP, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR); |
| 214 | break; |
| 215 | default: |
| 216 | OSMO_ASSERT(false); |
| 217 | break; |
| 218 | } |
| 219 | } |
| 220 | |
Neels Hofmeyr | 04da5e5 | 2018-06-12 21:51:23 +0200 | [diff] [blame] | 221 | /* Return the CI that the MGW allocated during CRCX response. This is purely informational for logging |
| 222 | * and identity tracking; the mgcp_conn_*() functions take care of using the right CI internally. */ |
| 223 | const char *mgcp_conn_get_ci(struct osmo_fsm_inst *fi) |
| 224 | { |
| 225 | struct mgcp_ctx *mgcp_ctx = fi->priv; |
| 226 | return mgcp_ctx->conn_id; |
| 227 | } |
| 228 | |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 229 | static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv) |
| 230 | { |
| 231 | struct osmo_fsm_inst *fi = priv; |
| 232 | struct mgcp_ctx *mgcp_ctx; |
| 233 | int rc; |
| 234 | |
| 235 | OSMO_ASSERT(fi); |
| 236 | mgcp_ctx = fi->priv; |
| 237 | OSMO_ASSERT(mgcp_ctx); |
| 238 | |
| 239 | mgcp_ctx->mgw_trans_pending = false; |
| 240 | |
| 241 | if (r->head.response_code != 200) { |
| 242 | LOGPFSML(fi, LOGL_ERROR, |
| 243 | "MGW/CRCX: response yields error: %d %s\n", r->head.response_code, r->head.comment); |
| 244 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | osmo_strlcpy(mgcp_ctx->conn_id, r->head.conn_id, sizeof(mgcp_ctx->conn_id)); |
| 249 | LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with CI: %s\n", mgcp_ctx->conn_id); |
| 250 | |
| 251 | rc = mgcp_response_parse_params(r); |
| 252 | if (rc) { |
| 253 | LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: Cannot parse CRCX response\n"); |
| 254 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 255 | return; |
| 256 | } |
| 257 | LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port); |
| 258 | |
| 259 | osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr)); |
| 260 | mgcp_ctx->conn_peer_remote.port = r->audio_port; |
| 261 | |
| 262 | if (strlen(r->head.endpoint) > 0) { |
| 263 | /* If we get an endpoint identifier back from the MGW, take it */ |
| 264 | osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, r->head.endpoint, |
| 265 | sizeof(mgcp_ctx->conn_peer_remote.endpoint)); |
| 266 | } else if (strstr(mgcp_ctx->conn_peer_local.endpoint, "*") == NULL) { |
| 267 | /* If we do not get an endpoint identifier back and the |
| 268 | * identifier we used to create the connection is not a |
| 269 | * wildcarded one, we take the local endpoint identifier |
| 270 | * instead */ |
| 271 | osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, mgcp_ctx->conn_peer_local.endpoint, |
| 272 | sizeof(mgcp_ctx->conn_peer_local.endpoint)); |
| 273 | } else { |
| 274 | LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: CRCX yielded not suitable endpoint identifier\n"); |
| 275 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | mgcp_ctx->conn_peer_remote.call_id = mgcp_ctx->conn_peer_local.call_id; |
| 280 | |
| 281 | osmo_fsm_inst_dispatch(fi, EV_CRCX_RESP, mgcp_ctx); |
| 282 | } |
| 283 | |
| 284 | static void fsm_crcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 285 | { |
| 286 | struct mgcp_ctx *mgcp_ctx = data; |
| 287 | OSMO_ASSERT(mgcp_ctx); |
| 288 | |
| 289 | switch (event) { |
| 290 | case EV_CRCX_RESP: |
| 291 | osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0); |
| 292 | if (mgcp_ctx->terminate) { |
| 293 | /* Trigger immediate DLCX if DLCX was requested while the FSM was |
| 294 | * busy with the previous operation */ |
| 295 | LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: FSM was busy while DLCX was requested, executing now...\n"); |
| 296 | osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx); |
| 297 | } else |
| 298 | osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote); |
| 299 | break; |
| 300 | default: |
| 301 | OSMO_ASSERT(false); |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv); |
| 307 | static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv); |
| 308 | |
| 309 | static void fsm_ready_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 310 | { |
| 311 | struct mgcp_ctx *mgcp_ctx = data; |
| 312 | struct msgb *msg; |
| 313 | struct mgcp_client *mgcp; |
| 314 | uint32_t new_state; |
| 315 | int rc; |
| 316 | |
| 317 | OSMO_ASSERT(mgcp_ctx); |
| 318 | mgcp = mgcp_ctx->mgcp; |
| 319 | OSMO_ASSERT(mgcp); |
| 320 | |
| 321 | switch (event) { |
| 322 | case EV_MDCX: |
| 323 | msg = make_mdcx_msg(mgcp_ctx); |
| 324 | OSMO_ASSERT(msg); |
| 325 | rc = mgcp_client_tx(mgcp, msg, mgw_mdcx_resp_cb, fi); |
| 326 | new_state = ST_MDCX_RESP; |
| 327 | break; |
| 328 | case EV_DLCX: |
| 329 | msg = make_dlcx_msg(mgcp_ctx); |
| 330 | OSMO_ASSERT(msg); |
| 331 | rc = mgcp_client_tx(mgcp, msg, mgw_dlcx_resp_cb, fi); |
| 332 | new_state = ST_DLCX_RESP; |
| 333 | break; |
| 334 | default: |
| 335 | OSMO_ASSERT(false); |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg); |
| 340 | mgcp_ctx->mgw_trans_pending = true; |
| 341 | |
| 342 | if (rc < 0) { |
| 343 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | osmo_fsm_inst_state_chg(fi, new_state, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR); |
| 348 | } |
| 349 | |
| 350 | static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv) |
| 351 | { |
| 352 | struct osmo_fsm_inst *fi = priv; |
| 353 | struct mgcp_ctx *mgcp_ctx; |
| 354 | int rc; |
| 355 | |
| 356 | OSMO_ASSERT(fi); |
| 357 | mgcp_ctx = fi->priv; |
| 358 | OSMO_ASSERT(mgcp_ctx); |
| 359 | |
| 360 | mgcp_ctx->mgw_trans_pending = false; |
| 361 | |
| 362 | if (r->head.response_code != 200) { |
| 363 | LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: response yields error: %d %s\n", r->head.response_code, |
| 364 | r->head.comment); |
| 365 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | rc = mgcp_response_parse_params(r); |
| 370 | if (rc) { |
| 371 | LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: Cannot parse MDCX response\n"); |
| 372 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 373 | return; |
| 374 | } |
| 375 | LOGPFSML(fi, LOGL_DEBUG, "MGW/MDCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port); |
| 376 | |
| 377 | osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr)); |
| 378 | mgcp_ctx->conn_peer_remote.port = r->audio_port; |
| 379 | |
| 380 | osmo_fsm_inst_dispatch(fi, EV_MDCX_RESP, mgcp_ctx); |
| 381 | } |
| 382 | |
| 383 | static void fsm_mdcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 384 | { |
| 385 | struct mgcp_ctx *mgcp_ctx = data; |
| 386 | OSMO_ASSERT(mgcp_ctx); |
| 387 | |
| 388 | switch (event) { |
| 389 | case EV_MDCX_RESP: |
| 390 | osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0); |
| 391 | if (mgcp_ctx->terminate) { |
| 392 | /* Trigger immediate DLCX if DLCX was requested while the FSM was |
| 393 | * busy with the previous operation */ |
| 394 | LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: FSM was busy while DLCX was requested, executing now...\n"); |
| 395 | osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx); |
| 396 | } else |
| 397 | osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote); |
| 398 | break; |
| 399 | default: |
| 400 | OSMO_ASSERT(false); |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv) |
| 406 | { |
| 407 | struct osmo_fsm_inst *fi = priv; |
| 408 | struct mgcp_ctx *mgcp_ctx; |
| 409 | |
| 410 | OSMO_ASSERT(fi); |
| 411 | mgcp_ctx = fi->priv; |
| 412 | OSMO_ASSERT(mgcp_ctx); |
| 413 | |
| 414 | mgcp_ctx->mgw_trans_pending = false; |
| 415 | |
| 416 | if (r->head.response_code != 250) { |
| 417 | LOGPFSML(fi, LOGL_ERROR, |
| 418 | "MGW/DLCX: response yields error: %d %s\n", r->head.response_code, r->head.comment); |
| 419 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | osmo_fsm_inst_dispatch(fi, EV_DLCX_RESP, mgcp_ctx); |
| 424 | } |
| 425 | |
| 426 | static void fsm_dlcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 427 | { |
| 428 | struct mgcp_ctx *mgcp_ctx = data; |
| 429 | OSMO_ASSERT(mgcp_ctx); |
| 430 | |
| 431 | switch (event) { |
| 432 | case EV_DLCX_RESP: |
| 433 | /* Rub out the connection identifier, since the connection |
| 434 | * is no longer present and we will use the connection id |
| 435 | * to know in error cases if the connection is still present |
| 436 | * or not */ |
| 437 | memset(mgcp_ctx->conn_id, 0, sizeof(mgcp_ctx->conn_id)); |
| 438 | |
| 439 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); |
| 440 | break; |
| 441 | default: |
| 442 | OSMO_ASSERT(false); |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | static int fsm_timeout_cb(struct osmo_fsm_inst *fi) |
| 448 | { |
| 449 | struct mgcp_ctx *mgcp_ctx = fi->priv; |
| 450 | struct mgcp_client *mgcp; |
| 451 | |
| 452 | OSMO_ASSERT(mgcp_ctx); |
| 453 | mgcp = mgcp_ctx->mgcp; |
| 454 | OSMO_ASSERT(mgcp); |
| 455 | |
| 456 | if (fi->T == MGCP_MGW_TIMEOUT_TIMER_NR) { |
| 457 | /* Note: We were unable to communicate with the MGW, |
| 458 | * unfortunately there is no meaningful action we can take |
| 459 | * now other than giving up. */ |
| 460 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL); |
| 461 | } else { |
| 462 | /* Note: Ther must not be any unsolicited timers |
| 463 | * in this FSM. If so, we have serious problem. */ |
| 464 | OSMO_ASSERT(false); |
| 465 | } |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static void fsm_cleanup_cb(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause) |
| 471 | { |
| 472 | struct mgcp_ctx *mgcp_ctx = fi->priv; |
| 473 | struct mgcp_client *mgcp; |
| 474 | struct msgb *msg; |
| 475 | |
| 476 | OSMO_ASSERT(mgcp_ctx); |
| 477 | mgcp = mgcp_ctx->mgcp; |
| 478 | OSMO_ASSERT(mgcp); |
| 479 | |
| 480 | /* If there is still a transaction pending, cancel it now. */ |
| 481 | if (mgcp_ctx->mgw_trans_pending) |
| 482 | mgcp_client_cancel(mgcp, mgcp_ctx->mgw_pending_trans); |
| 483 | |
| 484 | /* Should the FSM be terminated while there are still open connections |
| 485 | * on the MGW, we send an unconditional DLCX to terminate the |
| 486 | * connection. This is not the normal case. The user should always use |
| 487 | * mgcp_conn_delete() to instruct the FSM to perform a graceful exit */ |
| 488 | if (strlen(mgcp_ctx->conn_id)) { |
| 489 | LOGPFSML(fi, LOGL_ERROR, |
Harald Welte | d4e6aa4 | 2018-06-02 18:07:10 +0200 | [diff] [blame] | 490 | "MGW/DLCX: abrupt FSM termination with connections still present, sending unconditional DLCX...\n"); |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 491 | msg = make_dlcx_msg(mgcp_ctx); |
| 492 | OSMO_ASSERT(msg); |
| 493 | mgcp_client_tx(mgcp, msg, NULL, NULL); |
| 494 | } |
| 495 | |
| 496 | talloc_free(mgcp_ctx); |
| 497 | } |
| 498 | |
| 499 | static struct osmo_fsm_state fsm_mgcp_client_states[] = { |
| 500 | |
| 501 | /* Initial CRCX state. This state is immediately entered and executed |
| 502 | * when the FSM is started. The rationale is that we first have to |
| 503 | * create a connectin before we can execute other operations on that |
| 504 | * connection. */ |
| 505 | [ST_CRCX] = { |
| 506 | .in_event_mask = S(EV_CRCX), |
| 507 | .out_state_mask = S(ST_CRCX_RESP), |
| 508 | .name = OSMO_STRINGIFY(ST_CRCX), |
| 509 | .action = fsm_crcx_cb, |
| 510 | }, |
| 511 | |
| 512 | /* Wait for the response to a CRCX operation, check and process the |
| 513 | * results, change to ST_READY afterwards. */ |
| 514 | [ST_CRCX_RESP] = { |
| 515 | .in_event_mask = S(EV_CRCX_RESP), |
| 516 | .out_state_mask = S(ST_READY), |
| 517 | .name = OSMO_STRINGIFY(ST_CRCX_RESP), |
| 518 | .action = fsm_crcx_resp_cb, |
| 519 | }, |
| 520 | |
| 521 | /* In this idle state we wait for further operations (e.g. MDCX) that |
| 522 | * can be executed by the user using the API. There is no timeout in |
| 523 | * this state. The connection lives on until the user decides to |
| 524 | * terminate it (DLCX). */ |
| 525 | [ST_READY] = { |
| 526 | .in_event_mask = S(EV_MDCX) | S(EV_DLCX), |
| 527 | .out_state_mask = S(ST_MDCX_RESP) | S(ST_DLCX_RESP), |
| 528 | .name = OSMO_STRINGIFY(ST_READY), |
| 529 | .action = fsm_ready_cb, |
| 530 | }, |
| 531 | |
| 532 | /* Wait for the response of a MDCX operation, check and process the |
| 533 | * results, change to ST_READY afterwards. */ |
| 534 | [ST_MDCX_RESP] = { |
| 535 | .in_event_mask = S(EV_MDCX_RESP), |
| 536 | .out_state_mask = S(ST_READY), |
| 537 | .name = OSMO_STRINGIFY(ST_MDCX_RESP), |
| 538 | .action = fsm_mdcx_resp_cb, |
| 539 | }, |
| 540 | |
| 541 | /* Wait for the response of a DLCX operation and terminate the FSM |
| 542 | * normally. */ |
| 543 | [ST_DLCX_RESP] = { |
| 544 | .in_event_mask = S(EV_DLCX_RESP), |
| 545 | .out_state_mask = 0, |
| 546 | .name = OSMO_STRINGIFY(ST_DLCX_RESP), |
| 547 | .action = fsm_dlcx_resp_cb, |
| 548 | }, |
| 549 | }; |
| 550 | |
| 551 | static struct osmo_fsm fsm_mgcp_client = { |
| 552 | .name = "MGCP_CONN", |
| 553 | .states = fsm_mgcp_client_states, |
| 554 | .num_states = ARRAY_SIZE(fsm_mgcp_client_states), |
| 555 | .timer_cb = fsm_timeout_cb, |
| 556 | .cleanup = fsm_cleanup_cb, |
Philipp Maier | d2e3a52 | 2018-02-26 14:29:01 +0100 | [diff] [blame] | 557 | .event_names = fsm_mgcp_client_evt_names, |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 558 | }; |
| 559 | |
| 560 | /*! allocate FSM, and create a new connection on the MGW. |
| 561 | * \param[in] mgcp MGCP client descriptor. |
Neels Hofmeyr | ed1cff5 | 2018-05-17 23:59:46 +0200 | [diff] [blame] | 562 | * \param[in] parent_fi Parent FSM instance. |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 563 | * \param[in] parent_term_evt Event to be sent to parent when terminating. |
| 564 | * \param[in] parent_evt Event to be sent to parent when operation is done. |
| 565 | * \param[in] conn_peer Connection parameters (ip, port...). |
| 566 | * \returns newly-allocated, initialized and registered FSM instance, NULL on error. */ |
| 567 | struct osmo_fsm_inst *mgcp_conn_create(struct mgcp_client *mgcp, struct osmo_fsm_inst *parent_fi, |
| 568 | uint32_t parent_term_evt, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer) |
| 569 | { |
| 570 | struct mgcp_ctx *mgcp_ctx; |
| 571 | static bool fsm_registered = false; |
| 572 | struct osmo_fsm_inst *fi; |
| 573 | struct in_addr ip_test; |
| 574 | |
| 575 | OSMO_ASSERT(parent_fi); |
| 576 | OSMO_ASSERT(mgcp); |
| 577 | OSMO_ASSERT(conn_peer); |
| 578 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 579 | /* Check if IP/Port information in conn info makes sense */ |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 580 | if (conn_peer->port && inet_aton(conn_peer->addr, &ip_test) == 0) |
| 581 | return NULL; |
| 582 | |
| 583 | /* Register the fsm description (if not already done) */ |
| 584 | if (fsm_registered == false) { |
| 585 | osmo_fsm_register(&fsm_mgcp_client); |
| 586 | fsm_registered = true; |
| 587 | } |
| 588 | |
| 589 | /* Allocate and configure a new fsm instance */ |
| 590 | fi = osmo_fsm_inst_alloc_child(&fsm_mgcp_client, parent_fi, parent_term_evt); |
| 591 | OSMO_ASSERT(fi); |
| 592 | mgcp_ctx = talloc_zero(fi, struct mgcp_ctx); |
| 593 | OSMO_ASSERT(mgcp_ctx); |
| 594 | mgcp_ctx->mgcp = mgcp; |
| 595 | mgcp_ctx->parent_evt = parent_evt; |
| 596 | |
| 597 | memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local)); |
| 598 | fi->priv = mgcp_ctx; |
| 599 | |
| 600 | /* start state machine */ |
| 601 | OSMO_ASSERT(fi->state == ST_CRCX); |
| 602 | osmo_fsm_inst_dispatch(fi, EV_CRCX, mgcp_ctx); |
| 603 | |
| 604 | return fi; |
| 605 | } |
| 606 | |
| 607 | /*! modify an existing connection on the MGW. |
| 608 | * \param[in] fi FSM instance. |
| 609 | * \param[in] parent_evt Event to be sent to parent when operation is done. |
| 610 | * \param[in] conn_peer New connection information (ip, port...). |
| 611 | * \returns 0 on success, -EINVAL on error. */ |
| 612 | int mgcp_conn_modify(struct osmo_fsm_inst *fi, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer) |
| 613 | { |
| 614 | OSMO_ASSERT(fi); |
| 615 | struct mgcp_ctx *mgcp_ctx = fi->priv; |
| 616 | struct in_addr ip_test; |
| 617 | |
| 618 | OSMO_ASSERT(mgcp_ctx); |
| 619 | OSMO_ASSERT(conn_peer); |
| 620 | |
| 621 | /* The user must not issue an MDCX before the CRCX has completed, |
| 622 | * if this happens, it means that the parent FSM has overhead the |
| 623 | * parent_evt (mandatory!) and executed the MDCX without even |
| 624 | * waiting for the results. Another reason could be that the |
| 625 | * parent FSM got messed up */ |
| 626 | OSMO_ASSERT(fi->state != ST_CRCX_RESP); |
| 627 | |
| 628 | /* If the user tries to issue an MDCX while an DLCX operation is |
| 629 | * pending, there must be a serious problem with the paren FSM. |
| 630 | * Eeither the parent_term_evt (mandatory!) has been overheard, |
| 631 | * or the parant FSM got messed so badly that it still assumes |
| 632 | * a live connection although it as killed it. */ |
| 633 | OSMO_ASSERT(fi->state != ST_DLCX_RESP); |
| 634 | |
| 635 | /* Check if IP/Port parameters make sense */ |
Neels Hofmeyr | 5928dc9 | 2018-06-15 04:33:37 +0200 | [diff] [blame] | 636 | if (conn_peer->port == 0) { |
| 637 | LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, port == 0\n"); |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 638 | return -EINVAL; |
Neels Hofmeyr | 5928dc9 | 2018-06-15 04:33:37 +0200 | [diff] [blame] | 639 | } |
| 640 | if (inet_aton(conn_peer->addr, &ip_test) == 0) { |
| 641 | LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, IP address == 0.0.0.0\n"); |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 642 | return -EINVAL; |
Neels Hofmeyr | 5928dc9 | 2018-06-15 04:33:37 +0200 | [diff] [blame] | 643 | } |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 644 | |
| 645 | /*! The user may supply an endpoint identifier in conn_peer. The |
| 646 | * identifier is then checked. This check is optional. Later steps do |
| 647 | * not depend on the endpoint identifier supplied here because it is |
| 648 | * already implicitly known from the CRCX phase. */ |
Neels Hofmeyr | 5928dc9 | 2018-06-15 04:33:37 +0200 | [diff] [blame] | 649 | if (strlen(conn_peer->endpoint) && strcmp(conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint)) { |
| 650 | LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, endpoint mismatches: requested %s, should be %s\n", |
| 651 | conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint); |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 652 | return -EINVAL; |
Neels Hofmeyr | 5928dc9 | 2018-06-15 04:33:37 +0200 | [diff] [blame] | 653 | } |
Philipp Maier | 8bda7a7 | 2018-01-17 14:32:23 +0100 | [diff] [blame] | 654 | |
| 655 | /*! Note: The call-id is implicitly known from the previous CRCX and |
| 656 | * will not be checked even when it is set in conn_peer. */ |
| 657 | |
| 658 | mgcp_ctx->parent_evt = parent_evt; |
| 659 | memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local)); |
| 660 | osmo_fsm_inst_dispatch(fi, EV_MDCX, mgcp_ctx); |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | /*! delete existing connection on the MGW, destroy FSM afterwards. |
| 665 | * \param[in] fi FSM instance. */ |
| 666 | void mgcp_conn_delete(struct osmo_fsm_inst *fi) |
| 667 | { |
| 668 | OSMO_ASSERT(fi); |
| 669 | struct mgcp_ctx *mgcp_ctx = fi->priv; |
| 670 | |
| 671 | OSMO_ASSERT(mgcp_ctx); |
| 672 | |
| 673 | /* Unlink FSM from parent */ |
| 674 | osmo_fsm_inst_unlink_parent(fi, NULL); |
| 675 | |
| 676 | /* An error situation where the parent FSM must be killed immediately |
| 677 | * may lead into a situation where the DLCX can not be executed right |
| 678 | * at that moment because the FSM is still busy with another operation. |
| 679 | * In those cases we postpone the DLCX so that the FSM and the |
| 680 | * connections on the MGW get cleaned up gracefully. */ |
| 681 | if (fi->state != ST_READY) { |
| 682 | LOGPFSML(fi, LOGL_ERROR, "MGW: operation still pending, DLCX will be postponed.\n"); |
| 683 | mgcp_ctx->terminate = true; |
| 684 | return; |
| 685 | } |
| 686 | osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx); |
| 687 | } |