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"
74 * @return TRUE if the capture starts successfully, FALSE otherwise.
77 capture_start(capture_options *capture_opts)
82 /* close the currently loaded capture file */
83 cf_close(capture_opts->cf);
85 g_assert(capture_opts->state == CAPTURE_STOPPED);
86 capture_opts->state = CAPTURE_PREPARING;
88 /* try to start the capture child process */
89 ret = sync_pipe_start(capture_opts, capture_opts->save_file == NULL);
91 if(capture_opts->save_file != NULL) {
92 g_free(capture_opts->save_file);
93 capture_opts->save_file = NULL;
96 capture_opts->state = CAPTURE_STOPPED;
104 capture_stop(capture_options *capture_opts)
106 /* stop the capture child gracefully */
107 sync_pipe_stop(capture_opts);
112 capture_restart(capture_options *capture_opts)
114 capture_opts->restart = TRUE;
115 capture_stop(capture_opts);
120 capture_kill_child(capture_options *capture_opts)
122 /* kill the capture child */
123 sync_pipe_kill(capture_opts);
128 /* We've succeeded a (non real-time) capture, try to read it into a new capture file */
130 capture_input_read_all(capture_options *capture_opts, gboolean is_tempfile, gboolean drops_known,
136 /* Capture succeeded; attempt to open the capture file. */
137 if (cf_open(capture_opts->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
138 /* We're not doing a capture any more, so we don't have a save
143 /* Set the read filter to NULL. */
144 /* XXX - this is odd here, try to put it somewhere, where it fits better */
145 cf_set_rfcode(capture_opts->cf, NULL);
147 /* Get the packet-drop statistics.
149 XXX - there are currently no packet-drop statistics stored
150 in libpcap captures, and that's what we're reading.
152 At some point, we will add support in Wiretap to return
153 packet-drop statistics for capture file formats that store it,
154 and will make "cf_read()" get those statistics from Wiretap.
155 We clear the statistics (marking them as "not known") in
156 "cf_open()", and "cf_read()" will only fetch them and mark
157 them as known if Wiretap supplies them, so if we get the
158 statistics now, after calling "cf_open()" but before calling
159 "cf_read()", the values we store will be used by "cf_read()".
161 If a future libpcap capture file format stores the statistics,
162 we'll put them into the capture file that we write, and will
163 thus not have to set them here - "cf_read()" will get them from
164 the file and use them. */
166 cf_set_drops_known(capture_opts->cf, TRUE);
168 /* XXX - on some systems, libpcap doesn't bother filling in
169 "ps_ifdrop" - it doesn't even set it to zero - so we don't
170 bother looking at it.
172 Ideally, libpcap would have an interface that gave us
173 several statistics - perhaps including various interface
174 error statistics - and would tell us which of them it
175 supplies, allowing us to display only the ones it does. */
176 cf_set_drops(capture_opts->cf, drops);
179 /* read in the packet data */
180 switch (cf_read(capture_opts->cf)) {
184 /* Just because we got an error, that doesn't mean we were unable
185 to read any of the file; we handle what we could get from the
189 case CF_READ_ABORTED:
190 /* User wants to quit program. Exit by leaving the main loop,
191 so that any quit functions we registered get called. */
192 main_window_nested_quit();
196 /* if we didn't captured even a single packet, close the file again */
197 if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
198 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
199 "%sNo packets captured!%s\n\n"
200 "As no data was captured, closing the %scapture file!",
201 simple_dialog_primary_start(), simple_dialog_primary_end(),
202 (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
203 cf_close(capture_opts->cf);
209 /* capture child tells us, we have a new (or the first) capture file */
211 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
213 gboolean is_tempfile;
217 g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
218 /*g_warning("New capture file: %s", new_file);*/
220 /* free the old filename */
221 if(capture_opts->save_file != NULL) {
222 /* we start a new capture file, close the old one (if we had one before) */
223 if( ((capture_file *) capture_opts->cf)->state != FILE_CLOSED) {
224 cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
225 cf_finish_tail(capture_opts->cf, &err);
226 cf_close(capture_opts->cf);
228 g_free(capture_opts->save_file);
230 cf_set_tempfile(capture_opts->cf, FALSE);
232 /* we didn't had a save_file before, must be a tempfile */
234 cf_set_tempfile(capture_opts->cf, TRUE);
237 /* save the new filename */
238 capture_opts->save_file = g_strdup(new_file);
240 /* if we are in real-time mode, open the new file now */
241 if(capture_opts->real_time_mode) {
242 /* Attempt to open the capture file and set up to read from it. */
243 switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
247 /* Don't unlink (delete) the save file - leave it around,
248 for debugging purposes. */
249 g_free(capture_opts->save_file);
250 capture_opts->save_file = NULL;
255 cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
257 cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
260 capture_opts->state = CAPTURE_RUNNING;
266 /* capture child tells us, we have new packets to read */
268 capture_input_new_packets(capture_options *capture_opts, int to_read)
273 g_assert(capture_opts->save_file);
275 if(capture_opts->real_time_mode) {
276 /* Read from the capture file the number of records the child told us
278 XXX - do something if this fails? */
279 switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
283 /* Just because we got an error, that doesn't mean we were unable
284 to read any of the file; we handle what we could get from the
287 XXX - abort on a read error? */
288 cf_callback_invoke(cf_cb_live_capture_update_continue, capture_opts->cf);
289 main_window_update();
292 case CF_READ_ABORTED:
293 /* Kill the child capture process; the user wants to exit, and we
294 shouldn't just leave it running. */
295 capture_kill_child(capture_opts);
302 /* capture child closed it's side ot the pipe, do the required cleanup */
304 capture_input_closed(capture_options *capture_opts)
309 g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
311 /* if we didn't started the capture (happens if an error occured), do a fake start */
312 if(capture_opts->state == CAPTURE_PREPARING) {
313 if(capture_opts->real_time_mode) {
314 cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
316 cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
320 if(capture_opts->real_time_mode) {
321 /* first of all, we are not doing a capture any more */
322 cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
324 /* Read what remains of the capture file, and finish the capture.
325 XXX - do something if this fails? */
326 switch (cf_finish_tail(capture_opts->cf, &err)) {
329 if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
330 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
331 "%sNo packets captured!%s\n\n"
332 "As no data was captured, closing the %scapture file!",
333 simple_dialog_primary_start(), simple_dialog_primary_end(),
334 cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
335 cf_close(capture_opts->cf);
339 /* Just because we got an error, that doesn't mean we were unable
340 to read any of the file; we handle what we could get from the
344 case CF_READ_ABORTED:
345 /* Exit by leaving the main loop, so that any quit functions
346 we registered get called. */
351 /* first of all, we are not doing a capture any more */
352 cf_callback_invoke(cf_cb_live_capture_fixed_finished, capture_opts->cf);
354 /* this is a normal mode capture and if no error happened, read in the capture file data */
355 if(capture_opts->save_file != NULL) {
356 capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf),
357 cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
361 capture_opts->state = CAPTURE_STOPPED;
363 /* if we couldn't open a capture file, there's nothing more for us to do */
364 if(capture_opts->save_file == NULL) {
365 cf_close(capture_opts->cf);
369 /* does the user wants to restart the current capture? */
370 if(capture_opts->restart) {
371 capture_opts->restart = FALSE;
373 unlink(capture_opts->save_file);
375 /* if it was a tempfile, throw away the old filename (so it will become a tempfile again) */
376 if(cf_is_tempfile(capture_opts->cf)) {
377 g_free(capture_opts->save_file);
378 capture_opts->save_file = NULL;
381 /* ... and start the capture again */
382 capture_start(capture_opts);
384 /* We're not doing a capture any more, so we don't have a save file. */
385 g_free(capture_opts->save_file);
386 capture_opts->save_file = NULL;
391 #endif /* HAVE_LIBPCAP */