Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * osmo-pcap-server code |
| 3 | * |
Holger Hans Peter Freyther | 918be51 | 2016-08-18 18:37:13 +0200 | [diff] [blame] | 4 | * (C) 2011-2016 by Holger Hans Peter Freyther <holger@moiji-mobile.com> |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +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 | |
| 23 | #include <osmo-pcap/common.h> |
| 24 | #include <osmo-pcap/osmo_pcap_server.h> |
Holger Hans Peter Freyther | 9ea4da4 | 2016-09-06 11:38:56 +0200 | [diff] [blame^] | 25 | #include <osmo-pcap/osmo_tls.h> |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 26 | |
| 27 | #include <osmocom/core/application.h> |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +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 | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | 99526a6 | 2016-08-19 19:15:39 +0200 | [diff] [blame] | 32 | #include <osmocom/core/utils.h> |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 33 | |
| 34 | #include <osmocom/vty/logging.h> |
| 35 | #include <osmocom/vty/telnet_interface.h> |
Holger Hans Peter Freyther | 6e938ed | 2016-08-13 10:36:58 +0200 | [diff] [blame] | 36 | #include <osmocom/vty/stats.h> |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 37 | |
| 38 | #include <pcap.h> |
| 39 | #include <signal.h> |
| 40 | #include <stdio.h> |
| 41 | #include <string.h> |
| 42 | #include <time.h> |
| 43 | |
| 44 | #define _GNU_SOURCE |
| 45 | #include <getopt.h> |
| 46 | |
| 47 | #include "osmopcapconfig.h" |
| 48 | |
| 49 | static const char *config_file = "osmo-pcap-server.cfg"; |
| 50 | static int daemonize = 0; |
| 51 | |
| 52 | void *tall_bsc_ctx; |
| 53 | struct osmo_pcap_server *pcap_server; |
| 54 | extern void *tall_msgb_ctx; |
| 55 | extern void *tall_ctr_ctx; |
| 56 | |
Holger Hans Peter Freyther | 99526a6 | 2016-08-19 19:15:39 +0200 | [diff] [blame] | 57 | |
| 58 | static const struct rate_ctr_desc pcap_peer_ctr_desc[] = { |
| 59 | [PEER_CTR_CONNECT] = { "peer.connect", "Connect of a peer " }, |
| 60 | [PEER_CTR_BYTES] = { "peer.bytes", "Received bytes " }, |
| 61 | [PEER_CTR_PKTS] = { "peer.pkts", "Received packets " }, |
| 62 | [PEER_CTR_PROTATE] = { "peer.file_rotated","Capture file rotated" }, |
| 63 | }; |
| 64 | |
| 65 | static const struct rate_ctr_desc pcap_server_ctr_desc[] = { |
| 66 | [SERVER_CTR_CONNECT] = { "server.connect", "Connect of a peer " }, |
| 67 | [SERVER_CTR_BYTES] = { "server.bytes", "Received bytes " }, |
| 68 | [SERVER_CTR_PKTS] = { "server.pkts", "Received packets " }, |
| 69 | [SERVER_CTR_PROTATE] = { "server.file_rotated", "Capture file rotated" }, |
| 70 | [SERVER_CTR_NOCLIENT] = { "server.no_client", "Unknown connected " }, |
| 71 | }; |
| 72 | |
| 73 | const struct rate_ctr_group_desc pcap_peer_group_desc = { |
| 74 | .group_name_prefix = NULL, /* will be dynamically patched */ |
| 75 | .group_description = "PCAP peer statistics", |
| 76 | .num_ctr = ARRAY_SIZE(pcap_peer_ctr_desc), |
| 77 | .ctr_desc = pcap_peer_ctr_desc, |
| 78 | .class_id = OSMO_STATS_CLASS_PEER, |
| 79 | }; |
| 80 | |
| 81 | static const struct rate_ctr_group_desc pcap_server_group_desc = { |
| 82 | .group_name_prefix = "pcap.server", |
| 83 | .group_description = "PCAP Server global statistics", |
| 84 | .num_ctr = ARRAY_SIZE(pcap_server_ctr_desc), |
| 85 | .ctr_desc = pcap_server_ctr_desc, |
| 86 | .class_id = OSMO_STATS_CLASS_GLOBAL, |
| 87 | }; |
| 88 | |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 89 | static struct vty_app_info vty_info = { |
| 90 | .name = "OsmoPCAPServer", |
| 91 | .version = PACKAGE_VERSION, |
| 92 | .go_parent_cb = osmopcap_go_parent, |
| 93 | .is_config_node = osmopcap_is_config_node, |
| 94 | }; |
| 95 | |
| 96 | static void print_usage() |
| 97 | { |
| 98 | printf("Usage: osmo_pcap_server\n"); |
| 99 | } |
| 100 | |
| 101 | static void print_help() |
| 102 | { |
| 103 | printf(" Some useful help...\n"); |
| 104 | printf(" -h --help this text\n"); |
| 105 | printf(" -D --daemonize Fork the process into a background daemon\n"); |
| 106 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 107 | printf(" -s --disable-color\n"); |
| 108 | printf(" -T --timestamp. Print a timestamp in the debug output.\n"); |
| 109 | printf(" -e --log-level number. Set a global loglevel.\n"); |
| 110 | printf(" -c --config-file filename The config file to use.\n"); |
| 111 | } |
| 112 | |
| 113 | static void handle_options(int argc, char **argv) |
| 114 | { |
| 115 | while (1) { |
| 116 | int option_index = 0, c; |
| 117 | static struct option long_options[] = { |
| 118 | {"help", 0, 0, 'h'}, |
| 119 | {"daemonize", 0, 0, 'D'}, |
| 120 | {"debug", 1, 0, 'd'}, |
| 121 | {"disable-color", 0, 0, 's'}, |
| 122 | {"timestamp", 0, 0, 'T'}, |
| 123 | {"log-level", 1, 0, 'e'}, |
| 124 | {"config-file", 1, 0, 'c'}, |
| 125 | {0, 0, 0, 0} |
| 126 | }; |
| 127 | |
| 128 | c = getopt_long(argc, argv, "hd:DsTc:e:", |
| 129 | long_options, &option_index); |
| 130 | if (c == -1) |
| 131 | break; |
| 132 | |
| 133 | switch (c) { |
| 134 | case 'h': |
| 135 | print_usage(); |
| 136 | print_help(); |
| 137 | exit(0); |
| 138 | case 'D': |
| 139 | daemonize = 1; |
| 140 | break; |
| 141 | case 'd': |
| 142 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 143 | break; |
| 144 | case 's': |
| 145 | log_set_use_color(osmo_stderr_target, 0); |
| 146 | break; |
| 147 | case 'T': |
| 148 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 149 | break; |
| 150 | case 'e': |
| 151 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 152 | break; |
| 153 | case 'c': |
| 154 | config_file = strdup(optarg); |
| 155 | break; |
| 156 | default: |
| 157 | /* ignore */ |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static void signal_handler(int signal) |
| 164 | { |
| 165 | fprintf(stdout, "signal %u received\n", signal); |
| 166 | |
| 167 | switch (signal) { |
| 168 | case SIGINT: |
| 169 | exit(0); |
| 170 | break; |
| 171 | case SIGABRT: |
| 172 | /* in case of abort, we want to obtain a talloc report |
| 173 | * and then return to the caller, who will abort the process */ |
| 174 | case SIGUSR1: |
| 175 | talloc_report(tall_vty_ctx, stderr); |
| 176 | talloc_report_full(tall_bsc_ctx, stderr); |
| 177 | break; |
Daniel Willmann | c7401c6 | 2011-07-17 17:48:18 +0200 | [diff] [blame] | 178 | case SIGHUP: |
| 179 | osmo_pcap_server_reopen(pcap_server); |
| 180 | break; |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 181 | default: |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | static void talloc_init_ctx() |
| 187 | { |
| 188 | tall_bsc_ctx = talloc_named_const(NULL, 0, "server"); |
| 189 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 190 | tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter"); |
| 191 | } |
| 192 | |
| 193 | int main(int argc, char **argv) |
| 194 | { |
| 195 | int rc; |
| 196 | |
| 197 | talloc_init_ctx(); |
| 198 | osmo_init_logging(&log_info); |
| 199 | |
| 200 | vty_info.copyright = osmopcap_copyright; |
| 201 | vty_init(&vty_info); |
| 202 | logging_vty_add_cmds(&log_info); |
Holger Hans Peter Freyther | 6e938ed | 2016-08-13 10:36:58 +0200 | [diff] [blame] | 203 | osmo_stats_vty_add_cmds(&log_info); |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 204 | |
| 205 | /* parse options */ |
| 206 | handle_options(argc, argv); |
| 207 | |
| 208 | rate_ctr_init(tall_bsc_ctx); |
Holger Hans Peter Freyther | 6e938ed | 2016-08-13 10:36:58 +0200 | [diff] [blame] | 209 | osmo_stats_init(tall_bsc_ctx); |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 210 | |
| 211 | /* seed the PRNG */ |
| 212 | srand(time(NULL)); |
| 213 | |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 214 | signal(SIGINT, &signal_handler); |
| 215 | signal(SIGABRT, &signal_handler); |
| 216 | signal(SIGUSR1, &signal_handler); |
| 217 | osmo_init_ignore_signals(); |
Daniel Willmann | c7401c6 | 2011-07-17 17:48:18 +0200 | [diff] [blame] | 218 | signal(SIGHUP, &signal_handler); |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 219 | |
Holger Hans Peter Freyther | 9ea4da4 | 2016-09-06 11:38:56 +0200 | [diff] [blame^] | 220 | osmo_tls_init(); |
| 221 | |
Holger Hans Peter Freyther | 918be51 | 2016-08-18 18:37:13 +0200 | [diff] [blame] | 222 | rc = telnet_init(tall_bsc_ctx, NULL, 4241); |
| 223 | if (rc < 0) { |
| 224 | LOGP(DCLIENT, LOGL_ERROR, "Failed to bind telnet interface\n"); |
| 225 | exit(1); |
| 226 | } |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 227 | |
| 228 | pcap_server = talloc_zero(tall_bsc_ctx, struct osmo_pcap_server); |
| 229 | if (!pcap_server) { |
| 230 | LOGP(DSERVER, LOGL_ERROR, "Failed to allocate osmo_pcap_server.\n"); |
| 231 | exit(1); |
| 232 | } |
Holger Hans Peter Freyther | 99526a6 | 2016-08-19 19:15:39 +0200 | [diff] [blame] | 233 | pcap_server->ctrg = rate_ctr_group_alloc(pcap_server, &pcap_server_group_desc, 0); |
| 234 | if (!pcap_server->ctrg) { |
| 235 | LOGP(DSERVER, LOGL_ERROR, "Failed to allocate rate counter.\n"); |
| 236 | exit(1); |
| 237 | } |
| 238 | |
Holger Hans Peter Freyther | 9f6127f | 2011-05-31 22:52:41 +0200 | [diff] [blame] | 239 | INIT_LLIST_HEAD(&pcap_server->conn); |
Holger Hans Peter Freyther | 88c07f2 | 2011-06-01 14:02:54 +0200 | [diff] [blame] | 240 | pcap_server->base_path = talloc_strdup(pcap_server, "./"); |
| 241 | pcap_server->max_size = 1073741824; |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 242 | vty_server_init(pcap_server); |
| 243 | |
| 244 | if (vty_read_config_file(config_file, NULL) < 0) { |
| 245 | LOGP(DSERVER, LOGL_ERROR, |
| 246 | "Failed to parse the config file: %s\n", config_file); |
| 247 | exit(1); |
| 248 | } |
| 249 | |
| 250 | /* attempt to connect to the remote */ |
| 251 | if (osmo_pcap_server_listen(pcap_server) != 0) { |
| 252 | LOGP(DSERVER, LOGL_ERROR, |
| 253 | "Failed to listen for incoming data\n"); |
| 254 | exit(1); |
| 255 | } |
| 256 | |
| 257 | if (daemonize) { |
| 258 | rc = osmo_daemonize(); |
| 259 | if (rc < 0) { |
| 260 | perror("Error during daemonize"); |
| 261 | exit(1); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | while (1) { |
| 266 | osmo_select_main(0); |
| 267 | } |
| 268 | |
| 269 | return(0); |
| 270 | } |