blob: 056ab18f359404f77c3d3e8d477223f8b9ed80c4 [file] [log] [blame]
Holger Hans Peter Freyther530ecc02011-05-31 15:47:44 +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
23#include <osmo-pcap/osmo_pcap_client.h>
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +020024#include <osmo-pcap/common.h>
25
26#include <osmocom/core/talloc.h>
27
28#include <stdlib.h>
29
30
31#define PCAP_STRING "PCAP related functions\n"
32#define SERVER_STRING "Server string\n"
33
34static struct cmd_node client_node = {
35 CLIENT_NODE,
36 "%s(client)#",
37 1,
38};
39
40DEFUN(cfg_client,
41 cfg_client_cmd,
42 "client",
43 "Enter the client configuration\n")
44{
45 vty->node = CLIENT_NODE;
46 return CMD_SUCCESS;
47}
48
49static int config_write_client(struct vty *vty)
50{
51 vty_out(vty, "client%s", VTY_NEWLINE);
Holger Hans Peter Freytherbac0c982011-05-31 17:52:08 +020052
53 if (pcap_client->device)
54 vty_out(vty, " pcap device %s%s",
55 pcap_client->device, VTY_NEWLINE);
56
57 if (pcap_client->filter_string)
58 vty_out(vty, " pcap filter %s%s",
59 pcap_client->filter_string, VTY_NEWLINE);
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +020060 vty_out(vty, " pcap detect-loop %d%s",
61 pcap_client->filter_itself, VTY_NEWLINE);
Holger Hans Peter Freytherbac0c982011-05-31 17:52:08 +020062
63 if (pcap_client->srv_ip)
64 vty_out(vty, " server ip %s%s",
65 pcap_client->srv_ip, VTY_NEWLINE);
66
67 if (pcap_client->srv_port > 0)
68 vty_out(vty, " server port %d%s",
69 pcap_client->srv_port, VTY_NEWLINE);
Holger Hans Peter Freyther3b9b38c2011-05-31 17:42:13 +020070
71 return CMD_SUCCESS;
72}
73
74DEFUN(cfg_client_device,
75 cfg_client_device_cmd,
76 "pcap device NAME",
77 PCAP_STRING "the device to filter\n" "device name\n")
78{
79 osmo_client_capture(pcap_client, argv[0]);
80 return CMD_SUCCESS;
81}
82
83DEFUN(cfg_client_filter,
84 cfg_client_filter_cmd,
85 "pcap filter NAME",
86 PCAP_STRING "filter string in pcap syntax\n" "filter\n")
87{
88 if (osmo_client_filter(pcap_client, argv[0]) != 0) {
89 vty_out(vty, "Failed to set the device.%s", VTY_NEWLINE);
90 return CMD_WARNING;
91 }
92
93 return CMD_SUCCESS;
94}
95
96DEFUN(cfg_client_loop,
97 cfg_client_loop_cmd,
98 "pcap detect-loop (0|1)",
99 PCAP_STRING "detect loop and drop\n" "No detection\n" "Detection\n")
100{
101 pcap_client->filter_itself = atoi(argv[0]);
102 return CMD_SUCCESS;
103}
104
105DEFUN(cfg_server_ip,
106 cfg_server_ip_cmd,
107 "server ip A.B.C.D",
108 SERVER_STRING "IP Address of the server\n" "IP\n")
109{
110 talloc_free(pcap_client->srv_ip);
111 pcap_client->srv_ip = talloc_strdup(pcap_client, argv[0]);
112 return CMD_SUCCESS;
113}
114
115DEFUN(cfg_server_port,
116 cfg_server_port_cmd,
117 "server port <1-65535>",
118 SERVER_STRING "Port\n" "Number\n")
119{
120 pcap_client->srv_port = atoi(argv[0]);
121 return CMD_SUCCESS;
122}
123
124
125int vty_client_init(struct osmo_pcap_client *pcap)
126{
127 install_element(CONFIG_NODE, &cfg_client_cmd);
128 install_node(&client_node, config_write_client);
129 install_default(CLIENT_NODE);
130
131 install_element(CLIENT_NODE, &cfg_client_device_cmd);
132 install_element(CLIENT_NODE, &cfg_client_filter_cmd);
133 install_element(CLIENT_NODE, &cfg_client_loop_cmd);
134
135 install_element(CLIENT_NODE, &cfg_server_ip_cmd);
136 install_element(CLIENT_NODE, &cfg_server_port_cmd);
137
138 return 0;
139}