blob: 332aa91a39e22293c80eed918255106052832527 [file] [log] [blame]
Neels Hofmeyre9920f22017-07-10 15:07:22 +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
36#include <osmocom/legacy_mgcp/mgcp.h>
37#include <osmocom/legacy_mgcp/mgcp_internal.h>
38
39#define for_each_non_empty_line(line, save) \
40 for (line = strtok_r(NULL, "\r\n", &save); line;\
41 line = strtok_r(NULL, "\r\n", &save))
42
43
44static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end);
45
46struct mgcp_request {
47 char *name;
48 struct msgb *(*handle_request) (struct mgcp_parse_data *data);
49 char *debug_name;
50};
51
52#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
53 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
54
55static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data);
56static struct msgb *handle_create_con(struct mgcp_parse_data *data);
57static struct msgb *handle_delete_con(struct mgcp_parse_data *data);
58static struct msgb *handle_modify_con(struct mgcp_parse_data *data);
59static struct msgb *handle_rsip(struct mgcp_parse_data *data);
60static struct msgb *handle_noti_req(struct mgcp_parse_data *data);
61
62static void create_transcoder(struct mgcp_endpoint *endp);
63static void delete_transcoder(struct mgcp_endpoint *endp);
64
65static int setup_rtp_processing(struct mgcp_endpoint *endp);
66
67static int mgcp_analyze_header(struct mgcp_parse_data *parse, char *data);
68
69/* Display an mgcp message on the log output */
70void display_mgcp_message(unsigned char *message, unsigned int len,
71 char *preamble)
72{
73 unsigned char line[80];
74 unsigned char *ptr;
75 unsigned int consumed = 0;
76 unsigned int consumed_line = 0;
77 unsigned int line_count = 0;
78
79 if (!log_check_level(DLMGCP, LOGL_DEBUG))
80 return;
81
82 while (1) {
83 memset(line, 0, sizeof(line));
84 ptr = line;
85 consumed_line = 0;
86 do {
87 if (*message != '\n' && *message != '\r') {
88 *ptr = *message;
89 ptr++;
90 }
91 message++;
92 consumed++;
93 consumed_line++;
94 } while (*message != '\n' && consumed < len
95 && consumed_line < sizeof(line));
96
97 if (strlen((const char *)line)) {
98 LOGP(DLMGCP, LOGL_DEBUG, "%s: line #%02u: %s\n",
99 preamble, line_count, line);
100 line_count++;
101 }
102
103 if (consumed >= len)
104 return;
105 }
106}
107
108static int mgcp_check_param(const struct mgcp_endpoint *endp, const char *line)
109{
110 const size_t line_len = strlen(line);
111 if (line[0] != '\0' && line_len < 2) {
112 LOGP(DLMGCP, LOGL_ERROR,
113 "Wrong MGCP option format: '%s' on 0x%x\n",
114 line, ENDPOINT_NUMBER(endp));
115 return 0;
116 }
117
118 return 1;
119}
120
121static uint32_t generate_call_id(struct mgcp_config *cfg)
122{
123 int i;
124
125 /* use the call id */
126 ++cfg->last_call_id;
127
128 /* handle wrap around */
129 if (cfg->last_call_id == CI_UNUSED)
130 ++cfg->last_call_id;
131
132 /* callstack can only be of size number_of_endpoints */
133 /* verify that the call id is free, e.g. in case of overrun */
134 for (i = 1; i < cfg->trunk.number_endpoints; ++i)
135 if (cfg->trunk.endpoints[i].ci == cfg->last_call_id)
136 return generate_call_id(cfg);
137
138 return cfg->last_call_id;
139}
140
141/*
142 * array of function pointers for handling various
143 * messages. In the future this might be binary sorted
144 * for performance reasons.
145 */
146static const struct mgcp_request mgcp_requests [] = {
147 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
148 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
149 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
150 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
151 MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
152
153 /* SPEC extension */
154 MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
155};
156
157static struct msgb *mgcp_msgb_alloc(void)
158{
159 struct msgb *msg;
160 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
161 if (!msg)
162 LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
163
164 return msg;
165}
166
167static struct msgb *do_retransmission(const struct mgcp_endpoint *endp)
168{
169 struct msgb *msg = mgcp_msgb_alloc();
170 if (!msg)
171 return NULL;
172
173 msg->l2h = msgb_put(msg, strlen(endp->last_response));
174 memcpy(msg->l2h, endp->last_response, msgb_l2len(msg));
175 return msg;
176}
177
178static struct msgb *create_resp(struct mgcp_endpoint *endp, int code,
179 const char *txt, const char *msg,
180 const char *trans, const char *param,
181 const char *sdp)
182{
183 int len;
184 struct msgb *res;
185
186 res = mgcp_msgb_alloc();
187 if (!res)
188 return NULL;
189
190 len = snprintf((char *) res->data, 2048, "%d %s%s%s\r\n%s",
191 code, trans, txt, param ? param : "", sdp ? sdp : "");
192 if (len < 0) {
193 LOGP(DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n");
194 msgb_free(res);
195 return NULL;
196 }
197
198 res->l2h = msgb_put(res, len);
199 LOGP(DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code);
200 display_mgcp_message(res->l2h, msgb_l2len(res), "Generated response");
201
202 /*
203 * Remember the last transmission per endpoint.
204 */
205 if (endp) {
206 struct mgcp_trunk_config *tcfg = endp->tcfg;
207 talloc_free(endp->last_response);
208 talloc_free(endp->last_trans);
209 endp->last_trans = talloc_strdup(tcfg->endpoints, trans);
210 endp->last_response = talloc_strndup(tcfg->endpoints,
211 (const char *) res->l2h,
212 msgb_l2len(res));
213 }
214
215 return res;
216}
217
218static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp,
219 int code, const char *msg,
220 const char *trans, const char *param)
221{
222 return create_resp(endp, code, " OK", msg, trans, param, NULL);
223}
224
225static struct msgb *create_ok_response(struct mgcp_endpoint *endp,
226 int code, const char *msg, const char *trans)
227{
228 return create_ok_resp_with_param(endp, code, msg, trans, NULL);
229}
230
231static struct msgb *create_err_response(struct mgcp_endpoint *endp,
232 int code, const char *msg, const char *trans)
233{
234 return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL);
235}
236
237static int write_response_sdp(struct mgcp_endpoint *endp,
238 char *sdp_record, size_t size, const char *addr)
239{
240 const char *fmtp_extra;
241 const char *audio_name;
242 int payload_type;
243 int len;
244 int nchars;
245
246 endp->cfg->get_net_downlink_format_cb(endp, &payload_type,
247 &audio_name, &fmtp_extra);
248
249 len = snprintf(sdp_record, size,
250 "v=0\r\n"
251 "o=- %u 23 IN IP4 %s\r\n"
252 "s=-\r\n"
253 "c=IN IP4 %s\r\n"
254 "t=0 0\r\n",
255 endp->ci, addr, addr);
256
257 if (len < 0 || len >= size)
258 goto buffer_too_small;
259
260 if (payload_type >= 0) {
261 nchars = snprintf(sdp_record + len, size - len,
262 "m=audio %d RTP/AVP %d\r\n",
263 endp->net_end.local_port, payload_type);
264 if (nchars < 0 || nchars >= size - len)
265 goto buffer_too_small;
266
267 len += nchars;
268
269 if (audio_name && endp->tcfg->audio_send_name) {
270 nchars = snprintf(sdp_record + len, size - len,
271 "a=rtpmap:%d %s\r\n",
272 payload_type, audio_name);
273
274 if (nchars < 0 || nchars >= size - len)
275 goto buffer_too_small;
276
277 len += nchars;
278 }
279
280 if (fmtp_extra) {
281 nchars = snprintf(sdp_record + len, size - len,
282 "%s\r\n", fmtp_extra);
283
284 if (nchars < 0 || nchars >= size - len)
285 goto buffer_too_small;
286
287 len += nchars;
288 }
289 }
290 if (endp->bts_end.packet_duration_ms > 0 && endp->tcfg->audio_send_ptime) {
291 nchars = snprintf(sdp_record + len, size - len,
292 "a=ptime:%d\r\n",
293 endp->bts_end.packet_duration_ms);
294 if (nchars < 0 || nchars >= size - len)
295 goto buffer_too_small;
296
297 len += nchars;
298 }
299
300 return len;
301
302buffer_too_small:
303 LOGP(DLMGCP, LOGL_ERROR, "SDP buffer too small: %zu (needed %d)\n",
304 size, len);
305 return -1;
306}
307
308static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
309 const char *msg, const char *trans_id)
310{
311 const char *addr = endp->cfg->local_ip;
312 char sdp_record[4096];
313 int len;
314 int nchars;
315 char osmux_extension[strlen("\nX-Osmux: 255") + 1];
316
317 if (!addr)
318 addr = mgcp_net_src_addr(endp);
319
320 if (endp->osmux.state == OSMUX_STATE_NEGOTIATING) {
321 sprintf(osmux_extension, "\nX-Osmux: %u", endp->osmux.cid);
322 endp->osmux.state = OSMUX_STATE_ACTIVATING;
323 } else {
324 osmux_extension[0] = '\0';
325 }
326
327 len = snprintf(sdp_record, sizeof(sdp_record),
328 "I: %u%s\n\n", endp->ci, osmux_extension);
329 if (len < 0)
330 return NULL;
331
332 nchars = write_response_sdp(endp, sdp_record + len,
333 sizeof(sdp_record) - len - 1, addr);
334 if (nchars < 0)
335 return NULL;
336
337 len += nchars;
338
339 sdp_record[sizeof(sdp_record) - 1] = '\0';
340
341 return create_resp(endp, 200, " OK", msg, trans_id, NULL, sdp_record);
342}
343
344static void send_dummy(struct mgcp_endpoint *endp)
345{
346 if (endp->osmux.state != OSMUX_STATE_DISABLED)
347 osmux_send_dummy(endp);
348 else
349 mgcp_send_dummy(endp);
350}
351
352/*
353 * handle incoming messages:
354 * - this can be a command (four letters, space, transaction id)
355 * - or a response (three numbers, space, transaction id)
356 */
357struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
358{
359 struct mgcp_parse_data pdata;
360 int i, code, handled = 0;
361 struct msgb *resp = NULL;
362 char *data;
363
364 if (msgb_l2len(msg) < 4) {
365 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
366 return NULL;
367 }
368
369 if (mgcp_msg_terminate_nul(msg))
370 return NULL;
371
372 display_mgcp_message(msg->l2h, msgb_l2len(msg), "Received message");
373
374 /* attempt to treat it as a response */
375 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
376 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
377 return NULL;
378 }
379
380 msg->l3h = &msg->l2h[4];
381
382
383 /*
384 * Check for a duplicate message and respond.
385 */
386 memset(&pdata, 0, sizeof(pdata));
387 pdata.cfg = cfg;
388 data = strline_r((char *) msg->l3h, &pdata.save);
389 pdata.found = mgcp_analyze_header(&pdata, data);
390 if (pdata.endp && pdata.trans
391 && pdata.endp->last_trans
392 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
393 return do_retransmission(pdata.endp);
394 }
395
396 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
397 if (strncmp(mgcp_requests[i].name, (const char *) &msg->l2h[0], 4) == 0) {
398 handled = 1;
399 resp = mgcp_requests[i].handle_request(&pdata);
400 break;
401 }
402 }
403
404 if (!handled)
405 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]);
406
407 return resp;
408}
409
410/**
411 * We have a null terminated string with the endpoint name here. We only
412 * support two kinds. Simple ones as seen on the BSC level and the ones
413 * seen on the trunk side.
414 */
415static struct mgcp_endpoint *find_e1_endpoint(struct mgcp_config *cfg,
416 const char *mgcp)
417{
418 char *rest = NULL;
419 struct mgcp_trunk_config *tcfg;
420 int trunk, endp;
421
422 trunk = strtoul(mgcp + 6, &rest, 10);
423 if (rest == NULL || rest[0] != '/' || trunk < 1) {
424 LOGP(DLMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp);
425 return NULL;
426 }
427
428 endp = strtoul(rest + 1, &rest, 10);
429 if (rest == NULL || rest[0] != '@') {
430 LOGP(DLMGCP, LOGL_ERROR, "Wrong endpoint name '%s'\n", mgcp);
431 return NULL;
432 }
433
434 /* signalling is on timeslot 1 */
435 if (endp == 1)
436 return NULL;
437
438 tcfg = mgcp_trunk_num(cfg, trunk);
439 if (!tcfg) {
440 LOGP(DLMGCP, LOGL_ERROR, "The trunk %d is not declared.\n", trunk);
441 return NULL;
442 }
443
444 if (!tcfg->endpoints) {
445 LOGP(DLMGCP, LOGL_ERROR, "Endpoints of trunk %d not allocated.\n", trunk);
446 return NULL;
447 }
448
449 if (endp < 1 || endp >= tcfg->number_endpoints) {
450 LOGP(DLMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n", mgcp);
451 return NULL;
452 }
453
454 return &tcfg->endpoints[endp];
455}
456
457static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp)
458{
459 char *endptr = NULL;
460 unsigned int gw = INT_MAX;
461
462 if (strncmp(mgcp, "ds/e1", 5) == 0)
463 return find_e1_endpoint(cfg, mgcp);
464
465 gw = strtoul(mgcp, &endptr, 16);
466 if (gw > 0 && gw < cfg->trunk.number_endpoints && endptr[0] == '@')
467 return &cfg->trunk.endpoints[gw];
468
469 LOGP(DLMGCP, LOGL_ERROR, "Not able to find the endpoint: '%s'\n", mgcp);
470 return NULL;
471}
472
473/**
474 * @returns 0 when the status line was complete and transaction_id and
475 * endp out parameters are set.
476 */
477static int mgcp_analyze_header(struct mgcp_parse_data *pdata, char *data)
478{
479 int i = 0;
480 char *elem, *save = NULL;
481
482 OSMO_ASSERT(data);
483 pdata->trans = "000000";
484
485 for (elem = strtok_r(data, " ", &save); elem;
486 elem = strtok_r(NULL, " ", &save)) {
487 switch (i) {
488 case 0:
489 pdata->trans = elem;
490 break;
491 case 1:
492 pdata->endp = find_endpoint(pdata->cfg, elem);
493 if (!pdata->endp) {
494 LOGP(DLMGCP, LOGL_ERROR,
495 "Unable to find Endpoint `%s'\n", elem);
496 return -1;
497 }
498 break;
499 case 2:
500 if (strcmp("MGCP", elem)) {
501 LOGP(DLMGCP, LOGL_ERROR,
502 "MGCP header parsing error\n");
503 return -1;
504 }
505 break;
506 case 3:
507 if (strcmp("1.0", elem)) {
508 LOGP(DLMGCP, LOGL_ERROR, "MGCP version `%s' "
509 "not supported\n", elem);
510 return -1;
511 }
512 break;
513 }
514 i++;
515 }
516
517 if (i != 4) {
518 LOGP(DLMGCP, LOGL_ERROR, "MGCP status line too short.\n");
519 pdata->trans = "000000";
520 pdata->endp = NULL;
521 return -1;
522 }
523
524 return 0;
525}
526
527static int verify_call_id(const struct mgcp_endpoint *endp,
528 const char *callid)
529{
530 if (strcmp(endp->callid, callid) != 0) {
531 LOGP(DLMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
532 ENDPOINT_NUMBER(endp), endp->callid, callid);
533 return -1;
534 }
535
536 return 0;
537}
538
539static int verify_ci(const struct mgcp_endpoint *endp,
540 const char *_ci)
541{
542 uint32_t ci = strtoul(_ci, NULL, 10);
543
544 if (ci != endp->ci) {
545 LOGP(DLMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %u != %s\n",
546 ENDPOINT_NUMBER(endp), endp->ci, _ci);
547 return -1;
548 }
549
550 return 0;
551}
552
553static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
554{
555 if (p->found != 0)
556 return create_err_response(NULL, 500, "AUEP", p->trans);
557 else
558 return create_ok_response(p->endp, 200, "AUEP", p->trans);
559}
560
561static int parse_conn_mode(const char *msg, struct mgcp_endpoint *endp)
562{
563 int ret = 0;
564 if (strcmp(msg, "recvonly") == 0)
565 endp->conn_mode = MGCP_CONN_RECV_ONLY;
566 else if (strcmp(msg, "sendrecv") == 0)
567 endp->conn_mode = MGCP_CONN_RECV_SEND;
568 else if (strcmp(msg, "sendonly") == 0)
569 endp->conn_mode = MGCP_CONN_SEND_ONLY;
570 else if (strcmp(msg, "loopback") == 0)
571 endp->conn_mode = MGCP_CONN_LOOPBACK;
572 else {
573 LOGP(DLMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg);
574 ret = -1;
575 }
576
577 endp->net_end.output_enabled =
578 endp->conn_mode & MGCP_CONN_SEND_ONLY ? 1 : 0;
579 endp->bts_end.output_enabled =
580 endp->conn_mode & MGCP_CONN_RECV_ONLY ? 1 : 0;
581
582 LOGP(DLMGCP, LOGL_DEBUG, "endpoint %x connection mode '%s' %d output_enabled net %d bts %d\n",
583 ENDPOINT_NUMBER(endp),
584 msg, endp->conn_mode, endp->net_end.output_enabled,
585 endp->bts_end.output_enabled);
586
587 return ret;
588}
589
590static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_rtp_end *end,
591 struct mgcp_port_range *range,
592 int (*alloc)(struct mgcp_endpoint *endp, int port))
593{
594 int i;
595
596 if (range->mode == PORT_ALLOC_STATIC) {
597 end->local_alloc = PORT_ALLOC_STATIC;
598 return 0;
599 }
600
601 /* attempt to find a port */
602 for (i = 0; i < 200; ++i) {
603 int rc;
604
605 if (range->last_port >= range->range_end)
606 range->last_port = range->range_start;
607
608 rc = alloc(endp, range->last_port);
609
610 range->last_port += 2;
611 if (rc == 0) {
612 end->local_alloc = PORT_ALLOC_DYNAMIC;
613 return 0;
614 }
615
616 }
617
618 LOGP(DLMGCP, LOGL_ERROR, "Allocating a RTP/RTCP port failed 200 times 0x%x.\n",
619 ENDPOINT_NUMBER(endp));
620 return -1;
621}
622
623static int allocate_ports(struct mgcp_endpoint *endp)
624{
625 if (allocate_port(endp, &endp->net_end, &endp->cfg->net_ports,
626 mgcp_bind_net_rtp_port) != 0)
627 return -1;
628
629 if (allocate_port(endp, &endp->bts_end, &endp->cfg->bts_ports,
630 mgcp_bind_bts_rtp_port) != 0) {
631 mgcp_rtp_end_reset(&endp->net_end);
632 return -1;
633 }
634
635 if (endp->cfg->transcoder_ip && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) {
636 if (allocate_port(endp, &endp->trans_net,
637 &endp->cfg->transcoder_ports,
638 mgcp_bind_trans_net_rtp_port) != 0) {
639 mgcp_rtp_end_reset(&endp->net_end);
640 mgcp_rtp_end_reset(&endp->bts_end);
641 return -1;
642 }
643
644 if (allocate_port(endp, &endp->trans_bts,
645 &endp->cfg->transcoder_ports,
646 mgcp_bind_trans_bts_rtp_port) != 0) {
647 mgcp_rtp_end_reset(&endp->net_end);
648 mgcp_rtp_end_reset(&endp->bts_end);
649 mgcp_rtp_end_reset(&endp->trans_net);
650 return -1;
651 }
652
653 /* remember that we have set up transcoding */
654 endp->type = MGCP_RTP_TRANSCODED;
655 }
656
657 return 0;
658}
659
660/* Set the LCO from a string (see RFC 3435).
661 * The string is stored in the 'string' field. A NULL string is handled excatly
662 * like an empty string, the 'string' field is never NULL after this function
663 * has been called. */
664static void set_local_cx_options(void *ctx, struct mgcp_lco *lco,
665 const char *options)
666{
667 char *p_opt, *a_opt;
668 char codec[9];
669
670 talloc_free(lco->string);
671 talloc_free(lco->codec);
672 lco->codec = NULL;
673 lco->pkt_period_min = lco->pkt_period_max = 0;
674 lco->string = talloc_strdup(ctx, options ? options : "");
675
676 p_opt = strstr(lco->string, "p:");
677 if (p_opt && sscanf(p_opt, "p:%d-%d",
678 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
679 lco->pkt_period_max = lco->pkt_period_min;
680
681 a_opt = strstr(lco->string, "a:");
682 if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1)
683 lco->codec = talloc_strdup(ctx, codec);
684}
685
686void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
687 struct mgcp_rtp_end *rtp)
688{
689 struct mgcp_trunk_config *tcfg = endp->tcfg;
690
691 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
692
693 rtp->force_aligned_timing = tcfg->force_aligned_timing;
694 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
695
696 LOGP(DLMGCP, LOGL_DEBUG,
697 "Configuring RTP endpoint: local port %d%s%s\n",
698 ntohs(rtp->rtp_port),
699 rtp->force_aligned_timing ? ", force constant timing" : "",
700 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
701}
702
703uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
704 struct mgcp_rtp_end *rtp)
705{
706 int f = 0;
707
708 /* Get the number of frames per channel and packet */
709 if (rtp->frames_per_packet)
710 f = rtp->frames_per_packet;
711 else if (rtp->packet_duration_ms && rtp->codec.frame_duration_num) {
712 int den = 1000 * rtp->codec.frame_duration_num;
713 f = (rtp->packet_duration_ms * rtp->codec.frame_duration_den + den/2)
714 / den;
715 }
716
717 return rtp->codec.rate * f * rtp->codec.frame_duration_num / rtp->codec.frame_duration_den;
718}
719
720static int mgcp_parse_osmux_cid(const char *line)
721{
722 int osmux_cid;
723
724 if (sscanf(line + 2, "Osmux: %u", &osmux_cid) != 1)
725 return -1;
726
727 if (osmux_cid > OSMUX_CID_MAX) {
728 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
729 osmux_cid, OSMUX_CID_MAX);
730 return -1;
731 }
732 LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
733
734 return osmux_cid;
735}
736
737static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
738{
739 if (!endp->cfg->osmux_init) {
740 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
741 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
742 return -1;
743 }
744 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
745 }
746
747 return mgcp_parse_osmux_cid(line);
748}
749
750static struct msgb *handle_create_con(struct mgcp_parse_data *p)
751{
752 struct mgcp_trunk_config *tcfg;
753 struct mgcp_endpoint *endp = p->endp;
754 int error_code = 400;
755
756 const char *local_options = NULL;
757 const char *callid = NULL;
758 const char *mode = NULL;
759 char *line;
760 int have_sdp = 0, osmux_cid = -1;
761
762 if (p->found != 0)
763 return create_err_response(NULL, 510, "CRCX", p->trans);
764
765 /* parse CallID C: and LocalParameters L: */
766 for_each_line(line, p->save) {
767 if (!mgcp_check_param(endp, line))
768 continue;
769
770 switch (line[0]) {
771 case 'L':
772 local_options = (const char *) line + 3;
773 break;
774 case 'C':
775 callid = (const char *) line + 3;
776 break;
777 case 'M':
778 mode = (const char *) line + 3;
779 break;
780 case 'X':
781 /* Osmux is not enabled in this bsc, ignore it so the
782 * bsc-nat knows that we don't want to use Osmux.
783 */
784 if (!p->endp->cfg->osmux)
785 break;
786
787 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0)
788 osmux_cid = mgcp_osmux_setup(endp, line);
789 break;
790 case '\0':
791 have_sdp = 1;
792 goto mgcp_header_done;
793 default:
794 LOGP(DLMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
795 *line, *line, ENDPOINT_NUMBER(endp));
796 break;
797 }
798 }
799
800mgcp_header_done:
801 tcfg = p->endp->tcfg;
802
803 /* Check required data */
804 if (!callid || !mode) {
805 LOGP(DLMGCP, LOGL_ERROR, "Missing callid and mode in CRCX on 0x%x\n",
806 ENDPOINT_NUMBER(endp));
807 return create_err_response(endp, 400, "CRCX", p->trans);
808 }
809
810 if (endp->allocated) {
811 if (tcfg->force_realloc) {
812 LOGP(DLMGCP, LOGL_NOTICE, "Endpoint 0x%x already allocated. Forcing realloc.\n",
813 ENDPOINT_NUMBER(endp));
814 mgcp_release_endp(endp);
815 if (p->cfg->realloc_cb)
816 p->cfg->realloc_cb(tcfg, ENDPOINT_NUMBER(endp));
817 } else {
818 LOGP(DLMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n",
819 ENDPOINT_NUMBER(endp));
820 return create_err_response(endp, 400, "CRCX", p->trans);
821 }
822 }
823
824 /* copy some parameters */
825 endp->callid = talloc_strdup(tcfg->endpoints, callid);
826
827 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
828 local_options);
829
830 if (parse_conn_mode(mode, endp) != 0) {
831 error_code = 517;
832 goto error2;
833 }
834
835 /* initialize */
836 endp->net_end.rtp_port = endp->net_end.rtcp_port = endp->bts_end.rtp_port = endp->bts_end.rtcp_port = 0;
837 mgcp_rtp_end_config(endp, 0, &endp->net_end);
838 mgcp_rtp_end_config(endp, 0, &endp->bts_end);
839
840 /* set to zero until we get the info */
841 memset(&endp->net_end.addr, 0, sizeof(endp->net_end.addr));
842
843 /* bind to the port now */
844 if (allocate_ports(endp) != 0)
845 goto error2;
846
847 /* assign a local call identifier or fail */
848 endp->ci = generate_call_id(p->cfg);
849 if (endp->ci == CI_UNUSED)
850 goto error2;
851
852 /* Annotate Osmux circuit ID and set it to negotiating state until this
853 * is fully set up from the dummy load.
854 */
855 endp->osmux.state = OSMUX_STATE_DISABLED;
856 if (osmux_cid >= 0) {
857 endp->osmux.cid = osmux_cid;
858 endp->osmux.state = OSMUX_STATE_NEGOTIATING;
859 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
860 LOGP(DLMGCP, LOGL_ERROR,
861 "Osmux only and no osmux offered on 0x%x\n", ENDPOINT_NUMBER(endp));
862 goto error2;
863 }
864
865 endp->allocated = 1;
866
867 /* set up RTP media parameters */
868 mgcp_set_audio_info(p->cfg, &endp->bts_end.codec, tcfg->audio_payload, tcfg->audio_name);
869 endp->bts_end.fmtp_extra = talloc_strdup(tcfg->endpoints,
870 tcfg->audio_fmtp_extra);
871 if (have_sdp)
872 mgcp_parse_sdp_data(endp, &endp->net_end, p);
873 else if (endp->local_options.codec)
874 mgcp_set_audio_info(p->cfg, &endp->net_end.codec,
875 PTYPE_UNDEFINED, endp->local_options.codec);
876
877 if (p->cfg->bts_force_ptime) {
878 endp->bts_end.packet_duration_ms = p->cfg->bts_force_ptime;
879 endp->bts_end.force_output_ptime = 1;
880 }
881
882 if (setup_rtp_processing(endp) != 0)
883 goto error2;
884
885 /* policy CB */
886 if (p->cfg->policy_cb) {
887 int rc;
888 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
889 MGCP_ENDP_CRCX, p->trans);
890 switch (rc) {
891 case MGCP_POLICY_REJECT:
892 LOGP(DLMGCP, LOGL_NOTICE, "CRCX rejected by policy on 0x%x\n",
893 ENDPOINT_NUMBER(endp));
894 mgcp_release_endp(endp);
895 return create_err_response(endp, 400, "CRCX", p->trans);
896 break;
897 case MGCP_POLICY_DEFER:
898 /* stop processing */
899 create_transcoder(endp);
900 return NULL;
901 break;
902 case MGCP_POLICY_CONT:
903 /* just continue */
904 break;
905 }
906 }
907
908 LOGP(DLMGCP, LOGL_DEBUG, "Creating endpoint on: 0x%x CI: %u port: %u/%u\n",
909 ENDPOINT_NUMBER(endp), endp->ci,
910 endp->net_end.local_port, endp->bts_end.local_port);
911 if (p->cfg->change_cb)
912 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
913
914 if (endp->conn_mode & MGCP_CONN_RECV_ONLY && tcfg->keepalive_interval != 0) {
915 send_dummy(endp);
916 }
917
918 create_transcoder(endp);
919 return create_response_with_sdp(endp, "CRCX", p->trans);
920error2:
921 mgcp_release_endp(endp);
922 LOGP(DLMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
923 return create_err_response(endp, error_code, "CRCX", p->trans);
924}
925
926static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
927{
928 struct mgcp_endpoint *endp = p->endp;
929 int error_code = 500;
930 int silent = 0;
931 int have_sdp = 0;
932 char *line;
933 const char *local_options = NULL;
934
935 if (p->found != 0)
936 return create_err_response(NULL, 510, "MDCX", p->trans);
937
938 if (endp->ci == CI_UNUSED) {
939 LOGP(DLMGCP, LOGL_ERROR, "Endpoint is not "
940 "holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
941 return create_err_response(endp, 400, "MDCX", p->trans);
942 }
943
944 for_each_line(line, p->save) {
945 if (!mgcp_check_param(endp, line))
946 continue;
947
948 switch (line[0]) {
949 case 'C': {
950 if (verify_call_id(endp, line + 3) != 0)
951 goto error3;
952 break;
953 }
954 case 'I': {
955 if (verify_ci(endp, line + 3) != 0)
956 goto error3;
957 break;
958 }
959 case 'L':
960 local_options = (const char *) line + 3;
961 break;
962 case 'M':
963 if (parse_conn_mode(line + 3, endp) != 0) {
964 error_code = 517;
965 goto error3;
966 }
967 endp->orig_mode = endp->conn_mode;
968 break;
969 case 'Z':
970 silent = strcmp("noanswer", line + 3) == 0;
971 break;
972 case '\0':
973 /* SDP file begins */
974 have_sdp = 1;
975 mgcp_parse_sdp_data(endp, &endp->net_end, p);
976 /* This will exhaust p->save, so the loop will
977 * terminate next time.
978 */
979 break;
980 default:
981 LOGP(DLMGCP, LOGL_NOTICE, "Unhandled MGCP option: '%c'/%d on 0x%x\n",
982 line[0], line[0], ENDPOINT_NUMBER(endp));
983 break;
984 }
985 }
986
987 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
988 local_options);
989
990 if (!have_sdp && endp->local_options.codec)
991 mgcp_set_audio_info(p->cfg, &endp->net_end.codec,
992 PTYPE_UNDEFINED, endp->local_options.codec);
993
994 if (setup_rtp_processing(endp) != 0)
995 goto error3;
996
997 /* policy CB */
998 if (p->cfg->policy_cb) {
999 int rc;
1000 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1001 MGCP_ENDP_MDCX, p->trans);
1002 switch (rc) {
1003 case MGCP_POLICY_REJECT:
1004 LOGP(DLMGCP, LOGL_NOTICE, "MDCX rejected by policy on 0x%x\n",
1005 ENDPOINT_NUMBER(endp));
1006 if (silent)
1007 goto out_silent;
1008 return create_err_response(endp, 400, "MDCX", p->trans);
1009 break;
1010 case MGCP_POLICY_DEFER:
1011 /* stop processing */
1012 LOGP(DLMGCP, LOGL_DEBUG, "endp %x MDCX defer\n",
1013 ENDPOINT_NUMBER(endp));
1014 return NULL;
1015 break;
1016 case MGCP_POLICY_CONT:
1017 /* just continue */
1018 break;
1019 }
1020 }
1021
1022 mgcp_rtp_end_config(endp, 1, &endp->net_end);
1023 mgcp_rtp_end_config(endp, 1, &endp->bts_end);
1024
1025 /* modify */
1026 LOGP(DLMGCP, LOGL_DEBUG, "Modified endpoint on: 0x%x Server: %s:%u\n",
1027 ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port));
1028 if (p->cfg->change_cb)
1029 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX);
1030
1031 if (endp->conn_mode & MGCP_CONN_RECV_ONLY &&
1032 endp->tcfg->keepalive_interval != 0)
1033 send_dummy(endp);
1034
1035 if (silent)
1036 goto out_silent;
1037
1038 return create_response_with_sdp(endp, "MDCX", p->trans);
1039
1040error3:
1041 return create_err_response(endp, error_code, "MDCX", p->trans);
1042
1043
1044out_silent:
1045 LOGP(DLMGCP, LOGL_DEBUG, "endp %x Modify endpoint: silent exit\n",
1046 ENDPOINT_NUMBER(endp));
1047 return NULL;
1048}
1049
1050static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
1051{
1052 struct mgcp_endpoint *endp = p->endp;
1053 int error_code = 400;
1054 int silent = 0;
1055 char *line;
1056 char stats[1048];
1057
1058 if (p->found != 0)
1059 return create_err_response(NULL, error_code, "DLCX", p->trans);
1060
1061 if (!p->endp->allocated) {
1062 LOGP(DLMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n",
1063 ENDPOINT_NUMBER(endp));
1064 return create_err_response(endp, 400, "DLCX", p->trans);
1065 }
1066
1067 for_each_line(line, p->save) {
1068 if (!mgcp_check_param(endp, line))
1069 continue;
1070
1071 switch (line[0]) {
1072 case 'C':
1073 if (verify_call_id(endp, line + 3) != 0)
1074 goto error3;
1075 break;
1076 case 'I':
1077 if (verify_ci(endp, line + 3) != 0)
1078 goto error3;
1079 break;
1080 case 'Z':
1081 silent = strcmp("noanswer", line + 3) == 0;
1082 break;
1083 default:
1084 LOGP(DLMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
1085 line[0], line[0], ENDPOINT_NUMBER(endp));
1086 break;
1087 }
1088 }
1089
1090 /* policy CB */
1091 if (p->cfg->policy_cb) {
1092 int rc;
1093 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1094 MGCP_ENDP_DLCX, p->trans);
1095 switch (rc) {
1096 case MGCP_POLICY_REJECT:
1097 LOGP(DLMGCP, LOGL_NOTICE, "DLCX rejected by policy on 0x%x\n",
1098 ENDPOINT_NUMBER(endp));
1099 if (silent)
1100 goto out_silent;
1101 return create_err_response(endp, 400, "DLCX", p->trans);
1102 break;
1103 case MGCP_POLICY_DEFER:
1104 /* stop processing */
1105 delete_transcoder(endp);
1106 return NULL;
1107 break;
1108 case MGCP_POLICY_CONT:
1109 /* just continue */
1110 break;
1111 }
1112 }
1113
1114 /* free the connection */
1115 LOGP(DLMGCP, LOGL_DEBUG, "Deleted endpoint on: 0x%x Server: %s:%u\n",
1116 ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port));
1117
1118 /* save the statistics of the current call */
1119 mgcp_format_stats(endp, stats, sizeof(stats));
1120
1121 delete_transcoder(endp);
1122 mgcp_release_endp(endp);
1123 if (p->cfg->change_cb)
1124 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX);
1125
1126 if (silent)
1127 goto out_silent;
1128 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1129
1130error3:
1131 return create_err_response(endp, error_code, "DLCX", p->trans);
1132
1133out_silent:
1134 return NULL;
1135}
1136
1137static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1138{
1139 if (p->found != 0) {
1140 LOGP(DLMGCP, LOGL_ERROR, "Failed to find the endpoint.\n");
1141 return NULL;
1142 }
1143
1144 if (p->cfg->reset_cb)
1145 p->cfg->reset_cb(p->endp->tcfg);
1146 return NULL;
1147}
1148
1149static char extract_tone(const char *line)
1150{
1151 const char *str = strstr(line, "D/");
1152 if (!str)
1153 return CHAR_MAX;
1154
1155 return str[2];
1156}
1157
1158/*
1159 * This can request like DTMF detection and forward, fax detection... it
1160 * can also request when the notification should be send and such. We don't
1161 * do this right now.
1162 */
1163static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1164{
1165 int res = 0;
1166 char *line;
1167 char tone = CHAR_MAX;
1168
1169 if (p->found != 0)
1170 return create_err_response(NULL, 400, "RQNT", p->trans);
1171
1172 for_each_line(line, p->save) {
1173 switch (line[0]) {
1174 case 'S':
1175 tone = extract_tone(line);
1176 break;
1177 }
1178 }
1179
1180 /* we didn't see a signal request with a tone */
1181 if (tone == CHAR_MAX)
1182 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1183
1184 if (p->cfg->rqnt_cb)
1185 res = p->cfg->rqnt_cb(p->endp, tone);
1186
1187 return res == 0 ?
1188 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1189 create_err_response(p->endp, res, "RQNT", p->trans);
1190}
1191
1192static void mgcp_keepalive_timer_cb(void *_tcfg)
1193{
1194 struct mgcp_trunk_config *tcfg = _tcfg;
1195 int i;
1196 LOGP(DLMGCP, LOGL_DEBUG, "Triggered trunk %d keepalive timer.\n",
1197 tcfg->trunk_nr);
1198
1199 if (tcfg->keepalive_interval <= 0)
1200 return;
1201
1202 for (i = 1; i < tcfg->number_endpoints; ++i) {
1203 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
1204 if (endp->conn_mode == MGCP_CONN_RECV_ONLY)
1205 send_dummy(endp);
1206 }
1207
1208 LOGP(DLMGCP, LOGL_DEBUG, "Rescheduling trunk %d keepalive timer.\n",
1209 tcfg->trunk_nr);
1210 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval, 0);
1211}
1212
1213void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1214{
1215 tcfg->keepalive_interval = interval;
1216 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1217
1218 if (interval <= 0)
1219 osmo_timer_del(&tcfg->keepalive_timer);
1220 else
1221 osmo_timer_schedule(&tcfg->keepalive_timer,
1222 tcfg->keepalive_interval, 0);
1223}
1224
1225struct mgcp_config *mgcp_config_alloc(void)
1226{
1227 struct mgcp_config *cfg;
1228
1229 cfg = talloc_zero(NULL, struct mgcp_config);
1230 if (!cfg) {
1231 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1232 return NULL;
1233 }
1234
1235 cfg->source_port = 2427;
1236 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1237 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1238
1239 cfg->transcoder_remote_base = 4000;
1240
1241 cfg->bts_ports.base_port = RTP_PORT_DEFAULT;
1242 cfg->net_ports.base_port = RTP_PORT_NET_DEFAULT;
1243
1244 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1245 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1246
1247 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1248
1249 /* default trunk handling */
1250 cfg->trunk.cfg = cfg;
1251 cfg->trunk.trunk_nr = 0;
1252 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1253 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1254 cfg->trunk.audio_payload = 126;
1255 cfg->trunk.audio_send_ptime = 1;
1256 cfg->trunk.audio_send_name = 1;
1257 cfg->trunk.omit_rtcp = 0;
1258 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
1259
1260 INIT_LLIST_HEAD(&cfg->trunks);
1261
1262 return cfg;
1263}
1264
1265struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1266{
1267 struct mgcp_trunk_config *trunk;
1268
1269 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1270 if (!trunk) {
1271 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1272 return NULL;
1273 }
1274
1275 trunk->cfg = cfg;
1276 trunk->trunk_type = MGCP_TRUNK_E1;
1277 trunk->trunk_nr = nr;
1278 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1279 trunk->audio_payload = 126;
1280 trunk->audio_send_ptime = 1;
1281 trunk->audio_send_name = 1;
1282 trunk->number_endpoints = 33;
1283 trunk->omit_rtcp = 0;
1284 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
1285 llist_add_tail(&trunk->entry, &cfg->trunks);
1286 return trunk;
1287}
1288
1289struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1290{
1291 struct mgcp_trunk_config *trunk;
1292
1293 llist_for_each_entry(trunk, &cfg->trunks, entry)
1294 if (trunk->trunk_nr == index)
1295 return trunk;
1296
1297 return NULL;
1298}
1299
1300static void mgcp_rtp_codec_reset(struct mgcp_rtp_codec *codec)
1301{
1302 codec->payload_type = -1;
1303 talloc_free(codec->subtype_name);
1304 codec->subtype_name = NULL;
1305 talloc_free(codec->audio_name);
1306 codec->audio_name = NULL;
1307 codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
1308 codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
1309 codec->rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
1310 codec->channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
1311}
1312
1313static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end)
1314{
1315 if (end->local_alloc == PORT_ALLOC_DYNAMIC) {
1316 mgcp_free_rtp_port(end);
1317 end->local_port = 0;
1318 }
1319
1320 end->packets = 0;
1321 end->octets = 0;
1322 end->dropped_packets = 0;
1323 memset(&end->addr, 0, sizeof(end->addr));
1324 end->rtp_port = end->rtcp_port = 0;
1325 end->local_alloc = -1;
1326 talloc_free(end->fmtp_extra);
1327 end->fmtp_extra = NULL;
1328 talloc_free(end->rtp_process_data);
1329 end->rtp_process_data = NULL;
1330
1331 /* Set default values */
1332 end->frames_per_packet = 0; /* unknown */
1333 end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
1334 end->output_enabled = 0;
1335
1336 mgcp_rtp_codec_reset(&end->codec);
1337 mgcp_rtp_codec_reset(&end->alt_codec);
1338}
1339
1340static void mgcp_rtp_end_init(struct mgcp_rtp_end *end)
1341{
1342 mgcp_rtp_end_reset(end);
1343 end->rtp.fd = -1;
1344 end->rtcp.fd = -1;
1345}
1346
1347int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1348{
1349 int i;
1350
1351 /* Initialize all endpoints */
1352 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
1353 sizeof(struct mgcp_endpoint),
1354 tcfg->number_endpoints, "endpoints");
1355 if (!tcfg->endpoints)
1356 return -1;
1357
1358 for (i = 0; i < tcfg->number_endpoints; ++i) {
1359 tcfg->endpoints[i].osmux.allocated_cid = -1;
1360 tcfg->endpoints[i].ci = CI_UNUSED;
1361 tcfg->endpoints[i].cfg = tcfg->cfg;
1362 tcfg->endpoints[i].tcfg = tcfg;
1363 mgcp_rtp_end_init(&tcfg->endpoints[i].net_end);
1364 mgcp_rtp_end_init(&tcfg->endpoints[i].bts_end);
1365 mgcp_rtp_end_init(&tcfg->endpoints[i].trans_net);
1366 mgcp_rtp_end_init(&tcfg->endpoints[i].trans_bts);
1367 }
1368
1369 return 0;
1370}
1371
1372void mgcp_release_endp(struct mgcp_endpoint *endp)
1373{
1374 LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
1375 endp->ci = CI_UNUSED;
1376 endp->allocated = 0;
1377
1378 talloc_free(endp->callid);
1379 endp->callid = NULL;
1380
1381 talloc_free(endp->local_options.string);
1382 endp->local_options.string = NULL;
1383 talloc_free(endp->local_options.codec);
1384 endp->local_options.codec = NULL;
1385
1386 mgcp_rtp_end_reset(&endp->bts_end);
1387 mgcp_rtp_end_reset(&endp->net_end);
1388 mgcp_rtp_end_reset(&endp->trans_net);
1389 mgcp_rtp_end_reset(&endp->trans_bts);
1390 endp->type = MGCP_RTP_DEFAULT;
1391
1392 memset(&endp->net_state, 0, sizeof(endp->net_state));
1393 memset(&endp->bts_state, 0, sizeof(endp->bts_state));
1394
1395 endp->conn_mode = endp->orig_mode = MGCP_CONN_NONE;
1396
1397 if (endp->osmux.state == OSMUX_STATE_ENABLED)
1398 osmux_disable_endpoint(endp);
1399
1400 /* release the circuit ID if it had been allocated */
1401 osmux_release_cid(endp);
1402
1403 memset(&endp->taps, 0, sizeof(endp->taps));
1404}
1405
1406void mgcp_initialize_endp(struct mgcp_endpoint *endp)
1407{
1408 return mgcp_release_endp(endp);
1409}
1410
1411static int send_trans(struct mgcp_config *cfg, const char *buf, int len)
1412{
1413 struct sockaddr_in addr;
1414
1415 memset(&addr, 0, sizeof(addr));
1416 addr.sin_family = AF_INET;
1417 addr.sin_addr = cfg->transcoder_in;
1418 addr.sin_port = htons(2427);
1419 return sendto(cfg->gw_fd.bfd.fd, buf, len, 0,
1420 (struct sockaddr *) &addr, sizeof(addr));
1421}
1422
1423static void send_msg(struct mgcp_endpoint *endp, int endpoint, int port,
1424 const char *msg, const char *mode)
1425{
1426 char buf[2096];
1427 int len;
1428 int nchars;
1429
1430 /* hardcoded to AMR right now, we do not know the real type at this point */
1431 len = snprintf(buf, sizeof(buf),
1432 "%s 42 %x@mgw MGCP 1.0\r\n"
1433 "C: 4256\r\n"
1434 "M: %s\r\n"
1435 "\r\n",
1436 msg, endpoint, mode);
1437
1438 if (len < 0)
1439 return;
1440
1441 nchars = write_response_sdp(endp, buf + len, sizeof(buf) + len - 1, NULL);
1442 if (nchars < 0)
1443 return;
1444
1445 len += nchars;
1446
1447 buf[sizeof(buf) - 1] = '\0';
1448
1449 send_trans(endp->cfg, buf, len);
1450}
1451
1452static void send_dlcx(struct mgcp_endpoint *endp, int endpoint)
1453{
1454 char buf[2096];
1455 int len;
1456
1457 len = snprintf(buf, sizeof(buf),
1458 "DLCX 43 %x@mgw MGCP 1.0\r\n"
1459 "C: 4256\r\n"
1460 , endpoint);
1461
1462 if (len < 0)
1463 return;
1464
1465 buf[sizeof(buf) - 1] = '\0';
1466
1467 send_trans(endp->cfg, buf, len);
1468}
1469
1470static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1471{
1472 return write(cfg->gw_fd.bfd.fd, buf, len);
1473}
1474
1475int mgcp_send_reset_all(struct mgcp_config *cfg)
1476{
1477 static const char mgcp_reset[] = {
1478 "RSIP 1 *@mgw MGCP 1.0\r\n"
1479 };
1480
1481 return send_agent(cfg, mgcp_reset, sizeof mgcp_reset -1);
1482}
1483
1484int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1485{
1486 char buf[128];
1487 int len;
1488
1489 len = snprintf(buf, sizeof(buf),
1490 "RSIP 39 %x@mgw MGCP 1.0\r\n"
1491 , endpoint);
1492 if (len < 0)
1493 return len;
1494
1495 buf[sizeof(buf) - 1] = '\0';
1496
1497 return send_agent(endp->cfg, buf, len);
1498}
1499
1500static int setup_rtp_processing(struct mgcp_endpoint *endp)
1501{
1502 int rc = 0;
1503 struct mgcp_config *cfg = endp->cfg;
1504
1505 if (endp->type != MGCP_RTP_DEFAULT)
1506 return 0;
1507
1508 if (endp->conn_mode == MGCP_CONN_LOOPBACK)
1509 return 0;
1510
1511 if (endp->conn_mode & MGCP_CONN_SEND_ONLY)
1512 rc |= cfg->setup_rtp_processing_cb(endp, &endp->net_end, &endp->bts_end);
1513 else
1514 rc |= cfg->setup_rtp_processing_cb(endp, &endp->net_end, NULL);
1515
1516 if (endp->conn_mode & MGCP_CONN_RECV_ONLY)
1517 rc |= cfg->setup_rtp_processing_cb(endp, &endp->bts_end, &endp->net_end);
1518 else
1519 rc |= cfg->setup_rtp_processing_cb(endp, &endp->bts_end, NULL);
1520 return rc;
1521}
1522
1523static void create_transcoder(struct mgcp_endpoint *endp)
1524{
1525 int port;
1526 int in_endp = ENDPOINT_NUMBER(endp);
1527 int out_endp = endp_back_channel(in_endp);
1528
1529 if (endp->type != MGCP_RTP_TRANSCODED)
1530 return;
1531
1532 send_msg(endp, in_endp, endp->trans_bts.local_port, "CRCX", "sendrecv");
1533 send_msg(endp, in_endp, endp->trans_bts.local_port, "MDCX", "sendrecv");
1534 send_msg(endp, out_endp, endp->trans_net.local_port, "CRCX", "sendrecv");
1535 send_msg(endp, out_endp, endp->trans_net.local_port, "MDCX", "sendrecv");
1536
1537 port = rtp_calculate_port(in_endp, endp->cfg->transcoder_remote_base);
1538 endp->trans_bts.rtp_port = htons(port);
1539 endp->trans_bts.rtcp_port = htons(port + 1);
1540
1541 port = rtp_calculate_port(out_endp, endp->cfg->transcoder_remote_base);
1542 endp->trans_net.rtp_port = htons(port);
1543 endp->trans_net.rtcp_port = htons(port + 1);
1544}
1545
1546static void delete_transcoder(struct mgcp_endpoint *endp)
1547{
1548 int in_endp = ENDPOINT_NUMBER(endp);
1549 int out_endp = endp_back_channel(in_endp);
1550
1551 if (endp->type != MGCP_RTP_TRANSCODED)
1552 return;
1553
1554 send_dlcx(endp, in_endp);
1555 send_dlcx(endp, out_endp);
1556}
1557
1558int mgcp_reset_transcoder(struct mgcp_config *cfg)
1559{
1560 if (!cfg->transcoder_ip)
1561 return 0;
1562
1563 static const char mgcp_reset[] = {
1564 "RSIP 1 13@mgw MGCP 1.0\r\n"
1565 };
1566
1567 return send_trans(cfg, mgcp_reset, sizeof mgcp_reset -1);
1568}
1569
1570void mgcp_format_stats(struct mgcp_endpoint *endp, char *msg, size_t size)
1571{
1572 uint32_t expected, jitter;
1573 int ploss;
1574 int nchars;
1575 mgcp_state_calc_loss(&endp->net_state, &endp->net_end,
1576 &expected, &ploss);
1577 jitter = mgcp_state_calc_jitter(&endp->net_state);
1578
1579 nchars = snprintf(msg, size,
1580 "\r\nP: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
1581 endp->bts_end.packets, endp->bts_end.octets,
1582 endp->net_end.packets, endp->net_end.octets,
1583 ploss, jitter);
1584 if (nchars < 0 || nchars >= size)
1585 goto truncate;
1586
1587 msg += nchars;
1588 size -= nchars;
1589
1590 /* Error Counter */
1591 nchars = snprintf(msg, size,
1592 "\r\nX-Osmo-CP: EC TIS=%u, TOS=%u, TIR=%u, TOR=%u",
1593 endp->net_state.in_stream.err_ts_counter,
1594 endp->net_state.out_stream.err_ts_counter,
1595 endp->bts_state.in_stream.err_ts_counter,
1596 endp->bts_state.out_stream.err_ts_counter);
1597 if (nchars < 0 || nchars >= size)
1598 goto truncate;
1599
1600 msg += nchars;
1601 size -= nchars;
1602
1603 if (endp->osmux.state == OSMUX_STATE_ENABLED) {
1604 snprintf(msg, size,
1605 "\r\nX-Osmux-ST: CR=%u, BR=%u",
1606 endp->osmux.stats.chunks,
1607 endp->osmux.stats.octets);
1608 }
1609truncate:
1610 msg[size - 1] = '\0';
1611}
1612
1613int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
1614 uint32_t *pr, uint32_t *_or, int *loss, uint32_t *jitter)
1615{
1616 char *line, *save;
1617 int rc;
1618
1619 /* initialize with bad values */
1620 *ps = *os = *pr = *_or = *jitter = UINT_MAX;
1621 *loss = INT_MAX;
1622
1623
1624 line = strtok_r((char *) msg->l2h, "\r\n", &save);
1625 if (!line)
1626 return -1;
1627
1628 /* this can only parse the message that is created above... */
1629 for_each_non_empty_line(line, save) {
1630 switch (line[0]) {
1631 case 'P':
1632 rc = sscanf(line, "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
1633 ps, os, pr, _or, loss, jitter);
1634 return rc == 6 ? 0 : -1;
1635 }
1636 }
1637
1638 return -1;
1639}