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.
54 #include <epan/packet.h>
55 #include <epan/dfilter/dfilter.h>
58 #include "capture_sync.h"
59 #include "capture_ui_utils.h"
61 #include "pcap-util.h"
62 #include "alert_box.h"
63 #include "simple_dialog.h"
64 #include <epan/prefs.h>
65 #include "conditions.h"
66 #include "ringbuffer.h"
69 #include "capture-wpcap.h"
79 * @return TRUE if the capture starts successfully, FALSE otherwise.
82 capture_start(capture_options *capture_opts)
87 /* close the currently loaded capture file */
88 cf_close(capture_opts->cf);
90 g_assert(capture_opts->state == CAPTURE_STOPPED);
91 capture_opts->state = CAPTURE_PREPARING;
93 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Start ...");
95 /* try to start the capture child process */
96 ret = sync_pipe_start(capture_opts);
98 if(capture_opts->save_file != NULL) {
99 g_free(capture_opts->save_file);
100 capture_opts->save_file = NULL;
103 capture_opts->state = CAPTURE_STOPPED;
105 /* the capture child might not respond shortly after bringing it up */
106 /* (especially it will block, if no input coming from an input capture pipe (e.g. mkfifo) is coming in) */
108 /* to prevent problems, bring the main GUI into "capture mode" right after successfully */
109 /* spawn/exec the capture child, without waiting for any response from it */
110 cf_callback_invoke(cf_cb_live_capture_prepared, capture_opts);
118 capture_stop(capture_options *capture_opts)
120 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Stop ...");
122 /* stop the capture child gracefully */
123 sync_pipe_stop(capture_opts);
128 capture_restart(capture_options *capture_opts)
130 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Restart");
132 capture_opts->restart = TRUE;
133 capture_stop(capture_opts);
138 capture_kill_child(capture_options *capture_opts)
140 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "Capture Kill");
142 /* kill the capture child */
143 sync_pipe_kill(capture_opts);
148 /* We've succeeded a (non real-time) capture, try to read it into a new capture file */
150 capture_input_read_all(capture_options *capture_opts, gboolean is_tempfile, gboolean drops_known,
156 /* Capture succeeded; attempt to open the capture file. */
157 if (cf_open(capture_opts->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
158 /* We're not doing a capture any more, so we don't have a save
163 /* Set the read filter to NULL. */
164 /* XXX - this is odd here, try to put it somewhere, where it fits better */
165 cf_set_rfcode(capture_opts->cf, NULL);
167 /* Get the packet-drop statistics.
169 XXX - there are currently no packet-drop statistics stored
170 in libpcap captures, and that's what we're reading.
172 At some point, we will add support in Wiretap to return
173 packet-drop statistics for capture file formats that store it,
174 and will make "cf_read()" get those statistics from Wiretap.
175 We clear the statistics (marking them as "not known") in
176 "cf_open()", and "cf_read()" will only fetch them and mark
177 them as known if Wiretap supplies them, so if we get the
178 statistics now, after calling "cf_open()" but before calling
179 "cf_read()", the values we store will be used by "cf_read()".
181 If a future libpcap capture file format stores the statistics,
182 we'll put them into the capture file that we write, and will
183 thus not have to set them here - "cf_read()" will get them from
184 the file and use them. */
186 cf_set_drops_known(capture_opts->cf, TRUE);
188 /* XXX - on some systems, libpcap doesn't bother filling in
189 "ps_ifdrop" - it doesn't even set it to zero - so we don't
190 bother looking at it.
192 Ideally, libpcap would have an interface that gave us
193 several statistics - perhaps including various interface
194 error statistics - and would tell us which of them it
195 supplies, allowing us to display only the ones it does. */
196 cf_set_drops(capture_opts->cf, drops);
199 /* read in the packet data */
200 switch (cf_read(capture_opts->cf)) {
204 /* Just because we got an error, that doesn't mean we were unable
205 to read any of the file; we handle what we could get from the
209 case CF_READ_ABORTED:
210 /* User wants to quit program. Exit by leaving the main loop,
211 so that any quit functions we registered get called. */
212 main_window_nested_quit();
216 /* if we didn't captured even a single packet, close the file again */
217 if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
218 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
219 "%sNo packets captured!%s\n\n"
220 "As no data was captured, closing the %scapture file!",
221 simple_dialog_primary_start(), simple_dialog_primary_end(),
222 (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
223 cf_close(capture_opts->cf);
229 /* capture child tells us, we have a new (or the first) capture file */
231 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
233 gboolean is_tempfile;
237 if(capture_opts->state == CAPTURE_PREPARING) {
238 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started!");
240 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "File: \"%s\"", new_file);
242 g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
244 /* free the old filename */
245 if(capture_opts->save_file != NULL) {
246 /* we start a new capture file, close the old one (if we had one before) */
247 if( ((capture_file *) capture_opts->cf)->state != FILE_CLOSED) {
248 cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
249 cf_finish_tail(capture_opts->cf, &err);
250 cf_close(capture_opts->cf);
252 g_free(capture_opts->save_file);
254 cf_set_tempfile(capture_opts->cf, FALSE);
256 /* we didn't had a save_file before, must be a tempfile */
258 cf_set_tempfile(capture_opts->cf, TRUE);
261 /* save the new filename */
262 capture_opts->save_file = g_strdup(new_file);
264 /* if we are in real-time mode, open the new file now */
265 if(capture_opts->real_time_mode) {
266 /* Attempt to open the capture file and set up to read from it. */
267 switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
271 /* Don't unlink (delete) the save file - leave it around,
272 for debugging purposes. */
273 g_free(capture_opts->save_file);
274 capture_opts->save_file = NULL;
279 cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
281 cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
284 capture_opts->state = CAPTURE_RUNNING;
290 /* capture child tells us, we have new packets to read */
292 capture_input_new_packets(capture_options *capture_opts, int to_read)
297 g_assert(capture_opts->save_file);
299 if(capture_opts->real_time_mode) {
300 /* Read from the capture file the number of records the child told us
302 XXX - do something if this fails? */
303 switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
307 /* Just because we got an error, that doesn't mean we were unable
308 to read any of the file; we handle what we could get from the
311 XXX - abort on a read error? */
312 cf_callback_invoke(cf_cb_live_capture_update_continue, capture_opts->cf);
313 main_window_update();
316 case CF_READ_ABORTED:
317 /* Kill the child capture process; the user wants to exit, and we
318 shouldn't just leave it running. */
319 capture_kill_child(capture_opts);
326 /* capture child closed it's side ot the pipe, do the required cleanup */
328 capture_input_closed(capture_options *capture_opts)
333 g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture stopped!");
334 g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
336 /* if we didn't started the capture (happens if an error occured), do a fake start */
337 if(capture_opts->state == CAPTURE_PREPARING) {
338 if(capture_opts->real_time_mode) {
339 cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
341 cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
345 if(capture_opts->real_time_mode) {
346 /* first of all, we are not doing a capture any more */
347 cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
349 /* Read what remains of the capture file, and finish the capture.
350 XXX - do something if this fails? */
351 switch (cf_finish_tail(capture_opts->cf, &err)) {
354 if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
355 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
356 "%sNo packets captured!%s\n\n"
357 "As no data was captured, closing the %scapture file!",
358 simple_dialog_primary_start(), simple_dialog_primary_end(),
359 cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
360 cf_close(capture_opts->cf);
364 /* Just because we got an error, that doesn't mean we were unable
365 to read any of the file; we handle what we could get from the
369 case CF_READ_ABORTED:
370 /* Exit by leaving the main loop, so that any quit functions
371 we registered get called. */
376 /* first of all, we are not doing a capture any more */
377 cf_callback_invoke(cf_cb_live_capture_fixed_finished, capture_opts->cf);
379 /* this is a normal mode capture and if no error happened, read in the capture file data */
380 if(capture_opts->save_file != NULL) {
381 capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf),
382 cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
386 capture_opts->state = CAPTURE_STOPPED;
388 /* if we couldn't open a capture file, there's nothing more for us to do */
389 if(capture_opts->save_file == NULL) {
390 cf_close(capture_opts->cf);
394 /* does the user wants to restart the current capture? */
395 if(capture_opts->restart) {
396 capture_opts->restart = FALSE;
398 unlink(capture_opts->save_file);
400 /* if it was a tempfile, throw away the old filename (so it will become a tempfile again) */
401 if(cf_is_tempfile(capture_opts->cf)) {
402 g_free(capture_opts->save_file);
403 capture_opts->save_file = NULL;
406 /* ... and start the capture again */
407 capture_start(capture_opts);
409 /* We're not doing a capture any more, so we don't have a save file. */
410 g_free(capture_opts->save_file);
411 capture_opts->save_file = NULL;
416 #endif /* HAVE_LIBPCAP */