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