diff options
-rw-r--r-- | pcap-bpf.c | 13 | ||||
-rw-r--r-- | pcap-dag.c | 17 | ||||
-rw-r--r-- | pcap-dlpi.c | 18 | ||||
-rw-r--r-- | pcap-int.h | 10 | ||||
-rw-r--r-- | pcap-linux.c | 55 | ||||
-rw-r--r-- | pcap-nit.c | 14 | ||||
-rw-r--r-- | pcap-pf.c | 18 | ||||
-rw-r--r-- | pcap-snit.c | 14 | ||||
-rw-r--r-- | pcap-snoop.c | 13 | ||||
-rw-r--r-- | pcap-win32.c | 15 | ||||
-rw-r--r-- | pcap.c | 40 | ||||
-rw-r--r-- | savefile.c | 21 |
12 files changed, 153 insertions, 95 deletions
@@ -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); @@ -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; } diff --git a/pcap-dlpi.c b/pcap-dlpi.c index 5593fe32..c5b0062e 100644 --- a/pcap-dlpi.c +++ b/pcap-dlpi.c @@ -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: @@ -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 diff --git a/pcap-linux.c b/pcap-linux.c index efdd80cc..e03070b9 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -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; } @@ -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) @@ -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: diff --git a/pcap-snit.c b/pcap-snit.c index 774838db..3426c0d9 100644 --- a/pcap-snit.c +++ b/pcap-snit.c @@ -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) diff --git a/pcap-snoop.c b/pcap-snoop.c index 0cdddac2..83cb6d21 100644 --- a/pcap-snoop.c +++ b/pcap-snoop.c @@ -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); diff --git a/pcap-win32.c b/pcap-win32.c index 1b15eec8..8722517f 100644 --- a/pcap-win32.c +++ b/pcap-win32.c @@ -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) @@ -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); } @@ -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) |