blob: d0af8c61e89dbfbd05516f3ed228c81267f3b5ce [file] [log] [blame]
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2
3/*
4 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009 by on-waves.com
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24#include <ctype.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <time.h>
29#include <limits.h>
30#include <unistd.h>
31
32#include <sys/socket.h>
33#include <arpa/inet.h>
34
35#include <openbsc/debug.h>
36#include <openbsc/msgb.h>
37#include <openbsc/talloc.h>
38#include <openbsc/gsm_data.h>
39#include <openbsc/select.h>
40#include <openbsc/mgcp.h>
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +010041#include <openbsc/telnet_interface.h>
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020042
43#include <vty/command.h>
44#include <vty/vty.h>
45
46/* this is here for the vty... it will never be called */
47void subscr_put() { abort(); }
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020048
49#define _GNU_SOURCE
50#include <getopt.h>
51
52#warning "Make use of the rtp proxy code"
53
54static int source_port = 2427;
55static const char *local_ip = NULL;
56static const char *source_addr = "0.0.0.0";
57static struct bsc_fd bfd;
58static const unsigned int number_endpoints = 32 + 1;
59static const char *bts_ip = NULL;
60static struct in_addr bts_in;
61static int first_request = 1;
62static const char *audio_name = "GSM-EFR/8000";
63static int audio_payload = 97;
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +010064static int audio_loop = 0;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020065static int early_bind = 0;
66
67static char *config_file = "mgcp.cfg";
68
69/* used by msgb and mgcp */
70void *tall_bsc_ctx = NULL;
71
72enum mgcp_connection_mode {
73 MGCP_CONN_NONE = 0,
74 MGCP_CONN_RECV_ONLY = 1,
75 MGCP_CONN_SEND_ONLY = 2,
76 MGCP_CONN_RECV_SEND = MGCP_CONN_RECV_ONLY | MGCP_CONN_SEND_ONLY,
77};
78
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +010079enum {
80 DEST_NETWORK = 0,
81 DEST_BTS = 1,
82};
83
84enum {
85 PROTO_RTP,
86 PROTO_RTCP,
87};
88
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020089#define CI_UNUSED 0
90static unsigned int last_call_id = 0;
91
92struct mgcp_endpoint {
93 int ci;
94 char *callid;
95 char *local_options;
96 int conn_mode;
97
98 /* the local rtp port */
99 int rtp_port;
100
101 /*
102 * RTP mangling:
103 * - we get RTP and RTCP to us and need to forward to the BTS
104 * - we get RTP and RTCP from the BTS and forward to the network
105 */
106 struct bsc_fd local_rtp;
107 struct bsc_fd local_rtcp;
108
109 struct in_addr remote;
110
111 /* in network byte order */
112 int rtp, rtcp;
113 int bts_rtp, bts_rtcp;
114};
115
116static struct mgcp_endpoint *endpoints = NULL;
117#define ENDPOINT_NUMBER(endp) abs(endp - endpoints)
118
119/**
120 * Macro for tokenizing MGCP messages and SDP in one go.
121 *
122 */
123#define MSG_TOKENIZE_START \
124 line_start = 0; \
125 for (i = 0; i < msgb_l3len(msg); ++i) { \
126 /* we have a line end */ \
127 if (msg->l3h[i] == '\n') { \
128 /* skip the first line */ \
129 if (line_start == 0) { \
130 line_start = i + 1; \
131 continue; \
132 } \
133 \
134 /* check if we have a proper param */ \
135 if (i - line_start == 1 && msg->l3h[line_start] == '\r') { \
136 } else if (i - line_start > 2 \
137 && islower(msg->l3h[line_start]) \
138 && msg->l3h[line_start + 1] == '=') { \
139 } else if (i - line_start < 3 \
140 || msg->l3h[line_start + 1] != ':' \
141 || msg->l3h[line_start + 2] != ' ') \
142 goto error; \
143 \
144 msg->l3h[i] = '\0'; \
145 if (msg->l3h[i-1] == '\r') \
146 msg->l3h[i-1] = '\0';
147
148#define MSG_TOKENIZE_END \
149 line_start = i + 1; \
150 } \
151 }
152
153
154struct mgcp_msg_ptr {
155 unsigned int start;
156 unsigned int length;
157};
158
159struct mgcp_request {
160 char *name;
161 void (*handle_request) (struct msgb *msg, struct sockaddr_in *source);
162 char *debug_name;
163};
164
165#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
166 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
167
168static void handle_audit_endpoint(struct msgb *msg, struct sockaddr_in *source);
169static void handle_create_con(struct msgb *msg, struct sockaddr_in *source);
170static void handle_delete_con(struct msgb *msg, struct sockaddr_in *source);
171static void handle_modify_con(struct msgb *msg, struct sockaddr_in *source);
172
173static int generate_call_id()
174{
175 int i;
176
177 /* use the call id */
178 ++last_call_id;
179
180 /* handle wrap around */
181 if (last_call_id == CI_UNUSED)
182 ++last_call_id;
183
184 /* callstack can only be of size number_of_endpoints */
185 /* verify that the call id is free, e.g. in case of overrun */
186 for (i = 1; i < number_endpoints; ++i)
187 if (endpoints[i].ci == last_call_id)
188 return generate_call_id();
189
190 return last_call_id;
191}
192
193/* FIXIME/TODO: need to have a list of pending transactions and check that */
194static unsigned int generate_transaction_id()
195{
196 return abs(rand());
197}
198
199static int _send(int fd, struct in_addr *addr, int port, char *buf, int len)
200{
201 struct sockaddr_in out;
202 out.sin_family = AF_INET;
203 out.sin_port = port;
204 memcpy(&out.sin_addr, addr, sizeof(*addr));
205
206 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
207}
208
209/*
210 * There is data coming. We will have to figure out if it
211 * came from the BTS or the MediaGateway of the MSC. On top
212 * of that we need to figure out if it was RTP or RTCP.
213 *
214 * Currently we do not communicate with the BSC so we have
215 * no idea where the BTS is listening for RTP and need to
216 * do the classic routing trick. Wait for the first packet
217 * from the BTS and then go ahead.
218 */
219static int rtp_data_cb(struct bsc_fd *fd, unsigned int what)
220{
221 char buf[4096];
222 struct sockaddr_in addr;
223 socklen_t slen = sizeof(addr);
224 struct mgcp_endpoint *endp;
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100225 int rc, dest, proto;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200226
227 endp = (struct mgcp_endpoint *) fd->data;
228
229 rc = recvfrom(fd->fd, &buf, sizeof(buf), 0,
230 (struct sockaddr *) &addr, &slen);
231 if (rc < 0) {
232 DEBUGP(DMGCP, "Failed to receive message on: 0x%x\n",
233 ENDPOINT_NUMBER(endp));
234 return -1;
235 }
236
Holger Hans Peter Freyther48ecad42009-11-19 16:04:10 +0100237 /* do not forward aynthing... maybe there is a packet from the bts */
238 if (endp->ci == CI_UNUSED)
239 return -1;
240
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200241 /*
242 * Figure out where to forward it to. This code assumes that we
243 * have received the Connection Modify and know who is a legitimate
244 * partner. According to the spec we could attempt to forward even
245 * after the Create Connection but we will not as we are not really
246 * able to tell if this is legitimate.
247 */
248 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100249 dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0
250 ? DEST_BTS : DEST_NETWORK;
251 proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200252
253 /* We have no idea who called us, maybe it is the BTS. */
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100254 if (dest == DEST_NETWORK && endp->bts_rtp == 0) {
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200255 /* it was the BTS... */
256 if (memcmp(&addr.sin_addr, &bts_in, sizeof(bts_in)) == 0) {
257 if (fd == &endp->local_rtp) {
258 endp->bts_rtp = addr.sin_port;
259 endp->bts_rtcp = htons(ntohs(addr.sin_port) + 1);
260 } else {
261 endp->bts_rtp = htons(ntohs(addr.sin_port) - 1);
262 endp->bts_rtcp = addr.sin_port;
263 }
264
265 DEBUGP(DMGCP, "Found BTS for endpoint: 0x%x on port: %d\n",
266 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp));
267 }
268 }
269
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100270 /* dispatch */
271 if (audio_loop)
272 dest = !dest;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200273
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100274 if (dest == DEST_NETWORK) {
275 return _send(fd->fd, &endp->remote,
276 proto == PROTO_RTP ? endp->rtp : endp->rtcp,
277 buf, rc);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200278 } else {
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100279 return _send(fd->fd, &bts_in,
280 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
281 buf, rc);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200282 }
283}
284
285static int create_bind(struct bsc_fd *fd, int port)
286{
287 struct sockaddr_in addr;
288 int on = 1;
289
290 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
291 if (fd->fd < 0)
292 return -1;
293
294 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
295 memset(&addr, 0, sizeof(addr));
296 addr.sin_family = AF_INET;
297 addr.sin_port = htons(port);
298 inet_aton(source_addr, &addr.sin_addr);
299
300 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
301 return -1;
302
303 return 0;
304}
305
306static int bind_rtp(struct mgcp_endpoint *endp)
307{
308 /* set to zero until we get the info */
309 memset(&endp->remote, 0, sizeof(endp->remote));
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200310
311 if (create_bind(&endp->local_rtp, endp->rtp_port) != 0) {
312 DEBUGP(DMGCP, "Failed to create RTP port: %d on 0x%x\n",
313 endp->rtp_port, ENDPOINT_NUMBER(endp));
314 goto cleanup0;
315 }
316
317 if (create_bind(&endp->local_rtcp, endp->rtp_port + 1) != 0) {
318 DEBUGP(DMGCP, "Failed to create RTCP port: %d on 0x%x\n",
319 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
320 goto cleanup1;
321 }
322
323 endp->local_rtp.cb = rtp_data_cb;
324 endp->local_rtp.data = endp;
325 endp->local_rtp.when = BSC_FD_READ;
326 if (bsc_register_fd(&endp->local_rtp) != 0) {
327 DEBUGP(DMGCP, "Failed to register RTP port %d on 0x%x\n",
328 endp->rtp_port, ENDPOINT_NUMBER(endp));
329 goto cleanup2;
330 }
331
332 endp->local_rtcp.cb = rtp_data_cb;
333 endp->local_rtcp.data = endp;
334 endp->local_rtcp.when = BSC_FD_READ;
335 if (bsc_register_fd(&endp->local_rtcp) != 0) {
336 DEBUGP(DMGCP, "Failed to register RTCP port %d on 0x%x\n",
337 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
338 goto cleanup3;
339 }
340
341 return 0;
342
343cleanup3:
344 bsc_unregister_fd(&endp->local_rtp);
345cleanup2:
346 close(endp->local_rtcp.fd);
347 endp->local_rtcp.fd = -1;
348cleanup1:
349 close(endp->local_rtp.fd);
350 endp->local_rtp.fd = -1;
351cleanup0:
352 return -1;
353}
354
355/*
356 * array of function pointers for handling various
357 * messages. In the future this might be binary sorted
358 * for performance reasons.
359 */
360static const struct mgcp_request mgcp_requests [] = {
361 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
362 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
363 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
364 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
365};
366
367static void send_response_with_data(int code, const char *msg, const char *trans,
368 const char *data, struct sockaddr_in *source)
369{
370 char buf[4096];
371 int len;
372
373 if (data) {
374 len = snprintf(buf, sizeof(buf), "%d %s\n%s", code, trans, data);
375 } else {
376 len = snprintf(buf, sizeof(buf), "%d %s\n", code, trans);
377 }
378 DEBUGP(DMGCP, "Sending response: code: %d for '%s'\n", code, msg);
379
380 sendto(bfd.fd, buf, len, 0, (struct sockaddr *)source, sizeof(*source));
381}
382
383static void send_response(int code, const char *msg, const char *trans, struct sockaddr_in *source)
384{
385 send_response_with_data(code, msg, trans, NULL, source);
386}
387
388static void send_with_sdp(struct mgcp_endpoint *endp, const char *msg, const char *trans_id, struct sockaddr_in *source)
389{
390 const char *addr = local_ip;
391 char sdp_record[4096];
392
393 if (!addr)
394 addr = source_addr;
395
396 snprintf(sdp_record, sizeof(sdp_record) - 1,
397 "I: %d\n\n"
398 "v=0\r\n"
399 "c=IN IP4 %s\r\n"
400 "m=audio %d RTP/AVP %d\r\n"
401 "a=rtpmap:%d %s\r\n",
402 endp->ci, addr, endp->rtp_port,
403 audio_payload, audio_payload, audio_name);
404 return send_response_with_data(200, msg, trans_id, sdp_record, source);
405}
406
407/* send a static record */
408static void send_rsip(struct sockaddr_in *source)
409{
410 char reset[4096];
411 int len, rc;
412
413 len = snprintf(reset, sizeof(reset) - 1,
414 "RSIP %u *@mgw MGCP 1.0\n"
415 "RM: restart\n", generate_transaction_id());
416 rc = sendto(bfd.fd, reset, len, 0, (struct sockaddr *) source, sizeof(*source));
417 if (rc < 0) {
418 DEBUGP(DMGCP, "Failed to send RSIP: %d\n", rc);
419 }
420}
421
422/*
423 * handle incoming messages:
424 * - this can be a command (four letters, space, transaction id)
425 * - or a response (three numbers, space, transaction id)
426 */
427static void handle_message(struct msgb *msg, struct sockaddr_in *source)
428{
429 int code;
430
431 if (msg->len < 4) {
432 DEBUGP(DMGCP, "mgs too short: %d\n", msg->len);
433 return;
434 }
435
436 /* attempt to treat it as a response */
437 if (sscanf((const char *)&msg->data[0], "%3d %*s", &code) == 1) {
438 DEBUGP(DMGCP, "Response: Code: %d\n", code);
439 } else {
440 int i, handled = 0;
441 msg->l3h = &msg->l2h[4];
442 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i)
443 if (strncmp(mgcp_requests[i].name, (const char *) &msg->data[0], 4) == 0) {
444 handled = 1;
445 mgcp_requests[i].handle_request(msg, source);
446 }
447 if (!handled) {
448 DEBUGP(DMGCP, "MSG with type: '%.4s' not handled\n", &msg->data[0]);
449 }
450 }
451}
452
453/* string tokenizer for the poor */
454static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length)
455{
456 int i, found = 0;
457
458 int whitespace = 1;
459 for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) {
460 /* if we have a space we found an end */
461 if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
462 if (!whitespace) {
463 ++found;
464 whitespace = 1;
465 ptrs->length = i - ptrs->start - 1;
466 ++ptrs;
467 --ptrs_length;
468 } else {
469 /* skip any number of whitespace */
470 }
471
472 /* line end... stop */
473 if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n')
474 break;
475 } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
476 /* line end, be done */
477 break;
478 } else if (whitespace) {
479 whitespace = 0;
480 ptrs->start = i;
481 }
482 }
483
484 if (ptrs_length == 0)
485 return -1;
486 return found;
487}
488
489static struct mgcp_endpoint *find_endpoint(const char *mgcp)
490{
491 char *endptr = NULL;
492 unsigned int gw = INT_MAX;
493
494 gw = strtoul(mgcp, &endptr, 16);
495 if (gw == 0 || gw >= number_endpoints || strcmp(endptr, "@mgw") != 0) {
496 DEBUGP(DMGCP, "Not able to find endpoint: '%s'\n", mgcp);
497 return NULL;
498 }
499
500 return &endpoints[gw];
501}
502
503static int analyze_header(struct msgb *msg, struct mgcp_msg_ptr *ptr, int size,
504 const char **transaction_id, struct mgcp_endpoint **endp)
505{
506 int found;
507
508 if (size < 3) {
509 DEBUGP(DMGCP, "Not enough space in ptr\n");
510 return -1;
511 }
512
513 found = find_msg_pointers(msg, ptr, size);
514
515 if (found < 3) {
516 DEBUGP(DMGCP, "Gateway: Not enough params. Found: %d\n", found);
517 return -1;
518 }
519
520 /*
521 * replace the space with \0. the main method gurantess that
522 * we still have + 1 for null termination
523 */
524 msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0';
525 msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0';
526 msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0';
527 msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0';
528
529 if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0
530 || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) {
531 DEBUGP(DMGCP, "Wrong MGCP version. Not handling: '%s' '%s'\n",
532 (const char *)&msg->l3h[ptr[3].start],
533 (const char *)&msg->l3h[ptr[2].start]);
534 return -1;
535 }
536
537 *transaction_id = (const char *)&msg->l3h[ptr[0].start];
538 *endp = find_endpoint((const char *)&msg->l3h[ptr[1].start]);
539 return *endp == NULL;
540}
541
542static int verify_call_id(const struct mgcp_endpoint *endp,
543 const char *callid)
544{
545 if (strcmp(endp->callid, callid) != 0) {
546 DEBUGP(DMGCP, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
547 ENDPOINT_NUMBER(endp), endp->callid, callid);
548 return -1;
549 }
550
551 return 0;
552}
553
554static int verify_ci(const struct mgcp_endpoint *endp,
555 const char *ci)
556{
557 if (atoi(ci) != endp->ci) {
558 DEBUGP(DMGCP, "ConnectionIdentifiers do not match on 0x%x. %d != %s\n",
559 ENDPOINT_NUMBER(endp), endp->ci, ci);
560 return -1;
561 }
562
563 return 0;
564}
565
566static void handle_audit_endpoint(struct msgb *msg, struct sockaddr_in *source)
567{
568 struct mgcp_msg_ptr data_ptrs[6];
569 int found, response;
570 const char *trans_id;
571 struct mgcp_endpoint *endp;
572
573 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
574 if (found != 0)
575 response = 500;
576 else
577 response = 200;
578
579 return send_response(response, "AUEP", trans_id, source);
580}
581
582static int parse_conn_mode(const char* msg, int *conn_mode)
583{
584 int ret = 0;
585 if (strcmp(msg, "recvonly") == 0)
586 *conn_mode = MGCP_CONN_RECV_ONLY;
587 else if (strcmp(msg, "sendrecv") == 0)
588 *conn_mode = MGCP_CONN_RECV_SEND;
589 else {
590 DEBUGP(DMGCP, "Unknown connection mode: '%s'\n", msg);
591 ret = -1;
592 }
593
594 return ret;
595}
596
597static void handle_create_con(struct msgb *msg, struct sockaddr_in *source)
598{
599 struct mgcp_msg_ptr data_ptrs[6];
600 int found, i, line_start;
601 const char *trans_id;
602 struct mgcp_endpoint *endp;
603 int error_code = 500;
604
605 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
606 if (found != 0)
607 return send_response(500, "CRCX", trans_id, source);
608
609 if (endp->ci != CI_UNUSED) {
610 DEBUGP(DMGCP, "Endpoint is already used. 0x%x\n", ENDPOINT_NUMBER(endp));
611 return send_response(500, "CRCX", trans_id, source);
612 }
613
614 /* parse CallID C: and LocalParameters L: */
615 MSG_TOKENIZE_START
616 switch (msg->l3h[line_start]) {
617 case 'L':
618 endp->local_options = talloc_strdup(endpoints,
619 (const char *)&msg->l3h[line_start + 3]);
620 break;
621 case 'C':
622 endp->callid = talloc_strdup(endpoints,
623 (const char *)&msg->l3h[line_start + 3]);
624 break;
625 case 'M':
626 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
627 &endp->conn_mode) != 0) {
628 error_code = 517;
629 goto error2;
630 }
631 break;
632 default:
633 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
634 msg->l3h[line_start], msg->l3h[line_start],
635 ENDPOINT_NUMBER(endp));
636 break;
637 }
638 MSG_TOKENIZE_END
639
Holger Hans Peter Freyther48ecad42009-11-19 16:04:10 +0100640 /* initialize */
641 endp->rtp = endp->rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200642
643 /* bind to the port now */
644 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
645 if (!early_bind && bind_rtp(endp) != 0)
646 goto error2;
647
648 /* assign a local call identifier or fail */
649 endp->ci = generate_call_id();
650 if (endp->ci == CI_UNUSED)
651 goto error2;
652
653 DEBUGP(DMGCP, "Creating endpoint on: 0x%x CI: %u port: %u\n",
654 ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port);
655 return send_with_sdp(endp, "CRCX", trans_id, source);
656error:
657 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
658 hexdump(msg->l3h, msgb_l3len(msg)),
659 ENDPOINT_NUMBER(endp), line_start, i);
660 return send_response(error_code, "CRCX", trans_id, source);
661
662error2:
663 DEBUGP(DMGCP, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
664 return send_response(error_code, "CRCX", trans_id, source);
665}
666
667static void handle_modify_con(struct msgb *msg, struct sockaddr_in *source)
668{
669 struct mgcp_msg_ptr data_ptrs[6];
670 int found, i, line_start;
671 const char *trans_id;
672 struct mgcp_endpoint *endp;
673 int error_code = 500;
674
675 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
676 if (found != 0)
677 return send_response(error_code, "MDCX", trans_id, source);
678
679 if (endp->ci == CI_UNUSED) {
680 DEBUGP(DMGCP, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
681 return send_response(error_code, "MDCX", trans_id, source);
682 }
683
684 MSG_TOKENIZE_START
685 switch (msg->l3h[line_start]) {
686 case 'C': {
687 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
688 goto error3;
689 break;
690 }
691 case 'I': {
692 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
693 goto error3;
694 break;
695 }
696 case 'L':
697 /* skip */
698 break;
699 case 'M':
700 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
701 &endp->conn_mode) != 0) {
702 error_code = 517;
703 goto error3;
704 }
705 break;
706 case '\0':
707 /* SDP file begins */
708 break;
709 case 'a':
710 case 'o':
711 case 's':
712 case 't':
713 case 'v':
714 /* skip these SDP attributes */
715 break;
716 case 'm': {
717 int port;
718 const char *param = (const char *)&msg->l3h[line_start];
719
720 if (sscanf(param, "m=audio %d RTP/AVP %*d", &port) == 1) {
721 endp->rtp = htons(port);
722 endp->rtcp = htons(port + 1);
723 }
724 break;
725 }
726 case 'c': {
727 char ipv4[16];
728 const char *param = (const char *)&msg->l3h[line_start];
729
730 if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) {
731 inet_aton(ipv4, &endp->remote);
732 }
733 break;
734 }
735 default:
736 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
737 msg->l3h[line_start], msg->l3h[line_start],
738 ENDPOINT_NUMBER(endp));
739 break;
740 }
741 MSG_TOKENIZE_END
742
743 /* modify */
744 DEBUGP(DMGCP, "Modified endpoint on: 0x%x Server: %s:%u\n",
745 ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), endp->rtp);
746 return send_with_sdp(endp, "MDCX", trans_id, source);
747
748error:
749 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n",
750 hexdump(msg->l3h, msgb_l3len(msg)),
751 ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]);
752 return send_response(error_code, "MDCX", trans_id, source);
753
754error3:
755 return send_response(error_code, "MDCX", trans_id, source);
756}
757
758static void handle_delete_con(struct msgb *msg, struct sockaddr_in *source)
759{
760 struct mgcp_msg_ptr data_ptrs[6];
761 int found, i, line_start;
762 const char *trans_id;
763 struct mgcp_endpoint *endp;
764 int error_code = 500;
765
766 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
767 if (found != 0)
768 return send_response(error_code, "DLCX", trans_id, source);
769
770 if (endp->ci == CI_UNUSED) {
771 DEBUGP(DMGCP, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
772 return send_response(error_code, "DLCX", trans_id, source);
773 }
774
775 MSG_TOKENIZE_START
776 switch (msg->l3h[line_start]) {
777 case 'C': {
778 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
779 goto error3;
780 break;
781 }
782 case 'I': {
783 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
784 goto error3;
785 break;
786 }
787 default:
788 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
789 msg->l3h[line_start], msg->l3h[line_start],
790 ENDPOINT_NUMBER(endp));
791 break;
792 }
793 MSG_TOKENIZE_END
794
795
796 /* free the connection */
797 DEBUGP(DMGCP, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
798 endp->ci= CI_UNUSED;
799 talloc_free(endp->callid);
800 talloc_free(endp->local_options);
801
802 if (!early_bind) {
803 bsc_unregister_fd(&endp->local_rtp);
804 bsc_unregister_fd(&endp->local_rtcp);
805 }
806
Holger Hans Peter Freyther48ecad42009-11-19 16:04:10 +0100807 endp->rtp = endp->rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
808
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200809 return send_response(250, "DLCX", trans_id, source);
810
811error:
812 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
813 hexdump(msg->l3h, msgb_l3len(msg)),
814 ENDPOINT_NUMBER(endp), line_start, i);
815 return send_response(error_code, "DLCX", trans_id, source);
816
817error3:
818 return send_response(error_code, "DLCX", trans_id, source);
819}
820
821static void print_help()
822{
823 printf("Some useful help...\n");
824 printf(" -h --help is printing this text.\n");
825 printf(" -c --config-file filename The config file to use.\n");
826}
827
828static void handle_options(int argc, char** argv)
829{
830 while (1) {
831 int option_index = 0, c;
832 static struct option long_options[] = {
833 {"help", 0, 0, 'h'},
834 {"config-file", 1, 0, 'c'},
835 {0, 0, 0, 0},
836 };
837
838 c = getopt_long(argc, argv, "hc:", long_options, &option_index);
839
840 if (c == -1)
841 break;
842
843 switch(c) {
844 case 'h':
845 print_help();
846 exit(0);
847 break;
848 case 'c':
849 config_file = talloc_strdup(tall_bsc_ctx, optarg);
850 break;
851 default:
852 /* ignore */
853 break;
854 };
855 }
856}
857
858static int read_call_agent(struct bsc_fd *fd, unsigned int what)
859{
860 struct sockaddr_in addr;
861 socklen_t slen = sizeof(addr);
862 struct msgb *msg;
863
864 msg = (struct msgb *) fd->data;
865
866 /* read one less so we can use it as a \0 */
867 int rc = recvfrom(bfd.fd, msg->data, msg->data_len - 1, 0,
868 (struct sockaddr *) &addr, &slen);
869 if (rc < 0) {
870 perror("Gateway failed to read");
871 return -1;
872 } else if (slen > sizeof(addr)) {
873 fprintf(stderr, "Gateway received message from outerspace: %d %d\n",
874 slen, sizeof(addr));
875 return -1;
876 }
877
878 if (first_request) {
879 first_request = 0;
880 send_rsip(&addr);
881 return 0;
882 }
883
884 /* handle message now */
885 msg->l2h = msgb_put(msg, rc);
886 handle_message(msg, &addr);
887 msgb_reset(msg);
888 return 0;
889}
890
891/*
892 * vty code for mgcp below
893 */
894struct cmd_node mgcp_node = {
895 MGCP_NODE,
896 "%s(mgcp)#",
897 1,
898};
899
900static int config_write_mgcp(struct vty *vty)
901{
902 vty_out(vty, "mgcp%s", VTY_NEWLINE);
903 if (local_ip)
904 vty_out(vty, " local ip %s%s", local_ip, VTY_NEWLINE);
905 vty_out(vty, " bts ip %s%s", bts_ip, VTY_NEWLINE);
906 vty_out(vty, " bind ip %s%s", source_addr, VTY_NEWLINE);
907 vty_out(vty, " bind port %u%s", source_port, VTY_NEWLINE);
Holger Hans Peter Freyther5fddf472009-11-19 15:08:02 +0100908 vty_out(vty, " bind early %u%s", !!early_bind, VTY_NEWLINE);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200909 vty_out(vty, " rtp base %u%s", rtp_base_port, VTY_NEWLINE);
Holger Hans Peter Freyther5fddf472009-11-19 15:08:02 +0100910 vty_out(vty, " sdp audio payload number %u%s", audio_payload, VTY_NEWLINE);
911 vty_out(vty, " sdp audio payload name %s%s", audio_name, VTY_NEWLINE);
912 vty_out(vty, " loop %u%s", !!audio_loop, VTY_NEWLINE);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200913
914 return CMD_SUCCESS;
915}
916
Holger Hans Peter Freyther17e79162009-11-19 15:20:48 +0100917DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
918 SHOW_STR "Display information about the MGCP Media Gateway")
919{
920 int i;
921
922 vty_out(vty, "MGCP is up and running with %u endpoints:%s", number_endpoints - 1, VTY_NEWLINE);
923 for (i = 1; i < number_endpoints; ++i) {
924 struct mgcp_endpoint *endp = &endpoints[i];
925 vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u%s",
926 i, endp->ci,
927 ntohs(endp->rtp), ntohs(endp->rtcp),
928 ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp), VTY_NEWLINE);
929 }
930
931 return CMD_SUCCESS;
932}
933
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200934DEFUN(cfg_mgcp,
935 cfg_mgcp_cmd,
936 "mgcp",
937 "Configure the MGCP")
938{
939 vty->node = MGCP_NODE;
940 return CMD_SUCCESS;
941}
942
943DEFUN(cfg_mgcp_local_ip,
944 cfg_mgcp_local_ip_cmd,
945 "local ip IP",
946 "Set the IP to be used in SDP records")
947{
948 local_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
949 return CMD_SUCCESS;
950}
951
952DEFUN(cfg_mgcp_bts_ip,
953 cfg_mgcp_bts_ip_cmd,
954 "bts ip IP",
955 "Set the IP of the BTS for RTP forwarding")
956{
957 bts_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
958 inet_aton(bts_ip, &bts_in);
959 return CMD_SUCCESS;
960}
961
962DEFUN(cfg_mgcp_bind_ip,
963 cfg_mgcp_bind_ip_cmd,
964 "bind ip IP",
965 "Bind the MGCP to this local addr")
966{
967 source_addr = talloc_strdup(tall_bsc_ctx, argv[0]);
968 return CMD_SUCCESS;
969}
970
971DEFUN(cfg_mgcp_bind_port,
972 cfg_mgcp_bind_port_cmd,
973 "bind port <0-65534>",
974 "Bind the MGCP to this port")
975{
976 unsigned int port = atoi(argv[0]);
977 if (port > 65534) {
978 vty_out(vty, "%% wrong bind port '%s'%s", argv[0], VTY_NEWLINE);
979 return CMD_WARNING;
980 }
981
982 source_port = port;
983 return CMD_SUCCESS;
984}
985
986DEFUN(cfg_mgcp_bind_early,
987 cfg_mgcp_bind_early_cmd,
988 "bind early (0|1)",
989 "Bind all RTP ports early")
990{
991 unsigned int bind = atoi(argv[0]);
992 if (bind != 0 && bind != 1) {
993 vty_out(vty, "%% param must be 0 or 1.%s", VTY_NEWLINE);
994 return CMD_WARNING;
995 }
996
997 early_bind = bind == 1;
998 return CMD_SUCCESS;
999}
1000
1001DEFUN(cfg_mgcp_rtp_base_port,
1002 cfg_mgcp_rtp_base_port_cmd,
1003 "rtp base <0-65534>",
1004 "Base port to use")
1005{
1006 unsigned int port = atoi(argv[0]);
1007 if (port > 65534) {
1008 vty_out(vty, "%% wrong base port '%s'%s", argv[0], VTY_NEWLINE);
1009 return CMD_WARNING;
1010 }
1011
1012 rtp_base_port = port;
1013 return CMD_SUCCESS;
1014}
1015
1016DEFUN(cfg_mgcp_sdp_payload_number,
1017 cfg_mgcp_sdp_payload_number_cmd,
1018 "sdp audio payload number <1-255>",
1019 "Set the audio codec to use")
1020{
1021 unsigned int payload = atoi(argv[0]);
1022 if (payload > 255) {
1023 vty_out(vty, "%% wrong payload number '%s'%s", argv[0], VTY_NEWLINE);
1024 return CMD_WARNING;
1025 }
1026
1027 audio_payload = payload;
1028 return CMD_SUCCESS;
1029}
1030
1031DEFUN(cfg_mgcp_sdp_payload_name,
1032 cfg_mgcp_sdp_payload_name_cmd,
1033 "sdp audio payload name NAME",
1034 "Set the audio name to use")
1035{
1036 audio_name = talloc_strdup(tall_bsc_ctx, argv[0]);
1037 return CMD_SUCCESS;
1038}
1039
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +01001040DEFUN(cfg_mgcp_loop,
1041 cfg_mgcp_loop_cmd,
1042 "loop (0|1)",
1043 "Loop the audio")
1044{
1045 audio_loop = atoi(argv[0]);
1046 return CMD_SUCCESS;
1047}
1048
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001049int bsc_vty_init(struct gsm_network *dummy)
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001050{
1051 cmd_init(1);
1052 vty_init();
1053
Holger Hans Peter Freyther17e79162009-11-19 15:20:48 +01001054 install_element(VIEW_NODE, &show_mgcp_cmd);
1055
1056
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001057 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
1058 install_node(&mgcp_node, config_write_mgcp);
1059 install_default(MGCP_NODE);
1060 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
1061 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
1062 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
1063 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
1064 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
1065 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
1066 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
1067 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +01001068 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001069 return 0;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001070}
1071
1072int main(int argc, char** argv)
1073{
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001074 struct gsm_network dummy_network;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001075 struct sockaddr_in addr;
1076 int on = 1, i, rc;
1077
1078 tall_bsc_ctx = talloc_named_const(NULL, 1, "mgcp-callagent");
1079 handle_options(argc, argv);
1080
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001081 telnet_init(&dummy_network, 4243);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001082 rc = vty_read_config_file(config_file);
1083 if (rc < 0) {
1084 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1085 return rc;
1086 }
1087
1088
1089 if (!bts_ip) {
1090 fprintf(stderr, "Need to specify the BTS ip address for RTP handling.\n");
1091 return -1;
1092 }
1093
1094 endpoints = _talloc_zero_array(tall_bsc_ctx,
1095 sizeof(struct mgcp_endpoint),
1096 number_endpoints, "endpoints");
1097 if (!endpoints) {
1098 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", number_endpoints);
1099 return -1;
1100 }
1101
1102 /* Initialize all endpoints */
1103 for (i = 0; i < number_endpoints; ++i) {
1104 endpoints[i].local_rtp.fd = -1;
1105 endpoints[i].local_rtcp.fd = -1;
1106 endpoints[i].ci = CI_UNUSED;
1107 }
1108
1109 /* initialize the socket */
1110 bfd.when = BSC_FD_READ;
1111 bfd.cb = read_call_agent;
1112 bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
1113 if (bfd.fd < 0) {
1114 perror("Gateway failed to listen");
1115 return -1;
1116 }
1117
1118 setsockopt(bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1119
1120 memset(&addr, 0, sizeof(addr));
1121 addr.sin_family = AF_INET;
1122 addr.sin_port = htons(source_port);
1123 inet_aton(source_addr, &addr.sin_addr);
1124
1125 if (bind(bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1126 perror("Gateway failed to bind");
1127 return -1;
1128 }
1129
1130 bfd.data = msgb_alloc(4096, "mgcp-msg");
1131 if (!bfd.data) {
1132 fprintf(stderr, "Gateway memory error.\n");
1133 return -1;
1134 }
1135
1136
1137 if (bsc_register_fd(&bfd) != 0) {
1138 DEBUGP(DMGCP, "Failed to register the fd\n");
1139 return -1;
1140 }
1141
1142 /* initialisation */
1143 srand(time(NULL));
1144
1145 /* early bind */
1146 if (early_bind) {
1147 for (i = 1; i < number_endpoints; ++i) {
1148 struct mgcp_endpoint *endp = &endpoints[i];
1149 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
1150 if (bind_rtp(endp) != 0)
1151 return -1;
1152 }
1153 }
1154
1155 /* main loop */
1156 while (1) {
1157 bsc_select_main(0);
1158 }
1159
1160
1161 return 0;
1162}