bugfix: button in capture info dialog has to stop capturing
[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 "util.h"
59 #include "pcap-util.h"
60 #include "alert_box.h"
61 #include "simple_dialog.h"
62 #include <epan/prefs.h>
63 #include "globals.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  * Capture options.
74  */
75 capture_options capture_opts;
76 gboolean capture_child;         /* if this is the child for "-S" */
77
78
79 /* Win32 needs the O_BINARY flag for open() */
80 #ifndef O_BINARY
81 #define O_BINARY        0
82 #endif
83
84 static gboolean normal_do_capture(gboolean is_tempfile);
85 static void stop_capture_signal_handler(int signo);
86
87 /* Open a specified file, or create a temporary file, and start a capture
88    to the file in question.  Returns TRUE if the capture starts
89    successfully, FALSE otherwise. */
90 gboolean
91 do_capture(const char *save_file)
92 {
93   char tmpname[128+1];
94   gboolean is_tempfile;
95   gchar *capfile_name;
96   gboolean ret;
97
98   if (save_file != NULL) {
99     /* If the Sync option is set, we return to the caller while the capture
100      * is in progress.  Therefore we need to take a copy of save_file in
101      * case the caller destroys it after we return.
102      */
103     capfile_name = g_strdup(save_file);
104     if (capture_opts.multi_files_on) {
105       /* ringbuffer is enabled */
106       cfile.save_file_fd = ringbuf_init(capfile_name,
107           (capture_opts.has_ring_num_files) ? capture_opts.ring_num_files : 0);
108     } else {
109       /* Try to open/create the specified file for use as a capture buffer. */
110       cfile.save_file_fd = open(capfile_name, O_RDWR|O_BINARY|O_TRUNC|O_CREAT,
111                                 0600);
112     }
113     is_tempfile = FALSE;
114   } else {
115     /* Choose a random name for the capture buffer */
116     cfile.save_file_fd = create_tempfile(tmpname, sizeof tmpname, "ether");
117     capfile_name = g_strdup(tmpname);
118     is_tempfile = TRUE;
119   }
120   if (cfile.save_file_fd == -1) {
121     if (is_tempfile) {
122       simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
123         "The temporary file to which the capture would be saved (\"%s\")"
124         "could not be opened: %s.", capfile_name, strerror(errno));
125     } else {
126       if (capture_opts.multi_files_on) {
127         ringbuf_error_cleanup();
128       }
129       open_failure_alert_box(capfile_name, errno, TRUE);
130     }
131     g_free(capfile_name);
132     return FALSE;
133   }
134   cf_close(&cfile);
135   g_assert(cfile.save_file == NULL);
136   cfile.save_file = capfile_name;
137   /* cfile.save_file is "g_free"ed below, which is equivalent to
138      "g_free(capfile_name)". */
139
140   if (capture_opts.sync_mode) { 
141     /* sync mode: do the capture in a child process */
142     ret = sync_pipe_do_capture(is_tempfile);
143     /* capture is still running */
144     set_main_window_name("(Live Capture in Progress) - Ethereal");
145   } else {
146     /* normal mode: do the capture synchronously */
147     set_main_window_name("(Live Capture in Progress) - Ethereal");
148     ret = normal_do_capture(is_tempfile);
149     /* capture is finished here */
150   }
151
152   return ret;
153 }
154
155
156 /* start a normal capture session */
157 static gboolean
158 normal_do_capture(gboolean is_tempfile)
159 {
160     int capture_succeeded;
161     gboolean stats_known;
162     struct pcap_stat stats;
163     int err;
164
165     /* Not sync mode. */
166     capture_succeeded = capture_start(&stats_known, &stats);
167     if (capture_opts.quit_after_cap) {
168       /* DON'T unlink the save file.  Presumably someone wants it. */
169         main_window_exit();
170     }
171     if (!capture_succeeded) {
172       /* We didn't succeed in doing the capture, so we don't have a save
173          file. */
174       if (capture_opts.multi_files_on) {
175         ringbuf_free();
176       } else {
177         g_free(cfile.save_file);
178       }
179       cfile.save_file = NULL;
180       return FALSE;
181     }
182     /* Capture succeeded; attempt to read in the capture file. */
183     if ((err = cf_open(cfile.save_file, is_tempfile, &cfile)) != 0) {
184       /* We're not doing a capture any more, so we don't have a save
185          file. */
186       if (capture_opts.multi_files_on) {
187         ringbuf_free();
188       } else {
189         g_free(cfile.save_file);
190       }
191       cfile.save_file = NULL;
192       return FALSE;
193     }
194
195     /* Set the read filter to NULL. */
196     cfile.rfcode = NULL;
197
198     /* Get the packet-drop statistics.
199
200        XXX - there are currently no packet-drop statistics stored
201        in libpcap captures, and that's what we're reading.
202
203        At some point, we will add support in Wiretap to return
204        packet-drop statistics for capture file formats that store it,
205        and will make "cf_read()" get those statistics from Wiretap.
206        We clear the statistics (marking them as "not known") in
207        "cf_open()", and "cf_read()" will only fetch them and mark
208        them as known if Wiretap supplies them, so if we get the
209        statistics now, after calling "cf_open()" but before calling
210        "cf_read()", the values we store will be used by "cf_read()".
211
212        If a future libpcap capture file format stores the statistics,
213        we'll put them into the capture file that we write, and will
214        thus not have to set them here - "cf_read()" will get them from
215        the file and use them. */
216     if (stats_known) {
217       cfile.drops_known = TRUE;
218
219       /* XXX - on some systems, libpcap doesn't bother filling in
220          "ps_ifdrop" - it doesn't even set it to zero - so we don't
221          bother looking at it.
222
223          Ideally, libpcap would have an interface that gave us
224          several statistics - perhaps including various interface
225          error statistics - and would tell us which of them it
226          supplies, allowing us to display only the ones it does. */
227       cfile.drops = stats.ps_drop;
228     }
229     switch (cf_read(&cfile)) {
230
231     case READ_SUCCESS:
232     case READ_ERROR:
233       /* Just because we got an error, that doesn't mean we were unable
234          to read any of the file; we handle what we could get from the
235          file. */
236       break;
237
238     case READ_ABORTED:
239       /* Exit by leaving the main loop, so that any quit functions
240          we registered get called. */
241       main_window_nested_quit();
242       return FALSE;
243     }
244
245     /* We're not doing a capture any more, so we don't have a save
246        file. */
247     if (capture_opts.multi_files_on) {
248       ringbuf_free();
249     } else {
250       g_free(cfile.save_file);
251     }
252     cfile.save_file = NULL;
253
254     /* if we didn't captured even a single packet, close the file again */
255     if(cfile.count == 0) {
256       simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, 
257       "%sNo packets captured!%s\n\n"
258       "As no data was captured, closing the %scapture file!",
259       simple_dialog_primary_start(), simple_dialog_primary_end(),
260       (cfile.is_tempfile) ? "temporary " : "");
261       cf_close(&cfile);
262     }
263   return TRUE;
264 }
265
266
267 static void
268 stop_capture_signal_handler(int signo _U_)
269 {
270   capture_loop_stop();
271 }
272
273
274 int  
275 capture_start(gboolean *stats_known, struct pcap_stat *stats)
276 {
277 #ifndef _WIN32
278   /*
279    * Catch SIGUSR1, so that we exit cleanly if the parent process
280    * kills us with it due to the user selecting "Capture->Stop".
281    */
282   if (capture_child)
283     signal(SIGUSR1, stop_capture_signal_handler);
284 #endif
285
286   return capture_loop_start(stats_known, stats);
287 }
288
289 void
290 capture_stop(void)
291 {
292
293   if (capture_opts.sync_mode) { 
294     sync_pipe_stop();
295   }
296     
297   capture_loop_stop();
298 }
299
300 void
301 kill_capture_child(void)
302 {
303   if (capture_opts.sync_mode) { 
304     sync_pipe_kill();
305   }
306 }
307
308
309 #endif /* HAVE_LIBPCAP */