blob: 7c4e08189c9ac8237f675c88d398bed800c50d97 [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,
117 .ptmap_len = info->ptmap_len
Philipp Maier8bda7a72018-01-17 14:32:23 +0100118 };
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200119 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 Hofmeyre6d8e912018-08-23 16:36:48 +0200122
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 Maier8bda7a72018-01-17 14:32:23 +0100127}
128
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200129static void add_audio(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100130{
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200131 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 Maiera5e0cf02018-08-29 14:33:01 +0200134 mgcp_msg->conn_mode = MGCP_CONN_RECV_SEND;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100135}
136
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100137static void set_conn_mode(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *peer)
138{
139 enum mgcp_connection_mode conn_mode = peer->conn_mode;
140 if (conn_mode != MGCP_CONN_NONE)
141 mgcp_msg->conn_mode = conn_mode;
142}
143
Philipp Maier8bda7a72018-01-17 14:32:23 +0100144static struct msgb *make_mdcx_msg(struct mgcp_ctx *mgcp_ctx)
145{
146 struct mgcp_msg mgcp_msg;
147
148 mgcp_msg = (struct mgcp_msg) {
149 .verb = MGCP_VERB_MDCX,
150 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID |
151 MGCP_MSG_PRESENCE_CONN_MODE | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT),
152 .call_id = mgcp_ctx->conn_peer_remote.call_id,
153 .conn_id = mgcp_ctx->conn_id,
154 .conn_mode = MGCP_CONN_RECV_SEND,
155 .audio_ip = mgcp_ctx->conn_peer_local.addr,
156 .audio_port = mgcp_ctx->conn_peer_local.port,
Philipp Maier704c4f02018-06-07 18:51:31 +0200157 .ptime = mgcp_ctx->conn_peer_local.ptime,
Philipp Maier544448a2018-07-26 11:37:44 +0200158 .codecs_len = mgcp_ctx->conn_peer_local.codecs_len,
159 .ptmap_len = mgcp_ctx->conn_peer_local.ptmap_len
Philipp Maier8bda7a72018-01-17 14:32:23 +0100160 };
161 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
Philipp Maier704c4f02018-06-07 18:51:31 +0200162 memcpy(mgcp_msg.codecs, mgcp_ctx->conn_peer_local.codecs, sizeof(mgcp_msg.codecs));
Philipp Maier544448a2018-07-26 11:37:44 +0200163 memcpy(mgcp_msg.ptmap, mgcp_ctx->conn_peer_local.ptmap, sizeof(mgcp_msg.ptmap));
Philipp Maier8bda7a72018-01-17 14:32:23 +0100164
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100165 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
166
Philipp Maier8bda7a72018-01-17 14:32:23 +0100167 /* Note: We take the endpoint and the call_id from the remote
168 * connection info, because we can be confident that the
169 * information there is valid. For the local info, we explicitly
170 * allow endpoint and call_id to be optional */
171 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
172}
173
174struct msgb *make_dlcx_msg(struct mgcp_ctx *mgcp_ctx)
175{
176 struct mgcp_msg mgcp_msg;
177
178 mgcp_msg = (struct mgcp_msg) {
179 .verb = MGCP_VERB_DLCX,
180 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID),
181 .call_id = mgcp_ctx->conn_peer_remote.call_id,
182 .conn_id = mgcp_ctx->conn_id,
183 };
184 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
185
186 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
187}
188
189static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv);
190
191static void fsm_crcx_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
192{
193 struct mgcp_ctx *mgcp_ctx = data;
194 struct mgcp_client *mgcp;
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200195 struct mgcp_msg mgcp_msg;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100196 struct msgb *msg;
197 int rc;
198
199 OSMO_ASSERT(mgcp_ctx);
200 mgcp = mgcp_ctx->mgcp;
201 OSMO_ASSERT(mgcp);
202
203 switch (event) {
204 case EV_CRCX:
205 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: creating connection on MGW endpoint:%s...\n",
206 mgcp_ctx->conn_peer_local.endpoint);
207
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200208 make_crcx_msg(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100209 if (mgcp_ctx->conn_peer_local.port)
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200210 add_audio(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100211 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
212
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200213 msg = mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100214 OSMO_ASSERT(msg);
215
216 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
217 mgcp_ctx->mgw_trans_pending = true;
218 rc = mgcp_client_tx(mgcp, msg, mgw_crcx_resp_cb, fi);
219 if (rc < 0) {
220 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
221 return;
222 }
223
224 osmo_fsm_inst_state_chg(fi, ST_CRCX_RESP, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
225 break;
226 default:
227 OSMO_ASSERT(false);
228 break;
229 }
230}
231
Neels Hofmeyr04da5e52018-06-12 21:51:23 +0200232/* Return the CI that the MGW allocated during CRCX response. This is purely informational for logging
233 * and identity tracking; the mgcp_conn_*() functions take care of using the right CI internally. */
234const char *mgcp_conn_get_ci(struct osmo_fsm_inst *fi)
235{
236 struct mgcp_ctx *mgcp_ctx = fi->priv;
237 return mgcp_ctx->conn_id;
238}
239
Philipp Maier8bda7a72018-01-17 14:32:23 +0100240static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv)
241{
242 struct osmo_fsm_inst *fi = priv;
243 struct mgcp_ctx *mgcp_ctx;
244 int rc;
245
246 OSMO_ASSERT(fi);
247 mgcp_ctx = fi->priv;
248 OSMO_ASSERT(mgcp_ctx);
249
250 mgcp_ctx->mgw_trans_pending = false;
251
252 if (r->head.response_code != 200) {
253 LOGPFSML(fi, LOGL_ERROR,
254 "MGW/CRCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
255 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
256 return;
257 }
258
259 osmo_strlcpy(mgcp_ctx->conn_id, r->head.conn_id, sizeof(mgcp_ctx->conn_id));
260 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with CI: %s\n", mgcp_ctx->conn_id);
261
262 rc = mgcp_response_parse_params(r);
263 if (rc) {
264 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: Cannot parse CRCX response\n");
265 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
266 return;
267 }
268 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
269
270 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
271 mgcp_ctx->conn_peer_remote.port = r->audio_port;
272
273 if (strlen(r->head.endpoint) > 0) {
274 /* If we get an endpoint identifier back from the MGW, take it */
275 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, r->head.endpoint,
276 sizeof(mgcp_ctx->conn_peer_remote.endpoint));
277 } else if (strstr(mgcp_ctx->conn_peer_local.endpoint, "*") == NULL) {
278 /* If we do not get an endpoint identifier back and the
279 * identifier we used to create the connection is not a
280 * wildcarded one, we take the local endpoint identifier
281 * instead */
282 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, mgcp_ctx->conn_peer_local.endpoint,
283 sizeof(mgcp_ctx->conn_peer_local.endpoint));
284 } else {
285 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: CRCX yielded not suitable endpoint identifier\n");
286 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
287 return;
288 }
289
290 mgcp_ctx->conn_peer_remote.call_id = mgcp_ctx->conn_peer_local.call_id;
291
292 osmo_fsm_inst_dispatch(fi, EV_CRCX_RESP, mgcp_ctx);
293}
294
295static void fsm_crcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
296{
297 struct mgcp_ctx *mgcp_ctx = data;
298 OSMO_ASSERT(mgcp_ctx);
299
300 switch (event) {
301 case EV_CRCX_RESP:
302 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
303 if (mgcp_ctx->terminate) {
304 /* Trigger immediate DLCX if DLCX was requested while the FSM was
305 * busy with the previous operation */
306 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: FSM was busy while DLCX was requested, executing now...\n");
307 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
308 } else
309 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
310 break;
311 default:
312 OSMO_ASSERT(false);
313 break;
314 }
315}
316
317static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv);
318static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv);
319
320static void fsm_ready_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
321{
322 struct mgcp_ctx *mgcp_ctx = data;
323 struct msgb *msg;
324 struct mgcp_client *mgcp;
325 uint32_t new_state;
326 int rc;
327
328 OSMO_ASSERT(mgcp_ctx);
329 mgcp = mgcp_ctx->mgcp;
330 OSMO_ASSERT(mgcp);
331
332 switch (event) {
333 case EV_MDCX:
334 msg = make_mdcx_msg(mgcp_ctx);
335 OSMO_ASSERT(msg);
336 rc = mgcp_client_tx(mgcp, msg, mgw_mdcx_resp_cb, fi);
337 new_state = ST_MDCX_RESP;
338 break;
339 case EV_DLCX:
340 msg = make_dlcx_msg(mgcp_ctx);
341 OSMO_ASSERT(msg);
342 rc = mgcp_client_tx(mgcp, msg, mgw_dlcx_resp_cb, fi);
343 new_state = ST_DLCX_RESP;
344 break;
345 default:
346 OSMO_ASSERT(false);
347 break;
348 }
349
350 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
351 mgcp_ctx->mgw_trans_pending = true;
352
353 if (rc < 0) {
354 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
355 return;
356 }
357
358 osmo_fsm_inst_state_chg(fi, new_state, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
359}
360
361static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv)
362{
363 struct osmo_fsm_inst *fi = priv;
364 struct mgcp_ctx *mgcp_ctx;
365 int rc;
366
367 OSMO_ASSERT(fi);
368 mgcp_ctx = fi->priv;
369 OSMO_ASSERT(mgcp_ctx);
370
371 mgcp_ctx->mgw_trans_pending = false;
372
373 if (r->head.response_code != 200) {
374 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: response yields error: %d %s\n", r->head.response_code,
375 r->head.comment);
376 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
377 return;
378 }
379
380 rc = mgcp_response_parse_params(r);
381 if (rc) {
382 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: Cannot parse MDCX response\n");
383 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
384 return;
385 }
386 LOGPFSML(fi, LOGL_DEBUG, "MGW/MDCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
387
388 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
389 mgcp_ctx->conn_peer_remote.port = r->audio_port;
390
391 osmo_fsm_inst_dispatch(fi, EV_MDCX_RESP, mgcp_ctx);
392}
393
394static void fsm_mdcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
395{
396 struct mgcp_ctx *mgcp_ctx = data;
397 OSMO_ASSERT(mgcp_ctx);
398
399 switch (event) {
400 case EV_MDCX_RESP:
401 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
402 if (mgcp_ctx->terminate) {
403 /* Trigger immediate DLCX if DLCX was requested while the FSM was
404 * busy with the previous operation */
405 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: FSM was busy while DLCX was requested, executing now...\n");
406 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
407 } else
408 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
409 break;
410 default:
411 OSMO_ASSERT(false);
412 break;
413 }
414}
415
416static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv)
417{
418 struct osmo_fsm_inst *fi = priv;
419 struct mgcp_ctx *mgcp_ctx;
420
421 OSMO_ASSERT(fi);
422 mgcp_ctx = fi->priv;
423 OSMO_ASSERT(mgcp_ctx);
424
425 mgcp_ctx->mgw_trans_pending = false;
426
427 if (r->head.response_code != 250) {
428 LOGPFSML(fi, LOGL_ERROR,
429 "MGW/DLCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
430 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
431 return;
432 }
433
434 osmo_fsm_inst_dispatch(fi, EV_DLCX_RESP, mgcp_ctx);
435}
436
437static void fsm_dlcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
438{
439 struct mgcp_ctx *mgcp_ctx = data;
440 OSMO_ASSERT(mgcp_ctx);
441
442 switch (event) {
443 case EV_DLCX_RESP:
444 /* Rub out the connection identifier, since the connection
445 * is no longer present and we will use the connection id
446 * to know in error cases if the connection is still present
447 * or not */
448 memset(mgcp_ctx->conn_id, 0, sizeof(mgcp_ctx->conn_id));
449
450 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
451 break;
452 default:
453 OSMO_ASSERT(false);
454 break;
455 }
456}
457
458static int fsm_timeout_cb(struct osmo_fsm_inst *fi)
459{
460 struct mgcp_ctx *mgcp_ctx = fi->priv;
461 struct mgcp_client *mgcp;
462
463 OSMO_ASSERT(mgcp_ctx);
464 mgcp = mgcp_ctx->mgcp;
465 OSMO_ASSERT(mgcp);
466
467 if (fi->T == MGCP_MGW_TIMEOUT_TIMER_NR) {
468 /* Note: We were unable to communicate with the MGW,
469 * unfortunately there is no meaningful action we can take
470 * now other than giving up. */
471 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
472 } else {
473 /* Note: Ther must not be any unsolicited timers
474 * in this FSM. If so, we have serious problem. */
475 OSMO_ASSERT(false);
476 }
477
478 return 0;
479}
480
481static void fsm_cleanup_cb(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
482{
483 struct mgcp_ctx *mgcp_ctx = fi->priv;
484 struct mgcp_client *mgcp;
485 struct msgb *msg;
486
487 OSMO_ASSERT(mgcp_ctx);
488 mgcp = mgcp_ctx->mgcp;
489 OSMO_ASSERT(mgcp);
490
491 /* If there is still a transaction pending, cancel it now. */
492 if (mgcp_ctx->mgw_trans_pending)
493 mgcp_client_cancel(mgcp, mgcp_ctx->mgw_pending_trans);
494
495 /* Should the FSM be terminated while there are still open connections
496 * on the MGW, we send an unconditional DLCX to terminate the
497 * connection. This is not the normal case. The user should always use
498 * mgcp_conn_delete() to instruct the FSM to perform a graceful exit */
499 if (strlen(mgcp_ctx->conn_id)) {
500 LOGPFSML(fi, LOGL_ERROR,
Harald Welted4e6aa42018-06-02 18:07:10 +0200501 "MGW/DLCX: abrupt FSM termination with connections still present, sending unconditional DLCX...\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100502 msg = make_dlcx_msg(mgcp_ctx);
503 OSMO_ASSERT(msg);
504 mgcp_client_tx(mgcp, msg, NULL, NULL);
505 }
506
507 talloc_free(mgcp_ctx);
508}
509
510static struct osmo_fsm_state fsm_mgcp_client_states[] = {
511
512 /* Initial CRCX state. This state is immediately entered and executed
513 * when the FSM is started. The rationale is that we first have to
514 * create a connectin before we can execute other operations on that
515 * connection. */
516 [ST_CRCX] = {
517 .in_event_mask = S(EV_CRCX),
518 .out_state_mask = S(ST_CRCX_RESP),
519 .name = OSMO_STRINGIFY(ST_CRCX),
520 .action = fsm_crcx_cb,
521 },
522
523 /* Wait for the response to a CRCX operation, check and process the
524 * results, change to ST_READY afterwards. */
525 [ST_CRCX_RESP] = {
526 .in_event_mask = S(EV_CRCX_RESP),
527 .out_state_mask = S(ST_READY),
528 .name = OSMO_STRINGIFY(ST_CRCX_RESP),
529 .action = fsm_crcx_resp_cb,
530 },
531
532 /* In this idle state we wait for further operations (e.g. MDCX) that
533 * can be executed by the user using the API. There is no timeout in
534 * this state. The connection lives on until the user decides to
535 * terminate it (DLCX). */
536 [ST_READY] = {
537 .in_event_mask = S(EV_MDCX) | S(EV_DLCX),
538 .out_state_mask = S(ST_MDCX_RESP) | S(ST_DLCX_RESP),
539 .name = OSMO_STRINGIFY(ST_READY),
540 .action = fsm_ready_cb,
541 },
542
543 /* Wait for the response of a MDCX operation, check and process the
544 * results, change to ST_READY afterwards. */
545 [ST_MDCX_RESP] = {
546 .in_event_mask = S(EV_MDCX_RESP),
547 .out_state_mask = S(ST_READY),
548 .name = OSMO_STRINGIFY(ST_MDCX_RESP),
549 .action = fsm_mdcx_resp_cb,
550 },
551
552 /* Wait for the response of a DLCX operation and terminate the FSM
553 * normally. */
554 [ST_DLCX_RESP] = {
555 .in_event_mask = S(EV_DLCX_RESP),
556 .out_state_mask = 0,
557 .name = OSMO_STRINGIFY(ST_DLCX_RESP),
558 .action = fsm_dlcx_resp_cb,
559 },
560};
561
562static struct osmo_fsm fsm_mgcp_client = {
563 .name = "MGCP_CONN",
564 .states = fsm_mgcp_client_states,
565 .num_states = ARRAY_SIZE(fsm_mgcp_client_states),
566 .timer_cb = fsm_timeout_cb,
567 .cleanup = fsm_cleanup_cb,
Philipp Maierd2e3a522018-02-26 14:29:01 +0100568 .event_names = fsm_mgcp_client_evt_names,
Philipp Maier8bda7a72018-01-17 14:32:23 +0100569};
570
571/*! allocate FSM, and create a new connection on the MGW.
572 * \param[in] mgcp MGCP client descriptor.
Neels Hofmeyred1cff52018-05-17 23:59:46 +0200573 * \param[in] parent_fi Parent FSM instance.
Philipp Maier8bda7a72018-01-17 14:32:23 +0100574 * \param[in] parent_term_evt Event to be sent to parent when terminating.
575 * \param[in] parent_evt Event to be sent to parent when operation is done.
576 * \param[in] conn_peer Connection parameters (ip, port...).
577 * \returns newly-allocated, initialized and registered FSM instance, NULL on error. */
578struct osmo_fsm_inst *mgcp_conn_create(struct mgcp_client *mgcp, struct osmo_fsm_inst *parent_fi,
579 uint32_t parent_term_evt, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
580{
581 struct mgcp_ctx *mgcp_ctx;
582 static bool fsm_registered = false;
583 struct osmo_fsm_inst *fi;
584 struct in_addr ip_test;
585
586 OSMO_ASSERT(parent_fi);
587 OSMO_ASSERT(mgcp);
588 OSMO_ASSERT(conn_peer);
589
Philipp Maier704c4f02018-06-07 18:51:31 +0200590 /* Check if IP/Port information in conn info makes sense */
Philipp Maier8bda7a72018-01-17 14:32:23 +0100591 if (conn_peer->port && inet_aton(conn_peer->addr, &ip_test) == 0)
592 return NULL;
593
594 /* Register the fsm description (if not already done) */
595 if (fsm_registered == false) {
596 osmo_fsm_register(&fsm_mgcp_client);
597 fsm_registered = true;
598 }
599
600 /* Allocate and configure a new fsm instance */
601 fi = osmo_fsm_inst_alloc_child(&fsm_mgcp_client, parent_fi, parent_term_evt);
602 OSMO_ASSERT(fi);
603 mgcp_ctx = talloc_zero(fi, struct mgcp_ctx);
604 OSMO_ASSERT(mgcp_ctx);
605 mgcp_ctx->mgcp = mgcp;
606 mgcp_ctx->parent_evt = parent_evt;
607
608 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
609 fi->priv = mgcp_ctx;
610
611 /* start state machine */
612 OSMO_ASSERT(fi->state == ST_CRCX);
613 osmo_fsm_inst_dispatch(fi, EV_CRCX, mgcp_ctx);
614
615 return fi;
616}
617
618/*! modify an existing connection on the MGW.
619 * \param[in] fi FSM instance.
620 * \param[in] parent_evt Event to be sent to parent when operation is done.
621 * \param[in] conn_peer New connection information (ip, port...).
622 * \returns 0 on success, -EINVAL on error. */
623int mgcp_conn_modify(struct osmo_fsm_inst *fi, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
624{
625 OSMO_ASSERT(fi);
626 struct mgcp_ctx *mgcp_ctx = fi->priv;
627 struct in_addr ip_test;
628
629 OSMO_ASSERT(mgcp_ctx);
630 OSMO_ASSERT(conn_peer);
631
632 /* The user must not issue an MDCX before the CRCX has completed,
633 * if this happens, it means that the parent FSM has overhead the
634 * parent_evt (mandatory!) and executed the MDCX without even
635 * waiting for the results. Another reason could be that the
636 * parent FSM got messed up */
637 OSMO_ASSERT(fi->state != ST_CRCX_RESP);
638
639 /* If the user tries to issue an MDCX while an DLCX operation is
640 * pending, there must be a serious problem with the paren FSM.
641 * Eeither the parent_term_evt (mandatory!) has been overheard,
642 * or the parant FSM got messed so badly that it still assumes
643 * a live connection although it as killed it. */
644 OSMO_ASSERT(fi->state != ST_DLCX_RESP);
645
646 /* Check if IP/Port parameters make sense */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200647 if (conn_peer->port == 0) {
648 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, port == 0\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100649 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200650 }
651 if (inet_aton(conn_peer->addr, &ip_test) == 0) {
652 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, IP address == 0.0.0.0\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100653 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200654 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100655
656 /*! The user may supply an endpoint identifier in conn_peer. The
657 * identifier is then checked. This check is optional. Later steps do
658 * not depend on the endpoint identifier supplied here because it is
659 * already implicitly known from the CRCX phase. */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200660 if (strlen(conn_peer->endpoint) && strcmp(conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint)) {
661 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, endpoint mismatches: requested %s, should be %s\n",
662 conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100663 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200664 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100665
666 /*! Note: The call-id is implicitly known from the previous CRCX and
667 * will not be checked even when it is set in conn_peer. */
668
669 mgcp_ctx->parent_evt = parent_evt;
670 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
671 osmo_fsm_inst_dispatch(fi, EV_MDCX, mgcp_ctx);
672 return 0;
673}
674
675/*! delete existing connection on the MGW, destroy FSM afterwards.
676 * \param[in] fi FSM instance. */
677void mgcp_conn_delete(struct osmo_fsm_inst *fi)
678{
679 OSMO_ASSERT(fi);
680 struct mgcp_ctx *mgcp_ctx = fi->priv;
681
682 OSMO_ASSERT(mgcp_ctx);
683
684 /* Unlink FSM from parent */
685 osmo_fsm_inst_unlink_parent(fi, NULL);
686
687 /* An error situation where the parent FSM must be killed immediately
688 * may lead into a situation where the DLCX can not be executed right
689 * at that moment because the FSM is still busy with another operation.
690 * In those cases we postpone the DLCX so that the FSM and the
691 * connections on the MGW get cleaned up gracefully. */
692 if (fi->state != ST_READY) {
693 LOGPFSML(fi, LOGL_ERROR, "MGW: operation still pending, DLCX will be postponed.\n");
694 mgcp_ctx->terminate = true;
695 return;
696 }
697 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
698}