blob: f5ba41a9dde2b2313f3a35c76d6778a941a3922f [file] [log] [blame]
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +02001/*
2 * osmo-pcap-client code
3 *
Holger Hans Peter Freyther86282d62016-08-18 18:34:27 +02004 * (C) 2011-2016 by Holger Hans Peter Freyther <holger@moiji-mobile.com>
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +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
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
Holger Hans Peter Freytherf4164632016-08-18 18:39:53 +020055
56static const struct rate_ctr_desc pcap_client_ctr_desc[] = {
57 [CLIENT_CTR_CONNECT] = { "server.connect", "Connects to the server" },
58 [CLIENT_CTR_BYTES] = { "captured.bytes", "Captured bytes " },
59 [CLIENT_CTR_PKTS] = { "captured.pkts", "Captured packets " },
60 [CLIENT_CTR_2BIG] = { "bpf.too_big", "Captured data too big " },
61 [CLIENT_CTR_NOMEM] = { "client.no_mem", "No memory available " },
62 [CLIENT_CTR_QERR] = { "client.queue_err", "Can not queue data " },
Holger Hans Peter Freytherc3455dc2016-08-19 17:31:01 +020063 [CLIENT_CTR_PERR] = { "client.pcap_err", "libpcap error " },
64 [CLIENT_CTR_WERR] = { "client.write_err", "Write error " },
Holger Hans Peter Freytherfa5572e2016-08-19 20:27:35 +020065 [CLIENT_CTR_P_RECV] = { "pcap.recv", "PCAP received packets " },
66 [CLIENT_CTR_P_DROP] = { "pcap.drop", "PCAP dropped packets " },
67 [CLIENT_CTR_P_IFDROP] = { "pcap.ifdrop", "iface dropped packets " },
Holger Hans Peter Freytherf4164632016-08-18 18:39:53 +020068};
69
70static const struct rate_ctr_group_desc pcap_client_ctr_group_desc = {
71 .group_name_prefix = "pcap.client",
72 .group_description = "PCAP Client statistics",
73 .num_ctr = ARRAY_SIZE(pcap_client_ctr_desc),
74 .ctr_desc = pcap_client_ctr_desc,
75 .class_id = OSMO_STATS_CLASS_GLOBAL,
76};
77
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020078static struct vty_app_info vty_info = {
79 .name = "OsmoPCAPClient",
80 .version = PACKAGE_VERSION,
81 .go_parent_cb = osmopcap_go_parent,
82 .is_config_node = osmopcap_is_config_node,
83};
84
85static void print_usage()
86{
87 printf("Usage: osmo_pcap_client\n");
88}
89
90static void print_help()
91{
92 printf(" Some useful help...\n");
93 printf(" -h --help this text\n");
94 printf(" -D --daemonize Fork the process into a background daemon\n");
95 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
96 printf(" -s --disable-color\n");
97 printf(" -T --timestamp. Print a timestamp in the debug output.\n");
98 printf(" -e --log-level number. Set a global loglevel.\n");
99 printf(" -c --config-file filename The config file to use.\n");
100}
101
102static void handle_options(int argc, char **argv)
103{
104 while (1) {
105 int option_index = 0, c;
106 static struct option long_options[] = {
107 {"help", 0, 0, 'h'},
108 {"daemonize", 0, 0, 'D'},
109 {"debug", 1, 0, 'd'},
110 {"disable-color", 0, 0, 's'},
111 {"timestamp", 0, 0, 'T'},
112 {"log-level", 1, 0, 'e'},
113 {"config-file", 1, 0, 'c'},
114 {0, 0, 0, 0}
115 };
116
117 c = getopt_long(argc, argv, "hd:DsTc:e:",
118 long_options, &option_index);
119 if (c == -1)
120 break;
121
122 switch (c) {
123 case 'h':
124 print_usage();
125 print_help();
126 exit(0);
127 case 'D':
128 daemonize = 1;
129 break;
130 case 'd':
131 log_parse_category_mask(osmo_stderr_target, optarg);
132 break;
133 case 's':
134 log_set_use_color(osmo_stderr_target, 0);
135 break;
136 case 'T':
137 log_set_print_timestamp(osmo_stderr_target, 1);
138 break;
139 case 'e':
140 log_set_log_level(osmo_stderr_target, atoi(optarg));
141 break;
142 case 'c':
143 config_file = strdup(optarg);
144 break;
145 default:
146 /* ignore */
147 break;
148 }
149 }
150}
151
152static void signal_handler(int signal)
153{
154 fprintf(stdout, "signal %u received\n", signal);
155
156 switch (signal) {
157 case SIGINT:
158 exit(0);
159 break;
160 case SIGABRT:
161 /* in case of abort, we want to obtain a talloc report
162 * and then return to the caller, who will abort the process */
163 case SIGUSR1:
164 talloc_report(tall_vty_ctx, stderr);
165 talloc_report_full(tall_bsc_ctx, stderr);
166 break;
167 default:
168 break;
169 }
170}
171
172static void talloc_init_ctx()
173{
174 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
175 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
176 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
177}
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200178
179int main(int argc, char **argv)
180{
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200181 int rc;
182
183 talloc_init_ctx();
184 osmo_init_logging(&log_info);
185
186 vty_info.copyright = osmopcap_copyright;
187 vty_init(&vty_info);
188 logging_vty_add_cmds(&log_info);
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +0200189 osmo_stats_vty_add_cmds(&log_info);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200190
191 /* parse options */
192 handle_options(argc, argv);
193
194 rate_ctr_init(tall_bsc_ctx);
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +0200195 osmo_stats_init(tall_bsc_ctx);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200196
197 /* seed the PRNG */
198 srand(time(NULL));
199
200
201 signal(SIGINT, &signal_handler);
202 signal(SIGABRT, &signal_handler);
203 signal(SIGUSR1, &signal_handler);
204 osmo_init_ignore_signals();
205
Holger Hans Peter Freyther86282d62016-08-18 18:34:27 +0200206 rc = telnet_init(tall_bsc_ctx, NULL, 4240);
207 if (rc < 0) {
208 LOGP(DCLIENT, LOGL_ERROR, "Failed to bind telnet interface\n");
209 exit(1);
210 }
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200211
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200212 pcap_client = talloc_zero(tall_bsc_ctx, struct osmo_pcap_client);
213 if (!pcap_client) {
214 LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate osmo_pcap_client.\n");
215 exit(1);
216 }
217 pcap_client->fd.fd = -1;
218 vty_client_init(pcap_client);
219
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200220 /* initialize the queue */
221 osmo_wqueue_init(&pcap_client->wqueue, 10);
222 pcap_client->wqueue.bfd.fd = -1;
223
Holger Hans Peter Freytherf4164632016-08-18 18:39:53 +0200224 /* initialize the stats interface */
225 pcap_client->ctrg = rate_ctr_group_alloc(pcap_client, &pcap_client_ctr_group_desc, 0);
226 if (!pcap_client->ctrg) {
227 LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate rate ctr\n");
228 exit(1);
229 }
Holger Hans Peter Freythercd2d3db2011-05-31 18:39:33 +0200230
231 if (vty_read_config_file(config_file, NULL) < 0) {
232 LOGP(DCLIENT, LOGL_ERROR,
233 "Failed to parse the config file: %s\n", config_file);
234 exit(1);
235 }
236
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200237 /* attempt to connect to the remote */
238 osmo_client_connect(pcap_client);
239
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200240 if (daemonize) {
241 rc = osmo_daemonize();
242 if (rc < 0) {
243 perror("Error during daemonize");
244 exit(1);
245 }
246 }
247
248 while (1) {
249 osmo_select_main(0);
250 }
251
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200252 return(0);
253}