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