]> The Tcpdump Group git mirrors - libpcap/commitdiff
Add "getnonblock" and "setnonblock" operations, and set the function
authorguy <guy>
Thu, 20 Nov 2003 02:02:38 +0000 (02:02 +0000)
committerguy <guy>
Thu, 20 Nov 2003 02:02:38 +0000 (02:02 +0000)
pointers appropriately, rather than using #ifdefs and run-time checks.

Get rid of declaration of non-existent "pcap_set_datalink_platform()"
routine.

12 files changed:
pcap-bpf.c
pcap-dag.c
pcap-dlpi.c
pcap-int.h
pcap-linux.c
pcap-nit.c
pcap-pf.c
pcap-snit.c
pcap-snoop.c
pcap-win32.c
pcap.c
savefile.c

index 88505c9f2d2a9ca57e601dd160ccd2566edbcc69..82cc7fdd16cf79619d573d1160747d13752ea53b 100644 (file)
@@ -20,7 +20,7 @@
  */
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.68 2003-11-15 23:24:01 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.69 2003-11-20 02:02:38 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -763,6 +763,8 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
        p->read_op = pcap_read_bpf;
        p->setfilter_op = pcap_setfilter_bpf;
        p->set_datalink_op = pcap_set_datalink_bpf;
+       p->getnonblock_op = pcap_getnonblock_fd;
+       p->setnonblock_op = pcap_setnonblock_fd;
        p->stats_op = pcap_stats_bpf;
        p->close_op = pcap_close_bpf;
 
index fc940dab3a9571665eb83bc052c977fd16d68822..44b989ba2460af85f2992ce74688fb0dc4f272b0 100644 (file)
@@ -29,7 +29,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.12 2003-11-20 01:21:26 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.13 2003-11-20 02:02:38 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -101,6 +101,7 @@ static int dag_setfilter(pcap_t *p, struct bpf_program *fp);
 static int dag_stats(pcap_t *p, struct pcap_stat *ps);
 static int dag_set_datalink(pcap_t *p, int dlt);
 static int dag_get_datalink(pcap_t *p);
+static int dag_setnonblock(pcap_t *p, int nonblock, char *errbuf);
 
 static void delete_pcap_dag(pcap_t *p) {
   pcap_dag_node_t *curr = NULL, *prev = NULL;
@@ -478,6 +479,8 @@ pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, c
   handle->read_op = dag_read;
   handle->setfilter_op = dag_setfilter;
   handle->set_datalink_op = dag_set_datalink;
+  handle->getnonblock_op = pcap_getnonblock_fd;
+  handle->setnonblock_op = dag_setnonblock;
   handle->stats_op = dag_stats;
   handle->close_op = dag_platform_close;
 
@@ -635,6 +638,26 @@ dag_set_datalink(pcap_t *p, int dlt)
        return (0);
 }
 
