fix statusbar messages by splitting into update and fixed messages between capture...
[obnox/wireshark/wip.git] / capture.c
1 /* capture.c
2  * Routines for packet capture
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
71 /** 
72  * Start a capture.
73  *
74  * @return TRUE if the capture starts successfully, FALSE otherwise.
75  */
76 gboolean
77 capture_start(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 (non real-time) 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 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
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
153   /* read in the packet data */
154   switch (cf_read(capture_opts->cf)) {
155
156   case CF_READ_OK:
157   case CF_READ_ERROR:
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
160        file. */
161     break;
162
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();
167     return FALSE;
168   }
169
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);
178   }
179   return TRUE;
180 }
181
182
183 /* capture child tells us, we have a new (or the first) capture file */
184 gboolean
185 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
186 {
187   gboolean is_tempfile;
188   int  err;
189
190
191   /*g_warning("New capture file: %s", new_file);*/
192
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);
200     is_tempfile = FALSE;
201   } else {
202     /* we didn't had a save_file before, must be a tempfile */
203     is_tempfile = TRUE;
204     cf_set_tempfile(capture_opts->cf, TRUE);
205   }
206
207   /* save the new filename */
208   capture_opts->save_file = g_strdup(new_file);
209
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)) {
214     case CF_OK:
215       break;
216     case CF_ERROR:
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;
221       return FALSE;
222       break;
223     }
224
225     cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts->cf);
226   } else {
227     cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts->cf);
228   }
229
230
231   return TRUE;
232 }
233
234     
235 /* capture child tells us, we have new packets to read */
236 void
237 capture_input_new_packets(capture_options *capture_opts, int to_read)
238 {
239   int  err;
240
241
242   if(capture_opts->real_time_mode) {
243     /* Read from the capture file the number of records the child told us
244        it added.
245        XXX - do something if this fails? */
246     switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
247
248     case CF_READ_OK:
249     case CF_READ_ERROR:
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
252          file.
253
254          XXX - abort on a read error? */
255       break;
256
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);
261       break;
262     }
263   }
264 }
265
266
267 /* capture child closed it's side ot the pipe, do the required cleanup */
268 void
269 capture_input_closed(capture_options *capture_opts)
270 {
271     int  err;
272
273
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);
277
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)) {
281
282         case CF_READ_OK:
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);
290             }
291             break;
292         case CF_READ_ERROR:
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
295              file. */
296           break;
297
298         case CF_READ_ABORTED:
299           /* Exit by leaving the main loop, so that any quit functions
300              we registered get called. */
301           main_window_quit();
302         }
303
304     } else {
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);
307
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));
311     }
312
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;
317 }
318
319
320 void
321 capture_stop(capture_options *capture_opts)
322 {
323   /* stop the capture child gracefully, if we have one */
324   sync_pipe_stop(capture_opts);
325 }
326
327 void
328 capture_kill_child(capture_options *capture_opts)
329 {
330   /* kill the capture child, if we have one */
331   sync_pipe_kill(capture_opts);
332 }
333
334
335 #endif /* HAVE_LIBPCAP */