2
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
3
* filter.c - Read/Write the BPF filter associated to a network interface.
5
* Luca Deri <deri@ntop.org>
6
* Rocco Carbone <rocco@ntop.org>
7
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34
* Get/Set the BPF filter associated to a network interface.
36
int intop_set_filter (int argc, char * argv [])
39
* Notice the command name.
41
char * commandname = argv [0];
45
printf ("Usage: %s [-h] [-i interface] [expression]\n", xxx)
46
char * optstring = "hi:";
49
* Reserve here space for the local variables.
51
char * interface = NULL;
57
bpf_u_int32 netmask = {0};
60
* BPF filter and program.
64
struct bpf_program bpf_program = {0};
70
* Parse command line options to the application via standard system calls.
72
while ((c = getopt (argc, argv, optstring)) != -1)
91
* Safe to play with the 'active' interface (if any)
92
* in case no specific one was chosen by the user.
94
if (! interface && ! (interface = intop_interface (active)))
96
printf ("No interface is currently enabled for packet sniffing.\n");
101
* Lookup for the given name in the table of enabled interfaces.
103
current = intop_lookup_interface (interface);
106
printf ("%s: unknown interface %s.\n", commandname, interface);
111
* Avoid to modify parameters for interfaces not yet enabled via LBNL.
113
if (get_intop_flags (current) == INTERFACE_DOWN)
115
printf ("%s: unknown interface %s.\n", commandname, interface);
121
* Build a filter from all remaining command line arguments.
125
while (optind < argc)
127
roomsize += (strlen (argv [optind]) + 1 + 1);
130
strcat (filter, " ");
131
filter = realloc (filter, roomsize);
132
strcat (filter, argv [optind ++]);
136
filter = malloc (roomsize);
137
strcpy (filter, argv [optind ++]);
143
* Compile the mandatory 'filter' into a BPF program.
144
* By default optimization on the resulting code is required.
148
ph = intop_pcap (current);
149
netmask = intop_netmask (current);
151
if (pcap_compile (ph, & bpf_program, filter, 1, netmask) == -1)
153
printf ("%s: cannot compile the filter [%s] (reason: %s)\n",
154
commandname, filter, pcap_geterr (ph));
159
* And apply the filter to the handler.
161
if (pcap_setfilter (ph, & bpf_program) == -1)
163
printf ("%s: cannot set the filter [%s] (reason: %s)\n",
164
commandname, filter, pcap_geterr (ph));
168
set_intop_filter (current, filter);
173
filter = get_intop_filter (current);
175
printf ("current value for filtering packets is set to: [%s].\n", filter);
177
printf ("no filter has been currently set for interface %s.\n", interface);