blob: ee37a611510fcf359292afaf53e57071f9a3efc3 [file] [log] [blame]
Harald Welte59b04682009-06-10 05:40:52 +08001/* 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 Welte59b04682009-06-10 05:40:52 +080024#include <time.h>
Harald Welte59b04682009-06-10 05:40:52 +080025#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 Welte59b04682009-06-10 05:40:52 +080034#include <openbsc/select.h>
Harald Welte59b04682009-06-10 05:40:52 +080035#include <openbsc/debug.h>
Harald Welte59b04682009-06-10 05:40:52 +080036#include <openbsc/e1_input.h>
Harald Weltea8379772009-06-20 22:36:41 +020037#include <openbsc/talloc.h>
Harald Welted21adfe2009-11-30 19:37:18 +010038#include <openbsc/signal.h>
Harald Weltea8379772009-06-20 22:36:41 +020039
Harald Welte59b04682009-06-10 05:40:52 +080040/* MCC and MNC for the Location Area Identifier */
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020041struct gsm_network *bsc_gsmnet = 0;
Harald Welte59b04682009-06-10 05:40:52 +080042static const char *database_name = "hlr.sqlite3";
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +020043static const char *config_file = "openbsc.cfg";
Harald Welte3c062072009-07-28 18:25:29 +020044extern int ipacc_rtp_direct;
Harald Welte59b04682009-06-10 05:40:52 +080045
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020046extern int bsc_bootstrap_network(int (*mmc_rev)(struct gsm_network *, int, void *),
47 const char *cfg_file);
48extern int bsc_shutdown_net(struct gsm_network *net);
Harald Welte59b04682009-06-10 05:40:52 +080049
50static 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
63static void print_usage()
64{
65 printf("Usage: bsc_hack\n");
66}
67
68static void print_help()
69{
70 printf(" Some useful help...\n");
Harald Welte62868882009-08-08 16:12:58 +020071 printf(" -h --help this text\n");
Harald Welte59b04682009-06-10 05:40:52 +080072 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
73 printf(" -s --disable-color\n");
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +020074 printf(" -c --config-file filename The config file to use.\n");
Harald Welte59b04682009-06-10 05:40:52 +080075 printf(" -l --database db-name The database to use\n");
Harald Welte59b04682009-06-10 05:40:52 +080076 printf(" -r --reject-cause number The reject cause for LOCATION UPDATING REJECT.\n");
77 printf(" -p --pcap file The filename of the pcap file\n");
Harald Weltebea12fe2009-08-12 21:52:11 +020078 printf(" -T --timestamp Prefix every log line with a timestamp\n");
Harald Welte59b04682009-06-10 05:40:52 +080079}
80
81static void handle_options(int argc, char** argv)
82{
83 while (1) {
Harald Weltea8379772009-06-20 22:36:41 +020084 int option_index = 0, c;
Harald Welte59b04682009-06-10 05:40:52 +080085 static struct option long_options[] = {
86 {"help", 0, 0, 'h'},
87 {"debug", 1, 0, 'd'},
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +020088 {"config-file", 1, 0, 'c'},
Harald Welte59b04682009-06-10 05:40:52 +080089 {"disable-color", 0, 0, 's'},
Harald Welte59b04682009-06-10 05:40:52 +080090 {"database", 1, 0, 'l'},
91 {"authorize-everyone", 0, 0, 'a'},
92 {"reject-cause", 1, 0, 'r'},
93 {"pcap", 1, 0, 'p'},
Harald Welte59b04682009-06-10 05:40:52 +080094 {"timestamp", 0, 0, 'T'},
Harald Welte3c062072009-07-28 18:25:29 +020095 {"rtp-proxy", 0, 0, 'P'},
Harald Welte59b04682009-06-10 05:40:52 +080096 {0, 0, 0, 0}
97 };
98
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020099 c = getopt_long(argc, argv, "hd:sl:ar:p:TPc:",
Harald Welte59b04682009-06-10 05:40:52 +0800100 long_options, &option_index);
101 if (c == -1)
102 break;
103
104 switch (c) {
105 case 'h':
106 print_usage();
107 print_help();
108 exit(0);
109 case 's':
110 debug_use_color(0);
111 break;
112 case 'd':
113 debug_parse_category_mask(optarg);
114 break;
Harald Welte59b04682009-06-10 05:40:52 +0800115 case 'l':
116 database_name = strdup(optarg);
117 break;
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +0200118 case 'c':
119 config_file = strdup(optarg);
120 break;
Harald Welte59b04682009-06-10 05:40:52 +0800121 case 'r':
122 gsm0408_set_reject_cause(atoi(optarg));
123 break;
124 case 'p':
125 create_pcap_file(optarg);
126 break;
Harald Welte59b04682009-06-10 05:40:52 +0800127 case 'T':
128 debug_timestamp(1);
129 break;
Harald Welte3c062072009-07-28 18:25:29 +0200130 case 'P':
131 ipacc_rtp_direct = 0;
132 break;
Harald Welte59b04682009-06-10 05:40:52 +0800133 default:
134 /* ignore */
135 break;
136 }
137 }
138}
139
140static void signal_handler(int signal)
141{
142 fprintf(stdout, "signal %u received\n", signal);
143
144 switch (signal) {
Harald Welte3b714502009-08-06 17:43:50 +0200145 case SIGINT:
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200146 bsc_shutdown_net(bsc_gsmnet);
Harald Welted21adfe2009-11-30 19:37:18 +0100147 dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
Harald Welte3b714502009-08-06 17:43:50 +0200148 sleep(3);
149 exit(0);
Harald Welte59b04682009-06-10 05:40:52 +0800150 break;
Harald Welte4d5e6d52009-08-07 00:29:44 +0200151 case SIGABRT:
152 /* in case of abort, we want to obtain a talloc report
153 * and then return to the caller, who will abort the process */
Harald Weltea8379772009-06-20 22:36:41 +0200154 case SIGUSR1:
155 talloc_report_full(tall_bsc_ctx, stderr);
156 break;
Harald Welte59b04682009-06-10 05:40:52 +0800157 default:
158 break;
159 }
160}
161
162int main(int argc, char **argv)
163{
164 int rc;
165
Harald Weltea8379772009-06-20 22:36:41 +0200166 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
Harald Welte (local)8751ee92009-08-15 02:30:58 +0200167 talloc_ctx_init();
Harald Welte (local)bd931f12009-08-13 13:52:14 +0200168 on_dso_load_token();
Harald Welte (local)95713782009-08-16 13:18:51 +0200169 on_dso_load_rrlp();
Harald Weltea8379772009-06-20 22:36:41 +0200170
Harald Welte59b04682009-06-10 05:40:52 +0800171 /* parse options */
172 handle_options(argc, argv);
173
174 /* seed the PRNG */
175 srand(time(NULL));
176
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200177 if (db_init(database_name)) {
178 printf("DB: Failed to init database. Please check the option settings.\n");
179 return -1;
180 }
181 printf("DB: Database initialized.\n");
182
183 if (db_prepare()) {
184 printf("DB: Failed to prepare database.\n");
185 return -1;
186 }
187 printf("DB: Database prepared.\n");
188
189 rc = bsc_bootstrap_network(mncc_recv, config_file);
Harald Welte59b04682009-06-10 05:40:52 +0800190 if (rc < 0)
191 exit(1);
192
Harald Welte3b714502009-08-06 17:43:50 +0200193 signal(SIGINT, &signal_handler);
Harald Welte59b04682009-06-10 05:40:52 +0800194 signal(SIGABRT, &signal_handler);
Harald Weltea8379772009-06-20 22:36:41 +0200195 signal(SIGUSR1, &signal_handler);
Harald Welte59b04682009-06-10 05:40:52 +0800196
197 while (1) {
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200198 bsc_upqueue(bsc_gsmnet);
Harald Welte59b04682009-06-10 05:40:52 +0800199 bsc_select_main(0);
200 }
201}