blob: 71c0fdecfdd57be2cc14803a39333777e1dd8a68 [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 Maier87bd9be2017-08-22 16:35:41 +0200195/* Format MGCP response string (with SDP attached) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200196static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200197 struct mgcp_conn_rtp *conn,
198 const char *msg,
199 const char *trans_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200200{
201 const char *addr = endp->cfg->local_ip;
Philipp Maier8970c492017-10-11 13:33:42 +0200202 struct msgb *sdp;
203 int rc;
204 struct msgb *result;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200205 char osmux_extension[strlen("\nX-Osmux: 255") + 1];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100206 char local_ip_addr[INET_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200207
Philipp Maier8970c492017-10-11 13:33:42 +0200208 sdp = msgb_alloc_headroom(4096, 128, "sdp record");
209 if (!sdp)
210 return NULL;
211
Philipp Maier1cb1e382017-11-02 17:16:04 +0100212 if (!addr) {
213 mgcp_get_local_addr(local_ip_addr, conn);
214 addr = local_ip_addr;
215 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200216
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
218 sprintf(osmux_extension, "\nX-Osmux: %u", conn->osmux.cid);
219 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220 } else {
221 osmux_extension[0] = '\0';
222 }
223
Philipp Maier01d24a32017-11-21 17:26:09 +0100224 rc = msgb_printf(sdp, "I: %s%s\n\n", conn->conn->id, osmux_extension);
Philipp Maier8970c492017-10-11 13:33:42 +0200225 if (rc < 0)
226 goto error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227
Philipp Maier8970c492017-10-11 13:33:42 +0200228 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
229 if (rc < 0)
230 goto error;
231 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
232 msgb_free(sdp);
233 return result;
234error:
235 msgb_free(sdp);
236 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237}
238
Philipp Maier87bd9be2017-08-22 16:35:41 +0200239/* Send out dummy packet to keep the connection open, if the connection is an
240 * osmux connection, send the dummy packet via OSMUX */
241static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200243 if (conn->osmux.state != OSMUX_STATE_DISABLED)
244 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200245 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200247}
248
Philipp Maier87bd9be2017-08-22 16:35:41 +0200249/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200250 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200251 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200252struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
253{
254 struct mgcp_parse_data pdata;
255 int i, code, handled = 0;
256 struct msgb *resp = NULL;
257 char *data;
258
259 if (msgb_l2len(msg) < 4) {
260 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
261 return NULL;
262 }
263
264 if (mgcp_msg_terminate_nul(msg))
265 return NULL;
266
Philipp Maier87bd9be2017-08-22 16:35:41 +0200267 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200268
Philipp Maier87bd9be2017-08-22 16:35:41 +0200269 /* attempt to treat it as a response */
270 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200271 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
272 return NULL;
273 }
274
275 msg->l3h = &msg->l2h[4];
276
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200277 /*
278 * Check for a duplicate message and respond.
279 */
280 memset(&pdata, 0, sizeof(pdata));
281 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200282 data = mgcp_strline((char *)msg->l3h, &pdata.save);
283 pdata.found = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200284 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200285 && pdata.endp->last_trans
286 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200287 return do_retransmission(pdata.endp);
288 }
289
Harald Welteabbb6b92017-12-28 13:13:50 +0100290 /* check for general parser failure */
291 if (pdata.found < 0) {
292 LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h);
293 return create_err_response(NULL, -pdata.found, (const char *) msg->l2h, pdata.trans);
294 }
295
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200296 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200297 if (strncmp
298 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
299 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200300 handled = 1;
301 resp = mgcp_requests[i].handle_request(&pdata);
302 break;
303 }
304 }
305
306 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200307 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
308 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200309
310 return resp;
311}
312
Philipp Maier87bd9be2017-08-22 16:35:41 +0200313/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200314static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
315{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200316 LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100317 return create_ok_response(p->endp, 200, "AUEP", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200318}
319
Harald Welte1d1b98f2017-12-25 10:03:40 +0100320/* Try to find a free port by attempting to bind on it. Also handle the
Philipp Maier87bd9be2017-08-22 16:35:41 +0200321 * counter that points on the next free port. Since we have a pointer
Harald Welte1d1b98f2017-12-25 10:03:40 +0100322 * to the next free port, binding should work on the first attempt,
323 * nevertheless, try at least the next 200 ports before giving up */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200324static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200325{
326 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200327 struct mgcp_rtp_end *end;
328 struct mgcp_port_range *range;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200329
Philipp Maier87bd9be2017-08-22 16:35:41 +0200330 OSMO_ASSERT(conn);
331 end = &conn->end;
332 OSMO_ASSERT(end);
333
334 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200335
336 /* attempt to find a port */
337 for (i = 0; i < 200; ++i) {
338 int rc;
339
340 if (range->last_port >= range->range_end)
341 range->last_port = range->range_start;
342
Philipp Maier87bd9be2017-08-22 16:35:41 +0200343 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200344
345 range->last_port += 2;
346 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200347 return 0;
348 }
349
350 }
351
Philipp Maier87bd9be2017-08-22 16:35:41 +0200352 LOGP(DLMGCP, LOGL_ERROR,
353 "Allocating a RTP/RTCP port failed 200 times 0x%x.\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200354 ENDPOINT_NUMBER(endp));
355 return -1;
356}
357
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100359 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200360 * like an empty string, the 'string' field is never NULL after this function
361 * has been called. */
362static void set_local_cx_options(void *ctx, struct mgcp_lco *lco,
363 const char *options)
364{
365 char *p_opt, *a_opt;
366 char codec[9];
367
368 talloc_free(lco->string);
369 talloc_free(lco->codec);
370 lco->codec = NULL;
371 lco->pkt_period_min = lco->pkt_period_max = 0;
372 lco->string = talloc_strdup(ctx, options ? options : "");
373
374 p_opt = strstr(lco->string, "p:");
375 if (p_opt && sscanf(p_opt, "p:%d-%d",
376 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
377 lco->pkt_period_max = lco->pkt_period_min;
378
379 a_opt = strstr(lco->string, "a:");
380 if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1)
381 lco->codec = talloc_strdup(ctx, codec);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200382
383 LOGP(DLMGCP, LOGL_DEBUG,
384 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
385 lco->pkt_period_max, lco->codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200386}
387
388void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
389 struct mgcp_rtp_end *rtp)
390{
391 struct mgcp_trunk_config *tcfg = endp->tcfg;
392
393 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
394
395 rtp->force_aligned_timing = tcfg->force_aligned_timing;
396 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
397
398 LOGP(DLMGCP, LOGL_DEBUG,
399 "Configuring RTP endpoint: local port %d%s%s\n",
400 ntohs(rtp->rtp_port),
401 rtp->force_aligned_timing ? ", force constant timing" : "",
402 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
403}
404
405uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
406 struct mgcp_rtp_end *rtp)
407{
408 int f = 0;
409
410 /* Get the number of frames per channel and packet */
411 if (rtp->frames_per_packet)
412 f = rtp->frames_per_packet;
413 else if (rtp->packet_duration_ms && rtp->codec.frame_duration_num) {
414 int den = 1000 * rtp->codec.frame_duration_num;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200415 f = (rtp->packet_duration_ms * rtp->codec.frame_duration_den +
416 den / 2)
417 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200418 }
419
Philipp Maier87bd9be2017-08-22 16:35:41 +0200420 return rtp->codec.rate * f * rtp->codec.frame_duration_num /
421 rtp->codec.frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200422}
423
424static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
425{
426 if (!endp->cfg->osmux_init) {
427 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
428 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
429 return -1;
430 }
431 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
432 }
433
434 return mgcp_parse_osmux_cid(line);
435}
436
Philipp Maier87bd9be2017-08-22 16:35:41 +0200437/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200438static struct msgb *handle_create_con(struct mgcp_parse_data *p)
439{
440 struct mgcp_trunk_config *tcfg;
441 struct mgcp_endpoint *endp = p->endp;
442 int error_code = 400;
443
444 const char *local_options = NULL;
445 const char *callid = NULL;
446 const char *mode = NULL;
447 char *line;
448 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200449 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100450 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200451 char conn_name[512];
452
453 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200454
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200455 /* parse CallID C: and LocalParameters L: */
456 for_each_line(line, p->save) {
457 if (!mgcp_check_param(endp, line))
458 continue;
459
460 switch (line[0]) {
461 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200462 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200463 break;
464 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200465 callid = (const char *)line + 3;
466 break;
467 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100468 /* It is illegal to send a connection identifier
469 * together with a CRCX, the MGW will assign the
470 * connection identifier by itself on CRCX */
471 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200472 break;
473 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200474 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200475 break;
476 case 'X':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200477 /* If osmoux is disabled, just skip setting it up */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200478 if (!p->endp->cfg->osmux)
479 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200480 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) ==
481 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200482 osmux_cid = mgcp_osmux_setup(endp, line);
483 break;
484 case '\0':
485 have_sdp = 1;
486 goto mgcp_header_done;
487 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200488 LOGP(DLMGCP, LOGL_NOTICE,
489 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
490 ENDPOINT_NUMBER(endp), *line, *line);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200491 break;
492 }
493 }
494
495mgcp_header_done:
496 tcfg = p->endp->tcfg;
497
Philipp Maier87bd9be2017-08-22 16:35:41 +0200498 /* Check parameters */
499 if (!callid) {
500 LOGP(DLMGCP, LOGL_ERROR,
501 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200502 ENDPOINT_NUMBER(endp));
503 return create_err_response(endp, 400, "CRCX", p->trans);
504 }
505
Philipp Maier87bd9be2017-08-22 16:35:41 +0200506 if (!mode) {
507 LOGP(DLMGCP, LOGL_ERROR,
508 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
509 ENDPOINT_NUMBER(endp));
510 return create_err_response(endp, 400, "CRCX", p->trans);
511 }
512
Philipp Maier87bd9be2017-08-22 16:35:41 +0200513 /* Check if we are able to accept the creation of another connection */
514 if (llist_count(&endp->conns) >= endp->type->max_conns) {
515 LOGP(DLMGCP, LOGL_ERROR,
516 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
517 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200518 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200519 /* There is no more room for a connection, make some
520 * room by blindly tossing the oldest of the two two
521 * connections */
522 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200523 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200524 /* There is no more room for a connection, leave
525 * everything as it is and return with an error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200526 return create_err_response(endp, 400, "CRCX", p->trans);
527 }
528 }
529
Philipp Maier87bd9be2017-08-22 16:35:41 +0200530 /* Check if this endpoint already serves a call, if so, check if the
531 * callids match up so that we are sure that this is our call */
532 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
533 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100534 "CRCX: endpoint:0x%x allready seized by other call (%s)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200535 ENDPOINT_NUMBER(endp), endp->callid);
536 if (tcfg->force_realloc)
537 /* This is not our call, toss everything by releasing
538 * the entire endpoint. (rude!) */
539 mgcp_release_endp(endp);
540 else {
541 /* This is not our call, leave everything as it is and
542 * return with an error. */
543 return create_err_response(endp, 400, "CRCX", p->trans);
544 }
545 }
546
547 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100548 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200549 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200550 endp->callid = talloc_strdup(tcfg->endpoints, callid);
551
Philipp Maier87bd9be2017-08-22 16:35:41 +0200552 /* Extract audio codec information */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
554 local_options);
555
Philipp Maierffd75e42017-11-22 11:44:50 +0100556 snprintf(conn_name, sizeof(conn_name), "%s", callid);
557 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
558 if (!_conn) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200559 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100560 "CRCX: endpoint:0x%x unable to allocate RTP connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200561 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200562 goto error2;
563
Philipp Maier87bd9be2017-08-22 16:35:41 +0200564 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100565 conn = mgcp_conn_get_rtp(endp, _conn->id);
566 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200567
568 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
569 error_code = 517;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200570 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200571 }
572
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200573 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200574 * is fully set up from the dummy load. */
575 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200576 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200577 conn->osmux.cid = osmux_cid;
578 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200579 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
580 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100581 "CRCX: endpoint:0x%x osmux only and no osmux offered\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200582 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200583 goto error2;
584 }
585
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200586 /* set up RTP media parameters */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200587 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200588 mgcp_parse_sdp_data(endp, conn, p);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200589 else if (endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200590 mgcp_set_audio_info(p->cfg, &conn->end.codec,
591 PTYPE_UNDEFINED, endp->local_options.codec);
592 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
593 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200594
Philipp Maier87bd9be2017-08-22 16:35:41 +0200595 if (p->cfg->force_ptime) {
596 conn->end.packet_duration_ms = p->cfg->force_ptime;
597 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200598 }
599
Philipp Maier1cb1e382017-11-02 17:16:04 +0100600 mgcp_rtp_end_config(endp, 0, &conn->end);
601
602 if (allocate_port(endp, conn) != 0) {
603 goto error2;
604 }
605
Philipp Maier87bd9be2017-08-22 16:35:41 +0200606 if (setup_rtp_processing(endp, conn) != 0) {
607 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100608 "CRCX: endpoint:0x%x could not start RTP processing!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200609 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200610 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200611 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200612
613 /* policy CB */
614 if (p->cfg->policy_cb) {
615 int rc;
616 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200617 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200618 switch (rc) {
619 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200620 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100621 "CRCX: endpoint:0x%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200622 ENDPOINT_NUMBER(endp));
623 mgcp_release_endp(endp);
624 return create_err_response(endp, 400, "CRCX", p->trans);
625 break;
626 case MGCP_POLICY_DEFER:
627 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200628 return NULL;
629 break;
630 case MGCP_POLICY_CONT:
631 /* just continue */
632 break;
633 }
634 }
635
Philipp Maier87bd9be2017-08-22 16:35:41 +0200636 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100637 "CRCX: endpoint:0x%x Creating connection: CI: %s port: %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200638 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200639 if (p->cfg->change_cb)
640 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
641
Philipp Maiere726d4f2017-11-01 10:41:34 +0100642 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
643 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200644 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +0100645 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200646 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200647
Philipp Maier87bd9be2017-08-22 16:35:41 +0200648 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100649 "CRCX: endpoint:0x%x connection successfully created\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200650 ENDPOINT_NUMBER(endp));
651 return create_response_with_sdp(endp, conn, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200652error2:
653 mgcp_release_endp(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200654 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100655 "CRCX: endpoint:0x%x unable to create connection resource error\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200656 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200657 return create_err_response(endp, error_code, "CRCX", p->trans);
658}
659
Philipp Maier87bd9be2017-08-22 16:35:41 +0200660/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200661static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
662{
663 struct mgcp_endpoint *endp = p->endp;
664 int error_code = 500;
665 int silent = 0;
666 int have_sdp = 0;
667 char *line;
668 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200669 const char *mode = NULL;
670 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +0100671 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200672
673 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200674
Philipp Maier87bd9be2017-08-22 16:35:41 +0200675 if (llist_count(&endp->conns) <= 0) {
676 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100677 "MDCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200678 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200679 return create_err_response(endp, 400, "MDCX", p->trans);
680 }
681
682 for_each_line(line, p->save) {
683 if (!mgcp_check_param(endp, line))
684 continue;
685
686 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200687 case 'C':
688 if (mgcp_verify_call_id(endp, line + 3) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200689 goto error3;
690 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200691 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +0100692 conn_id = (const char *)line + 3;
693 if (mgcp_verify_ci(endp, conn_id) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200694 goto error3;
695 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200696 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200697 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200698 break;
699 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200700 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200701 break;
702 case 'Z':
703 silent = strcmp("noanswer", line + 3) == 0;
704 break;
705 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200706 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200707 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200708 break;
709 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200710 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100711 "MDCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200712 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200713 break;
714 }
715 }
716
Philipp Maier87bd9be2017-08-22 16:35:41 +0200717mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +0100718 if (!conn_id) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200719 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100720 "MDCX: endpoint:0x%x insufficient parameters, missing ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200721 ENDPOINT_NUMBER(endp));
722 return create_err_response(endp, 400, "MDCX", p->trans);
723 }
724
725 conn = mgcp_conn_get_rtp(endp, conn_id);
726 if (!conn)
727 return create_err_response(endp, 400, "MDCX", p->trans);
728
729 if (mode) {
730 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
731 error_code = 517;
732 goto error3;
733 }
734 } else
735 conn->conn->mode = conn->conn->mode_orig;
736
737 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200738 mgcp_parse_sdp_data(endp, conn, p);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200739
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200740 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
741 local_options);
742
743 if (!have_sdp && endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200744 mgcp_set_audio_info(p->cfg, &conn->end.codec,
745 PTYPE_UNDEFINED, endp->local_options.codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746
Philipp Maier87bd9be2017-08-22 16:35:41 +0200747 if (setup_rtp_processing(endp, conn) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200748 goto error3;
749
Philipp Maier87bd9be2017-08-22 16:35:41 +0200750
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200751 /* policy CB */
752 if (p->cfg->policy_cb) {
753 int rc;
754 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200755 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200756 switch (rc) {
757 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200758 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100759 "MDCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200760 ENDPOINT_NUMBER(endp));
761 if (silent)
762 goto out_silent;
763 return create_err_response(endp, 400, "MDCX", p->trans);
764 break;
765 case MGCP_POLICY_DEFER:
766 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200767 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100768 "MDCX: endpoint:0x%x defered by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200769 ENDPOINT_NUMBER(endp));
770 return NULL;
771 break;
772 case MGCP_POLICY_CONT:
773 /* just continue */
774 break;
775 }
776 }
777
Philipp Maier87bd9be2017-08-22 16:35:41 +0200778 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200779
780 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200781 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100782 "MDCX: endpoint:0x%x modified conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200783 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200784 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200785 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
786 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200787
Philipp Maiere726d4f2017-11-01 10:41:34 +0100788 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
789 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
790 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
791 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200792 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200793
794 if (silent)
795 goto out_silent;
796
Philipp Maier87bd9be2017-08-22 16:35:41 +0200797 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100798 "MDCX: endpoint:0x%x connection successfully modified\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200799 ENDPOINT_NUMBER(endp));
800 return create_response_with_sdp(endp, conn, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200801error3:
802 return create_err_response(endp, error_code, "MDCX", p->trans);
803
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200804out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +0100805 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:0x%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200806 ENDPOINT_NUMBER(endp));
807 return NULL;
808}
809
Philipp Maier87bd9be2017-08-22 16:35:41 +0200810/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
812{
813 struct mgcp_endpoint *endp = p->endp;
814 int error_code = 400;
815 int silent = 0;
816 char *line;
817 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +0100818 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200819 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200820
821 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100822 "DLCX: endpoint:0x%x deleting connection ...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200823 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200824
Philipp Maier87bd9be2017-08-22 16:35:41 +0200825 if (llist_count(&endp->conns) <= 0) {
826 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100827 "DLCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200828 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200829 return create_err_response(endp, 400, "DLCX", p->trans);
830 }
831
832 for_each_line(line, p->save) {
833 if (!mgcp_check_param(endp, line))
834 continue;
835
836 switch (line[0]) {
837 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200838 if (mgcp_verify_call_id(endp, line + 3) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200839 goto error3;
840 break;
841 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +0100842 conn_id = (const char *)line + 3;
843 if (mgcp_verify_ci(endp, conn_id) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200844 goto error3;
845 break;
846 case 'Z':
847 silent = strcmp("noanswer", line + 3) == 0;
848 break;
849 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200850 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100851 "DLCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200852 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853 break;
854 }
855 }
856
857 /* policy CB */
858 if (p->cfg->policy_cb) {
859 int rc;
860 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200861 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200862 switch (rc) {
863 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200864 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100865 "DLCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200866 ENDPOINT_NUMBER(endp));
867 if (silent)
868 goto out_silent;
869 return create_err_response(endp, 400, "DLCX", p->trans);
870 break;
871 case MGCP_POLICY_DEFER:
872 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200873 return NULL;
874 break;
875 case MGCP_POLICY_CONT:
876 /* just continue */
877 break;
878 }
879 }
880
Philipp Maierf4c0e372017-10-11 16:06:45 +0200881 /* When no connection id is supplied, we will interpret this as a
882 * wildcarded DLCX and drop all connections at once. (See also
883 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +0100884 if (!conn_id) {
Philipp Maierf4c0e372017-10-11 16:06:45 +0200885 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100886 "DLCX: endpoint:0x%x missing ci (connectionIdentifier), will remove all connections at once\n",
Philipp Maierf4c0e372017-10-11 16:06:45 +0200887 ENDPOINT_NUMBER(endp));
888
889 mgcp_release_endp(endp);
890
891 /* Note: In this case we do not return any statistics,
892 * as we assume that the client is not interested in
893 * this case. */
894 return create_ok_response(endp, 200, "DLCX", p->trans);
895 }
896
Philipp Maierf4c0e372017-10-11 16:06:45 +0200897 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200898 conn = mgcp_conn_get_rtp(endp, conn_id);
899 if (!conn)
900 goto error3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200901
Philipp Maier87bd9be2017-08-22 16:35:41 +0200902 /* save the statistics of the current connection */
903 mgcp_format_stats(stats, sizeof(stats), conn->conn);
904
905 /* delete connection */
Philipp Maier230e4fc2017-11-28 09:38:45 +0100906 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x deleting conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200907 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
908 mgcp_conn_free(endp, conn_id);
909 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100910 "DLCX: endpoint:0x%x connection successfully deleted\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200911 ENDPOINT_NUMBER(endp));
912
913 /* When all connections are closed, the endpoint will be released
914 * in order to be ready to be used by another call. */
915 if (llist_count(&endp->conns) <= 0) {
916 mgcp_release_endp(endp);
917 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100918 "DLCX: endpoint:0x%x endpoint released\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200919 ENDPOINT_NUMBER(endp));
920 }
921
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200922 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200923 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
924 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200925
926 if (silent)
927 goto out_silent;
928 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
929
930error3:
931 return create_err_response(endp, error_code, "DLCX", p->trans);
932
933out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +0100934 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x silent exit\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200935 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200936 return NULL;
937}
938
Philipp Maier87bd9be2017-08-22 16:35:41 +0200939/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200940static struct msgb *handle_rsip(struct mgcp_parse_data *p)
941{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200942 /* TODO: Also implement the resetting of a specific endpoint
943 * to make mgcp_send_reset_ep() work. Currently this will call
944 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
945 * to make read_call_agent() reset all endpoints when called
946 * next time. In order to selectively reset endpoints some
947 * mechanism to distinguish which endpoint shall be resetted
948 * is needed */
949
950 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
951
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200952 if (p->cfg->reset_cb)
953 p->cfg->reset_cb(p->endp->tcfg);
954 return NULL;
955}
956
957static char extract_tone(const char *line)
958{
959 const char *str = strstr(line, "D/");
960 if (!str)
961 return CHAR_MAX;
962
963 return str[2];
964}
965
Philipp Maier87bd9be2017-08-22 16:35:41 +0200966/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200967 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +0200968 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200969static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
970{
971 int res = 0;
972 char *line;
973 char tone = CHAR_MAX;
974
Philipp Maier87bd9be2017-08-22 16:35:41 +0200975 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
976
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200977 for_each_line(line, p->save) {
978 switch (line[0]) {
979 case 'S':
980 tone = extract_tone(line);
981 break;
982 }
983 }
984
985 /* we didn't see a signal request with a tone */
986 if (tone == CHAR_MAX)
987 return create_ok_response(p->endp, 200, "RQNT", p->trans);
988
989 if (p->cfg->rqnt_cb)
990 res = p->cfg->rqnt_cb(p->endp, tone);
991
992 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +0200993 create_ok_response(p->endp, 200, "RQNT", p->trans) :
994 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200995}
996
Philipp Maier87bd9be2017-08-22 16:35:41 +0200997/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +0100998 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200999static void mgcp_keepalive_timer_cb(void *_tcfg)
1000{
1001 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001002 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001003 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001004
Philipp Maiere726d4f2017-11-01 10:41:34 +01001005 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001006 tcfg->trunk_nr);
1007
Philipp Maiere726d4f2017-11-01 10:41:34 +01001008 /* Do not accept invalid configuration values
1009 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1010 * values greater 0 */
1011 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1012
1013 /* The dummy packet functionality has been disabled, we will exit
1014 * immediately, no further timer is scheduled, which means we will no
1015 * longer send dummy packets even when we did before */
1016 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1017 return;
1018
1019 /* In cases where only one dummy packet is sent, we do not need
1020 * the timer since the functions that handle the CRCX and MDCX are
1021 * triggering the sending of the dummy packet. So we behave like in
1022 * the MGCP_KEEPALIVE_NEVER case */
1023 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001024 return;
1025
Philipp Maier87bd9be2017-08-22 16:35:41 +02001026 /* Send walk over all endpoints and send out dummy packets through
1027 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001028 for (i = 1; i < tcfg->number_endpoints; ++i) {
1029 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001030 llist_for_each_entry(conn, &endp->conns, entry) {
1031 if (conn->mode == MGCP_CONN_RECV_ONLY)
1032 send_dummy(endp, &conn->u.rtp);
1033 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001034 }
1035
Philipp Maiere726d4f2017-11-01 10:41:34 +01001036 /* Schedule the keepalive timer for the next round */
1037 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001038 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001039 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1040 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001041}
1042
1043void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1044{
1045 tcfg->keepalive_interval = interval;
1046 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1047
1048 if (interval <= 0)
1049 osmo_timer_del(&tcfg->keepalive_timer);
1050 else
1051 osmo_timer_schedule(&tcfg->keepalive_timer,
1052 tcfg->keepalive_interval, 0);
1053}
1054
Philipp Maier87bd9be2017-08-22 16:35:41 +02001055/*! allocate configuration with default values.
1056 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001057struct mgcp_config *mgcp_config_alloc(void)
1058{
1059 struct mgcp_config *cfg;
1060
1061 cfg = talloc_zero(NULL, struct mgcp_config);
1062 if (!cfg) {
1063 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1064 return NULL;
1065 }
1066
Philipp Maier87bd9be2017-08-22 16:35:41 +02001067 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1068 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1069 cfg->net_ports.last_port = cfg->net_ports.range_start;
1070
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001071 cfg->source_port = 2427;
1072 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1073 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1074
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001075 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1076 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1077
1078 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1079
1080 /* default trunk handling */
1081 cfg->trunk.cfg = cfg;
1082 cfg->trunk.trunk_nr = 0;
1083 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1084 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1085 cfg->trunk.audio_payload = 126;
1086 cfg->trunk.audio_send_ptime = 1;
1087 cfg->trunk.audio_send_name = 1;
1088 cfg->trunk.omit_rtcp = 0;
1089 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
1090
1091 INIT_LLIST_HEAD(&cfg->trunks);
1092
1093 return cfg;
1094}
1095
Philipp Maier87bd9be2017-08-22 16:35:41 +02001096/*! allocate configuration with default values.
1097 * (called once at startup by VTY)
1098 * \param[in] cfg mgcp configuration
1099 * \param[in] nr trunk number
1100 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001101struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1102{
1103 struct mgcp_trunk_config *trunk;
1104
1105 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1106 if (!trunk) {
1107 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1108 return NULL;
1109 }
1110
1111 trunk->cfg = cfg;
1112 trunk->trunk_type = MGCP_TRUNK_E1;
1113 trunk->trunk_nr = nr;
1114 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1115 trunk->audio_payload = 126;
1116 trunk->audio_send_ptime = 1;
1117 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001118 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001119 trunk->omit_rtcp = 0;
1120 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
1121 llist_add_tail(&trunk->entry, &cfg->trunks);
1122 return trunk;
1123}
1124
Philipp Maier87bd9be2017-08-22 16:35:41 +02001125/*! get trunk configuration by trunk number (index).
1126 * \param[in] cfg mgcp configuration
1127 * \param[in] index trunk number
1128 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001129struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1130{
1131 struct mgcp_trunk_config *trunk;
1132
1133 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001134 if (trunk->trunk_nr == index)
1135 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001136
1137 return NULL;
1138}
1139
Philipp Maier87bd9be2017-08-22 16:35:41 +02001140/*! allocate endpoints and set default values.
1141 * (called once at startup by VTY)
1142 * \param[in] tcfg trunk configuration
1143 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001144int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1145{
1146 int i;
1147
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001148 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001149 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001150 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001151 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001152 if (!tcfg->endpoints)
1153 return -1;
1154
Philipp Maierfcd06552017-11-10 17:32:22 +01001155 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001156 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001157 tcfg->endpoints[i].cfg = tcfg->cfg;
1158 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001159
1160 /* NOTE: Currently all endpoints are of type RTP, this will
1161 * change when new variations are implemented */
1162 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001163 }
1164
Philipp Maierfcd06552017-11-10 17:32:22 +01001165 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001166 return 0;
1167}
1168
Harald Welte1d1b98f2017-12-25 10:03:40 +01001169/*! release endpoint, all open connections are closed.
Philipp Maier87bd9be2017-08-22 16:35:41 +02001170 * \param[in] endp endpoint to release */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001171void mgcp_release_endp(struct mgcp_endpoint *endp)
1172{
Philipp Maier230e4fc2017-11-28 09:38:45 +01001173 LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint:0x%x\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001174 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001175
Harald Welte1d1b98f2017-12-25 10:03:40 +01001176 /* Normally this function should only be called when
Philipp Maier87bd9be2017-08-22 16:35:41 +02001177 * all connections have been removed already. In case
1178 * that there are still connections open (e.g. when
1179 * RSIP is executed), free them all at once. */
1180 mgcp_conn_free_all(endp);
1181
1182 /* Reset endpoint parameters and states */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001183 talloc_free(endp->callid);
1184 endp->callid = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185 talloc_free(endp->local_options.string);
1186 endp->local_options.string = NULL;
1187 talloc_free(endp->local_options.codec);
1188 endp->local_options.codec = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001189}
1190
1191static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1192{
1193 return write(cfg->gw_fd.bfd.fd, buf, len);
1194}
1195
Philipp Maier87bd9be2017-08-22 16:35:41 +02001196/*! Reset all endpoints by sending RSIP message to self.
1197 * (called by VTY)
1198 * \param[in] endp trunk endpoint
1199 * \param[in] endpoint number
1200 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001201int mgcp_send_reset_all(struct mgcp_config *cfg)
1202{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001203 int rc;
1204
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001205 static const char mgcp_reset[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001206 "RSIP 1 *@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001207 };
1208
Philipp Maier87bd9be2017-08-22 16:35:41 +02001209 rc = send_agent(cfg, mgcp_reset, sizeof mgcp_reset - 1);
1210 if (rc <= 0)
1211 return -1;
1212
1213 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001214}
1215
Philipp Maier87bd9be2017-08-22 16:35:41 +02001216/*! Reset a single endpoint by sending RSIP message to self.
1217 * (called by VTY)
1218 * \param[in] endp trunk endpoint
1219 * \param[in] endpoint number
1220 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001221int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1222{
1223 char buf[128];
1224 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001225 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001226
1227 len = snprintf(buf, sizeof(buf),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001228 "RSIP 39 %x@mgw MGCP 1.0\r\n", endpoint);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001229 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001230 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001231
1232 buf[sizeof(buf) - 1] = '\0';
1233
Philipp Maier87bd9be2017-08-22 16:35:41 +02001234 rc = send_agent(endp->cfg, buf, len);
1235 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001236 return -1;
1237
Philipp Maier87bd9be2017-08-22 16:35:41 +02001238 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001239}