~ubuntu-branches/ubuntu/wily/ntop/wily-proposed

« back to all changes in this revision

Viewing changes to ntop/intop/filter.c

  • Committer: Bazaar Package Importer
  • Author(s): Dennis Schoen
  • Date: 2002-04-12 11:38:47 UTC
  • Revision ID: james.westby@ubuntu.com-20020412113847-4k4yydw0pzybc6g8
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
3
 * filter.c - Read/Write the BPF filter associated to a network interface.
 
4
 *
 
5
 * Luca Deri     <deri@ntop.org>
 
6
 * Rocco Carbone <rocco@ntop.org>
 
7
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 *
 
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.
 
22
 */
 
23
 
 
24
 
 
25
/*
 
26
 * ntop header file(s)
 
27
 */
 
28
#include "ntop.h"
 
29
 
 
30
#include "intop.h"
 
31
 
 
32
 
 
33
/*
 
34
 * Get/Set the BPF filter associated to a network interface.
 
35
 */
 
36
int intop_set_filter (int argc, char * argv [])
 
37
{
 
38
  /*
 
39
   * Notice the command name.
 
40
   */
 
41
  char * commandname = argv [0];
 
42
 
 
43
  int c;
 
44
#define USAGE(xxx) \
 
45
  printf ("Usage: %s [-h] [-i interface] [expression]\n", xxx)
 
46
  char * optstring = "hi:";
 
47
 
 
48
  /*
 
49
   * Reserve here space for the local variables.
 
50
   */
 
51
  char * interface = NULL;
 
52
  int current;
 
53
  /*
 
54
   * LBNL pcap handler.
 
55
   */
 
56
  pcap_t * ph = NULL;
 
57
  bpf_u_int32 netmask = {0};
 
58
 
 
59
  /*
 
60
   * BPF filter and program.
 
61
   */
 
62
  int roomsize;
 
63
  char * filter;
 
64
  struct bpf_program bpf_program = {0};
 
65
 
 
66
  optind = 0;
 
67
  optarg = NULL;
 
68
 
 
69
  /*
 
70
   * Parse command line options to the application via standard system calls.
 
71
   */
 
72
  while ((c = getopt (argc, argv, optstring)) != -1)
 
73
    {
 
74
      switch (c)
 
75
        {
 
76
        case 'h':
 
77
          USAGE (commandname);
 
78
          return (0);
 
79
 
 
80
        default:
 
81
          USAGE (commandname);
 
82
          return (-1);
 
83
 
 
84
        case 'i':
 
85
          interface = optarg;
 
86
          break;
 
87
        }
 
88
    }
 
89
 
 
90
  /*
 
91
   * Safe to play with the 'active' interface (if any)
 
92
   * in case no specific one was chosen by the user.
 
93
   */
 
94
  if (! interface && ! (interface = intop_interface (active)))
 
95
    {
 
96
      printf ("No interface is currently enabled for packet sniffing.\n");
 
97
      return (-1);
 
98
    }
 
99
 
 
100
  /*
 
101
   * Lookup for the given name in the table of enabled interfaces.
 
102
   */
 
103
  current = intop_lookup_interface (interface);
 
104
  if (current == -1)
 
105
    {
 
106
      printf ("%s: unknown interface %s.\n", commandname, interface);
 
107
      return (-1);
 
108
    }
 
109
 
 
110
  /*
 
111
   * Avoid to modify parameters for interfaces not yet enabled via LBNL.
 
112
   */
 
113
  if (get_intop_flags (current) == INTERFACE_DOWN)
 
114
    {
 
115
      printf ("%s: unknown interface %s.\n", commandname, interface);
 
116
      return (-1);
 
117
    }
 
118
 
 
119
 
 
120
  /*
 
121
   * Build a filter from all remaining command line arguments.
 
122
   */
 
123
  roomsize = 0;
 
124
  filter = NULL;
 
125
  while (optind < argc)
 
126
    {
 
127
      roomsize += (strlen (argv [optind]) + 1 + 1);
 
128
      if (filter)
 
129
        {
 
130
          strcat (filter, " ");
 
131
          filter = realloc (filter, roomsize);
 
132
          strcat (filter, argv [optind ++]);
 
133
        }
 
134
      else
 
135
        {
 
136
          filter = malloc (roomsize);
 
137
          strcpy (filter, argv [optind ++]);
 
138
        }
 
139
    }
 
140
 
 
141
 
 
142
  /*
 
143
   * Compile the mandatory 'filter' into a BPF program.
 
144
   * By default optimization on the resulting code is required.
 
145
   */
 
146
  if (filter)
 
147
    {
 
148
      ph = intop_pcap (current);
 
149
      netmask = intop_netmask (current);
 
150
 
 
151
      if (pcap_compile (ph, & bpf_program, filter, 1, netmask) == -1)
 
152
        {
 
153
          printf ("%s: cannot compile the filter [%s]  (reason: %s)\n",
 
154
                  commandname, filter, pcap_geterr (ph));
 
155
          return (-1);
 
156
        }
 
157
 
 
158
      /*
 
159
       * And apply the filter to the handler.
 
160
       */
 
161
      if (pcap_setfilter (ph, & bpf_program) == -1)
 
162
        {
 
163
          printf ("%s: cannot set the filter [%s] (reason: %s)\n",
 
164
                  commandname, filter, pcap_geterr (ph));
 
165
          return (-1);
 
166
        }
 
167
 
 
168
      set_intop_filter (current, filter);
 
169
      free (filter);
 
170
    }
 
171
  else
 
172
    {
 
173
      filter = get_intop_filter (current);
 
174
      if (filter)
 
175
        printf ("current value for filtering packets is set to: [%s].\n", filter);
 
176
      else
 
177
        printf ("no filter has been currently set for interface %s.\n", interface);
 
178
    }
 
179
 
 
180
  return (0);
 
181
}