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_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 (non real-time) 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 open 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);
153 /* read in the packet data */
154 switch (cf_read(capture_opts->cf)) {
158 /* Just because we got an error, that doesn't mean we were unable
159 to read any of the file; we handle what we could get from the
163 case CF_READ_ABORTED:
164 /* User wants to quit program. Exit by leaving the main loop,
165 so that any quit functions we registered get called. */
166 main_window_nested_quit();
170 /* if we didn't captured even a single packet, close the file again */
171 if(cf_packet_count(capture_opts->cf) == 0) {
172 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
173 "%sNo packets captured!%s\n\n"
174 "As no data was captured, closing the %scapture file!",
175 simple_dialog_primary_start(), simple_dialog_primary_end(),
176 (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
177 cf_close(capture_opts->cf);
183 /* capture child tells us, we have a new (or the first) capture file */
185 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
187 gboolean is_tempfile;
191 /*g_warning("New capture file: %s", new_file);*/
193 /* free the old filename */
194 if(capture_opts->save_file != NULL) {
195 /* we start a new capture file, simply close the old one */
196 /* XXX - is it enough to call cf_close here? */
197 /* XXX - is it safe to call cf_close even if the file is close before? */
198 cf_close(capture_opts->cf);
199 g_free(capture_opts->save_file);
202 /* we didn't had a save_file before, must be a tempfile */
204 cf_set_tempfile(capture_opts->cf, TRUE);
207 /* save the new filename */
208 capture_opts->save_file = g_strdup(new_file);
210 /* if we are in real-time mode, open the new file now */
211 if(capture_opts->real_time_mode) {
212 /* Attempt to open the capture file and set up to read from it. */
213 switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
217 /* Don't unlink (delete) the save file - leave it around,
218 for debugging purposes. */
219 g_free(capture_opts->save_file);
220 capture_opts->save_file = NULL;
225 cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts->cf);
227 cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts->cf);
235 /* capture child tells us, we have new packets to read */
237 capture_input_new_packets(capture_options *capture_opts, int to_read)
242 if(capture_opts->real_time_mode) {
243 /* Read from the capture file the number of records the child told us
245 XXX - do something if this fails? */
246 switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
250 /* Just because we got an error, that doesn't mean we were unable
251 to read any of the file; we handle what we could get from the
254 XXX - abort on a read error? */
257 case CF_READ_ABORTED:
258 /* Kill the child capture process; the user wants to exit, and we
259 shouldn't just leave it running. */
260 capture_kill_child(capture_opts);
267 /* capture child closed it's side ot the pipe, do the required cleanup */
269 capture_input_closed(capture_options *capture_opts)
274 if(capture_opts->real_time_mode) {
275 /* first of all, we are not doing a capture any more */
276 cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
278 /* Read what remains of the capture file, and finish the capture.
279 XXX - do something if this fails? */
280 switch (cf_finish_tail(capture_opts->cf, &err)) {
283 if(cf_packet_count(capture_opts->cf) == 0) {
284 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK,
285 "%sNo packets captured!%s\n\n"
286 "As no data was captured, closing the %scapture file!",
287 simple_dialog_primary_start(), simple_dialog_primary_end(),
288 cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
289 cf_close(capture_opts->cf);
293 /* Just because we got an error, that doesn't mean we were unable
294 to read any of the file; we handle what we could get from the
298 case CF_READ_ABORTED:
299 /* Exit by leaving the main loop, so that any quit functions
300 we registered get called. */
305 /* first of all, we are not doing a capture any more */
306 cf_callback_invoke(cf_cb_live_capture_fixed_finished, capture_opts->cf);
308 /* this is a normal mode capture, read in the capture file data */
309 capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf),
310 cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
313 /* We're not doing a capture any more, so we don't have a save file. */
314 g_assert(capture_opts->save_file);
315 g_free(capture_opts->save_file);
316 capture_opts->save_file = NULL;
321 capture_stop(capture_options *capture_opts)
323 /* stop the capture child gracefully, if we have one */
324 sync_pipe_stop(capture_opts);
328 capture_kill_child(capture_options *capture_opts)
330 /* kill the capture child, if we have one */
331 sync_pipe_kill(capture_opts);
335 #endif /* HAVE_LIBPCAP */