2 * Routines for packet capture
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
50 #include <epan/packet.h>
51 #include <epan/dfilter/dfilter.h>
54 #include "capture_sync.h"
55 #include "capture_ui_utils.h"
57 #include "pcap-util.h"
58 #include "alert_box.h"
59 #include "simple_dialog.h"
60 #include <epan/prefs.h>
61 #include "conditions.h"
62 #include "ringbuffer.h"
65 #include "capture-wpcap.h"
68 #include "file_util.h"
76 * @return TRUE if the capture starts successfully, FALSE otherwise.
79 capture_start(capture_options *capture_opts)
84 /* close the currently loaded capture file */
85 cf_close(capture_opts->cf);
87 g_assert(capture_opts->state == CAPTURE_STOPPED);
88 capture_opts->state = CAPTURE_PREPARING;
90 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Start ...");
92 /* try to start the capture child process */
93 ret = sync_pipe_start(capture_opts);
95 if(capture_opts->save_file != NULL) {
96 g_free(capture_opts->save_file);
97 capture_opts->save_file = NULL;
100 capture_opts->state = CAPTURE_STOPPED;
102 /* the capture child might not respond shortly after bringing it up */
103 /* (especially it will block, if no input coming from an input capture pipe (e.g. mkfifo) is coming in) */
105 /* to prevent problems, bring the main GUI into "capture mode" right after successfully */
106 /* spawn/exec the capture child, without waiting for any response from it */
107 cf_callback_invoke(cf_cb_live_capture_prepared, capture_opts);
115 capture_stop(capture_options *capture_opts)
117 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Stop ...");
119 cf_callback_invoke(cf_cb_live_capture_stopping, capture_opts);
121 /* stop the capture child gracefully */
122 sync_pipe_stop(capture_opts);
127 capture_restart(capture_options *capture_opts)
129 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Restart");
131 capture_opts->restart = TRUE;
132 capture_stop(capture_opts);
137 capture_kill_child(capture_options *capture_opts)
139 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "Capture Kill");
141 /* kill the capture child */
142 sync_pipe_kill(capture_opts);
147 /* We've succeeded a (non real-time) capture, try to read it into a new capture file */
149 capture_input_read_all(capture_options *capture_opts, gboolean is_tempfile, gboolean drops_known,
155 /* Capture succeeded; attempt to open the capture file. */
156 if (cf_open(capture_opts->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
157 /* We're not doing a capture any more, so we don't have a save
162 /* Set the read filter to NULL. */
163 /* XXX - this is odd here, try to put it somewhere, where it fits better */
164 cf_set_rfcode(capture_opts->cf, NULL);
166 /* Get the packet-drop statistics.
168 XXX - there are currently no packet-drop statistics stored
169 in libpcap captures, and that's what we're reading.
171 At some point, we will add support in Wiretap to return
172 packet-drop statistics for capture file formats that store it,
173 and will make "cf_read()" get those statistics from Wiretap.
174 We clear the statistics (marking them as "not known") in
175 "cf_open()", and "cf_read()" will only fetch them and mark
176 them as known if Wiretap supplies them, so if we get the
177 statistics now, after calling "cf_open()" but before calling
178 "cf_read()", the values we store will be used by "cf_read()".
180 If a future libpcap capture file format stores the statistics,
181 we'll put them into the capture file that we write, and will
182 thus not have to set them here - "cf_read()" will get them from
183 the file and use them. */
185 cf_set_drops_known(capture_opts->cf, TRUE);
187 /* XXX - on some systems, libpcap doesn't bother filling in
188 "ps_ifdrop" - it doesn't even set it to zero - so we don't
189 bother looking at it.
191 Ideally, libpcap would have an interface that gave us
192 several statistics - perhaps including various interface
193 error statistics - and would tell us which of them it
194 supplies, allowing us to display only the ones it does. */
195 cf_set_drops(capture_opts->cf, drops);
198 /* read in the packet data */
199 switch (cf_read(capture_opts->cf)) {
203 /* Just because we got an error, that doesn't mean we were unable
204 to read any of the file; we handle what we could get from the
208 case CF_READ_ABORTED:
209 /* User wants to quit program. Exit by leaving the main loop,
210 so that any quit functions we registered get called. */
211 main_window_nested_quit();
215 /* if we didn't captured even a single packet, close the file again */
216 if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
217 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
218 "%sNo packets captured!%s\n"
220 "As no data was captured, closing the %scapture file!\n"
223 "Help about capturing can be found at:\n"
225 " http://wiki.ethereal.com/CaptureSetup"
228 "Wireless (Wi-Fi/WLAN):\n"
229 "Try to switch off promiscuous mode in the Capture Options!"
232 simple_dialog_primary_start(), simple_dialog_primary_end(),
233 (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
234 cf_close(capture_opts->cf);
240 /* capture child tells us, we have a new (or the first) capture file */
242 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
244 gboolean is_tempfile;
248 if(capture_opts->state == CAPTURE_PREPARING) {
249 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started!");
251 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "File: \"%s\"", new_file);
253 g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
255 /* free the old filename */
256 if(capture_opts->save_file != NULL) {
257 /* we start a new capture file, close the old one (if we had one before) */
258 if( ((capture_file *) capture_opts->cf)->state != FILE_CLOSED) {
259 cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
260 cf_finish_tail(capture_opts->cf, &err);
261 cf_close(capture_opts->cf);
263 g_free(capture_opts->save_file);
265 cf_set_tempfile(capture_opts->cf, FALSE);
267 /* we didn't had a save_file before, must be a tempfile */
269 cf_set_tempfile(capture_opts->cf, TRUE);
272 /* save the new filename */
273 capture_opts->save_file = g_strdup(new_file);
275 /* if we are in real-time mode, open the new file now */
276 if(capture_opts->real_time_mode) {
277 /* Attempt to open the capture file and set up to read from it. */
278 switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
282 /* Don't unlink (delete) the save file - leave it around,
283 for debugging purposes. */
284 g_free(capture_opts->save_file);
285 capture_opts->save_file = NULL;
290 cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
292 cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
295 capture_opts->state = CAPTURE_RUNNING;
301 /* capture child tells us, we have new packets to read */
303 capture_input_new_packets(capture_options *capture_opts, int to_read)
308 g_assert(capture_opts->save_file);
310 if(capture_opts->real_time_mode) {
311 /* Read from the capture file the number of records the child told us
313 XXX - do something if this fails? */
314 switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
318 /* Just because we got an error, that doesn't mean we were unable
319 to read any of the file; we handle what we could get from the
322 XXX - abort on a read error? */
323 cf_callback_invoke(cf_cb_live_capture_update_continue, capture_opts->cf);
324 /* update the main window, so we get events (e.g. from the stop toolbar button) */
325 main_window_update();
328 case CF_READ_ABORTED:
329 /* Kill the child capture process; the user wants to exit, and we
330 shouldn't just leave it running. */
331 capture_kill_child(capture_opts);
338 /* capture child closed it's side ot the pipe, do the required cleanup */
340 capture_input_closed(capture_options *capture_opts)
345 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture stopped!");
346 g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
348 /* if we didn't started the capture (happens if an error occured), do a fake start */
349 if(capture_opts->state == CAPTURE_PREPARING) {
350 if(capture_opts->real_time_mode) {
351 cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
353 cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
357 if(capture_opts->real_time_mode) {
358 cf_read_status_t status;
360 /* Read what remains of the capture file. */
361 status = cf_finish_tail(capture_opts->cf, &err);
363 /* Tell the GUI, we are not doing a capture any more.
364 Must be done after the cf_finish_tail(), so file lengths are displayed
366 cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
368 /* Finish the capture. */
372 if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
373 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
374 "%sNo packets captured!%s\n"
376 "As no data was captured, closing the %scapture file!\n"
379 "Help about capturing can be found at:\n"
381 " http://wiki.ethereal.com/CaptureSetup"
384 "Wireless (Wi-Fi/WLAN):\n"
385 "Try to switch off promiscuous mode in the Capture Options!"
388 simple_dialog_primary_start(), simple_dialog_primary_end(),
389 cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
390 cf_close(capture_opts->cf);
394 /* Just because we got an error, that doesn't mean we were unable
395 to read any of the file; we handle what we could get from the
399 case CF_READ_ABORTED:
400 /* Exit by leaving the main loop, so that any quit functions
401 we registered get called. */
406 /* first of all, we are not doing a capture any more */
407 cf_callback_invoke(cf_cb_live_capture_fixed_finished, capture_opts->cf);
409 /* this is a normal mode capture and if no error happened, read in the capture file data */
410 if(capture_opts->save_file != NULL) {
411 capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf),
412 cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
416 capture_opts->state = CAPTURE_STOPPED;
418 /* if we couldn't open a capture file, there's nothing more for us to do */
419 if(capture_opts->save_file == NULL) {
420 cf_close(capture_opts->cf);
424 /* does the user wants to restart the current capture? */
425 if(capture_opts->restart) {
426 capture_opts->restart = FALSE;
428 eth_unlink(capture_opts->save_file);
430 /* if it was a tempfile, throw away the old filename (so it will become a tempfile again) */
431 if(cf_is_tempfile(capture_opts->cf)) {
432 g_free(capture_opts->save_file);
433 capture_opts->save_file = NULL;
436 /* ... and start the capture again */
437 capture_start(capture_opts);
439 /* We're not doing a capture any more, so we don't have a save file. */
440 g_free(capture_opts->save_file);
441 capture_opts->save_file = NULL;
446 #endif /* HAVE_LIBPCAP */