blob: 75d583bbe7915603a21a1fbab27fa625cde5c4cf [file] [log] [blame]
Philipp Maier8bda7a72018-01-17 14:32:23 +01001/* (C) 2018 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
2 * All Rights Reserved
3 *
4 * Author: Philipp Maier
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <osmocom/mgcp_client/mgcp_client.h>
22#include <osmocom/mgcp_client/mgcp_client_fsm.h>
23#include <osmocom/core/utils.h>
24#include <osmocom/core/fsm.h>
25#include <osmocom/core/byteswap.h>
26#include <arpa/inet.h>
27#include <osmocom/core/logging.h>
28
29/* Context information, this is attached to the priv pointer of the FSM and
30 * is also handed back when dispatcheing events to the parent FSM. This is
31 * purly intened and not meant to be accessible for the API user */
32struct mgcp_ctx {
33 /* MGCP client instance that is used to interact with the MGW */
34 struct mgcp_client *mgcp;
35
36 /* The ID of the last pending transaction. This is used internally
37 * to cancel the transaction in case of an error */
38 mgcp_trans_id_t mgw_pending_trans;
39
40 /* Flag to mark that there is a pending transaction */
41 bool mgw_trans_pending;
42
43 /* Connection ID which has been assigned by he MGW */
Neels Hofmeyr55e0dcf2018-09-03 21:36:56 +020044 char conn_id[MGCP_CONN_ID_MAXLEN];
Philipp Maier8bda7a72018-01-17 14:32:23 +010045
46 /* Local RTP connection info, the MGW will send outgoing traffic to the
47 * ip/port specified here. The Address does not have to be choosen right
48 * on the creation of a connection. It can always be modified later by
49 * the user. */
50 struct mgcp_conn_peer conn_peer_local;
51
52 /* Remote RTP connection info, the ip/port specified here is the address
53 * where the MGW expects the RTP data to be sent. This address is
54 * defined by soly by the MGW and can not be influenced by the user. */
55 struct mgcp_conn_peer conn_peer_remote;
56
57 /* The terminate flag is a way to handle cornercase sitations that
58 * might occur when the user runs into an error situation and sends
59 * a DLCX command while the FSM is waiting for a response. In this
60 * case the DLCX command is not executed immediately. Instead the
61 * terminate flag is set. When the response to from the previous
62 * operation is received, we know that there is a DLCX event is
63 * pending. The FSM then generates the EV_DLCX by itsself before
64 * it enters ST_READY to cause the immediate execution of the
65 * DLCX procedure. (If normal operations are executed too fast,
66 * the API functions will return an error. In general, the user
67 * should synchronize using the callback events) */
68 bool terminate;
69
70 /* Event that is sent when the current operation is completed (except
71 * for DLCX, there the specified parent_term_evt is sent instead) */
72 uint32_t parent_evt;
73};
74
75#define S(x) (1 << (x))
76
77#define MGCP_MGW_TIMEOUT 4 /* in seconds */
78#define MGCP_MGW_TIMEOUT_TIMER_NR 1
79
Philipp Maier01f03952018-02-26 14:33:25 +010080enum fsm_mgcp_client_states {
Philipp Maier8bda7a72018-01-17 14:32:23 +010081 ST_CRCX,
82 ST_CRCX_RESP,
83 ST_READY,
84 ST_MDCX_RESP,
85 ST_DLCX_RESP,
86};
87
Philipp Maier01f03952018-02-26 14:33:25 +010088enum fsm_mgcp_client_evt {
Philipp Maier8bda7a72018-01-17 14:32:23 +010089 EV_CRCX,
90 EV_CRCX_RESP,
91 EV_MDCX,
92 EV_MDCX_RESP,
93 EV_DLCX,
94 EV_DLCX_RESP,
95};
96
Philipp Maierd2e3a522018-02-26 14:29:01 +010097static const struct value_string fsm_mgcp_client_evt_names[] = {
98 OSMO_VALUE_STRING(EV_CRCX),
99 OSMO_VALUE_STRING(EV_CRCX_RESP),
100 OSMO_VALUE_STRING(EV_MDCX),
101 OSMO_VALUE_STRING(EV_MDCX_RESP),
102 OSMO_VALUE_STRING(EV_DLCX),
103 OSMO_VALUE_STRING(EV_DLCX_RESP),
104 {0, NULL}
105};
106
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200107static void make_crcx_msg(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100108{
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200109 *mgcp_msg = (struct mgcp_msg) {
Philipp Maier8bda7a72018-01-17 14:32:23 +0100110 .verb = MGCP_VERB_CRCX,
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200111 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID
112 | MGCP_MSG_PRESENCE_CONN_MODE),
113 .call_id = info->call_id,
Philipp Maier54eb0e12018-05-29 09:49:52 +0200114 .conn_mode = MGCP_CONN_RECV_ONLY,
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200115 .ptime = info->ptime,
116 .codecs_len = info->codecs_len,
Philipp Maier228e5912019-03-05 13:56:59 +0100117 .ptmap_len = info->ptmap_len,
118 .param_present = info->param_present
Philipp Maier8bda7a72018-01-17 14:32:23 +0100119 };
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200120 osmo_strlcpy(mgcp_msg->endpoint, info->endpoint, MGCP_ENDPOINT_MAXLEN);
121 memcpy(mgcp_msg->codecs, info->codecs, sizeof(mgcp_msg->codecs));
122 memcpy(mgcp_msg->ptmap, info->ptmap, sizeof(mgcp_msg->ptmap));
Philipp Maier228e5912019-03-05 13:56:59 +0100123 memcpy(&mgcp_msg->param, &info->param, sizeof(mgcp_msg->param));
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200124
125 if (info->x_osmo_ign) {
126 mgcp_msg->x_osmo_ign = info->x_osmo_ign;
127 mgcp_msg->presence |= MGCP_MSG_PRESENCE_X_OSMO_IGN;
128 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100129}
130
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200131static void add_audio(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *info)
Philipp Maier8bda7a72018-01-17 14:32:23 +0100132{
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200133 mgcp_msg->presence |= MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT;
134 mgcp_msg->audio_ip = info->addr;
135 mgcp_msg->audio_port = info->port;
Philipp Maiera5e0cf02018-08-29 14:33:01 +0200136 mgcp_msg->conn_mode = MGCP_CONN_RECV_SEND;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100137}
138
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100139static void set_conn_mode(struct mgcp_msg *mgcp_msg, struct mgcp_conn_peer *peer)
140{
141 enum mgcp_connection_mode conn_mode = peer->conn_mode;
142 if (conn_mode != MGCP_CONN_NONE)
143 mgcp_msg->conn_mode = conn_mode;
144}
145
Philipp Maier8bda7a72018-01-17 14:32:23 +0100146static struct msgb *make_mdcx_msg(struct mgcp_ctx *mgcp_ctx)
147{
148 struct mgcp_msg mgcp_msg;
149
150 mgcp_msg = (struct mgcp_msg) {
151 .verb = MGCP_VERB_MDCX,
152 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID |
153 MGCP_MSG_PRESENCE_CONN_MODE | MGCP_MSG_PRESENCE_AUDIO_IP | MGCP_MSG_PRESENCE_AUDIO_PORT),
154 .call_id = mgcp_ctx->conn_peer_remote.call_id,
155 .conn_id = mgcp_ctx->conn_id,
156 .conn_mode = MGCP_CONN_RECV_SEND,
157 .audio_ip = mgcp_ctx->conn_peer_local.addr,
158 .audio_port = mgcp_ctx->conn_peer_local.port,
Philipp Maier704c4f02018-06-07 18:51:31 +0200159 .ptime = mgcp_ctx->conn_peer_local.ptime,
Philipp Maier544448a2018-07-26 11:37:44 +0200160 .codecs_len = mgcp_ctx->conn_peer_local.codecs_len,
Philipp Maier228e5912019-03-05 13:56:59 +0100161 .ptmap_len = mgcp_ctx->conn_peer_local.ptmap_len,
162 .param_present = mgcp_ctx->conn_peer_local.param_present
Philipp Maier8bda7a72018-01-17 14:32:23 +0100163 };
164 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
Philipp Maier704c4f02018-06-07 18:51:31 +0200165 memcpy(mgcp_msg.codecs, mgcp_ctx->conn_peer_local.codecs, sizeof(mgcp_msg.codecs));
Philipp Maier544448a2018-07-26 11:37:44 +0200166 memcpy(mgcp_msg.ptmap, mgcp_ctx->conn_peer_local.ptmap, sizeof(mgcp_msg.ptmap));
Philipp Maier228e5912019-03-05 13:56:59 +0100167 memcpy(&mgcp_msg.param, &mgcp_ctx->conn_peer_local.param, sizeof(mgcp_ctx->conn_peer_local.param));
Philipp Maier8bda7a72018-01-17 14:32:23 +0100168
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100169 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
170
Philipp Maier8bda7a72018-01-17 14:32:23 +0100171 /* Note: We take the endpoint and the call_id from the remote
172 * connection info, because we can be confident that the
173 * information there is valid. For the local info, we explicitly
174 * allow endpoint and call_id to be optional */
175 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
176}
177
178struct msgb *make_dlcx_msg(struct mgcp_ctx *mgcp_ctx)
179{
180 struct mgcp_msg mgcp_msg;
181
182 mgcp_msg = (struct mgcp_msg) {
183 .verb = MGCP_VERB_DLCX,
184 .presence = (MGCP_MSG_PRESENCE_ENDPOINT | MGCP_MSG_PRESENCE_CALL_ID | MGCP_MSG_PRESENCE_CONN_ID),
185 .call_id = mgcp_ctx->conn_peer_remote.call_id,
186 .conn_id = mgcp_ctx->conn_id,
187 };
188 osmo_strlcpy(mgcp_msg.endpoint, mgcp_ctx->conn_peer_remote.endpoint, MGCP_ENDPOINT_MAXLEN);
189
190 return mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
191}
192
193static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv);
194
195static void fsm_crcx_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
196{
197 struct mgcp_ctx *mgcp_ctx = data;
198 struct mgcp_client *mgcp;
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200199 struct mgcp_msg mgcp_msg;
Philipp Maier8bda7a72018-01-17 14:32:23 +0100200 struct msgb *msg;
201 int rc;
202
203 OSMO_ASSERT(mgcp_ctx);
204 mgcp = mgcp_ctx->mgcp;
205 OSMO_ASSERT(mgcp);
206
207 switch (event) {
208 case EV_CRCX:
209 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: creating connection on MGW endpoint:%s...\n",
210 mgcp_ctx->conn_peer_local.endpoint);
211
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200212 make_crcx_msg(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100213 if (mgcp_ctx->conn_peer_local.port)
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200214 add_audio(&mgcp_msg, &mgcp_ctx->conn_peer_local);
Neels Hofmeyrcb760bd2019-03-04 21:07:54 +0100215 set_conn_mode(&mgcp_msg, &mgcp_ctx->conn_peer_local);
216
Neels Hofmeyr475f8682018-08-23 16:38:59 +0200217 msg = mgcp_msg_gen(mgcp_ctx->mgcp, &mgcp_msg);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100218 OSMO_ASSERT(msg);
219
220 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
221 mgcp_ctx->mgw_trans_pending = true;
222 rc = mgcp_client_tx(mgcp, msg, mgw_crcx_resp_cb, fi);
223 if (rc < 0) {
224 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
225 return;
226 }
227
228 osmo_fsm_inst_state_chg(fi, ST_CRCX_RESP, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
229 break;
230 default:
231 OSMO_ASSERT(false);
232 break;
233 }
234}
235
Neels Hofmeyr04da5e52018-06-12 21:51:23 +0200236/* Return the CI that the MGW allocated during CRCX response. This is purely informational for logging
237 * and identity tracking; the mgcp_conn_*() functions take care of using the right CI internally. */
238const char *mgcp_conn_get_ci(struct osmo_fsm_inst *fi)
239{
240 struct mgcp_ctx *mgcp_ctx = fi->priv;
241 return mgcp_ctx->conn_id;
242}
243
Philipp Maier8bda7a72018-01-17 14:32:23 +0100244static void mgw_crcx_resp_cb(struct mgcp_response *r, void *priv)
245{
246 struct osmo_fsm_inst *fi = priv;
247 struct mgcp_ctx *mgcp_ctx;
248 int rc;
249
250 OSMO_ASSERT(fi);
251 mgcp_ctx = fi->priv;
252 OSMO_ASSERT(mgcp_ctx);
253
254 mgcp_ctx->mgw_trans_pending = false;
255
256 if (r->head.response_code != 200) {
257 LOGPFSML(fi, LOGL_ERROR,
258 "MGW/CRCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
259 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
260 return;
261 }
262
263 osmo_strlcpy(mgcp_ctx->conn_id, r->head.conn_id, sizeof(mgcp_ctx->conn_id));
264 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with CI: %s\n", mgcp_ctx->conn_id);
265
266 rc = mgcp_response_parse_params(r);
267 if (rc) {
268 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: Cannot parse CRCX response\n");
269 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
270 return;
271 }
272 LOGPFSML(fi, LOGL_DEBUG, "MGW/CRCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
273
274 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
275 mgcp_ctx->conn_peer_remote.port = r->audio_port;
276
277 if (strlen(r->head.endpoint) > 0) {
278 /* If we get an endpoint identifier back from the MGW, take it */
279 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, r->head.endpoint,
280 sizeof(mgcp_ctx->conn_peer_remote.endpoint));
281 } else if (strstr(mgcp_ctx->conn_peer_local.endpoint, "*") == NULL) {
282 /* If we do not get an endpoint identifier back and the
283 * identifier we used to create the connection is not a
284 * wildcarded one, we take the local endpoint identifier
285 * instead */
286 osmo_strlcpy(mgcp_ctx->conn_peer_remote.endpoint, mgcp_ctx->conn_peer_local.endpoint,
287 sizeof(mgcp_ctx->conn_peer_local.endpoint));
288 } else {
289 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: CRCX yielded not suitable endpoint identifier\n");
290 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
291 return;
292 }
293
294 mgcp_ctx->conn_peer_remote.call_id = mgcp_ctx->conn_peer_local.call_id;
295
296 osmo_fsm_inst_dispatch(fi, EV_CRCX_RESP, mgcp_ctx);
297}
298
299static void fsm_crcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
300{
301 struct mgcp_ctx *mgcp_ctx = data;
302 OSMO_ASSERT(mgcp_ctx);
303
304 switch (event) {
305 case EV_CRCX_RESP:
306 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
307 if (mgcp_ctx->terminate) {
308 /* Trigger immediate DLCX if DLCX was requested while the FSM was
309 * busy with the previous operation */
310 LOGPFSML(fi, LOGL_ERROR, "MGW/CRCX: FSM was busy while DLCX was requested, executing now...\n");
311 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
312 } else
313 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
314 break;
315 default:
316 OSMO_ASSERT(false);
317 break;
318 }
319}
320
321static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv);
322static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv);
323
324static void fsm_ready_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
325{
326 struct mgcp_ctx *mgcp_ctx = data;
327 struct msgb *msg;
328 struct mgcp_client *mgcp;
329 uint32_t new_state;
330 int rc;
331
332 OSMO_ASSERT(mgcp_ctx);
333 mgcp = mgcp_ctx->mgcp;
334 OSMO_ASSERT(mgcp);
335
336 switch (event) {
337 case EV_MDCX:
338 msg = make_mdcx_msg(mgcp_ctx);
339 OSMO_ASSERT(msg);
340 rc = mgcp_client_tx(mgcp, msg, mgw_mdcx_resp_cb, fi);
341 new_state = ST_MDCX_RESP;
342 break;
343 case EV_DLCX:
344 msg = make_dlcx_msg(mgcp_ctx);
345 OSMO_ASSERT(msg);
346 rc = mgcp_client_tx(mgcp, msg, mgw_dlcx_resp_cb, fi);
347 new_state = ST_DLCX_RESP;
348 break;
349 default:
350 OSMO_ASSERT(false);
351 break;
352 }
353
354 mgcp_ctx->mgw_pending_trans = mgcp_msg_trans_id(msg);
355 mgcp_ctx->mgw_trans_pending = true;
356
357 if (rc < 0) {
358 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
359 return;
360 }
361
362 osmo_fsm_inst_state_chg(fi, new_state, MGCP_MGW_TIMEOUT, MGCP_MGW_TIMEOUT_TIMER_NR);
363}
364
365static void mgw_mdcx_resp_cb(struct mgcp_response *r, void *priv)
366{
367 struct osmo_fsm_inst *fi = priv;
368 struct mgcp_ctx *mgcp_ctx;
369 int rc;
370
371 OSMO_ASSERT(fi);
372 mgcp_ctx = fi->priv;
373 OSMO_ASSERT(mgcp_ctx);
374
375 mgcp_ctx->mgw_trans_pending = false;
376
377 if (r->head.response_code != 200) {
378 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: response yields error: %d %s\n", r->head.response_code,
379 r->head.comment);
380 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
381 return;
382 }
383
384 rc = mgcp_response_parse_params(r);
385 if (rc) {
386 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: Cannot parse MDCX response\n");
387 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
388 return;
389 }
390 LOGPFSML(fi, LOGL_DEBUG, "MGW/MDCX: MGW responded with address %s:%u\n", r->audio_ip, r->audio_port);
391
392 osmo_strlcpy(mgcp_ctx->conn_peer_remote.addr, r->audio_ip, sizeof(mgcp_ctx->conn_peer_remote.addr));
393 mgcp_ctx->conn_peer_remote.port = r->audio_port;
394
395 osmo_fsm_inst_dispatch(fi, EV_MDCX_RESP, mgcp_ctx);
396}
397
398static void fsm_mdcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
399{
400 struct mgcp_ctx *mgcp_ctx = data;
401 OSMO_ASSERT(mgcp_ctx);
402
403 switch (event) {
404 case EV_MDCX_RESP:
405 osmo_fsm_inst_state_chg(fi, ST_READY, 0, 0);
406 if (mgcp_ctx->terminate) {
407 /* Trigger immediate DLCX if DLCX was requested while the FSM was
408 * busy with the previous operation */
409 LOGPFSML(fi, LOGL_ERROR, "MGW/MDCX: FSM was busy while DLCX was requested, executing now...\n");
410 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
411 } else
412 osmo_fsm_inst_dispatch(fi->proc.parent, mgcp_ctx->parent_evt, &mgcp_ctx->conn_peer_remote);
413 break;
414 default:
415 OSMO_ASSERT(false);
416 break;
417 }
418}
419
420static void mgw_dlcx_resp_cb(struct mgcp_response *r, void *priv)
421{
422 struct osmo_fsm_inst *fi = priv;
423 struct mgcp_ctx *mgcp_ctx;
424
425 OSMO_ASSERT(fi);
426 mgcp_ctx = fi->priv;
427 OSMO_ASSERT(mgcp_ctx);
428
429 mgcp_ctx->mgw_trans_pending = false;
430
431 if (r->head.response_code != 250) {
432 LOGPFSML(fi, LOGL_ERROR,
433 "MGW/DLCX: response yields error: %d %s\n", r->head.response_code, r->head.comment);
434 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
435 return;
436 }
437
438 osmo_fsm_inst_dispatch(fi, EV_DLCX_RESP, mgcp_ctx);
439}
440
441static void fsm_dlcx_resp_cb(struct osmo_fsm_inst *fi, uint32_t event, void *data)
442{
443 struct mgcp_ctx *mgcp_ctx = data;
444 OSMO_ASSERT(mgcp_ctx);
445
446 switch (event) {
447 case EV_DLCX_RESP:
448 /* Rub out the connection identifier, since the connection
449 * is no longer present and we will use the connection id
450 * to know in error cases if the connection is still present
451 * or not */
452 memset(mgcp_ctx->conn_id, 0, sizeof(mgcp_ctx->conn_id));
453
454 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
455 break;
456 default:
457 OSMO_ASSERT(false);
458 break;
459 }
460}
461
462static int fsm_timeout_cb(struct osmo_fsm_inst *fi)
463{
464 struct mgcp_ctx *mgcp_ctx = fi->priv;
465 struct mgcp_client *mgcp;
466
467 OSMO_ASSERT(mgcp_ctx);
468 mgcp = mgcp_ctx->mgcp;
469 OSMO_ASSERT(mgcp);
470
471 if (fi->T == MGCP_MGW_TIMEOUT_TIMER_NR) {
472 /* Note: We were unable to communicate with the MGW,
473 * unfortunately there is no meaningful action we can take
474 * now other than giving up. */
475 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
476 } else {
477 /* Note: Ther must not be any unsolicited timers
478 * in this FSM. If so, we have serious problem. */
479 OSMO_ASSERT(false);
480 }
481
482 return 0;
483}
484
485static void fsm_cleanup_cb(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
486{
487 struct mgcp_ctx *mgcp_ctx = fi->priv;
488 struct mgcp_client *mgcp;
489 struct msgb *msg;
490
491 OSMO_ASSERT(mgcp_ctx);
492 mgcp = mgcp_ctx->mgcp;
493 OSMO_ASSERT(mgcp);
494
495 /* If there is still a transaction pending, cancel it now. */
496 if (mgcp_ctx->mgw_trans_pending)
497 mgcp_client_cancel(mgcp, mgcp_ctx->mgw_pending_trans);
498
499 /* Should the FSM be terminated while there are still open connections
500 * on the MGW, we send an unconditional DLCX to terminate the
501 * connection. This is not the normal case. The user should always use
502 * mgcp_conn_delete() to instruct the FSM to perform a graceful exit */
503 if (strlen(mgcp_ctx->conn_id)) {
504 LOGPFSML(fi, LOGL_ERROR,
Harald Welted4e6aa42018-06-02 18:07:10 +0200505 "MGW/DLCX: abrupt FSM termination with connections still present, sending unconditional DLCX...\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100506 msg = make_dlcx_msg(mgcp_ctx);
507 OSMO_ASSERT(msg);
508 mgcp_client_tx(mgcp, msg, NULL, NULL);
509 }
510
511 talloc_free(mgcp_ctx);
512}
513
514static struct osmo_fsm_state fsm_mgcp_client_states[] = {
515
516 /* Initial CRCX state. This state is immediately entered and executed
517 * when the FSM is started. The rationale is that we first have to
518 * create a connectin before we can execute other operations on that
519 * connection. */
520 [ST_CRCX] = {
521 .in_event_mask = S(EV_CRCX),
522 .out_state_mask = S(ST_CRCX_RESP),
523 .name = OSMO_STRINGIFY(ST_CRCX),
524 .action = fsm_crcx_cb,
525 },
526
527 /* Wait for the response to a CRCX operation, check and process the
528 * results, change to ST_READY afterwards. */
529 [ST_CRCX_RESP] = {
530 .in_event_mask = S(EV_CRCX_RESP),
531 .out_state_mask = S(ST_READY),
532 .name = OSMO_STRINGIFY(ST_CRCX_RESP),
533 .action = fsm_crcx_resp_cb,
534 },
535
536 /* In this idle state we wait for further operations (e.g. MDCX) that
537 * can be executed by the user using the API. There is no timeout in
538 * this state. The connection lives on until the user decides to
539 * terminate it (DLCX). */
540 [ST_READY] = {
541 .in_event_mask = S(EV_MDCX) | S(EV_DLCX),
542 .out_state_mask = S(ST_MDCX_RESP) | S(ST_DLCX_RESP),
543 .name = OSMO_STRINGIFY(ST_READY),
544 .action = fsm_ready_cb,
545 },
546
547 /* Wait for the response of a MDCX operation, check and process the
548 * results, change to ST_READY afterwards. */
549 [ST_MDCX_RESP] = {
550 .in_event_mask = S(EV_MDCX_RESP),
551 .out_state_mask = S(ST_READY),
552 .name = OSMO_STRINGIFY(ST_MDCX_RESP),
553 .action = fsm_mdcx_resp_cb,
554 },
555
556 /* Wait for the response of a DLCX operation and terminate the FSM
557 * normally. */
558 [ST_DLCX_RESP] = {
559 .in_event_mask = S(EV_DLCX_RESP),
560 .out_state_mask = 0,
561 .name = OSMO_STRINGIFY(ST_DLCX_RESP),
562 .action = fsm_dlcx_resp_cb,
563 },
564};
565
566static struct osmo_fsm fsm_mgcp_client = {
567 .name = "MGCP_CONN",
568 .states = fsm_mgcp_client_states,
569 .num_states = ARRAY_SIZE(fsm_mgcp_client_states),
570 .timer_cb = fsm_timeout_cb,
571 .cleanup = fsm_cleanup_cb,
Philipp Maierd2e3a522018-02-26 14:29:01 +0100572 .event_names = fsm_mgcp_client_evt_names,
Philipp Maier8bda7a72018-01-17 14:32:23 +0100573};
574
575/*! allocate FSM, and create a new connection on the MGW.
576 * \param[in] mgcp MGCP client descriptor.
Neels Hofmeyred1cff52018-05-17 23:59:46 +0200577 * \param[in] parent_fi Parent FSM instance.
Philipp Maier8bda7a72018-01-17 14:32:23 +0100578 * \param[in] parent_term_evt Event to be sent to parent when terminating.
579 * \param[in] parent_evt Event to be sent to parent when operation is done.
580 * \param[in] conn_peer Connection parameters (ip, port...).
581 * \returns newly-allocated, initialized and registered FSM instance, NULL on error. */
582struct osmo_fsm_inst *mgcp_conn_create(struct mgcp_client *mgcp, struct osmo_fsm_inst *parent_fi,
583 uint32_t parent_term_evt, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
584{
585 struct mgcp_ctx *mgcp_ctx;
586 static bool fsm_registered = false;
587 struct osmo_fsm_inst *fi;
588 struct in_addr ip_test;
589
590 OSMO_ASSERT(parent_fi);
591 OSMO_ASSERT(mgcp);
592 OSMO_ASSERT(conn_peer);
593
Philipp Maier704c4f02018-06-07 18:51:31 +0200594 /* Check if IP/Port information in conn info makes sense */
Philipp Maier8bda7a72018-01-17 14:32:23 +0100595 if (conn_peer->port && inet_aton(conn_peer->addr, &ip_test) == 0)
596 return NULL;
597
598 /* Register the fsm description (if not already done) */
599 if (fsm_registered == false) {
600 osmo_fsm_register(&fsm_mgcp_client);
601 fsm_registered = true;
602 }
603
604 /* Allocate and configure a new fsm instance */
605 fi = osmo_fsm_inst_alloc_child(&fsm_mgcp_client, parent_fi, parent_term_evt);
606 OSMO_ASSERT(fi);
607 mgcp_ctx = talloc_zero(fi, struct mgcp_ctx);
608 OSMO_ASSERT(mgcp_ctx);
609 mgcp_ctx->mgcp = mgcp;
610 mgcp_ctx->parent_evt = parent_evt;
611
612 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
613 fi->priv = mgcp_ctx;
614
615 /* start state machine */
616 OSMO_ASSERT(fi->state == ST_CRCX);
617 osmo_fsm_inst_dispatch(fi, EV_CRCX, mgcp_ctx);
618
619 return fi;
620}
621
622/*! modify an existing connection on the MGW.
623 * \param[in] fi FSM instance.
624 * \param[in] parent_evt Event to be sent to parent when operation is done.
625 * \param[in] conn_peer New connection information (ip, port...).
626 * \returns 0 on success, -EINVAL on error. */
627int mgcp_conn_modify(struct osmo_fsm_inst *fi, uint32_t parent_evt, struct mgcp_conn_peer *conn_peer)
628{
629 OSMO_ASSERT(fi);
630 struct mgcp_ctx *mgcp_ctx = fi->priv;
631 struct in_addr ip_test;
632
633 OSMO_ASSERT(mgcp_ctx);
634 OSMO_ASSERT(conn_peer);
635
636 /* The user must not issue an MDCX before the CRCX has completed,
637 * if this happens, it means that the parent FSM has overhead the
638 * parent_evt (mandatory!) and executed the MDCX without even
639 * waiting for the results. Another reason could be that the
640 * parent FSM got messed up */
641 OSMO_ASSERT(fi->state != ST_CRCX_RESP);
642
643 /* If the user tries to issue an MDCX while an DLCX operation is
644 * pending, there must be a serious problem with the paren FSM.
645 * Eeither the parent_term_evt (mandatory!) has been overheard,
646 * or the parant FSM got messed so badly that it still assumes
647 * a live connection although it as killed it. */
648 OSMO_ASSERT(fi->state != ST_DLCX_RESP);
649
650 /* Check if IP/Port parameters make sense */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200651 if (conn_peer->port == 0) {
652 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, port == 0\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100653 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200654 }
655 if (inet_aton(conn_peer->addr, &ip_test) == 0) {
656 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, IP address == 0.0.0.0\n");
Philipp Maier8bda7a72018-01-17 14:32:23 +0100657 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200658 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100659
660 /*! The user may supply an endpoint identifier in conn_peer. The
661 * identifier is then checked. This check is optional. Later steps do
662 * not depend on the endpoint identifier supplied here because it is
663 * already implicitly known from the CRCX phase. */
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200664 if (strlen(conn_peer->endpoint) && strcmp(conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint)) {
665 LOGPFSML(fi, LOGL_ERROR, "Cannot MDCX, endpoint mismatches: requested %s, should be %s\n",
666 conn_peer->endpoint, mgcp_ctx->conn_peer_remote.endpoint);
Philipp Maier8bda7a72018-01-17 14:32:23 +0100667 return -EINVAL;
Neels Hofmeyr5928dc92018-06-15 04:33:37 +0200668 }
Philipp Maier8bda7a72018-01-17 14:32:23 +0100669
670 /*! Note: The call-id is implicitly known from the previous CRCX and
671 * will not be checked even when it is set in conn_peer. */
672
673 mgcp_ctx->parent_evt = parent_evt;
674 memcpy(&mgcp_ctx->conn_peer_local, conn_peer, sizeof(mgcp_ctx->conn_peer_local));
675 osmo_fsm_inst_dispatch(fi, EV_MDCX, mgcp_ctx);
676 return 0;
677}
678
679/*! delete existing connection on the MGW, destroy FSM afterwards.
680 * \param[in] fi FSM instance. */
681void mgcp_conn_delete(struct osmo_fsm_inst *fi)
682{
683 OSMO_ASSERT(fi);
684 struct mgcp_ctx *mgcp_ctx = fi->priv;
685
686 OSMO_ASSERT(mgcp_ctx);
687
688 /* Unlink FSM from parent */
689 osmo_fsm_inst_unlink_parent(fi, NULL);
690
691 /* An error situation where the parent FSM must be killed immediately
692 * may lead into a situation where the DLCX can not be executed right
693 * at that moment because the FSM is still busy with another operation.
694 * In those cases we postpone the DLCX so that the FSM and the
695 * connections on the MGW get cleaned up gracefully. */
696 if (fi->state != ST_READY) {
697 LOGPFSML(fi, LOGL_ERROR, "MGW: operation still pending, DLCX will be postponed.\n");
698 mgcp_ctx->terminate = true;
699 return;
700 }
701 osmo_fsm_inst_dispatch(fi, EV_DLCX, mgcp_ctx);
702}
Neels Hofmeyr538d2c52019-01-28 03:51:35 +0100703
704const char *osmo_mgcpc_conn_peer_name(const struct mgcp_conn_peer *info)
705{
706 /* I'd be fine with a smaller buffer and accept truncation, but gcc possibly refuses to build if
707 * this buffer is too small. */
708 static char buf[1024];
709
710 if (!info)
711 return "NULL";
712
713 if (info->endpoint[0]
714 && info->addr[0])
715 snprintf(buf, sizeof(buf), "%s:%s:%u",
716 info->endpoint, info->addr, info->port);
717 else if (info->endpoint[0])
718 snprintf(buf, sizeof(buf), "%s", info->endpoint);
719 else if (info->addr[0])
720 snprintf(buf, sizeof(buf), "%s:%u", info->addr, info->port);
721 else
722 return "empty";
723 return buf;
724}