Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [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 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009 by On-Waves |
| 7 | * 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 Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 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 Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <ctype.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <time.h> |
| 29 | #include <limits.h> |
| 30 | #include <unistd.h> |
| 31 | |
| 32 | #include <sys/socket.h> |
| 33 | |
| 34 | #include <openbsc/debug.h> |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 35 | #include <openbsc/gsm_data.h> |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 36 | #include <openbsc/mgcp.h> |
| 37 | #include <openbsc/mgcp_internal.h> |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 38 | #include <openbsc/vty.h> |
| 39 | |
| 40 | #include <osmocom/core/application.h> |
| 41 | #include <osmocom/core/msgb.h> |
| 42 | #include <osmocom/core/talloc.h> |
| 43 | #include <osmocom/core/process.h> |
| 44 | #include <osmocom/core/select.h> |
| 45 | |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 46 | #include <osmocom/vty/telnet_interface.h> |
| 47 | #include <osmocom/vty/logging.h> |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 48 | |
| 49 | #include <osmocom/vty/command.h> |
| 50 | |
| 51 | #include "../../bscconfig.h" |
| 52 | |
| 53 | /* this is here for the vty... it will never be called */ |
| 54 | void subscr_put() { abort(); } |
| 55 | |
| 56 | #define _GNU_SOURCE |
| 57 | #include <getopt.h> |
| 58 | |
| 59 | #warning "Make use of the rtp proxy code" |
| 60 | |
| 61 | static struct mgcp_config *cfg; |
| 62 | static int reset_endpoints = 0; |
| 63 | static int daemonize = 0; |
| 64 | |
| 65 | const char *openbsc_copyright = |
| 66 | "Copyright (C) 2009-2010 Holger Freyther and On-Waves\r\n" |
| 67 | "Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\r\n" |
| 68 | "Dieter Spaar, Andreas Eversberg, Harald Welte\r\n\r\n" |
| 69 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
| 70 | "This is free software: you are free to change and redistribute it.\r\n" |
| 71 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
| 72 | |
| 73 | static char *config_file = "mgcp.cfg"; |
| 74 | |
| 75 | /* used by msgb and mgcp */ |
| 76 | void *tall_bsc_ctx = NULL; |
| 77 | |
| 78 | static void print_help() |
| 79 | { |
| 80 | printf("Some useful help...\n"); |
| 81 | printf(" -h --help is printing this text.\n"); |
| 82 | printf(" -c --config-file filename The config file to use.\n"); |
| 83 | } |
| 84 | |
| 85 | static void handle_options(int argc, char **argv) |
| 86 | { |
| 87 | while (1) { |
| 88 | int option_index = 0, c; |
| 89 | static struct option long_options[] = { |
| 90 | {"help", 0, 0, 'h'}, |
| 91 | {"config-file", 1, 0, 'c'}, |
| 92 | {"daemonize", 0, 0, 'D'}, |
| 93 | {"version", 0, 0, 'V'}, |
| 94 | {0, 0, 0, 0}, |
| 95 | }; |
| 96 | |
| 97 | c = getopt_long(argc, argv, "hc:VD", long_options, &option_index); |
| 98 | |
| 99 | if (c == -1) |
| 100 | break; |
| 101 | |
| 102 | switch(c) { |
| 103 | case 'h': |
| 104 | print_help(); |
| 105 | exit(0); |
| 106 | break; |
| 107 | case 'c': |
| 108 | config_file = talloc_strdup(tall_bsc_ctx, optarg); |
| 109 | break; |
| 110 | case 'V': |
| 111 | print_version(1); |
| 112 | exit(0); |
| 113 | break; |
| 114 | case 'D': |
| 115 | daemonize = 1; |
| 116 | break; |
| 117 | default: |
| 118 | /* ignore */ |
| 119 | break; |
| 120 | }; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /* simply remember this */ |
| 125 | static int mgcp_rsip_cb(struct mgcp_config *cfg) |
| 126 | { |
| 127 | reset_endpoints = 1; |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static int mgcp_change_cb(struct mgcp_trunk_config *cfg, int endpoint, int state) |
| 133 | { |
| 134 | if (state != MGCP_ENDP_MDCX) |
| 135 | return 0; |
| 136 | |
| 137 | mgcp_send_dummy(&cfg->endpoints[endpoint]); |
| 138 | return 0; |
| 139 | } |
| 140 | |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 141 | static int read_call_agent(struct osmo_fd *fd, unsigned int what) |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 142 | { |
| 143 | struct sockaddr_in addr; |
| 144 | socklen_t slen = sizeof(addr); |
| 145 | struct msgb *msg; |
| 146 | struct msgb *resp; |
| 147 | int i; |
| 148 | |
| 149 | msg = (struct msgb *) fd->data; |
| 150 | |
| 151 | /* read one less so we can use it as a \0 */ |
| 152 | int rc = recvfrom(cfg->gw_fd.bfd.fd, msg->data, msg->data_len - 1, 0, |
| 153 | (struct sockaddr *) &addr, &slen); |
| 154 | if (rc < 0) { |
| 155 | perror("Gateway failed to read"); |
| 156 | return -1; |
| 157 | } else if (slen > sizeof(addr)) { |
| 158 | fprintf(stderr, "Gateway received message from outerspace: %lu %d\n", |
| 159 | slen, sizeof(addr)); |
| 160 | return -1; |
| 161 | } |
| 162 | |
| 163 | /* handle message now */ |
| 164 | msg->l2h = msgb_put(msg, rc); |
| 165 | resp = mgcp_handle_message(cfg, msg); |
| 166 | msgb_reset(msg); |
| 167 | |
| 168 | if (resp) { |
| 169 | sendto(cfg->gw_fd.bfd.fd, resp->l2h, msgb_l2len(resp), 0, (struct sockaddr *) &addr, sizeof(addr)); |
| 170 | msgb_free(resp); |
| 171 | } |
| 172 | |
| 173 | if (reset_endpoints) { |
| 174 | LOGP(DMGCP, LOGL_NOTICE, "Asked to reset endpoints.\n"); |
| 175 | reset_endpoints = 0; |
| 176 | |
| 177 | /* is checking in_addr.s_addr == INADDR_LOOPBACK making it more secure? */ |
| 178 | for (i = 1; i < cfg->trunk.number_endpoints; ++i) |
| 179 | mgcp_free_endp(&cfg->trunk.endpoints[i]); |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
| 186 | |
| 187 | static struct vty_app_info vty_info = { |
| 188 | .name = "OpenBSC MGCP", |
| 189 | .version = PACKAGE_VERSION, |
| 190 | .go_parent_cb = bsc_vty_go_parent, |
| 191 | .is_config_node = bsc_vty_is_config_node, |
| 192 | }; |
| 193 | |
| 194 | int main(int argc, char **argv) |
| 195 | { |
| 196 | struct gsm_network dummy_network; |
| 197 | struct sockaddr_in addr; |
| 198 | int on = 1, rc; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 199 | |
| 200 | tall_bsc_ctx = talloc_named_const(NULL, 1, "mgcp-callagent"); |
| 201 | |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 202 | osmo_init_ignore_signals(); |
| 203 | osmo_init_logging(&log_info); |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 204 | |
| 205 | cfg = mgcp_config_alloc(); |
| 206 | if (!cfg) |
| 207 | return -1; |
| 208 | |
| 209 | vty_info.copyright = openbsc_copyright; |
| 210 | vty_init(&vty_info); |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 211 | logging_vty_add_cmds(&log_info); |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 212 | mgcp_vty_init(); |
| 213 | |
| 214 | handle_options(argc, argv); |
| 215 | |
| 216 | rc = mgcp_parse_config(config_file, cfg); |
| 217 | if (rc < 0) |
| 218 | return rc; |
| 219 | |
| 220 | rc = telnet_init(tall_bsc_ctx, &dummy_network, 4243); |
| 221 | if (rc < 0) |
| 222 | return rc; |
| 223 | |
| 224 | /* set some callbacks */ |
| 225 | cfg->reset_cb = mgcp_rsip_cb; |
| 226 | cfg->change_cb = mgcp_change_cb; |
| 227 | |
| 228 | /* we need to bind a socket */ |
| 229 | if (rc == 0) { |
| 230 | cfg->gw_fd.bfd.when = BSC_FD_READ; |
| 231 | cfg->gw_fd.bfd.cb = read_call_agent; |
| 232 | cfg->gw_fd.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 233 | if (cfg->gw_fd.bfd.fd < 0) { |
| 234 | perror("Gateway failed to listen"); |
| 235 | return -1; |
| 236 | } |
| 237 | |
| 238 | setsockopt(cfg->gw_fd.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 239 | |
| 240 | memset(&addr, 0, sizeof(addr)); |
| 241 | addr.sin_family = AF_INET; |
| 242 | addr.sin_port = htons(cfg->source_port); |
| 243 | inet_aton(cfg->source_addr, &addr.sin_addr); |
| 244 | |
| 245 | if (bind(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 246 | perror("Gateway failed to bind"); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | cfg->gw_fd.bfd.data = msgb_alloc(4096, "mgcp-msg"); |
| 251 | if (!cfg->gw_fd.bfd.data) { |
| 252 | fprintf(stderr, "Gateway memory error.\n"); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 257 | if (osmo_fd_register(&cfg->gw_fd.bfd) != 0) { |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 258 | LOGP(DMGCP, LOGL_FATAL, "Failed to register the fd\n"); |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | LOGP(DMGCP, LOGL_NOTICE, "Configured for MGCP.\n"); |
| 263 | } |
| 264 | |
| 265 | /* initialisation */ |
| 266 | srand(time(NULL)); |
| 267 | |
| 268 | if (daemonize) { |
| 269 | rc = osmo_daemonize(); |
| 270 | if (rc < 0) { |
| 271 | perror("Error during daemonize"); |
| 272 | exit(1); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* main loop */ |
| 277 | while (1) { |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 278 | osmo_select_main(0); |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |
| 282 | return 0; |
| 283 | } |