blob: 9ea0b66348e7b3779761a09a01bd0a25f55ff670 [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
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
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +020035#include <pcap.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020036#include <signal.h>
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +020037#include <stdio.h>
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020038#include <string.h>
39#include <time.h>
40
41#define _GNU_SOURCE
42#include <getopt.h>
43
44#include "osmopcapconfig.h"
45
46static const char *config_file = "osmo-pcap-client.cfg";
47static int daemonize = 0;
48
49void *tall_bsc_ctx;
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +020050struct osmo_pcap_client *pcap_client;
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +020051extern void *tall_msgb_ctx;
52extern void *tall_ctr_ctx;
53
54static struct vty_app_info vty_info = {
55 .name = "OsmoPCAPClient",
56 .version = PACKAGE_VERSION,
57 .go_parent_cb = osmopcap_go_parent,
58 .is_config_node = osmopcap_is_config_node,
59};
60
61static void print_usage()
62{
63 printf("Usage: osmo_pcap_client\n");
64}
65
66static 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
78static 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
128static 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
148static void talloc_init_ctx()
149{
150 tall_bsc_ctx = talloc_named_const(NULL, 0, "nat");
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}
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200154
155int main(int argc, char **argv)
156{
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200157 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, 4240);
181
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +0200182 pcap_client = talloc_zero(tall_bsc_ctx, struct osmo_pcap_client);
183 if (!pcap_client) {
184 LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate osmo_pcap_client.\n");
185 exit(1);
186 }
187 pcap_client->fd.fd = -1;
188 vty_client_init(pcap_client);
189
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200190 /* initialize the queue */
191 osmo_wqueue_init(&pcap_client->wqueue, 10);
192 pcap_client->wqueue.bfd.fd = -1;
193
Holger Hans Peter Freythercd2d3db2011-05-31 18:39:33 +0200194
195 if (vty_read_config_file(config_file, NULL) < 0) {
196 LOGP(DCLIENT, LOGL_ERROR,
197 "Failed to parse the config file: %s\n", config_file);
198 exit(1);
199 }
200
Holger Hans Peter Freyther77288202011-05-31 21:19:22 +0200201 /* attempt to connect to the remote */
202 osmo_client_connect(pcap_client);
203
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +0200204 if (daemonize) {
205 rc = osmo_daemonize();
206 if (rc < 0) {
207 perror("Error during daemonize");
208 exit(1);
209 }
210 }
211
212 while (1) {
213 osmo_select_main(0);
214 }
215
Holger Hans Peter Freyther430366a2011-05-31 11:16:40 +0200216 return(0);
217}