blob: 16e9cb86e261a0351df5116672c52b61d25d3ad5 [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>
35
Philipp Maier87bd9be2017-08-22 16:35:41 +020036#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020037#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020038#include <osmocom/mgcp/mgcp_internal.h>
39#include <osmocom/mgcp/mgcp_stat.h>
40#include <osmocom/mgcp/mgcp_msg.h>
41#include <osmocom/mgcp/mgcp_ep.h>
Philipp Maier8970c492017-10-11 13:33:42 +020042#include <osmocom/mgcp/mgcp_sdp.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020043
44struct mgcp_request {
45 char *name;
Philipp Maier87bd9be2017-08-22 16:35:41 +020046 struct msgb *(*handle_request) (struct mgcp_parse_data * data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020047 char *debug_name;
48};
49
50#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
51 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
52
53static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data);
54static struct msgb *handle_create_con(struct mgcp_parse_data *data);
55static struct msgb *handle_delete_con(struct mgcp_parse_data *data);
56static struct msgb *handle_modify_con(struct mgcp_parse_data *data);
57static struct msgb *handle_rsip(struct mgcp_parse_data *data);
58static struct msgb *handle_noti_req(struct mgcp_parse_data *data);
59
Philipp Maier87bd9be2017-08-22 16:35:41 +020060/* Initalize transcoder */
61static int setup_rtp_processing(struct mgcp_endpoint *endp,
62 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020063{
Philipp Maier87bd9be2017-08-22 16:35:41 +020064 struct mgcp_config *cfg = endp->cfg;
65 struct mgcp_conn_rtp *conn_src = NULL;
66 struct mgcp_conn_rtp *conn_dst = conn;
67 struct mgcp_conn *_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020068
Philipp Maier87bd9be2017-08-22 16:35:41 +020069 if (conn->type != MGCP_RTP_DEFAULT) {
70 LOGP(DLMGCP, LOGL_NOTICE,
71 "endpoint:%x RTP-setup: Endpoint is not configured as RTP default, stopping here!\n",
72 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020073 return 0;
74 }
75
Philipp Maier87bd9be2017-08-22 16:35:41 +020076 if (conn->conn->mode == MGCP_CONN_LOOPBACK) {
77 LOGP(DLMGCP, LOGL_NOTICE,
78 "endpoint:%x RTP-setup: Endpoint is in loopback mode, stopping here!\n",
79 ENDPOINT_NUMBER(endp));
80 return 0;
81 }
82
83 /* Find the "sister" connection */
84 llist_for_each_entry(_conn, &endp->conns, entry) {
85 if (_conn->id != conn->conn->id) {
86 conn_src = &_conn->u.rtp;
87 break;
88 }
89 }
90
91 return cfg->setup_rtp_processing_cb(endp, &conn_dst->end,
92 &conn_src->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020093}
94
Philipp Maier87bd9be2017-08-22 16:35:41 +020095/* array of function pointers for handling various
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020096 * messages. In the future this might be binary sorted
Philipp Maier87bd9be2017-08-22 16:35:41 +020097 * for performance reasons. */
98static const struct mgcp_request mgcp_requests[] = {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020099 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
Philipp Maier87bd9be2017-08-22 16:35:41 +0200100 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
101 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
102 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
103 MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200104
Philipp Maier87bd9be2017-08-22 16:35:41 +0200105 /* SPEC extension */
106 MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200107};
108
Philipp Maier87bd9be2017-08-22 16:35:41 +0200109/* Helper function to allocate some memory for responses and retransmissions */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200110static struct msgb *mgcp_msgb_alloc(void)
111{
112 struct msgb *msg;
113 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
114 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200115 LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200116
117 return msg;
118}
119
Philipp Maier87bd9be2017-08-22 16:35:41 +0200120/* Helper function for do_retransmission() and create_resp() */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200121static struct msgb *do_retransmission(const struct mgcp_endpoint *endp)
122{
123 struct msgb *msg = mgcp_msgb_alloc();
124 if (!msg)
125 return NULL;
126
127 msg->l2h = msgb_put(msg, strlen(endp->last_response));
128 memcpy(msg->l2h, endp->last_response, msgb_l2len(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200130 return msg;
131}
132
133static struct msgb *create_resp(struct mgcp_endpoint *endp, int code,
134 const char *txt, const char *msg,
135 const char *trans, const char *param,
136 const char *sdp)
137{
138 int len;
139 struct msgb *res;
140
141 res = mgcp_msgb_alloc();
142 if (!res)
143 return NULL;
144
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145 len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s",
146 code, trans, txt, param ? param : "", sdp ? sdp : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200147 if (len < 0) {
148 LOGP(DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n");
149 msgb_free(res);
150 return NULL;
151 }
152
153 res->l2h = msgb_put(res, len);
154 LOGP(DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200155 mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200156
157 /*
158 * Remember the last transmission per endpoint.
159 */
160 if (endp) {
161 struct mgcp_trunk_config *tcfg = endp->tcfg;
162 talloc_free(endp->last_response);
163 talloc_free(endp->last_trans);
164 endp->last_trans = talloc_strdup(tcfg->endpoints, trans);
165 endp->last_response = talloc_strndup(tcfg->endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200166 (const char *)res->l2h,
167 msgb_l2len(res));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200168 }
169
170 return res;
171}
172
173static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174 int code, const char *msg,
175 const char *trans,
176 const char *param)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177{
178 return create_resp(endp, code, " OK", msg, trans, param, NULL);
179}
180
181static struct msgb *create_ok_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200182 int code, const char *msg,
183 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200184{
185 return create_ok_resp_with_param(endp, code, msg, trans, NULL);
186}
187
188static struct msgb *create_err_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189 int code, const char *msg,
190 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200191{
192 return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL);
193}
194
Philipp Maier55295f72018-01-15 14:00:28 +0100195/* Add MGCP parameters to a message buffer */
196static int add_params(struct msgb *msg, const struct mgcp_endpoint *endp,
197 const struct mgcp_conn_rtp *conn)
198{
199 int rc;
200
201 if (endp->wildcarded_crcx) {
202 rc = msgb_printf(msg, "Z: %u@%s\n", ENDPOINT_NUMBER(endp),
203 endp->cfg->domain);
204 if (rc < 0)
205 return -EINVAL;
206 }
207
208 rc = msgb_printf(msg, "I: %s\n", conn->conn->id);
209 if (rc < 0)
210 return -EINVAL;
211
212 return 0;
213}
214
Philipp Maier87bd9be2017-08-22 16:35:41 +0200215/* Format MGCP response string (with SDP attached) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200216static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217 struct mgcp_conn_rtp *conn,
218 const char *msg,
Philipp Maier55295f72018-01-15 14:00:28 +0100219 const char *trans_id,
220 bool add_conn_params)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200221{
222 const char *addr = endp->cfg->local_ip;
Philipp Maier8970c492017-10-11 13:33:42 +0200223 struct msgb *sdp;
224 int rc;
225 struct msgb *result;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200226 char osmux_extension[strlen("\nX-Osmux: 255") + 1];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100227 char local_ip_addr[INET_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200228
Philipp Maier8970c492017-10-11 13:33:42 +0200229 sdp = msgb_alloc_headroom(4096, 128, "sdp record");
230 if (!sdp)
231 return NULL;
232
Philipp Maier1cb1e382017-11-02 17:16:04 +0100233 if (!addr) {
234 mgcp_get_local_addr(local_ip_addr, conn);
235 addr = local_ip_addr;
236 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237
Philipp Maier87bd9be2017-08-22 16:35:41 +0200238 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
239 sprintf(osmux_extension, "\nX-Osmux: %u", conn->osmux.cid);
240 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200241 } else {
242 osmux_extension[0] = '\0';
243 }
244
Philipp Maier55295f72018-01-15 14:00:28 +0100245 /* Attach optional connection parameters */
246 if (add_conn_params) {
247 rc = add_params(sdp, endp, conn);
248 if (rc < 0)
249 goto error;
250 }
251
252 rc = msgb_printf(sdp, "%s\n", osmux_extension);
Philipp Maier8970c492017-10-11 13:33:42 +0200253 if (rc < 0)
254 goto error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200255
Philipp Maier8970c492017-10-11 13:33:42 +0200256 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
257 if (rc < 0)
258 goto error;
259 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
260 msgb_free(sdp);
261 return result;
262error:
263 msgb_free(sdp);
264 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265}
266
Philipp Maier87bd9be2017-08-22 16:35:41 +0200267/* Send out dummy packet to keep the connection open, if the connection is an
268 * osmux connection, send the dummy packet via OSMUX */
269static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200270{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200271 if (conn->osmux.state != OSMUX_STATE_DISABLED)
272 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200273 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200274 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200275}
276
Philipp Maier87bd9be2017-08-22 16:35:41 +0200277/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200278 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200279 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200280struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
281{
282 struct mgcp_parse_data pdata;
Harald Weltee35eeae2017-12-28 13:47:37 +0100283 int rc, i, code, handled = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200284 struct msgb *resp = NULL;
285 char *data;
286
287 if (msgb_l2len(msg) < 4) {
288 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
289 return NULL;
290 }
291
292 if (mgcp_msg_terminate_nul(msg))
293 return NULL;
294
Philipp Maier87bd9be2017-08-22 16:35:41 +0200295 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200296
Philipp Maier87bd9be2017-08-22 16:35:41 +0200297 /* attempt to treat it as a response */
298 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200299 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
300 return NULL;
301 }
302
303 msg->l3h = &msg->l2h[4];
304
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200305 /*
306 * Check for a duplicate message and respond.
307 */
308 memset(&pdata, 0, sizeof(pdata));
309 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200310 data = mgcp_strline((char *)msg->l3h, &pdata.save);
Harald Weltee35eeae2017-12-28 13:47:37 +0100311 rc = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200312 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200313 && pdata.endp->last_trans
314 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200315 return do_retransmission(pdata.endp);
316 }
317
Harald Welteabbb6b92017-12-28 13:13:50 +0100318 /* check for general parser failure */
Harald Weltee35eeae2017-12-28 13:47:37 +0100319 if (rc < 0) {
Harald Welteabbb6b92017-12-28 13:13:50 +0100320 LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h);
Harald Weltee35eeae2017-12-28 13:47:37 +0100321 return create_err_response(NULL, -rc, (const char *) msg->l2h, pdata.trans);
Harald Welteabbb6b92017-12-28 13:13:50 +0100322 }
323
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200324 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200325 if (strncmp
326 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
327 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200328 handled = 1;
329 resp = mgcp_requests[i].handle_request(&pdata);
330 break;
331 }
332 }
333
334 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200335 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
336 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200337
338 return resp;
339}
340
Philipp Maier87bd9be2017-08-22 16:35:41 +0200341/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200342static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
343{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200344 LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100345 return create_ok_response(p->endp, 200, "AUEP", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200346}
347
Harald Welte1d1b98f2017-12-25 10:03:40 +0100348/* Try to find a free port by attempting to bind on it. Also handle the
Philipp Maier87bd9be2017-08-22 16:35:41 +0200349 * counter that points on the next free port. Since we have a pointer
Harald Welte1d1b98f2017-12-25 10:03:40 +0100350 * to the next free port, binding should work on the first attempt,
351 * nevertheless, try at least the next 200 ports before giving up */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200352static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200353{
354 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200355 struct mgcp_rtp_end *end;
356 struct mgcp_port_range *range;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200357
Philipp Maier87bd9be2017-08-22 16:35:41 +0200358 OSMO_ASSERT(conn);
359 end = &conn->end;
360 OSMO_ASSERT(end);
361
362 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200363
364 /* attempt to find a port */
365 for (i = 0; i < 200; ++i) {
366 int rc;
367
368 if (range->last_port >= range->range_end)
369 range->last_port = range->range_start;
370
Philipp Maier87bd9be2017-08-22 16:35:41 +0200371 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200372
373 range->last_port += 2;
374 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200375 return 0;
376 }
377
378 }
379
Philipp Maier87bd9be2017-08-22 16:35:41 +0200380 LOGP(DLMGCP, LOGL_ERROR,
381 "Allocating a RTP/RTCP port failed 200 times 0x%x.\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200382 ENDPOINT_NUMBER(endp));
383 return -1;
384}
385
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200386/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100387 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200388 * like an empty string, the 'string' field is never NULL after this function
389 * has been called. */
390static void set_local_cx_options(void *ctx, struct mgcp_lco *lco,
391 const char *options)
392{
393 char *p_opt, *a_opt;
394 char codec[9];
395
396 talloc_free(lco->string);
397 talloc_free(lco->codec);
398 lco->codec = NULL;
399 lco->pkt_period_min = lco->pkt_period_max = 0;
400 lco->string = talloc_strdup(ctx, options ? options : "");
401
402 p_opt = strstr(lco->string, "p:");
403 if (p_opt && sscanf(p_opt, "p:%d-%d",
404 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
405 lco->pkt_period_max = lco->pkt_period_min;
406
407 a_opt = strstr(lco->string, "a:");
408 if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1)
409 lco->codec = talloc_strdup(ctx, codec);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200410
411 LOGP(DLMGCP, LOGL_DEBUG,
412 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
413 lco->pkt_period_max, lco->codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200414}
415
416void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
417 struct mgcp_rtp_end *rtp)
418{
419 struct mgcp_trunk_config *tcfg = endp->tcfg;
420
421 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
422
423 rtp->force_aligned_timing = tcfg->force_aligned_timing;
424 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
425
426 LOGP(DLMGCP, LOGL_DEBUG,
427 "Configuring RTP endpoint: local port %d%s%s\n",
428 ntohs(rtp->rtp_port),
429 rtp->force_aligned_timing ? ", force constant timing" : "",
430 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
431}
432
433uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
434 struct mgcp_rtp_end *rtp)
435{
436 int f = 0;
437
438 /* Get the number of frames per channel and packet */
439 if (rtp->frames_per_packet)
440 f = rtp->frames_per_packet;
441 else if (rtp->packet_duration_ms && rtp->codec.frame_duration_num) {
442 int den = 1000 * rtp->codec.frame_duration_num;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200443 f = (rtp->packet_duration_ms * rtp->codec.frame_duration_den +
444 den / 2)
445 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200446 }
447
Philipp Maier87bd9be2017-08-22 16:35:41 +0200448 return rtp->codec.rate * f * rtp->codec.frame_duration_num /
449 rtp->codec.frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200450}
451
452static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
453{
454 if (!endp->cfg->osmux_init) {
455 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
456 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
457 return -1;
458 }
459 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
460 }
461
462 return mgcp_parse_osmux_cid(line);
463}
464
Philipp Maier87bd9be2017-08-22 16:35:41 +0200465/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200466static struct msgb *handle_create_con(struct mgcp_parse_data *p)
467{
468 struct mgcp_trunk_config *tcfg;
469 struct mgcp_endpoint *endp = p->endp;
470 int error_code = 400;
471
472 const char *local_options = NULL;
473 const char *callid = NULL;
474 const char *mode = NULL;
475 char *line;
476 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200477 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100478 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200479 char conn_name[512];
480
481 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200482
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200483 /* parse CallID C: and LocalParameters L: */
484 for_each_line(line, p->save) {
485 if (!mgcp_check_param(endp, line))
486 continue;
487
488 switch (line[0]) {
489 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200490 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200491 break;
492 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200493 callid = (const char *)line + 3;
494 break;
495 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100496 /* It is illegal to send a connection identifier
497 * together with a CRCX, the MGW will assign the
498 * connection identifier by itself on CRCX */
499 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200500 break;
501 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200502 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200503 break;
504 case 'X':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200505 /* If osmoux is disabled, just skip setting it up */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200506 if (!p->endp->cfg->osmux)
507 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200508 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) ==
509 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200510 osmux_cid = mgcp_osmux_setup(endp, line);
511 break;
512 case '\0':
513 have_sdp = 1;
514 goto mgcp_header_done;
515 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200516 LOGP(DLMGCP, LOGL_NOTICE,
517 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
518 ENDPOINT_NUMBER(endp), *line, *line);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200519 break;
520 }
521 }
522
523mgcp_header_done:
524 tcfg = p->endp->tcfg;
525
Philipp Maier87bd9be2017-08-22 16:35:41 +0200526 /* Check parameters */
527 if (!callid) {
528 LOGP(DLMGCP, LOGL_ERROR,
529 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200530 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +0100531 return create_err_response(endp, 516, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200532 }
533
Philipp Maier87bd9be2017-08-22 16:35:41 +0200534 if (!mode) {
535 LOGP(DLMGCP, LOGL_ERROR,
536 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
537 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +0100538 return create_err_response(endp, 517, "CRCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200539 }
540
Philipp Maier87bd9be2017-08-22 16:35:41 +0200541 /* Check if we are able to accept the creation of another connection */
542 if (llist_count(&endp->conns) >= endp->type->max_conns) {
543 LOGP(DLMGCP, LOGL_ERROR,
544 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
545 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200546 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200547 /* There is no more room for a connection, make some
548 * room by blindly tossing the oldest of the two two
549 * connections */
550 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200551 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200552 /* There is no more room for a connection, leave
553 * everything as it is and return with an error */
Harald Weltee35eeae2017-12-28 13:47:37 +0100554 return create_err_response(endp, 540, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200555 }
556 }
557
Philipp Maier87bd9be2017-08-22 16:35:41 +0200558 /* Check if this endpoint already serves a call, if so, check if the
559 * callids match up so that we are sure that this is our call */
560 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
561 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100562 "CRCX: endpoint:0x%x allready seized by other call (%s)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200563 ENDPOINT_NUMBER(endp), endp->callid);
564 if (tcfg->force_realloc)
565 /* This is not our call, toss everything by releasing
566 * the entire endpoint. (rude!) */
567 mgcp_release_endp(endp);
568 else {
569 /* This is not our call, leave everything as it is and
570 * return with an error. */
571 return create_err_response(endp, 400, "CRCX", p->trans);
572 }
573 }
574
575 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100576 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200577 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200578 endp->callid = talloc_strdup(tcfg->endpoints, callid);
579
Philipp Maier87bd9be2017-08-22 16:35:41 +0200580 /* Extract audio codec information */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200581 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
582 local_options);
583
Philipp Maierffd75e42017-11-22 11:44:50 +0100584 snprintf(conn_name, sizeof(conn_name), "%s", callid);
585 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
586 if (!_conn) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200587 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100588 "CRCX: endpoint:0x%x unable to allocate RTP connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200589 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200590 goto error2;
591
Philipp Maier87bd9be2017-08-22 16:35:41 +0200592 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100593 conn = mgcp_conn_get_rtp(endp, _conn->id);
594 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200595
596 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
597 error_code = 517;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200598 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200599 }
600
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200601 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200602 * is fully set up from the dummy load. */
603 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200604 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200605 conn->osmux.cid = osmux_cid;
606 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200607 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
608 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100609 "CRCX: endpoint:0x%x osmux only and no osmux offered\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200610 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200611 goto error2;
612 }
613
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200614 /* set up RTP media parameters */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200615 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200616 mgcp_parse_sdp_data(endp, conn, p);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200617 else if (endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200618 mgcp_set_audio_info(p->cfg, &conn->end.codec,
619 PTYPE_UNDEFINED, endp->local_options.codec);
620 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
621 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200622
Philipp Maier87bd9be2017-08-22 16:35:41 +0200623 if (p->cfg->force_ptime) {
624 conn->end.packet_duration_ms = p->cfg->force_ptime;
625 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200626 }
627
Philipp Maier1cb1e382017-11-02 17:16:04 +0100628 mgcp_rtp_end_config(endp, 0, &conn->end);
629
630 if (allocate_port(endp, conn) != 0) {
631 goto error2;
632 }
633
Philipp Maier87bd9be2017-08-22 16:35:41 +0200634 if (setup_rtp_processing(endp, conn) != 0) {
635 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100636 "CRCX: endpoint:0x%x could not start RTP processing!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200637 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200638 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200639 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200640
641 /* policy CB */
642 if (p->cfg->policy_cb) {
643 int rc;
644 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200645 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200646 switch (rc) {
647 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200648 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100649 "CRCX: endpoint:0x%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200650 ENDPOINT_NUMBER(endp));
651 mgcp_release_endp(endp);
652 return create_err_response(endp, 400, "CRCX", p->trans);
653 break;
654 case MGCP_POLICY_DEFER:
655 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200656 return NULL;
657 break;
658 case MGCP_POLICY_CONT:
659 /* just continue */
660 break;
661 }
662 }
663
Philipp Maier87bd9be2017-08-22 16:35:41 +0200664 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100665 "CRCX: endpoint:0x%x Creating connection: CI: %s port: %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200666 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200667 if (p->cfg->change_cb)
668 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
669
Philipp Maiere726d4f2017-11-01 10:41:34 +0100670 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
671 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200672 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +0100673 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200674 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200675
Philipp Maier87bd9be2017-08-22 16:35:41 +0200676 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100677 "CRCX: endpoint:0x%x connection successfully created\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200678 ENDPOINT_NUMBER(endp));
Philipp Maier55295f72018-01-15 14:00:28 +0100679 return create_response_with_sdp(endp, conn, "CRCX", p->trans, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200680error2:
681 mgcp_release_endp(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200682 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100683 "CRCX: endpoint:0x%x unable to create connection resource error\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200684 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200685 return create_err_response(endp, error_code, "CRCX", p->trans);
686}
687
Philipp Maier87bd9be2017-08-22 16:35:41 +0200688/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200689static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
690{
691 struct mgcp_endpoint *endp = p->endp;
692 int error_code = 500;
693 int silent = 0;
694 int have_sdp = 0;
695 char *line;
696 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200697 const char *mode = NULL;
698 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +0100699 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200700
701 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702
Philipp Maier87bd9be2017-08-22 16:35:41 +0200703 if (llist_count(&endp->conns) <= 0) {
704 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100705 "MDCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200706 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200707 return create_err_response(endp, 400, "MDCX", p->trans);
708 }
709
710 for_each_line(line, p->save) {
711 if (!mgcp_check_param(endp, line))
712 continue;
713
714 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200715 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +0100716 if (mgcp_verify_call_id(endp, line + 3) != 0) {
717 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200718 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100719 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200720 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200721 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +0100722 conn_id = (const char *)line + 3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100723 if (mgcp_verify_ci(endp, conn_id) != 0) {
724 error_code = 515;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200725 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100726 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200727 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200728 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200729 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200730 break;
731 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200732 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200733 break;
734 case 'Z':
735 silent = strcmp("noanswer", line + 3) == 0;
736 break;
737 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200738 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200739 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200740 break;
741 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200742 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100743 "MDCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200744 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200745 break;
746 }
747 }
748
Philipp Maier87bd9be2017-08-22 16:35:41 +0200749mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +0100750 if (!conn_id) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200751 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100752 "MDCX: endpoint:0x%x insufficient parameters, missing ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200753 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +0100754 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200755 }
756
757 conn = mgcp_conn_get_rtp(endp, conn_id);
758 if (!conn)
759 return create_err_response(endp, 400, "MDCX", p->trans);
760
761 if (mode) {
762 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
763 error_code = 517;
764 goto error3;
765 }
766 } else
767 conn->conn->mode = conn->conn->mode_orig;
768
769 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200770 mgcp_parse_sdp_data(endp, conn, p);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200771
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200772 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
773 local_options);
774
775 if (!have_sdp && endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200776 mgcp_set_audio_info(p->cfg, &conn->end.codec,
777 PTYPE_UNDEFINED, endp->local_options.codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200778
Philipp Maier87bd9be2017-08-22 16:35:41 +0200779 if (setup_rtp_processing(endp, conn) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200780 goto error3;
781
Philipp Maier87bd9be2017-08-22 16:35:41 +0200782
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200783 /* policy CB */
784 if (p->cfg->policy_cb) {
785 int rc;
786 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200787 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200788 switch (rc) {
789 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200790 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100791 "MDCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200792 ENDPOINT_NUMBER(endp));
793 if (silent)
794 goto out_silent;
795 return create_err_response(endp, 400, "MDCX", p->trans);
796 break;
797 case MGCP_POLICY_DEFER:
798 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200799 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100800 "MDCX: endpoint:0x%x defered by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200801 ENDPOINT_NUMBER(endp));
802 return NULL;
803 break;
804 case MGCP_POLICY_CONT:
805 /* just continue */
806 break;
807 }
808 }
809
Philipp Maier87bd9be2017-08-22 16:35:41 +0200810 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811
812 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200813 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100814 "MDCX: endpoint:0x%x modified conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200815 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200816 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200817 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
818 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200819
Philipp Maiere726d4f2017-11-01 10:41:34 +0100820 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
821 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
822 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
823 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200824 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200825
826 if (silent)
827 goto out_silent;
828
Philipp Maier87bd9be2017-08-22 16:35:41 +0200829 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100830 "MDCX: endpoint:0x%x connection successfully modified\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200831 ENDPOINT_NUMBER(endp));
Philipp Maier55295f72018-01-15 14:00:28 +0100832 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200833error3:
834 return create_err_response(endp, error_code, "MDCX", p->trans);
835
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200836out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +0100837 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:0x%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200838 ENDPOINT_NUMBER(endp));
839 return NULL;
840}
841
Philipp Maier87bd9be2017-08-22 16:35:41 +0200842/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200843static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
844{
845 struct mgcp_endpoint *endp = p->endp;
846 int error_code = 400;
847 int silent = 0;
848 char *line;
849 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +0100850 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200851 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200852
853 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100854 "DLCX: endpoint:0x%x deleting connection ...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200855 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200856
Philipp Maier87bd9be2017-08-22 16:35:41 +0200857 if (llist_count(&endp->conns) <= 0) {
858 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100859 "DLCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200860 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +0100861 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200862 }
863
864 for_each_line(line, p->save) {
865 if (!mgcp_check_param(endp, line))
866 continue;
867
868 switch (line[0]) {
869 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +0100870 if (mgcp_verify_call_id(endp, line + 3) != 0) {
871 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200872 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100873 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200874 break;
875 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +0100876 conn_id = (const char *)line + 3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100877 if (mgcp_verify_ci(endp, conn_id) != 0) {
878 error_code = 515;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200879 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100880 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200881 break;
882 case 'Z':
883 silent = strcmp("noanswer", line + 3) == 0;
884 break;
885 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100887 "DLCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200888 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200889 break;
890 }
891 }
892
893 /* policy CB */
894 if (p->cfg->policy_cb) {
895 int rc;
896 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200897 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200898 switch (rc) {
899 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200900 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100901 "DLCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200902 ENDPOINT_NUMBER(endp));
903 if (silent)
904 goto out_silent;
905 return create_err_response(endp, 400, "DLCX", p->trans);
906 break;
907 case MGCP_POLICY_DEFER:
908 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200909 return NULL;
910 break;
911 case MGCP_POLICY_CONT:
912 /* just continue */
913 break;
914 }
915 }
916
Philipp Maierf4c0e372017-10-11 16:06:45 +0200917 /* When no connection id is supplied, we will interpret this as a
918 * wildcarded DLCX and drop all connections at once. (See also
919 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +0100920 if (!conn_id) {
Philipp Maierf4c0e372017-10-11 16:06:45 +0200921 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100922 "DLCX: endpoint:0x%x missing ci (connectionIdentifier), will remove all connections at once\n",
Philipp Maierf4c0e372017-10-11 16:06:45 +0200923 ENDPOINT_NUMBER(endp));
924
925 mgcp_release_endp(endp);
926
927 /* Note: In this case we do not return any statistics,
928 * as we assume that the client is not interested in
929 * this case. */
930 return create_ok_response(endp, 200, "DLCX", p->trans);
931 }
932
Philipp Maierf4c0e372017-10-11 16:06:45 +0200933 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200934 conn = mgcp_conn_get_rtp(endp, conn_id);
935 if (!conn)
936 goto error3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200937
Philipp Maier87bd9be2017-08-22 16:35:41 +0200938 /* save the statistics of the current connection */
939 mgcp_format_stats(stats, sizeof(stats), conn->conn);
940
941 /* delete connection */
Philipp Maier230e4fc2017-11-28 09:38:45 +0100942 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x deleting conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200943 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
944 mgcp_conn_free(endp, conn_id);
945 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100946 "DLCX: endpoint:0x%x connection successfully deleted\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200947 ENDPOINT_NUMBER(endp));
948
949 /* When all connections are closed, the endpoint will be released
950 * in order to be ready to be used by another call. */
951 if (llist_count(&endp->conns) <= 0) {
952 mgcp_release_endp(endp);
953 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100954 "DLCX: endpoint:0x%x endpoint released\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200955 ENDPOINT_NUMBER(endp));
956 }
957
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200958 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200959 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
960 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200961
962 if (silent)
963 goto out_silent;
964 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
965
966error3:
967 return create_err_response(endp, error_code, "DLCX", p->trans);
968
969out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +0100970 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x silent exit\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200971 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200972 return NULL;
973}
974
Philipp Maier87bd9be2017-08-22 16:35:41 +0200975/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200976static struct msgb *handle_rsip(struct mgcp_parse_data *p)
977{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978 /* TODO: Also implement the resetting of a specific endpoint
979 * to make mgcp_send_reset_ep() work. Currently this will call
980 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
981 * to make read_call_agent() reset all endpoints when called
982 * next time. In order to selectively reset endpoints some
983 * mechanism to distinguish which endpoint shall be resetted
984 * is needed */
985
986 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
987
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200988 if (p->cfg->reset_cb)
989 p->cfg->reset_cb(p->endp->tcfg);
990 return NULL;
991}
992
993static char extract_tone(const char *line)
994{
995 const char *str = strstr(line, "D/");
996 if (!str)
997 return CHAR_MAX;
998
999 return str[2];
1000}
1001
Philipp Maier87bd9be2017-08-22 16:35:41 +02001002/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001003 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001004 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001005static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1006{
1007 int res = 0;
1008 char *line;
1009 char tone = CHAR_MAX;
1010
Philipp Maier87bd9be2017-08-22 16:35:41 +02001011 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1012
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001013 for_each_line(line, p->save) {
1014 switch (line[0]) {
1015 case 'S':
1016 tone = extract_tone(line);
1017 break;
1018 }
1019 }
1020
1021 /* we didn't see a signal request with a tone */
1022 if (tone == CHAR_MAX)
1023 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1024
1025 if (p->cfg->rqnt_cb)
1026 res = p->cfg->rqnt_cb(p->endp, tone);
1027
1028 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001029 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1030 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001031}
1032
Philipp Maier87bd9be2017-08-22 16:35:41 +02001033/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001034 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001035static void mgcp_keepalive_timer_cb(void *_tcfg)
1036{
1037 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001038 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001039 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001040
Philipp Maiere726d4f2017-11-01 10:41:34 +01001041 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001042 tcfg->trunk_nr);
1043
Philipp Maiere726d4f2017-11-01 10:41:34 +01001044 /* Do not accept invalid configuration values
1045 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1046 * values greater 0 */
1047 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1048
1049 /* The dummy packet functionality has been disabled, we will exit
1050 * immediately, no further timer is scheduled, which means we will no
1051 * longer send dummy packets even when we did before */
1052 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1053 return;
1054
1055 /* In cases where only one dummy packet is sent, we do not need
1056 * the timer since the functions that handle the CRCX and MDCX are
1057 * triggering the sending of the dummy packet. So we behave like in
1058 * the MGCP_KEEPALIVE_NEVER case */
1059 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001060 return;
1061
Philipp Maier87bd9be2017-08-22 16:35:41 +02001062 /* Send walk over all endpoints and send out dummy packets through
1063 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001064 for (i = 1; i < tcfg->number_endpoints; ++i) {
1065 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001066 llist_for_each_entry(conn, &endp->conns, entry) {
1067 if (conn->mode == MGCP_CONN_RECV_ONLY)
1068 send_dummy(endp, &conn->u.rtp);
1069 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001070 }
1071
Philipp Maiere726d4f2017-11-01 10:41:34 +01001072 /* Schedule the keepalive timer for the next round */
1073 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001074 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001075 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1076 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001077}
1078
1079void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1080{
1081 tcfg->keepalive_interval = interval;
1082 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1083
1084 if (interval <= 0)
1085 osmo_timer_del(&tcfg->keepalive_timer);
1086 else
1087 osmo_timer_schedule(&tcfg->keepalive_timer,
1088 tcfg->keepalive_interval, 0);
1089}
1090
Philipp Maier87bd9be2017-08-22 16:35:41 +02001091/*! allocate configuration with default values.
1092 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001093struct mgcp_config *mgcp_config_alloc(void)
1094{
1095 struct mgcp_config *cfg;
1096
1097 cfg = talloc_zero(NULL, struct mgcp_config);
1098 if (!cfg) {
1099 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1100 return NULL;
1101 }
1102
Philipp Maier12943ea2018-01-17 15:40:25 +01001103 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1104
Philipp Maier87bd9be2017-08-22 16:35:41 +02001105 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1106 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1107 cfg->net_ports.last_port = cfg->net_ports.range_start;
1108
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001109 cfg->source_port = 2427;
1110 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1111 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1112
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001113 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1114 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1115
1116 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1117
1118 /* default trunk handling */
1119 cfg->trunk.cfg = cfg;
1120 cfg->trunk.trunk_nr = 0;
1121 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1122 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1123 cfg->trunk.audio_payload = 126;
1124 cfg->trunk.audio_send_ptime = 1;
1125 cfg->trunk.audio_send_name = 1;
1126 cfg->trunk.omit_rtcp = 0;
1127 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
1128
1129 INIT_LLIST_HEAD(&cfg->trunks);
1130
1131 return cfg;
1132}
1133
Philipp Maier87bd9be2017-08-22 16:35:41 +02001134/*! allocate configuration with default values.
1135 * (called once at startup by VTY)
1136 * \param[in] cfg mgcp configuration
1137 * \param[in] nr trunk number
1138 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001139struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1140{
1141 struct mgcp_trunk_config *trunk;
1142
1143 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1144 if (!trunk) {
1145 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1146 return NULL;
1147 }
1148
1149 trunk->cfg = cfg;
1150 trunk->trunk_type = MGCP_TRUNK_E1;
1151 trunk->trunk_nr = nr;
1152 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1153 trunk->audio_payload = 126;
1154 trunk->audio_send_ptime = 1;
1155 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001156 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001157 trunk->omit_rtcp = 0;
1158 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
1159 llist_add_tail(&trunk->entry, &cfg->trunks);
1160 return trunk;
1161}
1162
Philipp Maier87bd9be2017-08-22 16:35:41 +02001163/*! get trunk configuration by trunk number (index).
1164 * \param[in] cfg mgcp configuration
1165 * \param[in] index trunk number
1166 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001167struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1168{
1169 struct mgcp_trunk_config *trunk;
1170
1171 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001172 if (trunk->trunk_nr == index)
1173 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001174
1175 return NULL;
1176}
1177
Philipp Maier87bd9be2017-08-22 16:35:41 +02001178/*! allocate endpoints and set default values.
1179 * (called once at startup by VTY)
1180 * \param[in] tcfg trunk configuration
1181 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001182int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1183{
1184 int i;
1185
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001186 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001187 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001188 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001189 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001190 if (!tcfg->endpoints)
1191 return -1;
1192
Philipp Maierfcd06552017-11-10 17:32:22 +01001193 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001194 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001195 tcfg->endpoints[i].cfg = tcfg->cfg;
1196 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001197
1198 /* NOTE: Currently all endpoints are of type RTP, this will
1199 * change when new variations are implemented */
1200 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001201 }
1202
Philipp Maierfcd06552017-11-10 17:32:22 +01001203 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001204 return 0;
1205}
1206
Harald Welte1d1b98f2017-12-25 10:03:40 +01001207/*! release endpoint, all open connections are closed.
Philipp Maier87bd9be2017-08-22 16:35:41 +02001208 * \param[in] endp endpoint to release */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001209void mgcp_release_endp(struct mgcp_endpoint *endp)
1210{
Philipp Maier230e4fc2017-11-28 09:38:45 +01001211 LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint:0x%x\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001212 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001213
Harald Welte1d1b98f2017-12-25 10:03:40 +01001214 /* Normally this function should only be called when
Philipp Maier87bd9be2017-08-22 16:35:41 +02001215 * all connections have been removed already. In case
1216 * that there are still connections open (e.g. when
1217 * RSIP is executed), free them all at once. */
1218 mgcp_conn_free_all(endp);
1219
1220 /* Reset endpoint parameters and states */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001221 talloc_free(endp->callid);
1222 endp->callid = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001223 talloc_free(endp->local_options.string);
1224 endp->local_options.string = NULL;
1225 talloc_free(endp->local_options.codec);
1226 endp->local_options.codec = NULL;
Philipp Maier55295f72018-01-15 14:00:28 +01001227 endp->wildcarded_crcx = false;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001228}
1229
1230static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1231{
1232 return write(cfg->gw_fd.bfd.fd, buf, len);
1233}
1234
Philipp Maier87bd9be2017-08-22 16:35:41 +02001235/*! Reset all endpoints by sending RSIP message to self.
1236 * (called by VTY)
1237 * \param[in] endp trunk endpoint
1238 * \param[in] endpoint number
1239 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001240int mgcp_send_reset_all(struct mgcp_config *cfg)
1241{
Philipp Maier12943ea2018-01-17 15:40:25 +01001242 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1243 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001244 int rc;
1245
Philipp Maier12943ea2018-01-17 15:40:25 +01001246 len = snprintf(buf, sizeof(buf),
1247 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1248 if (len < 0)
1249 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001250
Philipp Maier12943ea2018-01-17 15:40:25 +01001251 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001252 if (rc <= 0)
1253 return -1;
1254
1255 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001256}
1257
Philipp Maier87bd9be2017-08-22 16:35:41 +02001258/*! Reset a single endpoint by sending RSIP message to self.
1259 * (called by VTY)
1260 * \param[in] endp trunk endpoint
1261 * \param[in] endpoint number
1262 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001263int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1264{
Philipp Maier12943ea2018-01-17 15:40:25 +01001265 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001266 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001267 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001268
1269 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001270 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001271 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001272 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001273
Philipp Maier87bd9be2017-08-22 16:35:41 +02001274 rc = send_agent(endp->cfg, buf, len);
1275 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001276 return -1;
1277
Philipp Maier87bd9be2017-08-22 16:35:41 +02001278 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001279}