~ubuntu-branches/ubuntu/natty/ntop/natty

« back to all changes in this revision

Viewing changes to ntop/intop/open.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-01-30 21:59:13 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050130215913-xc3ke963bw49b3k4
Tags: 2:3.0-5
* Updated README.Debian file so users will understand what to do at
  install, closes: #291794, #287802.
* Updated ntop init script to give better output.
* Also changed log directory from /var/lib/ntop to /var/log/ntop,
  closes: #252352.
* Quoted the interface list to allow whitespace, closes: #267248.
* Added a couple of logcheck ignores, closes: #269321, #269319.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
3
 
 * open.c - open a network interface to look at packets on the network.
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
 
 * Obtain a LBNL packet capture descriptor
35
 
 * to look at packets on the network.
36
 
 */
37
 
int intop_open (int argc, char * argv [])
38
 
{
39
 
  /*
40
 
   * Notice the command name.
41
 
   */
42
 
  char * commandname = argv [0];
43
 
 
44
 
  int c;
45
 
#define USAGE(xxx) \
46
 
  printf ("Usage: %s [-h] [-q] [-i interface] [-s snaplen] [ expression ]\n", xxx)
47
 
  char * optstring = "hqi:s:";
48
 
 
49
 
  /*
50
 
   * Reserve here space for the local variables.
51
 
   */
52
 
  int quiet = 0;
53
 
  char * interface = NULL;
54
 
  int current;
55
 
  int snaplen = DEFAULT_SNAPLEN;
56
 
  char ebuf [PCAP_ERRBUF_SIZE] = {0};
57
 
  /*
58
 
   * LBNL pcap handler.
59
 
   */
60
 
  pcap_t * ph = NULL;
61
 
 
62
 
  /*
63
 
   * BPF filter.
64
 
   */
65
 
  int roomsize;
66
 
  char * filter;
67
 
 
68
 
  optind = 0;
69
 
  optarg = NULL;
70
 
 
71
 
  /*
72
 
   * Parse command line options to the application via standard system calls.
73
 
   */
74
 
  while ((c = getopt (argc, argv, optstring)) != -1)
75
 
    {
76
 
      switch (c)
77
 
        {
78
 
        case 'h':
79
 
          USAGE (commandname);
80
 
          return (0);
81
 
 
82
 
        default:
83
 
          USAGE (commandname);
84
 
          return (-1);
85
 
 
86
 
        case 'q':
87
 
          quiet = ! quiet;
88
 
          break;
89
 
 
90
 
        case 'i':
91
 
          interface = optarg;
92
 
          break;
93
 
 
94
 
        case 's':
95
 
          snaplen = atoi (optarg);
96
 
          break;
97
 
        }
98
 
    }
99
 
 
100
 
 
101
 
  /*
102
 
   * Check for valid names here.
103
 
   */
104
 
  if (!interface) {
105
 
    printf ("No interface selected, please specify one with -i\n");
106
 
    printf ("The list of all suitable interfaces can be obtained via the 'lsdev' command.\n");
107
 
    return (-1);
108
 
  }
109
 
 
110
 
  /*
111
 
   * Lookup for the given name in the table of enabled interfaces
112
 
   * (to avoid multiple open on the same network interface).
113
 
   */
114
 
  current = intop_lookup_interface (interface);
115
 
 
116
 
  if (current != -1)
117
 
    {
118
 
      printf ("%s: interface %s already enabled for packet capturing.\n", commandname, interface);
119
 
      return (-1);
120
 
    }
121
 
 
122
 
  /*
123
 
   * Time to initialize LBNL pcap library for the specified
124
 
   * interface in promiscuous mode.
125
 
   * The read timeout is set to 1 sec by default.
126
 
   */
127
 
  ph = pcap_open_live (interface, snaplen, 1, 1000, ebuf);
128
 
  if (! ph)
129
 
    {
130
 
      printf ("%s: cannot initialize the LBNL pcap package (reason: %s)\n",
131
 
              commandname, ebuf);
132
 
      return (-1);
133
 
    }
134
 
 
135
 
 
136
 
  /*
137
 
   * Build a filter from all remaining command line arguments.
138
 
   */
139
 
  roomsize = 0;
140
 
  filter = NULL;
141
 
  while (optind < argc)
142
 
    {
143
 
      roomsize += (strlen (argv [optind]) + 1 + 1);
144
 
      if (filter)
145
 
        {
146
 
          strcat (filter, " ");
147
 
          filter = realloc (filter, roomsize);
148
 
          strcat (filter, argv [optind ++]);
149
 
        }
150
 
      else
151
 
        {
152
 
          filter = malloc (roomsize);
153
 
          strcpy (filter, argv [optind ++]);
154
 
        }
155
 
    }
156
 
 
157
 
  /*
158
 
   * Get a new descriptor and save current parameters
159
 
   * to the table of interfaces managed by this program.
160
 
   * Also play with the interface stack.
161
 
   */
162
 
  previous = active;
163
 
  active = intopmalloc (interface, snaplen, ph, filter);
164
 
 
165
 
  if (filter)
166
 
    free (filter);
167
 
 
168
 
  /*
169
 
   * And update the status of the interface to READY.
170
 
   */
171
 
  set_intop_flags (active, INTERFACE_READY);
172
 
 
173
 
  /*
174
 
   * Update user prompt to include the active interface.
175
 
   */
176
 
  userprompt (interface);
177
 
 
178
 
  /*
179
 
   * Additional initialization only to make happy the ntop engine.
180
 
   */
181
 
  device [numDevices] . pcapPtr = ph;
182
 
  device [numDevices] . name = strdup (interface);
183
 
 
184
 
  resetStats();
185
 
  initCounters(0);
186
 
 
187
 
  return (0);
188
 
}