Holger Hans Peter Freyther | 430366a | 2011-05-31 11:16:40 +0200 | [diff] [blame] | 1 | /* |
| 2 | * osmo-pcap-client code |
| 3 | * |
Holger Hans Peter Freyther | 86282d6 | 2016-08-18 18:34:27 +0200 | [diff] [blame] | 4 | * (C) 2011-2016 by Holger Hans Peter Freyther <holger@moiji-mobile.com> |
Holger Hans Peter Freyther | 430366a | 2011-05-31 11:16:40 +0200 | [diff] [blame] | 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 Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 23 | #include <osmo-pcap/common.h> |
Holger Hans Peter Freyther | 3b9b38c | 2011-05-31 17:42:13 +0200 | [diff] [blame] | 24 | #include <osmo-pcap/osmo_pcap_client.h> |
Holger Hans Peter Freyther | c266796 | 2016-08-25 23:07:44 +0200 | [diff] [blame] | 25 | #include <osmo-pcap/osmo_tls.h> |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 26 | |
| 27 | #include <osmocom/core/application.h> |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 28 | #include <osmocom/core/rate_ctr.h> |
| 29 | #include <osmocom/core/select.h> |
Holger Hans Peter Freyther | 6e938ed | 2016-08-13 10:36:58 +0200 | [diff] [blame] | 30 | #include <osmocom/core/stats.h> |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
| 32 | |
| 33 | #include <osmocom/vty/logging.h> |
| 34 | #include <osmocom/vty/telnet_interface.h> |
Holger Hans Peter Freyther | 6e938ed | 2016-08-13 10:36:58 +0200 | [diff] [blame] | 35 | #include <osmocom/vty/stats.h> |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 36 | |
Holger Hans Peter Freyther | 430366a | 2011-05-31 11:16:40 +0200 | [diff] [blame] | 37 | #include <pcap.h> |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 38 | #include <signal.h> |
Holger Hans Peter Freyther | 430366a | 2011-05-31 11:16:40 +0200 | [diff] [blame] | 39 | #include <stdio.h> |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 40 | #include <string.h> |
| 41 | #include <time.h> |
| 42 | |
| 43 | #define _GNU_SOURCE |
| 44 | #include <getopt.h> |
| 45 | |
| 46 | #include "osmopcapconfig.h" |
| 47 | |
| 48 | static const char *config_file = "osmo-pcap-client.cfg"; |
| 49 | static int daemonize = 0; |
| 50 | |
Pau Espin Pedrol | 2828690 | 2018-10-08 13:24:33 +0200 | [diff] [blame] | 51 | void *tall_cli_ctx; |
Holger Hans Peter Freyther | 3b9b38c | 2011-05-31 17:42:13 +0200 | [diff] [blame] | 52 | struct osmo_pcap_client *pcap_client; |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 53 | extern void *tall_msgb_ctx; |
| 54 | extern void *tall_ctr_ctx; |
| 55 | |
Holger Hans Peter Freyther | f416463 | 2016-08-18 18:39:53 +0200 | [diff] [blame] | 56 | |
| 57 | static const struct rate_ctr_desc pcap_client_ctr_desc[] = { |
Pau Espin Pedrol | f10c578 | 2018-10-08 20:20:10 +0200 | [diff] [blame] | 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 " }, |
| 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 Freyther | f416463 | 2016-08-18 18:39:53 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static const struct rate_ctr_group_desc pcap_client_ctr_group_desc = { |
Pau Espin Pedrol | f10c578 | 2018-10-08 20:20:10 +0200 | [diff] [blame] | 72 | .group_name_prefix = "pcap:client", |
Holger Hans Peter Freyther | f416463 | 2016-08-18 18:39:53 +0200 | [diff] [blame] | 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 Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 79 | static 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 | |
| 86 | static void print_usage() |
| 87 | { |
| 88 | printf("Usage: osmo_pcap_client\n"); |
| 89 | } |
| 90 | |
| 91 | static 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 Steiger | 52f06fd | 2021-04-09 23:57:01 +0200 | [diff] [blame^] | 101 | |
| 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 | |
| 107 | static 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 Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void handle_options(int argc, char **argv) |
| 132 | { |
| 133 | while (1) { |
| 134 | int option_index = 0, c; |
Joachim Steiger | 52f06fd | 2021-04-09 23:57:01 +0200 | [diff] [blame^] | 135 | static int long_option = 0; |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 136 | 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 Steiger | 52f06fd | 2021-04-09 23:57:01 +0200 | [diff] [blame^] | 144 | {"vty-ref-mode", 1, &long_option, 1}, |
| 145 | {"vty-ref-xml", 0, &long_option, 2}, |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 146 | {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 Steiger | 52f06fd | 2021-04-09 23:57:01 +0200 | [diff] [blame^] | 159 | case 0: |
| 160 | handle_long_options(argv[0], long_option); |
| 161 | break; |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 162 | 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 Pedrol | 716a4db | 2020-11-25 19:11:43 +0100 | [diff] [blame] | 187 | static void signal_handler(int signum) |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 188 | { |
Pau Espin Pedrol | 716a4db | 2020-11-25 19:11:43 +0100 | [diff] [blame] | 189 | fprintf(stdout, "signal %u received\n", signum); |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 190 | |
Pau Espin Pedrol | 716a4db | 2020-11-25 19:11:43 +0100 | [diff] [blame] | 191 | switch (signum) { |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 192 | case SIGINT: |
| 193 | exit(0); |
| 194 | break; |
| 195 | case SIGABRT: |
Pau Espin Pedrol | 716a4db | 2020-11-25 19:11:43 +0100 | [diff] [blame] | 196 | /* 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 Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 207 | case SIGUSR1: |
| 208 | talloc_report(tall_vty_ctx, stderr); |
Pau Espin Pedrol | 2828690 | 2018-10-08 13:24:33 +0200 | [diff] [blame] | 209 | talloc_report_full(tall_cli_ctx, stderr); |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 210 | break; |
| 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static void talloc_init_ctx() |
| 217 | { |
Pau Espin Pedrol | 2828690 | 2018-10-08 13:24:33 +0200 | [diff] [blame] | 218 | 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 Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 221 | } |
Holger Hans Peter Freyther | 430366a | 2011-05-31 11:16:40 +0200 | [diff] [blame] | 222 | |
| 223 | int main(int argc, char **argv) |
| 224 | { |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 225 | int rc; |
| 226 | |
| 227 | talloc_init_ctx(); |
Pau Espin Pedrol | dc27ca8 | 2018-10-08 13:28:08 +0200 | [diff] [blame] | 228 | osmo_init_logging2(tall_cli_ctx, &log_info); |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 229 | |
| 230 | vty_info.copyright = osmopcap_copyright; |
| 231 | vty_init(&vty_info); |
Pau Espin Pedrol | 210ed93 | 2019-08-05 16:28:11 +0200 | [diff] [blame] | 232 | logging_vty_add_cmds(); |
| 233 | osmo_stats_vty_add_cmds(); |
Joachim Steiger | 52f06fd | 2021-04-09 23:57:01 +0200 | [diff] [blame^] | 234 | vty_client_init(tall_cli_ctx); |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 235 | |
| 236 | /* parse options */ |
| 237 | handle_options(argc, argv); |
| 238 | |
Pau Espin Pedrol | 2828690 | 2018-10-08 13:24:33 +0200 | [diff] [blame] | 239 | rate_ctr_init(tall_cli_ctx); |
| 240 | osmo_stats_init(tall_cli_ctx); |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 241 | |
| 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 Freyther | c266796 | 2016-08-25 23:07:44 +0200 | [diff] [blame] | 251 | osmo_tls_init(); |
| 252 | |
Pau Espin Pedrol | 2828690 | 2018-10-08 13:24:33 +0200 | [diff] [blame] | 253 | rc = telnet_init(tall_cli_ctx, NULL, OSMO_VTY_PORT_PCAP_CLIENT); |
Holger Hans Peter Freyther | 86282d6 | 2016-08-18 18:34:27 +0200 | [diff] [blame] | 254 | if (rc < 0) { |
| 255 | LOGP(DCLIENT, LOGL_ERROR, "Failed to bind telnet interface\n"); |
| 256 | exit(1); |
| 257 | } |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 258 | |
Pau Espin Pedrol | f946fa2 | 2018-10-08 15:35:13 +0200 | [diff] [blame] | 259 | pcap_client = osmo_pcap_client_alloc(tall_cli_ctx); |
Holger Hans Peter Freyther | 3b9b38c | 2011-05-31 17:42:13 +0200 | [diff] [blame] | 260 | if (!pcap_client) { |
| 261 | LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate osmo_pcap_client.\n"); |
| 262 | exit(1); |
| 263 | } |
Holger Hans Peter Freyther | 3b9b38c | 2011-05-31 17:42:13 +0200 | [diff] [blame] | 264 | |
Holger Hans Peter Freyther | 7728820 | 2011-05-31 21:19:22 +0200 | [diff] [blame] | 265 | /* initialize the queue */ |
Holger Hans Peter Freyther | e3d7c3e | 2016-11-08 15:02:38 +0100 | [diff] [blame] | 266 | INIT_LLIST_HEAD(&pcap_client->conns); |
Holger Hans Peter Freyther | 1448f50 | 2016-11-08 22:53:32 +0100 | [diff] [blame] | 267 | osmo_client_conn_init(&pcap_client->conn, pcap_client); |
| 268 | pcap_client->conn.name = "default"; |
Holger Hans Peter Freyther | 7728820 | 2011-05-31 21:19:22 +0200 | [diff] [blame] | 269 | |
Holger Hans Peter Freyther | f416463 | 2016-08-18 18:39:53 +0200 | [diff] [blame] | 270 | /* 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 Freyther | cd2d3db | 2011-05-31 18:39:33 +0200 | [diff] [blame] | 276 | |
| 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 Freyther | 7728820 | 2011-05-31 21:19:22 +0200 | [diff] [blame] | 283 | /* attempt to connect to the remote */ |
Holger Hans Peter Freyther | d7ab53d | 2016-11-08 23:00:33 +0100 | [diff] [blame] | 284 | if (pcap_client->conn.srv_ip && pcap_client->conn.srv_port > 0) |
| 285 | osmo_client_connect(&pcap_client->conn); |
Holger Hans Peter Freyther | 7728820 | 2011-05-31 21:19:22 +0200 | [diff] [blame] | 286 | |
Holger Hans Peter Freyther | 530ecc0 | 2011-05-31 15:47:44 +0200 | [diff] [blame] | 287 | 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 Freyther | 430366a | 2011-05-31 11:16:40 +0200 | [diff] [blame] | 299 | return(0); |
| 300 | } |