Put "cf_status_t" back.
[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 /* 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_
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #ifdef HAVE_LIBPCAP
34
35 #include <stdlib.h>
36 #include <string.h>
37
38 #ifdef HAVE_FCNTL_H
39 #include <fcntl.h>
40 #endif
41
42 #ifdef HAVE_IO_H
43 # include <io.h>
44 #endif
45
46 #include <signal.h>
47 #include <errno.h>
48
49 #include <pcap.h>
50
51 #include <glib.h>
52
53 #include <epan/packet.h>
54 #include <epan/dfilter/dfilter.h>
55 #include "file.h"
56 #include "capture.h"
57 #include "capture_sync.h"
58 #include "capture_ui_utils.h"
59 #include "util.h"
60 #include "pcap-util.h"
61 #include "alert_box.h"
62 #include "simple_dialog.h"
63 #include <epan/prefs.h>
64 #include "conditions.h"
65 #include "ringbuffer.h"
66
67 #ifdef _WIN32
68 #include "capture-wpcap.h"
69 #endif
70 #include "ui_util.h"
71
72
73 /* Win32 needs the O_BINARY flag for open() */
74 #ifndef O_BINARY
75 #define O_BINARY        0
76 #endif
77
78 static gboolean normal_do_capture(capture_options *capture_opts, gboolean is_tempfile);
79 static void stop_capture_signal_handler(int signo);
80
81
82 /* open the output file (temporary/specified name/ringbuffer) and close the old one */
83 /* Returns TRUE if the file opened successfully, FALSE otherwise. */
84 static gboolean
85 capture_open_output(capture_options *capture_opts, const char *save_file, gboolean *is_tempfile) {
86   char tmpname[128+1];
87   gchar *capfile_name;
88
89
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.
94      */
95     capfile_name = g_strdup(save_file);
96     if (capture_opts->multi_files_on) {
97       /* ringbuffer is enabled */
98       capture_opts->save_file_fd = ringbuf_init(capfile_name,
99           (capture_opts->has_ring_num_files) ? capture_opts->ring_num_files : 0);
100     } else {
101       /* Try to open/create the specified file for use as a capture buffer. */
102       capture_opts->save_file_fd = open(capfile_name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
103                                 0600);
104     }
105     *is_tempfile = FALSE;
106   } else {
107     /* Choose a random name for the temporary capture buffer */
108     capture_opts->save_file_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
109     capfile_name = g_strdup(tmpname);
110     *is_tempfile = TRUE;
111   }
112
113   /* did we fail to open the output file? */
114   if (capture_opts->save_file_fd == -1) {
115     if (is_tempfile) {
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));
119     } else {
120       if (capture_opts->multi_files_on) {
121         ringbuf_error_cleanup();
122       }
123       open_failure_alert_box(capfile_name, errno, TRUE);
124     }
125     g_free(capfile_name);
126     return FALSE;
127   }
128
129   /* close the old file */
130   cf_close(capture_opts->cf);
131   g_assert(capture_opts->save_file == NULL);
132   capture_opts->save_file = capfile_name;
133   /* capture_opts.save_file is "g_free"ed later, which is equivalent to
134      "g_free(capfile_name)". */
135
136   return TRUE;
137 }
138
139
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. */
143 gboolean
144 do_capture(capture_options *capture_opts, const char *save_file)
145 {
146   gboolean is_tempfile;
147   gboolean ret;
148   gchar *title;
149
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)) {
152     return FALSE;
153   }
154
155   title = g_strdup_printf("%s: Capturing - Ethereal",
156                           get_interface_descriptive_name(cf_get_iface(capture_opts->cf)));
157   if (capture_opts->sync_mode) {        
158     /* sync mode: do the capture in a child process */
159     ret = sync_pipe_do_capture(capture_opts, is_tempfile);
160     /* capture is still running */
161     set_main_window_name(title);
162   } else {
163     /* normal mode: do the capture synchronously */
164     set_main_window_name(title);
165     ret = normal_do_capture(capture_opts, is_tempfile);
166     /* capture is finished here */
167   }
168   g_free(title);
169
170   return ret;
171 }
172
173
174 /* start a normal capture session */
175 static gboolean
176 normal_do_capture(capture_options *capture_opts, gboolean is_tempfile)
177 {
178     int capture_succeeded;
179     gboolean stats_known;
180     struct pcap_stat stats;
181     int err;
182
183     /* Not sync mode. */
184     capture_succeeded = capture_start(capture_opts, &stats_known, &stats);
185     if (capture_opts->quit_after_cap) {
186       /* DON'T unlink the save file.  Presumably someone wants it. */
187         main_window_exit();
188     }
189     if (!capture_succeeded) {
190       /* We didn't succeed in doing the capture, so we don't have a save
191          file. */
192       if (capture_opts->multi_files_on) {
193         ringbuf_free();
194       } else {
195         g_free(capture_opts->save_file);
196       }
197       capture_opts->save_file = NULL;
198       return FALSE;
199     }
200     /* Capture succeeded; attempt to read in the capture file. */
201     if (cf_open(capture_opts->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
202       /* We're not doing a capture any more, so we don't have a save
203          file. */
204       if (capture_opts->multi_files_on) {
205         ringbuf_free();
206       } else {
207         g_free(capture_opts->save_file);
208       }
209       capture_opts->save_file = NULL;
210       return FALSE;
211     }
212
213     /* Set the read filter to NULL. */
214     cf_set_rfcode(capture_opts->cf, NULL);
215
216     /* Get the packet-drop statistics.
217
218        XXX - there are currently no packet-drop statistics stored
219        in libpcap captures, and that's what we're reading.
220
221        At some point, we will add support in Wiretap to return
222        packet-drop statistics for capture file formats that store it,
223        and will make "cf_read()" get those statistics from Wiretap.
224        We clear the statistics (marking them as "not known") in
225        "cf_open()", and "cf_read()" will only fetch them and mark
226        them as known if Wiretap supplies them, so if we get the
227        statistics now, after calling "cf_open()" but before calling
228        "cf_read()", the values we store will be used by "cf_read()".
229
230        If a future libpcap capture file format stores the statistics,
231        we'll put them into the capture file that we write, and will
232        thus not have to set them here - "cf_read()" will get them from
233        the file and use them. */
234     if (stats_known) {
235       cf_set_drops_known(capture_opts->cf, TRUE);
236
237       /* XXX - on some systems, libpcap doesn't bother filling in
238          "ps_ifdrop" - it doesn't even set it to zero - so we don't
239          bother looking at it.
240
241          Ideally, libpcap would have an interface that gave us
242          several statistics - perhaps including various interface
243          error statistics - and would tell us which of them it
244          supplies, allowing us to display only the ones it does. */
245       cf_set_drops(capture_opts->cf, stats.ps_drop);
246     }
247     switch (cf_read(capture_opts->cf)) {
248
249     case CF_READ_OK:
250     case CF_READ_ERROR:
251       /* Just because we got an error, that doesn't mean we were unable
252          to read any of the file; we handle what we could get from the
253          file. */
254       break;
255
256     case CF_READ_ABORTED:
257       /* Exit by leaving the main loop, so that any quit functions
258          we registered get called. */
259       main_window_nested_quit();
260       return FALSE;
261     }
262
263     /* We're not doing a capture any more, so we don't have a save
264        file. */
265     if (capture_opts->multi_files_on) {
266       ringbuf_free();
267     } else {
268       g_free(capture_opts->save_file);
269     }
270     capture_opts->save_file = NULL;
271
272     /* if we didn't captured even a single packet, close the file again */
273     if(cf_packet_count(capture_opts->cf) == 0) {
274       simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, 
275       "%sNo packets captured!%s\n\n"
276       "As no data was captured, closing the %scapture file!",
277       simple_dialog_primary_start(), simple_dialog_primary_end(),
278       (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
279       cf_close(capture_opts->cf);
280     }
281   return TRUE;
282 }
283
284
285 static void
286 stop_capture_signal_handler(int signo _U_)
287 {
288   capture_loop_stop();
289 }
290
291
292 int  
293 capture_start(capture_options *capture_opts, gboolean *stats_known, struct pcap_stat *stats)
294 {
295 #ifndef _WIN32
296   /*
297    * Catch SIGUSR1, so that we exit cleanly if the parent process
298    * kills us with it due to the user selecting "Capture->Stop".
299    */
300   if (capture_opts->capture_child)
301     signal(SIGUSR1, stop_capture_signal_handler);
302 #endif
303
304   return capture_loop_start(capture_opts, stats_known, stats);
305 }
306
307 void
308 capture_stop(capture_options *capture_opts)
309 {
310
311   if (capture_opts->sync_mode) {        
312     sync_pipe_stop(capture_opts);
313   }
314     
315   capture_loop_stop();
316 }
317
318 void
319 kill_capture_child(capture_options *capture_opts)
320 {
321   if (capture_opts->sync_mode) {        
322     sync_pipe_kill(capture_opts);
323   }
324 }
325
326
327 #endif /* HAVE_LIBPCAP */