blob: 83efa2146473fde4c89bd3612ea99734d81553f2 [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>
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +020027#include <osmocom/core/rate_ctr.h>
28#include <osmocom/core/select.h>
29#include <osmocom/core/talloc.h>
30
31#include <osmocom/vty/logging.h>
32#include <osmocom/vty/telnet_interface.h>
33
34#include <pcap.h>
35#include <signal.h>
36#include <stdio.h>
37#include <string.h>
38#include <time.h>
39
40#define _GNU_SOURCE
41#include <getopt.h>
42
43#include "osmopcapconfig.h"
44
45static const char *config_file = "osmo-pcap-server.cfg";
46static int daemonize = 0;
47
48void *tall_bsc_ctx;
49struct osmo_pcap_server *pcap_server;
50extern void *tall_msgb_ctx;
51extern void *tall_ctr_ctx;
52
53static struct vty_app_info vty_info = {
54 .name = "OsmoPCAPServer",
55 .version = PACKAGE_VERSION,
56 .go_parent_cb = osmopcap_go_parent,
57 .is_config_node = osmopcap_is_config_node,
58};
59
60static void print_usage()
61{
62 printf("Usage: osmo_pcap_server\n");
63}
64
65static void print_help()
66{
67 printf(" Some useful help...\n");
68 printf(" -h --help this text\n");
69 printf(" -D --daemonize Fork the process into a background daemon\n");
70 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
71 printf(" -s --disable-color\n");
72 printf(" -T --timestamp. Print a timestamp in the debug output.\n");
73 printf(" -e --log-level number. Set a global loglevel.\n");
74 printf(" -c --config-file filename The config file to use.\n");
75}
76
77static void handle_options(int argc, char **argv)
78{
79 while (1) {
80 int option_index = 0, c;
81 static struct option long_options[] = {
82 {"help", 0, 0, 'h'},
83 {"daemonize", 0, 0, 'D'},
84 {"debug", 1, 0, 'd'},
85 {"disable-color", 0, 0, 's'},
86 {"timestamp", 0, 0, 'T'},
87 {"log-level", 1, 0, 'e'},
88 {"config-file", 1, 0, 'c'},
89 {0, 0, 0, 0}
90 };
91
92 c = getopt_long(argc, argv, "hd:DsTc:e:",
93 long_options, &option_index);
94 if (c == -1)
95 break;
96
97 switch (c) {
98 case 'h':
99 print_usage();
100 print_help();
101 exit(0);
102 case 'D':
103 daemonize = 1;
104 break;
105 case 'd':
106 log_parse_category_mask(osmo_stderr_target, optarg);
107 break;
108 case 's':
109 log_set_use_color(osmo_stderr_target, 0);
110 break;
111 case 'T':
112 log_set_print_timestamp(osmo_stderr_target, 1);
113 break;
114 case 'e':
115 log_set_log_level(osmo_stderr_target, atoi(optarg));
116 break;
117 case 'c':
118 config_file = strdup(optarg);
119 break;
120 default:
121 /* ignore */
122 break;
123 }
124 }
125}
126
127static void signal_handler(int signal)
128{
129 fprintf(stdout, "signal %u received\n", signal);
130
131 switch (signal) {
132 case SIGINT:
133 exit(0);
134 break;
135 case SIGABRT:
136 /* in case of abort, we want to obtain a talloc report
137 * and then return to the caller, who will abort the process */
138 case SIGUSR1:
139 talloc_report(tall_vty_ctx, stderr);
140 talloc_report_full(tall_bsc_ctx, stderr);
141 break;
Daniel Willmannc7401c62011-07-17 17:48:18 +0200142 case SIGHUP:
143 osmo_pcap_server_reopen(pcap_server);
144 break;
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200145 default:
146 break;
147 }
148}
149
150static void talloc_init_ctx()
151{
152 tall_bsc_ctx = talloc_named_const(NULL, 0, "server");
153 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
154 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
155}
156
157int main(int argc, char **argv)
158{
159 int rc;
160
161 talloc_init_ctx();
162 osmo_init_logging(&log_info);
163
164 vty_info.copyright = osmopcap_copyright;
165 vty_init(&vty_info);
166 logging_vty_add_cmds(&log_info);
167
168 /* parse options */
169 handle_options(argc, argv);
170
171 rate_ctr_init(tall_bsc_ctx);
172
173 /* seed the PRNG */
174 srand(time(NULL));
175
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200176 signal(SIGINT, &signal_handler);
177 signal(SIGABRT, &signal_handler);
178 signal(SIGUSR1, &signal_handler);
179 osmo_init_ignore_signals();
Daniel Willmannc7401c62011-07-17 17:48:18 +0200180 signal(SIGHUP, &signal_handler);
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200181
182 telnet_init(tall_bsc_ctx, NULL, 4241);
183
184 pcap_server = talloc_zero(tall_bsc_ctx, struct osmo_pcap_server);
185 if (!pcap_server) {
186 LOGP(DSERVER, LOGL_ERROR, "Failed to allocate osmo_pcap_server.\n");
187 exit(1);
188 }
Holger Hans Peter Freyther9f6127f2011-05-31 22:52:41 +0200189 INIT_LLIST_HEAD(&pcap_server->conn);
Holger Hans Peter Freyther88c07f22011-06-01 14:02:54 +0200190 pcap_server->base_path = talloc_strdup(pcap_server, "./");
191 pcap_server->max_size = 1073741824;
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200192 vty_server_init(pcap_server);
193
194 if (vty_read_config_file(config_file, NULL) < 0) {
195 LOGP(DSERVER, LOGL_ERROR,
196 "Failed to parse the config file: %s\n", config_file);
197 exit(1);
198 }
199
200 /* attempt to connect to the remote */
201 if (osmo_pcap_server_listen(pcap_server) != 0) {
202 LOGP(DSERVER, LOGL_ERROR,
203 "Failed to listen for incoming data\n");
204 exit(1);
205 }
206
207 if (daemonize) {
208 rc = osmo_daemonize();
209 if (rc < 0) {
210 perror("Error during daemonize");
211 exit(1);
212 }
213 }
214
215 while (1) {
216 osmo_select_main(0);
217 }
218
219 return(0);
220}