blob: e83a119471a2c9329b51dc0f175ae072ba8bcfb2 [file] [log] [blame]
Philipp Maier8bda7a72018-01-17 14:32:23 +01001/* (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 */
32struct 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 Hofmeyr55e0dcf2018-09-03 21:36:56 +020044 char conn_id[MGCP_CONN_ID_MAXLEN];
Philipp Maier8bda7a72018-01-17 14:32:23 +010045
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 Maier01f03952018-02-26 14:33:25 +010080enum fsm_mgcp_client_states {
Philipp Maier8bda7a72018-01-17 14:32:23 +010081 ST_CRCX,
82 ST_CRCX_RESP,
83 ST_READY,
84 ST_MDCX_RESP,
85 ST_DLCX_RESP,
86};
87
Philipp Maier01f03952018-02-26 14:33:25 +010088enum fsm_mgcp_client_evt {
Philipp Maier8bda7a72018-01-17 14:32:23 +010089 EV_CRCX,
90 EV_CRCX_RESP,
91 EV_MDCX,
92 EV_MDCX_RESP,
93 EV_DLCX,
94 EV_DLCX_RESP,
95};
96
Philipp Maierd2e3a522018-02-26 14:29:01 +010097static 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 Hofmeyr475f8682018-08-23 16:38:59 +0200107static void make_crcx_msg(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100108{
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200109 *mgcp_msg = (struct mgcp_msg) {
Philipp Maier8bda7a72018-01-17 14:32:23 +0100110 .verb = MGCP_VERB_CRCX,
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200111 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID
112 | MGCP_MSG_PRESENCE_CONN_MODE),
113 .call_id = info->call_id,
Philipp Maier54eb0e12018-05-29 09:49:52 +0200114 .conn_mode = MGCP_CONN_RECV_ONLY,
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200115 .ptime = info->ptime,
116 .codecs_len = info->codecs_len,
Philipp Maier228e5912019-03-05 13:56:59 +0100117 .ptmap_len = info->ptmap_len,
118 .param_present = info->param_present
Philipp Maier8bda7a72018-01-17 14:32:23 +0100119 };
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200120 osmo_strlcpy(mgcp_msg->endpoint, info->endpoint, MGCP_ENDPOINT_MAXLEN);
121 memcpy(mgcp_msg->codecs, info->codecs, sizeof(mgcp_msg->codecs));
122 memcpy(mgcp_msg->ptmap, info->ptmap, sizeof(mgcp_msg->ptmap));
Philipp Maier228e5912019-03-05 13:56:59 +0100123 memcpy(&mgcp_msg->param, &info->param, sizeof(mgcp_msg->param));
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200124
125 if (info->x_osmo_ign) {
126 mgcp_msg->x_osmo_ign = info->x_osmo_ign;
127 mgcp_msg->presence |= MGCP_MSG_PRESENCE_X_OSMO_IGN;
128 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +0200129
130 if (info->x_osmo_osmux_use) {
131 mgcp_msg->x_osmo_osmux_cid = info->x_osmo_osmux_cid;
132 mgcp_msg->presence |= MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID;
133 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100134}
135
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200136static void add_audio(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100137{
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200138 mgcp_msg->presence |= MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT;
139 mgcp_msg->audio_ip = info->addr;
140 mgcp_msg->audio_port = info->port;
Philipp Maiera5e0cf02018-08-29 14:33:01 +0200141 mgcp_msg->conn_mode = MGCP_CONN_RECV_SEND;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100142}
143
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100144static void set_conn_mode(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *peer)
145{
146 enum mgcp_connection_mode conn_mode = peer->conn_mode;
147 if (conn_mode != MGCP_CONN_NONE)
148 mgcp_msg->conn_mode = conn_mode;
149}
150
Philipp Maier8bda7a72018-01-17 14:32:23 +0100151static struct msgb *make_mdcx_msg(struct mgcp_ctx *mgcp_ctx)
152{
153 struct mgcp_msg mgcp_msg;
154
155 mgcp_msg = (struct mgcp_msg) {
156 .verb = MGCP_VERB_MDCX,
157 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID |
158 MGCP_MSG_PRESENCE_CONN_MODE | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT),
159 .call_id = mgcp_ctx->conn_peer_remote.call_id,
160 .conn_id = mgcp_ctx->conn_id,
161 .conn_mode = MGCP_CONN_RECV_SEND,
162 .audio_ip = mgcp_ctx->conn_peer_local.addr,
163 .audio_port = mgcp_ctx->conn_peer_local.port,
Philipp Maier704c4f02018-06-07 18:51:31 +0200164 .ptime = mgcp_ctx->conn_peer_local.ptime,
Philipp Maier544448a2018-07-26 11:37:44 +0200165 .codecs_len = mgcp_ctx->conn_peer_local.codecs_len,
Philipp Maier228e5912019-03-05 13:56:59 +0100166 .ptmap_len = mgcp_ctx->conn_peer_local.ptmap_len,
167 .param_present = mgcp_ctx->conn_peer_local.param_present
Philipp Maier8bda7a72018-01-17 14:32:23 +0100168 };
169 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
Philipp Maier704c4f02018-06-07 18:51:31 +0200170 memcpy(mgcp_msg.codecs, mgcp_ctx->conn_peer_local.codecs, sizeof(mgcp_msg.codecs));
Philipp Maier544448a2018-07-26 11:37:44 +0200171 memcpy(mgcp_msg.ptmap, mgcp_ctx->conn_peer_local.ptmap, sizeof(mgcp_msg.ptmap));
Philipp Maier228e5912019-03-05 13:56:59 +0100172 memcpy(&mgcp_msg.param, &mgcp_ctx->conn_peer_local.param, sizeof(mgcp_ctx->conn_peer_local.param));
Philipp Maier8bda7a72018-01-17 14:32:23 +0100173
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100174 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
175
Pau Espin Pedrolca538fc2019-05-10 16:49:59 +0200176 if (mgcp_ctx->conn_peer_local.x_osmo_osmux_use) {
177 mgcp_msg.x_osmo_osmux_cid = mgcp_ctx->conn_peer_local.x_osmo_osmux_cid;
178 mgcp_msg.presence |= MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID;
179 }
180
Philipp Maier8bda7a72018-01-17 14:32:23 +0100181 /* Note: We take the endpoint and the call_id from the remote
182 * connection info, because we can be confident that the
183 * information there is valid. For the local info, we explicitly
184 * allow endpoint and call_id to be optional */
185 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
186}
187
188struct msgb *make_dlcx_msg(struct mgcp_ctx *mgcp_ctx)
189{
190 struct mgcp_msg mgcp_msg;
191
192 mgcp_msg = (struct mgcp_msg) {
193 .verb = MGCP_VERB_DLCX,
194 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID),
195 .call_id = mgcp_ctx->conn_peer_remote.call_id,
196 .conn_id = mgcp_ctx->conn_id,
197 };
198 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
199
200 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
201}
202
203static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv);
204
205static void fsm_crcx_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
206{
207 struct mgcp_ctx *mgcp_ctx = data;
208 struct mgcp_client *mgcp;
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200209 struct mgcp_msg mgcp_msg;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100210 struct msgb *msg;
211 int rc;
212
213 OSMO_ASSERT(mgcp_ctx);
214 mgcp = mgcp_ctx->mgcp;
215 OSMO_ASSERT(mgcp);
216
217 switch (event) {
218 case EV_CRCX:
219 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: creating connection on MGW endpoint:%s...\n",
220 mgcp_ctx->conn_peer_local.endpoint);
221
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200222 make_crcx_msg(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100223 if (mgcp_ctx->conn_peer_local.port)
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200224 add_audio(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100225 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
226
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200227 msg = mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100228 OSMO_ASSERT(msg);
229
230 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
231 mgcp_ctx->mgw_trans_pending = true;
232 rc = mgcp_client_tx(mgcp, msg, mgw_crcx_resp_cb, fi);
233 if (rc < 0) {
234 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
235 return;
236 }
237
238 osmo_fsm_inst_state_chg(fi, ST_CRCX_RESP, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
239 break;
240 default:
241 OSMO_ASSERT(false);
242 break;
243 }
244}
245
Neels Hofmeyr04da5e52018-06-12 21:51:23 +0200246/* Return the CI that the MGW allocated during CRCX response. This is purely informational for logging
247 * and identity tracking; the mgcp_conn_*() functions take care of using the right CI internally. */
248const char *mgcp_conn_get_ci(struct osmo_fsm_inst *fi)
249{
250 struct mgcp_ctx *mgcp_ctx = fi->priv;
251 return mgcp_ctx->conn_id;
252}
253
Philipp Maier8bda7a72018-01-17 14:32:23 +0100254static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv)
255{
256 struct osmo_fsm_inst *fi = priv;
257 struct mgcp_ctx *mgcp_ctx;
258 int rc;
259
260 OSMO_ASSERT(fi);
261 mgcp_ctx = fi->priv;
262 OSMO_ASSERT(mgcp_ctx);
263
264 mgcp_ctx->mgw_trans_pending = false;
265
266 if (r->head.response_code != 200) {
267 LOGPFSML(fi, LOGL_ERROR,
268 "MGW/CRCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
269 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
270 return;
271 }
272
273 osmo_strlcpy(mgcp_ctx->conn_id, r->head.conn_id, sizeof(mgcp_ctx->conn_id));
274 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with CI: %s\n", mgcp_ctx->conn_id);
275
276 rc = mgcp_response_parse_params(r);
277 if (rc) {
278 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: Cannot parse CRCX response\n");
279 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
280 return;
281 }
282 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200283 if (r->head.x_osmo_osmux_use) {
284 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
285 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
286 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
287 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100288
289 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
290 mgcp_ctx->conn_peer_remote.port = r->audio_port;
291
292 if (strlen(r->head.endpoint) > 0) {
293 /* If we get an endpoint identifier back from the MGW, take it */
294 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, r->head.endpoint,
295 sizeof(mgcp_ctx->conn_peer_remote.endpoint));
296 } else if (strstr(mgcp_ctx->conn_peer_local.endpoint, "*") == NULL) {
297 /* If we do not get an endpoint identifier back and the
298 * identifier we used to create the connection is not a
299 * wildcarded one, we take the local endpoint identifier
300 * instead */
301 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, mgcp_ctx->conn_peer_local.endpoint,
302 sizeof(mgcp_ctx->conn_peer_local.endpoint));
303 } else {
304 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: CRCX yielded not suitable endpoint identifier\n");
305 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
306 return;
307 }
308
309 mgcp_ctx->conn_peer_remote.call_id = mgcp_ctx->conn_peer_local.call_id;
310
311 osmo_fsm_inst_dispatch(fi, EV_CRCX_RESP, mgcp_ctx);
312}
313
314static void fsm_crcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
315{
316 struct mgcp_ctx *mgcp_ctx = data;
317 OSMO_ASSERT(mgcp_ctx);
318
319 switch (event) {
320 case EV_CRCX_RESP:
321 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
322 if (mgcp_ctx->terminate) {
323 /* Trigger immediate DLCX if DLCX was requested while the FSM was
324 * busy with the previous operation */
325 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: FSM was busy while DLCX was requested, executing now...\n");
326 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
327 } else
328 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
329 break;
330 default:
331 OSMO_ASSERT(false);
332 break;
333 }
334}
335
336static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv);
337static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv);
338
339static void fsm_ready_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
340{
341 struct mgcp_ctx *mgcp_ctx = data;
342 struct msgb *msg;
343 struct mgcp_client *mgcp;
344 uint32_t new_state;
345 int rc;
346
347 OSMO_ASSERT(mgcp_ctx);
348 mgcp = mgcp_ctx->mgcp;
349 OSMO_ASSERT(mgcp);
350
351 switch (event) {
352 case EV_MDCX:
353 msg = make_mdcx_msg(mgcp_ctx);
354 OSMO_ASSERT(msg);
355 rc = mgcp_client_tx(mgcp, msg, mgw_mdcx_resp_cb, fi);
356 new_state = ST_MDCX_RESP;
357 break;
358 case EV_DLCX:
359 msg = make_dlcx_msg(mgcp_ctx);
360 OSMO_ASSERT(msg);
361 rc = mgcp_client_tx(mgcp, msg, mgw_dlcx_resp_cb, fi);
362 new_state = ST_DLCX_RESP;
363 break;
364 default:
365 OSMO_ASSERT(false);
366 break;
367 }
368
369 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
370 mgcp_ctx->mgw_trans_pending = true;
371
372 if (rc < 0) {
373 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
374 return;
375 }
376
377 osmo_fsm_inst_state_chg(fi, new_state, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
378}
379
380static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv)
381{
382 struct osmo_fsm_inst *fi = priv;
383 struct mgcp_ctx *mgcp_ctx;
384 int rc;
385
386 OSMO_ASSERT(fi);
387 mgcp_ctx = fi->priv;
388 OSMO_ASSERT(mgcp_ctx);
389
390 mgcp_ctx->mgw_trans_pending = false;
391
392 if (r->head.response_code != 200) {
393 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: response yields error: %d %s\n", r->head.response_code,
394 r->head.comment);
395 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
396 return;
397 }
398
399 rc = mgcp_response_parse_params(r);
400 if (rc) {
401 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: Cannot parse MDCX response\n");
402 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
403 return;
404 }
405 LOGPFSML(fi, LOGL_DEBUG, "MGW/MDCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
406
Pau Espin Pedrolc63f15a2019-05-10 16:52:08 +0200407 if (r->head.x_osmo_osmux_use) {
408 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded using Osmux %u\n", r->head.x_osmo_osmux_cid);
409 mgcp_ctx->conn_peer_remote.x_osmo_osmux_use = true;
410 mgcp_ctx->conn_peer_remote.x_osmo_osmux_cid = r->head.x_osmo_osmux_cid;
411 }
412
Philipp Maier8bda7a72018-01-17 14:32:23 +0100413 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
414 mgcp_ctx->conn_peer_remote.port = r->audio_port;
415
416 osmo_fsm_inst_dispatch(fi, EV_MDCX_RESP, mgcp_ctx);
417}
418
419static void fsm_mdcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
420{
421 struct mgcp_ctx *mgcp_ctx = data;
422 OSMO_ASSERT(mgcp_ctx);
423
424 switch (event) {
425 case EV_MDCX_RESP:
426 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
427 if (mgcp_ctx->terminate) {
428 /* Trigger immediate DLCX if DLCX was requested while the FSM was
429 * busy with the previous operation */
430 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: FSM was busy while DLCX was requested, executing now...\n");
431 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
432 } else
433 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
434 break;
435 default:
436 OSMO_ASSERT(false);
437 break;
438 }
439}
440
441static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv)
442{
443 struct osmo_fsm_inst *fi = priv;
444 struct mgcp_ctx *mgcp_ctx;
445
446 OSMO_ASSERT(fi);
447 mgcp_ctx = fi->priv;
448 OSMO_ASSERT(mgcp_ctx);
449
450 mgcp_ctx->mgw_trans_pending = false;
451
452 if (r->head.response_code != 250) {
453 LOGPFSML(fi, LOGL_ERROR,
454 "MGW/DLCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
455 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
456 return;
457 }
458
459 osmo_fsm_inst_dispatch(fi, EV_DLCX_RESP, mgcp_ctx);
460}
461
462static void fsm_dlcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
463{
464 struct mgcp_ctx *mgcp_ctx = data;
465 OSMO_ASSERT(mgcp_ctx);
466
467 switch (event) {
468 case EV_DLCX_RESP:
469 /* Rub out the connection identifier, since the connection
470 * is no longer present and we will use the connection id
471 * to know in error cases if the connection is still present
472 * or not */
473 memset(mgcp_ctx->conn_id, 0, sizeof(mgcp_ctx->conn_id));
474
475 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
476 break;
477 default:
478 OSMO_ASSERT(false);
479 break;
480 }
481}
482
483static int fsm_timeout_cb(struct osmo_fsm_inst *fi)
484{
485 struct mgcp_ctx *mgcp_ctx = fi->priv;
486 struct mgcp_client *mgcp;
487
488 OSMO_ASSERT(mgcp_ctx);
489 mgcp = mgcp_ctx->mgcp;
490 OSMO_ASSERT(mgcp);
491
492 if (fi->T == MGCP_MGW_TIMEOUT_TIMER_NR) {
493 /* Note: We were unable to communicate with the MGW,
494 * unfortunately there is no meaningful action we can take
495 * now other than giving up. */
496 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
497 } else {
498 /* Note: Ther must not be any unsolicited timers
499 * in this FSM. If so, we have serious problem. */
500 OSMO_ASSERT(false);
501 }
502
503 return 0;
504}
505
506static void fsm_cleanup_cb(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
507{
508 struct mgcp_ctx *mgcp_ctx = fi->priv;
509 struct mgcp_client *mgcp;
510 struct msgb *msg;
511
512 OSMO_ASSERT(mgcp_ctx);
513 mgcp = mgcp_ctx->mgcp;
514 OSMO_ASSERT(mgcp);
515
516 /* If there is still a transaction pending, cancel it now. */
517 if (mgcp_ctx->mgw_trans_pending)
518 mgcp_client_cancel(mgcp, mgcp_ctx->mgw_pending_trans);
519
520 /* Should the FSM be terminated while there are still open connections
521 * on the MGW, we send an unconditional DLCX to terminate the
522 * connection. This is not the normal case. The user should always use
523 * mgcp_conn_delete() to instruct the FSM to perform a graceful exit */
524 if (strlen(mgcp_ctx->conn_id)) {
525 LOGPFSML(fi, LOGL_ERROR,
Harald Welted4e6aa42018-06-02 18:07:10 +0200526 "MGW/DLCX: abrupt FSM termination with connections still present, sending unconditional DLCX...\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100527 msg = make_dlcx_msg(mgcp_ctx);
528 OSMO_ASSERT(msg);
529 mgcp_client_tx(mgcp, msg, NULL, NULL);
530 }
531
532 talloc_free(mgcp_ctx);
533}
534
535static struct osmo_fsm_state fsm_mgcp_client_states[] = {
536
537 /* Initial CRCX state. This state is immediately entered and executed
538 * when the FSM is started. The rationale is that we first have to
539 * create a connectin before we can execute other operations on that
540 * connection. */
541 [ST_CRCX] = {
542 .in_event_mask = S(EV_CRCX),
543 .out_state_mask = S(ST_CRCX_RESP),
544 .name = OSMO_STRINGIFY(ST_CRCX),
545 .action = fsm_crcx_cb,
546 },
547
548 /* Wait for the response to a CRCX operation, check and process the
549 * results, change to ST_READY afterwards. */
550 [ST_CRCX_RESP] = {
551 .in_event_mask = S(EV_CRCX_RESP),
552 .out_state_mask = S(ST_READY),
553 .name = OSMO_STRINGIFY(ST_CRCX_RESP),
554 .action = fsm_crcx_resp_cb,
555 },
556
557 /* In this idle state we wait for further operations (e.g. MDCX) that
558 * can be executed by the user using the API. There is no timeout in
559 * this state. The connection lives on until the user decides to
560 * terminate it (DLCX). */
561 [ST_READY] = {
562 .in_event_mask = S(EV_MDCX) | S(EV_DLCX),
563 .out_state_mask = S(ST_MDCX_RESP) | S(ST_DLCX_RESP),
564 .name = OSMO_STRINGIFY(ST_READY),
565 .action = fsm_ready_cb,
566 },
567
568 /* Wait for the response of a MDCX operation, check and process the
569 * results, change to ST_READY afterwards. */
570 [ST_MDCX_RESP] = {
571 .in_event_mask = S(EV_MDCX_RESP),
572 .out_state_mask = S(ST_READY),
573 .name = OSMO_STRINGIFY(ST_MDCX_RESP),
574 .action = fsm_mdcx_resp_cb,
575 },
576
577 /* Wait for the response of a DLCX operation and terminate the FSM
578 * normally. */
579 [ST_DLCX_RESP] = {
580 .in_event_mask = S(EV_DLCX_RESP),
581 .out_state_mask = 0,
582 .name = OSMO_STRINGIFY(ST_DLCX_RESP),
583 .action = fsm_dlcx_resp_cb,
584 },
585};
586
587static struct osmo_fsm fsm_mgcp_client = {
588 .name = "MGCP_CONN",
589 .states = fsm_mgcp_client_states,
590 .num_states = ARRAY_SIZE(fsm_mgcp_client_states),
591 .timer_cb = fsm_timeout_cb,
592 .cleanup = fsm_cleanup_cb,
Philipp Maierd2e3a522018-02-26 14:29:01 +0100593 .event_names = fsm_mgcp_client_evt_names,
Philipp Maier8bda7a72018-01-17 14:32:23 +0100594};
595
596/*! allocate FSM, and create a new connection on the MGW.
597 * \param[in] mgcp MGCP client descriptor.
Neels Hofmeyred1cff52018-05-17 23:59:46 +0200598 * \param[in] parent_fi Parent FSM instance.
Philipp Maier8bda7a72018-01-17 14:32:23 +0100599 * \param[in] parent_term_evt Event to be sent to parent when terminating.
600 * \param[in] parent_evt Event to be sent to parent when operation is done.
601 * \param[in] conn_peer Connection parameters (ip, port...).
602 * \returns newly-allocated, initialized and registered FSM instance, NULL on error. */
603struct osmo_fsm_inst *mgcp_conn_create(struct mgcp_client *mgcp, struct osmo_fsm_inst *parent_fi,
604 uint32_t parent_term_evt, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
605{
606 struct mgcp_ctx *mgcp_ctx;
607 static bool fsm_registered = false;
608 struct osmo_fsm_inst *fi;
609 struct in_addr ip_test;
610
611 OSMO_ASSERT(parent_fi);
612 OSMO_ASSERT(mgcp);
613 OSMO_ASSERT(conn_peer);
614
Philipp Maier704c4f02018-06-07 18:51:31 +0200615 /* Check if IP/Port information in conn info makes sense */
Philipp Maier8bda7a72018-01-17 14:32:23 +0100616 if (conn_peer->port && inet_aton(conn_peer->addr, &ip_test) == 0)
617 return NULL;
618
619 /* Register the fsm description (if not already done) */
620 if (fsm_registered == false) {
621 osmo_fsm_register(&fsm_mgcp_client);
622 fsm_registered = true;
623 }
624
625 /* Allocate and configure a new fsm instance */
626 fi = osmo_fsm_inst_alloc_child(&fsm_mgcp_client, parent_fi, parent_term_evt);
627 OSMO_ASSERT(fi);
628 mgcp_ctx = talloc_zero(fi, struct mgcp_ctx);
629 OSMO_ASSERT(mgcp_ctx);
630 mgcp_ctx->mgcp = mgcp;
631 mgcp_ctx->parent_evt = parent_evt;
632
633 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
634 fi->priv = mgcp_ctx;
635
636 /* start state machine */
637 OSMO_ASSERT(fi->state == ST_CRCX);
638 osmo_fsm_inst_dispatch(fi, EV_CRCX, mgcp_ctx);
639
640 return fi;
641}
642
643/*! modify an existing connection on the MGW.
644 * \param[in] fi FSM instance.
645 * \param[in] parent_evt Event to be sent to parent when operation is done.
646 * \param[in] conn_peer New connection information (ip, port...).
647 * \returns 0 on success, -EINVAL on error. */
648int mgcp_conn_modify(struct osmo_fsm_inst *fi, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
649{
650 OSMO_ASSERT(fi);
651 struct mgcp_ctx *mgcp_ctx = fi->priv;
652 struct in_addr ip_test;
653
654 OSMO_ASSERT(mgcp_ctx);
655 OSMO_ASSERT(conn_peer);
656
657 /* The user must not issue an MDCX before the CRCX has completed,
658 * if this happens, it means that the parent FSM has overhead the
659 * parent_evt (mandatory!) and executed the MDCX without even
660 * waiting for the results. Another reason could be that the
661 * parent FSM got messed up */
662 OSMO_ASSERT(fi->state != ST_CRCX_RESP);
663
664 /* If the user tries to issue an MDCX while an DLCX operation is
665 * pending, there must be a serious problem with the paren FSM.
666 * Eeither the parent_term_evt (mandatory!) has been overheard,
667 * or the parant FSM got messed so badly that it still assumes
668 * a live connection although it as killed it. */
669 OSMO_ASSERT(fi->state != ST_DLCX_RESP);
670
671 /* Check if IP/Port parameters make sense */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200672 if (conn_peer->port == 0) {
673 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, port == 0\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100674 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200675 }
676 if (inet_aton(conn_peer->addr, &ip_test) == 0) {
677 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, IP address == 0.0.0.0\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100678 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200679 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100680
681 /*! The user may supply an endpoint identifier in conn_peer. The
682 * identifier is then checked. This check is optional. Later steps do
683 * not depend on the endpoint identifier supplied here because it is
684 * already implicitly known from the CRCX phase. */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200685 if (strlen(conn_peer->endpoint) && strcmp(conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint)) {
686 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, endpoint mismatches: requested %s, should be %s\n",
687 conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100688 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200689 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100690
691 /*! Note: The call-id is implicitly known from the previous CRCX and
692 * will not be checked even when it is set in conn_peer. */
693
694 mgcp_ctx->parent_evt = parent_evt;
695 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
696 osmo_fsm_inst_dispatch(fi, EV_MDCX, mgcp_ctx);
697 return 0;
698}
699
700/*! delete existing connection on the MGW, destroy FSM afterwards.
701 * \param[in] fi FSM instance. */
702void mgcp_conn_delete(struct osmo_fsm_inst *fi)
703{
704 OSMO_ASSERT(fi);
705 struct mgcp_ctx *mgcp_ctx = fi->priv;
706
707 OSMO_ASSERT(mgcp_ctx);
708
709 /* Unlink FSM from parent */
710 osmo_fsm_inst_unlink_parent(fi, NULL);
711
712 /* An error situation where the parent FSM must be killed immediately
713 * may lead into a situation where the DLCX can not be executed right
714 * at that moment because the FSM is still busy with another operation.
715 * In those cases we postpone the DLCX so that the FSM and the
716 * connections on the MGW get cleaned up gracefully. */
717 if (fi->state != ST_READY) {
718 LOGPFSML(fi, LOGL_ERROR, "MGW: operation still pending, DLCX will be postponed.\n");
719 mgcp_ctx->terminate = true;
720 return;
721 }
722 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
723}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100724
725const char *osmo_mgcpc_conn_peer_name(const struct mgcp_conn_peer *info)
726{
727 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
728 * this buffer is too small. */
729 static char buf[1024];
730
731 if (!info)
732 return "NULL";
733
734 if (info->endpoint[0]
735 && info->addr[0])
736 snprintf(buf, sizeof(buf), "%s:%s:%u",
737 info->endpoint, info->addr, info->port);
738 else if (info->endpoint[0])
739 snprintf(buf, sizeof(buf), "%s", info->endpoint);
740 else if (info->addr[0])
741 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
742 else
743 return "empty";
744 return buf;
745}