+static int
+dag_setnonblock(pcap_t *p, int nonblock, char *errbuf)
+{
+       /*
+        * Set non-blocking mode on the FD.
+        * XXX - is that necessary?  If not, don't bother calling it,
+        * and have a "dag_getnonblock()" function that looks at
+        * "p->md.dag_offset_flags".
+        */
+       if (pcap_setnonblock_fd(p, nonblock, errbuf) < 0)
+               return (-1);
+
+       if (nonblock) {
+               p->md.dag_offset_flags |= DAGF_NONBLOCK;
+       } else {
+               p->md.dag_offset_flags &= ~DAGF_NONBLOCK;
+       }
+       return (0);
+}
+               
 static int
 dag_get_datalink(pcap_t *p)
 {
index 3500d3cb8c566b59d9ed6e79b8eb2c074dde1517..4a9eafd4c516162137702e59fc637723e827d167 100644 (file)
@@ -38,7 +38,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.92 2003-11-15 23:24:02 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.93 2003-11-20 02:02:38 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -707,6 +707,8 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
        p->read_op = pcap_read_dlpi;
        p->setfilter_op = install_bpf_program;  /* no kernel filtering */
        p->set_datalink_op = NULL;      /* can't change data link type */
+       p->getnonblock_op = pcap_getnonblock_fd;
+       p->setnonblock_op = pcap_setnonblock_fd;
        p->stats_op = pcap_stats_dlpi;
        p->close_op = pcap_close_dlpi;
 
index 79129387b768c1669a53b28447dac80795ede0f9..fda88958002000becdcbfa00d4aa029395eaebe5 100644 (file)
@@ -30,7 +30,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.56 2003-11-20 01:21:26 guy Exp $ (LBL)
+ * @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.57 2003-11-20 02:02:39 guy Exp $ (LBL)
  */
 
 #ifndef pcap_int_h
@@ -131,6 +131,8 @@ struct pcap {
        int     (*read_op)(pcap_t *, int cnt, pcap_handler, u_char *);
        int     (*setfilter_op)(pcap_t *, struct bpf_program *);
        int     (*set_datalink_op)(pcap_t *, int);
+       int     (*getnonblock_op)(pcap_t *, char *);
+       int     (*setnonblock_op)(pcap_t *, int, char *);
        int     (*stats_op)(pcap_t *, struct pcap_stat *);
        void    (*close_op)(pcap_t *);
 
@@ -235,13 +237,12 @@ int       pcap_read(pcap_t *, int cnt, pcap_handler, u_char *);
 #endif
 
 /*
- * Internal interface for "pcap_set_datalink()".  Attempts to set the
- * link-layer type to the specified type; if that fails, returns -1.
- * (On platforms that don't support setting it at all, this can just
- * return 0 - on those platforms, "pcap_set_datalink()" has already
- * checked whether the DLT_ value is the one the device supports.
+ * Routines that most pcap implementations can use for non-blocking mode.
  */
-int    pcap_set_datalink_platform(pcap_t *, int);
+#ifndef WIN32
+int    pcap_getnonblock_fd(pcap_t *, char *);
+int    pcap_setnonblock_fd(pcap_t *p, int, char *);
+#endif
 
 /*
  * Internal interfaces for "pcap_findalldevs()".
index 9d72313d58b94f8f1158bd62503920c09dca4d16..b59d4ed234f89c4f950fd21359f77aa4620eec0a 100644 (file)
@@ -27,7 +27,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.100 2003-11-18 21:06:50 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.101 2003-11-20 02:02:39 guy Exp $ (LBL)";
 #endif
 
 /*
@@ -400,6 +400,8 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
        handle->read_op = pcap_read_linux;
        handle->setfilter_op = pcap_setfilter_linux;
        handle->set_datalink_op = NULL; /* can't change data link type */
+       handle->getnonblock_op = pcap_getnonblock_fd;
+       handle->setnonblock_op = pcap_setnonblock_fd;
        handle->stats_op = pcap_stats_linux;
        handle->close_op = pcap_close_linux;
 
index 14f3283a0cc96050e897467961783ded5e880401..3f6461048430e3134a2c9ff6449208a00b7c6752 100644 (file)
@@ -20,7 +20,7 @@
  */
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.51 2003-11-15 23:24:03 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.52 2003-11-20 02:02:40 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -283,6 +283,8 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
        p->read_op = pcap_read_nit;
        p->setfilter_op = install_bpf_program;  /* no kernel filtering */
        p->set_datalink_op = NULL;      /* can't change data link type */
+       p->getnonblock_op = pcap_getnonblock_fd;
+       p->setnonblock_op = pcap_setnonblock_fd;
        p->stats_op = pcap_stats_nit;
        p->close_op = pcap_close_nit;
 
index 9c529172d1cc06391eb4c6a9188dc5de86683a12..f2bd1291df50169b43a85293a46af630df1e0ff5 100644 (file)
--- a/pcap-pf.c
+++ b/pcap-pf.c
@@ -24,7 +24,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.80 2003-11-15 23:24:04 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.81 2003-11-20 02:02:40 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -437,6 +437,8 @@ your system may not be properly configured; see the packetfilter(4) man page\n",
        p->read_op = pcap_read_pf;
        p->setfilter_op = pcap_setfilter_pf;
        p->set_datalink_op = NULL;      /* can't change data link type */
+       p->getnonblock_op = pcap_getnonblock_fd;
+       p->setnonblock_op = pcap_setnonblock_fd;
        p->stats_op = pcap_stats_pf;
        p->close_op = pcap_close_pf;
 
index 635aea97c4afa6eb49f6215a0f4aa49fac5fa0f3..4c08b166f54f3dcb36dfee406a3306c3b4a07bef 100644 (file)
@@ -25,7 +25,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.67 2003-11-15 23:24:04 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.68 2003-11-20 02:02:40 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -341,6 +341,8 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
        p->read_op = pcap_read_snit;
        p->setfilter_op = install_bpf_program;  /* no kernel filtering */
        p->set_datalink_op = NULL;      /* can't change data link type */
+       p->getnonblock_op = pcap_getnonblock_fd;
+       p->setnonblock_op = pcap_setnonblock_fd;
        p->stats_op = pcap_stats_snit;
        p->close_op = pcap_close_snit;
 
index 6161774c47904a2543797cffe1f506a3931bf75e..0feaf7e43e70a674057901346697759a6a656642 100644 (file)
@@ -20,7 +20,7 @@
  */
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.47 2003-11-15 23:24:04 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.48 2003-11-20 02:02:40 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -326,6 +326,8 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
        p->read_op = pcap_read_snoop;
        p->setfilter_op = install_bpf_program;  /* no kernel filtering */
        p->set_datalink_op = NULL;      /* can't change data link type */
+       p->getnonblock_op = pcap_getnonblock_fd;
+       p->setnonblock_op = pcap_setnonblock_fd;
        p->stats_op = pcap_stats_snoop;
        p->close_op = pcap_close_snoop;
 
index bf906fb576bf042afd10b85aa39e33409833955a..d44a1da6a7c6ad725f7ce55ba0282c5e4f3fdf6f 100644 (file)
@@ -32,7 +32,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.16 2003-11-15 23:24:05 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.17 2003-11-20 02:02:40 guy Exp $ (LBL)";
 #endif
 
 #include <pcap-int.h>
@@ -44,6 +44,8 @@ int* _errno();
 #endif /* __MINGW32__ */
 
 static int pcap_setfilter_win32(pcap_t *, struct bpf_program *);
+static int pcap_getnonblock_win32(pcap_t *, char *);
+static int pcap_setnonblock_win32(pcap_t *, int, char *);
 
 #define        PcapBufSize 256000      /*dimension of the buffer in the pcap_t structure*/
 #define        SIZE_BUF 1000000
@@ -286,6 +288,8 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
        p->read_op = pcap_read_win32;
        p->setfilter_op = pcap_setfilter_win32;
        p->set_datalink_op = NULL;      /* can't change data link type */
+       p->getnonblock_op = pcap_getnonblock_win32;
+       p->setnonblock_op = pcap_setnonblock_win32;
        p->stats_op = pcap_stats_win32;
        p->close_op = pcap_close_win32;
 
@@ -312,6 +316,44 @@ pcap_setfilter_win32(pcap_t *p, struct bpf_program *fp)
 }
 
 
+static int
+pcap_getnonblock_win32(pcap_t *p, char *errbuf)
+{
+       /*
+        * XXX - if there were a PacketGetReadTimeout() call, we
+        * would use it, and return 1 if the timeout is -1
+        * and 0 otherwise.
+        */
+       return (p->nonblock);
+}
+
+static int
+pcap_setnonblock_win32(pcap_t *p, int nonblock, char *errbuf)
+{
+       int newtimeout;
+
+       if (nonblock) {
+               /*
+                * Set the read timeout to -1 for non-blocking mode.
+                */
+               newtimeout = -1;
+       } else {
+               /*
+                * Restore the timeout set when the device was opened.
+                * (Note that this may be -1, in which case we're not
+                * really leaving non-blocking mode.)
+                */
+               newtimeout = p->timeout;
+       }
+       if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
+               snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
+                   "PacketSetReadTimeout: %s", pcap_win32strerror());
+               return (-1);
+       }
+       p->nonblock = (newtimeout == -1);
+       return (0);
+}
+
 /* Set the driver working mode */
 int 
 pcap_setmode(pcap_t *p, int mode){
diff --git a/pcap.c b/pcap.c
index f9cb99bdcbaf3cf815bcf46b9c37dfc02f63e24e..aa584cbedf29821a54cb7b43598f3980f8f76227 100644 (file)
--- a/pcap.c
+++ b/pcap.c
@@ -33,7 +33,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.67 2003-11-20 01:21:26 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.68 2003-11-20 02:02:41 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -512,26 +512,25 @@ pcap_geterr(pcap_t *p)
        return (p->errbuf);
 }
 
