blob: df9564140cf3e8543ba228637def9a17cc37c0af [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
237 /*
238 * Figure out where to forward it to. This code assumes that we
239 * have received the Connection Modify and know who is a legitimate
240 * partner. According to the spec we could attempt to forward even
241 * after the Create Connection but we will not as we are not really
242 * able to tell if this is legitimate.
243 */
244 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100245 dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0
246 ? DEST_BTS : DEST_NETWORK;
247 proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200248
249 /* We have no idea who called us, maybe it is the BTS. */
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100250 if (dest == DEST_NETWORK && endp->bts_rtp == 0) {
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200251 /* it was the BTS... */
252 if (memcmp(&addr.sin_addr, &bts_in, sizeof(bts_in)) == 0) {
253 if (fd == &endp->local_rtp) {
254 endp->bts_rtp = addr.sin_port;
255 endp->bts_rtcp = htons(ntohs(addr.sin_port) + 1);
256 } else {
257 endp->bts_rtp = htons(ntohs(addr.sin_port) - 1);
258 endp->bts_rtcp = addr.sin_port;
259 }
260
261 DEBUGP(DMGCP, "Found BTS for endpoint: 0x%x on port: %d\n",
262 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp));
263 }
264 }
265
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100266 /* dispatch */
267 if (audio_loop)
268 dest = !dest;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200269
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100270 if (dest == DEST_NETWORK) {
271 return _send(fd->fd, &endp->remote,
272 proto == PROTO_RTP ? endp->rtp : endp->rtcp,
273 buf, rc);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200274 } else {
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100275 return _send(fd->fd, &bts_in,
276 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
277 buf, rc);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200278 }
279}
280
281static int create_bind(struct bsc_fd *fd, int port)
282{
283 struct sockaddr_in addr;
284 int on = 1;
285
286 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
287 if (fd->fd < 0)
288 return -1;
289
290 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
291 memset(&addr, 0, sizeof(addr));
292 addr.sin_family = AF_INET;
293 addr.sin_port = htons(port);
294 inet_aton(source_addr, &addr.sin_addr);
295
296 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
297 return -1;
298
299 return 0;
300}
301
302static int bind_rtp(struct mgcp_endpoint *endp)
303{
304 /* set to zero until we get the info */
305 memset(&endp->remote, 0, sizeof(endp->remote));
306 endp->bts_rtp = endp->bts_rtcp = 0;
307 endp->rtp = endp->rtcp = 0;
308
309 if (create_bind(&endp->local_rtp, endp->rtp_port) != 0) {
310 DEBUGP(DMGCP, "Failed to create RTP port: %d on 0x%x\n",
311 endp->rtp_port, ENDPOINT_NUMBER(endp));
312 goto cleanup0;
313 }
314
315 if (create_bind(&endp->local_rtcp, endp->rtp_port + 1) != 0) {
316 DEBUGP(DMGCP, "Failed to create RTCP port: %d on 0x%x\n",
317 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
318 goto cleanup1;
319 }
320
321 endp->local_rtp.cb = rtp_data_cb;
322 endp->local_rtp.data = endp;
323 endp->local_rtp.when = BSC_FD_READ;
324 if (bsc_register_fd(&endp->local_rtp) != 0) {
325 DEBUGP(DMGCP, "Failed to register RTP port %d on 0x%x\n",
326 endp->rtp_port, ENDPOINT_NUMBER(endp));
327 goto cleanup2;
328 }
329
330 endp->local_rtcp.cb = rtp_data_cb;
331 endp->local_rtcp.data = endp;
332 endp->local_rtcp.when = BSC_FD_READ;
333 if (bsc_register_fd(&endp->local_rtcp) != 0) {
334 DEBUGP(DMGCP, "Failed to register RTCP port %d on 0x%x\n",
335 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
336 goto cleanup3;
337 }
338
339 return 0;
340
341cleanup3:
342 bsc_unregister_fd(&endp->local_rtp);
343cleanup2:
344 close(endp->local_rtcp.fd);
345 endp->local_rtcp.fd = -1;
346cleanup1:
347 close(endp->local_rtp.fd);
348 endp->local_rtp.fd = -1;
349cleanup0:
350 return -1;
351}
352
353/*
354 * array of function pointers for handling various
355 * messages. In the future this might be binary sorted
356 * for performance reasons.
357 */
358static const struct mgcp_request mgcp_requests [] = {
359 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
360 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
361 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
362 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
363};
364
365static void send_response_with_data(int code, const char *msg, const char *trans,
366 const char *data, struct sockaddr_in *source)
367{
368 char buf[4096];
369 int len;
370
371 if (data) {
372 len = snprintf(buf, sizeof(buf), "%d %s\n%s", code, trans, data);
373 } else {
374 len = snprintf(buf, sizeof(buf), "%d %s\n", code, trans);
375 }
376 DEBUGP(DMGCP, "Sending response: code: %d for '%s'\n", code, msg);
377
378 sendto(bfd.fd, buf, len, 0, (struct sockaddr *)source, sizeof(*source));
379}
380
381static void send_response(int code, const char *msg, const char *trans, struct sockaddr_in *source)
382{
383 send_response_with_data(code, msg, trans, NULL, source);
384}
385
386static void send_with_sdp(struct mgcp_endpoint *endp, const char *msg, const char *trans_id, struct sockaddr_in *source)
387{
388 const char *addr = local_ip;
389 char sdp_record[4096];
390
391 if (!addr)
392 addr = source_addr;
393
394 snprintf(sdp_record, sizeof(sdp_record) - 1,
395 "I: %d\n\n"
396 "v=0\r\n"
397 "c=IN IP4 %s\r\n"
398 "m=audio %d RTP/AVP %d\r\n"
399 "a=rtpmap:%d %s\r\n",
400 endp->ci, addr, endp->rtp_port,
401 audio_payload, audio_payload, audio_name);
402 return send_response_with_data(200, msg, trans_id, sdp_record, source);
403}
404
405/* send a static record */
406static void send_rsip(struct sockaddr_in *source)
407{
408 char reset[4096];
409 int len, rc;
410
411 len = snprintf(reset, sizeof(reset) - 1,
412 "RSIP %u *@mgw MGCP 1.0\n"
413 "RM: restart\n", generate_transaction_id());
414 rc = sendto(bfd.fd, reset, len, 0, (struct sockaddr *) source, sizeof(*source));
415 if (rc < 0) {
416 DEBUGP(DMGCP, "Failed to send RSIP: %d\n", rc);
417 }
418}
419
420/*
421 * handle incoming messages:
422 * - this can be a command (four letters, space, transaction id)
423 * - or a response (three numbers, space, transaction id)
424 */
425static void handle_message(struct msgb *msg, struct sockaddr_in *source)
426{
427 int code;
428
429 if (msg->len < 4) {
430 DEBUGP(DMGCP, "mgs too short: %d\n", msg->len);
431 return;
432 }
433
434 /* attempt to treat it as a response */
435 if (sscanf((const char *)&msg->data[0], "%3d %*s", &code) == 1) {
436 DEBUGP(DMGCP, "Response: Code: %d\n", code);
437 } else {
438 int i, handled = 0;
439 msg->l3h = &msg->l2h[4];
440 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i)
441 if (strncmp(mgcp_requests[i].name, (const char *) &msg->data[0], 4) == 0) {
442 handled = 1;
443 mgcp_requests[i].handle_request(msg, source);
444 }
445 if (!handled) {
446 DEBUGP(DMGCP, "MSG with type: '%.4s' not handled\n", &msg->data[0]);
447 }
448 }
449}
450
451/* string tokenizer for the poor */
452static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length)
453{
454 int i, found = 0;
455
456 int whitespace = 1;
457 for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) {
458 /* if we have a space we found an end */
459 if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
460 if (!whitespace) {
461 ++found;
462 whitespace = 1;
463 ptrs->length = i - ptrs->start - 1;
464 ++ptrs;
465 --ptrs_length;
466 } else {
467 /* skip any number of whitespace */
468 }
469
470 /* line end... stop */
471 if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n')
472 break;
473 } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
474 /* line end, be done */
475 break;
476 } else if (whitespace) {
477 whitespace = 0;
478 ptrs->start = i;
479 }
480 }
481
482 if (ptrs_length == 0)
483 return -1;
484 return found;
485}
486
487static struct mgcp_endpoint *find_endpoint(const char *mgcp)
488{
489 char *endptr = NULL;
490 unsigned int gw = INT_MAX;
491
492 gw = strtoul(mgcp, &endptr, 16);
493 if (gw == 0 || gw >= number_endpoints || strcmp(endptr, "@mgw") != 0) {
494 DEBUGP(DMGCP, "Not able to find endpoint: '%s'\n", mgcp);
495 return NULL;
496 }
497
498 return &endpoints[gw];
499}
500
501static int analyze_header(struct msgb *msg, struct mgcp_msg_ptr *ptr, int size,
502 const char **transaction_id, struct mgcp_endpoint **endp)
503{
504 int found;
505
506 if (size < 3) {
507 DEBUGP(DMGCP, "Not enough space in ptr\n");
508 return -1;
509 }
510
511 found = find_msg_pointers(msg, ptr, size);
512
513 if (found < 3) {
514 DEBUGP(DMGCP, "Gateway: Not enough params. Found: %d\n", found);
515 return -1;
516 }
517
518 /*
519 * replace the space with \0. the main method gurantess that
520 * we still have + 1 for null termination
521 */
522 msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0';
523 msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0';
524 msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0';
525 msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0';
526
527 if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0
528 || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) {
529 DEBUGP(DMGCP, "Wrong MGCP version. Not handling: '%s' '%s'\n",
530 (const char *)&msg->l3h[ptr[3].start],
531 (const char *)&msg->l3h[ptr[2].start]);
532 return -1;
533 }
534
535 *transaction_id = (const char *)&msg->l3h[ptr[0].start];
536 *endp = find_endpoint((const char *)&msg->l3h[ptr[1].start]);
537 return *endp == NULL;
538}
539
540static int verify_call_id(const struct mgcp_endpoint *endp,
541 const char *callid)
542{
543 if (strcmp(endp->callid, callid) != 0) {
544 DEBUGP(DMGCP, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
545 ENDPOINT_NUMBER(endp), endp->callid, callid);
546 return -1;
547 }
548
549 return 0;
550}
551
552static int verify_ci(const struct mgcp_endpoint *endp,
553 const char *ci)
554{
555 if (atoi(ci) != endp->ci) {
556 DEBUGP(DMGCP, "ConnectionIdentifiers do not match on 0x%x. %d != %s\n",
557 ENDPOINT_NUMBER(endp), endp->ci, ci);
558 return -1;
559 }
560
561 return 0;
562}
563
564static void handle_audit_endpoint(struct msgb *msg, struct sockaddr_in *source)
565{
566 struct mgcp_msg_ptr data_ptrs[6];
567 int found, response;
568 const char *trans_id;
569 struct mgcp_endpoint *endp;
570
571 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
572 if (found != 0)
573 response = 500;
574 else
575 response = 200;
576
577 return send_response(response, "AUEP", trans_id, source);
578}
579
580static int parse_conn_mode(const char* msg, int *conn_mode)
581{
582 int ret = 0;
583 if (strcmp(msg, "recvonly") == 0)
584 *conn_mode = MGCP_CONN_RECV_ONLY;
585 else if (strcmp(msg, "sendrecv") == 0)
586 *conn_mode = MGCP_CONN_RECV_SEND;
587 else {
588 DEBUGP(DMGCP, "Unknown connection mode: '%s'\n", msg);
589 ret = -1;
590 }
591
592 return ret;
593}
594
595static void handle_create_con(struct msgb *msg, struct sockaddr_in *source)
596{
597 struct mgcp_msg_ptr data_ptrs[6];
598 int found, i, line_start;
599 const char *trans_id;
600 struct mgcp_endpoint *endp;
601 int error_code = 500;
602
603 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
604 if (found != 0)
605 return send_response(500, "CRCX", trans_id, source);
606
607 if (endp->ci != CI_UNUSED) {
608 DEBUGP(DMGCP, "Endpoint is already used. 0x%x\n", ENDPOINT_NUMBER(endp));
609 return send_response(500, "CRCX", trans_id, source);
610 }
611
612 /* parse CallID C: and LocalParameters L: */
613 MSG_TOKENIZE_START
614 switch (msg->l3h[line_start]) {
615 case 'L':
616 endp->local_options = talloc_strdup(endpoints,
617 (const char *)&msg->l3h[line_start + 3]);
618 break;
619 case 'C':
620 endp->callid = talloc_strdup(endpoints,
621 (const char *)&msg->l3h[line_start + 3]);
622 break;
623 case 'M':
624 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
625 &endp->conn_mode) != 0) {
626 error_code = 517;
627 goto error2;
628 }
629 break;
630 default:
631 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
632 msg->l3h[line_start], msg->l3h[line_start],
633 ENDPOINT_NUMBER(endp));
634 break;
635 }
636 MSG_TOKENIZE_END
637
638
639 /* bind to the port now */
640 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
641 if (!early_bind && bind_rtp(endp) != 0)
642 goto error2;
643
644 /* assign a local call identifier or fail */
645 endp->ci = generate_call_id();
646 if (endp->ci == CI_UNUSED)
647 goto error2;
648
649 DEBUGP(DMGCP, "Creating endpoint on: 0x%x CI: %u port: %u\n",
650 ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port);
651 return send_with_sdp(endp, "CRCX", trans_id, source);
652error:
653 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
654 hexdump(msg->l3h, msgb_l3len(msg)),
655 ENDPOINT_NUMBER(endp), line_start, i);
656 return send_response(error_code, "CRCX", trans_id, source);
657
658error2:
659 DEBUGP(DMGCP, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
660 return send_response(error_code, "CRCX", trans_id, source);
661}
662
663static void handle_modify_con(struct msgb *msg, struct sockaddr_in *source)
664{
665 struct mgcp_msg_ptr data_ptrs[6];
666 int found, i, line_start;
667 const char *trans_id;
668 struct mgcp_endpoint *endp;
669 int error_code = 500;
670
671 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
672 if (found != 0)
673 return send_response(error_code, "MDCX", trans_id, source);
674
675 if (endp->ci == CI_UNUSED) {
676 DEBUGP(DMGCP, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
677 return send_response(error_code, "MDCX", trans_id, source);
678 }
679
680 MSG_TOKENIZE_START
681 switch (msg->l3h[line_start]) {
682 case 'C': {
683 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
684 goto error3;
685 break;
686 }
687 case 'I': {
688 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
689 goto error3;
690 break;
691 }
692 case 'L':
693 /* skip */
694 break;
695 case 'M':
696 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
697 &endp->conn_mode) != 0) {
698 error_code = 517;
699 goto error3;
700 }
701 break;
702 case '\0':
703 /* SDP file begins */
704 break;
705 case 'a':
706 case 'o':
707 case 's':
708 case 't':
709 case 'v':
710 /* skip these SDP attributes */
711 break;
712 case 'm': {
713 int port;
714 const char *param = (const char *)&msg->l3h[line_start];
715
716 if (sscanf(param, "m=audio %d RTP/AVP %*d", &port) == 1) {
717 endp->rtp = htons(port);
718 endp->rtcp = htons(port + 1);
719 }
720 break;
721 }
722 case 'c': {
723 char ipv4[16];
724 const char *param = (const char *)&msg->l3h[line_start];
725
726 if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) {
727 inet_aton(ipv4, &endp->remote);
728 }
729 break;
730 }
731 default:
732 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
733 msg->l3h[line_start], msg->l3h[line_start],
734 ENDPOINT_NUMBER(endp));
735 break;
736 }
737 MSG_TOKENIZE_END
738
739 /* modify */
740 DEBUGP(DMGCP, "Modified endpoint on: 0x%x Server: %s:%u\n",
741 ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), endp->rtp);
742 return send_with_sdp(endp, "MDCX", trans_id, source);
743
744error:
745 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n",
746 hexdump(msg->l3h, msgb_l3len(msg)),
747 ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]);
748 return send_response(error_code, "MDCX", trans_id, source);
749
750error3:
751 return send_response(error_code, "MDCX", trans_id, source);
752}
753
754static void handle_delete_con(struct msgb *msg, struct sockaddr_in *source)
755{
756 struct mgcp_msg_ptr data_ptrs[6];
757 int found, i, line_start;
758 const char *trans_id;
759 struct mgcp_endpoint *endp;
760 int error_code = 500;
761
762 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
763 if (found != 0)
764 return send_response(error_code, "DLCX", trans_id, source);
765
766 if (endp->ci == CI_UNUSED) {
767 DEBUGP(DMGCP, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
768 return send_response(error_code, "DLCX", trans_id, source);
769 }
770
771 MSG_TOKENIZE_START
772 switch (msg->l3h[line_start]) {
773 case 'C': {
774 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
775 goto error3;
776 break;
777 }
778 case 'I': {
779 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
780 goto error3;
781 break;
782 }
783 default:
784 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
785 msg->l3h[line_start], msg->l3h[line_start],
786 ENDPOINT_NUMBER(endp));
787 break;
788 }
789 MSG_TOKENIZE_END
790
791
792 /* free the connection */
793 DEBUGP(DMGCP, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
794 endp->ci= CI_UNUSED;
795 talloc_free(endp->callid);
796 talloc_free(endp->local_options);
797
798 if (!early_bind) {
799 bsc_unregister_fd(&endp->local_rtp);
800 bsc_unregister_fd(&endp->local_rtcp);
801 }
802
803 return send_response(250, "DLCX", trans_id, source);
804
805error:
806 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
807 hexdump(msg->l3h, msgb_l3len(msg)),
808 ENDPOINT_NUMBER(endp), line_start, i);
809 return send_response(error_code, "DLCX", trans_id, source);
810
811error3:
812 return send_response(error_code, "DLCX", trans_id, source);
813}
814
815static void print_help()
816{
817 printf("Some useful help...\n");
818 printf(" -h --help is printing this text.\n");
819 printf(" -c --config-file filename The config file to use.\n");
820}
821
822static void handle_options(int argc, char** argv)
823{
824 while (1) {
825 int option_index = 0, c;
826 static struct option long_options[] = {
827 {"help", 0, 0, 'h'},
828 {"config-file", 1, 0, 'c'},
829 {0, 0, 0, 0},
830 };
831
832 c = getopt_long(argc, argv, "hc:", long_options, &option_index);
833
834 if (c == -1)
835 break;
836
837 switch(c) {
838 case 'h':
839 print_help();
840 exit(0);
841 break;
842 case 'c':
843 config_file = talloc_strdup(tall_bsc_ctx, optarg);
844 break;
845 default:
846 /* ignore */
847 break;
848 };
849 }
850}
851
852static int read_call_agent(struct bsc_fd *fd, unsigned int what)
853{
854 struct sockaddr_in addr;
855 socklen_t slen = sizeof(addr);
856 struct msgb *msg;
857
858 msg = (struct msgb *) fd->data;
859
860 /* read one less so we can use it as a \0 */
861 int rc = recvfrom(bfd.fd, msg->data, msg->data_len - 1, 0,
862 (struct sockaddr *) &addr, &slen);
863 if (rc < 0) {
864 perror("Gateway failed to read");
865 return -1;
866 } else if (slen > sizeof(addr)) {
867 fprintf(stderr, "Gateway received message from outerspace: %d %d\n",
868 slen, sizeof(addr));
869 return -1;
870 }
871
872 if (first_request) {
873 first_request = 0;
874 send_rsip(&addr);
875 return 0;
876 }
877
878 /* handle message now */
879 msg->l2h = msgb_put(msg, rc);
880 handle_message(msg, &addr);
881 msgb_reset(msg);
882 return 0;
883}
884
885/*
886 * vty code for mgcp below
887 */
888struct cmd_node mgcp_node = {
889 MGCP_NODE,
890 "%s(mgcp)#",
891 1,
892};
893
894static int config_write_mgcp(struct vty *vty)
895{
896 vty_out(vty, "mgcp%s", VTY_NEWLINE);
897 if (local_ip)
898 vty_out(vty, " local ip %s%s", local_ip, VTY_NEWLINE);
899 vty_out(vty, " bts ip %s%s", bts_ip, VTY_NEWLINE);
900 vty_out(vty, " bind ip %s%s", source_addr, VTY_NEWLINE);
901 vty_out(vty, " bind port %u%s", source_port, VTY_NEWLINE);
902 vty_out(vty, " rtp base %u%s", rtp_base_port, VTY_NEWLINE);
903
904 return CMD_SUCCESS;
905}
906
907DEFUN(cfg_mgcp,
908 cfg_mgcp_cmd,
909 "mgcp",
910 "Configure the MGCP")
911{
912 vty->node = MGCP_NODE;
913 return CMD_SUCCESS;
914}
915
916DEFUN(cfg_mgcp_local_ip,
917 cfg_mgcp_local_ip_cmd,
918 "local ip IP",
919 "Set the IP to be used in SDP records")
920{
921 local_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
922 return CMD_SUCCESS;
923}
924
925DEFUN(cfg_mgcp_bts_ip,
926 cfg_mgcp_bts_ip_cmd,
927 "bts ip IP",
928 "Set the IP of the BTS for RTP forwarding")
929{
930 bts_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
931 inet_aton(bts_ip, &bts_in);
932 return CMD_SUCCESS;
933}
934
935DEFUN(cfg_mgcp_bind_ip,
936 cfg_mgcp_bind_ip_cmd,
937 "bind ip IP",
938 "Bind the MGCP to this local addr")
939{
940 source_addr = talloc_strdup(tall_bsc_ctx, argv[0]);
941 return CMD_SUCCESS;
942}
943
944DEFUN(cfg_mgcp_bind_port,
945 cfg_mgcp_bind_port_cmd,
946 "bind port <0-65534>",
947 "Bind the MGCP to this port")
948{
949 unsigned int port = atoi(argv[0]);
950 if (port > 65534) {
951 vty_out(vty, "%% wrong bind port '%s'%s", argv[0], VTY_NEWLINE);
952 return CMD_WARNING;
953 }
954
955 source_port = port;
956 return CMD_SUCCESS;
957}
958
959DEFUN(cfg_mgcp_bind_early,
960 cfg_mgcp_bind_early_cmd,
961 "bind early (0|1)",
962 "Bind all RTP ports early")
963{
964 unsigned int bind = atoi(argv[0]);
965 if (bind != 0 && bind != 1) {
966 vty_out(vty, "%% param must be 0 or 1.%s", VTY_NEWLINE);
967 return CMD_WARNING;
968 }
969
970 early_bind = bind == 1;
971 return CMD_SUCCESS;
972}
973
974DEFUN(cfg_mgcp_rtp_base_port,
975 cfg_mgcp_rtp_base_port_cmd,
976 "rtp base <0-65534>",
977 "Base port to use")
978{
979 unsigned int port = atoi(argv[0]);
980 if (port > 65534) {
981 vty_out(vty, "%% wrong base port '%s'%s", argv[0], VTY_NEWLINE);
982 return CMD_WARNING;
983 }
984
985 rtp_base_port = port;
986 return CMD_SUCCESS;
987}
988
989DEFUN(cfg_mgcp_sdp_payload_number,
990 cfg_mgcp_sdp_payload_number_cmd,
991 "sdp audio payload number <1-255>",
992 "Set the audio codec to use")
993{
994 unsigned int payload = atoi(argv[0]);
995 if (payload > 255) {
996 vty_out(vty, "%% wrong payload number '%s'%s", argv[0], VTY_NEWLINE);
997 return CMD_WARNING;
998 }
999
1000 audio_payload = payload;
1001 return CMD_SUCCESS;
1002}
1003
1004DEFUN(cfg_mgcp_sdp_payload_name,
1005 cfg_mgcp_sdp_payload_name_cmd,
1006 "sdp audio payload name NAME",
1007 "Set the audio name to use")
1008{
1009 audio_name = talloc_strdup(tall_bsc_ctx, argv[0]);
1010 return CMD_SUCCESS;
1011}
1012
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +01001013DEFUN(cfg_mgcp_loop,
1014 cfg_mgcp_loop_cmd,
1015 "loop (0|1)",
1016 "Loop the audio")
1017{
1018 audio_loop = atoi(argv[0]);
1019 return CMD_SUCCESS;
1020}
1021
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001022int bsc_vty_init(struct gsm_network *dummy)
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001023{
1024 cmd_init(1);
1025 vty_init();
1026
1027 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
1028 install_node(&mgcp_node, config_write_mgcp);
1029 install_default(MGCP_NODE);
1030 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
1031 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
1032 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
1033 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
1034 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
1035 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
1036 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
1037 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +01001038 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001039 return 0;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001040}
1041
1042int main(int argc, char** argv)
1043{
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001044 struct gsm_network dummy_network;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001045 struct sockaddr_in addr;
1046 int on = 1, i, rc;
1047
1048 tall_bsc_ctx = talloc_named_const(NULL, 1, "mgcp-callagent");
1049 handle_options(argc, argv);
1050
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001051 telnet_init(&dummy_network, 4243);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001052 rc = vty_read_config_file(config_file);
1053 if (rc < 0) {
1054 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1055 return rc;
1056 }
1057
1058
1059 if (!bts_ip) {
1060 fprintf(stderr, "Need to specify the BTS ip address for RTP handling.\n");
1061 return -1;
1062 }
1063
1064 endpoints = _talloc_zero_array(tall_bsc_ctx,
1065 sizeof(struct mgcp_endpoint),
1066 number_endpoints, "endpoints");
1067 if (!endpoints) {
1068 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", number_endpoints);
1069 return -1;
1070 }
1071
1072 /* Initialize all endpoints */
1073 for (i = 0; i < number_endpoints; ++i) {
1074 endpoints[i].local_rtp.fd = -1;
1075 endpoints[i].local_rtcp.fd = -1;
1076 endpoints[i].ci = CI_UNUSED;
1077 }
1078
1079 /* initialize the socket */
1080 bfd.when = BSC_FD_READ;
1081 bfd.cb = read_call_agent;
1082 bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
1083 if (bfd.fd < 0) {
1084 perror("Gateway failed to listen");
1085 return -1;
1086 }
1087
1088 setsockopt(bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1089
1090 memset(&addr, 0, sizeof(addr));
1091 addr.sin_family = AF_INET;
1092 addr.sin_port = htons(source_port);
1093 inet_aton(source_addr, &addr.sin_addr);
1094
1095 if (bind(bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1096 perror("Gateway failed to bind");
1097 return -1;
1098 }
1099
1100 bfd.data = msgb_alloc(4096, "mgcp-msg");
1101 if (!bfd.data) {
1102 fprintf(stderr, "Gateway memory error.\n");
1103 return -1;
1104 }
1105
1106
1107 if (bsc_register_fd(&bfd) != 0) {
1108 DEBUGP(DMGCP, "Failed to register the fd\n");
1109 return -1;
1110 }
1111
1112 /* initialisation */
1113 srand(time(NULL));
1114
1115 /* early bind */
1116 if (early_bind) {
1117 for (i = 1; i < number_endpoints; ++i) {
1118 struct mgcp_endpoint *endp = &endpoints[i];
1119 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
1120 if (bind_rtp(endp) != 0)
1121 return -1;
1122 }
1123 }
1124
1125 /* main loop */
1126 while (1) {
1127 bsc_select_main(0);
1128 }
1129
1130
1131 return 0;
1132}