- undo disabling the GTK2 version
[obnox/wireshark/wip.git] / capture.c
1 /* capture.c
2  * Routines for packet capture windows
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_LIBPCAP
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <ctype.h>
34
35 #ifdef HAVE_FCNTL_H
36 #include <fcntl.h>
37 #endif
38
39 #ifdef HAVE_IO_H
40 # include <io.h>
41 #endif
42
43 #include <signal.h>
44 #include <errno.h>
45
46 #include <pcap.h>
47
48 #include <glib.h>
49
50 #include <epan/packet.h>
51 #include <epan/dfilter/dfilter.h>
52 #include "file.h"
53 #include "capture.h"
54 #include "capture_sync.h"
55 #include "capture_ui_utils.h"
56 #include "util.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"
63
64 #ifdef _WIN32
65 #include "capture-wpcap.h"
66 #endif
67 #include "ui_util.h"
68
69
70 /* Win32 needs the O_BINARY flag for open() */
71 #ifndef O_BINARY
72 #define O_BINARY        0
73 #endif
74
75 static gboolean normal_do_capture(capture_options *capture_opts, gboolean is_tempfile);
76 static void stop_capture_signal_handler(int signo);
77
78
79 /* open the output file (temporary/specified name/ringbuffer) and close the old one */
80 /* Returns TRUE if the file opened successfully, FALSE otherwise. */
81 static gboolean
82 capture_open_output(capture_options *capture_opts, gboolean *is_tempfile) {
83   char tmpname[128+1];
84   gchar *capfile_name;
85
86
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.
91      */
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);
97     } else {
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,
100                                 0600);
101     }
102     *is_tempfile = FALSE;
103   } else {
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);
107     *is_tempfile = TRUE;
108   }
109
110   /* did we fail to open the output file? */
111   if (capture_opts->save_file_fd == -1) {
112     if (is_tempfile) {
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));
116     } else {
117       if (capture_opts->multi_files_on) {
118         ringbuf_error_cleanup();
119       }
120       open_failure_alert_box(capfile_name, errno, TRUE);
121     }
122     g_free(capfile_name);
123     return FALSE;
124   }
125
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);
130   }
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)". */
134
135   return TRUE;
136 }
137
138
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. */
142 gboolean
143 do_capture(capture_options *capture_opts)
144 {
145   gboolean is_tempfile;
146   gboolean ret;
147
148   /* open the output file (temporary/specified name/ringbuffer) and close the old one */
149   if(!capture_open_output(capture_opts, &is_tempfile)) {
150     return FALSE;
151   }
152
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);
158   } else {
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 */
163   }
164
165   return ret;
166 }
167
168
169 /* start a normal capture session */
170 static gboolean
171 normal_do_capture(capture_options *capture_opts, gboolean is_tempfile)
172 {
173     int capture_succeeded;
174     gboolean stats_known;
175     struct pcap_stat stats;
176     int err;
177
178     /* Not sync mode. */
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. */
182         main_window_exit();
183     }
184     if (!capture_succeeded) {
185       /* We didn't succeed in doing the capture, so we don't have a save
186          file. */
187       if (capture_opts->multi_files_on) {
188         ringbuf_free();
189       } else {
190         g_free(capture_opts->save_file);
191       }
192       capture_opts->save_file = NULL;
193       return FALSE;
194     }
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
198          file. */
199       if (capture_opts->multi_files_on) {
200         ringbuf_free();
201       } else {
202         g_free(capture_opts->save_file);
203       }
204       capture_opts->save_file = NULL;
205       return FALSE;
206     }
207
208     /* Set the read filter to NULL. */
209     cf_set_rfcode(capture_opts->cf, NULL);
210
211     /* Get the packet-drop statistics.
212
213        XXX - there are currently no packet-drop statistics stored
214        in libpcap captures, and that's what we're reading.
215
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()".
224
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. */
229     if (stats_known) {
230       cf_set_drops_known(capture_opts->cf, TRUE);
231
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.
235
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);
241     }
242     switch (cf_read(capture_opts->cf)) {
243
244     case CF_READ_OK:
245     case CF_READ_ERROR:
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
248          file. */
249       break;
250
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();
255       return FALSE;
256     }
257
258     /* We're not doing a capture any more, so we don't have a save
259        file. */
260     if (capture_opts->multi_files_on) {
261       ringbuf_free();
262     } else {
263       g_free(capture_opts->save_file);
264     }
265     capture_opts->save_file = NULL;
266
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);
275     }
276   return TRUE;
277 }
278
279
280 static void
281 stop_capture_signal_handler(int signo _U_)
282 {
283   capture_loop_stop();
284 }
285
286
287 int  
288 capture_start(capture_options *capture_opts, gboolean *stats_known, struct pcap_stat *stats)
289 {
290 #ifndef _WIN32
291   /*
292    * Catch SIGUSR1, so that we exit cleanly if the parent process
293    * kills us with it due to the user selecting "Capture->Stop".
294    */
295   if (capture_opts->capture_child)
296     signal(SIGUSR1, stop_capture_signal_handler);
297 #endif
298
299   return capture_loop_start(capture_opts, stats_known, stats);
300 }
301
302 void
303 capture_stop(capture_options *capture_opts)
304 {
305
306   if (capture_opts->sync_mode) {        
307     sync_pipe_stop(capture_opts);
308   }
309     
310   capture_loop_stop();
311 }
312
313 void
314 capture_kill_child(capture_options *capture_opts)
315 {
316   if (capture_opts->sync_mode) {        
317     sync_pipe_kill(capture_opts);
318   }
319 }
320
321
322 #endif /* HAVE_LIBPCAP */