VirtualBox

source: vbox/trunk/src/VBox/Devices/USB/VUSBSnifferPcapNg.cpp@ 101556

Last change on this file since 101556 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.5 KB
Line 
1/* $Id: VUSBSnifferPcapNg.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * Virtual USB Sniffer facility - PCAP-NG format writer.
4 */
5
6/*
7 * Copyright (C) 2014-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_DRV_VUSB
33#include <VBox/log.h>
34#include <iprt/buildconfig.h>
35#include <iprt/errcore.h>
36#include <iprt/mem.h>
37#include <iprt/string.h>
38#include <iprt/system.h>
39#include <iprt/time.h>
40
41#include "VUSBSnifferInternal.h"
42
43
44/*********************************************************************************************************************************
45* Defined Constants And Macros *
46*********************************************************************************************************************************/
47
48/** DumpFile Section Header Block type. */
49#define DUMPFILE_SHB_BLOCK_TYPE UINT32_C(0x0a0d0d0a)
50/** The byte order magic value. */
51#define DUMPFILE_SHB_BYTE_ORDER_MAGIC UINT32_C(0x1a2b3c4d)
52/** Current major version. */
53#define DUMPFILE_SHB_VERSION_MAJOR UINT16_C(1)
54/** Current minor version. */
55#define DUMPFILE_SHB_VERSION_MINOR UINT16_C(0)
56
57/** Block type for the interface descriptor block. */
58#define DUMPFILE_IDB_BLOCK_TYPE UINT32_C(0x00000001)
59/** USB link type. */
60#define DUMPFILE_IDB_LINK_TYPE_USB_LINUX UINT16_C(189)
61#define DUMPFILE_IDB_LINK_TYPE_USB_LINUX_MMAPED UINT16_C(220)
62
63/** Block type for an enhanced packet block. */
64#define DUMPFILE_EPB_BLOCK_TYPE UINT32_C(0x00000006)
65
66/** USB packet event types. */
67#define DUMPFILE_USB_EVENT_TYPE_SUBMIT ('S')
68#define DUMPFILE_USB_EVENT_TYPE_COMPLETE ('C')
69#define DUMPFILE_USB_EVENT_TYPE_ERROR ('E')
70
71#define DUMPFILE_OPTION_CODE_END UINT16_C(0)
72#define DUMPFILE_OPTION_CODE_COMMENT UINT16_C(1)
73
74#define DUMPFILE_OPTION_CODE_HARDWARE UINT16_C(2)
75#define DUMPFILE_OPTION_CODE_OS UINT16_C(3)
76#define DUMPFILE_OPTION_CODE_USERAPP UINT16_C(4)
77
78#define DUMPFILE_IDB_OPTION_TS_RESOLUTION UINT16_C(9)
79
80
81/*********************************************************************************************************************************
82* DumpFile format structures *
83*********************************************************************************************************************************/
84
85/**
86 * DumpFile Block header.
87 */
88typedef struct DumpFileBlockHdr
89{
90 /** Block type. */
91 uint32_t u32BlockType;
92 /** Block total length. */
93 uint32_t u32BlockTotalLength;
94} DumpFileBlockHdr;
95/** Pointer to a block header. */
96typedef DumpFileBlockHdr *PDumpFileBlockHdr;
97
98/**
99 * DumpFile Option header.
100 */
101typedef struct DumpFileOptionHdr
102{
103 /** Option code. */
104 uint16_t u16OptionCode;
105 /** Block total length. */
106 uint16_t u16OptionLength;
107} DumpFileOptionHdr;
108/** Pointer to a option header. */
109typedef DumpFileOptionHdr *PDumpFileOptionHdr;
110
111/**
112 * DumpFile Section Header Block.
113 */
114typedef struct DumpFileShb
115{
116 /** Block header. */
117 DumpFileBlockHdr Hdr;
118 /** Byte order magic. */
119 uint32_t u32ByteOrderMagic;
120 /** Major version. */
121 uint16_t u16VersionMajor;
122 /** Minor version. */
123 uint16_t u16VersionMinor;
124 /** Section length. */
125 uint64_t u64SectionLength;
126} DumpFileShb;
127/** Pointer to a Section Header Block. */
128typedef DumpFileShb *PDumpFileShb;
129
130/**
131 * DumpFile Interface description block.
132 */
133typedef struct DumpFileIdb
134{
135 /** Block header. */
136 DumpFileBlockHdr Hdr;
137 /** Link type. */
138 uint16_t u16LinkType;
139 /** Reserved. */
140 uint16_t u16Reserved;
141 /** Maximum number of bytes dumped from each packet. */
142 uint32_t u32SnapLen;
143} DumpFileIdb;
144/** Pointer to an Interface description block. */
145typedef DumpFileIdb *PDumpFileIdb;
146
147/**
148 * DumpFile Enhanced packet block.
149 */
150typedef struct DumpFileEpb
151{
152 /** Block header. */
153 DumpFileBlockHdr Hdr;
154 /** Interface ID. */
155 uint32_t u32InterfaceId;
156 /** Timestamp (high). */
157 uint32_t u32TimestampHigh;
158 /** Timestamp (low). */
159 uint32_t u32TimestampLow;
160 /** Captured packet length. */
161 uint32_t u32CapturedLen;
162 /** Original packet length. */
163 uint32_t u32PacketLen;
164} DumpFileEpb;
165/** Pointer to an Enhanced packet block. */
166typedef DumpFileEpb *PDumpFileEpb;
167
168/**
169 * USB setup URB data.
170 */
171typedef struct DumpFileUsbSetup
172{
173 uint8_t bmRequestType;
174 uint8_t bRequest;
175 uint16_t wValue;
176 uint16_t wIndex;
177 uint16_t wLength;
178} DumpFileUsbSetup;
179typedef DumpFileUsbSetup *PDumpFileUsbSetup;
180
181/**
182 * USB Isochronous data.
183 */
184typedef struct DumpFileIsoRec
185{
186 int32_t i32ErrorCount;
187 int32_t i32NumDesc;
188} DumpFileIsoRec;
189typedef DumpFileIsoRec *PDumpFileIsoRec;
190
191/**
192 * USB packet header (Linux mmapped variant).
193 */
194typedef struct DumpFileUsbHeaderLnxMmapped
195{
196 /** Packet Id. */
197 uint64_t u64Id;
198 /** Event type. */
199 uint8_t u8EventType;
200 /** Transfer type. */
201 uint8_t u8TransferType;
202 /** Endpoint number. */
203 uint8_t u8EndpointNumber;
204 /** Device address. */
205 uint8_t u8DeviceAddress;
206 /** Bus id. */
207 uint16_t u16BusId;
208 /** Setup flag != 0 if the URB setup header is not present. */
209 uint8_t u8SetupFlag;
210 /** Data present flag != 0 if the URB data is not present. */
211 uint8_t u8DataFlag;
212 /** Timestamp (second part). */
213 uint64_t u64TimestampSec;
214 /** Timestamp (us part). */
215 uint32_t u32TimestampUSec;
216 /** Status. */
217 int32_t i32Status;
218 /** URB length. */
219 uint32_t u32UrbLength;
220 /** Recorded data length. */
221 uint32_t u32DataLength;
222 /** Union of data for different URB types. */
223 union
224 {
225 DumpFileUsbSetup UsbSetup;
226 DumpFileIsoRec IsoRec;
227 } u;
228 int32_t i32Interval;
229 int32_t i32StartFrame;
230 /** Copy of transfer flags. */
231 uint32_t u32XferFlags;
232 /** Number of isochronous descriptors. */
233 uint32_t u32NumDesc;
234} DumpFileUsbHeaderLnxMmapped;
235/** Pointer to a USB packet header. */
236typedef DumpFileUsbHeaderLnxMmapped *PDumpFileUsbHeaderLnxMmapped;
237
238AssertCompileSize(DumpFileUsbHeaderLnxMmapped, 64);
239
240/**
241 * USB packet isochronous descriptor.
242 */
243typedef struct DumpFileUsbIsoDesc
244{
245 int32_t i32Status;
246 uint32_t u32Offset;
247 uint32_t u32Len;
248 uint8_t au8Padding[4];
249} DumpFileUsbIsoDesc;
250typedef DumpFileUsbIsoDesc *PDumpFileUsbIsoDesc;
251
252
253/*********************************************************************************************************************************
254* Structures and Typedefs *
255*********************************************************************************************************************************/
256
257/**
258 * The internal VUSB sniffer state.
259 */
260typedef struct VUSBSNIFFERFMTINT
261{
262 /** Stream handle. */
263 PVUSBSNIFFERSTRM pStrm;
264 /** Current size of the block being written. */
265 uint32_t cbBlockCur;
266 /** Maximum size allocated for the block. */
267 uint32_t cbBlockMax;
268 /** Current block header. */
269 PDumpFileBlockHdr pBlockHdr;
270 /** Pointer to the block data which will be written on commit. */
271 uint8_t *pbBlockData;
272} VUSBSNIFFERFMTINT;
273
274
275/*********************************************************************************************************************************
276* Static Variables *
277*********************************************************************************************************************************/
278
279/**
280 * Supported file extensions.
281 */
282static const char *s_apszFileExts[] =
283{
284 "pcap",
285 "pcapng",
286 NULL
287};
288
289
290/*********************************************************************************************************************************
291* Internal Functions *
292*********************************************************************************************************************************/
293
294/**
295 * Allocates additional space for the block.
296 *
297 * @returns Pointer to the new unused space or NULL if out of memory.
298 * @param pThis The VUSB sniffer instance.
299 * @param cbAdditional The additional memory requested.
300 */
301static void *vusbSnifferBlockAllocSpace(PVUSBSNIFFERFMTINT pThis, uint32_t cbAdditional)
302{
303 /* Fast path where we have enough memory allocated. */
304 if (pThis->cbBlockCur + cbAdditional <= pThis->cbBlockMax)
305 {
306 void *pv = pThis->pbBlockData + pThis->cbBlockCur;
307 pThis->cbBlockCur += cbAdditional;
308 return pv;
309 }
310
311 /* Allocate additional memory. */
312 uint32_t cbNew = pThis->cbBlockCur + cbAdditional;
313 uint8_t *pbDataNew = (uint8_t *)RTMemRealloc(pThis->pbBlockData, cbNew);
314 if (pbDataNew)
315 {
316 pThis->pbBlockData = pbDataNew;
317 pThis->pBlockHdr = (PDumpFileBlockHdr)pbDataNew;
318
319 void *pv = pThis->pbBlockData + pThis->cbBlockCur;
320 pThis->cbBlockCur = cbNew;
321 pThis->cbBlockMax = cbNew;
322 return pv;
323 }
324
325 return NULL;
326}
327
328/**
329 * Adds new data to the current block.
330 *
331 * @returns VBox status code.
332 * @param pThis The VUSB sniffer instance.
333 * @param pvData The data to add.
334 * @param cbData Amount of data to add.
335 */
336static int vusbSnifferBlockAddData(PVUSBSNIFFERFMTINT pThis, const void *pvData, uint32_t cbData)
337{
338 int rc = VINF_SUCCESS;
339
340 Assert(pThis->cbBlockCur);
341 AssertPtr(pThis->pBlockHdr);
342
343 void *pv = vusbSnifferBlockAllocSpace(pThis, cbData);
344 if (pv)
345 memcpy(pv, pvData, cbData);
346 else
347 rc = VERR_NO_MEMORY;
348
349 return rc;
350}
351
352/**
353 * Aligns the current block data to a 32bit boundary.
354 *
355 * @returns VBox status code.
356 * @param pThis The VUSB sniffer instance.
357 */
358static int vusbSnifferBlockAlign(PVUSBSNIFFERFMTINT pThis)
359{
360 int rc = VINF_SUCCESS;
361
362 Assert(pThis->cbBlockCur);
363
364 /* Pad to 32bits. */
365 uint8_t abPad[3] = { 0 };
366 uint32_t cbPad = RT_ALIGN_32(pThis->cbBlockCur, 4) - pThis->cbBlockCur;
367
368 Assert(cbPad <= 3);
369 if (cbPad)
370 rc = vusbSnifferBlockAddData(pThis, abPad, cbPad);
371
372 return rc;
373}
374
375/**
376 * Commits the current block to the capture file.
377 *
378 * @returns VBox status code.
379 * @param pThis The VUSB sniffer instance.
380 */
381static int vusbSnifferBlockCommit(PVUSBSNIFFERFMTINT pThis)
382{
383 int rc = VINF_SUCCESS;
384
385 AssertPtr(pThis->pBlockHdr);
386
387 rc = vusbSnifferBlockAlign(pThis);
388 if (RT_SUCCESS(rc))
389 {
390 /* Update the block total length field. */
391 uint32_t *pcbTotalLength = (uint32_t *)vusbSnifferBlockAllocSpace(pThis, 4);
392 if (pcbTotalLength)
393 {
394 *pcbTotalLength = pThis->cbBlockCur;
395 pThis->pBlockHdr->u32BlockTotalLength = pThis->cbBlockCur;
396
397 /* Write the data. */
398 rc = pThis->pStrm->pfnWrite(pThis->pStrm, pThis->pbBlockData, pThis->cbBlockCur);
399 pThis->cbBlockCur = 0;
400 pThis->pBlockHdr = NULL;
401 }
402 else
403 rc = VERR_NO_MEMORY;
404 }
405
406 return rc;
407}
408
409/**
410 * Starts a new block for capturing.
411 *
412 * @returns VBox status code.
413 * @param pThis The VUSB sniffer instance.
414 * @param pBlockHdr Pointer to the block header for the new block.
415 * @param cbData Amount of data added with this block.
416 */
417static int vusbSnifferBlockNew(PVUSBSNIFFERFMTINT pThis, PDumpFileBlockHdr pBlockHdr, uint32_t cbData)
418{
419 int rc = VINF_SUCCESS;
420
421 /* Validate we don't get called while another block is active. */
422 Assert(!pThis->cbBlockCur);
423 Assert(!pThis->pBlockHdr);
424 pThis->pBlockHdr = (PDumpFileBlockHdr)vusbSnifferBlockAllocSpace(pThis, cbData);
425 if (pThis->pBlockHdr)
426 memcpy(pThis->pBlockHdr, pBlockHdr, cbData);
427 else
428 rc = VERR_NO_MEMORY;
429
430 return rc;
431}
432
433/**
434 * Add a new option to the current block.
435 *
436 * @returns VBox status code.
437 * @param pThis The VUSB sniffer instance.
438 * @param u16OptionCode The option code identifying the type of option.
439 * @param pvOption Raw data for the option.
440 * @param cbOption Size of the optiob data.
441 */
442static int vusbSnifferAddOption(PVUSBSNIFFERFMTINT pThis, uint16_t u16OptionCode, const void *pvOption, size_t cbOption)
443{
444 AssertStmt((uint16_t)cbOption == cbOption, cbOption = UINT16_MAX);
445 DumpFileOptionHdr OptHdr;
446 OptHdr.u16OptionCode = u16OptionCode;
447 OptHdr.u16OptionLength = (uint16_t)cbOption;
448 int rc = vusbSnifferBlockAddData(pThis, &OptHdr, sizeof(OptHdr));
449 if ( RT_SUCCESS(rc)
450 && u16OptionCode != DUMPFILE_OPTION_CODE_END
451 && cbOption != 0)
452 {
453 rc = vusbSnifferBlockAddData(pThis, pvOption, (uint16_t)cbOption);
454 if (RT_SUCCESS(rc))
455 rc = vusbSnifferBlockAlign(pThis);
456 }
457
458 return rc;
459}
460
461
462/** @interface_method_impl{VUSBSNIFFERFMT,pfnInit} */
463static DECLCALLBACK(int) vusbSnifferFmtPcapNgInit(PVUSBSNIFFERFMTINT pThis, PVUSBSNIFFERSTRM pStrm)
464{
465 pThis->pStrm = pStrm;
466 pThis->cbBlockCur = 0;
467 pThis->cbBlockMax = 0;
468 pThis->pbBlockData = NULL;
469
470 /* Write header and link type blocks. */
471 DumpFileShb Shb;
472
473 Shb.Hdr.u32BlockType = DUMPFILE_SHB_BLOCK_TYPE;
474 Shb.Hdr.u32BlockTotalLength = 0; /* Filled out by lower layer. */
475 Shb.u32ByteOrderMagic = DUMPFILE_SHB_BYTE_ORDER_MAGIC;
476 Shb.u16VersionMajor = DUMPFILE_SHB_VERSION_MAJOR;
477 Shb.u16VersionMinor = DUMPFILE_SHB_VERSION_MINOR;
478 Shb.u64SectionLength = UINT64_C(0xffffffffffffffff); /* -1 */
479
480 /* Write the blocks. */
481 int rc = vusbSnifferBlockNew(pThis, &Shb.Hdr, sizeof(Shb));
482 if (RT_SUCCESS(rc))
483 {
484 const char *pszOpt = RTBldCfgTargetDotArch();
485 rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_HARDWARE, pszOpt, strlen(pszOpt) + 1);
486 }
487
488 if (RT_SUCCESS(rc))
489 {
490 char szTmp[512];
491 size_t cbTmp = sizeof(szTmp);
492
493 RT_ZERO(szTmp);
494
495 /* Build the OS code. */
496 rc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, cbTmp);
497 if (RT_SUCCESS(rc))
498 {
499 size_t cb = strlen(szTmp);
500
501 szTmp[cb] = ' ';
502 rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, &szTmp[cb + 1], cbTmp - (cb + 1));
503 if (RT_SUCCESS(rc))
504 {
505 cb = strlen(szTmp);
506 szTmp[cb] = ' ';
507 rc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, &szTmp[cb + 1], cbTmp - (cb + 1));
508 }
509 }
510
511 if (RT_SUCCESS(rc) || rc == VERR_BUFFER_OVERFLOW)
512 rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_OS, szTmp, strlen(szTmp) + 1);
513 else
514 rc = VINF_SUCCESS; /* Skip OS code if building the string failed. */
515 }
516
517 if (RT_SUCCESS(rc))
518 {
519 /** @todo Add product info. */
520 }
521
522 if (RT_SUCCESS(rc))
523 rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_END, NULL, 0);
524 if (RT_SUCCESS(rc))
525 rc = vusbSnifferBlockCommit(pThis);
526
527 /* Write Interface descriptor block. */
528 if (RT_SUCCESS(rc))
529 {
530 DumpFileIdb Idb;
531
532 Idb.Hdr.u32BlockType = DUMPFILE_IDB_BLOCK_TYPE;
533 Idb.Hdr.u32BlockTotalLength = 0; /* Filled out by lower layer. */
534 Idb.u16LinkType = DUMPFILE_IDB_LINK_TYPE_USB_LINUX_MMAPED;
535 Idb.u16Reserved = 0;
536 Idb.u32SnapLen = UINT32_C(0xffffffff);
537
538 rc = vusbSnifferBlockNew(pThis, &Idb.Hdr, sizeof(Idb));
539 if (RT_SUCCESS(rc))
540 {
541 uint8_t u8TsResolution = 9; /* Nano second resolution. */
542 /* Add timestamp resolution option. */
543 rc = vusbSnifferAddOption(pThis, DUMPFILE_IDB_OPTION_TS_RESOLUTION,
544 &u8TsResolution, sizeof(u8TsResolution));
545 }
546 if (RT_SUCCESS(rc))
547 rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_END, NULL, 0);
548 if (RT_SUCCESS(rc))
549 rc = vusbSnifferBlockCommit(pThis);
550 }
551
552 if ( RT_FAILURE(rc)
553 && pThis->pbBlockData)
554 RTMemFree(pThis->pbBlockData);
555
556 return rc;
557}
558
559
560/** @interface_method_impl{VUSBSNIFFERFMT,pfnDestroy} */
561static DECLCALLBACK(void) vusbSnifferFmtPcapNgDestroy(PVUSBSNIFFERFMTINT pThis)
562{
563 if (pThis->pbBlockData)
564 RTMemFree(pThis->pbBlockData);
565}
566
567
568/** @interface_method_impl{VUSBSNIFFERFMT,pfnRecordEvent} */
569static DECLCALLBACK(int) vusbSnifferFmtPcapNgRecordEvent(PVUSBSNIFFERFMTINT pThis, PVUSBURB pUrb, VUSBSNIFFEREVENT enmEvent)
570{
571 DumpFileEpb Epb;
572 DumpFileUsbHeaderLnxMmapped UsbHdr;
573 uint32_t cbCapturedLength = sizeof(UsbHdr);
574 uint8_t *pbData = NULL;
575
576 RTTIMESPEC TimeNow;
577 RTTimeNow(&TimeNow);
578 uint64_t u64TimestampEvent = RTTimeSpecGetNano(&TimeNow);
579
580 /* Start with the enhanced packet block. */
581 Epb.Hdr.u32BlockType = DUMPFILE_EPB_BLOCK_TYPE;
582 Epb.Hdr.u32BlockTotalLength = 0;
583 Epb.u32InterfaceId = 0;
584 Epb.u32TimestampHigh = (u64TimestampEvent >> 32) & UINT32_C(0xffffffff);
585 Epb.u32TimestampLow = u64TimestampEvent & UINT32_C(0xffffffff);
586
587 UsbHdr.u64Id = (uintptr_t)pUrb; /** @todo check whether the pointer is a good ID. */
588 uint32_t cbUrbLength;
589 switch (enmEvent)
590 {
591 case VUSBSNIFFEREVENT_SUBMIT:
592 UsbHdr.u8EventType = DUMPFILE_USB_EVENT_TYPE_SUBMIT;
593 cbUrbLength = pUrb->cbData;
594 break;
595 case VUSBSNIFFEREVENT_COMPLETE:
596 UsbHdr.u8EventType = DUMPFILE_USB_EVENT_TYPE_COMPLETE;
597 cbUrbLength = pUrb->cbData;
598 break;
599 case VUSBSNIFFEREVENT_ERROR_SUBMIT:
600 case VUSBSNIFFEREVENT_ERROR_COMPLETE:
601 UsbHdr.u8EventType = DUMPFILE_USB_EVENT_TYPE_ERROR;
602 cbUrbLength = 0;
603 break;
604 default:
605 AssertMsgFailed(("Invalid event type %d\n", enmEvent));
606 cbUrbLength = 0;
607 }
608 uint32_t cbDataLength = cbUrbLength;
609 pbData = &pUrb->abData[0];
610
611 uint32_t cIsocPkts = 0;
612 switch (pUrb->enmType)
613 {
614 case VUSBXFERTYPE_ISOC:
615 {
616 int32_t cErrors = 0;
617
618 UsbHdr.u8TransferType = 0;
619 cIsocPkts = pUrb->cIsocPkts;
620 for (unsigned i = 0; i < cIsocPkts; i++)
621 if ( pUrb->aIsocPkts[i].enmStatus != VUSBSTATUS_OK
622 && pUrb->aIsocPkts[i].enmStatus != VUSBSTATUS_NOT_ACCESSED)
623 cErrors++;
624
625 UsbHdr.u.IsoRec.i32ErrorCount = cErrors;
626 UsbHdr.u.IsoRec.i32NumDesc = pUrb->cIsocPkts;
627 cbCapturedLength += cIsocPkts * sizeof(DumpFileUsbIsoDesc);
628 break;
629 }
630 case VUSBXFERTYPE_BULK:
631 UsbHdr.u8TransferType = 3;
632 break;
633 case VUSBXFERTYPE_INTR:
634 UsbHdr.u8TransferType = 1;
635 break;
636 case VUSBXFERTYPE_CTRL:
637 case VUSBXFERTYPE_MSG:
638 UsbHdr.u8TransferType = 2;
639 break;
640 default:
641 AssertMsgFailed(("invalid transfer type %d\n", pUrb->enmType));
642 }
643
644 if (pUrb->enmDir == VUSBDIRECTION_IN)
645 {
646 if (enmEvent == VUSBSNIFFEREVENT_SUBMIT)
647 cbDataLength = 0;
648 }
649 else if (pUrb->enmDir == VUSBDIRECTION_OUT)
650 {
651 if ( enmEvent == VUSBSNIFFEREVENT_COMPLETE
652 || pUrb->enmType == VUSBXFERTYPE_CTRL
653 || pUrb->enmType == VUSBXFERTYPE_MSG)
654 cbDataLength = 0;
655 }
656 else if ( pUrb->enmDir == VUSBDIRECTION_SETUP
657 && cbDataLength >= sizeof(VUSBSETUP))
658 cbDataLength -= sizeof(VUSBSETUP);
659
660 Epb.u32CapturedLen = cbCapturedLength + cbDataLength;
661 Epb.u32PacketLen = cbCapturedLength + cbUrbLength;
662
663 UsbHdr.u8EndpointNumber = pUrb->EndPt | (pUrb->enmDir == VUSBDIRECTION_IN ? 0x80 : 0x00);
664 UsbHdr.u8DeviceAddress = pUrb->DstAddress;
665 UsbHdr.u16BusId = 0;
666 UsbHdr.u8DataFlag = cbDataLength ? 0 : 1;
667 UsbHdr.u64TimestampSec = u64TimestampEvent / RT_NS_1SEC_64;
668 UsbHdr.u32TimestampUSec = u64TimestampEvent / RT_NS_1US_64 - UsbHdr.u64TimestampSec * RT_US_1SEC;
669 UsbHdr.i32Status = pUrb->enmStatus;
670 UsbHdr.u32UrbLength = cbUrbLength;
671 UsbHdr.u32DataLength = cbDataLength + cIsocPkts * sizeof(DumpFileUsbIsoDesc);
672 UsbHdr.i32Interval = 0;
673 UsbHdr.i32StartFrame = 0;
674 UsbHdr.u32XferFlags = 0;
675 UsbHdr.u32NumDesc = cIsocPkts;
676
677 if ( (pUrb->enmType == VUSBXFERTYPE_MSG || pUrb->enmType == VUSBXFERTYPE_CTRL)
678 && enmEvent == VUSBSNIFFEREVENT_SUBMIT)
679 {
680 PVUSBSETUP pSetup = (PVUSBSETUP)pUrb->abData;
681
682 UsbHdr.u.UsbSetup.bmRequestType = pSetup->bmRequestType;
683 UsbHdr.u.UsbSetup.bRequest = pSetup->bRequest;
684 UsbHdr.u.UsbSetup.wValue = pSetup->wValue;
685 UsbHdr.u.UsbSetup.wIndex = pSetup->wIndex;
686 UsbHdr.u.UsbSetup.wLength = pSetup->wLength;
687 UsbHdr.u8SetupFlag = 0;
688 }
689 else
690 UsbHdr.u8SetupFlag = '-'; /* Follow usbmon source here. */
691
692 /* Write the packet to the capture file. */
693 int rc = vusbSnifferBlockNew(pThis, &Epb.Hdr, sizeof(Epb));
694 if (RT_SUCCESS(rc))
695 rc = vusbSnifferBlockAddData(pThis, &UsbHdr, sizeof(UsbHdr));
696
697 /* Add Isochronous descriptors now. */
698 for (unsigned i = 0; i < cIsocPkts && RT_SUCCESS(rc); i++)
699 {
700 DumpFileUsbIsoDesc IsoDesc;
701 IsoDesc.i32Status = pUrb->aIsocPkts[i].enmStatus;
702 IsoDesc.u32Offset = pUrb->aIsocPkts[i].off;
703 IsoDesc.u32Len = pUrb->aIsocPkts[i].cb;
704 rc = vusbSnifferBlockAddData(pThis, &IsoDesc, sizeof(IsoDesc));
705 }
706
707 /* Record data. */
708 if ( RT_SUCCESS(rc)
709 && cbDataLength)
710 rc = vusbSnifferBlockAddData(pThis, pbData, cbDataLength);
711
712 if (RT_SUCCESS(rc))
713 rc = vusbSnifferAddOption(pThis, DUMPFILE_OPTION_CODE_END, NULL, 0);
714
715 if (RT_SUCCESS(rc))
716 rc = vusbSnifferBlockCommit(pThis);
717
718 return rc;
719}
720
721/**
722 * VUSB sniffer format writer.
723 */
724const VUSBSNIFFERFMT g_VUsbSnifferFmtPcapNg =
725{
726 /** szName */
727 "PCAPNG",
728 /** pszDesc */
729 "PCAP-NG format writer compatible with WireShark",
730 /** papszFileExts */
731 &s_apszFileExts[0],
732 /** cbFmt */
733 sizeof(VUSBSNIFFERFMTINT),
734 /** pfnInit */
735 vusbSnifferFmtPcapNgInit,
736 /** pfnDestroy */
737 vusbSnifferFmtPcapNgDestroy,
738 /** pfnRecordEvent */
739 vusbSnifferFmtPcapNgRecordEvent
740};
741
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette