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