]> The Tcpdump Group git mirrors - libpcap/commitdiff
Add a "close" function pointer to the pcap_t structure, which handles
authorguy <guy>
Fri, 25 Jul 2003 03:25:45 +0000 (03:25 +0000)
committerguy <guy>
Fri, 25 Jul 2003 03:25:45 +0000 (03:25 +0000)
the platform-dependent part of closing a pcap_t (and the
live-vs-savefile part as well, so that function must close the file
descriptor and free up any buffers allocated).

In the Digital UNIX support, add in a check for a memory allocation
failure.

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 39262f90afc02f545101ab946bc0db097bffcc51..1af0d8f980cbeb90579cb8e574c8428d52a74be6 100644 (file)
@@ -20,7 +20,7 @@
  */
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.61 2003-07-23 05:29:21 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.62 2003-07-25 03:25:45 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -440,6 +440,15 @@ bpf_open(pcap_t *p, char *errbuf)
        return (fd);
 }
 
+static void
+pcap_close_bpf(pcap_t *p)
+{
+       if (p->buffer != NULL)
+               free(p->buffer);
+       if (p->fd >= 0)
+               close(p->fd);
+}
+
 /*
  * XXX - on AIX, IBM's tcpdump (and perhaps the incompatible-with-everybody-
  * else's libpcap in AIX 5.1) appears to forcibly load the BPF driver
@@ -727,6 +736,8 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
        memset(p->buffer, 0x0, p->bufsize);
 #endif
 
+       p->close_op = pcap_close_bpf;
+
        return (p);
  bad:
        (void)close(fd);
index 4444f9c6895eca4a300c5cc2b7ff31dddeff7f45..9c5bb4172cdac578437156a04ff00cfc135c14c6 100644 (file)
@@ -19,7 +19,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.1 2003-07-23 05:29:21 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.2 2003-07-25 03:25:45 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -65,7 +65,6 @@ static int atexit_handler_installed = 0;
 #define dag_platform_finddevs pcap_platform_finddevs
 #define dag_setfilter pcap_setfilter
 #define dag_set_datalink_platform pcap_set_datalink_platform
-#define dag_platform_close pcap_platform_close
 #endif /* DAG_ONLY */
 
 static void delete_pcap_dag(pcap_t *p) {
@@ -87,14 +86,14 @@ static void delete_pcap_dag(pcap_t *p) {
 }
 
 /*
- * Performs a graceful shutdown of the DAG card and frees dynamic memory held
- * in the pcap_t structure.
+ * Performs a graceful shutdown of the DAG card, frees dynamic memory held
+ * in the pcap_t structure, and closes the file descriptor for the DAG card.
  */
 
-void dag_platform_close(pcap_t *p) {
+static void dag_platform_close(pcap_t *p) {
 
 #ifdef linux
-  if (p != NULL && p->md.is_dag && p->md.device != NULL) {
+  if (p != NULL && p->md.device != NULL) {
     if(dag_stop(p->fd) < 0)
       fprintf(stderr,"dag_stop %s: %s\n", p->md.device, strerror(errno));
     if(dag_close(p->fd) < 0)
@@ -103,7 +102,7 @@ void dag_platform_close(pcap_t *p) {
     free(p->md.device);
   }
 #else
-  if (p != NULL && p->md.is_dag) {
+  if (p != NULL) {
     if(dag_stop(p->fd) < 0)
       fprintf(stderr,"dag_stop: %s\n", strerror(errno));
     if(dag_close(p->fd) < 0)
@@ -111,6 +110,8 @@ void dag_platform_close(pcap_t *p) {
   }
 #endif
   delete_pcap_dag(p);
+  /* XXX - does "dag_close()" do this?  If so, we don't need to. */
+  close(p->fd);
 }
 
 static void atexit_handler(void) {
@@ -384,6 +385,8 @@ pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, c
     return NULL;
   }
 
+  handle->close_op = dag_platform_close;
+
   return handle;
 }
 
index 5593fe3242a2090392fb8d0387ba26b130b1bc8a..c5b0062e21a94c564fdb41de108ac57e7e166eb4 100644 (file)
@@ -38,7 +38,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.85 2003-02-19 08:06:26 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.86 2003-07-25 03:25:45 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -290,6 +290,15 @@ pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
 #endif /* A_PROMISCON_REQ */
 #endif /* HAVE_SOLARIS */
 
+static void
+pcap_close_dlpi(pcap_t *p)
+{
+       if (p->buffer != NULL)
+               free(p->buffer);
+       if (p->fd >= 0)
+               close(p->fd);
+}
+
 pcap_t *
 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
     char *ebuf)
@@ -654,9 +663,16 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
                    pcap_strerror(errno));
                goto bad;
        }
+
        /* Allocate data buffer */
        p->bufsize = PKTBUFSIZE;
        p->buffer = (u_char *)malloc(p->bufsize + p->offset);
+       if (p->buffer == NULL) {
+               strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
+               goto bad;
+       }
+
+       p->close_op = pcap_close_dlpi;
 
        return (p);
 bad:
index 5d4e5dac8ee79866dc66c8109076efc9def78f36..23363cddd334d8ec248f1872bf6ec83ff13163a9 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.47 2003-07-23 05:29:21 guy Exp $ (LBL)
+ * @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.48 2003-07-25 03:25:46 guy Exp $ (LBL)
  */
 
 #ifndef pcap_int_h
@@ -115,6 +115,10 @@ struct pcap {
         */
        u_char *pkt;
 
+       /*
+        * Methods.
+        */
+       void    (*close_op)(pcap_t *);
 
        /*
         * Placeholder for filter code if bpf not in kernel.
@@ -243,10 +247,6 @@ struct sockaddr *dup_sockaddr(struct sockaddr *, size_t);
 int    add_or_find_if(pcap_if_t **, pcap_if_t **, const char *, u_int,
            const char *, char *);
 
-#ifdef linux
-void   pcap_close_linux(pcap_t *);
-#endif
-
 #ifdef WIN32
 char   *pcap_win32strerror(void);
 #endif
index efdd80cc86b36578c01c2bea31d1e46770da66d4..e03070b91de4294ad63317cabb86cf3b6f32edad 100644 (file)
@@ -27,7 +27,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.90 2003-07-23 05:29:22 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-linux.c,v 1.91 2003-07-25 03:25:46 guy Exp $ (LBL)";
 #endif
 
 /*
@@ -187,6 +187,7 @@ static void map_arphrd_to_dlt(pcap_t *, int, int);
 static int live_open_old(pcap_t *, const char *, int, int, char *);
 static int live_open_new(pcap_t *, const char *, int, int, char *);
 static int pcap_read_packet(pcap_t *, pcap_handler, u_char *);
+static void pcap_close_linux(pcap_t *handle);
 
 /*
  * Wrap some ioctl calls
@@ -361,12 +362,7 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
                 */
                mtu = iface_get_mtu(handle->fd, device, ebuf);
                if (mtu == -1) {
-                       if (handle->md.clear_promisc)
-                               /* 2.0.x kernel */
-                               pcap_close_linux(handle);
-                       close(handle->fd);
-                       if (handle->md.device != NULL)
-                               free(handle->md.device);
+                       pcap_close_linux(handle);
                        free(handle);
                        return NULL;
                }
@@ -393,16 +389,13 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
        if (!handle->buffer) {
                snprintf(ebuf, PCAP_ERRBUF_SIZE,
                         "malloc: %s", pcap_strerror(errno));
-               if (handle->md.clear_promisc)
-                       /* 2.0.x kernel */
-                       pcap_close_linux(handle);
-               close(handle->fd);
-               if (handle->md.device != NULL)
-                       free(handle->md.device);
+               pcap_close_linux(handle);
                free(handle);
                return NULL;
        }
 
+       handle->close_op = pcap_close_linux;
+
        return handle;
 }
 
@@ -1459,18 +1452,11 @@ static void     pcap_close_all(void)
                pcap_close(handle);
 }
 
-void   pcap_close_linux( pcap_t *handle )
+static void    pcap_close_linux( pcap_t *handle )
 {
        struct pcap     *p, *prevp;
        struct ifreq    ifr;
 
-#ifdef HAVE_DAG_API
-       if (handle->md.is_dag) {
-               /* close actions will be done in dag_platform_close() */
-               return;
-       }
-#endif /* HAVE_DAG_API */
-
        if (handle->md.clear_promisc) {
                /*
                 * We put the interface into promiscuous mode; take
@@ -1535,6 +1521,10 @@ void     pcap_close_linux( pcap_t *handle )
        if (handle->md.device != NULL)
                free(handle->md.device);
        handle->md.device = NULL;
+       if (handle->buffer != NULL)
+               free(handle->buffer);
+       if (handle->fd >= 0)
+               close(handle->fd);
 }
 
 /*
@@ -1546,14 +1536,14 @@ static int
 live_open_old(pcap_t *handle, const char *device, int promisc,
              int to_ms, char *ebuf)
 {
-       int             sock_fd = -1, arptype;
+       int             arptype;
        struct ifreq    ifr;
 
        do {
                /* Open the socket */
 
-               sock_fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
-               if (sock_fd == -1) {
+               handle->fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
+               if (handle->fd == -1) {
                        snprintf(ebuf, PCAP_ERRBUF_SIZE,
                                 "socket: %s", pcap_strerror(errno));
                        break;
@@ -1572,13 +1562,13 @@ live_open_old(pcap_t *handle, const char *device, int promisc,
                                PCAP_ERRBUF_SIZE);
                        break;
                }
-               if (iface_bind_old(sock_fd, device, ebuf) == -1)
+               if (iface_bind_old(handle->fd, device, ebuf) == -1)
                        break;
 
                /*
                 * Try to get the link-layer type.
                 */
-               arptype = iface_get_arptype(sock_fd, device, ebuf);
+               arptype = iface_get_arptype(handle->fd, device, ebuf);
                if (arptype == -1)
                        break;
 
@@ -1598,7 +1588,7 @@ live_open_old(pcap_t *handle, const char *device, int promisc,
                if (promisc) {
                        memset(&ifr, 0, sizeof(ifr));
                        strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
-                       if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) == -1) {
+                       if (ioctl(handle->fd, SIOCGIFFLAGS, &ifr) == -1) {
                                snprintf(ebuf, PCAP_ERRBUF_SIZE,
                                         "ioctl: %s", pcap_strerror(errno));
                                break;
@@ -1632,7 +1622,7 @@ live_open_old(pcap_t *handle, const char *device, int promisc,
                                }
 
                                ifr.ifr_flags |= IFF_PROMISC;
-                               if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) == -1) {
+                               if (ioctl(handle->fd, SIOCSIFFLAGS, &ifr) == -1) {
                                        snprintf(ebuf, PCAP_ERRBUF_SIZE,
                                                 "ioctl: %s",
                                                 pcap_strerror(errno));
@@ -1649,10 +1639,6 @@ live_open_old(pcap_t *handle, const char *device, int promisc,
                        }
                }
 
-               /* Save the socket FD in the pcap structure */
-
-               handle->fd       = sock_fd;
-
                /*
                 * Default value for offset to align link-layer payload
                 * on a 4-byte boundary.
@@ -1663,10 +1649,7 @@ live_open_old(pcap_t *handle, const char *device, int promisc,
 
        } while (0);
 
-       if (handle->md.clear_promisc)
-               pcap_close_linux(handle);
-       if (sock_fd != -1)
-               close(sock_fd);
+       pcap_close_linux(handle);
        return 0;
 }
 
index f0bc8f47e8446362b33fbc18e819fe70289d011c..1c0117f0031c8f5753fd49f38516c0d0de940695 100644 (file)
@@ -20,7 +20,7 @@
  */
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.44 2002-12-22 02:36:49 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.45 2003-07-25 03:25:46 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -201,6 +201,15 @@ nit_setflags(int fd, int promisc, int to_ms, char *ebuf)
        return (0);
 }
 
+static void
+pcap_close_nit(pcap_t *p)
+{
+       if (p->buffer != NULL)
+               free(p->buffer);
+       if (p->fd >= 0)
+               close(p->fd);
+}
+
 pcap_t *
 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
     char *ebuf)
@@ -250,6 +259,9 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
                strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
                goto bad;
        }
+
+       p->close_op = pcap_close_nit;
+
        return (p);
  bad:
        if (fd >= 0)
index 7728abc86efc8e3cf5f5c206c04df43cabd5acbb..3fa123e4cb729b0e6027323c9d5f00da6b7f880a 100644 (file)
--- a/pcap-pf.c
+++ b/pcap-pf.c
@@ -24,7 +24,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.73 2003-05-02 08:35:42 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.74 2003-07-25 03:25:47 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -244,6 +244,15 @@ pcap_stats(pcap_t *p, struct pcap_stat *ps)
        return (0);
 }
 
+static void
+pcap_close_pf(pcap_t *p)
+{
+       if (p->buffer != NULL)
+               free(p->buffer);
+       if (p->fd >= 0)
+               close(p->fd);
+}
+
 pcap_t *
 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
     char *ebuf)
