diff options
-rw-r--r-- | include/osmo-pcap/osmo_pcap_client.h | 16 | ||||
-rw-r--r-- | src/Makefile.am | 3 | ||||
-rw-r--r-- | src/osmo_client_core.c | 75 | ||||
-rw-r--r-- | src/osmo_client_main.c | 10 | ||||
-rw-r--r-- | src/osmo_client_vty.c | 108 |
5 files changed, 210 insertions, 2 deletions
diff --git a/include/osmo-pcap/osmo_pcap_client.h b/include/osmo-pcap/osmo_pcap_client.h index 8dc2ee8..719529a 100644 --- a/include/osmo-pcap/osmo_pcap_client.h +++ b/include/osmo-pcap/osmo_pcap_client.h @@ -22,13 +22,27 @@ #include <pcap.h> +#include <osmocom/core/select.h> + struct osmo_pcap_client { char *device; pcap_t *handle; + char errbuf[PCAP_ERRBUF_SIZE]; struct bpf_program *bpf; char *filter_string; + int filter_itself; + + struct osmo_fd fd; + + char *srv_ip; + int srv_port; }; -int vty_client_init(); +extern struct osmo_pcap_client *pcap_client; + +int vty_client_init(struct osmo_pcap_client *); + +int osmo_client_capture(struct osmo_pcap_client *client, const char *device); +int osmo_client_filter(struct osmo_pcap_client *client, const char *filter); diff --git a/src/Makefile.am b/src/Makefile.am index 3e014ae..bb0bd56 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,5 +4,6 @@ AM_LDFLAGS = $(LIBOSMOCORE_LIBS) $(LIBOSMOVTY_LIBS) bin_PROGRAMS = osmo_pcap_client -osmo_pcap_client_SOURCES = osmo_client_main.c osmo_common.c +osmo_pcap_client_SOURCES = osmo_client_main.c osmo_common.c \ + osmo_client_core.c osmo_client_vty.c osmo_pcap_client_LDADD = $(PCAP_LIBS) diff --git a/src/osmo_client_core.c b/src/osmo_client_core.c new file mode 100644 index 0000000..af6cd0e --- /dev/null +++ b/src/osmo_client_core.c @@ -0,0 +1,75 @@ +/* + * osmo-pcap-client code + * + * (C) 2011 by Holger Hans Peter Freyther <zecke@selfish.org> + * (C) 2011 by On-Waves + * All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include <osmo-pcap/osmo_pcap_client.h> +#include <osmo-pcap/common.h> + +#include <osmocom/core/talloc.h> + +#include <limits.h> + + +static void free_all(struct osmo_pcap_client *client) +{ + if (!client->handle) + return; + + if (client->bpf) { + pcap_freecode(client->bpf); + client->bpf = NULL; + } + + if (client->fd.fd != -1) { + osmo_fd_unregister(&client->fd); + client->fd.fd = -1; + } + + pcap_close(client->handle); + client->handle = NULL; +} + +int osmo_client_capture(struct osmo_pcap_client *client, const char *device) +{ + talloc_free(client->device); + free_all(client); + + client->device = talloc_strdup(client, device); + if (!client) { + LOGP(DCLIENT, LOGL_ERROR, "Failed to copy string.\n"); + return 1; + } + + client->handle = pcap_open_live(client->device, 2000, 0, + 1000, client->errbuf); + if (!client->handle) { + LOGP(DCLIENT, LOGL_ERROR, + "Failed to open the device: %s\n", client->errbuf); + return 2; + } + + return 0; +} + +int osmo_client_filter(struct osmo_pcap_client *client, const char *filter) +{ + return 0; +} diff --git a/src/osmo_client_main.c b/src/osmo_client_main.c index f00530e..96f01ee 100644 --- a/src/osmo_client_main.c +++ b/src/osmo_client_main.c @@ -21,6 +21,7 @@ */ #include <osmo-pcap/common.h> +#include <osmo-pcap/osmo_pcap_client.h> #include <osmocom/core/application.h> #include <osmocom/core/process.h> @@ -46,6 +47,7 @@ static const char *config_file = "osmo-pcap-client.cfg"; static int daemonize = 0; void *tall_bsc_ctx; +struct osmo_pcap_client *pcap_client; extern void *tall_msgb_ctx; extern void *tall_ctr_ctx; @@ -177,6 +179,14 @@ int main(int argc, char **argv) telnet_init(tall_bsc_ctx, NULL, 4240); + pcap_client = talloc_zero(tall_bsc_ctx, struct osmo_pcap_client); + if (!pcap_client) { + LOGP(DCLIENT, LOGL_ERROR, "Failed to allocate osmo_pcap_client.\n"); + exit(1); + } + pcap_client->fd.fd = -1; + vty_client_init(pcap_client); + if (daemonize) { rc = osmo_daemonize(); if (rc < 0) { diff --git a/src/osmo_client_vty.c b/src/osmo_client_vty.c index b262e8d..3af4dfc 100644 --- a/src/osmo_client_vty.c +++ b/src/osmo_client_vty.c @@ -21,3 +21,111 @@ */ #include <osmo-pcap/osmo_pcap_client.h> +#include <osmo-pcap/common.h> + +#include <osmocom/core/talloc.h> + +#include <stdlib.h> + + +#define PCAP_STRING "PCAP related functions\n" +#define SERVER_STRING "Server string\n" + +static struct cmd_node client_node = { + CLIENT_NODE, + "%s(client)#", + 1, +}; + +DEFUN(cfg_client, + cfg_client_cmd, + "client", + "Enter the client configuration\n") +{ + vty->node = CLIENT_NODE; + return CMD_SUCCESS; +} + +static int config_write_client(struct vty *vty) +{ + vty_out(vty, "client%s", VTY_NEWLINE); + vty_out(vty, " pcap device %s%s", + pcap_client->device, VTY_NEWLINE); + vty_out(vty, " pcap filter %s%s", + pcap_client->filter_string, VTY_NEWLINE); + vty_out(vty, " pcap detect-loop %d%s", + pcap_client->filter_itself, VTY_NEWLINE); + vty_out(vty, " server ip %s%s", + pcap_client->srv_ip, VTY_NEWLINE); + vty_out(vty, " server port %d%s", + pcap_client->srv_port, VTY_NEWLINE); + + return CMD_SUCCESS; +} + +DEFUN(cfg_client_device, + cfg_client_device_cmd, + "pcap device NAME", + PCAP_STRING "the device to filter\n" "device name\n") +{ + osmo_client_capture(pcap_client, argv[0]); + return CMD_SUCCESS; +} + +DEFUN(cfg_client_filter, + cfg_client_filter_cmd, + "pcap filter NAME", + PCAP_STRING "filter string in pcap syntax\n" "filter\n") +{ + if (osmo_client_filter(pcap_client, argv[0]) != 0) { + vty_out(vty, "Failed to set the device.%s", VTY_NEWLINE); + return CMD_WARNING; + } + + return CMD_SUCCESS; +} + +DEFUN(cfg_client_loop, + cfg_client_loop_cmd, + "pcap detect-loop (0|1)", + PCAP_STRING "detect loop and drop\n" "No detection\n" "Detection\n") +{ + pcap_client->filter_itself = atoi(argv[0]); + return CMD_SUCCESS; +} + +DEFUN(cfg_server_ip, + cfg_server_ip_cmd, + "server ip A.B.C.D", + SERVER_STRING "IP Address of the server\n" "IP\n") +{ + talloc_free(pcap_client->srv_ip); + pcap_client->srv_ip = talloc_strdup(pcap_client, argv[0]); + return CMD_SUCCESS; +} + +DEFUN(cfg_server_port, + cfg_server_port_cmd, + "server port <1-65535>", + SERVER_STRING "Port\n" "Number\n") +{ + pcap_client->srv_port = atoi(argv[0]); + return CMD_SUCCESS; +} + + +int vty_client_init(struct osmo_pcap_client *pcap) +{ + install_element(CONFIG_NODE, &cfg_client_cmd); + install_node(&client_node, config_write_client); + install_default(CLIENT_NODE); + + install_element(CLIENT_NODE, &cfg_client_device_cmd); + install_element(CLIENT_NODE, &cfg_client_filter_cmd); + install_element(CLIENT_NODE, &cfg_client_loop_cmd); + + install_element(CLIENT_NODE, &cfg_server_ip_cmd); + install_element(CLIENT_NODE, &cfg_server_port_cmd); + + return 0; +} |