blob: 91cfb0cd3d3cbe5e6b977c975e0eb2c4c5d6aeff [file] [log] [blame]
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +02001/*
2 * osmo-pcap-client 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
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
Holger Hans Peter Freytherd5cbb672011-08-05 18:02:47 +020026#include <osmocore/process.h>
27#include <osmocore/rate_ctr.h>
28#include <osmocore/select.h>
29#include <osmocore/talloc.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020030
31#include <osmocom/vty/logging.h>
32#include <osmocom/vty/telnet_interface.h>
33
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +020034#include <pcap.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020035#include <signal.h>
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +020036#include <stdio.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020037#include <string.h>
38#include <time.h>
39
40#define _GNU_SOURCE
41#include <getopt.h>
42
43#include "osmopcapconfig.h"
44
Holger Hans Peter Freytherd5cbb672011-08-05 18:02:47 +020045
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020046static const char *config_file = "osmo-pcap-client.cfg";
47static int daemonize = 0;
Holger Hans Peter Freytherd5cbb672011-08-05 18:02:47 +020048static struct log_target *stderr_target = NULL;
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020049
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 Freytherd5cbb672011-08-05 18:02:47 +020055
56/* drop in */
57static void osmo_init_ignore_signals(void)
58{
59 /* Signals that by default would terminate */
60 signal(SIGPIPE, SIG_IGN);
61 signal(SIGALRM, SIG_IGN);
62 signal(SIGHUP, SIG_IGN);
63 signal(SIGIO, SIG_IGN);
64}
65
66static int osmo_init_logging(const struct log_info *log_info)
67{
68 log_init(log_info);
69 stderr_target = log_target_create_stderr();
70 if (!stderr_target)
71 return -1;
72
73 log_add_target(stderr_target);
74 log_set_all_filter(stderr_target, 1);
75 return 0;
76}
77
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020078static struct vty_app_info vty_info = {
79 .name = "OsmoPCAPClient",
80 .version = PACKAGE_VERSION,
81 .go_parent_cb = osmopcap_go_parent,
82 .is_config_node = osmopcap_is_config_node,
83};
84
85static void print_usage()
86{
87 printf("Usage: osmo_pcap_client\n");
88}
89
90static void print_help()
91{
92 printf(" Some useful help...\n");
93 printf(" -h --help this text\n");
94 printf(" -D --daemonize Fork the process into a background daemon\n");
95 printf(" -d option --debug=DRLL:DCC:DMM:DRR:DRSL:DNM enable debugging\n");
96 printf(" -s --disable-color\n");
97 printf(" -T --timestamp. Print a timestamp in the debug output.\n");
98 printf(" -e --log-level number. Set a global loglevel.\n");
99 printf(" -c --config-file filename The config file to use.\n");
100}
101
102static void handle_options(int argc, char **argv)
103{
104 while (1) {
105 int option_index = 0, c;
106 static struct option long_options[] = {
107 {"help", 0, 0, 'h'},
108 {"daemonize", 0, 0, 'D'},
109 {"debug", 1, 0, 'd'},
110 {"disable-color", 0, 0, 's'},
111 {"timestamp", 0, 0, 'T'},
112 {"log-level", 1, 0, 'e'},
113 {"config-file", 1, 0, 'c'},
114 {0, 0, 0, 0}
115 };
116
117 c = getopt_long(argc, argv, "hd:DsTc:e:",
118 long_options, &option_index);
119 if (c == -1)
120 break;
121
122 switch (c) {
123 case 'h':
124 print_usage();
125 print_help();
126 exit(0);
127 case 'D':
128 daemonize = 1;
129 break;
130 case 'd':
Holger Hans Peter Freytherd5cbb672011-08-05 18:02:47 +0200131 log_parse_category_mask(stderr_target, optarg);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200132 break;
133 case 's':
Holger Hans Peter Freytherd5cbb672011-08-05 18:02:47 +0200134 log_set_use_color(stderr_target, 0);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200135 break;
136 case 'T':
Holger Hans Peter Freytherd5cbb672011-08-05 18:02:47 +0200137 log_set_print_timestamp(stderr_target, 1);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200138 break;
139 case 'e':
Holger Hans Peter Freytherd5cbb672011-08-05 18:02:47 +0200140 log_set_log_level(stderr_target, atoi(optarg));
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200141 break;
142 case 'c':
143 config_file = strdup(optarg);
144 break;
145 default:
146 /* ignore */
147 break;
148 }
149 }
150}
151
152static void signal_handler(int signal)
153{
154 fprintf(stdout, "signal %u received\n", signal);
155
156 switch (signal) {
157 case SIGINT:
158 exit(0);
159 break;
160 case SIGABRT:
161 /* in case of abort, we want to obtain a talloc report
162 * and then return to the caller, who will abort the process */
163 case SIGUSR1:
164 talloc_report(tall_vty_ctx, stderr);
165 talloc_report_full(tall_bsc_ctx, stderr);
166 break;
167 default:
168 break;
169 }
170}
171
172static void talloc_init_ctx()
173{
174 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
175 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb");
176 tall_ctr_ctx = talloc_named_const(tall_bsc_ctx, 0, "counter");
177}
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200178
179int main(int argc, char **argv)
180{
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200181 int rc;
182
183 talloc_init_ctx();
184 osmo_init_logging(&log_info);
185
186 vty_info.copyright = osmopcap_copyright;
187 vty_init(&vty_info);
Holger Hans Peter Freytherd5cbb672011-08-05 18:02:47 +0200188 logging_vty_add_cmds();
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200189
190 /* parse options */
191 handle_options(argc, argv);
192
193 rate_ctr_init(tall_bsc_ctx);
194
195 /* seed the PRNG */
196 srand(time(NULL));
197
198
199 signal(SIGINT, &signal_handler);
200 signal(SIGABRT, &signal_handler);
201 signal(SIGUSR1, &signal_handler);
202 osmo_init_ignore_signals();
203
204 telnet_init(tall_bsc_ctx, NULL, 4240);
205
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200206 pcap_client = talloc_zero(tall_bsc_ctx, struct osmo_pcap_client);
207 if (!pcap_client) {
208 LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate osmo_pcap_client.\n");
209 exit(1);
210 }
211 pcap_client->fd.fd = -1;
212 vty_client_init(pcap_client);
213
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200214 /* initialize the queue */
Holger Hans Peter Freytherd5cbb672011-08-05 18:02:47 +0200215 write_queue_init(&pcap_client->wqueue, 10);
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200216 pcap_client->wqueue.bfd.fd = -1;
217
Holger Hans Peter Freythercd2d3db2011-05-31 18:39:33 +0200218
219 if (vty_read_config_file(config_file, NULL) < 0) {
220 LOGP(DCLIENT, LOGL_ERROR,
221 "Failed to parse the config file: %s\n", config_file);
222 exit(1);
223 }
224
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200225 /* attempt to connect to the remote */
226 osmo_client_connect(pcap_client);
227
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200228 if (daemonize) {
229 rc = osmo_daemonize();
230 if (rc < 0) {
231 perror("Error during daemonize");
232 exit(1);
233 }
234 }
235
236 while (1) {
Holger Hans Peter Freytherd5cbb672011-08-05 18:02:47 +0200237 bsc_select_main(0);
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200238 }
239
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200240 return(0);
241}