@@ -396,8 +405,15 @@ your system may not be properly configured; see the packetfilter(4) man page\n",
                        goto bad;
                }
        }
+
        p->bufsize = BUFSPACE;
        p->buffer = (u_char*)malloc(p->bufsize + p->offset);
+       if (p->buffer == NULL) {
+               strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
+               goto bad;
+       }
+
+       p->close_op = pcap_close_pf;
 
        return (p);
  bad:
index 774838db90f57849946a9f8394a3b2c2f5015950..3426c0d9a18783c3d57b7830d636cf4209325344 100644 (file)
@@ -25,7 +25,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.60 2002-12-22 02:36:50 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.61 2003-07-25 03:25:47 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -218,6 +218,15 @@ nit_setflags(int fd, int promisc, int to_ms, char *ebuf)
        return (0);
 }
 
+static void
+pcap_close_snit(pcap_t *p)
+{
+       if (p->buffer != NULL)
+               free(p->buffer);
+       if (p->fd >= 0)
+               close(p->fd);
+}
+
 pcap_t *
 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
     char *ebuf)
@@ -308,6 +317,9 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
                strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
                goto bad;
        }
+
+       p->close_op = pcap_close_snit;
+
        return (p);
  bad:
        if (fd >= 0)
index 0cdddac2b8bf5362386e59c7acd2ada27b3ede30..83cb6d21e13fae7f522d0f13557ed4bedb3be26f 100644 (file)
@@ -20,7 +20,7 @@
  */
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.39 2002-12-22 02:36:50 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.40 2003-07-25 03:25:47 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -142,6 +142,15 @@ pcap_stats(pcap_t *p, struct pcap_stat *ps)
        return (0);
 }
 
