blob: b509c843624dac5090e330c711fd72bfcb5fc926 [file] [log] [blame]
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +02001/*
2 * osmo-pcap-client 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
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020023#include <osmo-pcap/common.h>
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +020024#include <osmo-pcap/osmo_pcap_client.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020025
26#include <osmocom/core/application.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +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 Freyther530ecc02011-05-31 15:47:44 +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 Freyther530ecc02011-05-31 15:47:44 +020035
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +020036#include <pcap.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020037#include <signal.h>
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +020038#include <stdio.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020039#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-client.cfg";
48static int daemonize = 0;
49
50void *tall_bsc_ctx;
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +020051struct osmo_pcap_client *pcap_client;
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020052extern void *tall_msgb_ctx;
53extern void *tall_ctr_ctx;
54
55static struct vty_app_info vty_info = {
56 .name = "OsmoPCAPClient",
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_client\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;
144 default:
145 break;
146 }
147}
148
149static void talloc_init_ctx()
150{
151 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
152 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
153 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
154}
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200155
156int main(int argc, char **argv)
157{
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200158 int rc;
159
160 talloc_init_ctx();
161 osmo_init_logging(&log_info);
162
163 vty_info.copyright = osmopcap_copyright;
164 vty_init(&vty_info);
165 logging_vty_add_cmds(&log_info);
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +0200166 osmo_stats_vty_add_cmds(&log_info);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200167
168 /* parse options */
169 handle_options(argc, argv);
170
171 rate_ctr_init(tall_bsc_ctx);
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +0200172 osmo_stats_init(tall_bsc_ctx);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200173
174 /* seed the PRNG */
175 srand(time(NULL));
176
177
178 signal(SIGINT, &signal_handler);
179 signal(SIGABRT, &signal_handler);
180 signal(SIGUSR1, &signal_handler);
181 osmo_init_ignore_signals();
182
183 telnet_init(tall_bsc_ctx, NULL, 4240);
184
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200185 pcap_client = talloc_zero(tall_bsc_ctx, struct osmo_pcap_client);
186 if (!pcap_client) {
187 LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate osmo_pcap_client.\n");
188 exit(1);
189 }
190 pcap_client->fd.fd = -1;
191 vty_client_init(pcap_client);
192
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200193 /* initialize the queue */
194 osmo_wqueue_init(&pcap_client->wqueue, 10);
195 pcap_client->wqueue.bfd.fd = -1;
196
Holger Hans Peter Freythercd2d3db2011-05-31 18:39:33 +0200197
198 if (vty_read_config_file(config_file, NULL) < 0) {
199 LOGP(DCLIENT, LOGL_ERROR,
200 "Failed to parse the config file: %s\n", config_file);
201 exit(1);
202 }
203
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200204 /* attempt to connect to the remote */
205 osmo_client_connect(pcap_client);
206
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200207 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
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200219 return(0);
220}