blob: bc35e5c1d922b53c751e52d9f42abb3724091796 [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."},
67};
68
69const static struct rate_ctr_group_desc mgcp_crcx_ctr_group_desc = {
70 .group_name_prefix = "crcx",
71 .group_description = "crxc statistics",
72 .class_id = OSMO_STATS_CLASS_GLOBAL,
73 .num_ctr = ARRAY_SIZE(mgcp_crcx_ctr_desc),
74 .ctr_desc = mgcp_crcx_ctr_desc
75};
76
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020077static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data);
78static struct msgb *handle_create_con(struct mgcp_parse_data *data);
79static struct msgb *handle_delete_con(struct mgcp_parse_data *data);
80static struct msgb *handle_modify_con(struct mgcp_parse_data *data);
81static struct msgb *handle_rsip(struct mgcp_parse_data *data);
82static struct msgb *handle_noti_req(struct mgcp_parse_data *data);
83
Philipp Maier87bd9be2017-08-22 16:35:41 +020084/* Initalize transcoder */
85static int setup_rtp_processing(struct mgcp_endpoint *endp,
86 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020087{
Philipp Maier87bd9be2017-08-22 16:35:41 +020088 struct mgcp_config *cfg = endp->cfg;
89 struct mgcp_conn_rtp *conn_src = NULL;
90 struct mgcp_conn_rtp *conn_dst = conn;
91 struct mgcp_conn *_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020092
Philipp Maier87bd9be2017-08-22 16:35:41 +020093 if (conn->type != MGCP_RTP_DEFAULT) {
94 LOGP(DLMGCP, LOGL_NOTICE,
95 "endpoint:%x RTP-setup: Endpoint is not configured as RTP default, stopping here!\n",
96 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020097 return 0;
98 }
99
Philipp Maier87bd9be2017-08-22 16:35:41 +0200100 if (conn->conn->mode == MGCP_CONN_LOOPBACK) {
101 LOGP(DLMGCP, LOGL_NOTICE,
102 "endpoint:%x RTP-setup: Endpoint is in loopback mode, stopping here!\n",
103 ENDPOINT_NUMBER(endp));
104 return 0;
105 }
106
107 /* Find the "sister" connection */
108 llist_for_each_entry(_conn, &endp->conns, entry) {
109 if (_conn->id != conn->conn->id) {
110 conn_src = &_conn->u.rtp;
111 break;
112 }
113 }
114
Philipp Maieracc10352018-07-19 18:07:57 +0200115 return cfg->setup_rtp_processing_cb(endp, conn_dst, conn_src);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200116}
117
Philipp Maier87bd9be2017-08-22 16:35:41 +0200118/* array of function pointers for handling various
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200119 * messages. In the future this might be binary sorted
Philipp Maier87bd9be2017-08-22 16:35:41 +0200120 * for performance reasons. */
121static const struct mgcp_request mgcp_requests[] = {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200122 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
Philipp Maier87bd9be2017-08-22 16:35:41 +0200123 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
124 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
125 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
126 MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200127
Philipp Maier87bd9be2017-08-22 16:35:41 +0200128 /* SPEC extension */
129 MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200130};
131
Philipp Maier87bd9be2017-08-22 16:35:41 +0200132/* Helper function to allocate some memory for responses and retransmissions */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200133static struct msgb *mgcp_msgb_alloc(void)
134{
135 struct msgb *msg;
136 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
137 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200138 LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200139
140 return msg;
141}
142
Philipp Maier87bd9be2017-08-22 16:35:41 +0200143/* Helper function for do_retransmission() and create_resp() */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200144static struct msgb *do_retransmission(const struct mgcp_endpoint *endp)
145{
146 struct msgb *msg = mgcp_msgb_alloc();
147 if (!msg)
148 return NULL;
149
150 msg->l2h = msgb_put(msg, strlen(endp->last_response));
151 memcpy(msg->l2h, endp->last_response, msgb_l2len(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200152 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200153 return msg;
154}
155
156static struct msgb *create_resp(struct mgcp_endpoint *endp, int code,
157 const char *txt, const char *msg,
158 const char *trans, const char *param,
159 const char *sdp)
160{
161 int len;
162 struct msgb *res;
163
164 res = mgcp_msgb_alloc();
165 if (!res)
166 return NULL;
167
Philipp Maier87bd9be2017-08-22 16:35:41 +0200168 len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s",
169 code, trans, txt, param ? param : "", sdp ? sdp : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200170 if (len < 0) {
171 LOGP(DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n");
172 msgb_free(res);
173 return NULL;
174 }
175
176 res->l2h = msgb_put(res, len);
177 LOGP(DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200178 mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200179
180 /*
181 * Remember the last transmission per endpoint.
182 */
183 if (endp) {
184 struct mgcp_trunk_config *tcfg = endp->tcfg;
185 talloc_free(endp->last_response);
186 talloc_free(endp->last_trans);
187 endp->last_trans = talloc_strdup(tcfg->endpoints, trans);
188 endp->last_response = talloc_strndup(tcfg->endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189 (const char *)res->l2h,
190 msgb_l2len(res));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200191 }
192
193 return res;
194}
195
196static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200197 int code, const char *msg,
198 const char *trans,
199 const char *param)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200200{
201 return create_resp(endp, code, " OK", msg, trans, param, NULL);
202}
203
204static struct msgb *create_ok_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200205 int code, const char *msg,
206 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200207{
208 return create_ok_resp_with_param(endp, code, msg, trans, NULL);
209}
210
211static struct msgb *create_err_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200212 int code, const char *msg,
213 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200214{
215 return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL);
216}
217
Philipp Maier55295f72018-01-15 14:00:28 +0100218/* Add MGCP parameters to a message buffer */
219static int add_params(struct msgb *msg, const struct mgcp_endpoint *endp,
220 const struct mgcp_conn_rtp *conn)
221{
222 int rc;
223
Philipp Maier7f0966c2018-01-17 18:18:12 +0100224 /* NOTE: Only in the virtual trunk we allow dynamic endpoint names */
Philipp Maier207ab512018-02-02 14:19:26 +0100225 if (endp->wildcarded_req
Philipp Maier7f0966c2018-01-17 18:18:12 +0100226 && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) {
Philipp Maierdd4ede32018-02-01 17:52:52 +0100227 rc = msgb_printf(msg, "Z: %s%x@%s\r\n",
Philipp Maier7f0966c2018-01-17 18:18:12 +0100228 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
229 ENDPOINT_NUMBER(endp), endp->cfg->domain);
Philipp Maier55295f72018-01-15 14:00:28 +0100230 if (rc < 0)
231 return -EINVAL;
232 }
233
Philipp Maierc3cfae22018-01-22 12:03:03 +0100234 rc = msgb_printf(msg, "I: %s\r\n", conn->conn->id);
Philipp Maier55295f72018-01-15 14:00:28 +0100235 if (rc < 0)
236 return -EINVAL;
237
238 return 0;
239}
240
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241/* Format MGCP response string (with SDP attached) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200243 struct mgcp_conn_rtp *conn,
244 const char *msg,
Philipp Maier55295f72018-01-15 14:00:28 +0100245 const char *trans_id,
246 bool add_conn_params)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200247{
248 const char *addr = endp->cfg->local_ip;
Philipp Maier8970c492017-10-11 13:33:42 +0200249 struct msgb *sdp;
250 int rc;
251 struct msgb *result;
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100252 char osmux_extension[strlen("X-Osmux: 255") + 1];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100253 char local_ip_addr[INET_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200254
Philipp Maier8970c492017-10-11 13:33:42 +0200255 sdp = msgb_alloc_headroom(4096, 128, "sdp record");
256 if (!sdp)
257 return NULL;
258
Philipp Maier1cb1e382017-11-02 17:16:04 +0100259 if (!addr) {
260 mgcp_get_local_addr(local_ip_addr, conn);
261 addr = local_ip_addr;
262 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200263
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100265 sprintf(osmux_extension, "X-Osmux: %u", conn->osmux.cid);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200266 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200267 } else {
268 osmux_extension[0] = '\0';
269 }
270
Philipp Maier55295f72018-01-15 14:00:28 +0100271 /* Attach optional connection parameters */
272 if (add_conn_params) {
273 rc = add_params(sdp, endp, conn);
274 if (rc < 0)
275 goto error;
276 }
277
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100278 /* Attach optional OSMUX parameters */
279 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
Philipp Maierc3cfae22018-01-22 12:03:03 +0100280 rc = msgb_printf(sdp, "%s\r\n", osmux_extension);
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100281 if (rc < 0)
282 goto error;
283 }
284
285 /* Attach line break to separate the parameters from the SDP block */
Philipp Maierc3cfae22018-01-22 12:03:03 +0100286 rc = msgb_printf(sdp, "\r\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200287
Philipp Maier8970c492017-10-11 13:33:42 +0200288 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
289 if (rc < 0)
290 goto error;
291 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
292 msgb_free(sdp);
293 return result;
294error:
295 msgb_free(sdp);
296 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200297}
298
Philipp Maier87bd9be2017-08-22 16:35:41 +0200299/* Send out dummy packet to keep the connection open, if the connection is an
300 * osmux connection, send the dummy packet via OSMUX */
301static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200302{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200303 if (conn->osmux.state != OSMUX_STATE_DISABLED)
304 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200305 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200306 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200307}
308
Philipp Maier87bd9be2017-08-22 16:35:41 +0200309/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200310 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200311 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200312struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
313{
314 struct mgcp_parse_data pdata;
Harald Weltee35eeae2017-12-28 13:47:37 +0100315 int rc, i, code, handled = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200316 struct msgb *resp = NULL;
317 char *data;
318
319 if (msgb_l2len(msg) < 4) {
320 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
321 return NULL;
322 }
323
324 if (mgcp_msg_terminate_nul(msg))
325 return NULL;
326
Philipp Maier87bd9be2017-08-22 16:35:41 +0200327 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200328
Philipp Maier87bd9be2017-08-22 16:35:41 +0200329 /* attempt to treat it as a response */
330 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200331 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
332 return NULL;
333 }
334
335 msg->l3h = &msg->l2h[4];
336
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200337 /*
338 * Check for a duplicate message and respond.
339 */
340 memset(&pdata, 0, sizeof(pdata));
341 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200342 data = mgcp_strline((char *)msg->l3h, &pdata.save);
Harald Weltee35eeae2017-12-28 13:47:37 +0100343 rc = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200344 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200345 && pdata.endp->last_trans
346 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200347 return do_retransmission(pdata.endp);
348 }
349
Harald Welteabbb6b92017-12-28 13:13:50 +0100350 /* check for general parser failure */
Harald Weltee35eeae2017-12-28 13:47:37 +0100351 if (rc < 0) {
Harald Welteabbb6b92017-12-28 13:13:50 +0100352 LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h);
Harald Weltee35eeae2017-12-28 13:47:37 +0100353 return create_err_response(NULL, -rc, (const char *) msg->l2h, pdata.trans);
Harald Welteabbb6b92017-12-28 13:13:50 +0100354 }
355
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200356 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200357 if (strncmp
358 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
359 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200360 handled = 1;
361 resp = mgcp_requests[i].handle_request(&pdata);
362 break;
363 }
364 }
365
366 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200367 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
368 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200369
370 return resp;
371}
372
Philipp Maier87bd9be2017-08-22 16:35:41 +0200373/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200374static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
375{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200376 LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100377 return create_ok_response(p->endp, 200, "AUEP", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378}
379
Harald Welte1d1b98f2017-12-25 10:03:40 +0100380/* Try to find a free port by attempting to bind on it. Also handle the
Philipp Maier87bd9be2017-08-22 16:35:41 +0200381 * counter that points on the next free port. Since we have a pointer
Philipp Maierb38fb892018-05-22 13:52:21 +0200382 * to the next free port, binding should in work on the first attempt in
383 * general. In case of failure the next port is tryed until the whole port
384 * range is tryed once. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200385static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200386{
387 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200388 struct mgcp_rtp_end *end;
389 struct mgcp_port_range *range;
Philipp Maierb38fb892018-05-22 13:52:21 +0200390 unsigned int tries;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200391
Philipp Maier87bd9be2017-08-22 16:35:41 +0200392 OSMO_ASSERT(conn);
393 end = &conn->end;
394 OSMO_ASSERT(end);
395
396 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200397
398 /* attempt to find a port */
Philipp Maierb38fb892018-05-22 13:52:21 +0200399 tries = (range->range_end - range->range_start) / 2;
400 for (i = 0; i < tries; ++i) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200401 int rc;
402
403 if (range->last_port >= range->range_end)
404 range->last_port = range->range_start;
405
Philipp Maier87bd9be2017-08-22 16:35:41 +0200406 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200407
408 range->last_port += 2;
409 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200410 return 0;
411 }
412
413 }
414
Philipp Maier87bd9be2017-08-22 16:35:41 +0200415 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maierb38fb892018-05-22 13:52:21 +0200416 "Allocating a RTP/RTCP port failed %u times 0x%x.\n",
417 tries, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200418 return -1;
419}
420
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200421/*! Helper function for check_local_cx_options() to get a pointer of the next
422 * lco option identifier
423 * \param[in] lco string
424 * \returns pointer to the beginning of the LCO identifier, NULL on failure */
425char *get_lco_identifier(const char *options)
426{
427 char *ptr;
428 unsigned int count = 0;
429
430 /* Jump to the end of the lco identifier */
431 ptr = strstr(options, ":");
432 if (!ptr)
433 return NULL;
434
435 /* Walk backwards until the pointer points to the beginning of the
436 * lco identifier. We know that we stand at the beginning when we
437 * are either at the beginning of the memory or see a space or
438 * comma. (this is tolerant, it will accept a:10, b:11 as well as
439 * a:10,b:11) */
440 while (1) {
441 /* Endless loop protection */
442 if (count > 10000)
443 return NULL;
444 else if (ptr < options || *ptr == ' ' || *ptr == ',') {
445 ptr++;
446 break;
447 }
448 ptr--;
449 count++;
450 }
451
452 /* Check if we got any result */
453 if (*ptr == ':')
454 return NULL;
455
456 return ptr;
457}
458
459/*! Check the LCO option. This function checks for multiple appearence of LCO
460 * options, which is illegal
461 * \param[in] ctx talloc context
462 * \param[in] lco string
463 * \returns 0 on success, -1 on failure */
464int check_local_cx_options(void *ctx, const char *options)
465{
466 int i;
467 char *options_copy;
468 char *lco_identifier;
469 char *lco_identifier_end;
470 char *next_lco_identifier;
471
472 char **lco_seen;
473 unsigned int lco_seen_n = 0;
474
475 if (!options)
476 return -1;
477
478 lco_seen =
479 (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *));
480 options_copy = talloc_strdup(ctx, options);
481 lco_identifier = options_copy;
482
483 do {
484 /* Move the lco_identifier pointer to the beginning of the
485 * current lco option identifier */
486 lco_identifier = get_lco_identifier(lco_identifier);
487 if (!lco_identifier)
488 goto error;
489
490 /* Look ahead to the next LCO option early, since we
491 * will parse destructively */
492 next_lco_identifier = strstr(lco_identifier + 1, ",");
493
494 /* Pinch off the end of the lco field identifier name
495 * and see if we still got something, also check if
496 * there is some value after the colon. */
497 lco_identifier_end = strstr(lco_identifier, ":");
498 if (!lco_identifier_end)
499 goto error;
500 if (*(lco_identifier_end + 1) == ' '
501 || *(lco_identifier_end + 1) == ','
502 || *(lco_identifier_end + 1) == '\0')
503 goto error;
504 *lco_identifier_end = '\0';
505 if (strlen(lco_identifier) == 0)
506 goto error;
507
508 /* Check if we have already seen the current field identifier
509 * before. If yes, we must bail, an LCO must only appear once
510 * in the LCO string */
511 for (i = 0; i < lco_seen_n; i++) {
512 if (strcmp(lco_seen[i], lco_identifier) == 0)
513 goto error;
514 }
515 lco_seen[lco_seen_n] = lco_identifier;
516 lco_seen_n++;
517
518 /* The first identifier must always be found at the beginnning
519 * of the LCO string */
520 if (lco_seen[0] != options_copy)
521 goto error;
522
523 /* Go to the next lco option */
524 lco_identifier = next_lco_identifier;
525 } while (lco_identifier);
526
527 talloc_free(lco_seen);
528 talloc_free(options_copy);
529 return 0;
530error:
531 talloc_free(lco_seen);
532 talloc_free(options_copy);
533 return -1;
534}
535
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200536/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100537 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200538 * like an empty string, the 'string' field is never NULL after this function
539 * has been called. */
Philipp Maiera390d0b2018-01-31 17:30:19 +0100540static int set_local_cx_options(void *ctx, struct mgcp_lco *lco,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200541 const char *options)
542{
543 char *p_opt, *a_opt;
544 char codec[9];
545
Philipp Maier604410c2018-06-06 10:02:16 +0200546 if (!options)
547 return 0;
548 if (strlen(options) == 0)
549 return 0;
550
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200551 /* Make sure the encoding of the LCO is consistant before we proceed */
552 if (check_local_cx_options(ctx, options) != 0) {
553 LOGP(DLMGCP, LOGL_ERROR,
554 "local CX options: Internal inconsistency in Local Connection Options!\n");
555 return 524;
556 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200557
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200558 talloc_free(lco->string);
559 lco->string = talloc_strdup(ctx, options);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200560
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200561 p_opt = strstr(lco->string, "p:");
562 if (p_opt && sscanf(p_opt, "p:%d-%d",
563 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
564 lco->pkt_period_max = lco->pkt_period_min;
565
Philipp Maierbc0346e2018-06-07 09:52:16 +0200566 /* FIXME: LCO also supports the negotiation of more then one codec.
567 * (e.g. a:PCMU;G726-32) But this implementation only supports a single
568 * codec only. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200569 a_opt = strstr(lco->string, "a:");
Philipp Maier604410c2018-06-06 10:02:16 +0200570 if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1) {
571 talloc_free(lco->codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200572 lco->codec = talloc_strdup(ctx, codec);
Philipp Maier604410c2018-06-06 10:02:16 +0200573 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200574
575 LOGP(DLMGCP, LOGL_DEBUG,
576 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
577 lco->pkt_period_max, lco->codec);
Philipp Maiera390d0b2018-01-31 17:30:19 +0100578
579 /* Check if the packetization fits the 20ms raster */
580 if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) {
581 LOGP(DLMGCP, LOGL_ERROR,
582 "local CX options: packetization interval is not a multiple of 20ms!\n");
583 return 535;
584 }
585
586 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200587}
588
589void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
590 struct mgcp_rtp_end *rtp)
591{
592 struct mgcp_trunk_config *tcfg = endp->tcfg;
593
594 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
595
596 rtp->force_aligned_timing = tcfg->force_aligned_timing;
597 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
598
599 LOGP(DLMGCP, LOGL_DEBUG,
600 "Configuring RTP endpoint: local port %d%s%s\n",
601 ntohs(rtp->rtp_port),
602 rtp->force_aligned_timing ? ", force constant timing" : "",
603 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
604}
605
606uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
607 struct mgcp_rtp_end *rtp)
608{
609 int f = 0;
610
611 /* Get the number of frames per channel and packet */
612 if (rtp->frames_per_packet)
613 f = rtp->frames_per_packet;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200614 else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) {
615 int den = 1000 * rtp->codec->frame_duration_num;
616 f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den +
Philipp Maier87bd9be2017-08-22 16:35:41 +0200617 den / 2)
618 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200619 }
620
Philipp Maierbc0346e2018-06-07 09:52:16 +0200621 return rtp->codec->rate * f * rtp->codec->frame_duration_num /
622 rtp->codec->frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200623}
624
625static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
626{
627 if (!endp->cfg->osmux_init) {
628 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
629 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
630 return -1;
631 }
632 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
633 }
634
635 return mgcp_parse_osmux_cid(line);
636}
637
Philipp Maierbc0346e2018-06-07 09:52:16 +0200638/* Process codec information contained in CRCX/MDCX */
639static int handle_codec_info(struct mgcp_conn_rtp *conn,
640 struct mgcp_parse_data *p, int have_sdp, bool crcx)
641{
642 struct mgcp_endpoint *endp = p->endp;
643 int rc;
644 char *cmd;
645
646 if (crcx)
647 cmd = "CRCX";
648 else
649 cmd = "MDCX";
650
651 /* Collect codec information */
652 if (have_sdp) {
653 /* If we have SDP, we ignore the local connection options and
654 * use only the SDP information. */
655 mgcp_codec_reset_all(conn);
656 rc = mgcp_parse_sdp_data(endp, conn, p);
657 if (rc != 0) {
658 LOGP(DLMGCP, LOGL_ERROR,
659 "%s: endpoint:%x sdp not parseable\n", cmd,
660 ENDPOINT_NUMBER(endp));
661
662 /* See also RFC 3661: Protocol error */
663 return 510;
664 }
665 } else if (endp->local_options.codec) {
666 /* When no SDP is available, we use the codec information from
667 * the local connection options (if present) */
668 mgcp_codec_reset_all(conn);
669 rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec);
670 if (rc != 0)
671 goto error;
672 }
673
674 /* Make sure we always set a sane default codec */
675 if (conn->end.codecs_assigned == 0) {
676 /* When SDP and/or LCO did not supply any codec information,
677 * than it makes sense to pick a sane default: (payload-type 0,
678 * PCMU), see also: OS#2658 */
679 mgcp_codec_reset_all(conn);
680 rc = mgcp_codec_add(conn, 0, NULL);
681 if (rc != 0)
682 goto error;
683 }
684
685 /* Make codec decision */
686 if (mgcp_codec_decide(conn) != 0)
687 goto error;
688
689 return 0;
690
691error:
692 LOGP(DLMGCP, LOGL_ERROR,
693 "%s: endpoint:0x%x codec negotiation failure\n", cmd,
694 ENDPOINT_NUMBER(endp));
695
696 /* See also RFC 3661: Codec negotiation failure */
697 return 534;
698}
699
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200700static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line)
701{
702 char *saveptr = NULL;
703
704 if (strncmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER)))
705 return false;
706 line += strlen(MGCP_X_OSMO_IGN_HEADER);
707
708 while (1) {
709 char *token = strtok_r(line, " ", &saveptr);
710 line = NULL;
711 if (!token)
712 break;
713
714 if (!strcmp(token, "C"))
715 endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID;
716 else
Pau Espin Pedrol9ecceb62018-10-16 15:35:58 +0200717 LOGP(DLMGCP, LOGL_ERROR, "endpoint 0x%x: received unknown X-Osmo-IGN item '%s'\n",
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200718 ENDPOINT_NUMBER(endp), token);
719 }
720
721 return true;
722}
723
Philipp Maier87bd9be2017-08-22 16:35:41 +0200724/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200725static struct msgb *handle_create_con(struct mgcp_parse_data *p)
726{
Stefan Sperling1e174872018-10-25 18:36:10 +0200727 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200728 struct mgcp_endpoint *endp = p->endp;
729 int error_code = 400;
730
731 const char *local_options = NULL;
732 const char *callid = NULL;
733 const char *mode = NULL;
734 char *line;
735 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200736 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100737 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200738 char conn_name[512];
Philipp Maiera390d0b2018-01-31 17:30:19 +0100739 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200740
741 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200742
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200743 /* parse CallID C: and LocalParameters L: */
744 for_each_line(line, p->save) {
745 if (!mgcp_check_param(endp, line))
746 continue;
747
748 switch (line[0]) {
749 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200750 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200751 break;
752 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200753 callid = (const char *)line + 3;
754 break;
755 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100756 /* It is illegal to send a connection identifier
757 * together with a CRCX, the MGW will assign the
758 * connection identifier by itself on CRCX */
Stefan Sperling1e174872018-10-25 18:36:10 +0200759 rate_ctr_inc(&tcfg->mgcp_crcx_ctr_group->ctr[MGCP_CRCX_FAIL_BAD_ACTION]);
Philipp Maierffd75e42017-11-22 11:44:50 +0100760 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200761 break;
762 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200763 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200764 break;
765 case 'X':
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200766 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
767 /* If osmux is disabled, just skip setting it up */
768 if (!p->endp->cfg->osmux)
769 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200770 osmux_cid = mgcp_osmux_setup(endp, line);
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200771 break;
772 }
773
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200774 if (parse_x_osmo_ign(endp, line))
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200775 break;
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200776
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200777 /* Ignore unknown X-headers */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200778 break;
779 case '\0':
780 have_sdp = 1;
781 goto mgcp_header_done;
782 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200783 LOGP(DLMGCP, LOGL_NOTICE,
784 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
785 ENDPOINT_NUMBER(endp), *line, *line);
Stefan Sperling1e174872018-10-25 18:36:10 +0200786 rate_ctr_inc(&tcfg->mgcp_crcx_ctr_group->ctr[MGCP_CRCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100787 return create_err_response(NULL, 539, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200788 break;
789 }
790 }
791
792mgcp_header_done:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200793 /* Check parameters */
794 if (!callid) {
795 LOGP(DLMGCP, LOGL_ERROR,
796 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200797 ENDPOINT_NUMBER(endp));
Stefan Sperling1e174872018-10-25 18:36:10 +0200798 rate_ctr_inc(&tcfg->mgcp_crcx_ctr_group->ctr[MGCP_CRCX_FAIL_MISSING_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100799 return create_err_response(endp, 516, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200800 }
801
Philipp Maier87bd9be2017-08-22 16:35:41 +0200802 if (!mode) {
803 LOGP(DLMGCP, LOGL_ERROR,
804 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
805 ENDPOINT_NUMBER(endp));
Stefan Sperling1e174872018-10-25 18:36:10 +0200806 rate_ctr_inc(&tcfg->mgcp_crcx_ctr_group->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100807 return create_err_response(endp, 517, "CRCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200808 }
809
Philipp Maier87bd9be2017-08-22 16:35:41 +0200810 /* Check if we are able to accept the creation of another connection */
811 if (llist_count(&endp->conns) >= endp->type->max_conns) {
812 LOGP(DLMGCP, LOGL_ERROR,
813 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
814 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200815 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200816 /* There is no more room for a connection, make some
817 * room by blindly tossing the oldest of the two two
818 * connections */
819 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200820 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821 /* There is no more room for a connection, leave
822 * everything as it is and return with an error */
Stefan Sperling1e174872018-10-25 18:36:10 +0200823 rate_ctr_inc(&tcfg->mgcp_crcx_ctr_group->ctr[MGCP_CRCX_FAIL_LIMIT_EXCEEDED]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100824 return create_err_response(endp, 540, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200825 }
826 }
827
Philipp Maier87bd9be2017-08-22 16:35:41 +0200828 /* Check if this endpoint already serves a call, if so, check if the
829 * callids match up so that we are sure that this is our call */
830 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
831 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100832 "CRCX: endpoint:0x%x allready seized by other call (%s)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200833 ENDPOINT_NUMBER(endp), endp->callid);
834 if (tcfg->force_realloc)
835 /* This is not our call, toss everything by releasing
836 * the entire endpoint. (rude!) */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100837 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200838 else {
839 /* This is not our call, leave everything as it is and
840 * return with an error. */
Stefan Sperling1e174872018-10-25 18:36:10 +0200841 rate_ctr_inc(&tcfg->mgcp_crcx_ctr_group->ctr[MGCP_CRCX_FAIL_UNKNOWN_CALLID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200842 return create_err_response(endp, 400, "CRCX", p->trans);
843 }
844 }
845
846 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100847 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200848 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200849 endp->callid = talloc_strdup(tcfg->endpoints, callid);
850
Philipp Maierffd75e42017-11-22 11:44:50 +0100851 snprintf(conn_name, sizeof(conn_name), "%s", callid);
852 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
853 if (!_conn) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200854 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100855 "CRCX: endpoint:0x%x unable to allocate RTP connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200856 ENDPOINT_NUMBER(endp));
Stefan Sperling1e174872018-10-25 18:36:10 +0200857 rate_ctr_inc(&tcfg->mgcp_crcx_ctr_group->ctr[MGCP_CRCX_FAIL_ALLOC_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858 goto error2;
859
Philipp Maier87bd9be2017-08-22 16:35:41 +0200860 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100861 conn = mgcp_conn_get_rtp(endp, _conn->id);
862 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200863
864 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
865 error_code = 517;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200866 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200867 }
868
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200869 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200870 * is fully set up from the dummy load. */
871 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200872 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 conn->osmux.cid = osmux_cid;
874 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200875 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
876 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100877 "CRCX: endpoint:0x%x osmux only and no osmux offered\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200878 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200879 goto error2;
880 }
881
Philipp Maierbc0346e2018-06-07 09:52:16 +0200882 /* Set local connection options, if present */
883 if (local_options) {
884 rc = set_local_cx_options(endp->tcfg->endpoints,
885 &endp->local_options, local_options);
886 if (rc != 0) {
887 LOGP(DLMGCP, LOGL_ERROR,
888 "CRCX: endpoint:%x inavlid local connection options!\n",
889 ENDPOINT_NUMBER(endp));
890 error_code = rc;
891 goto error2;
892 }
893 }
894
895 /* Handle codec information and decide for a suitable codec */
896 rc = handle_codec_info(conn, p, have_sdp, true);
897 mgcp_codec_summary(conn);
898 if (rc) {
899 error_code = rc;
900 goto error2;
901 }
902
Philipp Maier87bd9be2017-08-22 16:35:41 +0200903 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
904 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200905
Philipp Maier87bd9be2017-08-22 16:35:41 +0200906 if (p->cfg->force_ptime) {
907 conn->end.packet_duration_ms = p->cfg->force_ptime;
908 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200909 }
910
Philipp Maier1cb1e382017-11-02 17:16:04 +0100911 mgcp_rtp_end_config(endp, 0, &conn->end);
912
Philipp Maierc3cc6542018-02-02 12:58:42 +0100913 /* check connection mode setting */
914 if (conn->conn->mode != MGCP_CONN_LOOPBACK
915 && conn->conn->mode != MGCP_CONN_RECV_ONLY
916 && conn->end.rtp_port == 0) {
917 LOGP(DLMGCP, LOGL_ERROR,
918 "CRCX: endpoint:%x selected connection mode type requires an opposite end!\n",
919 ENDPOINT_NUMBER(endp));
920 error_code = 527;
Stefan Sperling1e174872018-10-25 18:36:10 +0200921 rate_ctr_inc(&tcfg->mgcp_crcx_ctr_group->ctr[MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +0100922 goto error2;
923 }
924
Philipp Maier1cb1e382017-11-02 17:16:04 +0100925 if (allocate_port(endp, conn) != 0) {
926 goto error2;
927 }
928
Philipp Maier87bd9be2017-08-22 16:35:41 +0200929 if (setup_rtp_processing(endp, conn) != 0) {
930 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100931 "CRCX: endpoint:0x%x could not start RTP processing!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200932 ENDPOINT_NUMBER(endp));
Stefan Sperling1e174872018-10-25 18:36:10 +0200933 rate_ctr_inc(&tcfg->mgcp_crcx_ctr_group->ctr[MGCP_CRCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200934 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200935 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200936
937 /* policy CB */
938 if (p->cfg->policy_cb) {
939 int rc;
940 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200941 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200942 switch (rc) {
943 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200944 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100945 "CRCX: endpoint:0x%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200946 ENDPOINT_NUMBER(endp));
Philipp Maier1355d7e2018-02-01 14:30:06 +0100947 mgcp_endp_release(endp);
Stefan Sperling1e174872018-10-25 18:36:10 +0200948 rate_ctr_inc(&tcfg->mgcp_crcx_ctr_group->ctr[MGCP_CRCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200949 return create_err_response(endp, 400, "CRCX", p->trans);
950 break;
951 case MGCP_POLICY_DEFER:
952 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200953 return NULL;
954 break;
955 case MGCP_POLICY_CONT:
956 /* just continue */
957 break;
958 }
959 }
960
Philipp Maier87bd9be2017-08-22 16:35:41 +0200961 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100962 "CRCX: endpoint:0x%x Creating connection: CI: %s port: %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200963 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200964 if (p->cfg->change_cb)
965 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
966
Philipp Maiere726d4f2017-11-01 10:41:34 +0100967 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
968 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200969 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +0100970 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200971 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200972
Philipp Maier87bd9be2017-08-22 16:35:41 +0200973 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100974 "CRCX: endpoint:0x%x connection successfully created\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200975 ENDPOINT_NUMBER(endp));
Stefan Sperling1e174872018-10-25 18:36:10 +0200976 rate_ctr_inc(&tcfg->mgcp_crcx_ctr_group->ctr[MGCP_CRCX_SUCCESS]);
Philipp Maier55295f72018-01-15 14:00:28 +0100977 return create_response_with_sdp(endp, conn, "CRCX", p->trans, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200978error2:
Philipp Maier1355d7e2018-02-01 14:30:06 +0100979 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200980 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier3c8ccb62018-06-04 09:59:24 +0200981 "CRCX: endpoint:0x%x unable to create connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200982 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200983 return create_err_response(endp, error_code, "CRCX", p->trans);
984}
985
Philipp Maierbc0346e2018-06-07 09:52:16 +0200986
987
988
989
Philipp Maier87bd9be2017-08-22 16:35:41 +0200990/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200991static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
992{
993 struct mgcp_endpoint *endp = p->endp;
994 int error_code = 500;
995 int silent = 0;
996 int have_sdp = 0;
997 char *line;
998 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200999 const char *mode = NULL;
1000 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +01001001 const char *conn_id = NULL;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001002 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001003
1004 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001005
Philipp Maier5656fbf2018-02-02 14:41:58 +01001006 /* Prohibit wildcarded requests */
1007 if (endp->wildcarded_req) {
1008 LOGP(DLMGCP, LOGL_ERROR,
1009 "MDCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
1010 ENDPOINT_NUMBER(endp));
1011 return create_err_response(endp, 507, "MDCX", p->trans);
1012 }
1013
Philipp Maier87bd9be2017-08-22 16:35:41 +02001014 if (llist_count(&endp->conns) <= 0) {
1015 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001016 "MDCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001017 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001018 return create_err_response(endp, 400, "MDCX", p->trans);
1019 }
1020
1021 for_each_line(line, p->save) {
1022 if (!mgcp_check_param(endp, line))
1023 continue;
1024
1025 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001026 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001027 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1028 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001029 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001030 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001031 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001032 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001033 conn_id = (const char *)line + 3;
Neels Hofmeyreb72ff02018-09-03 23:00:07 +02001034 if ((error_code = mgcp_verify_ci(endp, conn_id)))
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001035 goto error3;
1036 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001037 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001038 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001039 break;
1040 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001041 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001042 break;
1043 case 'Z':
1044 silent = strcmp("noanswer", line + 3) == 0;
1045 break;
1046 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001047 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001048 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001049 break;
1050 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001051 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001052 "MDCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001053 ENDPOINT_NUMBER(endp), line[0], line[0]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001054 return create_err_response(NULL, 539, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001055 break;
1056 }
1057 }
1058
Philipp Maier87bd9be2017-08-22 16:35:41 +02001059mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +01001060 if (!conn_id) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001061 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001062 "MDCX: endpoint:0x%x insufficient parameters, missing ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001063 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +01001064 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001065 }
1066
1067 conn = mgcp_conn_get_rtp(endp, conn_id);
1068 if (!conn)
1069 return create_err_response(endp, 400, "MDCX", p->trans);
1070
1071 if (mode) {
1072 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
1073 error_code = 517;
1074 goto error3;
1075 }
1076 } else
1077 conn->conn->mode = conn->conn->mode_orig;
1078
Philipp Maierbc0346e2018-06-07 09:52:16 +02001079 /* Set local connection options, if present */
1080 if (local_options) {
1081 rc = set_local_cx_options(endp->tcfg->endpoints,
1082 &endp->local_options, local_options);
1083 if (rc != 0) {
1084 LOGP(DLMGCP, LOGL_ERROR,
1085 "MDCX: endpoint:%x inavlid local connection options!\n",
1086 ENDPOINT_NUMBER(endp));
1087 error_code = rc;
1088 goto error3;
1089 }
1090 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001091
Philipp Maierbc0346e2018-06-07 09:52:16 +02001092 /* Handle codec information and decide for a suitable codec */
1093 rc = handle_codec_info(conn, p, have_sdp, false);
1094 mgcp_codec_summary(conn);
1095 if (rc) {
Philipp Maieraf07f662018-02-02 11:34:02 +01001096 error_code = rc;
1097 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001098 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001099
Philipp Maierc3cc6542018-02-02 12:58:42 +01001100 /* check connection mode setting */
1101 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1102 && conn->conn->mode != MGCP_CONN_RECV_ONLY
1103 && conn->end.rtp_port == 0) {
1104 LOGP(DLMGCP, LOGL_ERROR,
1105 "MDCX: endpoint:%x selected connection mode type requires an opposite end!\n",
1106 ENDPOINT_NUMBER(endp));
1107 error_code = 527;
1108 goto error3;
1109 }
1110
Philipp Maierbc0346e2018-06-07 09:52:16 +02001111
Philipp Maier87bd9be2017-08-22 16:35:41 +02001112 if (setup_rtp_processing(endp, conn) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001113 goto error3;
1114
Philipp Maier87bd9be2017-08-22 16:35:41 +02001115
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001116 /* policy CB */
1117 if (p->cfg->policy_cb) {
1118 int rc;
1119 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001120 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001121 switch (rc) {
1122 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001123 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001124 "MDCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001125 ENDPOINT_NUMBER(endp));
1126 if (silent)
1127 goto out_silent;
1128 return create_err_response(endp, 400, "MDCX", p->trans);
1129 break;
1130 case MGCP_POLICY_DEFER:
1131 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001132 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001133 "MDCX: endpoint:0x%x defered by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001134 ENDPOINT_NUMBER(endp));
1135 return NULL;
1136 break;
1137 case MGCP_POLICY_CONT:
1138 /* just continue */
1139 break;
1140 }
1141 }
1142
Philipp Maier87bd9be2017-08-22 16:35:41 +02001143 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001144
1145 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001146 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001147 "MDCX: endpoint:0x%x modified conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001148 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001149 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001150 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1151 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001152
Philipp Maiere726d4f2017-11-01 10:41:34 +01001153 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1154 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1155 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
1156 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001157 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001158
1159 if (silent)
1160 goto out_silent;
1161
Philipp Maier87bd9be2017-08-22 16:35:41 +02001162 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001163 "MDCX: endpoint:0x%x connection successfully modified\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001164 ENDPOINT_NUMBER(endp));
Philipp Maier55295f72018-01-15 14:00:28 +01001165 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001166error3:
1167 return create_err_response(endp, error_code, "MDCX", p->trans);
1168
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001169out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001170 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:0x%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001171 ENDPOINT_NUMBER(endp));
1172 return NULL;
1173}
1174
Philipp Maier87bd9be2017-08-22 16:35:41 +02001175/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001176static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
1177{
1178 struct mgcp_endpoint *endp = p->endp;
1179 int error_code = 400;
1180 int silent = 0;
1181 char *line;
1182 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +01001183 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001184 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001185
1186 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001187 "DLCX: endpoint:0x%x deleting connection ...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001188 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001189
Philipp Maier5656fbf2018-02-02 14:41:58 +01001190 /* Prohibit wildcarded requests */
1191 if (endp->wildcarded_req) {
1192 LOGP(DLMGCP, LOGL_ERROR,
1193 "DLCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
1194 ENDPOINT_NUMBER(endp));
1195 return create_err_response(endp, 507, "DLCX", p->trans);
1196 }
1197
Philipp Maier87bd9be2017-08-22 16:35:41 +02001198 if (llist_count(&endp->conns) <= 0) {
1199 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001200 "DLCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001201 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +01001202 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001203 }
1204
1205 for_each_line(line, p->save) {
1206 if (!mgcp_check_param(endp, line))
1207 continue;
1208
1209 switch (line[0]) {
1210 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001211 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1212 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001213 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001214 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001215 break;
1216 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001217 conn_id = (const char *)line + 3;
Neels Hofmeyreb72ff02018-09-03 23:00:07 +02001218 if ((error_code = mgcp_verify_ci(endp, conn_id)))
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001219 goto error3;
1220 break;
1221 case 'Z':
1222 silent = strcmp("noanswer", line + 3) == 0;
1223 break;
1224 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001225 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001226 "DLCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001227 ENDPOINT_NUMBER(endp), line[0], line[0]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001228 return create_err_response(NULL, 539, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001229 break;
1230 }
1231 }
1232
1233 /* policy CB */
1234 if (p->cfg->policy_cb) {
1235 int rc;
1236 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001237 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001238 switch (rc) {
1239 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001240 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001241 "DLCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001242 ENDPOINT_NUMBER(endp));
1243 if (silent)
1244 goto out_silent;
1245 return create_err_response(endp, 400, "DLCX", p->trans);
1246 break;
1247 case MGCP_POLICY_DEFER:
1248 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001249 return NULL;
1250 break;
1251 case MGCP_POLICY_CONT:
1252 /* just continue */
1253 break;
1254 }
1255 }
1256
Philipp Maierf4c0e372017-10-11 16:06:45 +02001257 /* When no connection id is supplied, we will interpret this as a
1258 * wildcarded DLCX and drop all connections at once. (See also
1259 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +01001260 if (!conn_id) {
Philipp Maierf4c0e372017-10-11 16:06:45 +02001261 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001262 "DLCX: endpoint:0x%x missing ci (connectionIdentifier), will remove all connections at once\n",
Philipp Maierf4c0e372017-10-11 16:06:45 +02001263 ENDPOINT_NUMBER(endp));
1264
Philipp Maier1355d7e2018-02-01 14:30:06 +01001265 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001266
1267 /* Note: In this case we do not return any statistics,
1268 * as we assume that the client is not interested in
1269 * this case. */
1270 return create_ok_response(endp, 200, "DLCX", p->trans);
1271 }
1272
Philipp Maierf4c0e372017-10-11 16:06:45 +02001273 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001274 conn = mgcp_conn_get_rtp(endp, conn_id);
1275 if (!conn)
1276 goto error3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001277
Philipp Maier87bd9be2017-08-22 16:35:41 +02001278 /* save the statistics of the current connection */
1279 mgcp_format_stats(stats, sizeof(stats), conn->conn);
1280
1281 /* delete connection */
Philipp Maier230e4fc2017-11-28 09:38:45 +01001282 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x deleting conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001283 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
1284 mgcp_conn_free(endp, conn_id);
1285 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001286 "DLCX: endpoint:0x%x connection successfully deleted\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001287 ENDPOINT_NUMBER(endp));
1288
1289 /* When all connections are closed, the endpoint will be released
1290 * in order to be ready to be used by another call. */
1291 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001292 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001293 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001294 "DLCX: endpoint:0x%x endpoint released\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001295 ENDPOINT_NUMBER(endp));
1296 }
1297
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001298 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001299 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1300 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001301
1302 if (silent)
1303 goto out_silent;
1304 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1305
1306error3:
1307 return create_err_response(endp, error_code, "DLCX", p->trans);
1308
1309out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001310 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x silent exit\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001311 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001312 return NULL;
1313}
1314
Philipp Maier87bd9be2017-08-22 16:35:41 +02001315/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001316static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1317{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001318 /* TODO: Also implement the resetting of a specific endpoint
1319 * to make mgcp_send_reset_ep() work. Currently this will call
1320 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1321 * to make read_call_agent() reset all endpoints when called
1322 * next time. In order to selectively reset endpoints some
1323 * mechanism to distinguish which endpoint shall be resetted
1324 * is needed */
1325
1326 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1327
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001328 if (p->cfg->reset_cb)
1329 p->cfg->reset_cb(p->endp->tcfg);
1330 return NULL;
1331}
1332
1333static char extract_tone(const char *line)
1334{
1335 const char *str = strstr(line, "D/");
1336 if (!str)
1337 return CHAR_MAX;
1338
1339 return str[2];
1340}
1341
Philipp Maier87bd9be2017-08-22 16:35:41 +02001342/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001343 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001344 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001345static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1346{
1347 int res = 0;
1348 char *line;
1349 char tone = CHAR_MAX;
1350
Philipp Maier87bd9be2017-08-22 16:35:41 +02001351 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1352
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001353 for_each_line(line, p->save) {
1354 switch (line[0]) {
1355 case 'S':
1356 tone = extract_tone(line);
1357 break;
1358 }
1359 }
1360
1361 /* we didn't see a signal request with a tone */
1362 if (tone == CHAR_MAX)
1363 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1364
1365 if (p->cfg->rqnt_cb)
1366 res = p->cfg->rqnt_cb(p->endp, tone);
1367
1368 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001369 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1370 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001371}
1372
Philipp Maier87bd9be2017-08-22 16:35:41 +02001373/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001374 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001375static void mgcp_keepalive_timer_cb(void *_tcfg)
1376{
1377 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001378 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001379 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001380
Philipp Maiere726d4f2017-11-01 10:41:34 +01001381 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001382 tcfg->trunk_nr);
1383
Philipp Maiere726d4f2017-11-01 10:41:34 +01001384 /* Do not accept invalid configuration values
1385 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1386 * values greater 0 */
1387 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1388
1389 /* The dummy packet functionality has been disabled, we will exit
1390 * immediately, no further timer is scheduled, which means we will no
1391 * longer send dummy packets even when we did before */
1392 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1393 return;
1394
1395 /* In cases where only one dummy packet is sent, we do not need
1396 * the timer since the functions that handle the CRCX and MDCX are
1397 * triggering the sending of the dummy packet. So we behave like in
1398 * the MGCP_KEEPALIVE_NEVER case */
1399 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001400 return;
1401
Philipp Maier87bd9be2017-08-22 16:35:41 +02001402 /* Send walk over all endpoints and send out dummy packets through
1403 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001404 for (i = 1; i < tcfg->number_endpoints; ++i) {
1405 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001406 llist_for_each_entry(conn, &endp->conns, entry) {
1407 if (conn->mode == MGCP_CONN_RECV_ONLY)
1408 send_dummy(endp, &conn->u.rtp);
1409 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001410 }
1411
Philipp Maiere726d4f2017-11-01 10:41:34 +01001412 /* Schedule the keepalive timer for the next round */
1413 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001414 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001415 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1416 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001417}
1418
1419void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1420{
1421 tcfg->keepalive_interval = interval;
1422 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1423
1424 if (interval <= 0)
1425 osmo_timer_del(&tcfg->keepalive_timer);
1426 else
1427 osmo_timer_schedule(&tcfg->keepalive_timer,
1428 tcfg->keepalive_interval, 0);
1429}
1430
Stefan Sperling1e174872018-10-25 18:36:10 +02001431static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group)
1432{
1433 rate_ctr_group_free(rate_ctr_group);
1434 return 0;
1435}
1436
1437static void alloc_mgcp_crxc_rate_counters(struct mgcp_trunk_config *trunk, void *ctx)
1438{
1439 /* FIXME: Each new rate counter group requires a unique index. At the
1440 * moment we generate an index using a counter, but perhaps there is
1441 * a better way of assigning indices? */
1442 static unsigned int rate_ctr_index = 0;
1443
1444 if (trunk->mgcp_crcx_ctr_group != NULL)
1445 return;
1446
1447 trunk->mgcp_crcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_crcx_ctr_group_desc, rate_ctr_index);
1448 OSMO_ASSERT(trunk->mgcp_crcx_ctr_group);
1449 talloc_set_destructor(trunk->mgcp_crcx_ctr_group, free_rate_counter_group);
1450 rate_ctr_index++;
1451}
1452
Philipp Maier87bd9be2017-08-22 16:35:41 +02001453/*! allocate configuration with default values.
1454 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001455struct mgcp_config *mgcp_config_alloc(void)
1456{
1457 struct mgcp_config *cfg;
1458
1459 cfg = talloc_zero(NULL, struct mgcp_config);
1460 if (!cfg) {
1461 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1462 return NULL;
1463 }
1464
Philipp Maier12943ea2018-01-17 15:40:25 +01001465 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1466
Philipp Maier87bd9be2017-08-22 16:35:41 +02001467 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1468 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1469 cfg->net_ports.last_port = cfg->net_ports.range_start;
1470
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001471 cfg->source_port = 2427;
1472 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1473 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1474
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001475 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1476 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1477
1478 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1479
1480 /* default trunk handling */
1481 cfg->trunk.cfg = cfg;
1482 cfg->trunk.trunk_nr = 0;
1483 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1484 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1485 cfg->trunk.audio_payload = 126;
1486 cfg->trunk.audio_send_ptime = 1;
1487 cfg->trunk.audio_send_name = 1;
1488 cfg->trunk.omit_rtcp = 0;
1489 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperling1e174872018-10-25 18:36:10 +02001490 alloc_mgcp_crxc_rate_counters(&cfg->trunk, cfg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001491
1492 INIT_LLIST_HEAD(&cfg->trunks);
1493
1494 return cfg;
1495}
1496
Philipp Maier87bd9be2017-08-22 16:35:41 +02001497/*! allocate configuration with default values.
1498 * (called once at startup by VTY)
1499 * \param[in] cfg mgcp configuration
1500 * \param[in] nr trunk number
1501 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001502struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1503{
1504 struct mgcp_trunk_config *trunk;
1505
1506 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1507 if (!trunk) {
1508 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1509 return NULL;
1510 }
1511
1512 trunk->cfg = cfg;
1513 trunk->trunk_type = MGCP_TRUNK_E1;
1514 trunk->trunk_nr = nr;
1515 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1516 trunk->audio_payload = 126;
1517 trunk->audio_send_ptime = 1;
1518 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001519 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001520 trunk->omit_rtcp = 0;
1521 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperling1e174872018-10-25 18:36:10 +02001522 alloc_mgcp_crxc_rate_counters(trunk, trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001523 llist_add_tail(&trunk->entry, &cfg->trunks);
Stefan Sperling1e174872018-10-25 18:36:10 +02001524
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001525 return trunk;
1526}
1527
Philipp Maier87bd9be2017-08-22 16:35:41 +02001528/*! get trunk configuration by trunk number (index).
1529 * \param[in] cfg mgcp configuration
1530 * \param[in] index trunk number
1531 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001532struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1533{
1534 struct mgcp_trunk_config *trunk;
1535
1536 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001537 if (trunk->trunk_nr == index)
1538 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001539
1540 return NULL;
1541}
1542
Philipp Maier87bd9be2017-08-22 16:35:41 +02001543/*! allocate endpoints and set default values.
1544 * (called once at startup by VTY)
1545 * \param[in] tcfg trunk configuration
1546 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001547int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1548{
1549 int i;
1550
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001551 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001552 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001553 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001554 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001555 if (!tcfg->endpoints)
1556 return -1;
1557
Philipp Maierfcd06552017-11-10 17:32:22 +01001558 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001559 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001560 tcfg->endpoints[i].cfg = tcfg->cfg;
1561 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001562
1563 /* NOTE: Currently all endpoints are of type RTP, this will
1564 * change when new variations are implemented */
1565 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001566 }
1567
Philipp Maierfcd06552017-11-10 17:32:22 +01001568 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Stefan Sperling1e174872018-10-25 18:36:10 +02001569 alloc_mgcp_crxc_rate_counters(tcfg, tcfg->cfg);
1570
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001571 return 0;
1572}
1573
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001574static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1575{
1576 return write(cfg->gw_fd.bfd.fd, buf, len);
1577}
1578
Philipp Maier87bd9be2017-08-22 16:35:41 +02001579/*! Reset all endpoints by sending RSIP message to self.
1580 * (called by VTY)
1581 * \param[in] endp trunk endpoint
1582 * \param[in] endpoint number
1583 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001584int mgcp_send_reset_all(struct mgcp_config *cfg)
1585{
Philipp Maier12943ea2018-01-17 15:40:25 +01001586 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1587 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001588 int rc;
1589
Philipp Maier12943ea2018-01-17 15:40:25 +01001590 len = snprintf(buf, sizeof(buf),
1591 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1592 if (len < 0)
1593 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001594
Philipp Maier12943ea2018-01-17 15:40:25 +01001595 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001596 if (rc <= 0)
1597 return -1;
1598
1599 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001600}
1601
Philipp Maier87bd9be2017-08-22 16:35:41 +02001602/*! Reset a single endpoint by sending RSIP message to self.
1603 * (called by VTY)
1604 * \param[in] endp trunk endpoint
1605 * \param[in] endpoint number
1606 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001607int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1608{
Philipp Maier12943ea2018-01-17 15:40:25 +01001609 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001610 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001611 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001612
1613 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001614 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001615 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001616 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001617
Philipp Maier87bd9be2017-08-22 16:35:41 +02001618 rc = send_agent(endp->cfg, buf, len);
1619 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001620 return -1;
1621
Philipp Maier87bd9be2017-08-22 16:35:41 +02001622 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001623}