blob: 1549202710dff818b5617529f91707a64023eb6a [file] [log] [blame]
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freyther92026f12010-02-03 18:10:07 +01006 * (C) 2009 by On-Waves
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <ctype.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <time.h>
30#include <limits.h>
31#include <unistd.h>
32
33#include <sys/socket.h>
34#include <arpa/inet.h>
35
36#include <openbsc/debug.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010037#include <osmocore/msgb.h>
38#include <osmocore/talloc.h>
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010039#include <openbsc/gsm_data.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010040#include <osmocore/select.h>
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010041#include <openbsc/mgcp.h>
42#include <openbsc/telnet_interface.h>
43
44#include <vty/command.h>
45#include <vty/vty.h>
46
47#warning "Make use of the rtp proxy code"
48
49static int source_port = 2427;
50static const char *local_ip = NULL;
51static const char *source_addr = "0.0.0.0";
52static unsigned int number_endpoints = 0;
53static const char *bts_ip = NULL;
54static struct in_addr bts_in;
55static const char *audio_name = "GSM-EFR/8000";
56static int audio_payload = 97;
57static int audio_loop = 0;
58static int early_bind = 0;
59
60static char *forward_ip = NULL;
61static int forward_port = 0;
62
63enum mgcp_connection_mode {
64 MGCP_CONN_NONE = 0,
65 MGCP_CONN_RECV_ONLY = 1,
66 MGCP_CONN_SEND_ONLY = 2,
67 MGCP_CONN_RECV_SEND = MGCP_CONN_RECV_ONLY | MGCP_CONN_SEND_ONLY,
68};
69
70enum {
71 DEST_NETWORK = 0,
72 DEST_BTS = 1,
73};
74
75enum {
76 PROTO_RTP,
77 PROTO_RTCP,
78};
79
80#define CI_UNUSED 0
81static unsigned int last_call_id = 0;
82
83struct mgcp_endpoint {
84 int ci;
85 char *callid;
86 char *local_options;
87 int conn_mode;
88
89 /* the local rtp port we are binding to */
90 int rtp_port;
91
92 /*
93 * RTP mangling:
94 * - we get RTP and RTCP to us and need to forward to the BTS
95 * - we get RTP and RTCP from the BTS and forward to the network
96 */
97 struct bsc_fd local_rtp;
98 struct bsc_fd local_rtcp;
99
100 struct in_addr remote;
101 struct in_addr bts;
102
103 /* in network byte order */
104 int net_rtp, net_rtcp;
105 int bts_rtp, bts_rtcp;
106};
107
108static struct mgcp_endpoint *endpoints = NULL;
109#define ENDPOINT_NUMBER(endp) abs(endp - endpoints)
110
111/**
112 * Macro for tokenizing MGCP messages and SDP in one go.
113 *
114 */
115#define MSG_TOKENIZE_START \
116 line_start = 0; \
117 for (i = 0; i < msgb_l3len(msg); ++i) { \
118 /* we have a line end */ \
119 if (msg->l3h[i] == '\n') { \
120 /* skip the first line */ \
121 if (line_start == 0) { \
122 line_start = i + 1; \
123 continue; \
124 } \
125 \
126 /* check if we have a proper param */ \
127 if (i - line_start == 1 && msg->l3h[line_start] == '\r') { \
128 } else if (i - line_start > 2 \
129 && islower(msg->l3h[line_start]) \
130 && msg->l3h[line_start + 1] == '=') { \
131 } else if (i - line_start < 3 \
132 || msg->l3h[line_start + 1] != ':' \
133 || msg->l3h[line_start + 2] != ' ') \
134 goto error; \
135 \
136 msg->l3h[i] = '\0'; \
137 if (msg->l3h[i-1] == '\r') \
138 msg->l3h[i-1] = '\0';
139
140#define MSG_TOKENIZE_END \
141 line_start = i + 1; \
142 } \
143 }
144
145
146struct mgcp_msg_ptr {
147 unsigned int start;
148 unsigned int length;
149};
150
151struct mgcp_request {
152 char *name;
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100153 struct msgb *(*handle_request) (struct msgb *msg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100154 char *debug_name;
155};
156
157#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
158 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
159
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100160static struct msgb *handle_audit_endpoint(struct msgb *msg);
161static struct msgb *handle_create_con(struct msgb *msg);
162static struct msgb *handle_delete_con(struct msgb *msg);
163static struct msgb *handle_modify_con(struct msgb *msg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100164
Holger Hans Peter Freyther77f7afe2010-02-03 09:54:43 +0100165static mgcp_change change_cb;
166static void *change_cb_data;
167
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100168static int generate_call_id()
169{
170 int i;
171
172 /* use the call id */
173 ++last_call_id;
174
175 /* handle wrap around */
176 if (last_call_id == CI_UNUSED)
177 ++last_call_id;
178
179 /* callstack can only be of size number_of_endpoints */
180 /* verify that the call id is free, e.g. in case of overrun */
181 for (i = 1; i < number_endpoints; ++i)
182 if (endpoints[i].ci == last_call_id)
183 return generate_call_id();
184
185 return last_call_id;
186}
187
188/* FIXIME/TODO: need to have a list of pending transactions and check that */
189static unsigned int generate_transaction_id()
190{
191 return abs(rand());
192}
193
194static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
195{
196 struct sockaddr_in out;
197 out.sin_family = AF_INET;
198 out.sin_port = port;
199 memcpy(&out.sin_addr, addr, sizeof(*addr));
200
201 return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
202}
203
204/*
205 * There is data coming. We will have to figure out if it
206 * came from the BTS or the MediaGateway of the MSC. On top
207 * of that we need to figure out if it was RTP or RTCP.
208 *
209 * Currently we do not communicate with the BSC so we have
210 * no idea where the BTS is listening for RTP and need to
211 * do the classic routing trick. Wait for the first packet
212 * from the BTS and then go ahead.
213 */
214static int rtp_data_cb(struct bsc_fd *fd, unsigned int what)
215{
216 char buf[4096];
217 struct sockaddr_in addr;
218 socklen_t slen = sizeof(addr);
219 struct mgcp_endpoint *endp;
220 int rc, dest, proto;
221
222 endp = (struct mgcp_endpoint *) fd->data;
223
224 rc = recvfrom(fd->fd, &buf, sizeof(buf), 0,
225 (struct sockaddr *) &addr, &slen);
226 if (rc < 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100227 LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100228 ENDPOINT_NUMBER(endp));
229 return -1;
230 }
231
232 /* do not forward aynthing... maybe there is a packet from the bts */
233 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100234 LOGP(DMGCP, LOGL_ERROR, "Unknown message on endpoint: 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100235 return -1;
236 }
237
238 /*
239 * Figure out where to forward it to. This code assumes that we
240 * have received the Connection Modify and know who is a legitimate
241 * partner. According to the spec we could attempt to forward even
242 * after the Create Connection but we will not as we are not really
243 * able to tell if this is legitimate.
244 */
245 #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
246 dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 &&
247 (endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port)
248 ? DEST_BTS : DEST_NETWORK;
249 proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
250
251 /* We have no idea who called us, maybe it is the BTS. */
252 if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || forward_ip)) {
253 /* it was the BTS... */
254 if (!bts_ip || memcmp(&addr.sin_addr, &bts_in, sizeof(bts_in)) == 0) {
255 if (fd == &endp->local_rtp) {
256 endp->bts_rtp = addr.sin_port;
257 } else {
258 endp->bts_rtcp = addr.sin_port;
259 }
260
261 endp->bts = addr.sin_addr;
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100262 LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100263 ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp));
264 }
265 }
266
267 /* dispatch */
268 if (audio_loop)
269 dest = !dest;
270
271 if (dest == DEST_NETWORK) {
272 return udp_send(fd->fd, &endp->remote,
273 proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp,
274 buf, rc);
275 } else {
276 return udp_send(fd->fd, &endp->bts,
277 proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
278 buf, rc);
279 }
280}
281
282static int create_bind(struct bsc_fd *fd, int port)
283{
284 struct sockaddr_in addr;
285 int on = 1;
286
287 fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100288 if (fd->fd < 0) {
289 LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100290 return -1;
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100291 }
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100292
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
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100299 if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100300 return -1;
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100301 }
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100302
303 return 0;
304}
305
306static int bind_rtp(struct mgcp_endpoint *endp)
307{
308 if (create_bind(&endp->local_rtp, endp->rtp_port) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100309 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
310 source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100311 goto cleanup0;
312 }
313
314 if (create_bind(&endp->local_rtcp, endp->rtp_port + 1) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100315 LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
316 source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100317 goto cleanup1;
318 }
319
320 endp->local_rtp.cb = rtp_data_cb;
321 endp->local_rtp.data = endp;
322 endp->local_rtp.when = BSC_FD_READ;
323 if (bsc_register_fd(&endp->local_rtp) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100324 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100325 endp->rtp_port, ENDPOINT_NUMBER(endp));
326 goto cleanup2;
327 }
328
329 endp->local_rtcp.cb = rtp_data_cb;
330 endp->local_rtcp.data = endp;
331 endp->local_rtcp.when = BSC_FD_READ;
332 if (bsc_register_fd(&endp->local_rtcp) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100333 LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100334 endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
335 goto cleanup3;
336 }
337
338 return 0;
339
340cleanup3:
341 bsc_unregister_fd(&endp->local_rtp);
342cleanup2:
343 close(endp->local_rtcp.fd);
344 endp->local_rtcp.fd = -1;
345cleanup1:
346 close(endp->local_rtp.fd);
347 endp->local_rtp.fd = -1;
348cleanup0:
349 return -1;
350}
351
352/*
353 * array of function pointers for handling various
354 * messages. In the future this might be binary sorted
355 * for performance reasons.
356 */
357static const struct mgcp_request mgcp_requests [] = {
358 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
359 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
360 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
361 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
362};
363
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100364static struct msgb *mgcp_msgb_alloc(void)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100365{
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100366 struct msgb *msg;
367 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
368 if (!msg)
369 LOGP(DMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
370
371 return msg;
372}
373
374static struct msgb *send_response_with_data(int code, const char *msg, const char *trans,
375 const char *data)
376{
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100377 int len;
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100378 struct msgb *res;
379
380 res = mgcp_msgb_alloc();
381 if (!res)
382 return NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100383
384 if (data) {
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100385 len = snprintf((char *) res->data, 2048, "%d %s\n%s", code, trans, data);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100386 } else {
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100387 len = snprintf((char *) res->data, 2048, "%d %s\n", code, trans);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100388 }
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100389
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100390 res->l2h = msgb_put(res, len);
391 LOGP(DMGCP, LOGL_NOTICE, "Sending response: code: %d for '%s'\n", code, res->l2h);
392 return res;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100393}
394
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100395static struct msgb *send_response(int code, const char *msg, const char *trans)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100396{
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100397 return send_response_with_data(code, msg, trans, NULL);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100398}
399
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100400static struct msgb *send_with_sdp(struct mgcp_endpoint *endp, const char *msg, const char *trans_id)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100401{
402 const char *addr = local_ip;
403 char sdp_record[4096];
404
405 if (!addr)
406 addr = source_addr;
407
408 snprintf(sdp_record, sizeof(sdp_record) - 1,
409 "I: %d\n\n"
410 "v=0\r\n"
411 "c=IN IP4 %s\r\n"
412 "m=audio %d RTP/AVP %d\r\n"
413 "a=rtpmap:%d %s\r\n",
414 endp->ci, addr, endp->rtp_port,
415 audio_payload, audio_payload, audio_name);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100416 return send_response_with_data(200, msg, trans_id, sdp_record);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100417}
418
419/* send a static record */
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100420struct msgb *mgcp_create_rsip(void)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100421{
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100422 struct msgb *msg;
423 int len;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100424
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100425 msg = mgcp_msgb_alloc();
426 if (!msg)
427 return NULL;
428
429 len = snprintf((char *) msg->data, 2048,
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100430 "RSIP %u *@mgw MGCP 1.0\n"
431 "RM: restart\n", generate_transaction_id());
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100432 msg->l2h = msgb_put(msg, len);
433 return msg;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100434}
435
436/*
437 * handle incoming messages:
438 * - this can be a command (four letters, space, transaction id)
439 * - or a response (three numbers, space, transaction id)
440 */
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100441struct msgb *mgcp_handle_message(struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100442{
443 int code;
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100444 struct msgb *resp = NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100445
446 if (msg->len < 4) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100447 LOGP(DMGCP, LOGL_ERROR, "mgs too short: %d\n", msg->len);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100448 return NULL;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100449 }
450
451 /* attempt to treat it as a response */
452 if (sscanf((const char *)&msg->data[0], "%3d %*s", &code) == 1) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100453 LOGP(DMGCP, LOGL_NOTICE, "Response: Code: %d\n", code);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100454 } else {
455 int i, handled = 0;
456 msg->l3h = &msg->l2h[4];
457 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i)
458 if (strncmp(mgcp_requests[i].name, (const char *) &msg->data[0], 4) == 0) {
459 handled = 1;
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100460 resp = mgcp_requests[i].handle_request(msg);
461 break;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100462 }
463 if (!handled) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100464 LOGP(DMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->data[0]);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100465 }
466 }
467
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100468 return resp;
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100469}
470
471/* string tokenizer for the poor */
472static int find_msg_pointers(struct msgb *msg, struct mgcp_msg_ptr *ptrs, int ptrs_length)
473{
474 int i, found = 0;
475
476 int whitespace = 1;
477 for (i = 0; i < msgb_l3len(msg) && ptrs_length > 0; ++i) {
478 /* if we have a space we found an end */
479 if (msg->l3h[i] == ' ' || msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
480 if (!whitespace) {
481 ++found;
482 whitespace = 1;
483 ptrs->length = i - ptrs->start - 1;
484 ++ptrs;
485 --ptrs_length;
486 } else {
487 /* skip any number of whitespace */
488 }
489
490 /* line end... stop */
491 if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n')
492 break;
493 } else if (msg->l3h[i] == '\r' || msg->l3h[i] == '\n') {
494 /* line end, be done */
495 break;
496 } else if (whitespace) {
497 whitespace = 0;
498 ptrs->start = i;
499 }
500 }
501
502 if (ptrs_length == 0)
503 return -1;
504 return found;
505}
506
507static struct mgcp_endpoint *find_endpoint(const char *mgcp)
508{
509 char *endptr = NULL;
510 unsigned int gw = INT_MAX;
511
512 gw = strtoul(mgcp, &endptr, 16);
513 if (gw == 0 || gw >= number_endpoints || strcmp(endptr, "@mgw") != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100514 LOGP(DMGCP, LOGL_ERROR, "Not able to find endpoint: '%s'\n", mgcp);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100515 return NULL;
516 }
517
518 return &endpoints[gw];
519}
520
521static int analyze_header(struct msgb *msg, struct mgcp_msg_ptr *ptr, int size,
522 const char **transaction_id, struct mgcp_endpoint **endp)
523{
524 int found;
525
526 if (size < 3) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100527 LOGP(DMGCP, LOGL_ERROR, "Not enough space in ptr\n");
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100528 return -1;
529 }
530
531 found = find_msg_pointers(msg, ptr, size);
532
533 if (found < 3) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100534 LOGP(DMGCP, LOGL_ERROR, "Gateway: Not enough params. Found: %d\n", found);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100535 return -1;
536 }
537
538 /*
539 * replace the space with \0. the main method gurantess that
540 * we still have + 1 for null termination
541 */
542 msg->l3h[ptr[3].start + ptr[3].length + 1] = '\0';
543 msg->l3h[ptr[2].start + ptr[2].length + 1] = '\0';
544 msg->l3h[ptr[1].start + ptr[1].length + 1] = '\0';
545 msg->l3h[ptr[0].start + ptr[0].length + 1] = '\0';
546
547 if (strncmp("1.0", (const char *)&msg->l3h[ptr[3].start], 3) != 0
548 || strncmp("MGCP", (const char *)&msg->l3h[ptr[2].start], 4) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100549 LOGP(DMGCP, LOGL_ERROR, "Wrong MGCP version. Not handling: '%s' '%s'\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100550 (const char *)&msg->l3h[ptr[3].start],
551 (const char *)&msg->l3h[ptr[2].start]);
552 return -1;
553 }
554
555 *transaction_id = (const char *)&msg->l3h[ptr[0].start];
556 *endp = find_endpoint((const char *)&msg->l3h[ptr[1].start]);
557 return *endp == NULL;
558}
559
560static int verify_call_id(const struct mgcp_endpoint *endp,
561 const char *callid)
562{
563 if (strcmp(endp->callid, callid) != 0) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100564 LOGP(DMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100565 ENDPOINT_NUMBER(endp), endp->callid, callid);
566 return -1;
567 }
568
569 return 0;
570}
571
572static int verify_ci(const struct mgcp_endpoint *endp,
573 const char *ci)
574{
575 if (atoi(ci) != endp->ci) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100576 LOGP(DMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %d != %s\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100577 ENDPOINT_NUMBER(endp), endp->ci, ci);
578 return -1;
579 }
580
581 return 0;
582}
583
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100584static struct msgb *handle_audit_endpoint(struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100585{
586 struct mgcp_msg_ptr data_ptrs[6];
587 int found, response;
588 const char *trans_id;
589 struct mgcp_endpoint *endp;
590
591 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
592 if (found != 0)
593 response = 500;
594 else
595 response = 200;
596
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100597 return send_response(response, "AUEP", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100598}
599
600static int parse_conn_mode(const char* msg, int *conn_mode)
601{
602 int ret = 0;
603 if (strcmp(msg, "recvonly") == 0)
604 *conn_mode = MGCP_CONN_RECV_ONLY;
605 else if (strcmp(msg, "sendrecv") == 0)
606 *conn_mode = MGCP_CONN_RECV_SEND;
607 else {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100608 LOGP(DMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100609 ret = -1;
610 }
611
612 return ret;
613}
614
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100615static struct msgb *handle_create_con(struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100616{
617 struct mgcp_msg_ptr data_ptrs[6];
618 int found, i, line_start;
619 const char *trans_id;
620 struct mgcp_endpoint *endp;
621 int error_code = 500;
622
623 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
624 if (found != 0)
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100625 return send_response(500, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100626
627 if (endp->ci != CI_UNUSED) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100628 LOGP(DMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100629 return send_response(500, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100630 }
631
632 /* parse CallID C: and LocalParameters L: */
633 MSG_TOKENIZE_START
634 switch (msg->l3h[line_start]) {
635 case 'L':
636 endp->local_options = talloc_strdup(endpoints,
637 (const char *)&msg->l3h[line_start + 3]);
638 break;
639 case 'C':
640 endp->callid = talloc_strdup(endpoints,
641 (const char *)&msg->l3h[line_start + 3]);
642 break;
643 case 'M':
644 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
645 &endp->conn_mode) != 0) {
646 error_code = 517;
647 goto error2;
648 }
649 break;
650 default:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100651 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100652 msg->l3h[line_start], msg->l3h[line_start],
653 ENDPOINT_NUMBER(endp));
654 break;
655 }
656 MSG_TOKENIZE_END
657
658 /* initialize */
659 endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
660
661 /* set to zero until we get the info */
662 memset(&endp->remote, 0, sizeof(endp->remote));
663
664 /* bind to the port now */
665 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
666 if (!early_bind && bind_rtp(endp) != 0)
667 goto error2;
668
669 /* assign a local call identifier or fail */
670 endp->ci = generate_call_id();
671 if (endp->ci == CI_UNUSED)
672 goto error2;
673
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100674 LOGP(DMGCP, LOGL_NOTICE, "Creating endpoint on: 0x%x CI: %u port: %u\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100675 ENDPOINT_NUMBER(endp), endp->ci, endp->rtp_port);
Holger Hans Peter Freyther77f7afe2010-02-03 09:54:43 +0100676 if (change_cb)
677 change_cb(ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX, endp->rtp_port, change_cb_data);
678
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100679 return send_with_sdp(endp, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100680error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100681 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100682 hexdump(msg->l3h, msgb_l3len(msg)),
683 ENDPOINT_NUMBER(endp), line_start, i);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100684 return send_response(error_code, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100685
686error2:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100687 LOGP(DMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100688 return send_response(error_code, "CRCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100689}
690
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100691static struct msgb *handle_modify_con(struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100692{
693 struct mgcp_msg_ptr data_ptrs[6];
694 int found, i, line_start;
695 const char *trans_id;
696 struct mgcp_endpoint *endp;
697 int error_code = 500;
698
699 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
700 if (found != 0)
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100701 return send_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100702
703 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100704 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100705 return send_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100706 }
707
708 MSG_TOKENIZE_START
709 switch (msg->l3h[line_start]) {
710 case 'C': {
711 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
712 goto error3;
713 break;
714 }
715 case 'I': {
716 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
717 goto error3;
718 break;
719 }
720 case 'L':
721 /* skip */
722 break;
723 case 'M':
724 if (parse_conn_mode((const char *)&msg->l3h[line_start + 3],
725 &endp->conn_mode) != 0) {
726 error_code = 517;
727 goto error3;
728 }
729 break;
730 case '\0':
731 /* SDP file begins */
732 break;
733 case 'a':
734 case 'o':
735 case 's':
736 case 't':
737 case 'v':
738 /* skip these SDP attributes */
739 break;
740 case 'm': {
741 int port;
742 const char *param = (const char *)&msg->l3h[line_start];
743
744 if (sscanf(param, "m=audio %d RTP/AVP %*d", &port) == 1) {
745 endp->net_rtp = htons(port);
746 endp->net_rtcp = htons(port + 1);
747 }
748 break;
749 }
750 case 'c': {
751 char ipv4[16];
752 const char *param = (const char *)&msg->l3h[line_start];
753
754 if (sscanf(param, "c=IN IP4 %15s", ipv4) == 1) {
755 inet_aton(ipv4, &endp->remote);
756 }
757 break;
758 }
759 default:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100760 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100761 msg->l3h[line_start], msg->l3h[line_start],
762 ENDPOINT_NUMBER(endp));
763 break;
764 }
765 MSG_TOKENIZE_END
766
767 /* modify */
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100768 LOGP(DMGCP, LOGL_NOTICE, "Modified endpoint on: 0x%x Server: %s:%u\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100769 ENDPOINT_NUMBER(endp), inet_ntoa(endp->remote), endp->net_rtp);
Holger Hans Peter Freyther77f7afe2010-02-03 09:54:43 +0100770 if (change_cb)
771 change_cb(ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX, endp->rtp_port, change_cb_data);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100772 return send_with_sdp(endp, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100773
774error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100775 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d %d\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100776 hexdump(msg->l3h, msgb_l3len(msg)),
777 ENDPOINT_NUMBER(endp), line_start, i, msg->l3h[line_start]);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100778 return send_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100779
780error3:
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100781 return send_response(error_code, "MDCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100782}
783
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100784static struct msgb *handle_delete_con(struct msgb *msg)
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100785{
786 struct mgcp_msg_ptr data_ptrs[6];
787 int found, i, line_start;
788 const char *trans_id;
789 struct mgcp_endpoint *endp;
790 int error_code = 500;
791
792 found = analyze_header(msg, data_ptrs, ARRAY_SIZE(data_ptrs), &trans_id, &endp);
793 if (found != 0)
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100794 return send_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100795
796 if (endp->ci == CI_UNUSED) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100797 LOGP(DMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100798 return send_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100799 }
800
801 MSG_TOKENIZE_START
802 switch (msg->l3h[line_start]) {
803 case 'C': {
804 if (verify_call_id(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
805 goto error3;
806 break;
807 }
808 case 'I': {
809 if (verify_ci(endp, (const char *)&msg->l3h[line_start + 3]) != 0)
810 goto error3;
811 break;
812 }
813 default:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100814 LOGP(DMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100815 msg->l3h[line_start], msg->l3h[line_start],
816 ENDPOINT_NUMBER(endp));
817 break;
818 }
819 MSG_TOKENIZE_END
820
821
822 /* free the connection */
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100823 LOGP(DMGCP, LOGL_NOTICE, "Deleting endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100824 endp->ci= CI_UNUSED;
825 talloc_free(endp->callid);
826 talloc_free(endp->local_options);
827
828 if (!early_bind) {
829 bsc_unregister_fd(&endp->local_rtp);
830 bsc_unregister_fd(&endp->local_rtcp);
831 }
832
833 endp->net_rtp = endp->net_rtcp = endp->bts_rtp = endp->bts_rtcp = 0;
Holger Hans Peter Freyther77f7afe2010-02-03 09:54:43 +0100834 if (change_cb)
835 change_cb(ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX, endp->rtp_port, change_cb_data);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100836
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100837 return send_response(250, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100838
839error:
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +0100840 LOGP(DMGCP, LOGL_ERROR, "Malformed line: %s on 0x%x with: line_start: %d %d\n",
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100841 hexdump(msg->l3h, msgb_l3len(msg)),
842 ENDPOINT_NUMBER(endp), line_start, i);
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100843 return send_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100844
845error3:
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100846 return send_response(error_code, "DLCX", trans_id);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +0100847}
848
849/*
850 * vty code for mgcp below
851 */
852struct cmd_node mgcp_node = {
853 MGCP_NODE,
854 "%s(mgcp)#",
855 1,
856};
857
858static int config_write_mgcp(struct vty *vty)
859{
860 vty_out(vty, "mgcp%s", VTY_NEWLINE);
861 if (local_ip)
862 vty_out(vty, " local ip %s%s", local_ip, VTY_NEWLINE);
863 if (bts_ip)
864 vty_out(vty, " bts ip %s%s", bts_ip, VTY_NEWLINE);
865 vty_out(vty, " bind ip %s%s", source_addr, VTY_NEWLINE);
866 vty_out(vty, " bind port %u%s", source_port, VTY_NEWLINE);
867 vty_out(vty, " bind early %u%s", !!early_bind, VTY_NEWLINE);
868 vty_out(vty, " rtp base %u%s", rtp_base_port, VTY_NEWLINE);
869 vty_out(vty, " sdp audio payload number %u%s", audio_payload, VTY_NEWLINE);
870 vty_out(vty, " sdp audio payload name %s%s", audio_name, VTY_NEWLINE);
871 vty_out(vty, " loop %u%s", !!audio_loop, VTY_NEWLINE);
872 vty_out(vty, " endpoints %u%s", number_endpoints, VTY_NEWLINE);
873 if (forward_ip)
874 vty_out(vty, " forward audio ip %s%s", forward_ip, VTY_NEWLINE);
875 if (forward_port != 0)
876 vty_out(vty, " forward audio port %d%s", forward_port, VTY_NEWLINE);
877
878 return CMD_SUCCESS;
879}
880
881DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
882 SHOW_STR "Display information about the MGCP Media Gateway")
883{
884 int i;
885
886 vty_out(vty, "MGCP is up and running with %u endpoints:%s", number_endpoints - 1, VTY_NEWLINE);
887 for (i = 1; i < number_endpoints; ++i) {
888 struct mgcp_endpoint *endp = &endpoints[i];
889 vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u%s",
890 i, endp->ci,
891 ntohs(endp->net_rtp), ntohs(endp->net_rtcp),
892 ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp), VTY_NEWLINE);
893 }
894
895 return CMD_SUCCESS;
896}
897
898DEFUN(cfg_mgcp,
899 cfg_mgcp_cmd,
900 "mgcp",
901 "Configure the MGCP")
902{
903 vty->node = MGCP_NODE;
904 return CMD_SUCCESS;
905}
906
907DEFUN(cfg_mgcp_local_ip,
908 cfg_mgcp_local_ip_cmd,
909 "local ip IP",
910 "Set the IP to be used in SDP records")
911{
912 local_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
913 return CMD_SUCCESS;
914}
915
916DEFUN(cfg_mgcp_bts_ip,
917 cfg_mgcp_bts_ip_cmd,
918 "bts ip IP",
919 "Set the IP of the BTS for RTP forwarding")
920{
921 bts_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
922 inet_aton(bts_ip, &bts_in);
923 return CMD_SUCCESS;
924}
925
926DEFUN(cfg_mgcp_bind_ip,
927 cfg_mgcp_bind_ip_cmd,
928 "bind ip IP",
929 "Bind the MGCP to this local addr")
930{
931 source_addr = talloc_strdup(tall_bsc_ctx, argv[0]);
932 return CMD_SUCCESS;
933}
934
935DEFUN(cfg_mgcp_bind_port,
936 cfg_mgcp_bind_port_cmd,
937 "bind port <0-65534>",
938 "Bind the MGCP to this port")
939{
940 unsigned int port = atoi(argv[0]);
941 if (port > 65534) {
942 vty_out(vty, "%% wrong bind port '%s'%s", argv[0], VTY_NEWLINE);
943 return CMD_WARNING;
944 }
945
946 source_port = port;
947 return CMD_SUCCESS;
948}
949
950DEFUN(cfg_mgcp_bind_early,
951 cfg_mgcp_bind_early_cmd,
952 "bind early (0|1)",
953 "Bind all RTP ports early")
954{
955 unsigned int bind = atoi(argv[0]);
956 if (bind != 0 && bind != 1) {
957 vty_out(vty, "%% param must be 0 or 1.%s", VTY_NEWLINE);
958 return CMD_WARNING;
959 }
960
961 early_bind = bind == 1;
962 return CMD_SUCCESS;
963}
964
965DEFUN(cfg_mgcp_rtp_base_port,
966 cfg_mgcp_rtp_base_port_cmd,
967 "rtp base <0-65534>",
968 "Base port to use")
969{
970 unsigned int port = atoi(argv[0]);
971 if (port > 65534) {
972 vty_out(vty, "%% wrong base port '%s'%s", argv[0], VTY_NEWLINE);
973 return CMD_WARNING;
974 }
975
976 rtp_base_port = port;
977 return CMD_SUCCESS;
978}
979
980DEFUN(cfg_mgcp_sdp_payload_number,
981 cfg_mgcp_sdp_payload_number_cmd,
982 "sdp audio payload number <1-255>",
983 "Set the audio codec to use")
984{
985 unsigned int payload = atoi(argv[0]);
986 if (payload > 255) {
987 vty_out(vty, "%% wrong payload number '%s'%s", argv[0], VTY_NEWLINE);
988 return CMD_WARNING;
989 }
990
991 audio_payload = payload;
992 return CMD_SUCCESS;
993}
994
995DEFUN(cfg_mgcp_sdp_payload_name,
996 cfg_mgcp_sdp_payload_name_cmd,
997 "sdp audio payload name NAME",
998 "Set the audio name to use")
999{
1000 audio_name = talloc_strdup(tall_bsc_ctx, argv[0]);
1001 return CMD_SUCCESS;
1002}
1003
1004DEFUN(cfg_mgcp_loop,
1005 cfg_mgcp_loop_cmd,
1006 "loop (0|1)",
1007 "Loop the audio")
1008{
1009 audio_loop = atoi(argv[0]);
1010 return CMD_SUCCESS;
1011}
1012
1013DEFUN(cfg_mgcp_number_endp,
1014 cfg_mgcp_number_endp_cmd,
1015 "number endpoints <0-65534>",
1016 "The number of endpoints to allocate. This is not dynamic.")
1017{
1018 /* + 1 as we start counting at one */
1019 number_endpoints = atoi(argv[0]) + 1;
1020 return CMD_SUCCESS;
1021}
1022
1023DEFUN(cfg_mgcp_forward_ip,
1024 cfg_mgcp_forward_ip_cmd,
1025 "forward audio ip IP",
1026 "Forward packets from and to the IP. This disables most of the MGCP feature.")
1027{
1028 if (forward_ip)
1029 talloc_free(forward_ip);
1030 forward_ip = talloc_strdup(tall_bsc_ctx, argv[0]);
1031 return CMD_SUCCESS;
1032}
1033
1034DEFUN(cfg_mgcp_forward_port,
1035 cfg_mgcp_forward_port_cmd,
1036 "forward audio port <1-15000>",
1037 "Forward packets from and to the port. This disables most of the MGCP feature.")
1038{
1039 forward_port = atoi(argv[0]);
1040 return CMD_SUCCESS;
1041}
1042
1043int mgcp_vty_init(void)
1044{
1045 install_element(VIEW_NODE, &show_mgcp_cmd);
1046
1047 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
1048 install_node(&mgcp_node, config_write_mgcp);
1049 install_default(MGCP_NODE);
1050 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
1051 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
1052 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
1053 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
1054 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
1055 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
1056 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
1057 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
1058 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
1059 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
1060 install_element(MGCP_NODE, &cfg_mgcp_forward_ip_cmd);
1061 install_element(MGCP_NODE, &cfg_mgcp_forward_port_cmd);
1062 return 0;
1063}
1064
1065int mgcp_parse_config(const char *config_file, struct gsm_network *dummy_network)
1066{
1067 int i, rc;
1068
1069 rc = vty_read_config_file(config_file);
1070 if (rc < 0) {
1071 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
1072 return rc;
1073 }
1074
1075
1076 if (!bts_ip)
1077 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
1078
1079 endpoints = _talloc_zero_array(tall_bsc_ctx,
1080 sizeof(struct mgcp_endpoint),
1081 number_endpoints, "endpoints");
1082 if (!endpoints) {
1083 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", number_endpoints);
1084 return -1;
1085 }
1086
1087 /* Initialize all endpoints */
1088 for (i = 0; i < number_endpoints; ++i) {
1089 endpoints[i].local_rtp.fd = -1;
1090 endpoints[i].local_rtcp.fd = -1;
1091 endpoints[i].ci = CI_UNUSED;
1092 }
1093
1094 /*
1095 * This application supports two modes.
1096 * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
1097 * 2.) plain forwarding of RTP packets on the endpoints.
1098 * both modes are mutual exclusive
1099 */
1100 if (forward_ip) {
1101 int port = rtp_base_port;
1102 if (forward_port != 0)
1103 port = forward_port;
1104
1105 if (!early_bind) {
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +01001106 LOGP(DMGCP, LOGL_NOTICE, "Forwarding requires early bind.\n");
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01001107 return -1;
1108 }
1109
1110 /*
1111 * Store the forward IP and assign a ci. For early bind
1112 * the sockets will be created after this.
1113 */
1114 for (i = 1; i < number_endpoints; ++i) {
1115 struct mgcp_endpoint *endp = &endpoints[i];
1116 inet_aton(forward_ip, &endp->remote);
1117 endp->ci = CI_UNUSED + 23;
1118 endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port));
1119 endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1);
1120 }
1121
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +01001122 LOGP(DMGCP, LOGL_NOTICE, "Configured for Audio Forwarding.\n");
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01001123 }
1124
1125 /* early bind */
1126 if (early_bind) {
1127 for (i = 1; i < number_endpoints; ++i) {
1128 struct mgcp_endpoint *endp = &endpoints[i];
1129 endp->rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), rtp_base_port);
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +01001130 if (bind_rtp(endp) != 0) {
1131 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", endp->rtp_port);
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01001132 return -1;
Holger Hans Peter Freyther2ada71d2010-02-03 09:13:10 +01001133 }
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +01001134 }
1135 }
1136
1137 return !!forward_ip;
1138}
Holger Hans Peter Freyther77f7afe2010-02-03 09:54:43 +01001139
1140void mgcp_set_change_cb(mgcp_change cb, void *data)
1141{
1142 change_cb = cb;
1143 change_cb_data = data;
1144}