Skip to content

Commit

Permalink
Don't just have a single general-purpose buffer in probe_devices().
Browse files Browse the repository at this point in the history
Have separate buffers for device pathnames and USB device descriptors;
that may take extra memory on the stack, but it makes the code somewhat
cleaner.
  • Loading branch information
guyharris committed Dec 25, 2019
1 parent 9070709 commit 1292358
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions pcap-usb-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ int usb_mmap(pcap_t* handle)

#define USB_DT_DEVICE 1

#define USB_DEVICE_DESCRIPTOR_SIZE 18

/* probe the descriptors of the devices attached to the bus */
/* the descriptors will end up in the captured packet stream */
/* and be decoded by external apps like wireshark */
Expand All @@ -500,12 +502,13 @@ probe_devices(int bus)
struct usbdevfs_ctrltransfer ctrl;
struct dirent* data;
int ret = 0;
char buf[sizeof("/dev/bus/usb/000/") + NAME_MAX];
char busdevpath[sizeof("/dev/bus/usb/000/") + NAME_MAX];
DIR* dir;
char descriptor[USB_DEVICE_DESCRIPTOR_SIZE];

/* scan usb bus directories for device nodes */
snprintf(buf, sizeof(buf), "/dev/bus/usb/%03d", bus);
dir = opendir(buf);
snprintf(busdevpath, sizeof(busdevpath), "/dev/bus/usb/%03d", bus);
dir = opendir(busdevpath);
if (!dir)
return;

Expand All @@ -516,9 +519,9 @@ probe_devices(int bus)
if (name[0] == '.')
continue;

snprintf(buf, sizeof(buf), "/dev/bus/usb/%03d/%s", bus, data->d_name);
snprintf(busdevpath, sizeof(busdevpath), "/dev/bus/usb/%03d/%s", bus, data->d_name);

fd = open(buf, O_RDWR);
fd = open(busdevpath, O_RDWR);
if (fd == -1)
continue;

Expand All @@ -531,15 +534,15 @@ probe_devices(int bus)
ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
ctrl.wValue = USB_DT_DEVICE << 8;
ctrl.wIndex = 0;
ctrl.wLength = sizeof(buf);
ctrl.wLength = sizeof(descriptor);
#else
ctrl.requesttype = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
ctrl.request = USB_REQ_GET_DESCRIPTOR;
ctrl.value = USB_DT_DEVICE << 8;
ctrl.index = 0;
ctrl.length = sizeof(buf);
ctrl.length = sizeof(descriptor);
#endif
ctrl.data = buf;
ctrl.data = descriptor;
ctrl.timeout = CTRL_TIMEOUT;

ret = ioctl(fd, USBDEVFS_CONTROL, &ctrl);
Expand Down

0 comments on commit 1292358

Please sign in to comment.