2 * Routines for packet capture windows
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"
70 /* Win32 needs the O_BINARY flag for open() */
75 static gboolean normal_do_capture(capture_options *capture_opts, gboolean is_tempfile);
76 static void stop_capture_signal_handler(int signo);
79 /* open the output file (temporary/specified name/ringbuffer) and close the old one */
80 /* Returns TRUE if the file opened successfully, FALSE otherwise. */
82 capture_open_output(capture_options *capture_opts, gboolean *is_tempfile) {
87 if (capture_opts->save_file != NULL) {
88 /* If the Sync option is set, we return to the caller while the capture
89 * is in progress. Therefore we need to take a copy of save_file in
90 * case the caller destroys it after we return.
92 capfile_name = g_strdup(capture_opts->save_file);
93 if (capture_opts->multi_files_on) {
94 /* ringbuffer is enabled */
95 capture_opts->save_file_fd = ringbuf_init(capfile_name,
96 (capture_opts->has_ring_num_files) ? capture_opts->ring_num_files : 0);
98 /* Try to open/create the specified file for use as a capture buffer. */
99 capture_opts->save_file_fd = open(capfile_name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
102 *is_tempfile = FALSE;
104 /* Choose a random name for the temporary capture buffer */
105 capture_opts->save_file_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
106 capfile_name = g_strdup(tmpname);
110 /* did we fail to open the output file? */
111 if (capture_opts->save_file_fd == -1) {
113 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
114 "The temporary file to which the capture would be saved (\"%s\")"
115 "could not be opened: %s.", capfile_name, strerror(errno));
117 if (capture_opts->multi_files_on) {
118 ringbuf_error_cleanup();
120 open_failure_alert_box(capfile_name, errno, TRUE);
122 g_free(capfile_name);
126 /* close the old file */
127 cf_close(capture_opts->cf);
128 if(capture_opts->save_file != NULL) {
129 g_free(capture_opts->save_file);
131 capture_opts->save_file = capfile_name;
132 /* capture_opts.save_file is "g_free"ed later, which is equivalent to
133 "g_free(capfile_name)". */
139 /* Open a specified file, or create a temporary file, and start a capture
140 to the file in question. */
141 /* Returns TRUE if the capture starts successfully, FALSE otherwise. */
143 do_capture(capture_options *capture_opts)
145 gboolean is_tempfile;
148 /* open the output file (temporary/specified name/ringbuffer) and close the old one */
149 if(!capture_open_output(capture_opts, &is_tempfile)) {
153 if (capture_opts->sync_mode) {
154 /* sync mode: do the capture in a child process */
155 ret = sync_pipe_do_capture(capture_opts, is_tempfile);
156 /* capture is still running */
157 cf_callback_invoke(cf_cb_live_capture_prepare, capture_opts);
159 /* normal mode: do the capture synchronously */
160 cf_callback_invoke(cf_cb_live_capture_prepare, capture_opts);
161 ret = normal_do_capture(capture_opts, is_tempfile);
162 /* capture is finished here */
169 /* start a normal capture session */
171 normal_do_capture(capture_options *capture_opts, gboolean is_tempfile)
173 int capture_succeeded;
174 gboolean stats_known;
175 struct pcap_stat stats;
179 capture_succeeded = capture_start(capture_opts, &stats_known, &stats);
180 if (capture_opts->quit_after_cap) {
181 /* DON'T unlink the save file. Presumably someone wants it. */
184 if (!capture_succeeded) {
185 /* We didn't succeed in doing the capture, so we don't have a save
187 if (capture_opts->multi_files_on) {
190 g_free(capture_opts->save_file);
192 capture_opts->save_file = NULL;
195 /* Capture succeeded; attempt to read in the capture file. */
196 if (cf_open(capture_opts->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
197 /* We're not doing a capture any more, so we don't have a save
199 if (capture_opts->multi_files_on) {
202 g_free(capture_opts->save_file);
204 capture_opts->save_file = NULL;
208 /* Set the read filter to NULL. */
209 cf_set_rfcode(capture_opts->cf, NULL);
211 /* Get the packet-drop statistics.
213 XXX - there are currently no packet-drop statistics stored
214 in libpcap captures, and that's what we're reading.
216 At some point, we will add support in Wiretap to return
217 packet-drop statistics for capture file formats that store it,
218 and will make "cf_read()" get those statistics from Wiretap.
219 We clear the statistics (marking them as "not known") in
220 "cf_open()", and "cf_read()" will only fetch them and mark
221 them as known if Wiretap supplies them, so if we get the
222 statistics now, after calling "cf_open()" but before calling
223 "cf_read()", the values we store will be used by "cf_read()".
225 If a future libpcap capture file format stores the statistics,
226 we'll put them into the capture file that we write, and will
227 thus not have to set them here - "cf_read()" will get them from
228 the file and use them. */
230 cf_set_drops_known(capture_opts->cf, TRUE);
232 /* XXX - on some systems, libpcap doesn't bother filling in
233 "ps_ifdrop" - it doesn't even set it to zero - so we don't
234 bother looking at it.
236 Ideally, libpcap would have an interface that gave us
237 several statistics - perhaps including various interface
238 error statistics - and would tell us which of them it
239 supplies, allowing us to display only the ones it does. */
240 cf_set_drops(capture_opts->cf, stats.ps_drop);
242 switch (cf_read(capture_opts->cf)) {
246 /* Just because we got an error, that doesn't mean we were unable
247 to read any of the file; we handle what we could get from the
251 case CF_READ_ABORTED:
252 /* Exit by leaving the main loop, so that any quit functions
253 we registered get called. */
254 main_window_nested_quit();
258 /* We're not doing a capture any more, so we don't have a save
260 if (capture_opts->multi_files_on) {
263 g_free(capture_opts->save_file);
265 capture_opts->save_file = NULL;
267 /* if we didn't captured even a single packet, close the file again */
268 if(cf_packet_count(capture_opts->cf) == 0) {
269 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
270 "%sNo packets captured!%s\n\n"
271 "As no data was captured, closing the %scapture file!",
272 simple_dialog_primary_start(), simple_dialog_primary_end(),
273 (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
274 cf_close(capture_opts->cf);
281 stop_capture_signal_handler(int signo _U_)
288 capture_start(capture_options *capture_opts, gboolean *stats_known, struct pcap_stat *stats)
292 * Catch SIGUSR1, so that we exit cleanly if the parent process
293 * kills us with it due to the user selecting "Capture->Stop".
295 if (capture_opts->capture_child)
296 signal(SIGUSR1, stop_capture_signal_handler);
299 return capture_loop_start(capture_opts, stats_known, stats);
303 capture_stop(capture_options *capture_opts)
306 if (capture_opts->sync_mode) {
307 sync_pipe_stop(capture_opts);
314 capture_kill_child(capture_options *capture_opts)
316 if (capture_opts->sync_mode) {
317 sync_pipe_kill(capture_opts);
322 #endif /* HAVE_LIBPCAP */