+static void
+pcap_close_snoop(pcap_t *p)
+{
+       if (p->buffer != NULL)
+               free(p->buffer);
+       if (p->fd >= 0)
+               close(p->fd);
+}
+
 /* XXX can't disable promiscuous */
 pcap_t *
 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
@@ -285,6 +294,8 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
                goto bad;
        }
 
+       p->close_op = pcap_close_snoop;
+
        return (p);
  bad:
        (void)close(fd);
index 1b15eec82e79831d24e1cdbee1ec29c535338671..8722517ffc28c360df888264cddb92eafeefbd39 100644 (file)
@@ -32,7 +32,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.8 2003-05-15 14:30:30 risso Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap-win32.c,v 1.9 2003-07-25 03:25:47 guy Exp $ (LBL)";
 #endif
 
 #include <pcap-int.h>
@@ -137,6 +137,17 @@ pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
 }
 
 
+static void
+pcap_close_win32(pcap_t *p)
+{
+       if (p->buffer != NULL)
+               free(p->buffer);
+       if (p->adapter != NULL) {
+               PacketCloseAdapter(p->adapter);
+               p->adapter = NULL;
+       }
+}
+
 pcap_t *
 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
     char *ebuf)
@@ -243,6 +254,8 @@ pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
 
        PacketSetReadTimeout(p->adapter, to_ms);
 
