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.
25 /* With MSVC and a libethereal.dll this file needs to import some variables
26 in a special way. Therefore _NEED_VAR_IMPORT_ is defined. */
27 #define _NEED_VAR_IMPORT_
53 #include <epan/packet.h>
54 #include <epan/dfilter/dfilter.h>
57 #include "capture_sync.h"
59 #include "pcap-util.h"
60 #include "alert_box.h"
61 #include "simple_dialog.h"
62 #include <epan/prefs.h>
64 #include "conditions.h"
65 #include "ringbuffer.h"
68 #include "capture-wpcap.h"
73 /* Win32 needs the O_BINARY flag for open() */
78 static gboolean normal_do_capture(capture_options *capture_opts, gboolean is_tempfile);
79 static void stop_capture_signal_handler(int signo);
82 /* open the output file (temporary/specified name/ringbuffer) and close the old one */
83 /* Returns TRUE if the file opened successfully, FALSE otherwise. */
85 capture_open_output(capture_options *capture_opts, const char *save_file, gboolean *is_tempfile) {
90 if (save_file != NULL) {
91 /* If the Sync option is set, we return to the caller while the capture
92 * is in progress. Therefore we need to take a copy of save_file in
93 * case the caller destroys it after we return.
95 capfile_name = g_strdup(save_file);
96 if (capture_opts->multi_files_on) {
97 /* ringbuffer is enabled */
98 cfile.save_file_fd = ringbuf_init(capfile_name,
99 (capture_opts->has_ring_num_files) ? capture_opts->ring_num_files : 0);
101 /* Try to open/create the specified file for use as a capture buffer. */
102 cfile.save_file_fd = open(capfile_name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
105 *is_tempfile = FALSE;
107 /* Choose a random name for the temporary capture buffer */
108 cfile.save_file_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
109 capfile_name = g_strdup(tmpname);
113 /* did we fail to open the output file? */
114 if (cfile.save_file_fd == -1) {
116 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
117 "The temporary file to which the capture would be saved (\"%s\")"
118 "could not be opened: %s.", capfile_name, strerror(errno));
120 if (capture_opts->multi_files_on) {
121 ringbuf_error_cleanup();
123 open_failure_alert_box(capfile_name, errno, TRUE);
125 g_free(capfile_name);
129 /* close the old file */
131 g_assert(cfile.save_file == NULL);
132 cfile.save_file = capfile_name;
133 /* cfile.save_file is "g_free"ed later, which is equivalent to
134 "g_free(capfile_name)". */
140 /* Open a specified file, or create a temporary file, and start a capture
141 to the file in question. */
142 /* Returns TRUE if the capture starts successfully, FALSE otherwise. */
144 do_capture(capture_options *capture_opts, const char *save_file)
146 gboolean is_tempfile;
150 /* open the output file (temporary/specified name/ringbuffer) and close the old one */
151 if(!capture_open_output(capture_opts, save_file, &is_tempfile)) {
155 if (capture_opts->sync_mode) {
156 /* sync mode: do the capture in a child process */
157 ret = sync_pipe_do_capture(capture_opts, is_tempfile);
158 /* capture is still running */
159 set_main_window_name("(Live Capture in Progress) - Ethereal");
161 /* normal mode: do the capture synchronously */
162 set_main_window_name("(Live Capture in Progress) - Ethereal");
163 ret = normal_do_capture(capture_opts, is_tempfile);
164 /* capture is finished here */
171 /* start a normal capture session */
173 normal_do_capture(capture_options *capture_opts, gboolean is_tempfile)
175 int capture_succeeded;
176 gboolean stats_known;
177 struct pcap_stat stats;
181 capture_succeeded = capture_start(capture_opts, &stats_known, &stats);
182 if (capture_opts->quit_after_cap) {
183 /* DON'T unlink the save file. Presumably someone wants it. */
186 if (!capture_succeeded) {
187 /* We didn't succeed in doing the capture, so we don't have a save
189 if (capture_opts->multi_files_on) {
192 g_free(cfile.save_file);
194 cfile.save_file = NULL;
197 /* Capture succeeded; attempt to read in the capture file. */
198 if ((err = cf_open(cfile.save_file, is_tempfile, &cfile)) != 0) {
199 /* We're not doing a capture any more, so we don't have a save
201 if (capture_opts->multi_files_on) {
204 g_free(cfile.save_file);
206 cfile.save_file = NULL;
210 /* Set the read filter to NULL. */
213 /* Get the packet-drop statistics.
215 XXX - there are currently no packet-drop statistics stored
216 in libpcap captures, and that's what we're reading.
218 At some point, we will add support in Wiretap to return
219 packet-drop statistics for capture file formats that store it,
220 and will make "cf_read()" get those statistics from Wiretap.
221 We clear the statistics (marking them as "not known") in
222 "cf_open()", and "cf_read()" will only fetch them and mark
223 them as known if Wiretap supplies them, so if we get the
224 statistics now, after calling "cf_open()" but before calling
225 "cf_read()", the values we store will be used by "cf_read()".
227 If a future libpcap capture file format stores the statistics,
228 we'll put them into the capture file that we write, and will
229 thus not have to set them here - "cf_read()" will get them from
230 the file and use them. */
232 cfile.drops_known = TRUE;
234 /* XXX - on some systems, libpcap doesn't bother filling in
235 "ps_ifdrop" - it doesn't even set it to zero - so we don't
236 bother looking at it.
238 Ideally, libpcap would have an interface that gave us
239 several statistics - perhaps including various interface
240 error statistics - and would tell us which of them it
241 supplies, allowing us to display only the ones it does. */
242 cfile.drops = stats.ps_drop;
244 switch (cf_read(&cfile)) {
248 /* Just because we got an error, that doesn't mean we were unable
249 to read any of the file; we handle what we could get from the
254 /* Exit by leaving the main loop, so that any quit functions
255 we registered get called. */
256 main_window_nested_quit();
260 /* We're not doing a capture any more, so we don't have a save
262 if (capture_opts->multi_files_on) {
265 g_free(cfile.save_file);
267 cfile.save_file = NULL;
269 /* if we didn't captured even a single packet, close the file again */
270 if(cfile.count == 0) {
271 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
272 "%sNo packets captured!%s\n\n"
273 "As no data was captured, closing the %scapture file!",
274 simple_dialog_primary_start(), simple_dialog_primary_end(),
275 (cfile.is_tempfile) ? "temporary " : "");
283 stop_capture_signal_handler(int signo _U_)
290 capture_start(capture_options *capture_opts, gboolean *stats_known, struct pcap_stat *stats)
294 * Catch SIGUSR1, so that we exit cleanly if the parent process
295 * kills us with it due to the user selecting "Capture->Stop".
297 if (capture_opts->capture_child)
298 signal(SIGUSR1, stop_capture_signal_handler);
301 return capture_loop_start(capture_opts, stats_known, stats);
305 capture_stop(gboolean sync_mode)
316 kill_capture_child(gboolean sync_mode)
324 #endif /* HAVE_LIBPCAP */