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"
78 * @return TRUE if the capture starts successfully, FALSE otherwise.
81 capture_start(capture_options *capture_opts)
86 /* close the currently loaded capture file */
87 cf_close(capture_opts->cf);
89 g_assert(capture_opts->state == CAPTURE_STOPPED);
90 capture_opts->state = CAPTURE_PREPARING;
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 /* stop the capture child gracefully */
118 sync_pipe_stop(capture_opts);
123 capture_restart(capture_options *capture_opts)
125 capture_opts->restart = TRUE;
126 capture_stop(capture_opts);
131 capture_kill_child(capture_options *capture_opts)
133 /* kill the capture child */
134 sync_pipe_kill(capture_opts);
139 /* We've succeeded a (non real-time) capture, try to read it into a new capture file */
141 capture_input_read_all(capture_options *capture_opts, gboolean is_tempfile, gboolean drops_known,
147 /* Capture succeeded; attempt to open the capture file. */
148 if (cf_open(capture_opts->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
149 /* We're not doing a capture any more, so we don't have a save
154 /* Set the read filter to NULL. */
155 /* XXX - this is odd here, try to put it somewhere, where it fits better */
156 cf_set_rfcode(capture_opts->cf, NULL);
158 /* Get the packet-drop statistics.
160 XXX - there are currently no packet-drop statistics stored
161 in libpcap captures, and that's what we're reading.
163 At some point, we will add support in Wiretap to return
164 packet-drop statistics for capture file formats that store it,
165 and will make "cf_read()" get those statistics from Wiretap.
166 We clear the statistics (marking them as "not known") in
167 "cf_open()", and "cf_read()" will only fetch them and mark
168 them as known if Wiretap supplies them, so if we get the
169 statistics now, after calling "cf_open()" but before calling
170 "cf_read()", the values we store will be used by "cf_read()".
172 If a future libpcap capture file format stores the statistics,
173 we'll put them into the capture file that we write, and will
174 thus not have to set them here - "cf_read()" will get them from
175 the file and use them. */
177 cf_set_drops_known(capture_opts->cf, TRUE);
179 /* XXX - on some systems, libpcap doesn't bother filling in
180 "ps_ifdrop" - it doesn't even set it to zero - so we don't
181 bother looking at it.
183 Ideally, libpcap would have an interface that gave us
184 several statistics - perhaps including various interface
185 error statistics - and would tell us which of them it
186 supplies, allowing us to display only the ones it does. */
187 cf_set_drops(capture_opts->cf, drops);
190 /* read in the packet data */
191 switch (cf_read(capture_opts->cf)) {
195 /* Just because we got an error, that doesn't mean we were unable
196 to read any of the file; we handle what we could get from the
200 case CF_READ_ABORTED:
201 /* User wants to quit program. Exit by leaving the main loop,
202 so that any quit functions we registered get called. */
203 main_window_nested_quit();
207 /* if we didn't captured even a single packet, close the file again */
208 if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
209 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
210 "%sNo packets captured!%s\n\n"
211 "As no data was captured, closing the %scapture file!",
212 simple_dialog_primary_start(), simple_dialog_primary_end(),
213 (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
214 cf_close(capture_opts->cf);
220 /* capture child tells us, we have a new (or the first) capture file */
222 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
224 gboolean is_tempfile;
228 g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
229 /*g_warning("New capture file: %s", new_file);*/
231 /* free the old filename */
232 if(capture_opts->save_file != NULL) {
233 /* we start a new capture file, close the old one (if we had one before) */
234 if( ((capture_file *) capture_opts->cf)->state != FILE_CLOSED) {
235 cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
236 cf_finish_tail(capture_opts->cf, &err);
237 cf_close(capture_opts->cf);
239 g_free(capture_opts->save_file);
241 cf_set_tempfile(capture_opts->cf, FALSE);
243 /* we didn't had a save_file before, must be a tempfile */
245 cf_set_tempfile(capture_opts->cf, TRUE);
248 /* save the new filename */
249 capture_opts->save_file = g_strdup(new_file);
251 /* if we are in real-time mode, open the new file now */
252 if(capture_opts->real_time_mode) {
253 /* Attempt to open the capture file and set up to read from it. */
254 switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
258 /* Don't unlink (delete) the save file - leave it around,
259 for debugging purposes. */
260 g_free(capture_opts->save_file);
261 capture_opts->save_file = NULL;
266 cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
268 cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
271 capture_opts->state = CAPTURE_RUNNING;
277 /* capture child tells us, we have new packets to read */
279 capture_input_new_packets(capture_options *capture_opts, int to_read)
284 g_assert(capture_opts->save_file);
286 if(capture_opts->real_time_mode) {
287 /* Read from the capture file the number of records the child told us
289 XXX - do something if this fails? */
290 switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
294 /* Just because we got an error, that doesn't mean we were unable
295 to read any of the file; we handle what we could get from the
298 XXX - abort on a read error? */
299 cf_callback_invoke(cf_cb_live_capture_update_continue, capture_opts->cf);
300 main_window_update();
303 case CF_READ_ABORTED:
304 /* Kill the child capture process; the user wants to exit, and we
305 shouldn't just leave it running. */
306 capture_kill_child(capture_opts);
313 /* capture child closed it's side ot the pipe, do the required cleanup */
315 capture_input_closed(capture_options *capture_opts)
320 g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
322 /* if we didn't started the capture (happens if an error occured), do a fake start */
323 if(capture_opts->state == CAPTURE_PREPARING) {
324 if(capture_opts->real_time_mode) {
325 cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
327 cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
331 if(capture_opts->real_time_mode) {
332 /* first of all, we are not doing a capture any more */
333 cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
335 /* Read what remains of the capture file, and finish the capture.
336 XXX - do something if this fails? */
337 switch (cf_finish_tail(capture_opts->cf, &err)) {
340 if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
341 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
342 "%sNo packets captured!%s\n\n"
343 "As no data was captured, closing the %scapture file!",
344 simple_dialog_primary_start(), simple_dialog_primary_end(),
345 cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
346 cf_close(capture_opts->cf);
350 /* Just because we got an error, that doesn't mean we were unable
351 to read any of the file; we handle what we could get from the
355 case CF_READ_ABORTED:
356 /* Exit by leaving the main loop, so that any quit functions
357 we registered get called. */
362 /* first of all, we are not doing a capture any more */
363 cf_callback_invoke(cf_cb_live_capture_fixed_finished, capture_opts->cf);
365 /* this is a normal mode capture and if no error happened, read in the capture file data */
366 if(capture_opts->save_file != NULL) {
367 capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf),
368 cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
372 capture_opts->state = CAPTURE_STOPPED;
374 /* if we couldn't open a capture file, there's nothing more for us to do */
375 if(capture_opts->save_file == NULL) {
376 cf_close(capture_opts->cf);
380 /* does the user wants to restart the current capture? */
381 if(capture_opts->restart) {
382 capture_opts->restart = FALSE;
384 unlink(capture_opts->save_file);
386 /* if it was a tempfile, throw away the old filename (so it will become a tempfile again) */
387 if(cf_is_tempfile(capture_opts->cf)) {
388 g_free(capture_opts->save_file);
389 capture_opts->save_file = NULL;
392 /* ... and start the capture again */
393 capture_start(capture_opts);
395 /* We're not doing a capture any more, so we don't have a save file. */
396 g_free(capture_opts->save_file);
397 capture_opts->save_file = NULL;
402 #endif /* HAVE_LIBPCAP */