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