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