blob: ccbdfc8c10dacbf1f75a06e18fb88f6aeb255254 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <ctype.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <time.h>
28#include <limits.h>
29#include <unistd.h>
30#include <errno.h>
31
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/select.h>
Stefan Sperling1e174872018-10-25 18:36:10 +020035#include <osmocom/core/stats.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020036
Philipp Maier87bd9be2017-08-22 16:35:41 +020037#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020038#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020039#include <osmocom/mgcp/mgcp_internal.h>
40#include <osmocom/mgcp/mgcp_stat.h>
41#include <osmocom/mgcp/mgcp_msg.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010042#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maier8970c492017-10-11 13:33:42 +020043#include <osmocom/mgcp/mgcp_sdp.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020044#include <osmocom/mgcp/mgcp_codec.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020045
46struct mgcp_request {
47 char *name;
Philipp Maier87bd9be2017-08-22 16:35:41 +020048 struct msgb *(*handle_request) (struct mgcp_parse_data * data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020049 char *debug_name;
50};
51
52#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
53 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
54
Stefan Sperling1e174872018-10-25 18:36:10 +020055static const struct rate_ctr_desc mgcp_crcx_ctr_desc[] = {
56 [MGCP_CRCX_SUCCESS] = {"crcx:success", "CRCX command processed successfully."},
57 [MGCP_CRCX_FAIL_BAD_ACTION] = {"crcx:bad_action", "bad action in CRCX command."},
58 [MGCP_CRCX_FAIL_UNHANDLED_PARAM] = {"crcx:unhandled_param", "unhandled parameter in CRCX command."},
59 [MGCP_CRCX_FAIL_MISSING_CALLID] = {"crcx:missing_callid", "missing CallId in CRCX command."},
60 [MGCP_CRCX_FAIL_INVALID_MODE] = {"crcx:invalid_mode", "connection invalid mode in CRCX command."},
61 [MGCP_CRCX_FAIL_LIMIT_EXCEEDED] = {"crcx:limit_exceeded", "limit of concurrent connections was reached."},
62 [MGCP_CRCX_FAIL_UNKNOWN_CALLID] = {"crcx:unkown_callid", "unknown CallId in CRCX command."},
63 [MGCP_CRCX_FAIL_ALLOC_CONN] = {"crcx:alloc_conn_fail", "connection allocation failure."},
64 [MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC] = {"crcx:no_remote_conn_desc", "no opposite end specified for connection."},
65 [MGCP_CRCX_FAIL_START_RTP] = {"crcx:start_rtp_failure", "failure to start RTP processing."},
66 [MGCP_CRCX_FAIL_REJECTED_BY_POLICY] = {"crcx:conn_rejected", "connection rejected by policy."},
Stefan Sperlinga714abf2018-10-29 14:19:54 +010067 [MGCP_CRCX_FAIL_NO_OSMUX] = {"crcx:no_osmux", "no osmux offered by peer."},
68 [MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS] = {"crcx:conn_opt", "connection options invalid."},
69 [MGCP_CRCX_FAIL_CODEC_NEGOTIATION] = {"crcx:codec_nego", "codec negotiation failure."},
70 [MGCP_CRCX_FAIL_BIND_PORT] = {"crcx:bind_port", "port bind failure."},
Stefan Sperling1e174872018-10-25 18:36:10 +020071};
72
73const static struct rate_ctr_group_desc mgcp_crcx_ctr_group_desc = {
74 .group_name_prefix = "crcx",
75 .group_description = "crxc statistics",
76 .class_id = OSMO_STATS_CLASS_GLOBAL,
77 .num_ctr = ARRAY_SIZE(mgcp_crcx_ctr_desc),
78 .ctr_desc = mgcp_crcx_ctr_desc
79};
80
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020081static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data);
82static struct msgb *handle_create_con(struct mgcp_parse_data *data);
83static struct msgb *handle_delete_con(struct mgcp_parse_data *data);
84static struct msgb *handle_modify_con(struct mgcp_parse_data *data);
85static struct msgb *handle_rsip(struct mgcp_parse_data *data);
86static struct msgb *handle_noti_req(struct mgcp_parse_data *data);
87
Philipp Maier87bd9be2017-08-22 16:35:41 +020088/* Initalize transcoder */
89static int setup_rtp_processing(struct mgcp_endpoint *endp,
90 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020091{
Philipp Maier87bd9be2017-08-22 16:35:41 +020092 struct mgcp_config *cfg = endp->cfg;
93 struct mgcp_conn_rtp *conn_src = NULL;
94 struct mgcp_conn_rtp *conn_dst = conn;
95 struct mgcp_conn *_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020096
Philipp Maier87bd9be2017-08-22 16:35:41 +020097 if (conn->type != MGCP_RTP_DEFAULT) {
98 LOGP(DLMGCP, LOGL_NOTICE,
99 "endpoint:%x RTP-setup: Endpoint is not configured as RTP default, stopping here!\n",
100 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200101 return 0;
102 }
103
Philipp Maier87bd9be2017-08-22 16:35:41 +0200104 if (conn->conn->mode == MGCP_CONN_LOOPBACK) {
105 LOGP(DLMGCP, LOGL_NOTICE,
106 "endpoint:%x RTP-setup: Endpoint is in loopback mode, stopping here!\n",
107 ENDPOINT_NUMBER(endp));
108 return 0;
109 }
110
111 /* Find the "sister" connection */
112 llist_for_each_entry(_conn, &endp->conns, entry) {
113 if (_conn->id != conn->conn->id) {
114 conn_src = &_conn->u.rtp;
115 break;
116 }
117 }
118
Philipp Maieracc10352018-07-19 18:07:57 +0200119 return cfg->setup_rtp_processing_cb(endp, conn_dst, conn_src);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200120}
121
Philipp Maier87bd9be2017-08-22 16:35:41 +0200122/* array of function pointers for handling various
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200123 * messages. In the future this might be binary sorted
Philipp Maier87bd9be2017-08-22 16:35:41 +0200124 * for performance reasons. */
125static const struct mgcp_request mgcp_requests[] = {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
128 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
129 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
130 MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200131
Philipp Maier87bd9be2017-08-22 16:35:41 +0200132 /* SPEC extension */
133 MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200134};
135
Philipp Maier87bd9be2017-08-22 16:35:41 +0200136/* Helper function to allocate some memory for responses and retransmissions */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200137static struct msgb *mgcp_msgb_alloc(void)
138{
139 struct msgb *msg;
140 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
141 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200142 LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200143
144 return msg;
145}
146
Philipp Maier87bd9be2017-08-22 16:35:41 +0200147/* Helper function for do_retransmission() and create_resp() */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200148static struct msgb *do_retransmission(const struct mgcp_endpoint *endp)
149{
150 struct msgb *msg = mgcp_msgb_alloc();
151 if (!msg)
152 return NULL;
153
154 msg->l2h = msgb_put(msg, strlen(endp->last_response));
155 memcpy(msg->l2h, endp->last_response, msgb_l2len(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200156 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157 return msg;
158}
159
160static struct msgb *create_resp(struct mgcp_endpoint *endp, int code,
161 const char *txt, const char *msg,
162 const char *trans, const char *param,
163 const char *sdp)
164{
165 int len;
166 struct msgb *res;
167
168 res = mgcp_msgb_alloc();
169 if (!res)
170 return NULL;
171
Philipp Maier87bd9be2017-08-22 16:35:41 +0200172 len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s",
173 code, trans, txt, param ? param : "", sdp ? sdp : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200174 if (len < 0) {
175 LOGP(DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n");
176 msgb_free(res);
177 return NULL;
178 }
179
180 res->l2h = msgb_put(res, len);
181 LOGP(DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200182 mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200183
184 /*
185 * Remember the last transmission per endpoint.
186 */
187 if (endp) {
188 struct mgcp_trunk_config *tcfg = endp->tcfg;
189 talloc_free(endp->last_response);
190 talloc_free(endp->last_trans);
191 endp->last_trans = talloc_strdup(tcfg->endpoints, trans);
192 endp->last_response = talloc_strndup(tcfg->endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200193 (const char *)res->l2h,
194 msgb_l2len(res));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195 }
196
197 return res;
198}
199
200static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200201 int code, const char *msg,
202 const char *trans,
203 const char *param)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200204{
205 return create_resp(endp, code, " OK", msg, trans, param, NULL);
206}
207
208static struct msgb *create_ok_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200209 int code, const char *msg,
210 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200211{
212 return create_ok_resp_with_param(endp, code, msg, trans, NULL);
213}
214
215static struct msgb *create_err_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216 int code, const char *msg,
217 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200218{
219 return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL);
220}
221
Philipp Maier55295f72018-01-15 14:00:28 +0100222/* Add MGCP parameters to a message buffer */
223static int add_params(struct msgb *msg, const struct mgcp_endpoint *endp,
224 const struct mgcp_conn_rtp *conn)
225{
226 int rc;
227
Philipp Maier7f0966c2018-01-17 18:18:12 +0100228 /* NOTE: Only in the virtual trunk we allow dynamic endpoint names */
Philipp Maier207ab512018-02-02 14:19:26 +0100229 if (endp->wildcarded_req
Philipp Maier7f0966c2018-01-17 18:18:12 +0100230 && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) {
Philipp Maierdd4ede32018-02-01 17:52:52 +0100231 rc = msgb_printf(msg, "Z: %s%x@%s\r\n",
Philipp Maier7f0966c2018-01-17 18:18:12 +0100232 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
233 ENDPOINT_NUMBER(endp), endp->cfg->domain);
Philipp Maier55295f72018-01-15 14:00:28 +0100234 if (rc < 0)
235 return -EINVAL;
236 }
237
Philipp Maierc3cfae22018-01-22 12:03:03 +0100238 rc = msgb_printf(msg, "I: %s\r\n", conn->conn->id);
Philipp Maier55295f72018-01-15 14:00:28 +0100239 if (rc < 0)
240 return -EINVAL;
241
242 return 0;
243}
244
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245/* Format MGCP response string (with SDP attached) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200246static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200247 struct mgcp_conn_rtp *conn,
248 const char *msg,
Philipp Maier55295f72018-01-15 14:00:28 +0100249 const char *trans_id,
250 bool add_conn_params)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200251{
252 const char *addr = endp->cfg->local_ip;
Philipp Maier8970c492017-10-11 13:33:42 +0200253 struct msgb *sdp;
254 int rc;
255 struct msgb *result;
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100256 char osmux_extension[strlen("X-Osmux: 255") + 1];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100257 char local_ip_addr[INET_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258
Philipp Maier8970c492017-10-11 13:33:42 +0200259 sdp = msgb_alloc_headroom(4096, 128, "sdp record");
260 if (!sdp)
261 return NULL;
262
Philipp Maier1cb1e382017-11-02 17:16:04 +0100263 if (!addr) {
264 mgcp_get_local_addr(local_ip_addr, conn);
265 addr = local_ip_addr;
266 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200267
Philipp Maier87bd9be2017-08-22 16:35:41 +0200268 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100269 sprintf(osmux_extension, "X-Osmux: %u", conn->osmux.cid);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200270 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200271 } else {
272 osmux_extension[0] = '\0';
273 }
274
Philipp Maier55295f72018-01-15 14:00:28 +0100275 /* Attach optional connection parameters */
276 if (add_conn_params) {
277 rc = add_params(sdp, endp, conn);
278 if (rc < 0)
279 goto error;
280 }
281
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100282 /* Attach optional OSMUX parameters */
283 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
Philipp Maierc3cfae22018-01-22 12:03:03 +0100284 rc = msgb_printf(sdp, "%s\r\n", osmux_extension);
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100285 if (rc < 0)
286 goto error;
287 }
288
289 /* Attach line break to separate the parameters from the SDP block */
Philipp Maierc3cfae22018-01-22 12:03:03 +0100290 rc = msgb_printf(sdp, "\r\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200291
Philipp Maier8970c492017-10-11 13:33:42 +0200292 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
293 if (rc < 0)
294 goto error;
295 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
296 msgb_free(sdp);
297 return result;
298error:
299 msgb_free(sdp);
300 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200301}
302
Philipp Maier87bd9be2017-08-22 16:35:41 +0200303/* Send out dummy packet to keep the connection open, if the connection is an
304 * osmux connection, send the dummy packet via OSMUX */
305static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200306{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200307 if (conn->osmux.state != OSMUX_STATE_DISABLED)
308 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200309 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200310 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200311}
312
Philipp Maier87bd9be2017-08-22 16:35:41 +0200313/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200314 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200315 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200316struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
317{
318 struct mgcp_parse_data pdata;
Harald Weltee35eeae2017-12-28 13:47:37 +0100319 int rc, i, code, handled = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200320 struct msgb *resp = NULL;
321 char *data;
322
323 if (msgb_l2len(msg) < 4) {
324 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
325 return NULL;
326 }
327
328 if (mgcp_msg_terminate_nul(msg))
329 return NULL;
330
Philipp Maier87bd9be2017-08-22 16:35:41 +0200331 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200332
Philipp Maier87bd9be2017-08-22 16:35:41 +0200333 /* attempt to treat it as a response */
334 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200335 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
336 return NULL;
337 }
338
339 msg->l3h = &msg->l2h[4];
340
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200341 /*
342 * Check for a duplicate message and respond.
343 */
344 memset(&pdata, 0, sizeof(pdata));
345 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200346 data = mgcp_strline((char *)msg->l3h, &pdata.save);
Harald Weltee35eeae2017-12-28 13:47:37 +0100347 rc = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200348 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200349 && pdata.endp->last_trans
350 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200351 return do_retransmission(pdata.endp);
352 }
353
Harald Welteabbb6b92017-12-28 13:13:50 +0100354 /* check for general parser failure */
Harald Weltee35eeae2017-12-28 13:47:37 +0100355 if (rc < 0) {
Harald Welteabbb6b92017-12-28 13:13:50 +0100356 LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h);
Harald Weltee35eeae2017-12-28 13:47:37 +0100357 return create_err_response(NULL, -rc, (const char *) msg->l2h, pdata.trans);
Harald Welteabbb6b92017-12-28 13:13:50 +0100358 }
359
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200360 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200361 if (strncmp
362 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
363 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200364 handled = 1;
365 resp = mgcp_requests[i].handle_request(&pdata);
366 break;
367 }
368 }
369
370 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200371 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
372 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200373
374 return resp;
375}
376
Philipp Maier87bd9be2017-08-22 16:35:41 +0200377/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
379{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200380 LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100381 return create_ok_response(p->endp, 200, "AUEP", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200382}
383
Harald Welte1d1b98f2017-12-25 10:03:40 +0100384/* Try to find a free port by attempting to bind on it. Also handle the
Philipp Maier87bd9be2017-08-22 16:35:41 +0200385 * counter that points on the next free port. Since we have a pointer
Philipp Maierb38fb892018-05-22 13:52:21 +0200386 * to the next free port, binding should in work on the first attempt in
387 * general. In case of failure the next port is tryed until the whole port
388 * range is tryed once. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200389static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200390{
391 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200392 struct mgcp_rtp_end *end;
393 struct mgcp_port_range *range;
Philipp Maierb38fb892018-05-22 13:52:21 +0200394 unsigned int tries;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200395
Philipp Maier87bd9be2017-08-22 16:35:41 +0200396 OSMO_ASSERT(conn);
397 end = &conn->end;
398 OSMO_ASSERT(end);
399
400 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200401
402 /* attempt to find a port */
Philipp Maierb38fb892018-05-22 13:52:21 +0200403 tries = (range->range_end - range->range_start) / 2;
404 for (i = 0; i < tries; ++i) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200405 int rc;
406
407 if (range->last_port >= range->range_end)
408 range->last_port = range->range_start;
409
Philipp Maier87bd9be2017-08-22 16:35:41 +0200410 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200411
412 range->last_port += 2;
413 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200414 return 0;
415 }
416
417 }
418
Philipp Maier87bd9be2017-08-22 16:35:41 +0200419 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maierb38fb892018-05-22 13:52:21 +0200420 "Allocating a RTP/RTCP port failed %u times 0x%x.\n",
421 tries, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200422 return -1;
423}
424
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200425/*! Helper function for check_local_cx_options() to get a pointer of the next
426 * lco option identifier
427 * \param[in] lco string
428 * \returns pointer to the beginning of the LCO identifier, NULL on failure */
429char *get_lco_identifier(const char *options)
430{
431 char *ptr;
432 unsigned int count = 0;
433
434 /* Jump to the end of the lco identifier */
435 ptr = strstr(options, ":");
436 if (!ptr)
437 return NULL;
438
439 /* Walk backwards until the pointer points to the beginning of the
440 * lco identifier. We know that we stand at the beginning when we
441 * are either at the beginning of the memory or see a space or
442 * comma. (this is tolerant, it will accept a:10, b:11 as well as
443 * a:10,b:11) */
444 while (1) {
445 /* Endless loop protection */
446 if (count > 10000)
447 return NULL;
448 else if (ptr < options || *ptr == ' ' || *ptr == ',') {
449 ptr++;
450 break;
451 }
452 ptr--;
453 count++;
454 }
455
456 /* Check if we got any result */
457 if (*ptr == ':')
458 return NULL;
459
460 return ptr;
461}
462
463/*! Check the LCO option. This function checks for multiple appearence of LCO
464 * options, which is illegal
465 * \param[in] ctx talloc context
466 * \param[in] lco string
467 * \returns 0 on success, -1 on failure */
468int check_local_cx_options(void *ctx, const char *options)
469{
470 int i;
471 char *options_copy;
472 char *lco_identifier;
473 char *lco_identifier_end;
474 char *next_lco_identifier;
475
476 char **lco_seen;
477 unsigned int lco_seen_n = 0;
478
479 if (!options)
480 return -1;
481
482 lco_seen =
483 (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *));
484 options_copy = talloc_strdup(ctx, options);
485 lco_identifier = options_copy;
486
487 do {
488 /* Move the lco_identifier pointer to the beginning of the
489 * current lco option identifier */
490 lco_identifier = get_lco_identifier(lco_identifier);
491 if (!lco_identifier)
492 goto error;
493
494 /* Look ahead to the next LCO option early, since we
495 * will parse destructively */
496 next_lco_identifier = strstr(lco_identifier + 1, ",");
497
498 /* Pinch off the end of the lco field identifier name
499 * and see if we still got something, also check if
500 * there is some value after the colon. */
501 lco_identifier_end = strstr(lco_identifier, ":");
502 if (!lco_identifier_end)
503 goto error;
504 if (*(lco_identifier_end + 1) == ' '
505 || *(lco_identifier_end + 1) == ','
506 || *(lco_identifier_end + 1) == '\0')
507 goto error;
508 *lco_identifier_end = '\0';
509 if (strlen(lco_identifier) == 0)
510 goto error;
511
512 /* Check if we have already seen the current field identifier
513 * before. If yes, we must bail, an LCO must only appear once
514 * in the LCO string */
515 for (i = 0; i < lco_seen_n; i++) {
516 if (strcmp(lco_seen[i], lco_identifier) == 0)
517 goto error;
518 }
519 lco_seen[lco_seen_n] = lco_identifier;
520 lco_seen_n++;
521
522 /* The first identifier must always be found at the beginnning
523 * of the LCO string */
524 if (lco_seen[0] != options_copy)
525 goto error;
526
527 /* Go to the next lco option */
528 lco_identifier = next_lco_identifier;
529 } while (lco_identifier);
530
531 talloc_free(lco_seen);
532 talloc_free(options_copy);
533 return 0;
534error:
535 talloc_free(lco_seen);
536 talloc_free(options_copy);
537 return -1;
538}
539
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200540/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100541 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200542 * like an empty string, the 'string' field is never NULL after this function
543 * has been called. */
Philipp Maiera390d0b2018-01-31 17:30:19 +0100544static int set_local_cx_options(void *ctx, struct mgcp_lco *lco,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200545 const char *options)
546{
547 char *p_opt, *a_opt;
548 char codec[9];
549
Philipp Maier604410c2018-06-06 10:02:16 +0200550 if (!options)
551 return 0;
552 if (strlen(options) == 0)
553 return 0;
554
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200555 /* Make sure the encoding of the LCO is consistant before we proceed */
556 if (check_local_cx_options(ctx, options) != 0) {
557 LOGP(DLMGCP, LOGL_ERROR,
558 "local CX options: Internal inconsistency in Local Connection Options!\n");
559 return 524;
560 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200561
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200562 talloc_free(lco->string);
563 lco->string = talloc_strdup(ctx, options);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200564
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200565 p_opt = strstr(lco->string, "p:");
566 if (p_opt && sscanf(p_opt, "p:%d-%d",
567 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
568 lco->pkt_period_max = lco->pkt_period_min;
569
Philipp Maierbc0346e2018-06-07 09:52:16 +0200570 /* FIXME: LCO also supports the negotiation of more then one codec.
571 * (e.g. a:PCMU;G726-32) But this implementation only supports a single
572 * codec only. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200573 a_opt = strstr(lco->string, "a:");
Philipp Maier604410c2018-06-06 10:02:16 +0200574 if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1) {
575 talloc_free(lco->codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200576 lco->codec = talloc_strdup(ctx, codec);
Philipp Maier604410c2018-06-06 10:02:16 +0200577 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200578
579 LOGP(DLMGCP, LOGL_DEBUG,
580 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
581 lco->pkt_period_max, lco->codec);
Philipp Maiera390d0b2018-01-31 17:30:19 +0100582
583 /* Check if the packetization fits the 20ms raster */
584 if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) {
585 LOGP(DLMGCP, LOGL_ERROR,
586 "local CX options: packetization interval is not a multiple of 20ms!\n");
587 return 535;
588 }
589
590 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200591}
592
593void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
594 struct mgcp_rtp_end *rtp)
595{
596 struct mgcp_trunk_config *tcfg = endp->tcfg;
597
598 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
599
600 rtp->force_aligned_timing = tcfg->force_aligned_timing;
601 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
602
603 LOGP(DLMGCP, LOGL_DEBUG,
604 "Configuring RTP endpoint: local port %d%s%s\n",
605 ntohs(rtp->rtp_port),
606 rtp->force_aligned_timing ? ", force constant timing" : "",
607 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
608}
609
610uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
611 struct mgcp_rtp_end *rtp)
612{
613 int f = 0;
614
615 /* Get the number of frames per channel and packet */
616 if (rtp->frames_per_packet)
617 f = rtp->frames_per_packet;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200618 else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) {
619 int den = 1000 * rtp->codec->frame_duration_num;
620 f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den +
Philipp Maier87bd9be2017-08-22 16:35:41 +0200621 den / 2)
622 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200623 }
624
Philipp Maierbc0346e2018-06-07 09:52:16 +0200625 return rtp->codec->rate * f * rtp->codec->frame_duration_num /
626 rtp->codec->frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200627}
628
629static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
630{
631 if (!endp->cfg->osmux_init) {
632 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
633 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
634 return -1;
635 }
636 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
637 }
638
639 return mgcp_parse_osmux_cid(line);
640}
641
Philipp Maierbc0346e2018-06-07 09:52:16 +0200642/* Process codec information contained in CRCX/MDCX */
643static int handle_codec_info(struct mgcp_conn_rtp *conn,
644 struct mgcp_parse_data *p, int have_sdp, bool crcx)
645{
646 struct mgcp_endpoint *endp = p->endp;
647 int rc;
648 char *cmd;
649
650 if (crcx)
651 cmd = "CRCX";
652 else
653 cmd = "MDCX";
654
655 /* Collect codec information */
656 if (have_sdp) {
657 /* If we have SDP, we ignore the local connection options and
658 * use only the SDP information. */
659 mgcp_codec_reset_all(conn);
660 rc = mgcp_parse_sdp_data(endp, conn, p);
661 if (rc != 0) {
662 LOGP(DLMGCP, LOGL_ERROR,
663 "%s: endpoint:%x sdp not parseable\n", cmd,
664 ENDPOINT_NUMBER(endp));
665
666 /* See also RFC 3661: Protocol error */
667 return 510;
668 }
669 } else if (endp->local_options.codec) {
670 /* When no SDP is available, we use the codec information from
671 * the local connection options (if present) */
672 mgcp_codec_reset_all(conn);
673 rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec);
674 if (rc != 0)
675 goto error;
676 }
677
678 /* Make sure we always set a sane default codec */
679 if (conn->end.codecs_assigned == 0) {
680 /* When SDP and/or LCO did not supply any codec information,
681 * than it makes sense to pick a sane default: (payload-type 0,
682 * PCMU), see also: OS#2658 */
683 mgcp_codec_reset_all(conn);
684 rc = mgcp_codec_add(conn, 0, NULL);
685 if (rc != 0)
686 goto error;
687 }
688
689 /* Make codec decision */
690 if (mgcp_codec_decide(conn) != 0)
691 goto error;
692
693 return 0;
694
695error:
696 LOGP(DLMGCP, LOGL_ERROR,
697 "%s: endpoint:0x%x codec negotiation failure\n", cmd,
698 ENDPOINT_NUMBER(endp));
699
700 /* See also RFC 3661: Codec negotiation failure */
701 return 534;
702}
703
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200704static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line)
705{
706 char *saveptr = NULL;
707
708 if (strncmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER)))
709 return false;
710 line += strlen(MGCP_X_OSMO_IGN_HEADER);
711
712 while (1) {
713 char *token = strtok_r(line, " ", &saveptr);
714 line = NULL;
715 if (!token)
716 break;
717
718 if (!strcmp(token, "C"))
719 endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID;
720 else
Pau Espin Pedrol9ecceb62018-10-16 15:35:58 +0200721 LOGP(DLMGCP, LOGL_ERROR, "endpoint 0x%x: received unknown X-Osmo-IGN item '%s'\n",
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200722 ENDPOINT_NUMBER(endp), token);
723 }
724
725 return true;
726}
727
Philipp Maier87bd9be2017-08-22 16:35:41 +0200728/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200729static struct msgb *handle_create_con(struct mgcp_parse_data *p)
730{
Stefan Sperling1e174872018-10-25 18:36:10 +0200731 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200732 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling9270e912018-10-29 14:10:00 +0100733 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_crcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200734 int error_code = 400;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200735 const char *local_options = NULL;
736 const char *callid = NULL;
737 const char *mode = NULL;
738 char *line;
739 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200740 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100741 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200742 char conn_name[512];
Philipp Maiera390d0b2018-01-31 17:30:19 +0100743 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200744
745 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200747 /* parse CallID C: and LocalParameters L: */
748 for_each_line(line, p->save) {
749 if (!mgcp_check_param(endp, line))
750 continue;
751
752 switch (line[0]) {
753 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200754 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200755 break;
756 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200757 callid = (const char *)line + 3;
758 break;
759 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100760 /* It is illegal to send a connection identifier
761 * together with a CRCX, the MGW will assign the
762 * connection identifier by itself on CRCX */
Stefan Sperling9270e912018-10-29 14:10:00 +0100763 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BAD_ACTION]);
Philipp Maierffd75e42017-11-22 11:44:50 +0100764 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200765 break;
766 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200767 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200768 break;
769 case 'X':
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200770 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
771 /* If osmux is disabled, just skip setting it up */
772 if (!p->endp->cfg->osmux)
773 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200774 osmux_cid = mgcp_osmux_setup(endp, line);
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200775 break;
776 }
777
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200778 if (parse_x_osmo_ign(endp, line))
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200779 break;
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200780
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200781 /* Ignore unknown X-headers */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200782 break;
783 case '\0':
784 have_sdp = 1;
785 goto mgcp_header_done;
786 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200787 LOGP(DLMGCP, LOGL_NOTICE,
788 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
789 ENDPOINT_NUMBER(endp), *line, *line);
Stefan Sperling9270e912018-10-29 14:10:00 +0100790 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100791 return create_err_response(NULL, 539, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200792 break;
793 }
794 }
795
796mgcp_header_done:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200797 /* Check parameters */
798 if (!callid) {
799 LOGP(DLMGCP, LOGL_ERROR,
800 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200801 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100802 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_MISSING_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100803 return create_err_response(endp, 516, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200804 }
805
Philipp Maier87bd9be2017-08-22 16:35:41 +0200806 if (!mode) {
807 LOGP(DLMGCP, LOGL_ERROR,
808 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
809 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100810 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100811 return create_err_response(endp, 517, "CRCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200812 }
813
Philipp Maier87bd9be2017-08-22 16:35:41 +0200814 /* Check if we are able to accept the creation of another connection */
815 if (llist_count(&endp->conns) >= endp->type->max_conns) {
816 LOGP(DLMGCP, LOGL_ERROR,
817 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
818 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200819 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200820 /* There is no more room for a connection, make some
821 * room by blindly tossing the oldest of the two two
822 * connections */
823 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200824 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200825 /* There is no more room for a connection, leave
826 * everything as it is and return with an error */
Stefan Sperling9270e912018-10-29 14:10:00 +0100827 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_LIMIT_EXCEEDED]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100828 return create_err_response(endp, 540, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200829 }
830 }
831
Philipp Maier87bd9be2017-08-22 16:35:41 +0200832 /* Check if this endpoint already serves a call, if so, check if the
833 * callids match up so that we are sure that this is our call */
834 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
835 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100836 "CRCX: endpoint:0x%x allready seized by other call (%s)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200837 ENDPOINT_NUMBER(endp), endp->callid);
838 if (tcfg->force_realloc)
839 /* This is not our call, toss everything by releasing
840 * the entire endpoint. (rude!) */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100841 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200842 else {
843 /* This is not our call, leave everything as it is and
844 * return with an error. */
Stefan Sperling9270e912018-10-29 14:10:00 +0100845 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNKNOWN_CALLID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200846 return create_err_response(endp, 400, "CRCX", p->trans);
847 }
848 }
849
850 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100851 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200852 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853 endp->callid = talloc_strdup(tcfg->endpoints, callid);
854
Philipp Maierffd75e42017-11-22 11:44:50 +0100855 snprintf(conn_name, sizeof(conn_name), "%s", callid);
856 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
857 if (!_conn) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200858 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100859 "CRCX: endpoint:0x%x unable to allocate RTP connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200860 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100861 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_ALLOC_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200862 goto error2;
863
Philipp Maier87bd9be2017-08-22 16:35:41 +0200864 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100865 conn = mgcp_conn_get_rtp(endp, _conn->id);
866 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200867
868 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
869 error_code = 517;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100870 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200871 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200872 }
873
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200874 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200875 * is fully set up from the dummy load. */
876 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200877 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200878 conn->osmux.cid = osmux_cid;
879 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200880 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
881 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100882 "CRCX: endpoint:0x%x osmux only and no osmux offered\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200883 ENDPOINT_NUMBER(endp));
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100884 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200885 goto error2;
886 }
887
Philipp Maierbc0346e2018-06-07 09:52:16 +0200888 /* Set local connection options, if present */
889 if (local_options) {
890 rc = set_local_cx_options(endp->tcfg->endpoints,
891 &endp->local_options, local_options);
892 if (rc != 0) {
893 LOGP(DLMGCP, LOGL_ERROR,
894 "CRCX: endpoint:%x inavlid local connection options!\n",
895 ENDPOINT_NUMBER(endp));
896 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100897 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200898 goto error2;
899 }
900 }
901
902 /* Handle codec information and decide for a suitable codec */
903 rc = handle_codec_info(conn, p, have_sdp, true);
904 mgcp_codec_summary(conn);
905 if (rc) {
906 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100907 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_CODEC_NEGOTIATION]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200908 goto error2;
909 }
910
Philipp Maier87bd9be2017-08-22 16:35:41 +0200911 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
912 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200913
Philipp Maier87bd9be2017-08-22 16:35:41 +0200914 if (p->cfg->force_ptime) {
915 conn->end.packet_duration_ms = p->cfg->force_ptime;
916 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200917 }
918
Philipp Maier1cb1e382017-11-02 17:16:04 +0100919 mgcp_rtp_end_config(endp, 0, &conn->end);
920
Philipp Maierc3cc6542018-02-02 12:58:42 +0100921 /* check connection mode setting */
922 if (conn->conn->mode != MGCP_CONN_LOOPBACK
923 && conn->conn->mode != MGCP_CONN_RECV_ONLY
924 && conn->end.rtp_port == 0) {
925 LOGP(DLMGCP, LOGL_ERROR,
926 "CRCX: endpoint:%x selected connection mode type requires an opposite end!\n",
927 ENDPOINT_NUMBER(endp));
928 error_code = 527;
Stefan Sperling9270e912018-10-29 14:10:00 +0100929 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +0100930 goto error2;
931 }
932
Philipp Maier1cb1e382017-11-02 17:16:04 +0100933 if (allocate_port(endp, conn) != 0) {
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100934 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BIND_PORT]);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100935 goto error2;
936 }
937
Philipp Maier87bd9be2017-08-22 16:35:41 +0200938 if (setup_rtp_processing(endp, conn) != 0) {
939 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100940 "CRCX: endpoint:0x%x could not start RTP processing!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200941 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100942 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200943 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200944 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200945
946 /* policy CB */
947 if (p->cfg->policy_cb) {
948 int rc;
949 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200950 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200951 switch (rc) {
952 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200953 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100954 "CRCX: endpoint:0x%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200955 ENDPOINT_NUMBER(endp));
Philipp Maier1355d7e2018-02-01 14:30:06 +0100956 mgcp_endp_release(endp);
Stefan Sperling9270e912018-10-29 14:10:00 +0100957 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200958 return create_err_response(endp, 400, "CRCX", p->trans);
959 break;
960 case MGCP_POLICY_DEFER:
961 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200962 return NULL;
963 break;
964 case MGCP_POLICY_CONT:
965 /* just continue */
966 break;
967 }
968 }
969
Philipp Maier87bd9be2017-08-22 16:35:41 +0200970 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100971 "CRCX: endpoint:0x%x Creating connection: CI: %s port: %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200972 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200973 if (p->cfg->change_cb)
974 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
975
Philipp Maiere726d4f2017-11-01 10:41:34 +0100976 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
977 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +0100979 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200980 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200981
Philipp Maier87bd9be2017-08-22 16:35:41 +0200982 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100983 "CRCX: endpoint:0x%x connection successfully created\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200984 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100985 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_SUCCESS]);
Philipp Maier55295f72018-01-15 14:00:28 +0100986 return create_response_with_sdp(endp, conn, "CRCX", p->trans, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200987error2:
Philipp Maier1355d7e2018-02-01 14:30:06 +0100988 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200989 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier3c8ccb62018-06-04 09:59:24 +0200990 "CRCX: endpoint:0x%x unable to create connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200991 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200992 return create_err_response(endp, error_code, "CRCX", p->trans);
993}
994
Philipp Maierbc0346e2018-06-07 09:52:16 +0200995
996
997
998
Philipp Maier87bd9be2017-08-22 16:35:41 +0200999/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001000static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
1001{
1002 struct mgcp_endpoint *endp = p->endp;
1003 int error_code = 500;
1004 int silent = 0;
1005 int have_sdp = 0;
1006 char *line;
1007 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001008 const char *mode = NULL;
1009 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +01001010 const char *conn_id = NULL;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001011 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001012
1013 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001014
Philipp Maier5656fbf2018-02-02 14:41:58 +01001015 /* Prohibit wildcarded requests */
1016 if (endp->wildcarded_req) {
1017 LOGP(DLMGCP, LOGL_ERROR,
1018 "MDCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
1019 ENDPOINT_NUMBER(endp));
1020 return create_err_response(endp, 507, "MDCX", p->trans);
1021 }
1022
Philipp Maier87bd9be2017-08-22 16:35:41 +02001023 if (llist_count(&endp->conns) <= 0) {
1024 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001025 "MDCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001026 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001027 return create_err_response(endp, 400, "MDCX", p->trans);
1028 }
1029
1030 for_each_line(line, p->save) {
1031 if (!mgcp_check_param(endp, line))
1032 continue;
1033
1034 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001035 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001036 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1037 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001038 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001039 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001040 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001041 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001042 conn_id = (const char *)line + 3;
Neels Hofmeyreb72ff02018-09-03 23:00:07 +02001043 if ((error_code = mgcp_verify_ci(endp, conn_id)))
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001044 goto error3;
1045 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001046 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001047 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001048 break;
1049 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001050 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001051 break;
1052 case 'Z':
1053 silent = strcmp("noanswer", line + 3) == 0;
1054 break;
1055 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001056 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001057 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001058 break;
1059 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001060 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001061 "MDCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001062 ENDPOINT_NUMBER(endp), line[0], line[0]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001063 return create_err_response(NULL, 539, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001064 break;
1065 }
1066 }
1067
Philipp Maier87bd9be2017-08-22 16:35:41 +02001068mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +01001069 if (!conn_id) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001070 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001071 "MDCX: endpoint:0x%x insufficient parameters, missing ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001072 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +01001073 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001074 }
1075
1076 conn = mgcp_conn_get_rtp(endp, conn_id);
1077 if (!conn)
1078 return create_err_response(endp, 400, "MDCX", p->trans);
1079
1080 if (mode) {
1081 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
1082 error_code = 517;
1083 goto error3;
1084 }
1085 } else
1086 conn->conn->mode = conn->conn->mode_orig;
1087
Philipp Maierbc0346e2018-06-07 09:52:16 +02001088 /* Set local connection options, if present */
1089 if (local_options) {
1090 rc = set_local_cx_options(endp->tcfg->endpoints,
1091 &endp->local_options, local_options);
1092 if (rc != 0) {
1093 LOGP(DLMGCP, LOGL_ERROR,
1094 "MDCX: endpoint:%x inavlid local connection options!\n",
1095 ENDPOINT_NUMBER(endp));
1096 error_code = rc;
1097 goto error3;
1098 }
1099 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001100
Philipp Maierbc0346e2018-06-07 09:52:16 +02001101 /* Handle codec information and decide for a suitable codec */
1102 rc = handle_codec_info(conn, p, have_sdp, false);
1103 mgcp_codec_summary(conn);
1104 if (rc) {
Philipp Maieraf07f662018-02-02 11:34:02 +01001105 error_code = rc;
1106 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001107 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001108
Philipp Maierc3cc6542018-02-02 12:58:42 +01001109 /* check connection mode setting */
1110 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1111 && conn->conn->mode != MGCP_CONN_RECV_ONLY
1112 && conn->end.rtp_port == 0) {
1113 LOGP(DLMGCP, LOGL_ERROR,
1114 "MDCX: endpoint:%x selected connection mode type requires an opposite end!\n",
1115 ENDPOINT_NUMBER(endp));
1116 error_code = 527;
1117 goto error3;
1118 }
1119
Philipp Maierbc0346e2018-06-07 09:52:16 +02001120
Philipp Maier87bd9be2017-08-22 16:35:41 +02001121 if (setup_rtp_processing(endp, conn) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001122 goto error3;
1123
Philipp Maier87bd9be2017-08-22 16:35:41 +02001124
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001125 /* policy CB */
1126 if (p->cfg->policy_cb) {
1127 int rc;
1128 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001129 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001130 switch (rc) {
1131 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001132 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001133 "MDCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001134 ENDPOINT_NUMBER(endp));
1135 if (silent)
1136 goto out_silent;
1137 return create_err_response(endp, 400, "MDCX", p->trans);
1138 break;
1139 case MGCP_POLICY_DEFER:
1140 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001141 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001142 "MDCX: endpoint:0x%x defered by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001143 ENDPOINT_NUMBER(endp));
1144 return NULL;
1145 break;
1146 case MGCP_POLICY_CONT:
1147 /* just continue */
1148 break;
1149 }
1150 }
1151
Philipp Maier87bd9be2017-08-22 16:35:41 +02001152 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001153
1154 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001155 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001156 "MDCX: endpoint:0x%x modified conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001157 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001158 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001159 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1160 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001161
Philipp Maiere726d4f2017-11-01 10:41:34 +01001162 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1163 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1164 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
1165 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001166 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001167
1168 if (silent)
1169 goto out_silent;
1170
Philipp Maier87bd9be2017-08-22 16:35:41 +02001171 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001172 "MDCX: endpoint:0x%x connection successfully modified\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001173 ENDPOINT_NUMBER(endp));
Philipp Maier55295f72018-01-15 14:00:28 +01001174 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001175error3:
1176 return create_err_response(endp, error_code, "MDCX", p->trans);
1177
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001178out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001179 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:0x%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001180 ENDPOINT_NUMBER(endp));
1181 return NULL;
1182}
1183
Philipp Maier87bd9be2017-08-22 16:35:41 +02001184/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
1186{
1187 struct mgcp_endpoint *endp = p->endp;
1188 int error_code = 400;
1189 int silent = 0;
1190 char *line;
1191 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +01001192 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001193 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001194
1195 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001196 "DLCX: endpoint:0x%x deleting connection ...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001197 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001198
Philipp Maier5656fbf2018-02-02 14:41:58 +01001199 /* Prohibit wildcarded requests */
1200 if (endp->wildcarded_req) {
1201 LOGP(DLMGCP, LOGL_ERROR,
1202 "DLCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
1203 ENDPOINT_NUMBER(endp));
1204 return create_err_response(endp, 507, "DLCX", p->trans);
1205 }
1206
Philipp Maier87bd9be2017-08-22 16:35:41 +02001207 if (llist_count(&endp->conns) <= 0) {
1208 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001209 "DLCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001210 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +01001211 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001212 }
1213
1214 for_each_line(line, p->save) {
1215 if (!mgcp_check_param(endp, line))
1216 continue;
1217
1218 switch (line[0]) {
1219 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001220 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1221 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001222 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001223 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001224 break;
1225 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001226 conn_id = (const char *)line + 3;
Neels Hofmeyreb72ff02018-09-03 23:00:07 +02001227 if ((error_code = mgcp_verify_ci(endp, conn_id)))
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001228 goto error3;
1229 break;
1230 case 'Z':
1231 silent = strcmp("noanswer", line + 3) == 0;
1232 break;
1233 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001234 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001235 "DLCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001236 ENDPOINT_NUMBER(endp), line[0], line[0]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001237 return create_err_response(NULL, 539, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001238 break;
1239 }
1240 }
1241
1242 /* policy CB */
1243 if (p->cfg->policy_cb) {
1244 int rc;
1245 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001246 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001247 switch (rc) {
1248 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001249 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001250 "DLCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001251 ENDPOINT_NUMBER(endp));
1252 if (silent)
1253 goto out_silent;
1254 return create_err_response(endp, 400, "DLCX", p->trans);
1255 break;
1256 case MGCP_POLICY_DEFER:
1257 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001258 return NULL;
1259 break;
1260 case MGCP_POLICY_CONT:
1261 /* just continue */
1262 break;
1263 }
1264 }
1265
Philipp Maierf4c0e372017-10-11 16:06:45 +02001266 /* When no connection id is supplied, we will interpret this as a
1267 * wildcarded DLCX and drop all connections at once. (See also
1268 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +01001269 if (!conn_id) {
Philipp Maierf4c0e372017-10-11 16:06:45 +02001270 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001271 "DLCX: endpoint:0x%x missing ci (connectionIdentifier), will remove all connections at once\n",
Philipp Maierf4c0e372017-10-11 16:06:45 +02001272 ENDPOINT_NUMBER(endp));
1273
Philipp Maier1355d7e2018-02-01 14:30:06 +01001274 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001275
1276 /* Note: In this case we do not return any statistics,
1277 * as we assume that the client is not interested in
1278 * this case. */
1279 return create_ok_response(endp, 200, "DLCX", p->trans);
1280 }
1281
Philipp Maierf4c0e372017-10-11 16:06:45 +02001282 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001283 conn = mgcp_conn_get_rtp(endp, conn_id);
1284 if (!conn)
1285 goto error3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001286
Philipp Maier87bd9be2017-08-22 16:35:41 +02001287 /* save the statistics of the current connection */
1288 mgcp_format_stats(stats, sizeof(stats), conn->conn);
1289
1290 /* delete connection */
Philipp Maier230e4fc2017-11-28 09:38:45 +01001291 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x deleting conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001292 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
1293 mgcp_conn_free(endp, conn_id);
1294 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001295 "DLCX: endpoint:0x%x connection successfully deleted\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001296 ENDPOINT_NUMBER(endp));
1297
1298 /* When all connections are closed, the endpoint will be released
1299 * in order to be ready to be used by another call. */
1300 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001301 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001302 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001303 "DLCX: endpoint:0x%x endpoint released\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001304 ENDPOINT_NUMBER(endp));
1305 }
1306
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001307 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001308 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1309 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001310
1311 if (silent)
1312 goto out_silent;
1313 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1314
1315error3:
1316 return create_err_response(endp, error_code, "DLCX", p->trans);
1317
1318out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001319 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x silent exit\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001320 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001321 return NULL;
1322}
1323
Philipp Maier87bd9be2017-08-22 16:35:41 +02001324/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001325static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1326{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001327 /* TODO: Also implement the resetting of a specific endpoint
1328 * to make mgcp_send_reset_ep() work. Currently this will call
1329 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1330 * to make read_call_agent() reset all endpoints when called
1331 * next time. In order to selectively reset endpoints some
1332 * mechanism to distinguish which endpoint shall be resetted
1333 * is needed */
1334
1335 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1336
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001337 if (p->cfg->reset_cb)
1338 p->cfg->reset_cb(p->endp->tcfg);
1339 return NULL;
1340}
1341
1342static char extract_tone(const char *line)
1343{
1344 const char *str = strstr(line, "D/");
1345 if (!str)
1346 return CHAR_MAX;
1347
1348 return str[2];
1349}
1350
Philipp Maier87bd9be2017-08-22 16:35:41 +02001351/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001352 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001353 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001354static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1355{
1356 int res = 0;
1357 char *line;
1358 char tone = CHAR_MAX;
1359
Philipp Maier87bd9be2017-08-22 16:35:41 +02001360 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1361
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001362 for_each_line(line, p->save) {
1363 switch (line[0]) {
1364 case 'S':
1365 tone = extract_tone(line);
1366 break;
1367 }
1368 }
1369
1370 /* we didn't see a signal request with a tone */
1371 if (tone == CHAR_MAX)
1372 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1373
1374 if (p->cfg->rqnt_cb)
1375 res = p->cfg->rqnt_cb(p->endp, tone);
1376
1377 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001378 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1379 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001380}
1381
Philipp Maier87bd9be2017-08-22 16:35:41 +02001382/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001383 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001384static void mgcp_keepalive_timer_cb(void *_tcfg)
1385{
1386 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001387 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001388 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001389
Philipp Maiere726d4f2017-11-01 10:41:34 +01001390 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001391 tcfg->trunk_nr);
1392
Philipp Maiere726d4f2017-11-01 10:41:34 +01001393 /* Do not accept invalid configuration values
1394 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1395 * values greater 0 */
1396 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1397
1398 /* The dummy packet functionality has been disabled, we will exit
1399 * immediately, no further timer is scheduled, which means we will no
1400 * longer send dummy packets even when we did before */
1401 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1402 return;
1403
1404 /* In cases where only one dummy packet is sent, we do not need
1405 * the timer since the functions that handle the CRCX and MDCX are
1406 * triggering the sending of the dummy packet. So we behave like in
1407 * the MGCP_KEEPALIVE_NEVER case */
1408 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001409 return;
1410
Philipp Maier87bd9be2017-08-22 16:35:41 +02001411 /* Send walk over all endpoints and send out dummy packets through
1412 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001413 for (i = 1; i < tcfg->number_endpoints; ++i) {
1414 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001415 llist_for_each_entry(conn, &endp->conns, entry) {
1416 if (conn->mode == MGCP_CONN_RECV_ONLY)
1417 send_dummy(endp, &conn->u.rtp);
1418 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001419 }
1420
Philipp Maiere726d4f2017-11-01 10:41:34 +01001421 /* Schedule the keepalive timer for the next round */
1422 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001423 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001424 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1425 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001426}
1427
1428void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1429{
1430 tcfg->keepalive_interval = interval;
1431 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1432
1433 if (interval <= 0)
1434 osmo_timer_del(&tcfg->keepalive_timer);
1435 else
1436 osmo_timer_schedule(&tcfg->keepalive_timer,
1437 tcfg->keepalive_interval, 0);
1438}
1439
Stefan Sperling1e174872018-10-25 18:36:10 +02001440static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group)
1441{
1442 rate_ctr_group_free(rate_ctr_group);
1443 return 0;
1444}
1445
1446static void alloc_mgcp_crxc_rate_counters(struct mgcp_trunk_config *trunk, void *ctx)
1447{
1448 /* FIXME: Each new rate counter group requires a unique index. At the
1449 * moment we generate an index using a counter, but perhaps there is
1450 * a better way of assigning indices? */
1451 static unsigned int rate_ctr_index = 0;
1452
1453 if (trunk->mgcp_crcx_ctr_group != NULL)
1454 return;
1455
1456 trunk->mgcp_crcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_crcx_ctr_group_desc, rate_ctr_index);
1457 OSMO_ASSERT(trunk->mgcp_crcx_ctr_group);
1458 talloc_set_destructor(trunk->mgcp_crcx_ctr_group, free_rate_counter_group);
1459 rate_ctr_index++;
1460}
1461
Philipp Maier87bd9be2017-08-22 16:35:41 +02001462/*! allocate configuration with default values.
1463 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001464struct mgcp_config *mgcp_config_alloc(void)
1465{
1466 struct mgcp_config *cfg;
1467
1468 cfg = talloc_zero(NULL, struct mgcp_config);
1469 if (!cfg) {
1470 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1471 return NULL;
1472 }
1473
Philipp Maier12943ea2018-01-17 15:40:25 +01001474 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1475
Philipp Maier87bd9be2017-08-22 16:35:41 +02001476 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1477 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1478 cfg->net_ports.last_port = cfg->net_ports.range_start;
1479
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001480 cfg->source_port = 2427;
1481 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1482 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1483
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001484 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1485 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1486
1487 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1488
1489 /* default trunk handling */
1490 cfg->trunk.cfg = cfg;
1491 cfg->trunk.trunk_nr = 0;
1492 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1493 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1494 cfg->trunk.audio_payload = 126;
1495 cfg->trunk.audio_send_ptime = 1;
1496 cfg->trunk.audio_send_name = 1;
1497 cfg->trunk.omit_rtcp = 0;
1498 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperling1e174872018-10-25 18:36:10 +02001499 alloc_mgcp_crxc_rate_counters(&cfg->trunk, cfg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001500
1501 INIT_LLIST_HEAD(&cfg->trunks);
1502
1503 return cfg;
1504}
1505
Philipp Maier87bd9be2017-08-22 16:35:41 +02001506/*! allocate configuration with default values.
1507 * (called once at startup by VTY)
1508 * \param[in] cfg mgcp configuration
1509 * \param[in] nr trunk number
1510 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001511struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1512{
1513 struct mgcp_trunk_config *trunk;
1514
1515 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1516 if (!trunk) {
1517 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1518 return NULL;
1519 }
1520
1521 trunk->cfg = cfg;
1522 trunk->trunk_type = MGCP_TRUNK_E1;
1523 trunk->trunk_nr = nr;
1524 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1525 trunk->audio_payload = 126;
1526 trunk->audio_send_ptime = 1;
1527 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001528 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001529 trunk->omit_rtcp = 0;
1530 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperling1e174872018-10-25 18:36:10 +02001531 alloc_mgcp_crxc_rate_counters(trunk, trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001532 llist_add_tail(&trunk->entry, &cfg->trunks);
Stefan Sperling1e174872018-10-25 18:36:10 +02001533
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001534 return trunk;
1535}
1536
Philipp Maier87bd9be2017-08-22 16:35:41 +02001537/*! get trunk configuration by trunk number (index).
1538 * \param[in] cfg mgcp configuration
1539 * \param[in] index trunk number
1540 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001541struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1542{
1543 struct mgcp_trunk_config *trunk;
1544
1545 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001546 if (trunk->trunk_nr == index)
1547 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001548
1549 return NULL;
1550}
1551
Philipp Maier87bd9be2017-08-22 16:35:41 +02001552/*! allocate endpoints and set default values.
1553 * (called once at startup by VTY)
1554 * \param[in] tcfg trunk configuration
1555 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001556int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1557{
1558 int i;
1559
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001560 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001561 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001562 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001563 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001564 if (!tcfg->endpoints)
1565 return -1;
1566
Philipp Maierfcd06552017-11-10 17:32:22 +01001567 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001568 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001569 tcfg->endpoints[i].cfg = tcfg->cfg;
1570 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001571
1572 /* NOTE: Currently all endpoints are of type RTP, this will
1573 * change when new variations are implemented */
1574 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001575 }
1576
Philipp Maierfcd06552017-11-10 17:32:22 +01001577 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Stefan Sperling1e174872018-10-25 18:36:10 +02001578 alloc_mgcp_crxc_rate_counters(tcfg, tcfg->cfg);
1579
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001580 return 0;
1581}
1582
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001583static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1584{
1585 return write(cfg->gw_fd.bfd.fd, buf, len);
1586}
1587
Philipp Maier87bd9be2017-08-22 16:35:41 +02001588/*! Reset all endpoints by sending RSIP message to self.
1589 * (called by VTY)
1590 * \param[in] endp trunk endpoint
1591 * \param[in] endpoint number
1592 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001593int mgcp_send_reset_all(struct mgcp_config *cfg)
1594{
Philipp Maier12943ea2018-01-17 15:40:25 +01001595 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1596 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001597 int rc;
1598
Philipp Maier12943ea2018-01-17 15:40:25 +01001599 len = snprintf(buf, sizeof(buf),
1600 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1601 if (len < 0)
1602 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001603
Philipp Maier12943ea2018-01-17 15:40:25 +01001604 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001605 if (rc <= 0)
1606 return -1;
1607
1608 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001609}
1610
Philipp Maier87bd9be2017-08-22 16:35:41 +02001611/*! Reset a single endpoint by sending RSIP message to self.
1612 * (called by VTY)
1613 * \param[in] endp trunk endpoint
1614 * \param[in] endpoint number
1615 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001616int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1617{
Philipp Maier12943ea2018-01-17 15:40:25 +01001618 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001619 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001620 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001621
1622 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001623 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001624 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001625 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001626
Philipp Maier87bd9be2017-08-22 16:35:41 +02001627 rc = send_agent(endp->cfg, buf, len);
1628 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001629 return -1;
1630
Philipp Maier87bd9be2017-08-22 16:35:41 +02001631 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001632}