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