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 /* try to start the capture child process */
86 ret = sync_pipe_start(capture_opts, capture_opts->save_file == NULL);
88 if(capture_opts->save_file != NULL) {
89 g_free(capture_opts->save_file);
90 capture_opts->save_file = NULL;
99 capture_stop(capture_options *capture_opts)
101 /* stop the capture child gracefully */
102 sync_pipe_stop(capture_opts);
107 capture_clear(capture_options *capture_opts)
109 capture_opts->restart = TRUE;
110 capture_stop(capture_opts);
115 capture_kill_child(capture_options *capture_opts)
117 /* kill the capture child */
118 sync_pipe_kill(capture_opts);
123 /* We've succeeded a (non real-time) capture, try to read it into a new capture file */
125 capture_input_read_all(capture_options *capture_opts, gboolean is_tempfile, gboolean drops_known,
131 /* Capture succeeded; attempt to open the capture file. */
132 if (cf_open(capture_opts->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
133 /* We're not doing a capture any more, so we don't have a save
138 /* Set the read filter to NULL. */
139 /* XXX - this is odd here, try to put it somewhere, where it fits better */
140 cf_set_rfcode(capture_opts->cf, NULL);
142 /* Get the packet-drop statistics.
144 XXX - there are currently no packet-drop statistics stored
145 in libpcap captures, and that's what we're reading.
147 At some point, we will add support in Wiretap to return
148 packet-drop statistics for capture file formats that store it,
149 and will make "cf_read()" get those statistics from Wiretap.
150 We clear the statistics (marking them as "not known") in
151 "cf_open()", and "cf_read()" will only fetch them and mark
152 them as known if Wiretap supplies them, so if we get the
153 statistics now, after calling "cf_open()" but before calling
154 "cf_read()", the values we store will be used by "cf_read()".
156 If a future libpcap capture file format stores the statistics,
157 we'll put them into the capture file that we write, and will
158 thus not have to set them here - "cf_read()" will get them from
159 the file and use them. */
161 cf_set_drops_known(capture_opts->cf, TRUE);
163 /* XXX - on some systems, libpcap doesn't bother filling in
164 "ps_ifdrop" - it doesn't even set it to zero - so we don't
165 bother looking at it.
167 Ideally, libpcap would have an interface that gave us
168 several statistics - perhaps including various interface
169 error statistics - and would tell us which of them it
170 supplies, allowing us to display only the ones it does. */
171 cf_set_drops(capture_opts->cf, drops);
174 /* read in the packet data */
175 switch (cf_read(capture_opts->cf)) {
179 /* Just because we got an error, that doesn't mean we were unable
180 to read any of the file; we handle what we could get from the
184 case CF_READ_ABORTED:
185 /* User wants to quit program. Exit by leaving the main loop,
186 so that any quit functions we registered get called. */
187 main_window_nested_quit();
191 /* if we didn't captured even a single packet, close the file again */
192 if(cf_packet_count(capture_opts->cf) == 0) {
193 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
194 "%sNo packets captured!%s\n\n"
195 "As no data was captured, closing the %scapture file!",
196 simple_dialog_primary_start(), simple_dialog_primary_end(),
197 (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
198 cf_close(capture_opts->cf);
204 /* capture child tells us, we have a new (or the first) capture file */
206 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
208 gboolean is_tempfile;
212 /*g_warning("New capture file: %s", new_file);*/
214 /* free the old filename */
215 if(capture_opts->save_file != NULL) {
216 /* we start a new capture file, close the old one (if we had one before) */
217 if( ((capture_file *) capture_opts->cf)->state != FILE_CLOSED) {
218 cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
219 cf_finish_tail(capture_opts->cf, &err);
220 cf_close(capture_opts->cf);
222 g_free(capture_opts->save_file);
224 cf_set_tempfile(capture_opts->cf, FALSE);
226 /* we didn't had a save_file before, must be a tempfile */
228 cf_set_tempfile(capture_opts->cf, TRUE);
231 /* save the new filename */
232 capture_opts->save_file = g_strdup(new_file);
234 /* if we are in real-time mode, open the new file now */
235 if(capture_opts->real_time_mode) {
236 /* Attempt to open the capture file and set up to read from it. */
237 switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
241 /* Don't unlink (delete) the save file - leave it around,
242 for debugging purposes. */
243 g_free(capture_opts->save_file);
244 capture_opts->save_file = NULL;
249 cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
251 cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
259 /* capture child tells us, we have new packets to read */
261 capture_input_new_packets(capture_options *capture_opts, int to_read)
266 if(capture_opts->real_time_mode) {
267 /* Read from the capture file the number of records the child told us
269 XXX - do something if this fails? */
270 switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
274 /* Just because we got an error, that doesn't mean we were unable
275 to read any of the file; we handle what we could get from the
278 XXX - abort on a read error? */
279 main_window_update();
282 case CF_READ_ABORTED:
283 /* Kill the child capture process; the user wants to exit, and we
284 shouldn't just leave it running. */
285 capture_kill_child(capture_opts);
292 /* capture child closed it's side ot the pipe, do the required cleanup */
294 capture_input_closed(capture_options *capture_opts)
299 if(capture_opts->real_time_mode) {
300 /* first of all, we are not doing a capture any more */
301 cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
303 /* Read what remains of the capture file, and finish the capture.
304 XXX - do something if this fails? */
305 switch (cf_finish_tail(capture_opts->cf, &err)) {
308 if(cf_packet_count(capture_opts->cf) == 0) {
309 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
310 "%sNo packets captured!%s\n\n"
311 "As no data was captured, closing the %scapture file!",
312 simple_dialog_primary_start(), simple_dialog_primary_end(),
313 cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
314 cf_close(capture_opts->cf);
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
323 case CF_READ_ABORTED:
324 /* Exit by leaving the main loop, so that any quit functions
325 we registered get called. */
330 /* first of all, we are not doing a capture any more */
331 cf_callback_invoke(cf_cb_live_capture_fixed_finished, capture_opts->cf);
333 /* this is a normal mode capture, read in the capture file data */
334 capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf),
335 cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
338 /* does the user wants to restart the current capture? */
339 if(capture_opts->restart) {
340 capture_opts->restart = FALSE;
342 unlink(capture_opts->save_file);
344 /* if it was a tempfile, throw away the old filename (so it will become a tempfile again) */
345 if(cf_is_tempfile(capture_opts->cf)) {
346 g_free(capture_opts->save_file);
347 capture_opts->save_file = NULL;
350 /* ... and start the capture again */
351 capture_start(capture_opts);
353 /* We're not doing a capture any more, so we don't have a save file. */
354 g_free(capture_opts->save_file);
355 capture_opts->save_file = NULL;
360 #endif /* HAVE_LIBPCAP */