blob: 0bbeb7ae9e876f3f483f6801277f5d47be7f2ff0 [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 Freytherc2667962016-08-25 23:07:44 +020025#include <osmo-pcap/osmo_tls.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020026
27#include <osmocom/core/application.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +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 Freyther530ecc02011-05-31 15:47:44 +020031#include <osmocom/core/talloc.h>
32
33#include <osmocom/vty/logging.h>
34#include <osmocom/vty/telnet_interface.h>
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +020035#include <osmocom/vty/stats.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020036
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +020037#include <pcap.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020038#include <signal.h>
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +020039#include <stdio.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020040#include <string.h>
41#include <time.h>
42
43#define _GNU_SOURCE
44#include <getopt.h>
45
46#include "osmopcapconfig.h"
47
48static const char *config_file = "osmo-pcap-client.cfg";
49static int daemonize = 0;
50
51void *tall_bsc_ctx;
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +020052struct osmo_pcap_client *pcap_client;
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020053extern void *tall_msgb_ctx;
54extern void *tall_ctr_ctx;
55
Holger Hans Peter Freytherf4164632016-08-18 18:39:53 +020056
57static const struct rate_ctr_desc pcap_client_ctr_desc[] = {
58 [CLIENT_CTR_CONNECT] = { "server.connect", "Connects to the server" },
59 [CLIENT_CTR_BYTES] = { "captured.bytes", "Captured bytes " },
60 [CLIENT_CTR_PKTS] = { "captured.pkts", "Captured packets " },
61 [CLIENT_CTR_2BIG] = { "bpf.too_big", "Captured data too big " },
62 [CLIENT_CTR_NOMEM] = { "client.no_mem", "No memory available " },
63 [CLIENT_CTR_QERR] = { "client.queue_err", "Can not queue data " },
Holger Hans Peter Freytherc3455dc2016-08-19 17:31:01 +020064 [CLIENT_CTR_PERR] = { "client.pcap_err", "libpcap error " },
65 [CLIENT_CTR_WERR] = { "client.write_err", "Write error " },
Holger Hans Peter Freytherfa5572e2016-08-19 20:27:35 +020066 [CLIENT_CTR_P_RECV] = { "pcap.recv", "PCAP received packets " },
67 [CLIENT_CTR_P_DROP] = { "pcap.drop", "PCAP dropped packets " },
68 [CLIENT_CTR_P_IFDROP] = { "pcap.ifdrop", "iface dropped packets " },
Holger Hans Peter Freytherf4164632016-08-18 18:39:53 +020069};
70
71static const struct rate_ctr_group_desc pcap_client_ctr_group_desc = {
72 .group_name_prefix = "pcap.client",
73 .group_description = "PCAP Client statistics",
74 .num_ctr = ARRAY_SIZE(pcap_client_ctr_desc),
75 .ctr_desc = pcap_client_ctr_desc,
76 .class_id = OSMO_STATS_CLASS_GLOBAL,
77};
78
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020079static struct vty_app_info vty_info = {
80 .name = "OsmoPCAPClient",
81 .version = PACKAGE_VERSION,
82 .go_parent_cb = osmopcap_go_parent,
83 .is_config_node = osmopcap_is_config_node,
84};
85
86static void print_usage()
87{
88 printf("Usage: osmo_pcap_client\n");
89}
90
91static void print_help()
92{
93 printf(" Some useful help...\n");
94 printf(" -h --help this text\n");
95 printf(" -D --daemonize Fork the process into a background daemon\n");
96 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
97 printf(" -s --disable-color\n");
98 printf(" -T --timestamp. Print a timestamp in the debug output.\n");
99 printf(" -e --log-level number. Set a global loglevel.\n");
100 printf(" -c --config-file filename The config file to use.\n");
101}
102
103static void handle_options(int argc, char **argv)
104{
105 while (1) {
106 int option_index = 0, c;
107 static struct option long_options[] = {
108 {"help", 0, 0, 'h'},
109 {"daemonize", 0, 0, 'D'},
110 {"debug", 1, 0, 'd'},
111 {"disable-color", 0, 0, 's'},
112 {"timestamp", 0, 0, 'T'},
113 {"log-level", 1, 0, 'e'},
114 {"config-file", 1, 0, 'c'},
115 {0, 0, 0, 0}
116 };
117
118 c = getopt_long(argc, argv, "hd:DsTc:e:",
119 long_options, &option_index);
120 if (c == -1)
121 break;
122
123 switch (c) {
124 case 'h':
125 print_usage();
126 print_help();
127 exit(0);
128 case 'D':
129 daemonize = 1;
130 break;
131 case 'd':
132 log_parse_category_mask(osmo_stderr_target, optarg);
133 break;
134 case 's':
135 log_set_use_color(osmo_stderr_target, 0);
136 break;
137 case 'T':
138 log_set_print_timestamp(osmo_stderr_target, 1);
139 break;
140 case 'e':
141 log_set_log_level(osmo_stderr_target, atoi(optarg));
142 break;
143 case 'c':
144 config_file = strdup(optarg);
145 break;
146 default:
147 /* ignore */
148 break;
149 }
150 }
151}
152
153static void signal_handler(int signal)
154{
155 fprintf(stdout, "signal %u received\n", signal);
156
157 switch (signal) {
158 case SIGINT:
159 exit(0);
160 break;
161 case SIGABRT:
162 /* in case of abort, we want to obtain a talloc report
163 * and then return to the caller, who will abort the process */
164 case SIGUSR1:
165 talloc_report(tall_vty_ctx, stderr);
166 talloc_report_full(tall_bsc_ctx, stderr);
167 break;
168 default:
169 break;
170 }
171}
172
173static void talloc_init_ctx()
174{
175 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
176 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
177 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
178}
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200179
180int main(int argc, char **argv)
181{
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200182 int rc;
183
184 talloc_init_ctx();
185 osmo_init_logging(&log_info);
186
187 vty_info.copyright = osmopcap_copyright;
188 vty_init(&vty_info);
189 logging_vty_add_cmds(&log_info);
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +0200190 osmo_stats_vty_add_cmds(&log_info);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200191
192 /* parse options */
193 handle_options(argc, argv);
194
195 rate_ctr_init(tall_bsc_ctx);
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +0200196 osmo_stats_init(tall_bsc_ctx);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200197
198 /* seed the PRNG */
199 srand(time(NULL));
200
201
202 signal(SIGINT, &signal_handler);
203 signal(SIGABRT, &signal_handler);
204 signal(SIGUSR1, &signal_handler);
205 osmo_init_ignore_signals();
206
Holger Hans Peter Freytherc2667962016-08-25 23:07:44 +0200207 osmo_tls_init();
208
Holger Hans Peter Freyther86282d62016-08-18 18:34:27 +0200209 rc = telnet_init(tall_bsc_ctx, NULL, 4240);
210 if (rc < 0) {
211 LOGP(DCLIENT, LOGL_ERROR, "Failed to bind telnet interface\n");
212 exit(1);
213 }
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200214
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200215 pcap_client = talloc_zero(tall_bsc_ctx, struct osmo_pcap_client);
216 if (!pcap_client) {
217 LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate osmo_pcap_client.\n");
218 exit(1);
219 }
220 pcap_client->fd.fd = -1;
Holger Hans Peter Freytherc2667962016-08-25 23:07:44 +0200221 pcap_client->tls_verify = true;
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200222 vty_client_init(pcap_client);
223
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200224 /* initialize the queue */
225 osmo_wqueue_init(&pcap_client->wqueue, 10);
226 pcap_client->wqueue.bfd.fd = -1;
227
Holger Hans Peter Freytherf4164632016-08-18 18:39:53 +0200228 /* initialize the stats interface */
229 pcap_client->ctrg = rate_ctr_group_alloc(pcap_client, &pcap_client_ctr_group_desc, 0);
230 if (!pcap_client->ctrg) {
231 LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate rate ctr\n");
232 exit(1);
233 }
Holger Hans Peter Freythercd2d3db2011-05-31 18:39:33 +0200234
235 if (vty_read_config_file(config_file, NULL) < 0) {
236 LOGP(DCLIENT, LOGL_ERROR,
237 "Failed to parse the config file: %s\n", config_file);
238 exit(1);
239 }
240
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200241 /* attempt to connect to the remote */
242 osmo_client_connect(pcap_client);
243
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200244 if (daemonize) {
245 rc = osmo_daemonize();
246 if (rc < 0) {
247 perror("Error during daemonize");
248 exit(1);
249 }
250 }
251
252 while (1) {
253 osmo_select_main(0);
254 }
255
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200256 return(0);
257}