-/*
- * NOTE: in the future, these may need to call platform-dependent routines,
- * e.g. on platforms with memory-mapped packet-capture mechanisms where
- * "pcap_read()" uses "select()" or "poll()" to wait for packets to arrive.
- */
 int
 pcap_getnonblock(pcap_t *p, char *errbuf)
 {
+       return p->getnonblock_op(p, errbuf);
+}
+
+/*
+ * Get the current non-blocking mode setting, under the assumption that
+ * it's just the standard POSIX non-blocking flag.
+ *
+ * We don't look at "p->nonblock", in case somebody tweaked the FD
+ * directly.
+ */
 #ifndef WIN32
+int
+pcap_getnonblock_fd(pcap_t *p, char *errbuf)
+{
        int fdflags;
-#endif
 
-       if (p->sf.rfile != NULL) {
-               /*
-                * This is a savefile, not a live capture file, so
-                * never say it's in non-blocking mode.
-                */
-               return (0);
-       }
-#ifndef WIN32
        fdflags = fcntl(p->fd, F_GETFL, 0);
        if (fdflags == -1) {
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
@@ -542,37 +541,27 @@ pcap_getnonblock(pcap_t *p, char *errbuf)
                return (1);
        else
                return (0);
-#else
-       return (p->nonblock);
-#endif
 }
+#endif
 
 int
 pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
 {
+       return p->setnonblock_op(p, nonblock, errbuf);
+}
+
 #ifndef WIN32
+/*
+ * Set non-blocking mode, under the assumption that it's just the
+ * standard POSIX non-blocking flag.  (This can be called by the
+ * per-platform non-blocking-mode routine if that routine also
+ * needs to do some additional work.)
+ */
+int
+pcap_setnonblock_fd(pcap_t *p, int nonblock, char *errbuf)
+{
        int fdflags;
-#else
-       int newtimeout;
-#endif
-
-       if (p->sf.rfile != NULL) {
-               /*
-                * This is a savefile, not a live capture file, so
-                * ignore requests to put it in non-blocking mode.
-                */
-               return (0);
-       }
-
-#if HAVE_DAG_API
-       if (nonblock) {
-               p->md.dag_offset_flags |= DAGF_NONBLOCK;
-       } else {
-               p->md.dag_offset_flags &= ~DAGF_NONBLOCK;
-       }
-#endif /* HAVE_DAG_API */
 
-#ifndef WIN32
        fdflags = fcntl(p->fd, F_GETFL, 0);
        if (fdflags == -1) {
                snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "F_GETFL: %s",
@@ -588,29 +577,9 @@ pcap_setnonblock(pcap_t *p, int nonblock, char *errbuf)
                    pcap_strerror(errno));
                return (-1);
        }
-#else
-       if (nonblock) {
-               /*
-                * Set the read timeout to -1 for non-blocking mode.
-                */
-               newtimeout = -1;
-       } else {
-               /*
-                * Restore the timeout set when the device was opened.
-                * (Note that this may be -1, in which case we're not
-                * really leaving non-blocking mode.)
-                */
-               newtimeout = p->timeout;
-       }
-       if (!PacketSetReadTimeout(p->adapter, newtimeout)) {
-               snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
-                   "PacketSetReadTimeout: %s", pcap_win32strerror());
-               return (-1);
-       }
-       p->nonblock = (newtimeout == -1);
-#endif
        return (0);
 }
