blob: c256f864e308a6e85258c9765e57a7b328f395ee [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>
38
Harald Welte59b04682009-06-10 05:40:52 +080039/* MCC and MNC for the Location Area Identifier */
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020040struct gsm_network *bsc_gsmnet = 0;
Harald Welte59b04682009-06-10 05:40:52 +080041static const char *database_name = "hlr.sqlite3";
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +020042static const char *config_file = "openbsc.cfg";
Harald Welte3c062072009-07-28 18:25:29 +020043extern int ipacc_rtp_direct;
Harald Welte59b04682009-06-10 05:40:52 +080044
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020045extern int bsc_bootstrap_network(int (*mmc_rev)(struct gsm_network *, int, void *),
46 const char *cfg_file);
47extern int bsc_shutdown_net(struct gsm_network *net);
Harald Welte59b04682009-06-10 05:40:52 +080048
49static void create_pcap_file(char *file)
50{
51 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
52 int fd = open(file, O_WRONLY|O_TRUNC|O_CREAT, mode);
53
54 if (fd < 0) {
55 perror("Failed to open file for pcap");
56 return;
57 }
58
59 e1_set_pcap_fd(fd);
60}
61
62static void print_usage()
63{
64 printf("Usage: bsc_hack\n");
65}
66
67static void print_help()
68{
69 printf(" Some useful help...\n");
Harald Welte62868882009-08-08 16:12:58 +020070 printf(" -h --help this text\n");
Harald Welte59b04682009-06-10 05:40:52 +080071 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
72 printf(" -s --disable-color\n");
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +020073 printf(" -c --config-file filename The config file to use.\n");
Harald Welte59b04682009-06-10 05:40:52 +080074 printf(" -l --database db-name The database to use\n");
Harald Welte59b04682009-06-10 05:40:52 +080075 printf(" -r --reject-cause number The reject cause for LOCATION UPDATING REJECT.\n");
76 printf(" -p --pcap file The filename of the pcap file\n");
Harald Weltebea12fe2009-08-12 21:52:11 +020077 printf(" -T --timestamp Prefix every log line with a timestamp\n");
Harald Welte59b04682009-06-10 05:40:52 +080078}
79
80static void handle_options(int argc, char** argv)
81{
82 while (1) {
Harald Weltea8379772009-06-20 22:36:41 +020083 int option_index = 0, c;
Harald Welte59b04682009-06-10 05:40:52 +080084 static struct option long_options[] = {
85 {"help", 0, 0, 'h'},
86 {"debug", 1, 0, 'd'},
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +020087 {"config-file", 1, 0, 'c'},
Harald Welte59b04682009-06-10 05:40:52 +080088 {"disable-color", 0, 0, 's'},
Harald Welte59b04682009-06-10 05:40:52 +080089 {"database", 1, 0, 'l'},
90 {"authorize-everyone", 0, 0, 'a'},
91 {"reject-cause", 1, 0, 'r'},
92 {"pcap", 1, 0, 'p'},
Harald Welte59b04682009-06-10 05:40:52 +080093 {"timestamp", 0, 0, 'T'},
Harald Welte3c062072009-07-28 18:25:29 +020094 {"rtp-proxy", 0, 0, 'P'},
Harald Welte59b04682009-06-10 05:40:52 +080095 {0, 0, 0, 0}
96 };
97
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +020098 c = getopt_long(argc, argv, "hd:sl:ar:p:TPc:",
Harald Welte59b04682009-06-10 05:40:52 +080099 long_options, &option_index);
100 if (c == -1)
101 break;
102
103 switch (c) {
104 case 'h':
105 print_usage();
106 print_help();
107 exit(0);
108 case 's':
109 debug_use_color(0);
110 break;
111 case 'd':
112 debug_parse_category_mask(optarg);
113 break;
Harald Welte59b04682009-06-10 05:40:52 +0800114 case 'l':
115 database_name = strdup(optarg);
116 break;
Holger Hans Peter Freyther4fa91d02009-08-10 08:39:27 +0200117 case 'c':
118 config_file = strdup(optarg);
119 break;
Harald Welte59b04682009-06-10 05:40:52 +0800120 case 'r':
121 gsm0408_set_reject_cause(atoi(optarg));
122 break;
123 case 'p':
124 create_pcap_file(optarg);
125 break;
Harald Welte59b04682009-06-10 05:40:52 +0800126 case 'T':
127 debug_timestamp(1);
128 break;
Harald Welte3c062072009-07-28 18:25:29 +0200129 case 'P':
130 ipacc_rtp_direct = 0;
131 break;
Harald Welte59b04682009-06-10 05:40:52 +0800132 default:
133 /* ignore */
134 break;
135 }
136 }
137}
138
139static void signal_handler(int signal)
140{
141 fprintf(stdout, "signal %u received\n", signal);
142
143 switch (signal) {
Harald Welte3b714502009-08-06 17:43:50 +0200144 case SIGINT:
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200145 bsc_shutdown_net(bsc_gsmnet);
Harald Welte3b714502009-08-06 17:43:50 +0200146 sleep(3);
147 exit(0);
Harald Welte59b04682009-06-10 05:40:52 +0800148 break;
Harald Welte4d5e6d52009-08-07 00:29:44 +0200149 case SIGABRT:
150 /* in case of abort, we want to obtain a talloc report
151 * and then return to the caller, who will abort the process */
Harald Weltea8379772009-06-20 22:36:41 +0200152 case SIGUSR1:
153 talloc_report_full(tall_bsc_ctx, stderr);
154 break;
Harald Welte59b04682009-06-10 05:40:52 +0800155 default:
156 break;
157 }
158}
159
160int main(int argc, char **argv)
161{
162 int rc;
163
Harald Weltea8379772009-06-20 22:36:41 +0200164 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
Harald Welte (local)8751ee92009-08-15 02:30:58 +0200165 talloc_ctx_init();
Harald Welte (local)bd931f12009-08-13 13:52:14 +0200166 on_dso_load_token();
Harald Welte (local)95713782009-08-16 13:18:51 +0200167 on_dso_load_rrlp();
Harald Weltea8379772009-06-20 22:36:41 +0200168
Harald Welte59b04682009-06-10 05:40:52 +0800169 /* parse options */
170 handle_options(argc, argv);
171
172 /* seed the PRNG */
173 srand(time(NULL));
174
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200175 if (db_init(database_name)) {
176 printf("DB: Failed to init database. Please check the option settings.\n");
177 return -1;
178 }
179 printf("DB: Database initialized.\n");
180
181 if (db_prepare()) {
182 printf("DB: Failed to prepare database.\n");
183 return -1;
184 }
185 printf("DB: Database prepared.\n");
186
187 rc = bsc_bootstrap_network(mncc_recv, config_file);
Harald Welte59b04682009-06-10 05:40:52 +0800188 if (rc < 0)
189 exit(1);
190
Harald Welte3b714502009-08-06 17:43:50 +0200191 signal(SIGINT, &signal_handler);
Harald Welte59b04682009-06-10 05:40:52 +0800192 signal(SIGABRT, &signal_handler);
Harald Weltea8379772009-06-20 22:36:41 +0200193 signal(SIGUSR1, &signal_handler);
Harald Welte59b04682009-06-10 05:40:52 +0800194
195 while (1) {
Holger Hans Peter Freythercad370f2009-08-17 06:55:10 +0200196 bsc_upqueue(bsc_gsmnet);
Harald Welte59b04682009-06-10 05:40:52 +0800197 bsc_select_main(0);
198 }
199}