aboutsummaryrefslogtreecommitdiffstats
path: root/pcap-usb-linux.c
diff options
context:
space:
mode:
authorguy <guy>2008-04-14 20:40:58 +0000
committerguy <guy>2008-04-14 20:40:58 +0000
commit2527d1ac889ed9b6c6c36d1d312700a643beb346 (patch)
tree26a05e35cdf7c3c5aec5c65892c2a5f185a6a285 /pcap-usb-linux.c
parent0fdc174e4c2d9dfd58a11027ab8ce3b5f99874a5 (diff)
Turn close_op into cleanup_op; the routine that handles it can also be
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.
Diffstat (limited to 'pcap-usb-linux.c')
-rw-r--r--pcap-usb-linux.c47
1 files changed, 25 insertions, 22 deletions
diff --git a/pcap-usb-linux.c b/pcap-usb-linux.c
index 7b447bc..98bc16c 100644
--- a/pcap-usb-linux.c
+++ b/pcap-usb-linux.c
@@ -34,7 +34,7 @@
*/
#ifndef lint
static const char rcsid[] _U_ =
- "@(#) $Header: /tcpdump/master/libpcap/pcap-usb-linux.c,v 1.22 2008-04-04 19:37:45 guy Exp $ (LBL)";
+ "@(#) $Header: /tcpdump/master/libpcap/pcap-usb-linux.c,v 1.23 2008-04-14 20:40:58 guy Exp $ (LBL)";
#endif
#ifdef HAVE_CONFIG_H
@@ -126,8 +126,7 @@ static int usb_read_linux_mmap(pcap_t *, int , pcap_handler , u_char *);
static int usb_inject_linux(pcap_t *, const void *, size_t);
static int usb_setfilter_linux(pcap_t *, struct bpf_program *);
static int usb_setdirection_linux(pcap_t *, pcap_direction_t);
-static void usb_close_linux(pcap_t *);
-static void usb_close_linux_mmap(pcap_t *);
+static void usb_cleanup_linux_mmap(pcap_t *);
/* facility to add an USB device to the device list*/
static int
@@ -213,14 +212,13 @@ usb_activate(pcap_t* handle)
handle->set_datalink_op = NULL; /* can't change data link type */
handle->getnonblock_op = pcap_getnonblock_fd;
handle->setnonblock_op = pcap_setnonblock_fd;
- handle->close_op = usb_close_linux;
/*get usb bus index from device name */
if (sscanf(handle->opt.source, USB_IFACE"%d", &handle->md.ifindex) != 1)
{
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"Can't get USB bus index from %s", handle->opt.source);
- return -1;
+ return PCAP_ERROR;
}
/*now select the read method: try to open binary interface */
@@ -228,11 +226,18 @@ usb_activate(pcap_t* handle)
handle->fd = open(full_path, O_RDONLY, 0);
if (handle->fd >= 0)
{
+ if (p->opt.rfmon) {
+ /*
+ * Monitor mode doesn't apply to USB devices.
+ */
+ return PCAP_ERROR_RFMON_NOTSUP;
+ }
+
/* binary api is available, try to use fast mmap access */
if (usb_mmap(handle)) {
handle->stats_op = usb_stats_linux_bin;
handle->read_op = usb_read_linux_mmap;
- handle->close_op = usb_close_linux_mmap;
+ handle->cleanup_op = usb_cleanup_linux_mmap;
/*
* "handle->fd" is a real file, so "select()" and
@@ -255,12 +260,19 @@ usb_activate(pcap_t* handle)
/* no more fallback, give it up*/
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"Can't open USB bus file %s: %s", full_path, strerror(errno));
- return -1;
+ return PCAP_ERROR;
}
handle->stats_op = usb_stats_linux;
handle->read_op = usb_read_linux;
}
+ if (p->opt.rfmon) {
+ /*
+ * Monitor mode doesn't apply to USB devices.
+ */
+ return PCAP_ERROR_RFMON_NOTSUP;
+ }
+
/*
* "handle->fd" is a real file, so "select()" and "poll()"
* work on it.
@@ -273,8 +285,7 @@ usb_activate(pcap_t* handle)
if (!handle->buffer) {
snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,
"malloc: %s", pcap_strerror(errno));
- close(handle->fd);
- return -1;
+ return PCAP_ERROR;
}
return 0;
}
@@ -490,15 +501,6 @@ usb_inject_linux(pcap_t *handle, const void *buf, size_t size)
return (-1);
}
-static void
-usb_close_linux(pcap_t* handle)
-{
- /* handle fill be freed in pcap_close() 'common' code */
- close(handle->fd);
- if (handle->buffer)
- free(handle->buffer);
-}
-
static int
usb_stats_linux(pcap_t *handle, struct pcap_stat *stats)
{
@@ -719,9 +721,10 @@ usb_read_linux_mmap(pcap_t *handle, int max_packets, pcap_handler callback, u_ch
}
static void
-usb_close_linux_mmap(pcap_t* handle)
+usb_cleanup_linux_mmap(pcap_t* handle)
{
- /* handle will be freed in pcap_close() 'common' code, buffer must not
- * be freed because it's memory mapped */
- close(handle->fd);
+ /* buffer must not be freed because it's memory mapped */
+ /* XXX - does it need to be unmapped? */
+ handle->buffer = NULL;
+ pcap_cleanup_live_common(handle);
}