Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1 | /* NS-over-IP proxy */ |
| 2 | |
| 3 | /* (C) 2010 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2010 by On Waves |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <getopt.h> |
| 28 | #include <errno.h> |
| 29 | #include <sys/fcntl.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <netinet/in.h> |
| 34 | #include <arpa/inet.h> |
| 35 | |
| 36 | #include <osmocore/talloc.h> |
| 37 | #include <osmocore/select.h> |
| 38 | |
| 39 | #include <openbsc/signal.h> |
| 40 | #include <openbsc/debug.h> |
| 41 | #include <openbsc/gprs_ns.h> |
| 42 | #include <openbsc/telnet_interface.h> |
| 43 | #include <openbsc/vty.h> |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 44 | #include <openbsc/gb_proxy.h> |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 45 | |
| 46 | #include "../bscconfig.h" |
| 47 | |
| 48 | /* this is here for the vty... it will never be called */ |
| 49 | void subscr_put() { abort(); } |
| 50 | |
| 51 | #define _GNU_SOURCE |
| 52 | #include <getopt.h> |
| 53 | |
| 54 | void *tall_bsc_ctx; |
| 55 | |
| 56 | struct gprs_ns_inst *gbprox_nsi; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 57 | |
| 58 | const char *openbsc_version = "Osmocom NSIP Proxy " PACKAGE_VERSION; |
| 59 | const char *openbsc_copyright = |
| 60 | "Copyright (C) 2010 Harald Welte and On-Waves\n" |
| 61 | "Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\n" |
| 62 | "Dieter Spaar, Andreas Eversberg, Holger Freyther\n\n" |
| 63 | "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>\n" |
| 64 | "This is free software: you are free to change and redistribute it.\n" |
| 65 | "There is NO WARRANTY, to the extent permitted by law.\n"; |
| 66 | |
Harald Welte | 55fe055 | 2010-05-01 16:48:27 +0200 | [diff] [blame] | 67 | static char *config_file = "osmo_gbproxy.cfg"; |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 68 | static struct gbproxy_config gbcfg; |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 69 | |
| 70 | /* Pointer to the SGSN peer */ |
| 71 | extern struct gbprox_peer *gbprox_peer_sgsn; |
| 72 | |
| 73 | /* call-back function for the NS protocol */ |
| 74 | static int proxy_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, |
| 75 | struct msgb *msg, u_int16_t bvci) |
| 76 | { |
| 77 | int rc = 0; |
| 78 | |
| 79 | switch (event) { |
| 80 | case GPRS_NS_EVT_UNIT_DATA: |
| 81 | rc = gbprox_rcvmsg(msg, nsvc, bvci); |
| 82 | break; |
| 83 | default: |
| 84 | LOGP(DGPRS, LOGL_ERROR, "SGSN: Unknown event %u from NS\n", event); |
| 85 | if (msg) |
| 86 | talloc_free(msg); |
| 87 | rc = -EIO; |
| 88 | break; |
| 89 | } |
| 90 | return rc; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | int main(int argc, char **argv) |
| 95 | { |
| 96 | struct gsm_network dummy_network; |
| 97 | struct log_target *stderr_target; |
| 98 | struct sockaddr_in sin; |
| 99 | int rc; |
| 100 | |
| 101 | tall_bsc_ctx = talloc_named_const(NULL, 0, "nsip_proxy"); |
| 102 | |
| 103 | log_init(&log_info); |
| 104 | stderr_target = log_target_create_stderr(); |
| 105 | log_add_target(stderr_target); |
| 106 | log_set_all_filter(stderr_target, 1); |
| 107 | |
| 108 | telnet_init(&dummy_network, 4244); |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 109 | rc = gbproxy_parse_config(config_file, &gbcfg); |
| 110 | if (rc < 0) { |
| 111 | LOGP(DGPRS, LOGL_FATAL, "Cannot parse config file\n"); |
| 112 | exit(2); |
| 113 | } |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 114 | |
| 115 | gbprox_nsi = gprs_ns_instantiate(&proxy_ns_cb); |
| 116 | if (!gbprox_nsi) { |
| 117 | LOGP(DGPRS, LOGL_ERROR, "Unable to instantiate NS\n"); |
| 118 | exit(1); |
| 119 | } |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 120 | gbcfg.nsi = gbprox_nsi; |
| 121 | nsip_listen(gbprox_nsi, gbcfg.nsip_listen_port); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 122 | |
| 123 | /* 'establish' the outgoing connection to the SGSN */ |
Harald Welte | 3634543 | 2010-04-30 20:42:42 +0200 | [diff] [blame] | 124 | sin.sin_family = AF_INET; |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 125 | sin.sin_port = htons(gbcfg.nsip_sgsn_port); |
| 126 | sin.sin_addr.s_addr = htonl(gbcfg.nsip_sgsn_ip); |
| 127 | gbprox_peer_sgsn = nsip_connect(gbprox_nsi, &sin, |
| 128 | gbcfg.nsip_sgsn_nsei, |
| 129 | gbcfg.nsip_sgsn_nsvci); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 130 | |
| 131 | while (1) { |
| 132 | rc = bsc_select_main(0); |
| 133 | if (rc < 0) |
| 134 | exit(3); |
| 135 | } |
| 136 | |
| 137 | exit(0); |
| 138 | } |
| 139 | |
| 140 | struct gsm_network; |
| 141 | int bsc_vty_init(struct gsm_network *dummy) |
| 142 | { |
| 143 | cmd_init(1); |
| 144 | vty_init(); |
| 145 | |
| 146 | openbsc_vty_add_cmds(); |
Harald Welte | a167efb | 2010-04-30 21:49:24 +0200 | [diff] [blame] | 147 | gbproxy_vty_init(); |
Harald Welte | d1991e7 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |