blob: 945a871f96ecbd2c60581cfd09f4febfa0e4d2e6 [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 " },
63};
64
65static const struct rate_ctr_group_desc pcap_client_ctr_group_desc = {
66 .group_name_prefix = "pcap.client",
67 .group_description = "PCAP Client statistics",
68 .num_ctr = ARRAY_SIZE(pcap_client_ctr_desc),
69 .ctr_desc = pcap_client_ctr_desc,
70 .class_id = OSMO_STATS_CLASS_GLOBAL,
71};
72
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020073static struct vty_app_info vty_info = {
74 .name = "OsmoPCAPClient",
75 .version = PACKAGE_VERSION,
76 .go_parent_cb = osmopcap_go_parent,
77 .is_config_node = osmopcap_is_config_node,
78};
79
80static void print_usage()
81{
82 printf("Usage: osmo_pcap_client\n");
83}
84
85static void print_help()
86{
87 printf(" Some useful help...\n");
88 printf(" -h --help this text\n");
89 printf(" -D --daemonize Fork the process into a background daemon\n");
90 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
91 printf(" -s --disable-color\n");
92 printf(" -T --timestamp. Print a timestamp in the debug output.\n");
93 printf(" -e --log-level number. Set a global loglevel.\n");
94 printf(" -c --config-file filename The config file to use.\n");
95}
96
97static void handle_options(int argc, char **argv)
98{
99 while (1) {
100 int option_index = 0, c;
101 static struct option long_options[] = {
102 {"help", 0, 0, 'h'},
103 {"daemonize", 0, 0, 'D'},
104 {"debug", 1, 0, 'd'},
105 {"disable-color", 0, 0, 's'},
106 {"timestamp", 0, 0, 'T'},
107 {"log-level", 1, 0, 'e'},
108 {"config-file", 1, 0, 'c'},
109 {0, 0, 0, 0}
110 };
111
112 c = getopt_long(argc, argv, "hd:DsTc:e:",
113 long_options, &option_index);
114 if (c == -1)
115 break;
116
117 switch (c) {
118 case 'h':
119 print_usage();
120 print_help();
121 exit(0);
122 case 'D':
123 daemonize = 1;
124 break;
125 case 'd':
126 log_parse_category_mask(osmo_stderr_target, optarg);
127 break;
128 case 's':
129 log_set_use_color(osmo_stderr_target, 0);
130 break;
131 case 'T':
132 log_set_print_timestamp(osmo_stderr_target, 1);
133 break;
134 case 'e':
135 log_set_log_level(osmo_stderr_target, atoi(optarg));
136 break;
137 case 'c':
138 config_file = strdup(optarg);
139 break;
140 default:
141 /* ignore */
142 break;
143 }
144 }
145}
146
147static void signal_handler(int signal)
148{
149 fprintf(stdout, "signal %u received\n", signal);
150
151 switch (signal) {
152 case SIGINT:
153 exit(0);
154 break;
155 case SIGABRT:
156 /* in case of abort, we want to obtain a talloc report
157 * and then return to the caller, who will abort the process */
158 case SIGUSR1:
159 talloc_report(tall_vty_ctx, stderr);
160 talloc_report_full(tall_bsc_ctx, stderr);
161 break;
162 default:
163 break;
164 }
165}
166
167static void talloc_init_ctx()
168{
169 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
170 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
171 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
172}
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200173
174int main(int argc, char **argv)
175{
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200176 int rc;
177
178 talloc_init_ctx();
179 osmo_init_logging(&log_info);
180
181 vty_info.copyright = osmopcap_copyright;
182 vty_init(&vty_info);
183 logging_vty_add_cmds(&log_info);
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +0200184 osmo_stats_vty_add_cmds(&log_info);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200185
186 /* parse options */
187 handle_options(argc, argv);
188
189 rate_ctr_init(tall_bsc_ctx);
Holger Hans Peter Freyther6e938ed2016-08-13 10:36:58 +0200190 osmo_stats_init(tall_bsc_ctx);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200191
192 /* seed the PRNG */
193 srand(time(NULL));
194
195
196 signal(SIGINT, &signal_handler);
197 signal(SIGABRT, &signal_handler);
198 signal(SIGUSR1, &signal_handler);
199 osmo_init_ignore_signals();
200
Holger Hans Peter Freyther86282d62016-08-18 18:34:27 +0200201 rc = telnet_init(tall_bsc_ctx, NULL, 4240);
202 if (rc < 0) {
203 LOGP(DCLIENT, LOGL_ERROR, "Failed to bind telnet interface\n");
204 exit(1);
205 }
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200206
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200207 pcap_client = talloc_zero(tall_bsc_ctx, struct osmo_pcap_client);
208 if (!pcap_client) {
209 LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate osmo_pcap_client.\n");
210 exit(1);
211 }
212 pcap_client->fd.fd = -1;
213 vty_client_init(pcap_client);
214
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200215 /* initialize the queue */
216 osmo_wqueue_init(&pcap_client->wqueue, 10);
217 pcap_client->wqueue.bfd.fd = -1;
218
Holger Hans Peter Freytherf4164632016-08-18 18:39:53 +0200219 /* initialize the stats interface */
220 pcap_client->ctrg = rate_ctr_group_alloc(pcap_client, &pcap_client_ctr_group_desc, 0);
221 if (!pcap_client->ctrg) {
222 LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate rate ctr\n");
223 exit(1);
224 }
Holger Hans Peter Freythercd2d3db2011-05-31 18:39:33 +0200225
226 if (vty_read_config_file(config_file, NULL) < 0) {
227 LOGP(DCLIENT, LOGL_ERROR,
228 "Failed to parse the config file: %s\n", config_file);
229 exit(1);
230 }
231
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200232 /* attempt to connect to the remote */
233 osmo_client_connect(pcap_client);
234
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200235 if (daemonize) {
236 rc = osmo_daemonize();
237 if (rc < 0) {
238 perror("Error during daemonize");
239 exit(1);
240 }
241 }
242
243 while (1) {
244 osmo_select_main(0);
245 }
246
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200247 return(0);
248}