blob: 3d130db3ab7dde8bfdd9d33f245038186777a5eb [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
Holger Hans Peter Freyther14083be2010-01-05 12:21:36 +010067static char *forward_ip = NULL;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020068static char *config_file = "mgcp.cfg";
69
70/* used by msgb and mgcp */
71void *tall_bsc_ctx = NULL;
72
73enum mgcp_connection_mode {
74 MGCP_CONN_NONE = 0,
75 MGCP_CONN_RECV_ONLY = 1,
76 MGCP_CONN_SEND_ONLY = 2,
77 MGCP_CONN_RECV_SEND = MGCP_CONN_RECV_ONLY | MGCP_CONN_SEND_ONLY,
78};
79
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +010080enum {
81 DEST_NETWORK = 0,
82 DEST_BTS = 1,
83};
84
85enum {
86 PROTO_RTP,
87 PROTO_RTCP,
88};
89
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020090#define CI_UNUSED 0
91static unsigned int last_call_id = 0;
92
93struct mgcp_endpoint {
94 int ci;
95 char *callid;
96 char *local_options;
97 int conn_mode;
98
99 /* the local rtp port */
100 int rtp_port;
101
102 /*
103 * RTP mangling:
104 * - we get RTP and RTCP to us and need to forward to the BTS
105 * - we get RTP and RTCP from the BTS and forward to the network
106 */
107 struct bsc_fd local_rtp;
108 struct bsc_fd local_rtcp;
109
110 struct in_addr remote;
111
112 /* in network byte order */
113 int rtp, rtcp;
114 int bts_rtp, bts_rtcp;
115};
116
117static struct mgcp_endpoint *endpoints = NULL;
118#define ENDPOINT_NUMBER(endp) abs(endp - endpoints)
119
120/**
121 * Macro for tokenizing MGCP messages and SDP in one go.
122 *
123 */
124#define MSG_TOKENIZE_START \
125 line_start = 0; \
126 for (i = 0; i < msgb_l3len(msg); ++i) { \
127 /* we have a line end */ \
128 if (msg->l3h[i] == '\n') { \
129 /* skip the first line */ \
130 if (line_start == 0) { \
131 line_start = i + 1; \
132 continue; \
133 } \
134 \
135 /* check if we have a proper param */ \
136 if (i - line_start == 1 && msg->l3h[line_start] == '\r') { \
137 } else if (i - line_start > 2 \
138 && islower(msg->l3h[line_start]) \
139 && msg->l3h[line_start + 1] == '=') { \
140 } else if (i - line_start < 3 \
141 || msg->l3h[line_start + 1] != ':' \
142 || msg->l3h[line_start + 2] != ' ') \
143 goto error; \
144 \
145 msg->l3h[i] = '\0'; \
146 if (msg->l3h[i-1] == '\r') \
147 msg->l3h[i-1] = '\0';
148
149#define MSG_TOKENIZE_END \
150 line_start = i + 1; \
151 } \
152 }
153
154
155struct mgcp_msg_ptr {
156 unsigned int start;
157 unsigned int length;
158};
159
160struct mgcp_request {
161 char *name;
162 void (*handle_request) (struct msgb *msg, struct sockaddr_in *source);
163 char *debug_name;
164};
165
166#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
167 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
168
169static void handle_audit_endpoint(struct msgb *msg, struct sockaddr_in *source);
170static void handle_create_con(struct msgb *msg, struct sockaddr_in *source);
171static void handle_delete_con(struct msgb *msg, struct sockaddr_in *source);
172static void handle_modify_con(struct msgb *msg, struct sockaddr_in *source);
173
174static int generate_call_id()
175{
176 int i;
177
178 /* use the call id */
179 ++last_call_id;
180
181 /* handle wrap around */
182 if (last_call_id == CI_UNUSED)
183 ++last_call_id;
184
185 /* callstack can only be of size number_of_endpoints */
186 /* verify that the call id is free, e.g. in case of overrun */
187 for (i = 1; i < number_endpoints; ++i)
188 if (endpoints[i].ci == last_call_id)
189 return generate_call_id();
190
191 return last_call_id;
192}
193
194/* FIXIME/TODO: need to have a list of pending transactions and check that */
195static unsigned int generate_transaction_id()
196{
197 return abs(rand());
198}
199
200static int _send(int fd, struct in_addr *addr, int port, char *buf, int len)
201{
202 struct sockaddr_in out;
203 out.sin_family = AF_INET;
204 out.sin_port = port;
205 memcpy(&out.sin_addr, addr, sizeof(*addr));
206
207 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
208}
209
210/*
211 * There is data coming. We will have to figure out if it
212 * came from the BTS or the MediaGateway of the MSC. On top
213 * of that we need to figure out if it was RTP or RTCP.
214 *
215 * Currently we do not communicate with the BSC so we have
216 * no idea where the BTS is listening for RTP and need to
217 * do the classic routing trick. Wait for the first packet
218 * from the BTS and then go ahead.
219 */
220static int rtp_data_cb(struct bsc_fd *fd, unsigned int what)
221{
222 char buf[4096];
223 struct sockaddr_in addr;
224 socklen_t slen = sizeof(addr);
225 struct mgcp_endpoint *endp;
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100226 int rc, dest, proto;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200227
228 endp = (struct mgcp_endpoint *) fd->data;
229
230 rc = recvfrom(fd->fd, &buf, sizeof(buf), 0,
231 (struct sockaddr *) &addr, &slen);
232 if (rc < 0) {
233 DEBUGP(DMGCP, "Failed to receive message on: 0x%x\n",
234 ENDPOINT_NUMBER(endp));
235 return -1;
236 }
237
Holger Hans Peter Freyther48ecad42009-11-19 16:04:10 +0100238 /* do not forward aynthing... maybe there is a packet from the bts */
239 if (endp->ci == CI_UNUSED)
240 return -1;
241
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200242 /*
243 * Figure out where to forward it to. This code assumes that we
244 * have received the Connection Modify and know who is a legitimate
245 * partner. According to the spec we could attempt to forward even
246 * after the Create Connection but we will not as we are not really
247 * able to tell if this is legitimate.
248 */
249 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100250 dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0
251 ? DEST_BTS : DEST_NETWORK;
252 proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200253
254 /* We have no idea who called us, maybe it is the BTS. */
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100255 if (dest == DEST_NETWORK && endp->bts_rtp == 0) {
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200256 /* it was the BTS... */
257 if (memcmp(&addr.sin_addr, &bts_in, sizeof(bts_in)) == 0) {
258 if (fd == &endp->local_rtp) {
259 endp->bts_rtp = addr.sin_port;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200260 } else {
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200261 endp->bts_rtcp = addr.sin_port;
262 }
263
Holger Hans Peter Freyther8ea4b562009-11-19 16:25:53 +0100264 DEBUGP(DMGCP, "Found BTS for endpoint: 0x%x on port: %d/%d\n",
265 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp));
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200266 }
267 }
268
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100269 /* dispatch */
270 if (audio_loop)
271 dest = !dest;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200272
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100273 if (dest == DEST_NETWORK) {
274 return _send(fd->fd, &endp->remote,
275 proto == PROTO_RTP ? endp->rtp : endp->rtcp,
276 buf, rc);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200277 } else {
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +0100278 return _send(fd->fd, &bts_in,
279 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
280 buf, rc);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200281 }
282}
283
284static int create_bind(struct bsc_fd *fd, int port)
285{
286 struct sockaddr_in addr;
287 int on = 1;
288
289 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
290 if (fd->fd < 0)
291 return -1;
292
293 setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
294 memset(&addr, 0, sizeof(addr));
295 addr.sin_family = AF_INET;
296 addr.sin_port = htons(port);
297 inet_aton(source_addr, &addr.sin_addr);
298
299 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0)
300 return -1;
301
302 return 0;
303}
304
305static int bind_rtp(struct mgcp_endpoint *endp)
306{
307 /* set to zero until we get the info */
308 memset(&endp->remote, 0, sizeof(endp->remote));
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200309
310 if (create_bind(&endp->local_rtp, endp->rtp_port) != 0) {
311 DEBUGP(DMGCP, "Failed to create RTP port: %d on 0x%x\n",
312 endp->rtp_port, ENDPOINT_NUMBER(endp));
313 goto cleanup0;
314 }
315
316 if (create_bind(&endp->local_rtcp, endp->rtp_port + 1) != 0) {
317 DEBUGP(DMGCP, "Failed to create RTCP port: %d on 0x%x\n",
318 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
319 goto cleanup1;
320 }
321
322 endp->local_rtp.cb = rtp_data_cb;
323 endp->local_rtp.data = endp;
324 endp->local_rtp.when = BSC_FD_READ;
325 if (bsc_register_fd(&endp->local_rtp) != 0) {
326 DEBUGP(DMGCP, "Failed to register RTP port %d on 0x%x\n",
327 endp->rtp_port, ENDPOINT_NUMBER(endp));
328 goto cleanup2;
329 }
330
331 endp->local_rtcp.cb = rtp_data_cb;
332 endp->local_rtcp.data = endp;
333 endp->local_rtcp.when = BSC_FD_READ;
334 if (bsc_register_fd(&endp->local_rtcp) != 0) {
335 DEBUGP(DMGCP, "Failed to register RTCP port %d on 0x%x\n",
336 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
337 goto cleanup3;
338 }
339
340 return 0;
341
342cleanup3:
343 bsc_unregister_fd(&endp->local_rtp);
344cleanup2:
345 close(endp->local_rtcp.fd);
346 endp->local_rtcp.fd = -1;
347cleanup1:
348 close(endp->local_rtp.fd);
349 endp->local_rtp.fd = -1;
350cleanup0:
351 return -1;
352}
353
354/*
355 * array of function pointers for handling various
356 * messages. In the future this might be binary sorted
357 * for performance reasons.
358 */
359static const struct mgcp_request mgcp_requests [] = {
360 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
361 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
362 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
363 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
364};
365
366static void send_response_with_data(int code, const char *msg, const char *trans,
367 const char *data, struct sockaddr_in *source)
368{
369 char buf[4096];
370 int len;
371
372 if (data) {
373 len = snprintf(buf, sizeof(buf), "%d %s\n%s", code, trans, data);
374 } else {
375 len = snprintf(buf, sizeof(buf), "%d %s\n", code, trans);
376 }
377 DEBUGP(DMGCP, "Sending response: code: %d for '%s'\n", code, msg);
378
379 sendto(bfd.fd, buf, len, 0, (struct sockaddr *)source, sizeof(*source));
380}
381
382static void send_response(int code, const char *msg, const char *trans, struct sockaddr_in *source)
383{
384 send_response_with_data(code, msg, trans, NULL, source);
385}
386
387static void send_with_sdp(struct mgcp_endpoint *endp, const char *msg, const char *trans_id, struct sockaddr_in *source)
388{
389 const char *addr = local_ip;
390 char sdp_record[4096];
391
392 if (!addr)
393 addr = source_addr;
394
395 snprintf(sdp_record, sizeof(sdp_record) - 1,
396 "I: %d\n\n"
397 "v=0\r\n"
398 "c=IN IP4 %s\r\n"
399 "m=audio %d RTP/AVP %d\r\n"
400 "a=rtpmap:%d %s\r\n",
401 endp->ci, addr, endp->rtp_port,
402 audio_payload, audio_payload, audio_name);
403 return send_response_with_data(200, msg, trans_id, sdp_record, source);
404}
405
406/* send a static record */
407static void send_rsip(struct sockaddr_in *source)
408{
409 char reset[4096];
410 int len, rc;
411
412 len = snprintf(reset, sizeof(reset) - 1,
413 "RSIP %u *@mgw MGCP 1.0\n"
414 "RM: restart\n", generate_transaction_id());
415 rc = sendto(bfd.fd, reset, len, 0, (struct sockaddr *) source, sizeof(*source));
416 if (rc < 0) {
417 DEBUGP(DMGCP, "Failed to send RSIP: %d\n", rc);
418 }
419}
420
421/*
422 * handle incoming messages:
423 * - this can be a command (four letters, space, transaction id)
424 * - or a response (three numbers, space, transaction id)
425 */
426static void handle_message(struct msgb *msg, struct sockaddr_in *source)
427{
428 int code;
429
430 if (msg->len < 4) {
431 DEBUGP(DMGCP, "mgs too short: %d\n", msg->len);
432 return;
433 }
434
435 /* attempt to treat it as a response */
436 if (sscanf((const char *)&msg->data[0], "%3d %*s", &code) == 1) {
437 DEBUGP(DMGCP, "Response: Code: %d\n", code);
438 } else {
439 int i, handled = 0;
440 msg->l3h = &msg->l2h[4];
441 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i)
442 if (strncmp(mgcp_requests[i].name, (const char *) &msg->data[0], 4) == 0) {
443 handled = 1;
444 mgcp_requests[i].handle_request(msg, source);
445 }
446 if (!handled) {
447 DEBUGP(DMGCP, "MSG with type: '%.4s' not handled\n", &msg->data[0]);
448 }
449 }
450}
451
452/* string tokenizer for the poor */
453static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length)
454{
455 int i, found = 0;
456
457 int whitespace = 1;
458 for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) {
459 /* if we have a space we found an end */
460 if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
461 if (!whitespace) {
462 ++found;
463 whitespace = 1;
464 ptrs->length = i - ptrs->start - 1;
465 ++ptrs;
466 --ptrs_length;
467 } else {
468 /* skip any number of whitespace */
469 }
470
471 /* line end... stop */
472 if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n')
473 break;
474 } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
475 /* line end, be done */
476 break;
477 } else if (whitespace) {
478 whitespace = 0;
479 ptrs->start = i;
480 }
481 }
482
483 if (ptrs_length == 0)
484 return -1;
485 return found;
486}
487
488static struct mgcp_endpoint *find_endpoint(const char *mgcp)
489{
490 char *endptr = NULL;
491 unsigned int gw = INT_MAX;
492
493 gw = strtoul(mgcp, &endptr, 16);
494 if (gw == 0 || gw >= number_endpoints || strcmp(endptr, "@mgw") != 0) {
495 DEBUGP(DMGCP, "Not able to find endpoint: '%s'\n", mgcp);
496 return NULL;
497 }
498
499 return &endpoints[gw];
500}
501
502static int analyze_header(struct msgb *msg, struct mgcp_msg_ptr *ptr, int size,
503 const char **transaction_id, struct mgcp_endpoint **endp)
504{
505 int found;
506
507 if (size < 3) {
508 DEBUGP(DMGCP, "Not enough space in ptr\n");
509 return -1;
510 }
511
512 found = find_msg_pointers(msg, ptr, size);
513
514 if (found < 3) {
515 DEBUGP(DMGCP, "Gateway: Not enough params. Found: %d\n", found);
516 return -1;
517 }
518
519 /*
520 * replace the space with \0. the main method gurantess that
521 * we still have + 1 for null termination
522 */
523 msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0';
524 msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0';
525 msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0';
526 msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0';
527
528 if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0
529 || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) {
530 DEBUGP(DMGCP, "Wrong MGCP version. Not handling: '%s' '%s'\n",
531 (const char *)&msg->l3h[ptr[3].start],
532 (const char *)&msg->l3h[ptr[2].start]);
533 return -1;
534 }
535
536 *transaction_id = (const char *)&msg->l3h[ptr[0].start];
537 *endp = find_endpoint((const char *)&msg->l3h[ptr[1].start]);
538 return *endp == NULL;
539}
540
541static int verify_call_id(const struct mgcp_endpoint *endp,
542 const char *callid)
543{
544 if (strcmp(endp->callid, callid) != 0) {
545 DEBUGP(DMGCP, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
546 ENDPOINT_NUMBER(endp), endp->callid, callid);
547 return -1;
548 }
549
550 return 0;
551}
552
553static int verify_ci(const struct mgcp_endpoint *endp,
554 const char *ci)
555{
556 if (atoi(ci) != endp->ci) {
557 DEBUGP(DMGCP, "ConnectionIdentifiers do not match on 0x%x. %d != %s\n",
558 ENDPOINT_NUMBER(endp), endp->ci, ci);
559 return -1;
560 }
561
562 return 0;
563}
564
565static void handle_audit_endpoint(struct msgb *msg, struct sockaddr_in *source)
566{
567 struct mgcp_msg_ptr data_ptrs[6];
568 int found, response;
569 const char *trans_id;
570 struct mgcp_endpoint *endp;
571
572 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
573 if (found != 0)
574 response = 500;
575 else
576 response = 200;
577
578 return send_response(response, "AUEP", trans_id, source);
579}
580
581static int parse_conn_mode(const char* msg, int *conn_mode)
582{
583 int ret = 0;
584 if (strcmp(msg, "recvonly") == 0)
585 *conn_mode = MGCP_CONN_RECV_ONLY;
586 else if (strcmp(msg, "sendrecv") == 0)
587 *conn_mode = MGCP_CONN_RECV_SEND;
588 else {
589 DEBUGP(DMGCP, "Unknown connection mode: '%s'\n", msg);
590 ret = -1;
591 }
592
593 return ret;
594}
595
596static void handle_create_con(struct msgb *msg, struct sockaddr_in *source)
597{
598 struct mgcp_msg_ptr data_ptrs[6];
599 int found, i, line_start;
600 const char *trans_id;
601 struct mgcp_endpoint *endp;
602 int error_code = 500;
603
604 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
605 if (found != 0)
606 return send_response(500, "CRCX", trans_id, source);
607
608 if (endp->ci != CI_UNUSED) {
609 DEBUGP(DMGCP, "Endpoint is already used. 0x%x\n", ENDPOINT_NUMBER(endp));
610 return send_response(500, "CRCX", trans_id, source);
611 }
612
613 /* parse CallID C: and LocalParameters L: */
614 MSG_TOKENIZE_START
615 switch (msg->l3h[line_start]) {
616 case 'L':
617 endp->local_options = talloc_strdup(endpoints,
618 (const char *)&msg->l3h[line_start + 3]);
619 break;
620 case 'C':
621 endp->callid = talloc_strdup(endpoints,
622 (const char *)&msg->l3h[line_start + 3]);
623 break;
624 case 'M':
625 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
626 &endp->conn_mode) != 0) {
627 error_code = 517;
628 goto error2;
629 }
630 break;
631 default:
632 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
633 msg->l3h[line_start], msg->l3h[line_start],
634 ENDPOINT_NUMBER(endp));
635 break;
636 }
637 MSG_TOKENIZE_END
638
Holger Hans Peter Freyther48ecad42009-11-19 16:04:10 +0100639 /* initialize */
640 endp->rtp = endp->rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200641
642 /* bind to the port now */
643 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
644 if (!early_bind && bind_rtp(endp) != 0)
645 goto error2;
646
647 /* assign a local call identifier or fail */
648 endp->ci = generate_call_id();
649 if (endp->ci == CI_UNUSED)
650 goto error2;
651
652 DEBUGP(DMGCP, "Creating endpoint on: 0x%x CI: %u port: %u\n",
653 ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port);
654 return send_with_sdp(endp, "CRCX", trans_id, source);
655error:
656 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
657 hexdump(msg->l3h, msgb_l3len(msg)),
658 ENDPOINT_NUMBER(endp), line_start, i);
659 return send_response(error_code, "CRCX", trans_id, source);
660
661error2:
662 DEBUGP(DMGCP, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
663 return send_response(error_code, "CRCX", trans_id, source);
664}
665
666static void handle_modify_con(struct msgb *msg, struct sockaddr_in *source)
667{
668 struct mgcp_msg_ptr data_ptrs[6];
669 int found, i, line_start;
670 const char *trans_id;
671 struct mgcp_endpoint *endp;
672 int error_code = 500;
673
674 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
675 if (found != 0)
676 return send_response(error_code, "MDCX", trans_id, source);
677
678 if (endp->ci == CI_UNUSED) {
679 DEBUGP(DMGCP, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
680 return send_response(error_code, "MDCX", trans_id, source);
681 }
682
683 MSG_TOKENIZE_START
684 switch (msg->l3h[line_start]) {
685 case 'C': {
686 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
687 goto error3;
688 break;
689 }
690 case 'I': {
691 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
692 goto error3;
693 break;
694 }
695 case 'L':
696 /* skip */
697 break;
698 case 'M':
699 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
700 &endp->conn_mode) != 0) {
701 error_code = 517;
702 goto error3;
703 }
704 break;
705 case '\0':
706 /* SDP file begins */
707 break;
708 case 'a':
709 case 'o':
710 case 's':
711 case 't':
712 case 'v':
713 /* skip these SDP attributes */
714 break;
715 case 'm': {
716 int port;
717 const char *param = (const char *)&msg->l3h[line_start];
718
719 if (sscanf(param, "m=audio %d RTP/AVP %*d", &port) == 1) {
720 endp->rtp = htons(port);
721 endp->rtcp = htons(port + 1);
722 }
723 break;
724 }
725 case 'c': {
726 char ipv4[16];
727 const char *param = (const char *)&msg->l3h[line_start];
728
729 if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) {
730 inet_aton(ipv4, &endp->remote);
731 }
732 break;
733 }
734 default:
735 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
736 msg->l3h[line_start], msg->l3h[line_start],
737 ENDPOINT_NUMBER(endp));
738 break;
739 }
740 MSG_TOKENIZE_END
741
742 /* modify */
743 DEBUGP(DMGCP, "Modified endpoint on: 0x%x Server: %s:%u\n",
744 ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), endp->rtp);
745 return send_with_sdp(endp, "MDCX", trans_id, source);
746
747error:
748 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n",
749 hexdump(msg->l3h, msgb_l3len(msg)),
750 ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]);
751 return send_response(error_code, "MDCX", trans_id, source);
752
753error3:
754 return send_response(error_code, "MDCX", trans_id, source);
755}
756
757static void handle_delete_con(struct msgb *msg, struct sockaddr_in *source)
758{
759 struct mgcp_msg_ptr data_ptrs[6];
760 int found, i, line_start;
761 const char *trans_id;
762 struct mgcp_endpoint *endp;
763 int error_code = 500;
764
765 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
766 if (found != 0)
767 return send_response(error_code, "DLCX", trans_id, source);
768
769 if (endp->ci == CI_UNUSED) {
770 DEBUGP(DMGCP, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
771 return send_response(error_code, "DLCX", trans_id, source);
772 }
773
774 MSG_TOKENIZE_START
775 switch (msg->l3h[line_start]) {
776 case 'C': {
777 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
778 goto error3;
779 break;
780 }
781 case 'I': {
782 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
783 goto error3;
784 break;
785 }
786 default:
787 DEBUGP(DMGCP, "Unhandled option: '%c'/%d on 0x%x\n",
788 msg->l3h[line_start], msg->l3h[line_start],
789 ENDPOINT_NUMBER(endp));
790 break;
791 }
792 MSG_TOKENIZE_END
793
794
795 /* free the connection */
796 DEBUGP(DMGCP, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
797 endp->ci= CI_UNUSED;
798 talloc_free(endp->callid);
799 talloc_free(endp->local_options);
800
801 if (!early_bind) {
802 bsc_unregister_fd(&endp->local_rtp);
803 bsc_unregister_fd(&endp->local_rtcp);
804 }
805
Holger Hans Peter Freyther48ecad42009-11-19 16:04:10 +0100806 endp->rtp = endp->rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
807
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200808 return send_response(250, "DLCX", trans_id, source);
809
810error:
811 DEBUGP(DMGCP, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
812 hexdump(msg->l3h, msgb_l3len(msg)),
813 ENDPOINT_NUMBER(endp), line_start, i);
814 return send_response(error_code, "DLCX", trans_id, source);
815
816error3:
817 return send_response(error_code, "DLCX", trans_id, source);
818}
819
820static void print_help()
821{
822 printf("Some useful help...\n");
823 printf(" -h --help is printing this text.\n");
824 printf(" -c --config-file filename The config file to use.\n");
825}
826
827static void handle_options(int argc, char** argv)
828{
829 while (1) {
830 int option_index = 0, c;
831 static struct option long_options[] = {
832 {"help", 0, 0, 'h'},
833 {"config-file", 1, 0, 'c'},
834 {0, 0, 0, 0},
835 };
836
837 c = getopt_long(argc, argv, "hc:", long_options, &option_index);
838
839 if (c == -1)
840 break;
841
842 switch(c) {
843 case 'h':
844 print_help();
845 exit(0);
846 break;
847 case 'c':
848 config_file = talloc_strdup(tall_bsc_ctx, optarg);
849 break;
850 default:
851 /* ignore */
852 break;
853 };
854 }
855}
856
857static int read_call_agent(struct bsc_fd *fd, unsigned int what)
858{
859 struct sockaddr_in addr;
860 socklen_t slen = sizeof(addr);
861 struct msgb *msg;
862
863 msg = (struct msgb *) fd->data;
864
865 /* read one less so we can use it as a \0 */
866 int rc = recvfrom(bfd.fd, msg->data, msg->data_len - 1, 0,
867 (struct sockaddr *) &addr, &slen);
868 if (rc < 0) {
869 perror("Gateway failed to read");
870 return -1;
871 } else if (slen > sizeof(addr)) {
872 fprintf(stderr, "Gateway received message from outerspace: %d %d\n",
873 slen, sizeof(addr));
874 return -1;
875 }
876
877 if (first_request) {
878 first_request = 0;
879 send_rsip(&addr);
880 return 0;
881 }
882
883 /* handle message now */
884 msg->l2h = msgb_put(msg, rc);
885 handle_message(msg, &addr);
886 msgb_reset(msg);
887 return 0;
888}
889
890/*
891 * vty code for mgcp below
892 */
893struct cmd_node mgcp_node = {
894 MGCP_NODE,
895 "%s(mgcp)#",
896 1,
897};
898
899static int config_write_mgcp(struct vty *vty)
900{
901 vty_out(vty, "mgcp%s", VTY_NEWLINE);
902 if (local_ip)
903 vty_out(vty, " local ip %s%s", local_ip, VTY_NEWLINE);
904 vty_out(vty, " bts ip %s%s", bts_ip, VTY_NEWLINE);
905 vty_out(vty, " bind ip %s%s", source_addr, VTY_NEWLINE);
906 vty_out(vty, " bind port %u%s", source_port, VTY_NEWLINE);
Holger Hans Peter Freyther5fddf472009-11-19 15:08:02 +0100907 vty_out(vty, " bind early %u%s", !!early_bind, VTY_NEWLINE);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +0200908 vty_out(vty, " rtp base %u%s", rtp_base_port, VTY_NEWLINE);
Holger Hans Peter Freyther5fddf472009-11-19 15:08:02 +0100909 vty_out(vty, " sdp audio payload number %u%s", audio_payload, VTY_NEWLINE);
910 vty_out(vty, " sdp audio payload name %s%s", audio_name, VTY_NEWLINE);
911 vty_out(vty, " loop %u%s", !!audio_loop, VTY_NEWLINE);
Holger Hans Peter Freyther4ec389e2009-11-20 11:10:31 +0100912 vty_out(vty, " endpoints %u%s", number_endpoints, 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 Freyther4ec389e2009-11-20 11:10:31 +01001049DEFUN(cfg_mgcp_number_endp,
1050 cfg_mgcp_number_endp_cmd,
1051 "number endpoints <0-65534>",
1052 "The number of endpoints to allocate. This is not dynamic.")
1053{
1054 /* + 1 as we start counting at one */
1055 number_endpoints = atoi(argv[0]) + 1;
1056 return CMD_SUCCESS;
1057}
1058
Holger Hans Peter Freyther14083be2010-01-05 12:21:36 +01001059DEFUN(cfg_mgcp_forward,
1060 cfg_mgcp_forward_cmd,
1061 "forward audio IP",
1062 "Forward packets from and to the IP. This disables most of the MGCP feature.")
1063{
1064 if (forward_ip)
1065 talloc_free(forward_ip);
1066 forward_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
1067 return CMD_SUCCESS;
1068}
1069
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001070int bsc_vty_init(struct gsm_network *dummy)
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001071{
1072 cmd_init(1);
1073 vty_init();
1074
Holger Hans Peter Freyther17e79162009-11-19 15:20:48 +01001075 install_element(VIEW_NODE, &show_mgcp_cmd);
1076
1077
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001078 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
1079 install_node(&mgcp_node, config_write_mgcp);
1080 install_default(MGCP_NODE);
1081 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
1082 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
1083 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
1084 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
1085 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
1086 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
1087 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
1088 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
Holger Hans Peter Freyther138b7ec2009-11-18 18:32:31 +01001089 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
Holger Hans Peter Freyther4ec389e2009-11-20 11:10:31 +01001090 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
Holger Hans Peter Freyther14083be2010-01-05 12:21:36 +01001091 install_element(MGCP_NODE, &cfg_mgcp_forward_cmd);
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001092 return 0;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001093}
1094
1095int main(int argc, char** argv)
1096{
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001097 struct gsm_network dummy_network;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001098 struct sockaddr_in addr;
1099 int on = 1, i, rc;
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +01001100 struct debug_target *stderr_target;
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001101
1102 tall_bsc_ctx = talloc_named_const(NULL, 1, "mgcp-callagent");
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +01001103
1104 debug_init();
1105 stderr_target = debug_target_create_stderr();
1106 debug_add_target(stderr_target);
1107 debug_set_all_filter(stderr_target, 1);
1108
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001109 handle_options(argc, argv);
1110
Holger Hans Peter Freyther338fa562009-11-19 15:03:39 +01001111 telnet_init(&dummy_network, 4243);
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001112 rc = vty_read_config_file(config_file);
1113 if (rc < 0) {
1114 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1115 return rc;
1116 }
1117
1118
1119 if (!bts_ip) {
1120 fprintf(stderr, "Need to specify the BTS ip address for RTP handling.\n");
1121 return -1;
1122 }
1123
1124 endpoints = _talloc_zero_array(tall_bsc_ctx,
1125 sizeof(struct mgcp_endpoint),
1126 number_endpoints, "endpoints");
1127 if (!endpoints) {
1128 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", number_endpoints);
1129 return -1;
1130 }
1131
1132 /* Initialize all endpoints */
1133 for (i = 0; i < number_endpoints; ++i) {
1134 endpoints[i].local_rtp.fd = -1;
1135 endpoints[i].local_rtcp.fd = -1;
1136 endpoints[i].ci = CI_UNUSED;
1137 }
1138
Holger Hans Peter Freyther14083be2010-01-05 12:21:36 +01001139 /*
1140 * This application supports two modes.
1141 * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
1142 * 2.) plain forwarding of RTP packets on the endpoints.
1143 * both modes are mutual exclusive
1144 */
1145 if (forward_ip) {
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001146
Holger Hans Peter Freyther14083be2010-01-05 12:21:36 +01001147 if (!early_bind) {
1148 DEBUGP(DMGCP, "Forwarding requires early bind.\n");
1149 return -1;
1150 }
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001151
Holger Hans Peter Freyther14083be2010-01-05 12:21:36 +01001152 /*
1153 * Store the forward IP and assign a ci. For early bind
1154 * the sockets will be created after this.
1155 */
1156 for (i = 1; i < number_endpoints; ++i) {
1157 struct mgcp_endpoint *endp = &endpoints[i];
1158 inet_aton(forward_ip, &endp->remote);
1159 endp->ci = CI_UNUSED + 23;
1160 }
Holger Hans Peter Freytherf986cfc2010-01-05 12:25:25 +01001161
1162 DEBUGP(DMGCP, "Configured for Audio Forwarding.\n");
Holger Hans Peter Freyther14083be2010-01-05 12:21:36 +01001163 } else {
1164 bfd.when = BSC_FD_READ;
1165 bfd.cb = read_call_agent;
1166 bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
1167 if (bfd.fd < 0) {
1168 perror("Gateway failed to listen");
1169 return -1;
1170 }
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001171
Holger Hans Peter Freyther14083be2010-01-05 12:21:36 +01001172 setsockopt(bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001173
Holger Hans Peter Freyther14083be2010-01-05 12:21:36 +01001174 memset(&addr, 0, sizeof(addr));
1175 addr.sin_family = AF_INET;
1176 addr.sin_port = htons(source_port);
1177 inet_aton(source_addr, &addr.sin_addr);
1178
1179 if (bind(bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1180 perror("Gateway failed to bind");
1181 return -1;
1182 }
1183
1184 bfd.data = msgb_alloc(4096, "mgcp-msg");
1185 if (!bfd.data) {
1186 fprintf(stderr, "Gateway memory error.\n");
1187 return -1;
1188 }
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001189
1190
Holger Hans Peter Freyther14083be2010-01-05 12:21:36 +01001191 if (bsc_register_fd(&bfd) != 0) {
1192 DEBUGP(DMGCP, "Failed to register the fd\n");
1193 return -1;
1194 }
Holger Hans Peter Freytherf986cfc2010-01-05 12:25:25 +01001195
1196 DEBUGP(DMGCP, "Configured for MGCP.\n");
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001197 }
1198
1199 /* initialisation */
1200 srand(time(NULL));
1201
1202 /* early bind */
1203 if (early_bind) {
1204 for (i = 1; i < number_endpoints; ++i) {
1205 struct mgcp_endpoint *endp = &endpoints[i];
1206 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
1207 if (bind_rtp(endp) != 0)
1208 return -1;
1209 }
1210 }
1211
1212 /* main loop */
1213 while (1) {
1214 bsc_select_main(0);
1215 }
1216
1217
1218 return 0;
1219}