blob: 4ef46cc725bde97c427a23aa4500d73ad149513c [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
Pau Espin Pedrol28286902018-10-08 13:24:33 +020051void *tall_cli_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[] = {
Pau Espin Pedrolf10c5782018-10-08 20:20:10 +020058 [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 " },
64 [CLIENT_CTR_PERR] = { "client:pcap_err", "libpcap error " },
65 [CLIENT_CTR_WERR] = { "client:write_err", "Write error " },
66 [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 = {
Pau Espin Pedrolf10c5782018-10-08 20:20:10 +020072 .group_name_prefix = "pcap:client",
Holger Hans Peter Freytherf4164632016-08-18 18:39:53 +020073 .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");
Joachim Steiger52f06fd2021-04-09 23:57:01 +0200101
102 printf("\nVTY reference generation:\n");
103 printf(" --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n");
104 printf(" --vty-ref-xml Generate the VTY reference XML output and exit.\n");
105}
106
107static void handle_long_options(const char *prog_name, const int long_option)
108{
109 static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT;
110 switch (long_option) {
111 case 1:
112 vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg);
113 if (vty_ref_mode < 0) {
114 fprintf(stderr, "%s: Unknown VTY reference generation "
115 "mode '%s'\n", prog_name, optarg);
116 exit(2);
117 }
118 break;
119 case 2:
120 fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n",
121 get_value_string(vty_ref_gen_mode_names, vty_ref_mode),
122 get_value_string(vty_ref_gen_mode_desc, vty_ref_mode));
123 vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode);
124 exit(0);
125 default:
126 fprintf(stderr, "%s: error parsing cmdline options\n", prog_name);
127 exit(2);
128 }
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200129}
130
131static void handle_options(int argc, char **argv)
132{
133 while (1) {
134 int option_index = 0, c;
Joachim Steiger52f06fd2021-04-09 23:57:01 +0200135 static int long_option = 0;
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200136 static struct option long_options[] = {
137 {"help", 0, 0, 'h'},
138 {"daemonize", 0, 0, 'D'},
139 {"debug", 1, 0, 'd'},
140 {"disable-color", 0, 0, 's'},
141 {"timestamp", 0, 0, 'T'},
142 {"log-level", 1, 0, 'e'},
143 {"config-file", 1, 0, 'c'},
Joachim Steiger52f06fd2021-04-09 23:57:01 +0200144 {"vty-ref-mode", 1, &long_option, 1},
145 {"vty-ref-xml", 0, &long_option, 2},
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200146 {0, 0, 0, 0}
147 };
148
149 c = getopt_long(argc, argv, "hd:DsTc:e:",
150 long_options, &option_index);
151 if (c == -1)
152 break;
153
154 switch (c) {
155 case 'h':
156 print_usage();
157 print_help();
158 exit(0);
Joachim Steiger52f06fd2021-04-09 23:57:01 +0200159 case 0:
160 handle_long_options(argv[0], long_option);
161 break;
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200162 case 'D':
163 daemonize = 1;
164 break;
165 case 'd':
166 log_parse_category_mask(osmo_stderr_target, optarg);
167 break;
168 case 's':
169 log_set_use_color(osmo_stderr_target, 0);
170 break;
171 case 'T':
172 log_set_print_timestamp(osmo_stderr_target, 1);
173 break;
174 case 'e':
175 log_set_log_level(osmo_stderr_target, atoi(optarg));
176 break;
177 case 'c':
178 config_file = strdup(optarg);
179 break;
180 default:
181 /* ignore */
182 break;
183 }
184 }
185}
186
Pau Espin Pedrol716a4db2020-11-25 19:11:43 +0100187static void signal_handler(int signum)
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200188{
Pau Espin Pedrol716a4db2020-11-25 19:11:43 +0100189 fprintf(stdout, "signal %u received\n", signum);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200190
Pau Espin Pedrol716a4db2020-11-25 19:11:43 +0100191 switch (signum) {
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200192 case SIGINT:
193 exit(0);
194 break;
195 case SIGABRT:
Pau Espin Pedrol716a4db2020-11-25 19:11:43 +0100196 /* in case of abort, we want to obtain a talloc report and
197 * then run default SIGABRT handler, who will generate coredump
198 * and abort the process. abort() should do this for us after we
199 * return, but program wouldn't exit if an external SIGABRT is
200 * received.
201 */
202 talloc_report(tall_vty_ctx, stderr);
203 talloc_report_full(tall_cli_ctx, stderr);
204 signal(SIGABRT, SIG_DFL);
205 raise(SIGABRT);
206 break;
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200207 case SIGUSR1:
208 talloc_report(tall_vty_ctx, stderr);
Pau Espin Pedrol28286902018-10-08 13:24:33 +0200209 talloc_report_full(tall_cli_ctx, stderr);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200210 break;
211 default:
212 break;
213 }
214}
215
216static void talloc_init_ctx()
217{
Pau Espin Pedrol28286902018-10-08 13:24:33 +0200218 tall_cli_ctx = talloc_named_const(NULL, 0, "client");
219 tall_msgb_ctx = talloc_named_const(tall_cli_ctx, 0, "msgb");
220 tall_ctr_ctx = talloc_named_const(tall_cli_ctx, 0, "counter");
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200221}
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200222
223int main(int argc, char **argv)
224{
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200225 int rc;
226
227 talloc_init_ctx();
Pau Espin Pedroldc27ca82018-10-08 13:28:08 +0200228 osmo_init_logging2(tall_cli_ctx, &log_info);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200229
230 vty_info.copyright = osmopcap_copyright;
231 vty_init(&vty_info);
Pau Espin Pedrol210ed932019-08-05 16:28:11 +0200232 logging_vty_add_cmds();
233 osmo_stats_vty_add_cmds();
Joachim Steiger52f06fd2021-04-09 23:57:01 +0200234 vty_client_init(tall_cli_ctx);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200235
236 /* parse options */
237 handle_options(argc, argv);
238
Pau Espin Pedrol28286902018-10-08 13:24:33 +0200239 rate_ctr_init(tall_cli_ctx);
240 osmo_stats_init(tall_cli_ctx);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200241
242 /* seed the PRNG */
243 srand(time(NULL));
244
245
246 signal(SIGINT, &signal_handler);
247 signal(SIGABRT, &signal_handler);
248 signal(SIGUSR1, &signal_handler);
249 osmo_init_ignore_signals();
250
Holger Hans Peter Freytherc2667962016-08-25 23:07:44 +0200251 osmo_tls_init();
252
Pau Espin Pedrol28286902018-10-08 13:24:33 +0200253 rc = telnet_init(tall_cli_ctx, NULL, OSMO_VTY_PORT_PCAP_CLIENT);
Holger Hans Peter Freyther86282d62016-08-18 18:34:27 +0200254 if (rc < 0) {
255 LOGP(DCLIENT, LOGL_ERROR, "Failed to bind telnet interface\n");
256 exit(1);
257 }
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200258
Pau Espin Pedrolf946fa22018-10-08 15:35:13 +0200259 pcap_client = osmo_pcap_client_alloc(tall_cli_ctx);
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200260 if (!pcap_client) {
261 LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate osmo_pcap_client.\n");
262 exit(1);
263 }
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200264
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200265 /* initialize the queue */
Holger Hans Peter Freythere3d7c3e2016-11-08 15:02:38 +0100266 INIT_LLIST_HEAD(&pcap_client->conns);
Holger Hans Peter Freyther1448f502016-11-08 22:53:32 +0100267 osmo_client_conn_init(&pcap_client->conn, pcap_client);
268 pcap_client->conn.name = "default";
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200269
Holger Hans Peter Freytherf4164632016-08-18 18:39:53 +0200270 /* initialize the stats interface */
271 pcap_client->ctrg = rate_ctr_group_alloc(pcap_client, &pcap_client_ctr_group_desc, 0);
272 if (!pcap_client->ctrg) {
273 LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate rate ctr\n");
274 exit(1);
275 }
Holger Hans Peter Freythercd2d3db2011-05-31 18:39:33 +0200276
277 if (vty_read_config_file(config_file, NULL) < 0) {
278 LOGP(DCLIENT, LOGL_ERROR,
279 "Failed to parse the config file: %s\n", config_file);
280 exit(1);
281 }
282
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200283 /* attempt to connect to the remote */
Holger Hans Peter Freytherd7ab53d2016-11-08 23:00:33 +0100284 if (pcap_client->conn.srv_ip && pcap_client->conn.srv_port > 0)
285 osmo_client_connect(&pcap_client->conn);
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200286
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200287 if (daemonize) {
288 rc = osmo_daemonize();
289 if (rc < 0) {
290 perror("Error during daemonize");
291 exit(1);
292 }
293 }
294
295 while (1) {
296 osmo_select_main(0);
297 }
298
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200299 return(0);
300}