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 static void stop_capture_signal_handler(int signo);
75 /* Returns TRUE if the capture starts successfully, FALSE otherwise. */
77 do_capture(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_do_capture(capture_opts, capture_opts->save_file == NULL);
89 /* tell callbacks (menu, ...) that capture is running now */
90 cf_callback_invoke(cf_cb_live_capture_prepare, capture_opts);
92 if(capture_opts->save_file != NULL) {
93 g_free(capture_opts->save_file);
94 capture_opts->save_file = NULL;
102 /* we've succeeded a capture, try to read it into a new capture file */
104 capture_input_read_all(capture_options *capture_opts, gboolean is_tempfile, gboolean drops_known,
110 /* Capture succeeded; attempt to read in the capture file. */
111 if (cf_open(capture_opts->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
112 /* We're not doing a capture any more, so we don't have a save
117 /* Set the read filter to NULL. */
118 /* XXX - this is odd here, try to put it somewhere, where it fits better */
119 cf_set_rfcode(capture_opts->cf, NULL);
121 /* Get the packet-drop statistics.
123 XXX - there are currently no packet-drop statistics stored
124 in libpcap captures, and that's what we're reading.
126 At some point, we will add support in Wiretap to return
127 packet-drop statistics for capture file formats that store it,
128 and will make "cf_read()" get those statistics from Wiretap.
129 We clear the statistics (marking them as "not known") in
130 "cf_open()", and "cf_read()" will only fetch them and mark
131 them as known if Wiretap supplies them, so if we get the
132 statistics now, after calling "cf_open()" but before calling
133 "cf_read()", the values we store will be used by "cf_read()".
135 If a future libpcap capture file format stores the statistics,
136 we'll put them into the capture file that we write, and will
137 thus not have to set them here - "cf_read()" will get them from
138 the file and use them. */
140 cf_set_drops_known(capture_opts->cf, TRUE);
142 /* XXX - on some systems, libpcap doesn't bother filling in
143 "ps_ifdrop" - it doesn't even set it to zero - so we don't
144 bother looking at it.
146 Ideally, libpcap would have an interface that gave us
147 several statistics - perhaps including various interface
148 error statistics - and would tell us which of them it
149 supplies, allowing us to display only the ones it does. */
150 cf_set_drops(capture_opts->cf, drops);
152 switch (cf_read(capture_opts->cf)) {
156 /* Just because we got an error, that doesn't mean we were unable
157 to read any of the file; we handle what we could get from the
161 case CF_READ_ABORTED:
162 /* Exit by leaving the main loop, so that any quit functions
163 we registered get called. */
164 main_window_nested_quit();
168 /* if we didn't captured even a single packet, close the file again */
169 if(cf_packet_count(capture_opts->cf) == 0) {
170 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
171 "%sNo packets captured!%s\n\n"
172 "As no data was captured, closing the %scapture file!",
173 simple_dialog_primary_start(), simple_dialog_primary_end(),
174 (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
175 cf_close(capture_opts->cf);
181 /* capture child tells us, we have a new (or the first) capture file */
183 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
185 gboolean is_tempfile;
189 /*g_warning("New capture file: %s", new_file);*/
191 /* save the new filename */
192 if(capture_opts->save_file != NULL) {
193 /* we start a new capture file, simply close the old one */
194 /* XXX - is it enough to call cf_close here? */
195 /* XXX - is it safe to call cf_close even if the file is close before? */
196 cf_close(capture_opts->cf);
197 g_free(capture_opts->save_file);
200 /* we didn't had a save_file before, must be a tempfile */
202 cf_set_tempfile(capture_opts->cf, TRUE);
204 capture_opts->save_file = g_strdup(new_file);
206 /* if we are in real-time mode, open the new file */
207 if(capture_opts->real_time_mode) {
208 /* The child process started a capture.
209 Attempt to open the capture file and set up to read it. */
210 switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
214 /* Don't unlink the save file - leave it around, for debugging
216 g_free(capture_opts->save_file);
217 capture_opts->save_file = NULL;
227 /* capture child tells us, we have new packets to read */
229 capture_input_new_packets(capture_options *capture_opts, int to_read)
234 if(capture_opts->real_time_mode) {
235 /* Read from the capture file the number of records the child told us
237 XXX - do something if this fails? */
238 switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
242 /* Just because we got an error, that doesn't mean we were unable
243 to read any of the file; we handle what we could get from the
246 XXX - abort on a read error? */
249 case CF_READ_ABORTED:
250 /* Kill the child capture process; the user wants to exit, and we
251 shouldn't just leave it running. */
252 capture_kill_child(capture_opts);
259 /* capture child closed it's side ot the pipe, do the required cleanup */
261 capture_input_closed(capture_options *capture_opts)
266 if(capture_opts->real_time_mode) {
267 /* Read what remains of the capture file, and finish the capture.
268 XXX - do something if this fails? */
269 switch (cf_finish_tail(capture_opts->cf, &err)) {
272 if(cf_packet_count(capture_opts->cf) == 0) {
273 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
274 "%sNo packets captured!%s\n\n"
275 "As no data was captured, closing the %scapture file!",
276 simple_dialog_primary_start(), simple_dialog_primary_end(),
277 cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
278 cf_close(capture_opts->cf);
282 /* Just because we got an error, that doesn't mean we were unable
283 to read any of the file; we handle what we could get from the
287 case CF_READ_ABORTED:
288 /* Exit by leaving the main loop, so that any quit functions
289 we registered get called. */
293 /* this is a normal mode capture, read in the capture file data */
294 capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf),
295 cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
298 /* We're not doing a capture any more, so we don't have a save file. */
299 g_assert(capture_opts->save_file);
300 g_free(capture_opts->save_file);
301 capture_opts->save_file = NULL;
307 capture_child_stop_signal_handler(int signo _U_)
315 capture_child_start(capture_options *capture_opts, gboolean *stats_known, struct pcap_stat *stats)
319 * Catch SIGUSR1, so that we exit cleanly if the parent process
320 * kills us with it due to the user selecting "Capture->Stop".
322 signal(SIGUSR1, capture_child_stop_signal_handler);
325 return capture_loop_start(capture_opts, stats_known, stats);
329 capture_child_stop(capture_options *capture_opts)
331 /* stop the capture loop */
336 capture_stop(capture_options *capture_opts)
338 /* stop the capture child, if we have one */
339 sync_pipe_stop(capture_opts);
343 capture_kill_child(capture_options *capture_opts)
345 /* kill the capture child, if we have one */
346 sync_pipe_kill(capture_opts);
350 #endif /* HAVE_LIBPCAP */