+       p->close_op = pcap_close_win32;
+
        return (p);
 bad:
        if (p->adapter)
diff --git a/pcap.c b/pcap.c
index 90bb3e172a5991c3c79345dfeb46ccfcb6384c2a..0b483b5ad7abea0962b9faa27c8daa166b8026ee 100644 (file)
--- a/pcap.c
+++ b/pcap.c
@@ -33,7 +33,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.56 2003-07-23 05:29:22 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/pcap.c,v 1.57 2003-07-25 03:25:48 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -610,6 +610,12 @@ pcap_strerror(int errnum)
 #endif
 }
 
+static void
+pcap_close_dead(pcap_t *p)
+{
+       /* Nothing to do. */
+}
+
 pcap_t *
 pcap_open_dead(int linktype, int snaplen)
 {
@@ -619,46 +625,18 @@ pcap_open_dead(int linktype, int snaplen)
        if (p == NULL)
                return NULL;
        memset (p, 0, sizeof(*p));
-#ifndef WIN32
-       p->fd = -1;
-#else
-       p->adapter = NULL;
-#endif /* WIN32 */
        p->snapshot = snaplen;
        p->linktype = linktype;
+       p->close_op = pcap_close_dead;
        return p;
 }
 
 void
 pcap_close(pcap_t *p)
 {
-       /*XXX*/
-#ifndef WIN32
-       if (p->fd >= 0) {
-#ifdef linux
-               pcap_close_linux(p);
-#endif
-#ifdef HAVE_DAG_API
-               dag_platform_close(p);
-#endif
-               close(p->fd);
-       }
-#else /* WIN32 */
-       if (p->adapter != NULL) {
-               PacketCloseAdapter(p->adapter);
-               p->adapter = NULL;
-       }
-#endif /* WIN32 */
-       if (p->sf.rfile != NULL) {
-               if (p->sf.rfile != stdin)
-                       (void)fclose(p->sf.rfile);
-               if (p->sf.base != NULL)
-                       free(p->sf.base);
-       } else if (p->buffer != NULL)
-               free(p->buffer);
+       p->close_op(p);
        if (p->dlt_list != NULL)
                free(p->dlt_list);
-
        pcap_freecode(&p->fcode);
        free(p);
 }
