capture engine:
[metze/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_start(capture_opts, capture_opts->save_file == NULL);
87   if(!ret) {
88       if(capture_opts->save_file != NULL) {
89           g_free(capture_opts->save_file);
90           capture_opts->save_file = NULL;
91       }
92   }
93
94   return ret;
95 }
96
97
98 void
99 capture_stop(capture_options *capture_opts)
100 {
101   /* stop the capture child gracefully */
102   sync_pipe_stop(capture_opts);
103 }
104
105
106 void
107 capture_clear(capture_options *capture_opts)
108 {
109     capture_opts->restart = TRUE;
110     capture_stop(capture_opts);
111 }
112
113
114 void
115 capture_kill_child(capture_options *capture_opts)
116 {
117   /* kill the capture child */
118   sync_pipe_kill(capture_opts);
119 }
120
121
122
123 /* We've succeeded a (non real-time) capture, try to read it into a new capture file */
124 static gboolean
125 capture_input_read_all(capture_options *capture_opts, gboolean is_tempfile, gboolean drops_known,
126 guint32 drops)
127 {
128   int err;
129
130
131   /* Capture succeeded; attempt to open the capture file. */
132   if (cf_open(capture_opts->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
133     /* We're not doing a capture any more, so we don't have a save
134        file. */
135     return FALSE;
136   }
137
138   /* Set the read filter to NULL. */
139   /* XXX - this is odd here, try to put it somewhere, where it fits better */
140   cf_set_rfcode(capture_opts->cf, NULL);
141
142   /* Get the packet-drop statistics.
143
144      XXX - there are currently no packet-drop statistics stored
145      in libpcap captures, and that's what we're reading.
146
147      At some point, we will add support in Wiretap to return
148      packet-drop statistics for capture file formats that store it,
149      and will make "cf_read()" get those statistics from Wiretap.
150      We clear the statistics (marking them as "not known") in
151      "cf_open()", and "cf_read()" will only fetch them and mark
152      them as known if Wiretap supplies them, so if we get the
153      statistics now, after calling "cf_open()" but before calling
154      "cf_read()", the values we store will be used by "cf_read()".
155
156      If a future libpcap capture file format stores the statistics,
157      we'll put them into the capture file that we write, and will
158      thus not have to set them here - "cf_read()" will get them from
159      the file and use them. */
160   if (drops_known) {
161     cf_set_drops_known(capture_opts->cf, TRUE);
162
163     /* XXX - on some systems, libpcap doesn't bother filling in
164        "ps_ifdrop" - it doesn't even set it to zero - so we don't
165        bother looking at it.
166
167        Ideally, libpcap would have an interface that gave us
168        several statistics - perhaps including various interface
169        error statistics - and would tell us which of them it
170        supplies, allowing us to display only the ones it does. */
171     cf_set_drops(capture_opts->cf, drops);
172   }
173
174   /* read in the packet data */
175   switch (cf_read(capture_opts->cf)) {
176
177   case CF_READ_OK:
178   case CF_READ_ERROR:
179     /* Just because we got an error, that doesn't mean we were unable
180        to read any of the file; we handle what we could get from the
181        file. */
182     break;
183
184   case CF_READ_ABORTED:
185     /* User wants to quit program. Exit by leaving the main loop, 
186        so that any quit functions we registered get called. */
187     main_window_nested_quit();
188     return FALSE;
189   }
190
191   /* if we didn't captured even a single packet, close the file again */
192   if(cf_packet_count(capture_opts->cf) == 0) {
193     simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, 
194     "%sNo packets captured!%s\n\n"
195     "As no data was captured, closing the %scapture file!",
196     simple_dialog_primary_start(), simple_dialog_primary_end(),
197     (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
198     cf_close(capture_opts->cf);
199   }
200   return TRUE;
201 }
202
203
204 /* capture child tells us, we have a new (or the first) capture file */
205 gboolean
206 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
207 {
208   gboolean is_tempfile;
209   int  err;
210
211
212   /*g_warning("New capture file: %s", new_file);*/
213
214   /* free the old filename */
215   if(capture_opts->save_file != NULL) {
216     /* we start a new capture file, close the old one (if we had one before) */
217     if( ((capture_file *) capture_opts->cf)->state != FILE_CLOSED) {
218         cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
219         cf_finish_tail(capture_opts->cf, &err);
220         cf_close(capture_opts->cf);
221     }
222     g_free(capture_opts->save_file);
223     is_tempfile = FALSE;
224     cf_set_tempfile(capture_opts->cf, FALSE);
225   } else {
226     /* we didn't had a save_file before, must be a tempfile */
227     is_tempfile = TRUE;
228     cf_set_tempfile(capture_opts->cf, TRUE);
229   }
230
231   /* save the new filename */
232   capture_opts->save_file = g_strdup(new_file);
233
234   /* if we are in real-time mode, open the new file now */
235   if(capture_opts->real_time_mode) {
236     /* Attempt to open the capture file and set up to read from it. */
237        switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
238     case CF_OK:
239       break;
240     case CF_ERROR:
241       /* Don't unlink (delete) the save file - leave it around, 
242          for debugging purposes. */
243       g_free(capture_opts->save_file);
244       capture_opts->save_file = NULL;
245       return FALSE;
246       break;
247     }
248
249     cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
250   } else {
251     cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
252   }
253
254
255   return TRUE;
256 }
257
258     
259 /* capture child tells us, we have new packets to read */
260 void
261 capture_input_new_packets(capture_options *capture_opts, int to_read)
262 {
263   int  err;
264
265
266   if(capture_opts->real_time_mode) {
267     /* Read from the capture file the number of records the child told us
268        it added.
269        XXX - do something if this fails? */
270     switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
271
272     case CF_READ_OK:
273     case CF_READ_ERROR:
274       /* Just because we got an error, that doesn't mean we were unable
275          to read any of the file; we handle what we could get from the
276          file.
277
278          XXX - abort on a read error? */
279          main_window_update();
280       break;
281
282     case CF_READ_ABORTED:
283       /* Kill the child capture process; the user wants to exit, and we
284          shouldn't just leave it running. */
285       capture_kill_child(capture_opts);
286       break;
287     }
288   }
289 }
290
291
292 /* capture child closed it's side ot the pipe, do the required cleanup */
293 void
294 capture_input_closed(capture_options *capture_opts)
295 {
296     int  err;
297
298
299     if(capture_opts->real_time_mode) {
300         /* first of all, we are not doing a capture any more */
301         cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
302
303         /* Read what remains of the capture file, and finish the capture.
304            XXX - do something if this fails? */
305         switch (cf_finish_tail(capture_opts->cf, &err)) {
306
307         case CF_READ_OK:
308             if(cf_packet_count(capture_opts->cf) == 0) {
309               simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, 
310               "%sNo packets captured!%s\n\n"
311               "As no data was captured, closing the %scapture file!",
312               simple_dialog_primary_start(), simple_dialog_primary_end(),
313               cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
314               cf_close(capture_opts->cf);
315             }
316             break;
317         case CF_READ_ERROR:
318           /* Just because we got an error, that doesn't mean we were unable
319              to read any of the file; we handle what we could get from the
320              file. */
321           break;
322
323         case CF_READ_ABORTED:
324           /* Exit by leaving the main loop, so that any quit functions
325              we registered get called. */
326           main_window_quit();
327         }
328
329     } else {
330         /* first of all, we are not doing a capture any more */
331         cf_callback_invoke(cf_cb_live_capture_fixed_finished, capture_opts->cf);
332
333         /* this is a normal mode capture, read in the capture file data */
334         capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf), 
335             cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
336     }
337
338     /* does the user wants to restart the current capture? */
339     if(capture_opts->restart) {
340         capture_opts->restart = FALSE;
341
342         unlink(capture_opts->save_file);
343
344         /* if it was a tempfile, throw away the old filename (so it will become a tempfile again) */
345         if(cf_is_tempfile(capture_opts->cf)) {
346             g_free(capture_opts->save_file);
347             capture_opts->save_file = NULL;
348         }
349
350         /* ... and start the capture again */
351         capture_start(capture_opts);
352     } else {
353         /* We're not doing a capture any more, so we don't have a save file. */
354         g_free(capture_opts->save_file);
355         capture_opts->save_file = NULL;
356     }
357 }
358
359
360 #endif /* HAVE_LIBPCAP */