blob: fff6d6039525ae4da66782e7bcb3e772be75652a [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;
Holger Hans Peter Freyther4ec389e2009-11-20 11:10:31 +010058static unsigned int number_endpoints = 0;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020059static 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;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200259 } else {
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200260 endp->bts_rtcp = addr.sin_port;
261 }
262
Holger Hans Peter Freyther8ea4b562009-11-19 16:25:53 +0100263 DEBUGP(DMGCP, "Found BTS for endpoint: 0x%x on port: %d/%d\n",
264 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp));
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200265 }
266 }
267
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100268 /* dispatch */
269 if (audio_loop)
270 dest = !dest;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200271
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100272 if (dest == DEST_NETWORK) {
273 return _send(fd->fd, &endp->remote,
274 proto == PROTO_RTP ? endp->rtp : endp->rtcp,
275 buf, rc);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200276 } else {
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100277 return _send(fd->fd, &bts_in,
278 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
279 buf, rc);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200280 }
281}
282
283static int create_bind(struct bsc_fd *fd, int port)
284{
285 struct sockaddr_in addr;
286 int on = 1;
287
288 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
289 if (fd->fd < 0)
290 return -1;
291
292 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
293 memset(&addr, 0, sizeof(addr));
294 addr.sin_family = AF_INET;
295 addr.sin_port = htons(port);
296 inet_aton(source_addr, &addr.sin_addr);
297
298 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
299 return -1;
300
301 return 0;
302}
303
304static int bind_rtp(struct mgcp_endpoint *endp)
305{
306 /* set to zero until we get the info */
307 memset(&endp->remote, 0, sizeof(endp->remote));
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200308
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
Holger Hans Peter Freyther48ecad42009-11-19 16:04:10 +0100638 /* initialize */
639 endp->rtp = endp->rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200640
641 /* bind to the port now */
642 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
643 if (!early_bind && bind_rtp(endp) != 0)
644 goto error2;
645
646 /* assign a local call identifier or fail */
647 endp->ci = generate_call_id();
648 if (endp->ci == CI_UNUSED)
649 goto error2;
650
651 DEBUGP(DMGCP, "Creating endpoint on: 0x%x CI: %u port: %u\n",
652 ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port);
653 return send_with_sdp(endp, "CRCX", trans_id, source);
654error:
655 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
656 hexdump(msg->l3h, msgb_l3len(msg)),
657 ENDPOINT_NUMBER(endp), line_start, i);
658 return send_response(error_code, "CRCX", trans_id, source);
659
660error2:
661 DEBUGP(DMGCP, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
662 return send_response(error_code, "CRCX", trans_id, source);
663}
664
665static void handle_modify_con(struct msgb *msg, struct sockaddr_in *source)
666{
667 struct mgcp_msg_ptr data_ptrs[6];
668 int found, i, line_start;
669 const char *trans_id;
670 struct mgcp_endpoint *endp;
671 int error_code = 500;
672
673 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
674 if (found != 0)
675 return send_response(error_code, "MDCX", trans_id, source);
676
677 if (endp->ci == CI_UNUSED) {
678 DEBUGP(DMGCP, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
679 return send_response(error_code, "MDCX", trans_id, source);
680 }
681
682 MSG_TOKENIZE_START
683 switch (msg->l3h[line_start]) {
684 case 'C': {
685 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
686 goto error3;
687 break;
688 }
689 case 'I': {
690 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
691 goto error3;
692 break;
693 }
694 case 'L':
695 /* skip */
696 break;
697 case 'M':
698 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
699 &endp->conn_mode) != 0) {
700 error_code = 517;
701 goto error3;
702 }
703 break;
704 case '\0':
705 /* SDP file begins */
706 break;
707 case 'a':
708 case 'o':
709 case 's':
710 case 't':
711 case 'v':
712 /* skip these SDP attributes */
713 break;
714 case 'm': {
715 int port;
716 const char *param = (const char *)&msg->l3h[line_start];
717
718 if (sscanf(param, "m=audio %d RTP/AVP %*d", &port) == 1) {
719 endp->rtp = htons(port);
720 endp->rtcp = htons(port + 1);
721 }
722 break;
723 }
724 case 'c': {
725 char ipv4[16];
726 const char *param = (const char *)&msg->l3h[line_start];
727
728 if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) {
729 inet_aton(ipv4, &endp->remote);
730 }
731 break;
732 }
733 default:
734 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
735 msg->l3h[line_start], msg->l3h[line_start],
736 ENDPOINT_NUMBER(endp));
737 break;
738 }
739 MSG_TOKENIZE_END
740
741 /* modify */
742 DEBUGP(DMGCP, "Modified endpoint on: 0x%x Server: %s:%u\n",
743 ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), endp->rtp);
744 return send_with_sdp(endp, "MDCX", trans_id, source);
745
746error:
747 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n",
748 hexdump(msg->l3h, msgb_l3len(msg)),
749 ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]);
750 return send_response(error_code, "MDCX", trans_id, source);
751
752error3:
753 return send_response(error_code, "MDCX", trans_id, source);
754}
755
756static void handle_delete_con(struct msgb *msg, struct sockaddr_in *source)
757{
758 struct mgcp_msg_ptr data_ptrs[6];
759 int found, i, line_start;
760 const char *trans_id;
761 struct mgcp_endpoint *endp;
762 int error_code = 500;
763
764 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
765 if (found != 0)
766 return send_response(error_code, "DLCX", trans_id, source);
767
768 if (endp->ci == CI_UNUSED) {
769 DEBUGP(DMGCP, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
770 return send_response(error_code, "DLCX", trans_id, source);
771 }
772
773 MSG_TOKENIZE_START
774 switch (msg->l3h[line_start]) {
775 case 'C': {
776 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
777 goto error3;
778 break;
779 }
780 case 'I': {
781 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
782 goto error3;
783 break;
784 }
785 default:
786 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
787 msg->l3h[line_start], msg->l3h[line_start],
788 ENDPOINT_NUMBER(endp));
789 break;
790 }
791 MSG_TOKENIZE_END
792
793
794 /* free the connection */
795 DEBUGP(DMGCP, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
796 endp->ci= CI_UNUSED;
797 talloc_free(endp->callid);
798 talloc_free(endp->local_options);
799
800 if (!early_bind) {
801 bsc_unregister_fd(&endp->local_rtp);
802 bsc_unregister_fd(&endp->local_rtcp);
803 }
804
Holger Hans Peter Freyther48ecad42009-11-19 16:04:10 +0100805 endp->rtp = endp->rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
806
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200807 return send_response(250, "DLCX", trans_id, source);
808
809error:
810 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
811 hexdump(msg->l3h, msgb_l3len(msg)),
812 ENDPOINT_NUMBER(endp), line_start, i);
813 return send_response(error_code, "DLCX", trans_id, source);
814
815error3:
816 return send_response(error_code, "DLCX", trans_id, source);
817}
818
819static void print_help()
820{
821 printf("Some useful help...\n");
822 printf(" -h --help is printing this text.\n");
823 printf(" -c --config-file filename The config file to use.\n");
824}
825
826static void handle_options(int argc, char** argv)
827{
828 while (1) {
829 int option_index = 0, c;
830 static struct option long_options[] = {
831 {"help", 0, 0, 'h'},
832 {"config-file", 1, 0, 'c'},
833 {0, 0, 0, 0},
834 };
835
836 c = getopt_long(argc, argv, "hc:", long_options, &option_index);
837
838 if (c == -1)
839 break;
840
841 switch(c) {
842 case 'h':
843 print_help();
844 exit(0);
845 break;
846 case 'c':
847 config_file = talloc_strdup(tall_bsc_ctx, optarg);
848 break;
849 default:
850 /* ignore */
851 break;
852 };
853 }
854}
855
856static int read_call_agent(struct bsc_fd *fd, unsigned int what)
857{
858 struct sockaddr_in addr;
859 socklen_t slen = sizeof(addr);
860 struct msgb *msg;
861
862 msg = (struct msgb *) fd->data;
863
864 /* read one less so we can use it as a \0 */
865 int rc = recvfrom(bfd.fd, msg->data, msg->data_len - 1, 0,
866 (struct sockaddr *) &addr, &slen);
867 if (rc < 0) {
868 perror("Gateway failed to read");
869 return -1;
870 } else if (slen > sizeof(addr)) {
871 fprintf(stderr, "Gateway received message from outerspace: %d %d\n",
872 slen, sizeof(addr));
873 return -1;
874 }
875
876 if (first_request) {
877 first_request = 0;
878 send_rsip(&addr);
879 return 0;
880 }
881
882 /* handle message now */
883 msg->l2h = msgb_put(msg, rc);
884 handle_message(msg, &addr);
885 msgb_reset(msg);
886 return 0;
887}
888
889/*
890 * vty code for mgcp below
891 */
892struct cmd_node mgcp_node = {
893 MGCP_NODE,
894 "%s(mgcp)#",
895 1,
896};
897
898static int config_write_mgcp(struct vty *vty)
899{
900 vty_out(vty, "mgcp%s", VTY_NEWLINE);
901 if (local_ip)
902 vty_out(vty, " local ip %s%s", local_ip, VTY_NEWLINE);
903 vty_out(vty, " bts ip %s%s", bts_ip, VTY_NEWLINE);
904 vty_out(vty, " bind ip %s%s", source_addr, VTY_NEWLINE);
905 vty_out(vty, " bind port %u%s", source_port, VTY_NEWLINE);
Holger Hans Peter Freyther5fddf472009-11-19 15:08:02 +0100906 vty_out(vty, " bind early %u%s", !!early_bind, VTY_NEWLINE);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200907 vty_out(vty, " rtp base %u%s", rtp_base_port, VTY_NEWLINE);
Holger Hans Peter Freyther5fddf472009-11-19 15:08:02 +0100908 vty_out(vty, " sdp audio payload number %u%s", audio_payload, VTY_NEWLINE);
909 vty_out(vty, " sdp audio payload name %s%s", audio_name, VTY_NEWLINE);
910 vty_out(vty, " loop %u%s", !!audio_loop, VTY_NEWLINE);
Holger Hans Peter Freyther4ec389e2009-11-20 11:10:31 +0100911 vty_out(vty, " endpoints %u%s", number_endpoints, VTY_NEWLINE);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200912
913 return CMD_SUCCESS;
914}
915
Holger Hans Peter Freyther17e79162009-11-19 15:20:48 +0100916DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
917 SHOW_STR "Display information about the MGCP Media Gateway")
918{
919 int i;
920
921 vty_out(vty, "MGCP is up and running with %u endpoints:%s", number_endpoints - 1, VTY_NEWLINE);
922 for (i = 1; i < number_endpoints; ++i) {
923 struct mgcp_endpoint *endp = &endpoints[i];
924 vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u%s",
925 i, endp->ci,
926 ntohs(endp->rtp), ntohs(endp->rtcp),
927 ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp), VTY_NEWLINE);
928 }
929
930 return CMD_SUCCESS;
931}
932
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200933DEFUN(cfg_mgcp,
934 cfg_mgcp_cmd,
935 "mgcp",
936 "Configure the MGCP")
937{
938 vty->node = MGCP_NODE;
939 return CMD_SUCCESS;
940}
941
942DEFUN(cfg_mgcp_local_ip,
943 cfg_mgcp_local_ip_cmd,
944 "local ip IP",
945 "Set the IP to be used in SDP records")
946{
947 local_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
948 return CMD_SUCCESS;
949}
950
951DEFUN(cfg_mgcp_bts_ip,
952 cfg_mgcp_bts_ip_cmd,
953 "bts ip IP",
954 "Set the IP of the BTS for RTP forwarding")
955{
956 bts_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
957 inet_aton(bts_ip, &bts_in);
958 return CMD_SUCCESS;
959}
960
961DEFUN(cfg_mgcp_bind_ip,
962 cfg_mgcp_bind_ip_cmd,
963 "bind ip IP",
964 "Bind the MGCP to this local addr")
965{
966 source_addr = talloc_strdup(tall_bsc_ctx, argv[0]);
967 return CMD_SUCCESS;
968}
969
970DEFUN(cfg_mgcp_bind_port,
971 cfg_mgcp_bind_port_cmd,
972 "bind port <0-65534>",
973 "Bind the MGCP to this port")
974{
975 unsigned int port = atoi(argv[0]);
976 if (port > 65534) {
977 vty_out(vty, "%% wrong bind port '%s'%s", argv[0], VTY_NEWLINE);
978 return CMD_WARNING;
979 }
980
981 source_port = port;
982 return CMD_SUCCESS;
983}
984
985DEFUN(cfg_mgcp_bind_early,
986 cfg_mgcp_bind_early_cmd,
987 "bind early (0|1)",
988 "Bind all RTP ports early")
989{
990 unsigned int bind = atoi(argv[0]);
991 if (bind != 0 && bind != 1) {
992 vty_out(vty, "%% param must be 0 or 1.%s", VTY_NEWLINE);
993 return CMD_WARNING;
994 }
995
996 early_bind = bind == 1;
997 return CMD_SUCCESS;
998}
999
1000DEFUN(cfg_mgcp_rtp_base_port,
1001 cfg_mgcp_rtp_base_port_cmd,
1002 "rtp base <0-65534>",
1003 "Base port to use")
1004{
1005 unsigned int port = atoi(argv[0]);
1006 if (port > 65534) {
1007 vty_out(vty, "%% wrong base port '%s'%s", argv[0], VTY_NEWLINE);
1008 return CMD_WARNING;
1009 }
1010
1011 rtp_base_port = port;
1012 return CMD_SUCCESS;
1013}
1014
1015DEFUN(cfg_mgcp_sdp_payload_number,
1016 cfg_mgcp_sdp_payload_number_cmd,
1017 "sdp audio payload number <1-255>",
1018 "Set the audio codec to use")
1019{
1020 unsigned int payload = atoi(argv[0]);
1021 if (payload > 255) {
1022 vty_out(vty, "%% wrong payload number '%s'%s", argv[0], VTY_NEWLINE);
1023 return CMD_WARNING;
1024 }
1025
1026 audio_payload = payload;
1027 return CMD_SUCCESS;
1028}
1029
1030DEFUN(cfg_mgcp_sdp_payload_name,
1031 cfg_mgcp_sdp_payload_name_cmd,
1032 "sdp audio payload name NAME",
1033 "Set the audio name to use")
1034{
1035 audio_name = talloc_strdup(tall_bsc_ctx, argv[0]);
1036 return CMD_SUCCESS;
1037}
1038
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +01001039DEFUN(cfg_mgcp_loop,
1040 cfg_mgcp_loop_cmd,
1041 "loop (0|1)",
1042 "Loop the audio")
1043{
1044 audio_loop = atoi(argv[0]);
1045 return CMD_SUCCESS;
1046}
1047
Holger Hans Peter Freyther4ec389e2009-11-20 11:10:31 +01001048DEFUN(cfg_mgcp_number_endp,
1049 cfg_mgcp_number_endp_cmd,
1050 "number endpoints <0-65534>",
1051 "The number of endpoints to allocate. This is not dynamic.")
1052{
1053 /* + 1 as we start counting at one */
1054 number_endpoints = atoi(argv[0]) + 1;
1055 return CMD_SUCCESS;
1056}
1057
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001058int bsc_vty_init(struct gsm_network *dummy)
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001059{
1060 cmd_init(1);
1061 vty_init();
1062
Holger Hans Peter Freyther17e79162009-11-19 15:20:48 +01001063 install_element(VIEW_NODE, &show_mgcp_cmd);
1064
1065
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001066 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
1067 install_node(&mgcp_node, config_write_mgcp);
1068 install_default(MGCP_NODE);
1069 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
1070 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
1071 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
1072 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
1073 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
1074 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
1075 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
1076 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +01001077 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
Holger Hans Peter Freyther4ec389e2009-11-20 11:10:31 +01001078 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001079 return 0;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001080}
1081
1082int main(int argc, char** argv)
1083{
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001084 struct gsm_network dummy_network;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001085 struct sockaddr_in addr;
1086 int on = 1, i, rc;
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +01001087 struct debug_target *stderr_target;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001088
1089 tall_bsc_ctx = talloc_named_const(NULL, 1, "mgcp-callagent");
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +01001090
1091 debug_init();
1092 stderr_target = debug_target_create_stderr();
1093 debug_add_target(stderr_target);
1094 debug_set_all_filter(stderr_target, 1);
1095
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001096 handle_options(argc, argv);
1097
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001098 telnet_init(&dummy_network, 4243);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001099 rc = vty_read_config_file(config_file);
1100 if (rc < 0) {
1101 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1102 return rc;
1103 }
1104
1105
1106 if (!bts_ip) {
1107 fprintf(stderr, "Need to specify the BTS ip address for RTP handling.\n");
1108 return -1;
1109 }
1110
1111 endpoints = _talloc_zero_array(tall_bsc_ctx,
1112 sizeof(struct mgcp_endpoint),
1113 number_endpoints, "endpoints");
1114 if (!endpoints) {
1115 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", number_endpoints);
1116 return -1;
1117 }
1118
1119 /* Initialize all endpoints */
1120 for (i = 0; i < number_endpoints; ++i) {
1121 endpoints[i].local_rtp.fd = -1;
1122 endpoints[i].local_rtcp.fd = -1;
1123 endpoints[i].ci = CI_UNUSED;
1124 }
1125
1126 /* initialize the socket */
1127 bfd.when = BSC_FD_READ;
1128 bfd.cb = read_call_agent;
1129 bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
1130 if (bfd.fd < 0) {
1131 perror("Gateway failed to listen");
1132 return -1;
1133 }
1134
1135 setsockopt(bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
1136
1137 memset(&addr, 0, sizeof(addr));
1138 addr.sin_family = AF_INET;
1139 addr.sin_port = htons(source_port);
1140 inet_aton(source_addr, &addr.sin_addr);
1141
1142 if (bind(bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1143 perror("Gateway failed to bind");
1144 return -1;
1145 }
1146
1147 bfd.data = msgb_alloc(4096, "mgcp-msg");
1148 if (!bfd.data) {
1149 fprintf(stderr, "Gateway memory error.\n");
1150 return -1;
1151 }
1152
1153
1154 if (bsc_register_fd(&bfd) != 0) {
1155 DEBUGP(DMGCP, "Failed to register the fd\n");
1156 return -1;
1157 }
1158
1159 /* initialisation */
1160 srand(time(NULL));
1161
1162 /* early bind */
1163 if (early_bind) {
1164 for (i = 1; i < number_endpoints; ++i) {
1165 struct mgcp_endpoint *endp = &endpoints[i];
1166 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
1167 if (bind_rtp(endp) != 0)
1168 return -1;
1169 }
1170 }
1171
1172 /* main loop */
1173 while (1) {
1174 bsc_select_main(0);
1175 }
1176
1177
1178 return 0;
1179}