fix Solaris build (I've removed O_BINARY)
[metze/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 static void stop_capture_signal_handler(int signo);
71
72
73
74 /* start a capture */
75 /* Returns TRUE if the capture starts successfully, FALSE otherwise. */
76 gboolean
77 do_capture(capture_options *capture_opts)
78 {
79   gboolean ret;
80
81
82   /* close the currently loaded capture file */
83   cf_close(capture_opts->cf);
84
85   /* try to start the capture child process */
86   ret = sync_pipe_do_capture(capture_opts, capture_opts->save_file == NULL);
87
88   if(ret) {
89       /* tell callbacks (menu, ...) that capture is running now */
90       cf_callback_invoke(cf_cb_live_capture_prepare, capture_opts);
91   } else {
92       if(capture_opts->save_file != NULL) {
93           g_free(capture_opts->save_file);
94           capture_opts->save_file = NULL;
95       }
96   }
97
98   return ret;
99 }
100
101
102 /* we've succeeded a capture, try to read it into a new capture file */
103 static gboolean
104 capture_input_read_all(capture_options *capture_opts, gboolean is_tempfile, gboolean drops_known,
105 guint32 drops)
106 {
107     int err;
108
109
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
113          file. */
114       return FALSE;
115     }
116
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);
120
121     /* Get the packet-drop statistics.
122
123        XXX - there are currently no packet-drop statistics stored
124        in libpcap captures, and that's what we're reading.
125
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()".
134
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. */
139     if (drops_known) {
140       cf_set_drops_known(capture_opts->cf, TRUE);
141
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.
145
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);
151     }
152     switch (cf_read(capture_opts->cf)) {
153
154     case CF_READ_OK:
155     case CF_READ_ERROR:
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
158          file. */
159       break;
160
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();
165       return FALSE;
166     }
167
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);
176     }
177   return TRUE;
178 }
179
180
181 /* capture child tells us, we have a new (or the first) capture file */
182 gboolean
183 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
184 {
185   gboolean is_tempfile;
186   int  err;
187
188
189       /*g_warning("New capture file: %s", new_file);*/
190
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);
198         is_tempfile = FALSE;
199       } else {
200         /* we didn't had a save_file before, must be a tempfile */
201         is_tempfile = TRUE;
202         cf_set_tempfile(capture_opts->cf, TRUE);
203       }
204       capture_opts->save_file = g_strdup(new_file);
205
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)) {
211         case CF_OK:
212             break;
213         case CF_ERROR:
214             /* Don't unlink the save file - leave it around, for debugging
215             purposes. */
216             g_free(capture_opts->save_file);
217             capture_opts->save_file = NULL;
218             return FALSE;
219             break;
220         }
221     }
222
223     return TRUE;
224 }
225
226     
227 /* capture child tells us, we have new packets to read */
228 void
229 capture_input_new_packets(capture_options *capture_opts, int to_read)
230 {
231   int  err;
232
233
234   if(capture_opts->real_time_mode) {
235       /* Read from the capture file the number of records the child told us
236          it added.
237          XXX - do something if this fails? */
238       switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
239
240       case CF_READ_OK:
241       case CF_READ_ERROR:
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
244            file.
245
246            XXX - abort on a read error? */
247         break;
248
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);
253         break;
254       }
255   }
256 }
257
258
259 /* capture child closed it's side ot the pipe, do the required cleanup */
260 void
261 capture_input_closed(capture_options *capture_opts)
262 {
263     int  err;
264
265
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)) {
270
271         case CF_READ_OK:
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);
279             }
280             break;
281         case CF_READ_ERROR:
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
284              file. */
285           break;
286
287         case CF_READ_ABORTED:
288           /* Exit by leaving the main loop, so that any quit functions
289              we registered get called. */
290           main_window_quit();
291         }
292     } else {
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));
296     }
297
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;
302 }
303
304
305 #ifndef _WIN32
306 static void
307 capture_child_stop_signal_handler(int signo _U_)
308 {
309   capture_loop_stop();
310 }
311 #endif
312
313
314 int  
315 capture_child_start(capture_options *capture_opts, gboolean *stats_known, struct pcap_stat *stats)
316 {
317 #ifndef _WIN32
318   /*
319    * Catch SIGUSR1, so that we exit cleanly if the parent process
320    * kills us with it due to the user selecting "Capture->Stop".
321    */
322     signal(SIGUSR1, capture_child_stop_signal_handler);
323 #endif
324
325   return capture_loop_start(capture_opts, stats_known, stats);
326 }
327
328 void
329 capture_child_stop(capture_options *capture_opts)
330 {
331   /* stop the capture loop */
332   capture_loop_stop();
333 }
334
335 void
336 capture_stop(capture_options *capture_opts)
337 {
338   /* stop the capture child, if we have one */
339   sync_pipe_stop(capture_opts);
340 }
341
342 void
343 capture_kill_child(capture_options *capture_opts)
344 {
345   /* kill the capture child, if we have one */
346   sync_pipe_kill(capture_opts);
347 }
348
349
350 #endif /* HAVE_LIBPCAP */