blob: 5c1ad58b84dd2fe7a8441a5f26efd5633524e708 [file] [log] [blame]
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +02001/*
2 * osmo-pcap-server code
3 *
4 * (C) 2011 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2011 by On-Waves
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <osmo-pcap/common.h>
24#include <osmo-pcap/osmo_pcap_server.h>
25
26#include <osmocom/core/application.h>
27#include <osmocom/core/process.h>
28#include <osmocom/core/rate_ctr.h>
29#include <osmocom/core/select.h>
30#include <osmocom/core/talloc.h>
31
32#include <osmocom/vty/logging.h>
33#include <osmocom/vty/telnet_interface.h>
34
35#include <pcap.h>
36#include <signal.h>
37#include <stdio.h>
38#include <string.h>
39#include <time.h>
40
41#define _GNU_SOURCE
42#include <getopt.h>
43
44#include "osmopcapconfig.h"
45
46static const char *config_file = "osmo-pcap-server.cfg";
47static int daemonize = 0;
48
49void *tall_bsc_ctx;
50struct osmo_pcap_server *pcap_server;
51extern void *tall_msgb_ctx;
52extern void *tall_ctr_ctx;
53
54static struct vty_app_info vty_info = {
55 .name = "OsmoPCAPServer",
56 .version = PACKAGE_VERSION,
57 .go_parent_cb = osmopcap_go_parent,
58 .is_config_node = osmopcap_is_config_node,
59};
60
61static void print_usage()
62{
63 printf("Usage: osmo_pcap_server\n");
64}
65
66static void print_help()
67{
68 printf(" Some useful help...\n");
69 printf(" -h --help this text\n");
70 printf(" -D --daemonize Fork the process into a background daemon\n");
71 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
72 printf(" -s --disable-color\n");
73 printf(" -T --timestamp. Print a timestamp in the debug output.\n");
74 printf(" -e --log-level number. Set a global loglevel.\n");
75 printf(" -c --config-file filename The config file to use.\n");
76}
77
78static void handle_options(int argc, char **argv)
79{
80 while (1) {
81 int option_index = 0, c;
82 static struct option long_options[] = {
83 {"help", 0, 0, 'h'},
84 {"daemonize", 0, 0, 'D'},
85 {"debug", 1, 0, 'd'},
86 {"disable-color", 0, 0, 's'},
87 {"timestamp", 0, 0, 'T'},
88 {"log-level", 1, 0, 'e'},
89 {"config-file", 1, 0, 'c'},
90 {0, 0, 0, 0}
91 };
92
93 c = getopt_long(argc, argv, "hd:DsTc:e:",
94 long_options, &option_index);
95 if (c == -1)
96 break;
97
98 switch (c) {
99 case 'h':
100 print_usage();
101 print_help();
102 exit(0);
103 case 'D':
104 daemonize = 1;
105 break;
106 case 'd':
107 log_parse_category_mask(osmo_stderr_target, optarg);
108 break;
109 case 's':
110 log_set_use_color(osmo_stderr_target, 0);
111 break;
112 case 'T':
113 log_set_print_timestamp(osmo_stderr_target, 1);
114 break;
115 case 'e':
116 log_set_log_level(osmo_stderr_target, atoi(optarg));
117 break;
118 case 'c':
119 config_file = strdup(optarg);
120 break;
121 default:
122 /* ignore */
123 break;
124 }
125 }
126}
127
128static void signal_handler(int signal)
129{
130 fprintf(stdout, "signal %u received\n", signal);
131
132 switch (signal) {
133 case SIGINT:
134 exit(0);
135 break;
136 case SIGABRT:
137 /* in case of abort, we want to obtain a talloc report
138 * and then return to the caller, who will abort the process */
139 case SIGUSR1:
140 talloc_report(tall_vty_ctx, stderr);
141 talloc_report_full(tall_bsc_ctx, stderr);
142 break;
143 default:
144 break;
145 }
146}
147
148static void talloc_init_ctx()
149{
150 tall_bsc_ctx = talloc_named_const(NULL, 0, "server");
151 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
152 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
153}
154
155int main(int argc, char **argv)
156{
157 int rc;
158
159 talloc_init_ctx();
160 osmo_init_logging(&log_info);
161
162 vty_info.copyright = osmopcap_copyright;
163 vty_init(&vty_info);
164 logging_vty_add_cmds(&log_info);
165
166 /* parse options */
167 handle_options(argc, argv);
168
169 rate_ctr_init(tall_bsc_ctx);
170
171 /* seed the PRNG */
172 srand(time(NULL));
173
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200174 signal(SIGINT, &signal_handler);
175 signal(SIGABRT, &signal_handler);
176 signal(SIGUSR1, &signal_handler);
177 osmo_init_ignore_signals();
178
179 telnet_init(tall_bsc_ctx, NULL, 4241);
180
181 pcap_server = talloc_zero(tall_bsc_ctx, struct osmo_pcap_server);
182 if (!pcap_server) {
183 LOGP(DSERVER, LOGL_ERROR, "Failed to allocate osmo_pcap_server.\n");
184 exit(1);
185 }
Holger Hans Peter Freyther9f6127f2011-05-31 22:52:41 +0200186 INIT_LLIST_HEAD(&pcap_server->conn);
Holger Hans Peter Freyther88c07f22011-06-01 14:02:54 +0200187 pcap_server->base_path = talloc_strdup(pcap_server, "./");
188 pcap_server->max_size = 1073741824;
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200189 vty_server_init(pcap_server);
190
191 if (vty_read_config_file(config_file, NULL) < 0) {
192 LOGP(DSERVER, LOGL_ERROR,
193 "Failed to parse the config file: %s\n", config_file);
194 exit(1);
195 }
196
197 /* attempt to connect to the remote */
198 if (osmo_pcap_server_listen(pcap_server) != 0) {
199 LOGP(DSERVER, LOGL_ERROR,
200 "Failed to listen for incoming data\n");
201 exit(1);
202 }
203
204 if (daemonize) {
205 rc = osmo_daemonize();
206 if (rc < 0) {
207 perror("Error during daemonize");
208 exit(1);
209 }
210 }
211
212 while (1) {
213 osmo_select_main(0);
214 }
215
216 return(0);
217}