Skip to content

Commit

Permalink
Turn close_op into cleanup_op; the routine that handles it can also be
Browse files Browse the repository at this point in the history
used to clean up after a failed pcap_activate() call.  Convert the
existing close_op routines to cleanup_op routines, and use them to clean
up; rename pcap_close_common() to pcap_cleanup_live_common(), and use it
directly if there's no platform-dependent cleanup needed.  That means we
don't have to write the same cleanup code twice (and possibly forget
stuff in the version done on a failed pcap_activate() call).

Have the cleanup routines do whatever is necessary to indicate that
cleanup has been done, and not do any particular cleaning up if it's
already been done (i.e., don't free something if the pointer to it is
null and null out the pointer once it's been freed, don't close an FD if
it's -1 and set it to -1 once it's been closed, etc.).

For device types/platforms where we don't support monitor mode, check
for it and return PCAP_ERROR_RFMON_NOTSUP - but do so after we've
checked whether we can open the device, so we return "no such device" or
"permission denied" rather than "that device doesn't support monitor
mode" if we can't open the device in the first place.

Fix a comment.
  • Loading branch information
yuguy committed Apr 14, 2008
1 parent 0fdc174 commit 2527d1a
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 174 deletions.
30 changes: 10 additions & 20 deletions pcap-bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
#ifndef lint
static const char rcsid[] _U_ =
"@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.109 2008-04-10 03:10:33 guy Exp $ (LBL)";
"@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.110 2008-04-14 20:40:58 guy Exp $ (LBL)";
#endif

#ifdef HAVE_CONFIG_H
Expand Down Expand Up @@ -870,7 +870,7 @@ bpf_load(char *errbuf)
* Turn off rfmon mode if necessary.
*/
static void
pcap_close_bpf(pcap_t *p)
pcap_cleanup_bpf(pcap_t *p)
{
#ifdef HAVE_BSD_IEEE80211
int sock;
Expand Down Expand Up @@ -939,12 +939,14 @@ pcap_close_bpf(pcap_t *p)
* have to take the interface out of some mode.
*/
pcap_remove_from_pcaps_to_close(p);
p->md.must_clear = 0;
}

if (p->md.device != NULL)
if (p->md.device != NULL) {
free(p->md.device);
p->md.device = NULL;
pcap_close_common(p);
p->md.device = NULL;
}
pcap_cleanup_live_common(p);
}

static int
Expand Down Expand Up @@ -1561,7 +1563,7 @@ pcap_activate_bpf(pcap_t *p)
#endif /* _AIX */

if (p->opt.promisc) {
/* set promiscuous mode, okay if it fails */
/* set promiscuous mode, just warn if it fails */
if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) {
snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCPROMISC: %s",
pcap_strerror(errno));
Expand Down Expand Up @@ -1668,23 +1670,11 @@ pcap_activate_bpf(pcap_t *p)
p->getnonblock_op = pcap_getnonblock_fd;
p->setnonblock_op = pcap_setnonblock_fd;
p->stats_op = pcap_stats_bpf;
p->close_op = pcap_close_bpf;
p->cleanup_op = pcap_cleanup_bpf;

return (status);
bad:
(void)close(fd);
if (p->dlt_list != NULL) {
free(p->dlt_list);
p->dlt_list = NULL;
}
if (p->md.device != NULL) {
free(p->md.device);
p->md.device = NULL;
}
if (p->buffer != NULL) {
free(p->buffer);
p->buffer = NULL;
}
pcap_cleanup_bpf(p);
return (status);
}

Expand Down
24 changes: 12 additions & 12 deletions pcap-bt-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
#ifndef lint
static const char rcsid[] _U_ =
"@(#) $Header: /tcpdump/master/libpcap/pcap-bt-linux.c,v 1.12 2008-04-07 03:57:32 guy Exp $ (LBL)";
"@(#) $Header: /tcpdump/master/libpcap/pcap-bt-linux.c,v 1.13 2008-04-14 20:40:58 guy Exp $ (LBL)";
#endif

#ifdef HAVE_CONFIG_H
Expand Down Expand Up @@ -155,6 +155,7 @@ bt_activate(pcap_t* handle)
int opt;
int dev_id;
struct hci_filter flt;
int err = PCAP_ERROR;

/* get bt interface id */
if (sscanf(handle->opt.source, BT_IFACE"%d", &dev_id) != 1)
Expand All @@ -178,7 +179,6 @@ bt_activate(pcap_t* handle)
handle->getnonblock_op = pcap_getnonblock_fd;
handle->setnonblock_op = pcap_setnonblock_fd;
handle->stats_op = bt_stats_linux;
handle->close_op = bt_close_linux;
handle->md.ifindex = dev_id;

/* Create HCI socket */
Expand Down Expand Up @@ -231,6 +231,14 @@ bt_activate(pcap_t* handle)
goto close_fail;
}

if (p->opt.rfmon) {
/*
* Monitor mode doesn't apply to Bluetooth devices.
*/
err = PCAP_ERROR_RFMON_NOTSUP;
goto close_fail;
}

