2009-10-09 05:08:11 +00:00
|
|
|
/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
|
2010-02-03 07:50:33 +00:00
|
|
|
/* The main method to drive it as a standalone process */
|
2009-10-09 05:08:11 +00:00
|
|
|
|
|
|
|
/*
|
2011-06-09 22:04:55 +00:00
|
|
|
* (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
|
|
|
|
* (C) 2009-2011 by On-Waves
|
2009-10-09 05:08:11 +00:00
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2011-01-01 14:25:50 +00:00
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2009-10-09 05:08:11 +00:00
|
|
|
* (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
|
2011-01-01 14:25:50 +00:00
|
|
|
* GNU Affero General Public License for more details.
|
2009-10-09 05:08:11 +00:00
|
|
|
*
|
2011-01-01 14:25:50 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-10-09 05:08:11 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <openbsc/debug.h>
|
2011-05-12 14:02:07 +00:00
|
|
|
#include <openbsc/gsm_data.h>
|
|
|
|
#include <openbsc/mgcp.h>
|
|
|
|
#include <openbsc/mgcp_internal.h>
|
|
|
|
#include <openbsc/vty.h>
|
|
|
|
|
|
|
|
#include <osmocom/core/application.h>
|
2011-03-22 15:47:59 +00:00
|
|
|
#include <osmocom/core/msgb.h>
|
|
|
|
#include <osmocom/core/talloc.h>
|
|
|
|
#include <osmocom/core/select.h>
|
2015-11-02 15:07:57 +00:00
|
|
|
#include <osmocom/core/stats.h>
|
|
|
|
#include <osmocom/core/rate_ctr.h>
|
2011-05-12 14:02:07 +00:00
|
|
|
|
2011-02-19 15:48:17 +00:00
|
|
|
#include <osmocom/vty/telnet_interface.h>
|
|
|
|
#include <osmocom/vty/logging.h>
|
2014-08-24 15:54:49 +00:00
|
|
|
#include <osmocom/vty/ports.h>
|
2010-05-19 17:45:32 +00:00
|
|
|
#include <osmocom/vty/command.h>
|
2015-11-02 15:07:57 +00:00
|
|
|
#include <osmocom/vty/stats.h>
|
2010-03-30 10:43:53 +00:00
|
|
|
|
2010-03-22 16:09:32 +00:00
|
|
|
#include "../../bscconfig.h"
|
|
|
|
|
2014-03-13 13:25:51 +00:00
|
|
|
#ifdef BUILD_MGCP_TRANSCODING
|
2014-05-08 12:08:37 +00:00
|
|
|
#include "openbsc/mgcp_transcode.h"
|
2014-03-13 13:25:51 +00:00
|
|
|
#endif
|
|
|
|
|
2009-10-09 05:08:11 +00:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
#warning "Make use of the rtp proxy code"
|
|
|
|
|
2010-02-20 20:21:02 +00:00
|
|
|
static struct mgcp_config *cfg;
|
2011-06-09 22:04:55 +00:00
|
|
|
static struct mgcp_trunk_config *reset_trunk;
|
2010-03-31 05:01:35 +00:00
|
|
|
static int reset_endpoints = 0;
|
2010-08-25 17:43:54 +00:00
|
|
|
static int daemonize = 0;
|
2010-03-31 05:01:35 +00:00
|
|
|
|
2010-03-29 06:47:44 +00:00
|
|
|
const char *openbsc_copyright =
|
2010-09-11 05:32:30 +00:00
|
|
|
"Copyright (C) 2009-2010 Holger Freyther and On-Waves\r\n"
|
2011-01-01 14:25:50 +00:00
|
|
|
"Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\r\n"
|
2010-09-11 05:32:30 +00:00
|
|
|
"Dieter Spaar, Andreas Eversberg, Harald Welte\r\n\r\n"
|
2011-01-01 14:25:50 +00:00
|
|
|
"License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
|
2010-09-11 05:32:30 +00:00
|
|
|
"This is free software: you are free to change and redistribute it.\r\n"
|
|
|
|
"There is NO WARRANTY, to the extent permitted by law.\r\n";
|
2009-10-09 05:08:11 +00:00
|
|
|
|
|
|
|
static char *config_file = "mgcp.cfg";
|
|
|
|
|
|
|
|
/* used by msgb and mgcp */
|
|
|
|
void *tall_bsc_ctx = NULL;
|
|
|
|
|
|
|
|
static void print_help()
|
|
|
|
{
|
|
|
|
printf("Some useful help...\n");
|
|
|
|
printf(" -h --help is printing this text.\n");
|
|
|
|
printf(" -c --config-file filename The config file to use.\n");
|
2012-09-20 14:04:54 +00:00
|
|
|
printf(" -s --disable-color\n");
|
2012-01-27 12:21:46 +00:00
|
|
|
printf(" -D --daemonize Fork the process into a background daemon\n");
|
|
|
|
printf(" -V --version Print the version number\n");
|
2009-10-09 05:08:11 +00:00
|
|
|
}
|
|
|
|
|
2010-09-17 22:44:24 +00:00
|
|
|
static void handle_options(int argc, char **argv)
|
2009-10-09 05:08:11 +00:00
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
int option_index = 0, c;
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{"config-file", 1, 0, 'c'},
|
2010-08-25 17:43:54 +00:00
|
|
|
{"daemonize", 0, 0, 'D'},
|
2010-03-22 16:09:32 +00:00
|
|
|
{"version", 0, 0, 'V'},
|
2012-09-20 14:04:54 +00:00
|
|
|
{"disable-color", 0, 0, 's'},
|
2009-10-09 05:08:11 +00:00
|
|
|
{0, 0, 0, 0},
|
|
|
|
};
|
|
|
|
|
2010-08-25 17:43:54 +00:00
|
|
|
c = getopt_long(argc, argv, "hc:VD", long_options, &option_index);
|
2009-10-09 05:08:11 +00:00
|
|
|
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch(c) {
|
|
|
|
case 'h':
|
|
|
|
print_help();
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
config_file = talloc_strdup(tall_bsc_ctx, optarg);
|
|
|
|
break;
|
2012-09-20 14:04:54 +00:00
|
|
|
case 's':
|
|
|
|
log_set_use_color(osmo_stderr_target, 0);
|
|
|
|
break;
|
2010-03-22 16:09:32 +00:00
|
|
|
case 'V':
|
2010-05-16 17:20:24 +00:00
|
|
|
print_version(1);
|
2010-03-22 16:09:32 +00:00
|
|
|
exit(0);
|
|
|
|
break;
|
2010-08-25 17:43:54 +00:00
|
|
|
case 'D':
|
|
|
|
daemonize = 1;
|
|
|
|
break;
|
2009-10-09 05:08:11 +00:00
|
|
|
default:
|
|
|
|
/* ignore */
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-31 05:01:35 +00:00
|
|
|
/* simply remember this */
|
2011-06-09 22:04:55 +00:00
|
|
|
static int mgcp_rsip_cb(struct mgcp_trunk_config *tcfg)
|
2010-03-31 05:01:35 +00:00
|
|
|
{
|
|
|
|
reset_endpoints = 1;
|
2011-06-09 22:04:55 +00:00
|
|
|
reset_trunk = tcfg;
|
2010-03-31 05:01:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-06 10:11:23 +00:00
|
|
|
static int read_call_agent(struct osmo_fd *fd, unsigned int what)
|
2009-10-09 05:08:11 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
socklen_t slen = sizeof(addr);
|
|
|
|
struct msgb *msg;
|
2010-02-03 10:03:45 +00:00
|
|
|
struct msgb *resp;
|
2010-03-31 05:01:35 +00:00
|
|
|
int i;
|
2009-10-09 05:08:11 +00:00
|
|
|
|
|
|
|
msg = (struct msgb *) fd->data;
|
|
|
|
|
|
|
|
/* read one less so we can use it as a \0 */
|
2010-09-18 12:40:22 +00:00
|
|
|
int rc = recvfrom(cfg->gw_fd.bfd.fd, msg->data, msg->data_len - 1, 0,
|
2009-10-09 05:08:11 +00:00
|
|
|
(struct sockaddr *) &addr, &slen);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("Gateway failed to read");
|
|
|
|
return -1;
|
|
|
|
} else if (slen > sizeof(addr)) {
|
2012-03-16 11:18:39 +00:00
|
|
|
fprintf(stderr, "Gateway received message from outerspace: %zu %zu\n",
|
2015-08-03 07:28:41 +00:00
|
|
|
(size_t) slen, sizeof(addr));
|
2009-10-09 05:08:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* handle message now */
|
|
|
|
msg->l2h = msgb_put(msg, rc);
|
2010-02-20 20:21:02 +00:00
|
|
|
resp = mgcp_handle_message(cfg, msg);
|
2009-10-09 05:08:11 +00:00
|
|
|
msgb_reset(msg);
|
2010-02-03 10:03:45 +00:00
|
|
|
|
|
|
|
if (resp) {
|
2010-09-18 12:40:22 +00:00
|
|
|
sendto(cfg->gw_fd.bfd.fd, resp->l2h, msgb_l2len(resp), 0, (struct sockaddr *) &addr, sizeof(addr));
|
2010-02-03 10:03:45 +00:00
|
|
|
msgb_free(resp);
|
|
|
|
}
|
2010-03-31 05:01:35 +00:00
|
|
|
|
|
|
|
if (reset_endpoints) {
|
2011-06-09 22:04:55 +00:00
|
|
|
LOGP(DMGCP, LOGL_NOTICE,
|
|
|
|
"Asked to reset endpoints: %d/%d\n",
|
|
|
|
reset_trunk->trunk_nr, reset_trunk->trunk_type);
|
2010-03-31 05:01:35 +00:00
|
|
|
reset_endpoints = 0;
|
|
|
|
|
|
|
|
/* is checking in_addr.s_addr == INADDR_LOOPBACK making it more secure? */
|
2011-06-09 22:04:55 +00:00
|
|
|
for (i = 1; i < reset_trunk->number_endpoints; ++i)
|
2014-07-22 13:00:52 +00:00
|
|
|
mgcp_release_endp(&reset_trunk->endpoints[i]);
|
2010-03-31 05:01:35 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 05:08:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-24 13:38:38 +00:00
|
|
|
extern int bsc_vty_go_parent(struct vty *vty);
|
2010-05-25 21:31:39 +00:00
|
|
|
|
|
|
|
static struct vty_app_info vty_info = {
|
|
|
|
.name = "OpenBSC MGCP",
|
|
|
|
.version = PACKAGE_VERSION,
|
|
|
|
.go_parent_cb = bsc_vty_go_parent,
|
2010-09-04 03:00:01 +00:00
|
|
|
.is_config_node = bsc_vty_is_config_node,
|
2010-05-25 21:31:39 +00:00
|
|
|
};
|
2010-01-13 22:36:53 +00:00
|
|
|
|
2010-09-17 22:44:24 +00:00
|
|
|
int main(int argc, char **argv)
|
2009-10-09 05:08:11 +00:00
|
|
|
{
|
2009-11-19 14:03:39 +00:00
|
|
|
struct gsm_network dummy_network;
|
2009-10-09 05:08:11 +00:00
|
|
|
struct sockaddr_in addr;
|
2010-02-03 07:50:33 +00:00
|
|
|
int on = 1, rc;
|
2009-10-09 05:08:11 +00:00
|
|
|
|
|
|
|
tall_bsc_ctx = talloc_named_const(NULL, 1, "mgcp-callagent");
|
2009-12-22 21:32:51 +00:00
|
|
|
|
2011-05-12 14:02:07 +00:00
|
|
|
osmo_init_ignore_signals();
|
|
|
|
osmo_init_logging(&log_info);
|
2009-12-22 21:32:51 +00:00
|
|
|
|
2010-02-20 20:21:02 +00:00
|
|
|
cfg = mgcp_config_alloc();
|
|
|
|
if (!cfg)
|
|
|
|
return -1;
|
|
|
|
|
2014-03-13 13:25:51 +00:00
|
|
|
#ifdef BUILD_MGCP_TRANSCODING
|
|
|
|
cfg->setup_rtp_processing_cb = &mgcp_transcoding_setup;
|
|
|
|
cfg->rtp_processing_cb = &mgcp_transcoding_process_rtp;
|
|
|
|
cfg->get_net_downlink_format_cb = &mgcp_transcoding_net_downlink_format;
|
|
|
|
#endif
|
|
|
|
|
2010-05-25 21:31:39 +00:00
|
|
|
vty_info.copyright = openbsc_copyright;
|
|
|
|
vty_init(&vty_info);
|
2011-03-09 12:36:32 +00:00
|
|
|
logging_vty_add_cmds(&log_info);
|
2015-11-02 15:07:57 +00:00
|
|
|
osmo_stats_vty_add_cmds(&log_info);
|
2010-05-16 18:52:23 +00:00
|
|
|
mgcp_vty_init();
|
2010-05-16 17:20:24 +00:00
|
|
|
|
2009-10-09 05:08:11 +00:00
|
|
|
handle_options(argc, argv);
|
|
|
|
|
2015-11-02 15:07:57 +00:00
|
|
|
rate_ctr_init(tall_bsc_ctx);
|
|
|
|
osmo_stats_init(tall_bsc_ctx);
|
|
|
|
|
2013-08-02 19:14:14 +00:00
|
|
|
rc = mgcp_parse_config(config_file, cfg, MGCP_BSC);
|
2010-02-03 07:50:33 +00:00
|
|
|
if (rc < 0)
|
2009-10-09 05:08:11 +00:00
|
|
|
return rc;
|
|
|
|
|
enable telnet VTY bind address config for various programs
Following the 'line vty'/'bind A.B.C.D' command added in libosmocore, use the
configured address to set the telnet bind for the VTY line. It is now possible
to publish the VTY on a specific local interface (including 0.0.0.0 aka "any").
Implement in all of:
osmo-gbproxy
osmo-gtphub
osmo-sgsn
osmo-bsc
osmo-bsc_nat
osmo-bsc_mgcp
osmo-nitb
In some of these main programs, move the telnet initialization below the
configuration parsing.
Historically, this was not a good idea for programs using bsc_init.c (aka
bsc_bootstrap_network()), since they expected a gsm_network struct pointer in
((struct telnet_connection*)vty->priv)->priv, so that telnet had to be either
initialized or replaced by a dummy struct. In the meantime, the gsm_network
struct is not actually looked up in a priv pointer but in the static bsc_vty.c
scope (bsc_gsmnet), so this limitation is mere legacy (even though said legacy
is still there in an "#if 0" chunk).
In the other binaries I have briefly looked at the init sequence dependencies
and found no reason to initialize telnet above the config file parsing. In any
case, I have tested every single one of abovementioned binaries to verify that
they still parse the example config successfully and launch, allowing VTY
connections on the configured address(es). I hope this suffices.
In all of the above, log VTY address and port. LOGL_INFO is disabled by default
in some of the logging scopes, and since it is a single log message right at
program launch, I decided for the slightly more aggressive LOGL_NOTICE.
2016-02-23 13:09:38 +00:00
|
|
|
/* start telnet after reading config for vty_get_bind_addr() */
|
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "VTY at %s %d\n",
|
|
|
|
vty_get_bind_addr(), OSMO_VTY_PORT_BSC_MGCP);
|
|
|
|
rc = telnet_init_dynif(tall_bsc_ctx, &dummy_network,
|
|
|
|
vty_get_bind_addr(), OSMO_VTY_PORT_BSC_MGCP);
|
2010-05-16 18:52:23 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
2010-03-31 05:01:35 +00:00
|
|
|
/* set some callbacks */
|
|
|
|
cfg->reset_cb = mgcp_rsip_cb;
|
2009-10-09 05:08:11 +00:00
|
|
|
|
2010-02-03 07:50:33 +00:00
|
|
|
/* we need to bind a socket */
|
|
|
|
if (rc == 0) {
|
2010-09-18 12:40:22 +00:00
|
|
|
cfg->gw_fd.bfd.when = BSC_FD_READ;
|
|
|
|
cfg->gw_fd.bfd.cb = read_call_agent;
|
|
|
|
cfg->gw_fd.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (cfg->gw_fd.bfd.fd < 0) {
|
2010-01-05 11:21:36 +00:00
|
|
|
perror("Gateway failed to listen");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-10-09 05:08:11 +00:00
|
|
|
|
2010-09-18 12:40:22 +00:00
|
|
|
setsockopt(cfg->gw_fd.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
|
2009-10-09 05:08:11 +00:00
|
|
|
|
2010-01-05 11:21:36 +00:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sin_family = AF_INET;
|
2010-02-20 20:21:02 +00:00
|
|
|
addr.sin_port = htons(cfg->source_port);
|
|
|
|
inet_aton(cfg->source_addr, &addr.sin_addr);
|
2009-10-09 05:08:11 +00:00
|
|
|
|
2010-09-18 12:40:22 +00:00
|
|
|
if (bind(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
2010-01-05 11:21:36 +00:00
|
|
|
perror("Gateway failed to bind");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-10-09 05:08:11 +00:00
|
|
|
|
2010-09-18 12:40:22 +00:00
|
|
|
cfg->gw_fd.bfd.data = msgb_alloc(4096, "mgcp-msg");
|
|
|
|
if (!cfg->gw_fd.bfd.data) {
|
2010-01-05 11:21:36 +00:00
|
|
|
fprintf(stderr, "Gateway memory error.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-01-26 23:38:59 +00:00
|
|
|
if (cfg->call_agent_addr) {
|
|
|
|
addr.sin_port = htons(2727);
|
|
|
|
inet_aton(cfg->call_agent_addr, &addr.sin_addr);
|
|
|
|
if (connect(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
|
|
|
LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n",
|
|
|
|
cfg->call_agent_addr, errno);
|
|
|
|
close(cfg->gw_fd.bfd.fd);
|
|
|
|
cfg->gw_fd.bfd.fd = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-01-05 11:21:36 +00:00
|
|
|
|
2011-05-06 10:11:23 +00:00
|
|
|
if (osmo_fd_register(&cfg->gw_fd.bfd) != 0) {
|
2010-03-31 04:16:45 +00:00
|
|
|
LOGP(DMGCP, LOGL_FATAL, "Failed to register the fd\n");
|
2010-01-05 11:21:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-01-05 11:25:25 +00:00
|
|
|
|
2010-03-31 04:16:45 +00:00
|
|
|
LOGP(DMGCP, LOGL_NOTICE, "Configured for MGCP.\n");
|
2009-10-09 05:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialisation */
|
|
|
|
srand(time(NULL));
|
|
|
|
|
2010-08-25 17:43:54 +00:00
|
|
|
if (daemonize) {
|
|
|
|
rc = osmo_daemonize();
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("Error during daemonize");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-09 05:08:11 +00:00
|
|
|
/* main loop */
|
|
|
|
while (1) {
|
2011-05-06 10:11:23 +00:00
|
|
|
osmo_select_main(0);
|
2009-10-09 05:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|