Skip to content

Commit

Permalink
pcap: Add appropriate endianness conversions for USB pcap header
Browse files Browse the repository at this point in the history
The USB pcap header should always be little endian. As such, add the
appropriate conversions where necessary to ensure that the data is
interpreted correctly on big endian architectures.
  • Loading branch information
benzea committed Mar 3, 2024
1 parent 570b58e commit 671e399
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/umockdev-pcap.vala
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ internal class IoctlUsbPcapHandler : IoctlBase {
usb_header_mmapped *urb_hdr = (void*) cur_buf;

cur_waiting_since = now;
last_pkt_time_ms = urb_hdr.ts_sec * 1000 + urb_hdr.ts_usec / 1000;
last_pkt_time_ms = from_little_endian(urb_hdr.ts_sec) * 1000 + from_little_endian(urb_hdr).ts_usec / 1000;
start_time_ms = last_pkt_time_ms;
}

Expand All @@ -216,10 +216,10 @@ internal class IoctlUsbPcapHandler : IoctlBase {

usb_header_mmapped *urb_hdr = (void*) cur_buf;

uint64 cur_pkt_time_ms = urb_hdr.ts_sec * 1000 + urb_hdr.ts_usec / 1000;
uint64 cur_pkt_time_ms = from_little_endian(urb_hdr.ts_sec) * 1000 + from_little_endian(urb_hdr).ts_usec / 1000;

/* Discard anything from a different bus/device */
if (urb_hdr.bus_id != bus || urb_hdr.device_address != device)
if (from_little_endian(urb_hdr.bus_id) != bus || urb_hdr.device_address != device)
continue;

/* Print out debug info, if we need 5s longer than the recording
Expand All @@ -230,7 +230,7 @@ internal class IoctlUsbPcapHandler : IoctlBase {
(ulong) (now - cur_waiting_since) / 1000,
(ulong) (cur_pkt_time_ms - last_pkt_time_ms));
message("Trying to reap at recording position %c %s packet, for endpoint 0x%02x with length %u, replay may be stuck (time: %.3f)",
urb_hdr.event_type, urb_type_to_string(urb_hdr.transfer_type), urb_hdr.endpoint_number, urb_hdr.urb_len, (cur_pkt_time_ms - start_time_ms) / 1000.0);
urb_hdr.event_type, urb_type_to_string(urb_hdr.transfer_type), urb_hdr.endpoint_number, from_little_endian(urb_hdr.urb_len), (cur_pkt_time_ms - start_time_ms) / 1000.0);
message("The device has currently %u in-flight URBs:", urbs.length);

for (var i = 0; i < urbs.length; i++) {
Expand Down Expand Up @@ -277,7 +277,7 @@ internal class IoctlUsbPcapHandler : IoctlBase {
*/
if ((urb.type != urb_hdr.transfer_type) ||
((urb.type != URB_CONTROL) && (urb.endpoint != urb_hdr.endpoint_number)) ||
(urb_buffer_length != urb_hdr.urb_len)) {
(urb_buffer_length != from_little_endian(urb_hdr.urb_len))) {

if (debug)
stderr.printf("UMockdev: Queued URB %d has a metadata mismatch!\n", i);
Expand All @@ -290,10 +290,10 @@ internal class IoctlUsbPcapHandler : IoctlBase {
continue;
}

if (urb_hdr.data_len > 0) {
if (from_little_endian(urb_hdr.data_len) > 0) {
/* Data must have been captured. */
assert(urb_hdr.data_flag == 0);
assert(urb_hdr.data_len == urb_buffer_length);
assert(from_little_endian(urb_hdr.data_len) == urb_buffer_length);

/* Compare the full buffer (as we are outgoing) */
if (Posix.memcmp(urb_buffer, &cur_buf[sizeof(usb_header_mmapped)], urb_buffer_length) != 0) {
Expand All @@ -317,10 +317,10 @@ internal class IoctlUsbPcapHandler : IoctlBase {
}

/* Everything matches, mark as submitted */
urb_data.pcap_id = urb_hdr.id;
urb_data.pcap_id = from_little_endian(urb_hdr.id);

/* Packet was handled. */
last_pkt_time_ms = urb_hdr.ts_sec * 1000 + urb_hdr.ts_usec / 1000;
last_pkt_time_ms = from_little_endian(urb_hdr.ts_sec) * 1000 + from_little_endian(urb_hdr).ts_usec / 1000;
break;
}

Expand All @@ -337,7 +337,7 @@ internal class IoctlUsbPcapHandler : IoctlBase {
for (int i = 0; i < urbs.length; i++) {
urb_info = urbs.index(i);

if (urb_info.pcap_id == urb_hdr.id) {
if (urb_info.pcap_id == from_little_endian(urb_hdr.id)) {
urb = (Ioctl.usbdevfs_urb*) urb_info.urb_data.data;
urbs.remove_index(i);
break;
Expand All @@ -355,7 +355,7 @@ internal class IoctlUsbPcapHandler : IoctlBase {
/* We can reap this urb!
* Copy any data back if present.
*/
if (urb_hdr.data_len > 0) {
if (from_little_endian(urb_hdr.data_len) > 0) {
assert(urb_hdr.data_flag == 0);

uint8* urb_buffer = urb.buffer;
Expand All @@ -366,16 +366,16 @@ internal class IoctlUsbPcapHandler : IoctlBase {
if (urb.type == URB_CONTROL)
urb_buffer = &urb.buffer[8];

Posix.memcpy(urb_buffer, &cur_buf[sizeof(usb_header_mmapped)], urb_hdr.data_len);
Posix.memcpy(urb_buffer, &cur_buf[sizeof(usb_header_mmapped)], from_little_endian(urb_hdr.data_len));
}
urb.status = (int) urb_hdr.status;
urb.actual_length = (int) urb_hdr.urb_len;
urb.status = (int) from_little_endian(urb_hdr.status);
urb.actual_length = (int) from_little_endian(urb_hdr.urb_len);

/* Does this need further handling? */
assert(urb_hdr.start_frame == 0);
urb.start_frame = (int) urb_hdr.start_frame;
assert(from_little_endian(urb_hdr.start_frame) == 0);
urb.start_frame = (int) from_little_endian(urb_hdr.start_frame);

last_pkt_time_ms = urb_hdr.ts_sec * 1000 + urb_hdr.ts_usec / 1000;
last_pkt_time_ms = from_little_endian(urb_hdr.ts_sec) * 1000 + from_little_endian(urb_hdr).ts_usec / 1000;

return urb_info;
}
Expand Down

0 comments on commit 671e399

Please sign in to comment.