if (handle->opt.buffer_size == 0) {
/*
* Set the socket buffer size to the specified value.
Expand All @@ -248,8 +256,8 @@ bt_activate(pcap_t* handle)
return 0;

close_fail:
close(handle->fd);
return PCAP_ERROR;
pcap_cleanup_live_common(p);
return err;
}

static int
Expand Down Expand Up @@ -322,14 +330,6 @@ bt_inject_linux(pcap_t *handle, const void *buf, size_t size)
}


static void
bt_close_linux(pcap_t* handle)
{
close(handle->fd);
free(handle->buffer);
}


static int
bt_stats_linux(pcap_t *handle, struct pcap_stat *stats)
{
Expand Down
19 changes: 12 additions & 7 deletions pcap-dag.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#ifndef lint
static const char rcsid[] _U_ =
"@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.38 2008-04-08 03:00:14 guy Exp $ (LBL)";
"@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.39 2008-04-14 20:40:58 guy Exp $ (LBL)";
#endif

#ifdef HAVE_CONFIG_H
Expand Down Expand Up @@ -125,7 +125,7 @@ delete_pcap_dag(pcap_t *p)
*/

static void
dag_platform_close(pcap_t *p)
dag_platform_cleanup(pcap_t *p)
{

if (p != NULL) {
Expand All @@ -139,10 +139,14 @@ dag_platform_close(pcap_t *p)
if(dag_stop(p->fd) < 0)
fprintf(stderr,"dag_stop: %s\n", strerror(errno));
#endif /* HAVE_DAG_STREAMS_API */
if(dag_close(p->fd) < 0)
fprintf(stderr,"dag_close: %s\n", strerror(errno));
if(p->fd != -1) {
if(dag_close(p->fd) < 0)
fprintf(stderr,"dag_close: %s\n", strerror(errno));
p->fd = -1;
}
delete_pcap_dag(p);
pcap_cleanup_live_common(p);
}
delete_pcap_dag(p);
/* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
}

Expand All @@ -151,7 +155,7 @@ atexit_handler(void)
{
while (pcap_dags != NULL) {
if (pcap_dags->pid == getpid()) {
dag_platform_close(pcap_dags->p);
dag_platform_cleanup(pcap_dags->p);
} else {
delete_pcap_dag(pcap_dags->p);
}
Expand Down Expand Up @@ -783,7 +787,7 @@ static int dag_activate(pcap_t* handle)
handle->getnonblock_op = pcap_getnonblock_fd;
handle->setnonblock_op = dag_setnonblock;
handle->stats_op = dag_stats;
handle->close_op = dag_platform_close;
handle->cleanup_op = dag_platform_cleanup;
handle->md.stat.ps_drop = 0;
handle->md.stat.ps_recv = 0;
return 0;
Expand All @@ -809,6 +813,7 @@ static int dag_activate(pcap_t* handle)
delete_pcap_dag(handle);

fail:
pcap_cleanup_live_common(handle);
if (newDev != NULL) {
free((char *)newDev);
}
Expand Down
22 changes: 10 additions & 12 deletions pcap-dlpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

#ifndef lint
static const char rcsid[] _U_ =
"@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.126 2008-04-10 00:50:34 guy Exp $ (LBL)";
"@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.127 2008-04-14 20:40:58 guy Exp $ (LBL)";
#endif

#ifdef HAVE_CONFIG_H
Expand Down Expand Up @@ -314,11 +314,13 @@ pcap_inject_dlpi(pcap_t *p, const void *buf, size_t size)
#endif /* HAVE_SOLARIS */

static void
pcap_close_dlpi(pcap_t *p)
pcap_cleanup_dlpi(pcap_t *p)
{
pcap_close_common(p);
if (p->send_fd >= 0)
if (p->send_fd >= 0) {
close(p->send_fd);
p->send_fd = -1;
}
pcap_cleanup_live_common(p);
}

static int
Expand All @@ -344,9 +346,6 @@ pcap_activate_dlpi(pcap_t *p)
#endif
int status = PCAP_ERROR;

p->fd = -1; /* indicate that it hasn't been opened yet */
p->send_fd = -1;

#ifdef HAVE_DEV_DLPI
/*
** Remove any "/dev/" on the front of the device.
Expand Down Expand Up @@ -750,14 +749,11 @@ pcap_activate_dlpi(pcap_t *p)
p->getnonblock_op = pcap_getnonblock_fd;
p->setnonblock_op = pcap_setnonblock_fd;
p->stats_op = pcap_stats_dlpi;
p->close_op = pcap_close_dlpi;
p->cleanup_op = pcap_cleanup_dlpi;

return (status);
bad:
if (p->fd >= 0)
close(p->fd);
if (p->send_fd >= 0)
close(p->send_fd);
pcap_cleanup_dlpi(p);
return (status);
}

Expand Down Expand Up @@ -1686,6 +1682,8 @@ pcap_create(const char *device, char *ebuf)
if (p == NULL)
return (NULL);

p->send_fd = -1; /* it hasn't been opened yet */

p->activate_op = pcap_activate_dlpi;
return (p);
}
6 changes: 3 additions & 3 deletions pcap-dos.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* pcap-dos.c: Interface to PKTDRVR, NDIS2 and 32-bit pmode
* network drivers.
*
* @(#) $Header: /tcpdump/master/libpcap/pcap-dos.c,v 1.4 2008-04-04 19:37:45 guy Exp $ (LBL)
* @(#) $Header: /tcpdump/master/libpcap/pcap-dos.c,v 1.5 2008-04-14 20:40:58 guy Exp $ (LBL)
*/

#include <stdio.h>
Expand Down Expand Up @@ -177,7 +177,7 @@ static int pcap_activate_dos (pcap_t *pcap)
pcap->snapshot = ETH_MAX;

pcap->linktype = DLT_EN10MB; /* !! */
pcap->close_op = pcap_close_dos;
pcap->cleanup_op = pcap_cleanup_dos;
pcap->read_op = pcap_read_dos;
pcap->stats_op = pcap_stats_dos;
pcap->inject_op = pcap_sendpacket_dos;
Expand Down Expand Up @@ -430,7 +430,7 @@ u_long pcap_filter_packets (void)
/*
* Close pcap device. Not called for offline captures.
*/
static void pcap_close_dos (pcap_t *p)
static void pcap_cleanup_dos (pcap_t *p)
{
if (p && !exc_occured)
{
Expand Down
8 changes: 4 additions & 4 deletions pcap-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.89 2008-04-04 19:37:45 guy Exp $ (LBL)
* @(#) $Header: /tcpdump/master/libpcap/pcap-int.h,v 1.90 2008-04-14 20:40:58 guy Exp $ (LBL)
*/

#ifndef pcap_int_h
Expand Down Expand Up @@ -193,7 +193,7 @@ typedef int (*setbuff_op_t)(pcap_t *, int);
typedef int (*setmode_op_t)(pcap_t *, int);
typedef int (*setmintocopy_op_t)(pcap_t *, int);
#endif
typedef void (*close_op_t)(pcap_t *);
typedef void (*cleanup_op_t)(pcap_t *);

struct pcap {
#ifdef WIN32
Expand Down Expand Up @@ -270,7 +270,7 @@ struct pcap {
setmode_op_t setmode_op;
setmintocopy_op_t setmintocopy_op;
#endif
close_op_t close_op;
cleanup_op_t cleanup_op;

/*
* Placeholder for filter code if bpf not in kernel.
Expand Down Expand Up @@ -391,7 +391,7 @@ pcap_t *pcap_create_common(const char *, char *);
int pcap_do_addexit(pcap_t *);
void pcap_add_to_pcaps_to_close(pcap_t *);
void pcap_remove_from_pcaps_to_close(pcap_t *);
void pcap_close_common(pcap_t *);
void pcap_cleanup_live_common(pcap_t *);
int pcap_not_initialized(pcap_t *);
int pcap_check_activated(pcap_t *);

Expand Down
23 changes: 11 additions & 12 deletions pcap-libdlpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

#ifndef lint
static const char rcsid[] _U_ =
"@(#) $Header: /tcpdump/master/libpcap/pcap-libdlpi.c,v 1.5 2008-04-09 19:58:02 guy Exp $ (LBL)";
"@(#) $Header: /tcpdump/master/libpcap/pcap-libdlpi.c,v 1.6 2008-04-14 20:40:58 guy Exp $ (LBL)";
#endif

#ifdef HAVE_CONFIG_H
Expand Down Expand Up @@ -104,8 +104,6 @@ pcap_activate_libdlpi(pcap_t *p)
dlpi_info_t dlinfo;
int err = PCAP_ERROR;

p->fd = -1; /* indicate that it hasn't been opened yet */

/*
* Enable Solaris raw and passive DLPI extensions;
* dlpi_open() will not fail if the underlying link does not support
Expand Down Expand Up @@ -211,14 +209,11 @@ pcap_activate_libdlpi(pcap_t *p)
p->getnonblock_op = pcap_getnonblock_fd;
p->setnonblock_op = pcap_setnonblock_fd;
p->stats_op = pcap_stats_dlpi;
p->close_op = pcap_close_libdlpi;
p->cleanup_op = pcap_cleanup_libdlpi;

return (0);
bad:
/* Get rid of any link-layer type list we allocated. */
if (p->dlt_list != NULL)
free(p->dlt_list);
dlpi_close(p->dlpi_hd);
pcap_cleanup_libdlpi(p);
return (err);
}

Expand Down Expand Up @@ -338,13 +333,17 @@ pcap_inject_libdlpi(pcap_t *p, const void *buf, size_t size)
}

/*
* Close dlpi handle and deallocate data buffer.
* Close dlpi handle.
*/
static void
pcap_close_libdlpi(pcap_t *p)
pcap_cleanup_libdlpi(pcap_t *p)
{
dlpi_close(p->dlpi_hd);
free(p->buffer);
if (p->dlpi_hd != NULL) {
dlpi_close(p->dlpi_hd);
p->dlpi_hd = NULL;
p->fd = -1;
}
pcap_cleanup_live_common(p);
}

/*
Expand Down
Loading

0 comments on commit 2527d1a

Please sign in to comment.