index 2ec8925c1d3c361c34aac024acf149a3936f3c7e..2a8bc9033f977521913c0506a697c9a70c5937cd 100644 (file)
@@ -30,7 +30,7 @@
 
 #ifndef lint
 static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.81 2003-06-27 07:57:10 guy Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/libpcap/savefile.c,v 1.82 2003-07-25 03:25:48 guy Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -410,6 +410,15 @@ swap_hdr(struct pcap_file_header *hp)
        hp->linktype = SWAPLONG(hp->linktype);
 }
 
+static void
+sf_close(pcap_t *p)
+{
+       if (p->sf.rfile != stdin)
+               (void)fclose(p->sf.rfile);
+       if (p->sf.base != NULL)
+               free(p->sf.base);
+}
+
 pcap_t *
 pcap_open_offline(const char *fname, char *errbuf)
 {
@@ -426,14 +435,6 @@ pcap_open_offline(const char *fname, char *errbuf)
        }
 
        memset((char *)p, 0, sizeof(*p));
-       /*
-        * Set this field so we don't close stdin in pcap_close!
-        */
-#ifndef WIN32
-       p->fd = -1;
-#else
-       p->adapter = NULL;
-#endif
 
        if (fname[0] == '-' && fname[1] == '\0')
                fp = stdin;
@@ -523,6 +524,8 @@ pcap_open_offline(const char *fname, char *errbuf)
        pcap_fddipad = 0;
 #endif
 
+       p->close_op = sf_close;
+
        return (p);
  bad:
        if(fp)