blob: c0695ef35d34f670afe6f8e1c968d6ef7db4d2ea [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* A hackish minimal BSC (+MSC +HLR) implementation */
2
Harald Welte32201c12009-03-10 12:15:10 +00003/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Freyther219518d2009-01-02 22:04:43 +00004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte52b1f982008-12-23 20:25:15 +00005 * 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
Harald Weltef6b7a902008-12-26 00:05:11 +000023#include <unistd.h>
Harald Weltef6b7a902008-12-26 00:05:11 +000024#include <time.h>
Harald Weltead384642008-12-26 10:20:07 +000025#include <errno.h>
Harald Welted1252502009-01-01 01:50:32 +000026#include <signal.h>
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000027#include <fcntl.h>
28#include <sys/stat.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029
Holger Freytherb332f612008-12-27 12:46:51 +000030#define _GNU_SOURCE
31#include <getopt.h>
32
Harald Welte255539c2008-12-28 02:26:27 +000033#include <openbsc/db.h>
Harald Weltead384642008-12-26 10:20:07 +000034#include <openbsc/select.h>
Harald Welte702d8702008-12-26 20:25:35 +000035#include <openbsc/debug.h>
Harald Welte1fa60c82009-02-09 18:13:26 +000036#include <openbsc/e1_input.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020037#include <openbsc/talloc.h>
Harald Welte3961fcc2009-11-30 19:37:18 +010038#include <openbsc/signal.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020039
Holger Freytherefde7fb2008-12-28 14:14:56 +000040/* MCC and MNC for the Location Area Identifier */
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020041struct gsm_network *bsc_gsmnet = 0;
Holger Freytherbde36102008-12-28 22:51:39 +000042static const char *database_name = "hlr.sqlite3";
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +020043static const char *config_file = "openbsc.cfg";
Harald Welte805f6442009-07-28 18:25:29 +020044extern int ipacc_rtp_direct;
Holger Freytherefde7fb2008-12-28 14:14:56 +000045
Holger Hans Peter Freyther8b457fb2009-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 Weltef6b7a902008-12-26 00:05:11 +000049
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000050static 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
Holger Freyther0469cf62009-03-31 12:14:16 +000060 e1_set_pcap_fd(fd);
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000061}
62
Holger Freytherb332f612008-12-27 12:46:51 +000063static void print_usage()
64{
65 printf("Usage: bsc_hack\n");
66}
67
68static void print_help()
69{
70 printf(" Some useful help...\n");
Harald Welte42581822009-08-08 16:12:58 +020071 printf(" -h --help this text\n");
Holger Freytherb332f612008-12-27 12:46:51 +000072 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
Holger Freytherefde7fb2008-12-28 14:14:56 +000073 printf(" -s --disable-color\n");
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +020074 printf(" -c --config-file filename The config file to use.\n");
Holger Freytherbde36102008-12-28 22:51:39 +000075 printf(" -l --database db-name The database to use\n");
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000076 printf(" -p --pcap file The filename of the pcap file\n");
Harald Weltec70979a2009-08-12 21:52:11 +020077 printf(" -T --timestamp Prefix every log line with a timestamp\n");
Harald Weltef314f682009-12-18 15:05:02 +010078 printf(" -P --rtp-proxy Enable the RTP Proxy code inside OpenBSC\n");
Holger Freytherb332f612008-12-27 12:46:51 +000079}
80
81static void handle_options(int argc, char** argv)
82{
83 while (1) {
Harald Welte2cf161b2009-06-20 22:36:41 +020084 int option_index = 0, c;
Holger Freytherb332f612008-12-27 12:46:51 +000085 static struct option long_options[] = {
86 {"help", 0, 0, 'h'},
87 {"debug", 1, 0, 'd'},
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +020088 {"config-file", 1, 0, 'c'},
Holger Freytherefde7fb2008-12-28 14:14:56 +000089 {"disable-color", 0, 0, 's'},
Holger Freytherbde36102008-12-28 22:51:39 +000090 {"database", 1, 0, 'l'},
Holger Freyther89824fc2008-12-30 16:18:18 +000091 {"authorize-everyone", 0, 0, 'a'},
Holger Freyther9a3ee0f2009-01-02 00:40:15 +000092 {"pcap", 1, 0, 'p'},
Harald Welted3ff51d2009-06-09 20:21:57 +000093 {"timestamp", 0, 0, 'T'},
Harald Welte805f6442009-07-28 18:25:29 +020094 {"rtp-proxy", 0, 0, 'P'},
Holger Freytherb332f612008-12-27 12:46:51 +000095 {0, 0, 0, 0}
96 };
97
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +020098 c = getopt_long(argc, argv, "hd:sl:ar:p:TPc:",
Holger Freytherb332f612008-12-27 12:46:51 +000099 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);
Holger Freytherefde7fb2008-12-28 14:14:56 +0000108 case 's':
Holger Freytherb332f612008-12-27 12:46:51 +0000109 debug_use_color(0);
110 break;
111 case 'd':
112 debug_parse_category_mask(optarg);
113 break;
Harald Welte8965da42009-01-06 18:09:02 +0000114 case 'l':
Holger Freytherbde36102008-12-28 22:51:39 +0000115 database_name = strdup(optarg);
116 break;
Holger Hans Peter Freytherd5d1cef2009-08-10 08:39:27 +0200117 case 'c':
118 config_file = strdup(optarg);
119 break;
Holger Freyther9a3ee0f2009-01-02 00:40:15 +0000120 case 'p':
121 create_pcap_file(optarg);
122 break;
Harald Welted3ff51d2009-06-09 20:21:57 +0000123 case 'T':
124 debug_timestamp(1);
125 break;
Harald Welte805f6442009-07-28 18:25:29 +0200126 case 'P':
127 ipacc_rtp_direct = 0;
128 break;
Holger Freytherb332f612008-12-27 12:46:51 +0000129 default:
130 /* ignore */
131 break;
132 }
133 }
134}
135
Harald Welted1252502009-01-01 01:50:32 +0000136static void signal_handler(int signal)
137{
138 fprintf(stdout, "signal %u received\n", signal);
139
140 switch (signal) {
Harald Weltef294f452009-08-06 17:43:50 +0200141 case SIGINT:
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200142 bsc_shutdown_net(bsc_gsmnet);
Harald Welte3961fcc2009-11-30 19:37:18 +0100143 dispatch_signal(SS_GLOBAL, S_GLOBAL_SHUTDOWN, NULL);
Harald Weltef294f452009-08-06 17:43:50 +0200144 sleep(3);
145 exit(0);
Harald Welted1252502009-01-01 01:50:32 +0000146 break;
Harald Welte31c3d342009-08-07 00:29:44 +0200147 case SIGABRT:
148 /* in case of abort, we want to obtain a talloc report
149 * and then return to the caller, who will abort the process */
Harald Welte2cf161b2009-06-20 22:36:41 +0200150 case SIGUSR1:
151 talloc_report_full(tall_bsc_ctx, stderr);
152 break;
Harald Welted1252502009-01-01 01:50:32 +0000153 default:
154 break;
155 }
156}
157
Harald Weltef6b7a902008-12-26 00:05:11 +0000158int main(int argc, char **argv)
159{
Harald Welte1fa60c82009-02-09 18:13:26 +0000160 int rc;
161
Harald Welte2cf161b2009-06-20 22:36:41 +0200162 tall_bsc_ctx = talloc_named_const(NULL, 1, "openbsc");
Harald Welte (local)d19e58b2009-08-15 02:30:58 +0200163 talloc_ctx_init();
Harald Welte (local)50d12712009-08-13 13:52:14 +0200164 on_dso_load_token();
Harald Welte (local)b8afe812009-08-16 13:18:51 +0200165 on_dso_load_rrlp();
Harald Welte8d77b952009-12-17 00:31:10 +0100166 on_dso_load_ho_dec();
Harald Welte2cf161b2009-06-20 22:36:41 +0200167
Holger Freytherb332f612008-12-27 12:46:51 +0000168 /* parse options */
169 handle_options(argc, argv);
170
Harald Welte65ccf882009-02-24 22:36:20 +0000171 /* seed the PRNG */
172 srand(time(NULL));
173
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200174 if (db_init(database_name)) {
175 printf("DB: Failed to init database. Please check the option settings.\n");
176 return -1;
177 }
178 printf("DB: Database initialized.\n");
179
180 if (db_prepare()) {
181 printf("DB: Failed to prepare database.\n");
182 return -1;
183 }
184 printf("DB: Database prepared.\n");
185
186 rc = bsc_bootstrap_network(mncc_recv, config_file);
Harald Welte1fa60c82009-02-09 18:13:26 +0000187 if (rc < 0)
188 exit(1);
Harald Weltef6b7a902008-12-26 00:05:11 +0000189
Harald Weltef294f452009-08-06 17:43:50 +0200190 signal(SIGINT, &signal_handler);
Harald Welted1252502009-01-01 01:50:32 +0000191 signal(SIGABRT, &signal_handler);
Harald Welte2cf161b2009-06-20 22:36:41 +0200192 signal(SIGUSR1, &signal_handler);
Holger Hans Peter Freytherf49fac22009-11-24 20:06:41 +0100193 signal(SIGPIPE, SIG_IGN);
Harald Welted1252502009-01-01 01:50:32 +0000194
Harald Weltef6b7a902008-12-26 00:05:11 +0000195 while (1) {
Holger Hans Peter Freyther8b457fb2009-08-17 06:55:10 +0200196 bsc_upqueue(bsc_gsmnet);
Harald Welte04d3c922009-05-23 06:07:04 +0000197 bsc_select_main(0);
Harald Weltef6b7a902008-12-26 00:05:11 +0000198 }
199}