Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * osmo-pcap-server code |
| 3 | * |
| 4 | * (C) 2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 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> |
| 25 | |
| 26 | #include <osmocom/core/application.h> |
| 27 | #include <osmocom/core/process.h> |
| 28 | #include <osmocom/core/rate_ctr.h> |
| 29 | #include <osmocom/core/select.h> |
| 30 | #include <osmocom/core/talloc.h> |
| 31 | |
| 32 | #include <osmocom/vty/logging.h> |
| 33 | #include <osmocom/vty/telnet_interface.h> |
| 34 | |
| 35 | #include <pcap.h> |
| 36 | #include <signal.h> |
| 37 | #include <stdio.h> |
| 38 | #include <string.h> |
| 39 | #include <time.h> |
| 40 | |
| 41 | #define _GNU_SOURCE |
| 42 | #include <getopt.h> |
| 43 | |
| 44 | #include "osmopcapconfig.h" |
| 45 | |
| 46 | static const char *config_file = "osmo-pcap-server.cfg"; |
| 47 | static int daemonize = 0; |
| 48 | |
| 49 | void *tall_bsc_ctx; |
| 50 | struct osmo_pcap_server *pcap_server; |
| 51 | extern void *tall_msgb_ctx; |
| 52 | extern void *tall_ctr_ctx; |
| 53 | |
| 54 | static struct vty_app_info vty_info = { |
| 55 | .name = "OsmoPCAPServer", |
| 56 | .version = PACKAGE_VERSION, |
| 57 | .go_parent_cb = osmopcap_go_parent, |
| 58 | .is_config_node = osmopcap_is_config_node, |
| 59 | }; |
| 60 | |
| 61 | static void print_usage() |
| 62 | { |
| 63 | printf("Usage: osmo_pcap_server\n"); |
| 64 | } |
| 65 | |
| 66 | static void print_help() |
| 67 | { |
| 68 | printf(" Some useful help...\n"); |
| 69 | printf(" -h --help this text\n"); |
| 70 | printf(" -D --daemonize Fork the process into a background daemon\n"); |
| 71 | printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n"); |
| 72 | printf(" -s --disable-color\n"); |
| 73 | printf(" -T --timestamp. Print a timestamp in the debug output.\n"); |
| 74 | printf(" -e --log-level number. Set a global loglevel.\n"); |
| 75 | printf(" -c --config-file filename The config file to use.\n"); |
| 76 | } |
| 77 | |
| 78 | static void handle_options(int argc, char **argv) |
| 79 | { |
| 80 | while (1) { |
| 81 | int option_index = 0, c; |
| 82 | static struct option long_options[] = { |
| 83 | {"help", 0, 0, 'h'}, |
| 84 | {"daemonize", 0, 0, 'D'}, |
| 85 | {"debug", 1, 0, 'd'}, |
| 86 | {"disable-color", 0, 0, 's'}, |
| 87 | {"timestamp", 0, 0, 'T'}, |
| 88 | {"log-level", 1, 0, 'e'}, |
| 89 | {"config-file", 1, 0, 'c'}, |
| 90 | {0, 0, 0, 0} |
| 91 | }; |
| 92 | |
| 93 | c = getopt_long(argc, argv, "hd:DsTc:e:", |
| 94 | long_options, &option_index); |
| 95 | if (c == -1) |
| 96 | break; |
| 97 | |
| 98 | switch (c) { |
| 99 | case 'h': |
| 100 | print_usage(); |
| 101 | print_help(); |
| 102 | exit(0); |
| 103 | case 'D': |
| 104 | daemonize = 1; |
| 105 | break; |
| 106 | case 'd': |
| 107 | log_parse_category_mask(osmo_stderr_target, optarg); |
| 108 | break; |
| 109 | case 's': |
| 110 | log_set_use_color(osmo_stderr_target, 0); |
| 111 | break; |
| 112 | case 'T': |
| 113 | log_set_print_timestamp(osmo_stderr_target, 1); |
| 114 | break; |
| 115 | case 'e': |
| 116 | log_set_log_level(osmo_stderr_target, atoi(optarg)); |
| 117 | break; |
| 118 | case 'c': |
| 119 | config_file = strdup(optarg); |
| 120 | break; |
| 121 | default: |
| 122 | /* ignore */ |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void signal_handler(int signal) |
| 129 | { |
| 130 | fprintf(stdout, "signal %u received\n", signal); |
| 131 | |
| 132 | switch (signal) { |
| 133 | case SIGINT: |
| 134 | exit(0); |
| 135 | break; |
| 136 | case SIGABRT: |
| 137 | /* in case of abort, we want to obtain a talloc report |
| 138 | * and then return to the caller, who will abort the process */ |
| 139 | case SIGUSR1: |
| 140 | talloc_report(tall_vty_ctx, stderr); |
| 141 | talloc_report_full(tall_bsc_ctx, stderr); |
| 142 | break; |
| 143 | default: |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | static void talloc_init_ctx() |
| 149 | { |
| 150 | tall_bsc_ctx = talloc_named_const(NULL, 0, "server"); |
| 151 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 152 | tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter"); |
| 153 | } |
| 154 | |
| 155 | int main(int argc, char **argv) |
| 156 | { |
| 157 | int rc; |
| 158 | |
| 159 | talloc_init_ctx(); |
| 160 | osmo_init_logging(&log_info); |
| 161 | |
| 162 | vty_info.copyright = osmopcap_copyright; |
| 163 | vty_init(&vty_info); |
| 164 | logging_vty_add_cmds(&log_info); |
| 165 | |
| 166 | /* parse options */ |
| 167 | handle_options(argc, argv); |
| 168 | |
| 169 | rate_ctr_init(tall_bsc_ctx); |
| 170 | |
| 171 | /* seed the PRNG */ |
| 172 | srand(time(NULL)); |
| 173 | |
| 174 | |
| 175 | signal(SIGINT, &signal_handler); |
| 176 | signal(SIGABRT, &signal_handler); |
| 177 | signal(SIGUSR1, &signal_handler); |
| 178 | osmo_init_ignore_signals(); |
| 179 | |
| 180 | telnet_init(tall_bsc_ctx, NULL, 4241); |
| 181 | |
| 182 | pcap_server = talloc_zero(tall_bsc_ctx, struct osmo_pcap_server); |
| 183 | if (!pcap_server) { |
| 184 | LOGP(DSERVER, LOGL_ERROR, "Failed to allocate osmo_pcap_server.\n"); |
| 185 | exit(1); |
| 186 | } |
Holger Hans Peter Freyther | 9f6127f | 2011-05-31 22:52:41 +0200 | [diff] [blame^] | 187 | INIT_LLIST_HEAD(&pcap_server->conn); |
Holger Hans Peter Freyther | 13619dd | 2011-05-31 22:09:08 +0200 | [diff] [blame] | 188 | vty_server_init(pcap_server); |
| 189 | |
| 190 | if (vty_read_config_file(config_file, NULL) < 0) { |
| 191 | LOGP(DSERVER, LOGL_ERROR, |
| 192 | "Failed to parse the config file: %s\n", config_file); |
| 193 | exit(1); |
| 194 | } |
| 195 | |
| 196 | /* attempt to connect to the remote */ |
| 197 | if (osmo_pcap_server_listen(pcap_server) != 0) { |
| 198 | LOGP(DSERVER, LOGL_ERROR, |
| 199 | "Failed to listen for incoming data\n"); |
| 200 | exit(1); |
| 201 | } |
| 202 | |
| 203 | if (daemonize) { |
| 204 | rc = osmo_daemonize(); |
| 205 | if (rc < 0) { |
| 206 | perror("Error during daemonize"); |
| 207 | exit(1); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | while (1) { |
| 212 | osmo_select_main(0); |
| 213 | } |
| 214 | |
| 215 | return(0); |
| 216 | } |