Merge remote branch 'origin/on-waves/mgcp'
This commit is contained in:
commit
1d7f2f80b8
|
@ -5,7 +5,7 @@ noinst_HEADERS = abis_nm.h abis_rsl.h db.h gsm_04_08.h gsm_data.h \
|
|||
ipaccess.h rs232.h openbscdefines.h rtp_proxy.h \
|
||||
bsc_rll.h mncc.h transaction.h ussd.h gsm_04_80.h \
|
||||
silent_call.h mgcp.h meas_rep.h rest_octets.h \
|
||||
system_information.h handover.h
|
||||
system_information.h handover.h mgcp_internal.h
|
||||
|
||||
openbsc_HEADERS = gsm_04_08.h meas_rep.h
|
||||
openbscdir = $(includedir)/openbsc
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
|
||||
|
||||
/*
|
||||
* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
|
||||
* (C) 2009 by On-Waves
|
||||
* (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
|
||||
* (C) 2009-2010 by On-Waves
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
|
@ -26,8 +26,9 @@
|
|||
|
||||
#include <osmocore/msgb.h>
|
||||
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define RTP_PORT_DEFAULT 4000
|
||||
extern unsigned int rtp_base_port;
|
||||
|
||||
/**
|
||||
* Calculate the RTP audio port for the given multiplex
|
||||
|
@ -53,18 +54,69 @@ static inline int rtp_calculate_port(int multiplex, int base)
|
|||
return base + (multiplex * 2);
|
||||
}
|
||||
|
||||
int mgcp_parse_config(const char *config_file, struct gsm_network *dummy_network);
|
||||
|
||||
struct msgb *mgcp_handle_message(struct msgb *msg);
|
||||
struct msgb *mgcp_create_rsip(void);
|
||||
int mgcp_vty_init(void);
|
||||
/*
|
||||
* Handling of MGCP Endpoints and the MGCP Config
|
||||
*/
|
||||
struct mgcp_endpoint;
|
||||
struct mgcp_config;
|
||||
|
||||
/* endpoint managed */
|
||||
#define MGCP_ENDP_CRCX 1
|
||||
#define MGCP_ENDP_DLCX 2
|
||||
#define MGCP_ENDP_MDCX 3
|
||||
|
||||
typedef int (*mgcp_change)(int endpoint, int state, int local_rtp, void *);
|
||||
void mgcp_set_change_cb(mgcp_change cb, void *data);
|
||||
/*
|
||||
* what to do with the msg?
|
||||
* - continue as usual?
|
||||
* - reject and send a failure code?
|
||||
* - defer? do not send anything
|
||||
*/
|
||||
#define MGCP_POLICY_CONT 4
|
||||
#define MGCP_POLICY_REJECT 5
|
||||
#define MGCP_POLICY_DEFER 6
|
||||
|
||||
typedef int (*mgcp_change)(struct mgcp_config *cfg, int endpoint, int state, int local_rtp);
|
||||
typedef int (*mgcp_policy)(struct mgcp_config *cfg, int endpoint, int state, const char *transactio_id);
|
||||
|
||||
struct mgcp_config {
|
||||
int source_port;
|
||||
char *local_ip;
|
||||
char *source_addr;
|
||||
unsigned int number_endpoints;
|
||||
char *bts_ip;
|
||||
|
||||
struct in_addr bts_in;
|
||||
char *audio_name;
|
||||
int audio_payload;
|
||||
int audio_loop;
|
||||
int early_bind;
|
||||
int rtp_base_port;
|
||||
|
||||
char *forward_ip;
|
||||
int forward_port;
|
||||
|
||||
mgcp_change change_cb;
|
||||
mgcp_policy policy_cb;
|
||||
void *data;
|
||||
|
||||
struct mgcp_endpoint *endpoints;
|
||||
unsigned int last_call_id;
|
||||
};
|
||||
|
||||
/* config management */
|
||||
struct mgcp_config *mgcp_config_alloc(void);
|
||||
int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg);
|
||||
int mgcp_vty_init(void);
|
||||
int mgcp_endpoints_allocate(struct mgcp_config *cfg);
|
||||
int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port);
|
||||
void mgcp_free_endp(struct mgcp_endpoint *endp);
|
||||
|
||||
/*
|
||||
* format helper functions
|
||||
*/
|
||||
struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg);
|
||||
struct msgb *mgcp_create_rsip(void);
|
||||
struct msgb *mgcp_create_response_with_data(int code, const char *msg, const char *trans, const char *data);
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
/* MGCP Private Data */
|
||||
|
||||
/*
|
||||
* (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
|
||||
* (C) 2009-2010 by On-Waves
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef OPENBSC_MGCP_DATA_H
|
||||
#define OPENBSC_MGCP_DATA_H
|
||||
|
||||
#include <osmocore/select.h>
|
||||
|
||||
#define CI_UNUSED 0
|
||||
|
||||
struct mgcp_endpoint {
|
||||
int ci;
|
||||
char *callid;
|
||||
char *local_options;
|
||||
int conn_mode;
|
||||
|
||||
int bts_payload_type;
|
||||
int net_payload_type;
|
||||
|
||||
/* the local rtp port we are binding to */
|
||||
int rtp_port;
|
||||
|
||||
/*
|
||||
* RTP mangling:
|
||||
* - we get RTP and RTCP to us and need to forward to the BTS
|
||||
* - we get RTP and RTCP from the BTS and forward to the network
|
||||
*/
|
||||
struct bsc_fd local_rtp;
|
||||
struct bsc_fd local_rtcp;
|
||||
|
||||
struct in_addr remote;
|
||||
struct in_addr bts;
|
||||
|
||||
/* in network byte order */
|
||||
int net_rtp, net_rtcp;
|
||||
int bts_rtp, bts_rtcp;
|
||||
|
||||
/* backpointer */
|
||||
struct mgcp_config *cfg;
|
||||
};
|
||||
|
||||
#define ENDPOINT_NUMBER(endp) abs(endp - endp->cfg->endpoints)
|
||||
|
||||
#endif
|
|
@ -41,7 +41,8 @@ ipaccess_config_LDADD = libbsc.a libmsc.a libbsc.a libvty.a -ldl -ldbi $(LIBCRYP
|
|||
|
||||
isdnsync_SOURCES = isdnsync.c
|
||||
|
||||
bsc_mgcp_SOURCES = mgcp/mgcp_main.c mgcp/mgcp_protocol.c debug.c telnet_interface.c
|
||||
bsc_mgcp_SOURCES = mgcp/mgcp_main.c mgcp/mgcp_protocol.c mgcp/mgcp_network.c mgcp/mgcp_vty.c \
|
||||
debug.c telnet_interface.c
|
||||
bsc_mgcp_LDADD = libvty.a
|
||||
|
||||
ipaccess_proxy_SOURCES = ipaccess/ipaccess-proxy.c debug.c
|
||||
|
|
|
@ -31,7 +31,6 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <openbsc/debug.h>
|
||||
#include <osmocore/msgb.h>
|
||||
|
@ -41,9 +40,6 @@
|
|||
#include <openbsc/mgcp.h>
|
||||
#include <openbsc/telnet_interface.h>
|
||||
|
||||
#include <vty/command.h>
|
||||
#include <vty/vty.h>
|
||||
|
||||
/* this is here for the vty... it will never be called */
|
||||
void subscr_put() { abort(); }
|
||||
|
||||
|
@ -52,18 +48,15 @@ void subscr_put() { abort(); }
|
|||
|
||||
#warning "Make use of the rtp proxy code"
|
||||
|
||||
static int source_port = 2427;
|
||||
static const char *source_addr = "0.0.0.0";
|
||||
static struct bsc_fd bfd;
|
||||
static int first_request = 1;
|
||||
static struct mgcp_config *cfg;
|
||||
|
||||
static char *config_file = "mgcp.cfg";
|
||||
|
||||
/* used by msgb and mgcp */
|
||||
void *tall_bsc_ctx = NULL;
|
||||
|
||||
unsigned int rtp_base_port = RTP_PORT_DEFAULT;
|
||||
|
||||
static void print_help()
|
||||
{
|
||||
printf("Some useful help...\n");
|
||||
|
@ -136,7 +129,7 @@ static int read_call_agent(struct bsc_fd *fd, unsigned int what)
|
|||
|
||||
/* handle message now */
|
||||
msg->l2h = msgb_put(msg, rc);
|
||||
resp = mgcp_handle_message(msg);
|
||||
resp = mgcp_handle_message(cfg, msg);
|
||||
msgb_reset(msg);
|
||||
|
||||
if (resp) {
|
||||
|
@ -147,15 +140,6 @@ static int read_call_agent(struct bsc_fd *fd, unsigned int what)
|
|||
}
|
||||
|
||||
|
||||
int bsc_vty_init(struct gsm_network *dummy)
|
||||
{
|
||||
cmd_init(1);
|
||||
vty_init();
|
||||
|
||||
mgcp_vty_init();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
struct gsm_network dummy_network;
|
||||
|
@ -170,10 +154,14 @@ int main(int argc, char** argv)
|
|||
debug_add_target(stderr_target);
|
||||
debug_set_all_filter(stderr_target, 1);
|
||||
|
||||
cfg = mgcp_config_alloc();
|
||||
if (!cfg)
|
||||
return -1;
|
||||
|
||||
handle_options(argc, argv);
|
||||
|
||||
telnet_init(&dummy_network, 4243);
|
||||
rc = mgcp_parse_config(config_file, &dummy_network);
|
||||
rc = mgcp_parse_config(config_file, cfg);
|
||||
if (rc < 0)
|
||||
return rc;
|
||||
|
||||
|
@ -192,8 +180,8 @@ int main(int argc, char** argv)
|
|||
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(source_port);
|
||||
inet_aton(source_addr, &addr.sin_addr);
|
||||
addr.sin_port = htons(cfg->source_port);
|
||||
inet_aton(cfg->source_addr, &addr.sin_addr);
|
||||
|
||||
if (bind(bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
||||
perror("Gateway failed to bind");
|
||||
|
|
|
@ -0,0 +1,255 @@
|
|||
/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
|
||||
/* The protocol implementation */
|
||||
|
||||
/*
|
||||
* (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
|
||||
* (C) 2009-2010 by On-Waves
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <endian.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <osmocore/msgb.h>
|
||||
#include <osmocore/talloc.h>
|
||||
#include <osmocore/select.h>
|
||||
|
||||
#include <openbsc/debug.h>
|
||||
#include <openbsc/mgcp.h>
|
||||
#include <openbsc/mgcp_internal.h>
|
||||
|
||||
#warning "Make use of the rtp proxy code"
|
||||
|
||||
/* according to rtp_proxy.c RFC 3550 */
|
||||
struct rtp_hdr {
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
u_int8_t csrc_count:4,
|
||||
extension:1,
|
||||
padding:1,
|
||||
version:2;
|
||||
u_int8_t payload_type:7,
|
||||
marker:1;
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
u_int8_t version:2,
|
||||
padding:1,
|
||||
extension:1,
|
||||
csrc_count:4;
|
||||
u_int8_t marker:1,
|
||||
payload_type:7;
|
||||
#endif
|
||||
u_int16_t sequence;
|
||||
u_int32_t timestamp;
|
||||
u_int32_t ssrc;
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
enum {
|
||||
DEST_NETWORK = 0,
|
||||
DEST_BTS = 1,
|
||||
};
|
||||
|
||||
enum {
|
||||
PROTO_RTP,
|
||||
PROTO_RTCP,
|
||||
};
|
||||
|
||||
|
||||
static int udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
|
||||
{
|
||||
struct sockaddr_in out;
|
||||
out.sin_family = AF_INET;
|
||||
out.sin_port = port;
|
||||
memcpy(&out.sin_addr, addr, sizeof(*addr));
|
||||
|
||||
return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
|
||||
}
|
||||
|
||||
static void patch_payload(int payload, char *data, int len)
|
||||
{
|
||||
struct rtp_hdr *rtp_hdr;
|
||||
|
||||
if (len < sizeof(*rtp_hdr))
|
||||
return;
|
||||
|
||||
rtp_hdr = (struct rtp_hdr *) data;
|
||||
rtp_hdr->payload_type = payload;
|
||||
}
|
||||
|
||||
/*
|
||||
* There is data coming. We will have to figure out if it
|
||||
* came from the BTS or the MediaGateway of the MSC. On top
|
||||
* of that we need to figure out if it was RTP or RTCP.
|
||||
*
|
||||
* Currently we do not communicate with the BSC so we have
|
||||
* no idea where the BTS is listening for RTP and need to
|
||||
* do the classic routing trick. Wait for the first packet
|
||||
* from the BTS and then go ahead.
|
||||
*/
|
||||
static int rtp_data_cb(struct bsc_fd *fd, unsigned int what)
|
||||
{
|
||||
char buf[4096];
|
||||
struct sockaddr_in addr;
|
||||
socklen_t slen = sizeof(addr);
|
||||
struct mgcp_endpoint *endp;
|
||||
struct mgcp_config *cfg;
|
||||
int rc, dest, proto;
|
||||
|
||||
endp = (struct mgcp_endpoint *) fd->data;
|
||||
cfg = endp->cfg;
|
||||
|
||||
rc = recvfrom(fd->fd, &buf, sizeof(buf), 0,
|
||||
(struct sockaddr *) &addr, &slen);
|
||||
if (rc < 0) {
|
||||
LOGP(DMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x\n",
|
||||
ENDPOINT_NUMBER(endp));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* do not forward aynthing... maybe there is a packet from the bts */
|
||||
if (endp->ci == CI_UNUSED) {
|
||||
LOGP(DMGCP, LOGL_ERROR, "Unknown message on endpoint: 0x%x\n", ENDPOINT_NUMBER(endp));
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Figure out where to forward it to. This code assumes that we
|
||||
* have received the Connection Modify and know who is a legitimate
|
||||
* partner. According to the spec we could attempt to forward even
|
||||
* after the Create Connection but we will not as we are not really
|
||||
* able to tell if this is legitimate.
|
||||
*/
|
||||
#warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
|
||||
dest = memcmp(&addr.sin_addr, &endp->remote, sizeof(addr.sin_addr)) == 0 &&
|
||||
(endp->net_rtp == addr.sin_port || endp->net_rtcp == addr.sin_port)
|
||||
? DEST_BTS : DEST_NETWORK;
|
||||
proto = fd == &endp->local_rtp ? PROTO_RTP : PROTO_RTCP;
|
||||
|
||||
/* We have no idea who called us, maybe it is the BTS. */
|
||||
if (dest == DEST_NETWORK && (endp->bts_rtp == 0 || cfg->forward_ip)) {
|
||||
/* it was the BTS... */
|
||||
if (!cfg->bts_ip || memcmp(&addr.sin_addr, &cfg->bts_in, sizeof(cfg->bts_in)) == 0) {
|
||||
if (fd == &endp->local_rtp) {
|
||||
endp->bts_rtp = addr.sin_port;
|
||||
} else {
|
||||
endp->bts_rtcp = addr.sin_port;
|
||||
}
|
||||
|
||||
endp->bts = addr.sin_addr;
|
||||
LOGP(DMGCP, LOGL_NOTICE, "Found BTS for endpoint: 0x%x on port: %d/%d\n",
|
||||
ENDPOINT_NUMBER(endp), ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp));
|
||||
}
|
||||
}
|
||||
|
||||
/* dispatch */
|
||||
if (cfg->audio_loop)
|
||||
dest = !dest;
|
||||
|
||||
if (dest == DEST_NETWORK) {
|
||||
patch_payload(endp->net_payload_type, buf, rc);
|
||||
return udp_send(fd->fd, &endp->remote,
|
||||
proto == PROTO_RTP ? endp->net_rtp : endp->net_rtcp,
|
||||
buf, rc);
|
||||
} else {
|
||||
patch_payload(endp->bts_payload_type, buf, rc);
|
||||
return udp_send(fd->fd, &endp->bts,
|
||||
proto == PROTO_RTP ? endp->bts_rtp : endp->bts_rtcp,
|
||||
buf, rc);
|
||||
}
|
||||
}
|
||||
|
||||
static int create_bind(const char *source_addr, struct bsc_fd *fd, int port)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
int on = 1;
|
||||
|
||||
fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd->fd < 0) {
|
||||
LOGP(DMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
||||
memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
inet_aton(source_addr, &addr.sin_addr);
|
||||
|
||||
if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bind_rtp(struct mgcp_endpoint *endp)
|
||||
{
|
||||
struct mgcp_config *cfg = endp->cfg;
|
||||
|
||||
if (create_bind(cfg->source_addr, &endp->local_rtp, endp->rtp_port) != 0) {
|
||||
LOGP(DMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
|
||||
cfg->source_addr, endp->rtp_port, ENDPOINT_NUMBER(endp));
|
||||
goto cleanup0;
|
||||
}
|
||||
|
||||
if (create_bind(cfg->source_addr, &endp->local_rtcp, endp->rtp_port + 1) != 0) {
|
||||
LOGP(DMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
|
||||
cfg->source_addr, endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
|
||||
goto cleanup1;
|
||||
}
|
||||
|
||||
endp->local_rtp.cb = rtp_data_cb;
|
||||
endp->local_rtp.data = endp;
|
||||
endp->local_rtp.when = BSC_FD_READ;
|
||||
if (bsc_register_fd(&endp->local_rtp) != 0) {
|
||||
LOGP(DMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
|
||||
endp->rtp_port, ENDPOINT_NUMBER(endp));
|
||||
goto cleanup2;
|
||||
}
|
||||
|
||||
endp->local_rtcp.cb = rtp_data_cb;
|
||||
endp->local_rtcp.data = endp;
|
||||
endp->local_rtcp.when = BSC_FD_READ;
|
||||
if (bsc_register_fd(&endp->local_rtcp) != 0) {
|
||||
LOGP(DMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
|
||||
endp->rtp_port + 1, ENDPOINT_NUMBER(endp));
|
||||
goto cleanup3;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
cleanup3:
|
||||
bsc_unregister_fd(&endp->local_rtp);
|
||||
cleanup2:
|
||||
close(endp->local_rtcp.fd);
|
||||
endp->local_rtcp.fd = -1;
|
||||
cleanup1:
|
||||
close(endp->local_rtp.fd);
|
||||
endp->local_rtp.fd = -1;
|
||||
cleanup0:
|
||||
return -1;
|
||||
}
|
||||
|
||||
int mgcp_bind_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
|
||||
{
|
||||
endp->rtp_port = rtp_port;
|
||||
return bind_rtp(endp);
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,339 @@
|
|||
/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
|
||||
/* The protocol implementation */
|
||||
|
||||
/*
|
||||
* (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
|
||||
* (C) 2009-2010 by On-Waves
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <osmocore/talloc.h>
|
||||
|
||||
#include <openbsc/debug.h>
|
||||
#include <openbsc/mgcp.h>
|
||||
#include <openbsc/mgcp_internal.h>
|
||||
|
||||
#include <vty/command.h>
|
||||
#include <vty/vty.h>
|
||||
|
||||
static struct mgcp_config *g_cfg = NULL;
|
||||
|
||||
/*
|
||||
* vty code for mgcp below
|
||||
*/
|
||||
struct cmd_node mgcp_node = {
|
||||
MGCP_NODE,
|
||||
"%s(mgcp)#",
|
||||
1,
|
||||
};
|
||||
|
||||
static int config_write_mgcp(struct vty *vty)
|
||||
{
|
||||
vty_out(vty, "mgcp%s", VTY_NEWLINE);
|
||||
if (g_cfg->local_ip)
|
||||
vty_out(vty, " local ip %s%s", g_cfg->local_ip, VTY_NEWLINE);
|
||||
if (g_cfg->bts_ip)
|
||||
vty_out(vty, " bts ip %s%s", g_cfg->bts_ip, VTY_NEWLINE);
|
||||
vty_out(vty, " bind ip %s%s", g_cfg->source_addr, VTY_NEWLINE);
|
||||
vty_out(vty, " bind port %u%s", g_cfg->source_port, VTY_NEWLINE);
|
||||
vty_out(vty, " bind early %u%s", !!g_cfg->early_bind, VTY_NEWLINE);
|
||||
vty_out(vty, " rtp base %u%s", g_cfg->rtp_base_port, VTY_NEWLINE);
|
||||
vty_out(vty, " sdp audio payload number %u%s", g_cfg->audio_payload, VTY_NEWLINE);
|
||||
vty_out(vty, " sdp audio payload name %s%s", g_cfg->audio_name, VTY_NEWLINE);
|
||||
vty_out(vty, " loop %u%s", !!g_cfg->audio_loop, VTY_NEWLINE);
|
||||
vty_out(vty, " endpoints %u%s", g_cfg->number_endpoints, VTY_NEWLINE);
|
||||
if (g_cfg->forward_ip)
|
||||
vty_out(vty, " forward audio ip %s%s", g_cfg->forward_ip, VTY_NEWLINE);
|
||||
if (g_cfg->forward_port != 0)
|
||||
vty_out(vty, " forward audio port %d%s", g_cfg->forward_port, VTY_NEWLINE);
|
||||
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
|
||||
SHOW_STR "Display information about the MGCP Media Gateway")
|
||||
{
|
||||
int i;
|
||||
|
||||
vty_out(vty, "MGCP is up and running with %u endpoints:%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
|
||||
for (i = 1; i < g_cfg->number_endpoints; ++i) {
|
||||
struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
|
||||
vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u%s",
|
||||
i, endp->ci,
|
||||
ntohs(endp->net_rtp), ntohs(endp->net_rtcp),
|
||||
ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp), VTY_NEWLINE);
|
||||
}
|
||||
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp,
|
||||
cfg_mgcp_cmd,
|
||||
"mgcp",
|
||||
"Configure the MGCP")
|
||||
{
|
||||
vty->node = MGCP_NODE;
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_local_ip,
|
||||
cfg_mgcp_local_ip_cmd,
|
||||
"local ip IP",
|
||||
"Set the IP to be used in SDP records")
|
||||
{
|
||||
if (g_cfg->local_ip)
|
||||
talloc_free(g_cfg->local_ip);
|
||||
g_cfg->local_ip = talloc_strdup(g_cfg, argv[0]);
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_bts_ip,
|
||||
cfg_mgcp_bts_ip_cmd,
|
||||
"bts ip IP",
|
||||
"Set the IP of the BTS for RTP forwarding")
|
||||
{
|
||||
if (g_cfg->bts_ip)
|
||||
talloc_free(g_cfg->bts_ip);
|
||||
g_cfg->bts_ip = talloc_strdup(g_cfg, argv[0]);
|
||||
inet_aton(g_cfg->bts_ip, &g_cfg->bts_in);
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_bind_ip,
|
||||
cfg_mgcp_bind_ip_cmd,
|
||||
"bind ip IP",
|
||||
"Bind the MGCP to this local addr")
|
||||
{
|
||||
if (g_cfg->source_addr)
|
||||
talloc_free(g_cfg->source_addr);
|
||||
g_cfg->source_addr = talloc_strdup(g_cfg, argv[0]);
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_bind_port,
|
||||
cfg_mgcp_bind_port_cmd,
|
||||
"bind port <0-65534>",
|
||||
"Bind the MGCP to this port")
|
||||
{
|
||||
unsigned int port = atoi(argv[0]);
|
||||
if (port > 65534) {
|
||||
vty_out(vty, "%% wrong bind port '%s'%s", argv[0], VTY_NEWLINE);
|
||||
return CMD_WARNING;
|
||||
}
|
||||
|
||||
g_cfg->source_port = port;
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_bind_early,
|
||||
cfg_mgcp_bind_early_cmd,
|
||||
"bind early (0|1)",
|
||||
"Bind all RTP ports early")
|
||||
{
|
||||
unsigned int bind = atoi(argv[0]);
|
||||
if (bind != 0 && bind != 1) {
|
||||
vty_out(vty, "%% param must be 0 or 1.%s", VTY_NEWLINE);
|
||||
return CMD_WARNING;
|
||||
}
|
||||
|
||||
g_cfg->early_bind = bind == 1;
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_rtp_base_port,
|
||||
cfg_mgcp_rtp_base_port_cmd,
|
||||
"rtp base <0-65534>",
|
||||
"Base port to use")
|
||||
{
|
||||
unsigned int port = atoi(argv[0]);
|
||||
if (port > 65534) {
|
||||
vty_out(vty, "%% wrong base port '%s'%s", argv[0], VTY_NEWLINE);
|
||||
return CMD_WARNING;
|
||||
}
|
||||
|
||||
g_cfg->rtp_base_port = port;
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_sdp_payload_number,
|
||||
cfg_mgcp_sdp_payload_number_cmd,
|
||||
"sdp audio payload number <1-255>",
|
||||
"Set the audio codec to use")
|
||||
{
|
||||
unsigned int payload = atoi(argv[0]);
|
||||
if (payload > 255) {
|
||||
vty_out(vty, "%% wrong payload number '%s'%s", argv[0], VTY_NEWLINE);
|
||||
return CMD_WARNING;
|
||||
}
|
||||
|
||||
g_cfg->audio_payload = payload;
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_sdp_payload_name,
|
||||
cfg_mgcp_sdp_payload_name_cmd,
|
||||
"sdp audio payload name NAME",
|
||||
"Set the audio name to use")
|
||||
{
|
||||
if (g_cfg->audio_name)
|
||||
talloc_free(g_cfg->audio_name);
|
||||
g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]);
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_loop,
|
||||
cfg_mgcp_loop_cmd,
|
||||
"loop (0|1)",
|
||||
"Loop the audio")
|
||||
{
|
||||
g_cfg->audio_loop = atoi(argv[0]);
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_number_endp,
|
||||
cfg_mgcp_number_endp_cmd,
|
||||
"number endpoints <0-65534>",
|
||||
"The number of endpoints to allocate. This is not dynamic.")
|
||||
{
|
||||
/* + 1 as we start counting at one */
|
||||
g_cfg->number_endpoints = atoi(argv[0]) + 1;
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_forward_ip,
|
||||
cfg_mgcp_forward_ip_cmd,
|
||||
"forward audio ip IP",
|
||||
"Forward packets from and to the IP. This disables most of the MGCP feature.")
|
||||
{
|
||||
if (g_cfg->forward_ip)
|
||||
talloc_free(g_cfg->forward_ip);
|
||||
g_cfg->forward_ip = talloc_strdup(g_cfg, argv[0]);
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
DEFUN(cfg_mgcp_forward_port,
|
||||
cfg_mgcp_forward_port_cmd,
|
||||
"forward audio port <1-15000>",
|
||||
"Forward packets from and to the port. This disables most of the MGCP feature.")
|
||||
{
|
||||
g_cfg->forward_port = atoi(argv[0]);
|
||||
return CMD_SUCCESS;
|
||||
}
|
||||
|
||||
int mgcp_vty_init(void)
|
||||
{
|
||||
install_element(VIEW_NODE, &show_mgcp_cmd);
|
||||
|
||||
install_element(CONFIG_NODE, &cfg_mgcp_cmd);
|
||||
install_node(&mgcp_node, config_write_mgcp);
|
||||
install_default(MGCP_NODE);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_forward_ip_cmd);
|
||||
install_element(MGCP_NODE, &cfg_mgcp_forward_port_cmd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
|
||||
{
|
||||
int i, rc;
|
||||
|
||||
g_cfg = cfg;
|
||||
rc = vty_read_config_file(config_file);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
if (!g_cfg->bts_ip)
|
||||
fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
|
||||
|
||||
if (mgcp_endpoints_allocate(g_cfg) != 0) {
|
||||
fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* This application supports two modes.
|
||||
* 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
|
||||
* 2.) plain forwarding of RTP packets on the endpoints.
|
||||
* both modes are mutual exclusive
|
||||
*/
|
||||
if (g_cfg->forward_ip) {
|
||||
int port = g_cfg->rtp_base_port;
|
||||
if (g_cfg->forward_port != 0)
|
||||
port = g_cfg->forward_port;
|
||||
|
||||
if (!g_cfg->early_bind) {
|
||||
LOGP(DMGCP, LOGL_NOTICE, "Forwarding requires early bind.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Store the forward IP and assign a ci. For early bind
|
||||
* the sockets will be created after this.
|
||||
*/
|
||||
for (i = 1; i < g_cfg->number_endpoints; ++i) {
|
||||
struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
|
||||
inet_aton(g_cfg->forward_ip, &endp->remote);
|
||||
endp->ci = CI_UNUSED + 23;
|
||||
endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port));
|
||||
endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1);
|
||||
}
|
||||
|
||||
LOGP(DMGCP, LOGL_NOTICE, "Configured for Audio Forwarding.\n");
|
||||
}
|
||||
|
||||
/* early bind */
|
||||
if (g_cfg->early_bind) {
|
||||
for (i = 1; i < g_cfg->number_endpoints; ++i) {
|
||||
struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
|
||||
int rtp_port;
|
||||
|
||||
rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_base_port);
|
||||
if (mgcp_bind_rtp_port(endp, rtp_port) != 0) {
|
||||
LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return !!g_cfg->forward_ip;
|
||||
}
|
||||
|
||||
struct gsm_network;
|
||||
int bsc_vty_init(struct gsm_network *dummy)
|
||||
{
|
||||
cmd_init(1);
|
||||
vty_init();
|
||||
|
||||
mgcp_vty_init();
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue