diff options
author | Guy Harris <guy@alum.mit.edu> | 2020-02-17 19:58:39 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2020-02-17 19:58:39 -0800 |
commit | 9c946d4d8061763d608534af6e155d1319785f8a (patch) | |
tree | c25d8bf649cdc11d69b5766366f4de29bc2d6f35 /pcap-linux.c | |
parent | df95a26b67a494ce791a4dce1db3117801f6f2c9 (diff) |
We don't need separate mmapped and non-mmapped cleanup routines.
Just destroy the ring and free the oneshot buffer in the regular cleanup
routine; destroy_ring() will work OK if there's nothing to destroy.
Diffstat (limited to 'pcap-linux.c')
-rw-r--r-- | pcap-linux.c | 37 |
1 files changed, 19 insertions, 18 deletions
diff --git a/pcap-linux.c b/pcap-linux.c index 36a8e986..8d9ce9be 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -255,7 +255,6 @@ union thdr { static void destroy_ring(pcap_t *handle); static int create_ring(pcap_t *handle, int *status); static int prepare_tpacket_socket(pcap_t *handle); -static void pcap_cleanup_linux_mmap(pcap_t *); static int pcap_read_linux_mmap_v2(pcap_t *, int, pcap_handler , u_char *); #ifdef HAVE_TPACKET3 static int pcap_read_linux_mmap_v3(pcap_t *, int, pcap_handler , u_char *); @@ -1168,6 +1167,19 @@ static void pcap_cleanup_linux( pcap_t *handle ) pcap_remove_from_pcaps_to_close(handle); } + if (handle->fd != -1) { + /* + * Destroy the ring buffer (assuming we've set it up), + * and unmap it if it's mapped. + */ + destroy_ring(handle); + } + + if (handlep->oneshot_buffer != NULL) { + free(handlep->oneshot_buffer); + handlep->oneshot_buffer = NULL; + } + if (handlep->mondevice != NULL) { free(handlep->mondevice); handlep->mondevice = NULL; @@ -3110,7 +3122,6 @@ setup_mmapped(pcap_t *handle, int *status) break; #endif } - handle->cleanup_op = pcap_cleanup_linux_mmap; handle->setfilter_op = pcap_setfilter_linux_mmap; handle->setnonblock_op = pcap_setnonblock_mmap; handle->getnonblock_op = pcap_getnonblock_mmap; @@ -3715,10 +3726,14 @@ destroy_ring(pcap_t *handle) { struct pcap_linux *handlep = handle->priv; - /* tell the kernel to destroy the ring*/ + /* + * Tell the kernel to destroy the ring. + * We don't check for setsockopt failure, as 1) we can't recover + * from an error and 2) we might not yet have set it up in the + * first place. + */ struct tpacket_req req; memset(&req, 0, sizeof(req)); - /* do not test for setsockopt failure, as we can't recover from any error */ (void)setsockopt(handle->fd, SOL_PACKET, PACKET_RX_RING, (void *) &req, sizeof(req)); @@ -3760,20 +3775,6 @@ pcap_oneshot_mmap(u_char *user, const struct pcap_pkthdr *h, *sp->pkt = handlep->oneshot_buffer; } -static void -pcap_cleanup_linux_mmap( pcap_t *handle ) -{ - struct pcap_linux *handlep = handle->priv; - - destroy_ring(handle); - if (handlep->oneshot_buffer != NULL) { - free(handlep->oneshot_buffer); - handlep->oneshot_buffer = NULL; - } - pcap_cleanup_linux(handle); -} - - static int pcap_getnonblock_mmap(pcap_t *handle) { |