+#endif
 
 #ifdef WIN32
 /*
index 608275e074143f6d00f1e8efdf59c373b95755e1..fa0130b0b76599fe125e6437dce5879fe38a961c 100644 (file)
@@ -30,7 +30,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.96 2003-11-18 21:06:51 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.97 2003-11-20 02:02:41 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -428,6 +428,26 @@ swap_hdr(struct pcap_file_header *hp)
        hp->linktype = SWAPLONG(hp->linktype);
 }
 
+static int
+sf_getnonblock(pcap_t *p, char *errbuf)
+{
+       /*
+        * This is a savefile, not a live capture file, so never say
+        * it's in non-blocking mode.
+        */
+       return (0);
+}
+
+static int
+sf_setnonblock(pcap_t *p, int nonblock, char *errbuf)
+{
+       /*
+        * This is a savefile, not a live capture file, so ignore
+        * requests to put it in non-blocking mode.
+        */
+       return (0);
+}
+
 static int
 sf_stats(pcap_t *p, struct pcap_stat *ps)
 {
@@ -583,6 +603,8 @@ pcap_open_offline(const char *fname, char *errbuf)
        p->read_op = pcap_offline_read;
        p->setfilter_op = install_bpf_program;
        p->set_datalink_op = NULL;      /* we don't support munging link-layer headers */
+       p->getnonblock_op = sf_getnonblock;
+       p->setnonblock_op = sf_setnonblock;
        p->stats_op = sf_stats;
        p->close_op = sf_close;