Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* The main method to drive it as a standalone process */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2011 by On-Waves |
Harald Welte | a896f9c | 2017-11-17 15:08:41 +0100 | [diff] [blame] | 7 | * (C) 2017 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 8 | * All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Affero General Public License as published by |
| 12 | * the Free Software Foundation; either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Affero General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Affero General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 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 | #include <errno.h> |
| 33 | |
| 34 | #include <sys/socket.h> |
| 35 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 36 | #include <osmocom/mgcp/mgcp.h> |
| 37 | #include <osmocom/mgcp/mgcp_internal.h> |
| 38 | #include <osmocom/mgcp/vty.h> |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 39 | #include <osmocom/mgcp/debug.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 40 | #include <osmocom/mgcp/mgcp_endp.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 41 | |
| 42 | #include <osmocom/core/application.h> |
| 43 | #include <osmocom/core/msgb.h> |
| 44 | #include <osmocom/core/talloc.h> |
| 45 | #include <osmocom/core/select.h> |
| 46 | #include <osmocom/core/stats.h> |
| 47 | #include <osmocom/core/rate_ctr.h> |
| 48 | #include <osmocom/core/logging.h> |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 49 | #include <osmocom/core/socket.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 50 | |
| 51 | #include <osmocom/vty/telnet_interface.h> |
| 52 | #include <osmocom/vty/logging.h> |
| 53 | #include <osmocom/vty/ports.h> |
| 54 | #include <osmocom/vty/command.h> |
| 55 | #include <osmocom/vty/stats.h> |
Neels Hofmeyr | 515e341 | 2018-02-17 14:10:41 +0100 | [diff] [blame] | 56 | #include <osmocom/vty/misc.h> |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 57 | |
| 58 | #include "../../bscconfig.h" |
| 59 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 60 | #define _GNU_SOURCE |
| 61 | #include <getopt.h> |
| 62 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 63 | /* FIXME: Make use of the rtp proxy code */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 64 | |
| 65 | static struct mgcp_config *cfg; |
| 66 | static struct mgcp_trunk_config *reset_trunk; |
| 67 | static int reset_endpoints = 0; |
| 68 | static int daemonize = 0; |
| 69 | |
Harald Welte | 839fc95 | 2017-11-17 20:52:44 +0100 | [diff] [blame] | 70 | const char *osmomgw_copyright = |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 71 | "Copyright (C) 2009-2010 Holger Freyther and On-Waves\r\n" |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 72 | "Copyright (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n" |
Harald Welte | 839fc95 | 2017-11-17 20:52:44 +0100 | [diff] [blame] | 73 | "Contributions by Pablo Neira Ayuso, Jacob Erlbeck, Neels Hofmeyr\r\n" |
| 74 | "Philipp Maier\r\n\r\n" |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 75 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
| 76 | "This is free software: you are free to change and redistribute it.\r\n" |
| 77 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
| 78 | |
Harald Welte | d164b05 | 2017-11-17 15:10:52 +0100 | [diff] [blame] | 79 | static char *config_file = "osmo-mgw.cfg"; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 80 | |
| 81 | /* used by msgb and mgcp */ |
| 82 | void *tall_bsc_ctx = NULL; |
| 83 | |
| 84 | static void print_help() |
| 85 | { |
| 86 | printf("Some useful help...\n"); |
| 87 | printf(" -h --help is printing this text.\n"); |
| 88 | printf(" -c --config-file filename The config file to use.\n"); |
| 89 | printf(" -s --disable-color\n"); |
| 90 | printf(" -D --daemonize Fork the process into a background daemon\n"); |
| 91 | printf(" -V --version Print the version number\n"); |
| 92 | } |
| 93 | |
| 94 | static void handle_options(int argc, char **argv) |
| 95 | { |
| 96 | while (1) { |
| 97 | int option_index = 0, c; |
| 98 | static struct option long_options[] = { |
| 99 | {"help", 0, 0, 'h'}, |
| 100 | {"config-file", 1, 0, 'c'}, |
| 101 | {"daemonize", 0, 0, 'D'}, |
| 102 | {"version", 0, 0, 'V'}, |
| 103 | {"disable-color", 0, 0, 's'}, |
| 104 | {0, 0, 0, 0}, |
| 105 | }; |
| 106 | |
Neels Hofmeyr | cc39218 | 2018-11-25 22:33:32 +0100 | [diff] [blame] | 107 | c = getopt_long(argc, argv, "hc:sVD", long_options, &option_index); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 108 | |
| 109 | if (c == -1) |
| 110 | break; |
| 111 | |
| 112 | switch(c) { |
| 113 | case 'h': |
| 114 | print_help(); |
| 115 | exit(0); |
| 116 | break; |
| 117 | case 'c': |
| 118 | config_file = talloc_strdup(tall_bsc_ctx, optarg); |
| 119 | break; |
| 120 | case 's': |
| 121 | log_set_use_color(osmo_stderr_target, 0); |
| 122 | break; |
| 123 | case 'V': |
| 124 | print_version(1); |
| 125 | exit(0); |
| 126 | break; |
| 127 | case 'D': |
| 128 | daemonize = 1; |
| 129 | break; |
| 130 | default: |
| 131 | /* ignore */ |
| 132 | break; |
| 133 | }; |
| 134 | } |
| 135 | } |
| 136 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 137 | /* Callback function to be called when the RSIP ("Reset in Progress") mgcp |
| 138 | * command is received */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 139 | static int mgcp_rsip_cb(struct mgcp_trunk_config *tcfg) |
| 140 | { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 141 | /* Set flag so that, when read_call_agent() is called next time |
| 142 | * the reset can progress */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 143 | reset_endpoints = 1; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 144 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 145 | reset_trunk = tcfg; |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static int read_call_agent(struct osmo_fd *fd, unsigned int what) |
| 151 | { |
| 152 | struct sockaddr_in addr; |
| 153 | socklen_t slen = sizeof(addr); |
| 154 | struct msgb *msg; |
| 155 | struct msgb *resp; |
| 156 | int i; |
| 157 | |
| 158 | msg = (struct msgb *) fd->data; |
| 159 | |
| 160 | /* read one less so we can use it as a \0 */ |
| 161 | int rc = recvfrom(cfg->gw_fd.bfd.fd, msg->data, msg->data_len - 1, 0, |
| 162 | (struct sockaddr *) &addr, &slen); |
| 163 | if (rc < 0) { |
| 164 | perror("Gateway failed to read"); |
| 165 | return -1; |
| 166 | } else if (slen > sizeof(addr)) { |
| 167 | fprintf(stderr, "Gateway received message from outerspace: %zu %zu\n", |
| 168 | (size_t) slen, sizeof(addr)); |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | /* handle message now */ |
| 173 | msg->l2h = msgb_put(msg, rc); |
| 174 | resp = mgcp_handle_message(cfg, msg); |
| 175 | msgb_reset(msg); |
| 176 | |
| 177 | if (resp) { |
| 178 | sendto(cfg->gw_fd.bfd.fd, resp->l2h, msgb_l2len(resp), 0, (struct sockaddr *) &addr, sizeof(addr)); |
| 179 | msgb_free(resp); |
| 180 | } |
| 181 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 182 | /* reset endpoints */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 183 | if (reset_endpoints) { |
| 184 | LOGP(DLMGCP, LOGL_NOTICE, |
| 185 | "Asked to reset endpoints: %d/%d\n", |
| 186 | reset_trunk->trunk_nr, reset_trunk->trunk_type); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 187 | |
| 188 | /* reset flag */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 189 | reset_endpoints = 0; |
| 190 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 191 | /* Walk over all endpoints and trigger a release, this will release all |
| 192 | * endpoints, possible open connections are forcefully dropped */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 193 | for (i = 1; i < reset_trunk->number_endpoints; ++i) |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 194 | mgcp_endp_release(&reset_trunk->endpoints[i]); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | int mgcp_vty_is_config_node(struct vty *vty, int node) |
| 201 | { |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 202 | switch (node) { |
| 203 | case CONFIG_NODE: |
| 204 | return 0; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 205 | |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 206 | default: |
| 207 | return 1; |
| 208 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | int mgcp_vty_go_parent(struct vty *vty) |
| 212 | { |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 213 | switch (vty->node) { |
| 214 | case TRUNK_NODE: |
| 215 | vty->node = MGCP_NODE; |
| 216 | vty->index = NULL; |
| 217 | break; |
| 218 | case MGCP_NODE: |
| 219 | default: |
| 220 | if (mgcp_vty_is_config_node(vty, vty->node)) |
| 221 | vty->node = CONFIG_NODE; |
| 222 | else |
| 223 | vty->node = ENABLE_NODE; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 224 | |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 225 | vty->index = NULL; |
| 226 | } |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 227 | |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 228 | return vty->node; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
| 232 | static struct vty_app_info vty_info = { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 233 | .name = "OsmoMGW", |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 234 | .version = PACKAGE_VERSION, |
| 235 | .go_parent_cb = mgcp_vty_go_parent, |
| 236 | .is_config_node = mgcp_vty_is_config_node, |
| 237 | }; |
| 238 | |
| 239 | static const struct log_info_cat log_categories[] = { |
| 240 | /* DLMGCP is provided by the MGCP library */ |
Philipp Maier | c341388 | 2017-10-27 12:26:54 +0200 | [diff] [blame] | 241 | [DRTP] = { |
| 242 | .name = "DRTP", |
| 243 | .description = "RTP stream handling", |
| 244 | .color = "\033[1;30m", |
| 245 | .enabled = 1,.loglevel = LOGL_NOTICE, |
| 246 | }, |
Neels Hofmeyr | 306dc09 | 2018-09-30 05:01:20 +0200 | [diff] [blame] | 247 | [DIUUP] = { |
| 248 | .name = "DIUUP", |
| 249 | .description = "IuUP within RTP stream handling", |
| 250 | .color = "\033[1;31m", |
| 251 | .enabled = 1,.loglevel = LOGL_NOTICE, |
| 252 | }, |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 253 | }; |
| 254 | |
| 255 | const struct log_info log_info = { |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 256 | .cat = log_categories, |
| 257 | .num_cat = ARRAY_SIZE(log_categories), |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | int main(int argc, char **argv) |
| 261 | { |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 262 | unsigned int flags; |
| 263 | int rc; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 264 | |
| 265 | tall_bsc_ctx = talloc_named_const(NULL, 1, "mgcp-callagent"); |
Neels Hofmeyr | 515e341 | 2018-02-17 14:10:41 +0100 | [diff] [blame] | 266 | vty_info.tall_ctx = tall_bsc_ctx; |
| 267 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 268 | msgb_talloc_ctx_init(tall_bsc_ctx, 0); |
| 269 | |
| 270 | osmo_init_ignore_signals(); |
Neels Hofmeyr | 60f8e31 | 2018-03-30 23:01:07 +0200 | [diff] [blame] | 271 | osmo_init_logging2(tall_bsc_ctx, &log_info); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 272 | |
| 273 | cfg = mgcp_config_alloc(); |
| 274 | if (!cfg) |
| 275 | return -1; |
| 276 | |
Harald Welte | 839fc95 | 2017-11-17 20:52:44 +0100 | [diff] [blame] | 277 | vty_info.copyright = osmomgw_copyright; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 278 | vty_init(&vty_info); |
Pau Espin Pedrol | f54eb96 | 2019-08-05 16:05:14 +0200 | [diff] [blame] | 279 | logging_vty_add_cmds(); |
Neels Hofmeyr | 515e341 | 2018-02-17 14:10:41 +0100 | [diff] [blame] | 280 | osmo_talloc_vty_add_cmds(); |
Pau Espin Pedrol | f54eb96 | 2019-08-05 16:05:14 +0200 | [diff] [blame] | 281 | osmo_stats_vty_add_cmds(); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 282 | mgcp_vty_init(); |
| 283 | |
| 284 | handle_options(argc, argv); |
| 285 | |
| 286 | rate_ctr_init(tall_bsc_ctx); |
| 287 | osmo_stats_init(tall_bsc_ctx); |
| 288 | |
| 289 | rc = mgcp_parse_config(config_file, cfg, MGCP_BSC); |
| 290 | if (rc < 0) |
| 291 | return rc; |
| 292 | |
| 293 | /* start telnet after reading config for vty_get_bind_addr() */ |
| 294 | rc = telnet_init_dynif(tall_bsc_ctx, NULL, |
Philipp Maier | 9a3543a | 2017-11-14 15:23:40 +0100 | [diff] [blame] | 295 | vty_get_bind_addr(), OSMO_VTY_PORT_MGW); |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 296 | if (rc < 0) |
| 297 | return rc; |
| 298 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 299 | /* Set the reset callback function. This functions is called when the |
| 300 | * mgcp-command "RSIP" (Reset in Progress) is received */ |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 301 | cfg->reset_cb = mgcp_rsip_cb; |
| 302 | |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 303 | /* we need to bind a socket */ |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 304 | flags = OSMO_SOCK_F_BIND; |
| 305 | if (cfg->call_agent_addr) |
| 306 | flags |= OSMO_SOCK_F_CONNECT; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 307 | |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 308 | rc = osmo_sock_init2_ofd(&cfg->gw_fd.bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 309 | cfg->source_addr, cfg->source_port, |
| 310 | cfg->call_agent_addr, cfg->call_agent_addr ? 2727 : 0, flags); |
| 311 | if (rc < 0) { |
| 312 | perror("Gateway failed to bind"); |
| 313 | return -1; |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 316 | cfg->gw_fd.bfd.cb = read_call_agent; |
| 317 | cfg->gw_fd.bfd.data = msgb_alloc(4096, "mgcp-msg"); |
| 318 | if (!cfg->gw_fd.bfd.data) { |
| 319 | fprintf(stderr, "Gateway memory error.\n"); |
| 320 | return -1; |
| 321 | } |
| 322 | |
Philipp Maier | eb0bde0 | 2018-01-18 16:27:29 +0100 | [diff] [blame] | 323 | LOGP(DLMGCP, LOGL_NOTICE, "Configured for MGCP, listen on %s:%u\n", |
| 324 | cfg->source_addr, cfg->source_port); |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 325 | |
Neels Hofmeyr | f83ec56 | 2017-09-07 19:18:40 +0200 | [diff] [blame] | 326 | /* initialisation */ |
| 327 | srand(time(NULL)); |
| 328 | |
| 329 | if (daemonize) { |
| 330 | rc = osmo_daemonize(); |
| 331 | if (rc < 0) { |
| 332 | perror("Error during daemonize"); |
| 333 | exit(1); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /* main loop */ |
| 338 | while (1) { |
| 339 | osmo_select_main(0); |
| 340 | } |
| 341 | |
| 342 | |
| 343 | return 0; |
| 344 | } |