Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* A hackish minimal BSC (+MSC +HLR) implementation */ |
| 2 | |
| 3 | /* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 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> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 24 | #include <time.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <signal.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <sys/stat.h> |
| 29 | |
| 30 | #define _GNU_SOURCE |
| 31 | #include <getopt.h> |
| 32 | |
| 33 | #include <openbsc/db.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 34 | #include <openbsc/select.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 35 | #include <openbsc/debug.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 36 | #include <openbsc/e1_input.h> |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 37 | #include <openbsc/talloc.h> |
Harald Welte | d21adfe | 2009-11-30 19:37:18 +0100 | [diff] [blame] | 38 | #include <openbsc/signal.h> |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 39 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 40 | /* MCC and MNC for the Location Area Identifier */ |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 41 | struct gsm_network *bsc_gsmnet = 0; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 42 | static const char *database_name = "hlr.sqlite3"; |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 43 | static const char *config_file = "openbsc.cfg"; |
Harald Welte | 3c06207 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 44 | extern int ipacc_rtp_direct; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 45 | |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 46 | extern int bsc_bootstrap_network(int (*mmc_rev)(struct gsm_network *, int, void *), |
| 47 | const char *cfg_file); |
| 48 | extern int bsc_shutdown_net(struct gsm_network *net); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 49 | |
| 50 | static void create_pcap_file(char *file) |
| 51 | { |
| 52 | mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
| 53 | int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode); |
| 54 | |
| 55 | if (fd < 0) { |
| 56 | perror("Failed to open file for pcap"); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | e1_set_pcap_fd(fd); |
| 61 | } |
| 62 | |
| 63 | static void print_usage() |
| 64 | { |
| 65 | printf("Usage: bsc_hack\n"); |
| 66 | } |
| 67 | |
| 68 | static void print_help() |
| 69 | { |
| 70 | printf(" Some useful help...\n"); |
Harald Welte | 6286888 | 2009-08-08 16:12:58 +0200 | [diff] [blame] | 71 | printf(" -h --help this text\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 72 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 73 | printf(" -s --disable-color\n"); |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 74 | printf(" -c --config-file filename The config file to use.\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 75 | printf(" -l --database db-name The database to use\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 76 | printf(" -p --pcap file The filename of the pcap file\n"); |
Harald Welte | bea12fe | 2009-08-12 21:52:11 +0200 | [diff] [blame] | 77 | printf(" -T --timestamp Prefix every log line with a timestamp\n"); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static void handle_options(int argc, char** argv) |
| 81 | { |
| 82 | while (1) { |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 83 | int option_index = 0, c; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 84 | static struct option long_options[] = { |
| 85 | {"help", 0, 0, 'h'}, |
| 86 | {"debug", 1, 0, 'd'}, |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 87 | {"config-file", 1, 0, 'c'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 88 | {"disable-color", 0, 0, 's'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 89 | {"database", 1, 0, 'l'}, |
| 90 | {"authorize-everyone", 0, 0, 'a'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 91 | {"pcap", 1, 0, 'p'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 92 | {"timestamp", 0, 0, 'T'}, |
Harald Welte | 3c06207 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 93 | {"rtp-proxy", 0, 0, 'P'}, |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 94 | {0, 0, 0, 0} |
| 95 | }; |
| 96 | |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 97 | c = getopt_long(argc, argv, "hd:sl:ar:p:TPc:", |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 98 | long_options, &option_index); |
| 99 | if (c == -1) |
| 100 | break; |
| 101 | |
| 102 | switch (c) { |
| 103 | case 'h': |
| 104 | print_usage(); |
| 105 | print_help(); |
| 106 | exit(0); |
| 107 | case 's': |
| 108 | debug_use_color(0); |
| 109 | break; |
| 110 | case 'd': |
| 111 | debug_parse_category_mask(optarg); |
| 112 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 113 | case 'l': |
| 114 | database_name = strdup(optarg); |
| 115 | break; |
Holger Hans Peter Freyther | 4fa91d0 | 2009-08-10 08:39:27 +0200 | [diff] [blame] | 116 | case 'c': |
| 117 | config_file = strdup(optarg); |
| 118 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 119 | case 'p': |
| 120 | create_pcap_file(optarg); |
| 121 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 122 | case 'T': |
| 123 | debug_timestamp(1); |
| 124 | break; |
Harald Welte | 3c06207 | 2009-07-28 18:25:29 +0200 | [diff] [blame] | 125 | case 'P': |
| 126 | ipacc_rtp_direct = 0; |
| 127 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 128 | default: |
| 129 | /* ignore */ |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static void signal_handler(int signal) |
| 136 | { |
| 137 | fprintf(stdout, "signal %u received\n", signal); |
| 138 | |
| 139 | switch (signal) { |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 140 | case SIGINT: |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 141 | bsc_shutdown_net(bsc_gsmnet); |
Harald Welte | d21adfe | 2009-11-30 19:37:18 +0100 | [diff] [blame] | 142 | dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL); |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 143 | sleep(3); |
| 144 | exit(0); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 145 | break; |
Harald Welte | 4d5e6d5 | 2009-08-07 00:29:44 +0200 | [diff] [blame] | 146 | case SIGABRT: |
| 147 | /* in case of abort, we want to obtain a talloc report |
| 148 | * and then return to the caller, who will abort the process */ |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 149 | case SIGUSR1: |
| 150 | talloc_report_full(tall_bsc_ctx, stderr); |
| 151 | break; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 152 | default: |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | int main(int argc, char **argv) |
| 158 | { |
| 159 | int rc; |
| 160 | |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 161 | tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc"); |
Harald Welte (local) | 8751ee9 | 2009-08-15 02:30:58 +0200 | [diff] [blame] | 162 | talloc_ctx_init(); |
Harald Welte (local) | bd931f1 | 2009-08-13 13:52:14 +0200 | [diff] [blame] | 163 | on_dso_load_token(); |
Harald Welte (local) | 9571378 | 2009-08-16 13:18:51 +0200 | [diff] [blame] | 164 | on_dso_load_rrlp(); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 165 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 166 | /* parse options */ |
| 167 | handle_options(argc, argv); |
| 168 | |
| 169 | /* seed the PRNG */ |
| 170 | srand(time(NULL)); |
| 171 | |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 172 | if (db_init(database_name)) { |
| 173 | printf("DB: Failed to init database. Please check the option settings.\n"); |
| 174 | return -1; |
| 175 | } |
| 176 | printf("DB: Database initialized.\n"); |
| 177 | |
| 178 | if (db_prepare()) { |
| 179 | printf("DB: Failed to prepare database.\n"); |
| 180 | return -1; |
| 181 | } |
| 182 | printf("DB: Database prepared.\n"); |
| 183 | |
| 184 | rc = bsc_bootstrap_network(mncc_recv, config_file); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 185 | if (rc < 0) |
| 186 | exit(1); |
| 187 | |
Harald Welte | 3b71450 | 2009-08-06 17:43:50 +0200 | [diff] [blame] | 188 | signal(SIGINT, &signal_handler); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 189 | signal(SIGABRT, &signal_handler); |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 190 | signal(SIGUSR1, &signal_handler); |
Holger Hans Peter Freyther | 49b05d3 | 2009-11-24 20:06:41 +0100 | [diff] [blame] | 191 | signal(SIGPIPE, SIG_IGN); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 192 | |
| 193 | while (1) { |
Holger Hans Peter Freyther | cad370f | 2009-08-17 06:55:10 +0200 | [diff] [blame] | 194 | bsc_upqueue(bsc_gsmnet); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 195 | bsc_select_main(0); |
| 196 | } |
| 197 | } |