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