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> |
| 35 | #include <osmocore/msgb.h> |
| 36 | #include <osmocore/talloc.h> |
| 37 | #include <osmocore/process.h> |
| 38 | #include <openbsc/gsm_data.h> |
| 39 | #include <osmocore/select.h> |
| 40 | #include <openbsc/mgcp.h> |
| 41 | #include <openbsc/mgcp_internal.h> |
| 42 | #include <osmocom/vty/telnet_interface.h> |
| 43 | #include <osmocom/vty/logging.h> |
| 44 | #include <openbsc/vty.h> |
| 45 | |
| 46 | #include <osmocom/vty/command.h> |
| 47 | |
| 48 | #include "../../bscconfig.h" |
| 49 | |
| 50 | /* this is here for the vty... it will never be called */ |
| 51 | void subscr_put() { abort(); } |
| 52 | |
| 53 | #define _GNU_SOURCE |
| 54 | #include <getopt.h> |
| 55 | |
| 56 | #warning "Make use of the rtp proxy code" |
| 57 | |
| 58 | static struct mgcp_config *cfg; |
| 59 | static int reset_endpoints = 0; |
| 60 | static int daemonize = 0; |
| 61 | |
| 62 | const char *openbsc_copyright = |
| 63 | "Copyright (C) 2009-2010 Holger Freyther and On-Waves\r\n" |
| 64 | "Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\r\n" |
| 65 | "Dieter Spaar, Andreas Eversberg, Harald Welte\r\n\r\n" |
| 66 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
| 67 | "This is free software: you are free to change and redistribute it.\r\n" |
| 68 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
| 69 | |
| 70 | static char *config_file = "mgcp.cfg"; |
| 71 | |
| 72 | /* used by msgb and mgcp */ |
| 73 | void *tall_bsc_ctx = NULL; |
| 74 | |
| 75 | static void print_help() |
| 76 | { |
| 77 | printf("Some useful help...\n"); |
| 78 | printf(" -h --help is printing this text.\n"); |
| 79 | printf(" -c --config-file filename The config file to use.\n"); |
| 80 | } |
| 81 | |
| 82 | static void handle_options(int argc, char **argv) |
| 83 | { |
| 84 | while (1) { |
| 85 | int option_index = 0, c; |
| 86 | static struct option long_options[] = { |
| 87 | {"help", 0, 0, 'h'}, |
| 88 | {"config-file", 1, 0, 'c'}, |
| 89 | {"daemonize", 0, 0, 'D'}, |
| 90 | {"version", 0, 0, 'V'}, |
| 91 | {0, 0, 0, 0}, |
| 92 | }; |
| 93 | |
| 94 | c = getopt_long(argc, argv, "hc:VD", long_options, &option_index); |
| 95 | |
| 96 | if (c == -1) |
| 97 | break; |
| 98 | |
| 99 | switch(c) { |
| 100 | case 'h': |
| 101 | print_help(); |
| 102 | exit(0); |
| 103 | break; |
| 104 | case 'c': |
| 105 | config_file = talloc_strdup(tall_bsc_ctx, optarg); |
| 106 | break; |
| 107 | case 'V': |
| 108 | print_version(1); |
| 109 | exit(0); |
| 110 | break; |
| 111 | case 'D': |
| 112 | daemonize = 1; |
| 113 | break; |
| 114 | default: |
| 115 | /* ignore */ |
| 116 | break; |
| 117 | }; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* simply remember this */ |
| 122 | static int mgcp_rsip_cb(struct mgcp_config *cfg) |
| 123 | { |
| 124 | reset_endpoints = 1; |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static int mgcp_change_cb(struct mgcp_trunk_config *cfg, int endpoint, int state) |
| 130 | { |
| 131 | if (state != MGCP_ENDP_MDCX) |
| 132 | return 0; |
| 133 | |
| 134 | mgcp_send_dummy(&cfg->endpoints[endpoint]); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int read_call_agent(struct bsc_fd *fd, unsigned int what) |
| 139 | { |
| 140 | struct sockaddr_in addr; |
| 141 | socklen_t slen = sizeof(addr); |
| 142 | struct msgb *msg; |
| 143 | struct msgb *resp; |
| 144 | int i; |
| 145 | |
| 146 | msg = (struct msgb *) fd->data; |
| 147 | |
| 148 | /* read one less so we can use it as a \0 */ |
| 149 | int rc = recvfrom(cfg->gw_fd.bfd.fd, msg->data, msg->data_len - 1, 0, |
| 150 | (struct sockaddr *) &addr, &slen); |
| 151 | if (rc < 0) { |
| 152 | perror("Gateway failed to read"); |
| 153 | return -1; |
| 154 | } else if (slen > sizeof(addr)) { |
| 155 | fprintf(stderr, "Gateway received message from outerspace: %lu %d\n", |
| 156 | slen, sizeof(addr)); |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | /* handle message now */ |
| 161 | msg->l2h = msgb_put(msg, rc); |
| 162 | resp = mgcp_handle_message(cfg, msg); |
| 163 | msgb_reset(msg); |
| 164 | |
| 165 | if (resp) { |
| 166 | sendto(cfg->gw_fd.bfd.fd, resp->l2h, msgb_l2len(resp), 0, (struct sockaddr *) &addr, sizeof(addr)); |
| 167 | msgb_free(resp); |
| 168 | } |
| 169 | |
| 170 | if (reset_endpoints) { |
| 171 | LOGP(DMGCP, LOGL_NOTICE, "Asked to reset endpoints.\n"); |
| 172 | reset_endpoints = 0; |
| 173 | |
| 174 | /* is checking in_addr.s_addr == INADDR_LOOPBACK making it more secure? */ |
| 175 | for (i = 1; i < cfg->trunk.number_endpoints; ++i) |
| 176 | mgcp_free_endp(&cfg->trunk.endpoints[i]); |
| 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
| 183 | |
| 184 | static struct vty_app_info vty_info = { |
| 185 | .name = "OpenBSC MGCP", |
| 186 | .version = PACKAGE_VERSION, |
| 187 | .go_parent_cb = bsc_vty_go_parent, |
| 188 | .is_config_node = bsc_vty_is_config_node, |
| 189 | }; |
| 190 | |
| 191 | int main(int argc, char **argv) |
| 192 | { |
| 193 | struct gsm_network dummy_network; |
| 194 | struct sockaddr_in addr; |
| 195 | int on = 1, rc; |
| 196 | struct log_target *stderr_target; |
| 197 | |
| 198 | tall_bsc_ctx = talloc_named_const(NULL, 1, "mgcp-callagent"); |
| 199 | |
| 200 | log_init(&log_info); |
| 201 | stderr_target = log_target_create_stderr(); |
| 202 | log_add_target(stderr_target); |
| 203 | log_set_all_filter(stderr_target, 1); |
| 204 | |
| 205 | cfg = mgcp_config_alloc(); |
| 206 | if (!cfg) |
| 207 | return -1; |
| 208 | |
| 209 | vty_info.copyright = openbsc_copyright; |
| 210 | vty_init(&vty_info); |
| 211 | logging_vty_add_cmds(); |
| 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 | |
| 257 | if (bsc_register_fd(&cfg->gw_fd.bfd) != 0) { |
| 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) { |
| 278 | bsc_select_main(0); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | return 0; |
| 283 | } |