Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 2 | /* The main method to drive it as a standalone process */ |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 3 | |
| 4 | /* |
Holger Hans Peter Freyther | 26bd848 | 2011-06-10 00:04:55 +0200 | [diff] [blame] | 5 | * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2011 by On-Waves |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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 |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 18 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 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/>. |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 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> |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 33 | |
| 34 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 35 | #include <openbsc/gsm_data.h> |
| 36 | #include <openbsc/mgcp.h> |
| 37 | #include <openbsc/mgcp_internal.h> |
| 38 | #include <openbsc/vty.h> |
| 39 | |
| 40 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 41 | #include <osmocom/core/msgb.h> |
| 42 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | dd5fff4 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 43 | #include <osmocom/core/select.h> |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 44 | |
Harald Welte | 30f93fb | 2011-02-19 16:48:17 +0100 | [diff] [blame] | 45 | #include <osmocom/vty/telnet_interface.h> |
| 46 | #include <osmocom/vty/logging.h> |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 47 | |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 48 | #include <osmocom/vty/command.h> |
Holger Hans Peter Freyther | 9856ee2 | 2010-03-30 12:43:53 +0200 | [diff] [blame] | 49 | |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 50 | #include "../../bscconfig.h" |
| 51 | |
Jacob Erlbeck | 9fac800 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 52 | #ifdef BUILD_MGCP_TRANSCODING |
| 53 | #include "mgcp_transcode.h" |
| 54 | #endif |
| 55 | |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 56 | /* this is here for the vty... it will never be called */ |
| 57 | void subscr_put() { abort(); } |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 58 | |
| 59 | #define _GNU_SOURCE |
| 60 | #include <getopt.h> |
| 61 | |
| 62 | #warning "Make use of the rtp proxy code" |
| 63 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 64 | static struct mgcp_config *cfg; |
Holger Hans Peter Freyther | 26bd848 | 2011-06-10 00:04:55 +0200 | [diff] [blame] | 65 | static struct mgcp_trunk_config *reset_trunk; |
Holger Hans Peter Freyther | 3b812d0 | 2010-03-31 07:01:35 +0200 | [diff] [blame] | 66 | static int reset_endpoints = 0; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 67 | static int daemonize = 0; |
Holger Hans Peter Freyther | 3b812d0 | 2010-03-31 07:01:35 +0200 | [diff] [blame] | 68 | |
Holger Hans Peter Freyther | 7113514 | 2010-03-29 08:47:44 +0200 | [diff] [blame] | 69 | const char *openbsc_copyright = |
Holger Hans Peter Freyther | 260646d | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 70 | "Copyright (C) 2009-2010 Holger Freyther and On-Waves\r\n" |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 71 | "Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\r\n" |
Holger Hans Peter Freyther | 260646d | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 72 | "Dieter Spaar, Andreas Eversberg, Harald Welte\r\n\r\n" |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 73 | "License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n" |
Holger Hans Peter Freyther | 260646d | 2010-09-11 13:32:30 +0800 | [diff] [blame] | 74 | "This is free software: you are free to change and redistribute it.\r\n" |
| 75 | "There is NO WARRANTY, to the extent permitted by law.\r\n"; |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 76 | |
| 77 | static char *config_file = "mgcp.cfg"; |
| 78 | |
| 79 | /* used by msgb and mgcp */ |
| 80 | void *tall_bsc_ctx = NULL; |
| 81 | |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 82 | static void print_help() |
| 83 | { |
| 84 | printf("Some useful help...\n"); |
| 85 | printf(" -h --help is printing this text.\n"); |
| 86 | printf(" -c --config-file filename The config file to use.\n"); |
Holger Hans Peter Freyther | 7d7e298 | 2012-09-20 16:04:54 +0200 | [diff] [blame] | 87 | printf(" -s --disable-color\n"); |
Harald Welte | d1bba8b | 2012-01-27 13:21:46 +0100 | [diff] [blame] | 88 | printf(" -D --daemonize Fork the process into a background daemon\n"); |
| 89 | printf(" -V --version Print the version number\n"); |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 92 | static void handle_options(int argc, char **argv) |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 93 | { |
| 94 | while (1) { |
| 95 | int option_index = 0, c; |
| 96 | static struct option long_options[] = { |
| 97 | {"help", 0, 0, 'h'}, |
| 98 | {"config-file", 1, 0, 'c'}, |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 99 | {"daemonize", 0, 0, 'D'}, |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 100 | {"version", 0, 0, 'V'}, |
Holger Hans Peter Freyther | 7d7e298 | 2012-09-20 16:04:54 +0200 | [diff] [blame] | 101 | {"disable-color", 0, 0, 's'}, |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 102 | {0, 0, 0, 0}, |
| 103 | }; |
| 104 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 105 | c = getopt_long(argc, argv, "hc:VD", long_options, &option_index); |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 106 | |
| 107 | if (c == -1) |
| 108 | break; |
| 109 | |
| 110 | switch(c) { |
| 111 | case 'h': |
| 112 | print_help(); |
| 113 | exit(0); |
| 114 | break; |
| 115 | case 'c': |
| 116 | config_file = talloc_strdup(tall_bsc_ctx, optarg); |
| 117 | break; |
Holger Hans Peter Freyther | 7d7e298 | 2012-09-20 16:04:54 +0200 | [diff] [blame] | 118 | case 's': |
| 119 | log_set_use_color(osmo_stderr_target, 0); |
| 120 | break; |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 121 | case 'V': |
Harald Welte | 10c29f6 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 122 | print_version(1); |
Harald Welte | fa13cad | 2010-03-23 00:09:32 +0800 | [diff] [blame] | 123 | exit(0); |
| 124 | break; |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 125 | case 'D': |
| 126 | daemonize = 1; |
| 127 | break; |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 128 | default: |
| 129 | /* ignore */ |
| 130 | break; |
| 131 | }; |
| 132 | } |
| 133 | } |
| 134 | |
Holger Hans Peter Freyther | 3b812d0 | 2010-03-31 07:01:35 +0200 | [diff] [blame] | 135 | /* simply remember this */ |
Holger Hans Peter Freyther | 26bd848 | 2011-06-10 00:04:55 +0200 | [diff] [blame] | 136 | static int mgcp_rsip_cb(struct mgcp_trunk_config *tcfg) |
Holger Hans Peter Freyther | 3b812d0 | 2010-03-31 07:01:35 +0200 | [diff] [blame] | 137 | { |
| 138 | reset_endpoints = 1; |
Holger Hans Peter Freyther | 26bd848 | 2011-06-10 00:04:55 +0200 | [diff] [blame] | 139 | reset_trunk = tcfg; |
Holger Hans Peter Freyther | 3b812d0 | 2010-03-31 07:01:35 +0200 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 144 | static int read_call_agent(struct osmo_fd *fd, unsigned int what) |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 145 | { |
| 146 | struct sockaddr_in addr; |
| 147 | socklen_t slen = sizeof(addr); |
| 148 | struct msgb *msg; |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 149 | struct msgb *resp; |
Holger Hans Peter Freyther | 3b812d0 | 2010-03-31 07:01:35 +0200 | [diff] [blame] | 150 | int i; |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 151 | |
| 152 | msg = (struct msgb *) fd->data; |
| 153 | |
| 154 | /* read one less so we can use it as a \0 */ |
Holger Hans Peter Freyther | 5d15019 | 2010-09-18 20:40:22 +0800 | [diff] [blame] | 155 | int rc = recvfrom(cfg->gw_fd.bfd.fd, msg->data, msg->data_len - 1, 0, |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 156 | (struct sockaddr *) &addr, &slen); |
| 157 | if (rc < 0) { |
| 158 | perror("Gateway failed to read"); |
| 159 | return -1; |
| 160 | } else if (slen > sizeof(addr)) { |
Holger Hans Peter Freyther | 5a65444 | 2012-03-16 12:18:39 +0100 | [diff] [blame] | 161 | fprintf(stderr, "Gateway received message from outerspace: %zu %zu\n", |
| 162 | slen, sizeof(addr)); |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 163 | return -1; |
| 164 | } |
| 165 | |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 166 | /* handle message now */ |
| 167 | msg->l2h = msgb_put(msg, rc); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 168 | resp = mgcp_handle_message(cfg, msg); |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 169 | msgb_reset(msg); |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 170 | |
| 171 | if (resp) { |
Holger Hans Peter Freyther | 5d15019 | 2010-09-18 20:40:22 +0800 | [diff] [blame] | 172 | sendto(cfg->gw_fd.bfd.fd, resp->l2h, msgb_l2len(resp), 0, (struct sockaddr *) &addr, sizeof(addr)); |
Holger Hans Peter Freyther | ad4e339 | 2010-02-03 11:03:45 +0100 | [diff] [blame] | 173 | msgb_free(resp); |
| 174 | } |
Holger Hans Peter Freyther | 3b812d0 | 2010-03-31 07:01:35 +0200 | [diff] [blame] | 175 | |
| 176 | if (reset_endpoints) { |
Holger Hans Peter Freyther | 26bd848 | 2011-06-10 00:04:55 +0200 | [diff] [blame] | 177 | LOGP(DMGCP, LOGL_NOTICE, |
| 178 | "Asked to reset endpoints: %d/%d\n", |
| 179 | reset_trunk->trunk_nr, reset_trunk->trunk_type); |
Holger Hans Peter Freyther | 3b812d0 | 2010-03-31 07:01:35 +0200 | [diff] [blame] | 180 | reset_endpoints = 0; |
| 181 | |
| 182 | /* is checking in_addr.s_addr == INADDR_LOOPBACK making it more secure? */ |
Holger Hans Peter Freyther | 26bd848 | 2011-06-10 00:04:55 +0200 | [diff] [blame] | 183 | for (i = 1; i < reset_trunk->number_endpoints; ++i) |
| 184 | mgcp_free_endp(&reset_trunk->endpoints[i]); |
Holger Hans Peter Freyther | 3b812d0 | 2010-03-31 07:01:35 +0200 | [diff] [blame] | 185 | } |
| 186 | |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
Holger Hans Peter Freyther | 3538c77 | 2010-06-08 16:11:06 +0800 | [diff] [blame] | 190 | extern enum node_type bsc_vty_go_parent(struct vty *vty); |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 191 | |
| 192 | static struct vty_app_info vty_info = { |
| 193 | .name = "OpenBSC MGCP", |
| 194 | .version = PACKAGE_VERSION, |
| 195 | .go_parent_cb = bsc_vty_go_parent, |
Holger Hans Peter Freyther | ba289d0 | 2010-09-04 11:00:01 +0800 | [diff] [blame] | 196 | .is_config_node = bsc_vty_is_config_node, |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 197 | }; |
Holger Hans Peter Freyther | 089c7f5 | 2010-01-13 23:36:53 +0100 | [diff] [blame] | 198 | |
Holger Hans Peter Freyther | dfd61a3 | 2010-09-18 06:44:24 +0800 | [diff] [blame] | 199 | int main(int argc, char **argv) |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 200 | { |
Holger Hans Peter Freyther | 3adc628 | 2009-11-19 15:03:39 +0100 | [diff] [blame] | 201 | struct gsm_network dummy_network; |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 202 | struct sockaddr_in addr; |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 203 | int on = 1, rc; |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 204 | |
| 205 | tall_bsc_ctx = talloc_named_const(NULL, 1, "mgcp-callagent"); |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 206 | |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 207 | osmo_init_ignore_signals(); |
| 208 | osmo_init_logging(&log_info); |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 209 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 210 | cfg = mgcp_config_alloc(); |
| 211 | if (!cfg) |
| 212 | return -1; |
| 213 | |
Jacob Erlbeck | 9fac800 | 2014-03-13 14:25:51 +0100 | [diff] [blame] | 214 | #ifdef BUILD_MGCP_TRANSCODING |
| 215 | cfg->setup_rtp_processing_cb = &mgcp_transcoding_setup; |
| 216 | cfg->rtp_processing_cb = &mgcp_transcoding_process_rtp; |
| 217 | cfg->get_net_downlink_format_cb = &mgcp_transcoding_net_downlink_format; |
| 218 | #endif |
| 219 | |
Harald Welte | 778c4b9 | 2010-05-25 23:31:39 +0200 | [diff] [blame] | 220 | vty_info.copyright = openbsc_copyright; |
| 221 | vty_init(&vty_info); |
Pablo Neira Ayuso | 36ad9a4 | 2011-03-09 13:36:32 +0100 | [diff] [blame] | 222 | logging_vty_add_cmds(&log_info); |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 223 | mgcp_vty_init(); |
Harald Welte | 10c29f6 | 2010-05-16 19:20:24 +0200 | [diff] [blame] | 224 | |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 225 | handle_options(argc, argv); |
| 226 | |
Pablo Neira Ayuso | a4e3694 | 2013-08-02 21:14:14 +0200 | [diff] [blame] | 227 | rc = mgcp_parse_config(config_file, cfg, MGCP_BSC); |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 228 | if (rc < 0) |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 229 | return rc; |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 230 | |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 231 | rc = telnet_init(tall_bsc_ctx, &dummy_network, 4243); |
| 232 | if (rc < 0) |
| 233 | return rc; |
| 234 | |
Holger Hans Peter Freyther | 3b812d0 | 2010-03-31 07:01:35 +0200 | [diff] [blame] | 235 | /* set some callbacks */ |
| 236 | cfg->reset_cb = mgcp_rsip_cb; |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 237 | |
Holger Hans Peter Freyther | 26143c8 | 2010-02-03 08:50:33 +0100 | [diff] [blame] | 238 | /* we need to bind a socket */ |
| 239 | if (rc == 0) { |
Holger Hans Peter Freyther | 5d15019 | 2010-09-18 20:40:22 +0800 | [diff] [blame] | 240 | cfg->gw_fd.bfd.when = BSC_FD_READ; |
| 241 | cfg->gw_fd.bfd.cb = read_call_agent; |
| 242 | cfg->gw_fd.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 243 | if (cfg->gw_fd.bfd.fd < 0) { |
Holger Hans Peter Freyther | c8b831a | 2010-01-05 12:21:36 +0100 | [diff] [blame] | 244 | perror("Gateway failed to listen"); |
| 245 | return -1; |
| 246 | } |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 247 | |
Holger Hans Peter Freyther | 5d15019 | 2010-09-18 20:40:22 +0800 | [diff] [blame] | 248 | setsockopt(cfg->gw_fd.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 249 | |
Holger Hans Peter Freyther | c8b831a | 2010-01-05 12:21:36 +0100 | [diff] [blame] | 250 | memset(&addr, 0, sizeof(addr)); |
| 251 | addr.sin_family = AF_INET; |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 252 | addr.sin_port = htons(cfg->source_port); |
| 253 | inet_aton(cfg->source_addr, &addr.sin_addr); |
Holger Hans Peter Freyther | c8b831a | 2010-01-05 12:21:36 +0100 | [diff] [blame] | 254 | |
Holger Hans Peter Freyther | 5d15019 | 2010-09-18 20:40:22 +0800 | [diff] [blame] | 255 | if (bind(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
Holger Hans Peter Freyther | c8b831a | 2010-01-05 12:21:36 +0100 | [diff] [blame] | 256 | perror("Gateway failed to bind"); |
| 257 | return -1; |
| 258 | } |
| 259 | |
Holger Hans Peter Freyther | 5d15019 | 2010-09-18 20:40:22 +0800 | [diff] [blame] | 260 | cfg->gw_fd.bfd.data = msgb_alloc(4096, "mgcp-msg"); |
| 261 | if (!cfg->gw_fd.bfd.data) { |
Holger Hans Peter Freyther | c8b831a | 2010-01-05 12:21:36 +0100 | [diff] [blame] | 262 | fprintf(stderr, "Gateway memory error.\n"); |
| 263 | return -1; |
| 264 | } |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 265 | |
Harald Welte | 2d166e3 | 2012-01-27 00:38:59 +0100 | [diff] [blame] | 266 | if (cfg->call_agent_addr) { |
| 267 | addr.sin_port = htons(2727); |
| 268 | inet_aton(cfg->call_agent_addr, &addr.sin_addr); |
| 269 | if (connect(cfg->gw_fd.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 270 | LOGP(DMGCP, LOGL_ERROR, "Failed to connect to: '%s'. errno: %d\n", |
| 271 | cfg->call_agent_addr, errno); |
| 272 | close(cfg->gw_fd.bfd.fd); |
| 273 | cfg->gw_fd.bfd.fd = -1; |
| 274 | return -1; |
| 275 | } |
| 276 | } |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 277 | |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 278 | if (osmo_fd_register(&cfg->gw_fd.bfd) != 0) { |
Holger Hans Peter Freyther | e08796b | 2010-03-31 06:16:45 +0200 | [diff] [blame] | 279 | LOGP(DMGCP, LOGL_FATAL, "Failed to register the fd\n"); |
Holger Hans Peter Freyther | c8b831a | 2010-01-05 12:21:36 +0100 | [diff] [blame] | 280 | return -1; |
| 281 | } |
Holger Hans Peter Freyther | a5a1dde | 2010-01-05 12:25:25 +0100 | [diff] [blame] | 282 | |
Holger Hans Peter Freyther | e08796b | 2010-03-31 06:16:45 +0200 | [diff] [blame] | 283 | LOGP(DMGCP, LOGL_NOTICE, "Configured for MGCP.\n"); |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* initialisation */ |
| 287 | srand(time(NULL)); |
| 288 | |
Harald Welte | c4ae176 | 2010-08-25 19:43:54 +0200 | [diff] [blame] | 289 | if (daemonize) { |
| 290 | rc = osmo_daemonize(); |
| 291 | if (rc < 0) { |
| 292 | perror("Error during daemonize"); |
| 293 | exit(1); |
| 294 | } |
| 295 | } |
| 296 | |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 297 | /* main loop */ |
| 298 | while (1) { |
Pablo Neira Ayuso | 04d24cd | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 299 | osmo_select_main(0); |
Holger Hans Peter Freyther | 9a26b7c | 2009-10-09 07:08:11 +0200 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | |
| 303 | return 0; |
| 304 | } |