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