add some more help text to the "no packets captured" error dialog:
[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 #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 #include "file_util.h"
69 #include "log.h"
70
71
72
73 /** 
74  * Start a capture.
75  *
76  * @return TRUE if the capture starts successfully, FALSE otherwise.
77  */
78 gboolean
79 capture_start(capture_options *capture_opts)
80 {
81   gboolean ret;
82
83
84   /* close the currently loaded capture file */
85   cf_close(capture_opts->cf);
86
87   g_assert(capture_opts->state == CAPTURE_STOPPED);
88   capture_opts->state = CAPTURE_PREPARING;
89
90   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Start ...");
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   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Stop ...");
118
119   cf_callback_invoke(cf_cb_live_capture_stopping, capture_opts);
120
121   /* stop the capture child gracefully */
122   sync_pipe_stop(capture_opts);
123 }
124
125
126 void
127 capture_restart(capture_options *capture_opts)
128 {
129     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture Restart");
130
131     capture_opts->restart = TRUE;
132     capture_stop(capture_opts);
133 }
134
135
136 void
137 capture_kill_child(capture_options *capture_opts)
138 {
139   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_INFO, "Capture Kill");
140
141   /* kill the capture child */
142   sync_pipe_kill(capture_opts);
143 }
144
145
146
147 /* We've succeeded a (non real-time) capture, try to read it into a new capture file */
148 static gboolean
149 capture_input_read_all(capture_options *capture_opts, gboolean is_tempfile, gboolean drops_known,
150 guint32 drops)
151 {
152   int err;
153
154
155   /* Capture succeeded; attempt to open the capture file. */
156   if (cf_open(capture_opts->cf, capture_opts->save_file, is_tempfile, &err) != CF_OK) {
157     /* We're not doing a capture any more, so we don't have a save
158        file. */
159     return FALSE;
160   }
161
162   /* Set the read filter to NULL. */
163   /* XXX - this is odd here, try to put it somewhere, where it fits better */
164   cf_set_rfcode(capture_opts->cf, NULL);
165
166   /* Get the packet-drop statistics.
167
168      XXX - there are currently no packet-drop statistics stored
169      in libpcap captures, and that's what we're reading.
170
171      At some point, we will add support in Wiretap to return
172      packet-drop statistics for capture file formats that store it,
173      and will make "cf_read()" get those statistics from Wiretap.
174      We clear the statistics (marking them as "not known") in
175      "cf_open()", and "cf_read()" will only fetch them and mark
176      them as known if Wiretap supplies them, so if we get the
177      statistics now, after calling "cf_open()" but before calling
178      "cf_read()", the values we store will be used by "cf_read()".
179
180      If a future libpcap capture file format stores the statistics,
181      we'll put them into the capture file that we write, and will
182      thus not have to set them here - "cf_read()" will get them from
183      the file and use them. */
184   if (drops_known) {
185     cf_set_drops_known(capture_opts->cf, TRUE);
186
187     /* XXX - on some systems, libpcap doesn't bother filling in
188        "ps_ifdrop" - it doesn't even set it to zero - so we don't
189        bother looking at it.
190
191        Ideally, libpcap would have an interface that gave us
192        several statistics - perhaps including various interface
193        error statistics - and would tell us which of them it
194        supplies, allowing us to display only the ones it does. */
195     cf_set_drops(capture_opts->cf, drops);
196   }
197
198   /* read in the packet data */
199   switch (cf_read(capture_opts->cf)) {
200
201   case CF_READ_OK:
202   case CF_READ_ERROR:
203     /* Just because we got an error, that doesn't mean we were unable
204        to read any of the file; we handle what we could get from the
205        file. */
206     break;
207
208   case CF_READ_ABORTED:
209     /* User wants to quit program. Exit by leaving the main loop, 
210        so that any quit functions we registered get called. */
211     main_window_nested_quit();
212     return FALSE;
213   }
214
215   /* if we didn't captured even a single packet, close the file again */
216   if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
217     simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, 
218 "%sNo packets captured!%s\n"
219 "\n"
220 "As no data was captured, closing the %scapture file!\n"
221 "\n"
222 "\n"
223 "Help about capturing can be found at:\n"
224 "\n"
225 "       http://wiki.ethereal.com/CaptureSetup"
226 #ifdef _WIN32
227 "\n\n"
228 "Wireless (Wi-Fi/WLAN):\n"
229 "Try to switch off promiscuous mode in the Capture Options!"
230 #endif
231 "",
232     simple_dialog_primary_start(), simple_dialog_primary_end(),
233     (cf_is_tempfile(capture_opts->cf)) ? "temporary " : "");
234     cf_close(capture_opts->cf);
235   }
236   return TRUE;
237 }
238
239
240 /* capture child tells us, we have a new (or the first) capture file */
241 gboolean
242 capture_input_new_file(capture_options *capture_opts, gchar *new_file)
243 {
244   gboolean is_tempfile;
245   int  err;
246
247
248   if(capture_opts->state == CAPTURE_PREPARING) {
249     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started!");
250   }
251   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "File: \"%s\"", new_file);
252
253   g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
254
255   /* free the old filename */
256   if(capture_opts->save_file != NULL) {
257     /* we start a new capture file, close the old one (if we had one before) */
258     if( ((capture_file *) capture_opts->cf)->state != FILE_CLOSED) {
259         cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
260         cf_finish_tail(capture_opts->cf, &err);
261         cf_close(capture_opts->cf);
262     }
263     g_free(capture_opts->save_file);
264     is_tempfile = FALSE;
265     cf_set_tempfile(capture_opts->cf, FALSE);
266   } else {
267     /* we didn't had a save_file before, must be a tempfile */
268     is_tempfile = TRUE;
269     cf_set_tempfile(capture_opts->cf, TRUE);
270   }
271
272   /* save the new filename */
273   capture_opts->save_file = g_strdup(new_file);
274
275   /* if we are in real-time mode, open the new file now */
276   if(capture_opts->real_time_mode) {
277     /* Attempt to open the capture file and set up to read from it. */
278        switch(cf_start_tail(capture_opts->cf, capture_opts->save_file, is_tempfile, &err)) {
279     case CF_OK:
280       break;
281     case CF_ERROR:
282       /* Don't unlink (delete) the save file - leave it around, 
283          for debugging purposes. */
284       g_free(capture_opts->save_file);
285       capture_opts->save_file = NULL;
286       return FALSE;
287       break;
288     }
289
290     cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
291   } else {
292     cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
293   }
294
295   capture_opts->state = CAPTURE_RUNNING;
296
297   return TRUE;
298 }
299
300     
301 /* capture child tells us, we have new packets to read */
302 void
303 capture_input_new_packets(capture_options *capture_opts, int to_read)
304 {
305   int  err;
306
307
308   g_assert(capture_opts->save_file);
309
310   if(capture_opts->real_time_mode) {
311     /* Read from the capture file the number of records the child told us
312        it added.
313        XXX - do something if this fails? */
314     switch (cf_continue_tail(capture_opts->cf, to_read, &err)) {
315
316     case CF_READ_OK:
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
322          XXX - abort on a read error? */
323          cf_callback_invoke(cf_cb_live_capture_update_continue, capture_opts->cf);
324                  /* update the main window, so we get events (e.g. from the stop toolbar button) */
325          main_window_update();
326       break;
327
328     case CF_READ_ABORTED:
329       /* Kill the child capture process; the user wants to exit, and we
330          shouldn't just leave it running. */
331       capture_kill_child(capture_opts);
332       break;
333     }
334   }
335 }
336
337
338 /* capture child closed it's side ot the pipe, do the required cleanup */
339 void
340 capture_input_closed(capture_options *capture_opts)
341 {
342     int  err;
343
344
345     g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture stopped!");
346     g_assert(capture_opts->state == CAPTURE_PREPARING || capture_opts->state == CAPTURE_RUNNING);
347
348     /* if we didn't started the capture (happens if an error occured), do a fake start */
349     if(capture_opts->state == CAPTURE_PREPARING) {
350         if(capture_opts->real_time_mode) {
351             cf_callback_invoke(cf_cb_live_capture_update_started, capture_opts);
352         } else {
353             cf_callback_invoke(cf_cb_live_capture_fixed_started, capture_opts);
354         }
355     }
356
357     if(capture_opts->real_time_mode) {
358                 cf_read_status_t status;
359
360         /* Read what remains of the capture file. */
361         status = cf_finish_tail(capture_opts->cf, &err);
362
363         /* Tell the GUI, we are not doing a capture any more.
364                    Must be done after the cf_finish_tail(), so file lengths are displayed 
365                    correct. */
366         cf_callback_invoke(cf_cb_live_capture_update_finished, capture_opts->cf);
367
368         /* Finish the capture. */
369         switch (status) {
370
371         case CF_READ_OK:
372             if(cf_packet_count(capture_opts->cf) == 0 && !capture_opts->restart) {
373                 simple_dialog(ESD_TYPE_INFO, ESD_BTN_OK, 
374 "%sNo packets captured!%s\n"
375 "\n"
376 "As no data was captured, closing the %scapture file!\n"
377 "\n"
378 "\n"
379 "Help about capturing can be found at:\n"
380 "\n"
381 "       http://wiki.ethereal.com/CaptureSetup"
382 #ifdef _WIN32
383 "\n\n"
384 "Wireless (Wi-Fi/WLAN):\n"
385 "Try to switch off promiscuous mode in the Capture Options!"
386 #endif
387 "",
388               simple_dialog_primary_start(), simple_dialog_primary_end(),
389               cf_is_tempfile(capture_opts->cf) ? "temporary " : "");
390               cf_close(capture_opts->cf);
391             }
392             break;
393         case CF_READ_ERROR:
394           /* Just because we got an error, that doesn't mean we were unable
395              to read any of the file; we handle what we could get from the
396              file. */
397           break;
398
399         case CF_READ_ABORTED:
400           /* Exit by leaving the main loop, so that any quit functions
401              we registered get called. */
402           main_window_quit();
403         }
404
405     } else {
406         /* first of all, we are not doing a capture any more */
407         cf_callback_invoke(cf_cb_live_capture_fixed_finished, capture_opts->cf);
408
409         /* this is a normal mode capture and if no error happened, read in the capture file data */
410         if(capture_opts->save_file != NULL) {
411             capture_input_read_all(capture_opts, cf_is_tempfile(capture_opts->cf), 
412                 cf_get_drops_known(capture_opts->cf), cf_get_drops(capture_opts->cf));
413         }
414     }
415
416     capture_opts->state = CAPTURE_STOPPED;
417
418     /* if we couldn't open a capture file, there's nothing more for us to do */
419     if(capture_opts->save_file == NULL) {
420         cf_close(capture_opts->cf);
421         return;
422     }
423
424     /* does the user wants to restart the current capture? */
425     if(capture_opts->restart) {
426         capture_opts->restart = FALSE;
427
428         eth_unlink(capture_opts->save_file);
429
430         /* if it was a tempfile, throw away the old filename (so it will become a tempfile again) */
431         if(cf_is_tempfile(capture_opts->cf)) {
432             g_free(capture_opts->save_file);
433             capture_opts->save_file = NULL;
434         }
435
436         /* ... and start the capture again */
437         capture_start(capture_opts);
438     } else {
439         /* We're not doing a capture any more, so we don't have a save file. */
440         g_free(capture_opts->save_file);
441         capture_opts->save_file = NULL;
442     }
443 }
444
445
446 #endif /* HAVE_LIBPCAP */