blob: f54274540dc498ca11b3ad7c37962ee285744568 [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 Maier8970c492017-10-11 13:33:42 +0200224 rc = msgb_printf(sdp, "I: %u%s\n\n", conn->conn->id, osmux_extension);
225 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
290 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200291 if (strncmp
292 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
293 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200294 handled = 1;
295 resp = mgcp_requests[i].handle_request(&pdata);
296 break;
297 }
298 }
299
300 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200301 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
302 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200303
304 return resp;
305}
306
Philipp Maier87bd9be2017-08-22 16:35:41 +0200307/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200308static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
309{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200310 LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
311
312 if (p->found != 0) {
313 LOGP(DLMGCP, LOGL_ERROR,
314 "AUEP: failed to find the endpoint.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200315 return create_err_response(NULL, 500, "AUEP", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200316 } else
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200317 return create_ok_response(p->endp, 200, "AUEP", p->trans);
318}
319
Philipp Maier87bd9be2017-08-22 16:35:41 +0200320/* Try to find a free port by attemting to bind on it. Also handle the
321 * counter that points on the next free port. Since we have a pointer
322 * to the next free port, binding should work on the first attemt,
323 * neverless, try at least the next 200 ports before giving up */
324static 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).
Philipp Maier87bd9be2017-08-22 16:35:41 +0200359 * The string is stored in the 'string' field. A NULL string is handled excatlyy
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;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200446 const char *ci = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200447 const char *mode = NULL;
448 char *line;
449 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200450 struct mgcp_conn_rtp *conn = NULL;
451 uint32_t conn_id;
452 char conn_name[512];
453
454 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200455
456 if (p->found != 0)
457 return create_err_response(NULL, 510, "CRCX", p->trans);
458
459 /* parse CallID C: and LocalParameters L: */
460 for_each_line(line, p->save) {
461 if (!mgcp_check_param(endp, line))
462 continue;
463
464 switch (line[0]) {
465 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200466 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200467 break;
468 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200469 callid = (const char *)line + 3;
470 break;
471 case 'I':
472 ci = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200473 break;
474 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200475 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200476 break;
477 case 'X':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478 /* If osmoux is disabled, just skip setting it up */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200479 if (!p->endp->cfg->osmux)
480 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200481 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) ==
482 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200483 osmux_cid = mgcp_osmux_setup(endp, line);
484 break;
485 case '\0':
486 have_sdp = 1;
487 goto mgcp_header_done;
488 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200489 LOGP(DLMGCP, LOGL_NOTICE,
490 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
491 ENDPOINT_NUMBER(endp), *line, *line);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200492 break;
493 }
494 }
495
496mgcp_header_done:
497 tcfg = p->endp->tcfg;
498
Philipp Maier87bd9be2017-08-22 16:35:41 +0200499 /* Check parameters */
500 if (!callid) {
501 LOGP(DLMGCP, LOGL_ERROR,
502 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200503 ENDPOINT_NUMBER(endp));
504 return create_err_response(endp, 400, "CRCX", p->trans);
505 }
506
Philipp Maier87bd9be2017-08-22 16:35:41 +0200507 if (!mode) {
508 LOGP(DLMGCP, LOGL_ERROR,
509 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
510 ENDPOINT_NUMBER(endp));
511 return create_err_response(endp, 400, "CRCX", p->trans);
512 }
513
514 if (!ci) {
515 LOGP(DLMGCP, LOGL_ERROR,
516 "CRCX: endpoint:%x insufficient parameters, missing connection id\n",
517 ENDPOINT_NUMBER(endp));
518 return create_err_response(endp, 400, "CRCX", p->trans);
519 }
520
521 /* Check if we are able to accept the creation of another connection */
522 if (llist_count(&endp->conns) >= endp->type->max_conns) {
523 LOGP(DLMGCP, LOGL_ERROR,
524 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
525 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200526 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200527 /* There is no more room for a connection, make some
528 * room by blindly tossing the oldest of the two two
529 * connections */
530 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200531 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200532 /* There is no more room for a connection, leave
533 * everything as it is and return with an error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200534 return create_err_response(endp, 400, "CRCX", p->trans);
535 }
536 }
537
Philipp Maier87bd9be2017-08-22 16:35:41 +0200538 /* Check if this endpoint already serves a call, if so, check if the
539 * callids match up so that we are sure that this is our call */
540 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
541 LOGP(DLMGCP, LOGL_ERROR,
542 "CRCX: endpoint:%x allready seized by other call (%s)\n",
543 ENDPOINT_NUMBER(endp), endp->callid);
544 if (tcfg->force_realloc)
545 /* This is not our call, toss everything by releasing
546 * the entire endpoint. (rude!) */
547 mgcp_release_endp(endp);
548 else {
549 /* This is not our call, leave everything as it is and
550 * return with an error. */
551 return create_err_response(endp, 400, "CRCX", p->trans);
552 }
553 }
554
555 /* Set the callid, creation of another connection will only be possible
556 * when the callid matches up. (Connections are distinuished by their
557 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200558 endp->callid = talloc_strdup(tcfg->endpoints, callid);
559
Philipp Maier87bd9be2017-08-22 16:35:41 +0200560 /* Extract audio codec information */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200561 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
562 local_options);
563
Philipp Maier87bd9be2017-08-22 16:35:41 +0200564 if (mgcp_parse_ci(&conn_id, ci)) {
565 LOGP(DLMGCP, LOGL_ERROR,
566 "CRCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n",
567 ENDPOINT_NUMBER(endp));
568 return create_err_response(endp, 400, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200569 }
570
Philipp Maier87bd9be2017-08-22 16:35:41 +0200571 /* Only accept another connection when the connection ID is different. */
572 if (mgcp_conn_get_rtp(endp, conn_id)) {
573 LOGP(DLMGCP, LOGL_ERROR,
574 "CRCX: endpoint:%x there is already a connection with id %u present!\n",
575 conn_id, ENDPOINT_NUMBER(endp));
576 if (tcfg->force_realloc) {
577 /* Ignore the existing connection by just freeing it */
578 mgcp_conn_free(endp, conn_id);
579 } else {
580 /* There is already a connection with that ID present,
581 * leave everything as it is and return with an error. */
582 return create_err_response(endp, 400, "CRCX", p->trans);
583 }
584 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200585
Philipp Maier87bd9be2017-08-22 16:35:41 +0200586 snprintf(conn_name, sizeof(conn_name), "%s-%u", callid, conn_id);
587 mgcp_conn_alloc(NULL, endp, conn_id, MGCP_CONN_TYPE_RTP,
588 conn_name);
589 conn = mgcp_conn_get_rtp(endp, conn_id);
590 if (!conn) {
591 LOGP(DLMGCP, LOGL_ERROR,
592 "CRCX: endpoint:%x unable to allocate RTP connection\n",
593 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200594 goto error2;
595
Philipp Maier87bd9be2017-08-22 16:35:41 +0200596 }
597
598 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
599 error_code = 517;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200600 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200601 }
602
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200603 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200604 * is fully set up from the dummy load. */
605 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200606 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200607 conn->osmux.cid = osmux_cid;
608 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200609 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
610 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200611 "CRCX: endpoint:%x osmux only and no osmux offered\n",
612 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200613 goto error2;
614 }
615
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200616 /* set up RTP media parameters */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200617 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200618 mgcp_parse_sdp_data(endp, conn, p);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200619 else if (endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200620 mgcp_set_audio_info(p->cfg, &conn->end.codec,
621 PTYPE_UNDEFINED, endp->local_options.codec);
622 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
623 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200624
Philipp Maier87bd9be2017-08-22 16:35:41 +0200625 if (p->cfg->force_ptime) {
626 conn->end.packet_duration_ms = p->cfg->force_ptime;
627 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200628 }
629
Philipp Maier1cb1e382017-11-02 17:16:04 +0100630 mgcp_rtp_end_config(endp, 0, &conn->end);
631
632 if (allocate_port(endp, conn) != 0) {
633 goto error2;
634 }
635
Philipp Maier87bd9be2017-08-22 16:35:41 +0200636 if (setup_rtp_processing(endp, conn) != 0) {
637 LOGP(DLMGCP, LOGL_ERROR,
638 "CRCX: endpoint:%x could not start RTP processing!\n",
639 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200640 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200641 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200642
643 /* policy CB */
644 if (p->cfg->policy_cb) {
645 int rc;
646 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200647 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200648 switch (rc) {
649 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200650 LOGP(DLMGCP, LOGL_NOTICE,
651 "CRCX: endpoint:%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200652 ENDPOINT_NUMBER(endp));
653 mgcp_release_endp(endp);
654 return create_err_response(endp, 400, "CRCX", p->trans);
655 break;
656 case MGCP_POLICY_DEFER:
657 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200658 return NULL;
659 break;
660 case MGCP_POLICY_CONT:
661 /* just continue */
662 break;
663 }
664 }
665
Philipp Maier87bd9be2017-08-22 16:35:41 +0200666 LOGP(DLMGCP, LOGL_DEBUG,
667 "CRCX: endpoint:%x Creating connection: CI: %u port: %u\n",
668 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200669 if (p->cfg->change_cb)
670 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
671
Philipp Maiere726d4f2017-11-01 10:41:34 +0100672 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
673 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200674 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +0100675 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200676 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200677
Philipp Maier87bd9be2017-08-22 16:35:41 +0200678 LOGP(DLMGCP, LOGL_NOTICE,
679 "CRCX: endpoint:%x connection successfully created\n",
680 ENDPOINT_NUMBER(endp));
681 return create_response_with_sdp(endp, conn, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200682error2:
683 mgcp_release_endp(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200684 LOGP(DLMGCP, LOGL_NOTICE,
685 "CRCX: endpoint:%x unable to create connection resource error\n",
686 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200687 return create_err_response(endp, error_code, "CRCX", p->trans);
688}
689
Philipp Maier87bd9be2017-08-22 16:35:41 +0200690/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200691static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
692{
693 struct mgcp_endpoint *endp = p->endp;
694 int error_code = 500;
695 int silent = 0;
696 int have_sdp = 0;
697 char *line;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200698 const char *ci = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200699 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200700 const char *mode = NULL;
701 struct mgcp_conn_rtp *conn = NULL;
702 uint32_t conn_id;
703
704 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200705
706 if (p->found != 0)
707 return create_err_response(NULL, 510, "MDCX", p->trans);
708
Philipp Maier87bd9be2017-08-22 16:35:41 +0200709 if (llist_count(&endp->conns) <= 0) {
710 LOGP(DLMGCP, LOGL_ERROR,
711 "MDCX: endpoint:%x endpoint is not holding a connection.\n",
712 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200713 return create_err_response(endp, 400, "MDCX", p->trans);
714 }
715
716 for_each_line(line, p->save) {
717 if (!mgcp_check_param(endp, line))
718 continue;
719
720 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200721 case 'C':
722 if (mgcp_verify_call_id(endp, line + 3) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200723 goto error3;
724 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200725 case 'I':
726 ci = (const char *)line + 3;
727 if (mgcp_verify_ci(endp, ci) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200728 goto error3;
729 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200730 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200731 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200732 break;
733 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200734 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200735 break;
736 case 'Z':
737 silent = strcmp("noanswer", line + 3) == 0;
738 break;
739 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200740 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200741 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200742 break;
743 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200744 LOGP(DLMGCP, LOGL_NOTICE,
745 "MDCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n",
746 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200747 break;
748 }
749 }
750
Philipp Maier87bd9be2017-08-22 16:35:41 +0200751mgcp_header_done:
752 if (mgcp_parse_ci(&conn_id, ci)) {
753 LOGP(DLMGCP, LOGL_ERROR,
754 "MDCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n",
755 ENDPOINT_NUMBER(endp));
756 return create_err_response(endp, 400, "MDCX", p->trans);
757 }
758
759 conn = mgcp_conn_get_rtp(endp, conn_id);
760 if (!conn)
761 return create_err_response(endp, 400, "MDCX", p->trans);
762
763 if (mode) {
764 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
765 error_code = 517;
766 goto error3;
767 }
768 } else
769 conn->conn->mode = conn->conn->mode_orig;
770
771 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200772 mgcp_parse_sdp_data(endp, conn, p);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200773
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200774 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
775 local_options);
776
777 if (!have_sdp && endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200778 mgcp_set_audio_info(p->cfg, &conn->end.codec,
779 PTYPE_UNDEFINED, endp->local_options.codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200780
Philipp Maier87bd9be2017-08-22 16:35:41 +0200781 if (setup_rtp_processing(endp, conn) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200782 goto error3;
783
Philipp Maier87bd9be2017-08-22 16:35:41 +0200784
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200785 /* policy CB */
786 if (p->cfg->policy_cb) {
787 int rc;
788 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200789 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200790 switch (rc) {
791 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200792 LOGP(DLMGCP, LOGL_NOTICE,
793 "MDCX: endpoint:%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200794 ENDPOINT_NUMBER(endp));
795 if (silent)
796 goto out_silent;
797 return create_err_response(endp, 400, "MDCX", p->trans);
798 break;
799 case MGCP_POLICY_DEFER:
800 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200801 LOGP(DLMGCP, LOGL_DEBUG,
802 "MDCX: endpoint:%x defered by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200803 ENDPOINT_NUMBER(endp));
804 return NULL;
805 break;
806 case MGCP_POLICY_CONT:
807 /* just continue */
808 break;
809 }
810 }
811
Philipp Maier87bd9be2017-08-22 16:35:41 +0200812 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200813
814 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200815 LOGP(DLMGCP, LOGL_DEBUG,
816 "MDCX: endpoint:%x modified conn:%s\n",
817 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200818 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200819 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
820 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200821
Philipp Maiere726d4f2017-11-01 10:41:34 +0100822 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
823 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
824 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
825 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200826 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200827
828 if (silent)
829 goto out_silent;
830
Philipp Maier87bd9be2017-08-22 16:35:41 +0200831 LOGP(DLMGCP, LOGL_NOTICE,
832 "MDCX: endpoint:%x connection successfully modified\n",
833 ENDPOINT_NUMBER(endp));
834 return create_response_with_sdp(endp, conn, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200835error3:
836 return create_err_response(endp, error_code, "MDCX", p->trans);
837
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200838out_silent:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200839 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200840 ENDPOINT_NUMBER(endp));
841 return NULL;
842}
843
Philipp Maier87bd9be2017-08-22 16:35:41 +0200844/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200845static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
846{
847 struct mgcp_endpoint *endp = p->endp;
848 int error_code = 400;
849 int silent = 0;
850 char *line;
851 char stats[1048];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200852 const char *ci = NULL;
853 struct mgcp_conn_rtp *conn = NULL;
854 uint32_t conn_id;
855
Neels Hofmeyr653c4ff2017-11-10 01:41:45 +0100856 if (p->found != 0)
857 return create_err_response(NULL, error_code, "DLCX", p->trans);
858
Philipp Maier87bd9be2017-08-22 16:35:41 +0200859 LOGP(DLMGCP, LOGL_NOTICE,
860 "DLCX: endpoint:%x deleting connection ...\n",
861 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200862
Philipp Maier87bd9be2017-08-22 16:35:41 +0200863 if (llist_count(&endp->conns) <= 0) {
864 LOGP(DLMGCP, LOGL_ERROR,
865 "DLCX: endpoint:%x endpoint is not holding a connection.\n",
866 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200867 return create_err_response(endp, 400, "DLCX", p->trans);
868 }
869
870 for_each_line(line, p->save) {
871 if (!mgcp_check_param(endp, line))
872 continue;
873
874 switch (line[0]) {
875 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200876 if (mgcp_verify_call_id(endp, line + 3) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200877 goto error3;
878 break;
879 case 'I':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200880 ci = (const char *)line + 3;
881 if (mgcp_verify_ci(endp, ci) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200882 goto error3;
883 break;
884 case 'Z':
885 silent = strcmp("noanswer", line + 3) == 0;
886 break;
887 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200888 LOGP(DLMGCP, LOGL_NOTICE,
889 "DLCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n",
890 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200891 break;
892 }
893 }
894
895 /* policy CB */
896 if (p->cfg->policy_cb) {
897 int rc;
898 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200899 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200900 switch (rc) {
901 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200902 LOGP(DLMGCP, LOGL_NOTICE,
903 "DLCX: endpoint:%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200904 ENDPOINT_NUMBER(endp));
905 if (silent)
906 goto out_silent;
907 return create_err_response(endp, 400, "DLCX", p->trans);
908 break;
909 case MGCP_POLICY_DEFER:
910 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200911 return NULL;
912 break;
913 case MGCP_POLICY_CONT:
914 /* just continue */
915 break;
916 }
917 }
918
Philipp Maierf4c0e372017-10-11 16:06:45 +0200919 /* When no connection id is supplied, we will interpret this as a
920 * wildcarded DLCX and drop all connections at once. (See also
921 * RFC3435 Section F.7) */
922 if (!ci) {
923 LOGP(DLMGCP, LOGL_NOTICE,
924 "DLCX: endpoint:%x missing ci (connectionIdentifier), will remove all connections at once\n",
925 ENDPOINT_NUMBER(endp));
926
927 mgcp_release_endp(endp);
928
929 /* Note: In this case we do not return any statistics,
930 * as we assume that the client is not interested in
931 * this case. */
932 return create_ok_response(endp, 200, "DLCX", p->trans);
933 }
934
935 /* Parse the connection id */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200936 if (mgcp_parse_ci(&conn_id, ci)) {
937 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maierf4c0e372017-10-11 16:06:45 +0200938 "DLCX: endpoint:%x insufficient parameters, invalid ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200939 ENDPOINT_NUMBER(endp));
940 return create_err_response(endp, 400, "DLCX", p->trans);
941 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200942
Philipp Maierf4c0e372017-10-11 16:06:45 +0200943 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200944 conn = mgcp_conn_get_rtp(endp, conn_id);
945 if (!conn)
946 goto error3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200947
Philipp Maier87bd9be2017-08-22 16:35:41 +0200948 /* save the statistics of the current connection */
949 mgcp_format_stats(stats, sizeof(stats), conn->conn);
950
951 /* delete connection */
952 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x deleting conn:%s\n",
953 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
954 mgcp_conn_free(endp, conn_id);
955 LOGP(DLMGCP, LOGL_NOTICE,
956 "DLCX: endpoint:%x connection successfully deleted\n",
957 ENDPOINT_NUMBER(endp));
958
959 /* When all connections are closed, the endpoint will be released
960 * in order to be ready to be used by another call. */
961 if (llist_count(&endp->conns) <= 0) {
962 mgcp_release_endp(endp);
963 LOGP(DLMGCP, LOGL_DEBUG,
964 "DLCX: endpoint:%x endpoint released\n",
965 ENDPOINT_NUMBER(endp));
966 }
967
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200968 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200969 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
970 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200971
972 if (silent)
973 goto out_silent;
974 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
975
976error3:
977 return create_err_response(endp, error_code, "DLCX", p->trans);
978
979out_silent:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200980 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x silent exit\n",
981 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200982 return NULL;
983}
984
Philipp Maier87bd9be2017-08-22 16:35:41 +0200985/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200986static struct msgb *handle_rsip(struct mgcp_parse_data *p)
987{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200988 /* TODO: Also implement the resetting of a specific endpoint
989 * to make mgcp_send_reset_ep() work. Currently this will call
990 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
991 * to make read_call_agent() reset all endpoints when called
992 * next time. In order to selectively reset endpoints some
993 * mechanism to distinguish which endpoint shall be resetted
994 * is needed */
995
996 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
997
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200998 if (p->found != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200999 LOGP(DLMGCP, LOGL_ERROR,
1000 "RSIP: failed to find the endpoint.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001001 return NULL;
1002 }
1003
1004 if (p->cfg->reset_cb)
1005 p->cfg->reset_cb(p->endp->tcfg);
1006 return NULL;
1007}
1008
1009static char extract_tone(const char *line)
1010{
1011 const char *str = strstr(line, "D/");
1012 if (!str)
1013 return CHAR_MAX;
1014
1015 return str[2];
1016}
1017
Philipp Maier87bd9be2017-08-22 16:35:41 +02001018/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001019 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001020 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001021static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1022{
1023 int res = 0;
1024 char *line;
1025 char tone = CHAR_MAX;
1026
Philipp Maier87bd9be2017-08-22 16:35:41 +02001027 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1028
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001029 if (p->found != 0)
1030 return create_err_response(NULL, 400, "RQNT", p->trans);
1031
1032 for_each_line(line, p->save) {
1033 switch (line[0]) {
1034 case 'S':
1035 tone = extract_tone(line);
1036 break;
1037 }
1038 }
1039
1040 /* we didn't see a signal request with a tone */
1041 if (tone == CHAR_MAX)
1042 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1043
1044 if (p->cfg->rqnt_cb)
1045 res = p->cfg->rqnt_cb(p->endp, tone);
1046
1047 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001048 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1049 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001050}
1051
Philipp Maier87bd9be2017-08-22 16:35:41 +02001052/* Connection keepalive timer, will take care that dummy packets are send
1053 * regulary, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001054static void mgcp_keepalive_timer_cb(void *_tcfg)
1055{
1056 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001057 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001058 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001059
Philipp Maiere726d4f2017-11-01 10:41:34 +01001060 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001061 tcfg->trunk_nr);
1062
Philipp Maiere726d4f2017-11-01 10:41:34 +01001063 /* Do not accept invalid configuration values
1064 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1065 * values greater 0 */
1066 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1067
1068 /* The dummy packet functionality has been disabled, we will exit
1069 * immediately, no further timer is scheduled, which means we will no
1070 * longer send dummy packets even when we did before */
1071 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1072 return;
1073
1074 /* In cases where only one dummy packet is sent, we do not need
1075 * the timer since the functions that handle the CRCX and MDCX are
1076 * triggering the sending of the dummy packet. So we behave like in
1077 * the MGCP_KEEPALIVE_NEVER case */
1078 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001079 return;
1080
Philipp Maier87bd9be2017-08-22 16:35:41 +02001081 /* Send walk over all endpoints and send out dummy packets through
1082 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001083 for (i = 1; i < tcfg->number_endpoints; ++i) {
1084 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001085 llist_for_each_entry(conn, &endp->conns, entry) {
1086 if (conn->mode == MGCP_CONN_RECV_ONLY)
1087 send_dummy(endp, &conn->u.rtp);
1088 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001089 }
1090
Philipp Maiere726d4f2017-11-01 10:41:34 +01001091 /* Schedule the keepalive timer for the next round */
1092 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001093 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001094 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1095 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001096}
1097
1098void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1099{
1100 tcfg->keepalive_interval = interval;
1101 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1102
1103 if (interval <= 0)
1104 osmo_timer_del(&tcfg->keepalive_timer);
1105 else
1106 osmo_timer_schedule(&tcfg->keepalive_timer,
1107 tcfg->keepalive_interval, 0);
1108}
1109
Philipp Maier87bd9be2017-08-22 16:35:41 +02001110/*! allocate configuration with default values.
1111 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001112struct mgcp_config *mgcp_config_alloc(void)
1113{
1114 struct mgcp_config *cfg;
1115
1116 cfg = talloc_zero(NULL, struct mgcp_config);
1117 if (!cfg) {
1118 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1119 return NULL;
1120 }
1121
Philipp Maier87bd9be2017-08-22 16:35:41 +02001122 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1123 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1124 cfg->net_ports.last_port = cfg->net_ports.range_start;
1125
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001126 cfg->source_port = 2427;
1127 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1128 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1129
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001130 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1131 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1132
1133 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1134
1135 /* default trunk handling */
1136 cfg->trunk.cfg = cfg;
1137 cfg->trunk.trunk_nr = 0;
1138 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1139 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1140 cfg->trunk.audio_payload = 126;
1141 cfg->trunk.audio_send_ptime = 1;
1142 cfg->trunk.audio_send_name = 1;
1143 cfg->trunk.omit_rtcp = 0;
1144 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
1145
1146 INIT_LLIST_HEAD(&cfg->trunks);
1147
1148 return cfg;
1149}
1150
Philipp Maier87bd9be2017-08-22 16:35:41 +02001151/*! allocate configuration with default values.
1152 * (called once at startup by VTY)
1153 * \param[in] cfg mgcp configuration
1154 * \param[in] nr trunk number
1155 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001156struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1157{
1158 struct mgcp_trunk_config *trunk;
1159
1160 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1161 if (!trunk) {
1162 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1163 return NULL;
1164 }
1165
1166 trunk->cfg = cfg;
1167 trunk->trunk_type = MGCP_TRUNK_E1;
1168 trunk->trunk_nr = nr;
1169 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1170 trunk->audio_payload = 126;
1171 trunk->audio_send_ptime = 1;
1172 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001173 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001174 trunk->omit_rtcp = 0;
1175 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
1176 llist_add_tail(&trunk->entry, &cfg->trunks);
1177 return trunk;
1178}
1179
Philipp Maier87bd9be2017-08-22 16:35:41 +02001180/*! get trunk configuration by trunk number (index).
1181 * \param[in] cfg mgcp configuration
1182 * \param[in] index trunk number
1183 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001184struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1185{
1186 struct mgcp_trunk_config *trunk;
1187
1188 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001189 if (trunk->trunk_nr == index)
1190 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001191
1192 return NULL;
1193}
1194
Philipp Maier87bd9be2017-08-22 16:35:41 +02001195/*! allocate endpoints and set default values.
1196 * (called once at startup by VTY)
1197 * \param[in] tcfg trunk configuration
1198 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001199int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1200{
1201 int i;
1202
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001203 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001204 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001205 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001206 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001207 if (!tcfg->endpoints)
1208 return -1;
1209
Philipp Maierfcd06552017-11-10 17:32:22 +01001210 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001211 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001212 tcfg->endpoints[i].cfg = tcfg->cfg;
1213 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001214
1215 /* NOTE: Currently all endpoints are of type RTP, this will
1216 * change when new variations are implemented */
1217 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001218 }
1219
Philipp Maierfcd06552017-11-10 17:32:22 +01001220 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001221 return 0;
1222}
1223
Philipp Maier87bd9be2017-08-22 16:35:41 +02001224/*! relase endpoint, all open connections are closed.
1225 * \param[in] endp endpoint to release */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001226void mgcp_release_endp(struct mgcp_endpoint *endp)
1227{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001228 LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint:%x\n",
1229 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001230
Philipp Maier87bd9be2017-08-22 16:35:41 +02001231 /* Normally this function should only be called wehen
1232 * all connections have been removed already. In case
1233 * that there are still connections open (e.g. when
1234 * RSIP is executed), free them all at once. */
1235 mgcp_conn_free_all(endp);
1236
1237 /* Reset endpoint parameters and states */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001238 talloc_free(endp->callid);
1239 endp->callid = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001240 talloc_free(endp->local_options.string);
1241 endp->local_options.string = NULL;
1242 talloc_free(endp->local_options.codec);
1243 endp->local_options.codec = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001244}
1245
1246static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1247{
1248 return write(cfg->gw_fd.bfd.fd, buf, len);
1249}
1250
Philipp Maier87bd9be2017-08-22 16:35:41 +02001251/*! Reset all endpoints by sending RSIP message to self.
1252 * (called by VTY)
1253 * \param[in] endp trunk endpoint
1254 * \param[in] endpoint number
1255 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001256int mgcp_send_reset_all(struct mgcp_config *cfg)
1257{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001258 int rc;
1259
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001260 static const char mgcp_reset[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001261 "RSIP 1 *@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001262 };
1263
Philipp Maier87bd9be2017-08-22 16:35:41 +02001264 rc = send_agent(cfg, mgcp_reset, sizeof mgcp_reset - 1);
1265 if (rc <= 0)
1266 return -1;
1267
1268 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001269}
1270
Philipp Maier87bd9be2017-08-22 16:35:41 +02001271/*! Reset a single endpoint by sending RSIP message to self.
1272 * (called by VTY)
1273 * \param[in] endp trunk endpoint
1274 * \param[in] endpoint number
1275 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001276int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1277{
1278 char buf[128];
1279 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001280 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001281
1282 len = snprintf(buf, sizeof(buf),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001283 "RSIP 39 %x@mgw MGCP 1.0\r\n", endpoint);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001284 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001285 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001286
1287 buf[sizeof(buf) - 1] = '\0';
1288
Philipp Maier87bd9be2017-08-22 16:35:41 +02001289 rc = send_agent(endp->cfg, buf, len);
1290 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001291 return -1;
1292
Philipp Maier87bd9be2017-08-22 16:35:41 +02001293 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001294}