Fix for bug 1195.
[obnox/wireshark/wip.git] / capture_loop.h
index c0a91e2e24c9be6875831892c23fd931a6c8402b..907e0c6ff3b2c54f872078ad99c7f4da64bea60b 100644 (file)
@@ -3,8 +3,8 @@
  *
  * $Id$
  *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
  * This program is free software; you can redistribute it and/or
@@ -24,7 +24,7 @@
 
 
 /** @file
- *  
+ *
  *  Do the low-level work of a capture.
  *
  */
 #ifndef __CAPTURE_LOOP_H__
 #define __CAPTURE_LOOP_H__
 
-#ifndef _WIN32
 /*
  * Get information about libpcap format from "wiretap/libpcap.h".
  * XXX - can we just use pcap_open_offline() to read the pipe?
  */
 #include "wiretap/libpcap.h"
-#endif
 
 /** Do the low-level work of a capture.
  *  Returns TRUE if it succeeds, FALSE otherwise. */
@@ -51,35 +49,56 @@ extern void capture_loop_stop(void);
 /*** the following is internal only (should be moved to capture_loop_int.h) ***/
 
 
+#ifndef HAVE_PCAP_BREAKLOOP
 /*
- * We don't want to do a "select()" on the pcap_t's file descriptor on
- * BSD (because "select()" doesn't work correctly on BPF devices on at
- * least some releases of some flavors of BSD), and we don't want to do
- * it on Windows (because "select()" is something for sockets, not for
- * arbitrary handles).  (Note that "Windows" here includes Cygwin;
- * even in its pretend-it's-UNIX environment, we're using WinPcap, not
- * a UNIX libpcap.)
+ * We don't have pcap_breakloop(), which is the only way to ensure that
+ * pcap_dispatch(), pcap_loop(), or even pcap_next() or pcap_next_ex()
+ * won't, if the call to read the next packet or batch of packets is
+ * is interrupted by a signal on UN*X, just go back and try again to
+ * read again.
+ *
+ * On UN*X, we catch SIGUSR1 as a "stop capturing" signal, and, in
+ * the signal handler, set a flag to stop capturing; however, without
+ * a guarantee of that sort, we can't guarantee that we'll stop capturing
+ * if the read will be retried and won't time out if no packets arrive.
+ *
+ * Therefore, on at least some platforms, we work around the lack of
+ * pcap_breakloop() by doing a select() on the pcap_t's file descriptor
+ * to wait for packets to arrive, so that we're probably going to be
+ * blocked in the select() when the signal arrives, and can just bail
+ * out of the loop at that point.
+ *
+ * However, we don't want to that on BSD (because "select()" doesn't work
+ * correctly on BPF devices on at least some releases of some flavors of
+ * BSD), and we don't want to do it on Windows (because "select()" is
+ * something for sockets, not for arbitrary handles).  (Note that "Windows"
+ * here includes Cygwin; even in its pretend-it's-UNIX environment, we're
+ * using WinPcap, not a UNIX libpcap.)
+ *
+ * Fortunately, we don't need to do it on BSD, because the libpcap timeout
+ * on BSD times out even if no packets have arrived, so we'll eventually
+ * exit pcap_dispatch() with an indication that no packets have arrived,
+ * and will break out of the capture loop at that point.
  *
- * We *do* want to do it on other platforms, as, on other platforms (with
- * the possible exception of Ultrix and Digital UNIX), the read timeout
- * doesn't expire if no packets have arrived, so a "pcap_dispatch()" call
- * will block until packets arrive, causing the UI to hang.
+ * On Windows, we can't send a SIGUSR1 to stop capturing, so none of this
+ * applies in any case.
  *
  * XXX - the various BSDs appear to define BSD in <sys/param.h>; we don't
  * want to include it if it's not present on this platform, however.
  */
-#if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && \
+# if !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && \
     !defined(__bsdi__) && !defined(__APPLE__) && !defined(_WIN32) && \
     !defined(__CYGWIN__)
-# define MUST_DO_SELECT
-#endif
+#  define MUST_DO_SELECT
+# endif /* avoid select */
+#endif /* HAVE_PCAP_BREAKLOOP */
 
 typedef void (*capture_packet_cb_fct)(u_char *, const struct pcap_pkthdr *, const u_char *);
 
 
-/* moved from capture_loop.c here, so we can combine it (and the related functions) with tethereal */
+/* moved from capture_loop.c here, so we can combine it (and the related functions) with tshark */
 /* XXX - should be moved back to capture_loop.c */
-/* E: capture_loop.c only (Ethereal/dumpcap) T: tethereal only */
+/* E: capture_loop.c only (Wireshark/dumpcap) T: tshark only */
 typedef struct _loop_data {
   /* common */
   gboolean       go;                    /* TRUE as long as we're supposed to keep capturing */
@@ -101,7 +120,6 @@ typedef struct _loop_data {
 
   /* capture pipe (unix only "input file") */
   gboolean       from_cap_pipe;         /* TRUE if we are capturing data from a capture pipe */
-#ifndef _WIN32
   struct pcap_hdr cap_pipe_hdr;         /* ? */
   struct pcaprec_modified_hdr cap_pipe_rechdr;  /* ? */
   int            cap_pipe_fd;           /* the file descriptor of the capture pipe */
@@ -116,7 +134,6 @@ typedef struct _loop_data {
          STATE_READ_DATA
        } cap_pipe_state;
   enum { PIPOK, PIPEOF, PIPERR, PIPNEXIST } cap_pipe_err;
-#endif
 
   /* output file */
   FILE          *pdh;
@@ -138,14 +155,11 @@ typedef enum {
 extern initfilter_status_t
 capture_loop_init_filter(pcap_t *pcap_h, gboolean from_cap_pipe, const gchar * iface, gchar * cfilter);
 
-#ifdef HAVE_LIBPCAP
-#ifndef _WIN32
-extern int 
-cap_pipe_dispatch(loop_data *, guchar *, char *, int);
-#endif /* _WIN32 */
-#endif
+int
+capture_loop_dispatch(capture_options *capture_opts _U_, loop_data *ld,
+                     char *errmsg, int errmsg_len);
 
-extern gboolean 
+extern gboolean
 capture_loop_open_input(capture_options *capture_opts, loop_data *ld,
                         char *errmsg, size_t errmsg_len,
                         char *secondary_errmsg, size_t secondary_errmsg_len);
@@ -156,7 +170,7 @@ capture_loop_open_output(capture_options *capture_opts, int *save_file_fd, char
 extern gboolean
 capture_loop_init_output(capture_options *capture_opts, int save_file_fd, loop_data *ld, char *errmsg, int errmsg_len);
 
-extern gboolean 
+extern gboolean
 capture_loop_close_output(capture_options *capture_opts, loop_data *ld, int *err_close);
 
 /*
@@ -176,7 +190,7 @@ extern void
 report_packet_drops(int drops);
 
 /** Report an error in the capture. */
-extern void 
+extern void
 report_capture_error(const char *error_msg, const char *secondary_error_msg);
 
 /** Report an error with a capture filter. */