blob: 37a96323a4ac6c5cfb20ce20ce0abefd427c1916 [file] [log] [blame]
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +02001/*
2 * osmo-pcap-server code
3 *
Holger Hans Peter Freyther918be512016-08-18 18:37:13 +02004 * (C) 2011-2016 by Holger Hans Peter Freyther <holger@moiji-mobile.com>
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +02005 * (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>
Holger Hans Peter Freyther9ea4da42016-09-06 11:38:56 +020025#include <osmo-pcap/osmo_tls.h>
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +020026
27#include <osmocom/core/application.h>
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +020028#include <osmocom/core/rate_ctr.h>
29#include <osmocom/core/select.h>
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +020030#include <osmocom/core/stats.h>
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +020031#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther99526a62016-08-19 19:15:39 +020032#include <osmocom/core/utils.h>
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +020033
34#include <osmocom/vty/logging.h>
35#include <osmocom/vty/telnet_interface.h>
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +020036#include <osmocom/vty/stats.h>
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +020037
38#include <pcap.h>
39#include <signal.h>
40#include <stdio.h>
41#include <string.h>
42#include <time.h>
43
44#define _GNU_SOURCE
45#include <getopt.h>
46
47#include "osmopcapconfig.h"
48
49static const char *config_file = "osmo-pcap-server.cfg";
50static int daemonize = 0;
51
52void *tall_bsc_ctx;
53struct osmo_pcap_server *pcap_server;
54extern void *tall_msgb_ctx;
55extern void *tall_ctr_ctx;
56
Holger Hans Peter Freyther99526a62016-08-19 19:15:39 +020057
58static const struct rate_ctr_desc pcap_peer_ctr_desc[] = {
59 [PEER_CTR_CONNECT] = { "peer.connect", "Connect of a peer " },
60 [PEER_CTR_BYTES] = { "peer.bytes", "Received bytes " },
61 [PEER_CTR_PKTS] = { "peer.pkts", "Received packets " },
62 [PEER_CTR_PROTATE] = { "peer.file_rotated","Capture file rotated" },
63};
64
65static const struct rate_ctr_desc pcap_server_ctr_desc[] = {
66 [SERVER_CTR_CONNECT] = { "server.connect", "Connect of a peer " },
67 [SERVER_CTR_BYTES] = { "server.bytes", "Received bytes " },
68 [SERVER_CTR_PKTS] = { "server.pkts", "Received packets " },
69 [SERVER_CTR_PROTATE] = { "server.file_rotated", "Capture file rotated" },
70 [SERVER_CTR_NOCLIENT] = { "server.no_client", "Unknown connected " },
71};
72
73const struct rate_ctr_group_desc pcap_peer_group_desc = {
74 .group_name_prefix = NULL, /* will be dynamically patched */
75 .group_description = "PCAP peer statistics",
76 .num_ctr = ARRAY_SIZE(pcap_peer_ctr_desc),
77 .ctr_desc = pcap_peer_ctr_desc,
78 .class_id = OSMO_STATS_CLASS_PEER,
79};
80
81static const struct rate_ctr_group_desc pcap_server_group_desc = {
82 .group_name_prefix = "pcap.server",
83 .group_description = "PCAP Server global statistics",
84 .num_ctr = ARRAY_SIZE(pcap_server_ctr_desc),
85 .ctr_desc = pcap_server_ctr_desc,
86 .class_id = OSMO_STATS_CLASS_GLOBAL,
87};
88
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +020089static struct vty_app_info vty_info = {
90 .name = "OsmoPCAPServer",
91 .version = PACKAGE_VERSION,
92 .go_parent_cb = osmopcap_go_parent,
93 .is_config_node = osmopcap_is_config_node,
94};
95
96static void print_usage()
97{
98 printf("Usage: osmo_pcap_server\n");
99}
100
101static void print_help()
102{
103 printf(" Some useful help...\n");
104 printf(" -h --help this text\n");
105 printf(" -D --daemonize Fork the process into a background daemon\n");
106 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
107 printf(" -s --disable-color\n");
108 printf(" -T --timestamp. Print a timestamp in the debug output.\n");
109 printf(" -e --log-level number. Set a global loglevel.\n");
110 printf(" -c --config-file filename The config file to use.\n");
111}
112
113static void handle_options(int argc, char **argv)
114{
115 while (1) {
116 int option_index = 0, c;
117 static struct option long_options[] = {
118 {"help", 0, 0, 'h'},
119 {"daemonize", 0, 0, 'D'},
120 {"debug", 1, 0, 'd'},
121 {"disable-color", 0, 0, 's'},
122 {"timestamp", 0, 0, 'T'},
123 {"log-level", 1, 0, 'e'},
124 {"config-file", 1, 0, 'c'},
125 {0, 0, 0, 0}
126 };
127
128 c = getopt_long(argc, argv, "hd:DsTc:e:",
129 long_options, &option_index);
130 if (c == -1)
131 break;
132
133 switch (c) {
134 case 'h':
135 print_usage();
136 print_help();
137 exit(0);
138 case 'D':
139 daemonize = 1;
140 break;
141 case 'd':
142 log_parse_category_mask(osmo_stderr_target, optarg);
143 break;
144 case 's':
145 log_set_use_color(osmo_stderr_target, 0);
146 break;
147 case 'T':
148 log_set_print_timestamp(osmo_stderr_target, 1);
149 break;
150 case 'e':
151 log_set_log_level(osmo_stderr_target, atoi(optarg));
152 break;
153 case 'c':
154 config_file = strdup(optarg);
155 break;
156 default:
157 /* ignore */
158 break;
159 }
160 }
161}
162
163static void signal_handler(int signal)
164{
165 fprintf(stdout, "signal %u received\n", signal);
166
167 switch (signal) {
168 case SIGINT:
169 exit(0);
170 break;
171 case SIGABRT:
172 /* in case of abort, we want to obtain a talloc report
173 * and then return to the caller, who will abort the process */
174 case SIGUSR1:
175 talloc_report(tall_vty_ctx, stderr);
176 talloc_report_full(tall_bsc_ctx, stderr);
177 break;
Daniel Willmannc7401c62011-07-17 17:48:18 +0200178 case SIGHUP:
179 osmo_pcap_server_reopen(pcap_server);
180 break;
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200181 default:
182 break;
183 }
184}
185
186static void talloc_init_ctx()
187{
188 tall_bsc_ctx = talloc_named_const(NULL, 0, "server");
189 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
190 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
191}
192
193int main(int argc, char **argv)
194{
195 int rc;
196
197 talloc_init_ctx();
198 osmo_init_logging(&log_info);
199
200 vty_info.copyright = osmopcap_copyright;
201 vty_init(&vty_info);
202 logging_vty_add_cmds(&log_info);
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +0200203 osmo_stats_vty_add_cmds(&log_info);
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200204
205 /* parse options */
206 handle_options(argc, argv);
207
208 rate_ctr_init(tall_bsc_ctx);
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +0200209 osmo_stats_init(tall_bsc_ctx);
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200210
211 /* seed the PRNG */
212 srand(time(NULL));
213
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200214 signal(SIGINT, &signal_handler);
215 signal(SIGABRT, &signal_handler);
216 signal(SIGUSR1, &signal_handler);
217 osmo_init_ignore_signals();
Daniel Willmannc7401c62011-07-17 17:48:18 +0200218 signal(SIGHUP, &signal_handler);
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200219
Holger Hans Peter Freyther9ea4da42016-09-06 11:38:56 +0200220 osmo_tls_init();
221
Holger Hans Peter Freyther918be512016-08-18 18:37:13 +0200222 rc = telnet_init(tall_bsc_ctx, NULL, 4241);
223 if (rc < 0) {
224 LOGP(DCLIENT, LOGL_ERROR, "Failed to bind telnet interface\n");
225 exit(1);
226 }
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200227
228 pcap_server = talloc_zero(tall_bsc_ctx, struct osmo_pcap_server);
229 if (!pcap_server) {
230 LOGP(DSERVER, LOGL_ERROR, "Failed to allocate osmo_pcap_server.\n");
231 exit(1);
232 }
Holger Hans Peter Freyther99526a62016-08-19 19:15:39 +0200233 pcap_server->ctrg = rate_ctr_group_alloc(pcap_server, &pcap_server_group_desc, 0);
234 if (!pcap_server->ctrg) {
235 LOGP(DSERVER, LOGL_ERROR, "Failed to allocate rate counter.\n");
236 exit(1);
237 }
238
Holger Hans Peter Freyther9f6127f2011-05-31 22:52:41 +0200239 INIT_LLIST_HEAD(&pcap_server->conn);
Holger Hans Peter Freyther88c07f22011-06-01 14:02:54 +0200240 pcap_server->base_path = talloc_strdup(pcap_server, "./");
241 pcap_server->max_size = 1073741824;
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200242 vty_server_init(pcap_server);
243
244 if (vty_read_config_file(config_file, NULL) < 0) {
245 LOGP(DSERVER, LOGL_ERROR,
246 "Failed to parse the config file: %s\n", config_file);
247 exit(1);
248 }
249
Holger Hans Peter Freythercf29fd72016-09-06 20:27:32 +0200250 osmo_tls_server_init(pcap_server);
251
Holger Hans Peter Freyther13619dd2011-05-31 22:09:08 +0200252 /* attempt to connect to the remote */
253 if (osmo_pcap_server_listen(pcap_server) != 0) {
254 LOGP(DSERVER, LOGL_ERROR,
255 "Failed to listen for incoming data\n");
256 exit(1);
257 }
258
259 if (daemonize) {
260 rc = osmo_daemonize();
261 if (rc < 0) {
262 perror("Error during daemonize");
263 exit(1);
264 }
265 }
266
267 while (1) {
268 osmo_select_main(0);
269 }
270
271 return(0);
272}