2
* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
3
* open.c - open a network interface to look at packets on the network.
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
* Obtain a LBNL packet capture descriptor
35
* to look at packets on the network.
37
int intop_open (int argc, char * argv [])
40
* Notice the command name.
42
char * commandname = argv [0];
46
printf ("Usage: %s [-h] [-q] [-i interface] [-s snaplen] [ expression ]\n", xxx)
47
char * optstring = "hqi:s:";
50
* Reserve here space for the local variables.
53
char * interface = NULL;
55
int snaplen = DEFAULT_SNAPLEN;
56
char ebuf [PCAP_ERRBUF_SIZE] = {0};
72
* Parse command line options to the application via standard system calls.
74
while ((c = getopt (argc, argv, optstring)) != -1)
95
snaplen = atoi (optarg);
102
* Check for valid names here.
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");
111
* Lookup for the given name in the table of enabled interfaces
112
* (to avoid multiple open on the same network interface).
114
current = intop_lookup_interface (interface);
118
printf ("%s: interface %s already enabled for packet capturing.\n", commandname, interface);
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.
127
ph = pcap_open_live (interface, snaplen, 1, 1000, ebuf);
130
printf ("%s: cannot initialize the LBNL pcap package (reason: %s)\n",
137
* Build a filter from all remaining command line arguments.
141
while (optind < argc)
143
roomsize += (strlen (argv [optind]) + 1 + 1);
146
strcat (filter, " ");
147
filter = realloc (filter, roomsize);
148
strcat (filter, argv [optind ++]);
152
filter = malloc (roomsize);
153
strcpy (filter, argv [optind ++]);
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.
163
active = intopmalloc (interface, snaplen, ph, filter);
169
* And update the status of the interface to READY.
171
set_intop_flags (active, INTERFACE_READY);
174
* Update user prompt to include the active interface.
176
userprompt (interface);
179
* Additional initialization only to make happy the ntop engine.
181
device [numDevices] . pcapPtr = ph;
182
device [numDevices] . name = strdup (interface);