VUSBSniffer.cpp revision 2b73ea648457a8fb1ec027d1eeea63f879dad507
/* $Id$ */
/** @file
* Virtual USB - Sniffer facility.
*/
/*
* Copyright (C) 2014 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DRV_VUSB
#include <iprt/buildconfig.h>
#include "VUSBSniffer.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** DumpFile Section Header Block type. */
/** The byte order magic value. */
/** Current major version. */
/** Current minor version. */
#define DUMPFILE_SHB_VERSION_MINOR UINT16_C(0)
/** Block type for the interface descriptor block. */
/** USB link type. */
/** Block type for an enhanced packet block. */
/** USB packet event types. */
#define DUMPFILE_USB_EVENT_TYPE_SUBMIT ('S')
#define DUMPFILE_USB_EVENT_TYPE_COMPLETE ('C')
#define DUMPFILE_USB_EVENT_TYPE_ERROR ('E')
#define DUMPFILE_OPTION_CODE_END UINT16_C(0)
/*******************************************************************************
* DumpFile format structures *
*******************************************************************************/
/**
* DumpFile Block header.
*/
typedef struct DumpFileBlockHdr
{
/** Block type. */
/** Block total length. */
/** Pointer to a block header. */
typedef DumpFileBlockHdr *PDumpFileBlockHdr;
/**
* DumpFile Option header.
*/
typedef struct DumpFileOptionHdr
{
/** Option code. */
/** Block total length. */
/** Pointer to a option header. */
typedef DumpFileOptionHdr *PDumpFileOptionHdr;
/**
* DumpFile Section Header Block.
*/
typedef struct DumpFileShb
{
/** Block header. */
/** Byte order magic. */
/** Major version. */
/** Minor version. */
/** Section length. */
} DumpFileShb;
/** Pointer to a Section Header Block. */
typedef DumpFileShb *PDumpFileShb;
/**
* DumpFile Interface description block.
*/
typedef struct DumpFileIdb
{
/** Block header. */
/** Link type. */
/** Reserved. */
/** Maximum number of bytes dumped from each packet. */
} DumpFileIdb;
/** Pointer to an Interface description block. */
typedef DumpFileIdb *PDumpFileIdb;
/**
* DumpFile Enhanced packet block.
*/
typedef struct DumpFileEpb
{
/** Block header. */
/** Interface ID. */
/** Timestamp (high). */
/** Timestamp (low). */
/** Captured packet length. */
/** Original packet length. */
} DumpFileEpb;
/** Pointer to an Enhanced packet block. */
typedef DumpFileEpb *PDumpFileEpb;
/**
* USB setup URB data.
*/
typedef struct DumpFileUsbSetup
{
typedef DumpFileUsbSetup *PDumpFileUsbSetup;
/**
* USB Isochronous data.
*/
typedef struct DumpFileIsoRec
{
typedef DumpFileIsoRec *PDumpFileIsoRec;
/**
* USB packet header (Linux mmapped variant).
*/
typedef struct DumpFileUsbHeaderLnxMmapped
{
/** Packet Id. */
/** Event type. */
/** Transfer type. */
/** Endpoint number. */
/** Device address. */
/** Bus id. */
/** Setup flag != 0 if the URB setup header is not present. */
/** Data present flag != 0 if the URB data is not present. */
/** Timestamp (second part). */
/** Timestamp (us part). */
/** Status. */
/** URB length. */
/** Recorded data length. */
/** Union of data for different URB types. */
union
{
} u;
/** Copy of transfer flags. */
/** Number of isochronous descriptors. */
/** Pointer to a USB packet header. */
/**
* USB packet isochronous descriptor.
*/
typedef struct DumpFileUsbIsoDesc
{
typedef DumpFileUsbIsoDesc *PDumpFileUsbIsoDesc;
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The internal VUSB sniffer state.
*/
typedef struct VUSBSNIFFERINT
{
/** The file handle to dump to. */
/** Current size of the block being written. */
/** Maximum size allocated for the block. */
/** Current block header. */
/** Pointer to the block data which will be written on commit. */
/** Pointer to the internal VUSB sniffer state. */
typedef VUSBSNIFFERINT *PVUSBSNIFFERINT;
/**
* Allocates additional space for the block.
*
* @returns Pointer to the new unused space or NULL if out of memory.
* @param pThis The VUSB sniffer instance.
* @param cbAdditional The additional memory requested.
*/
{
/* Fast path where we have enough memory allocated. */
{
return pv;
}
/* Allocate additional memory. */
if (pbDataNew)
{
return pv;
}
return NULL;
}
/**
* Commits the current block to the capture file.
*
* @returns VBox status code.
* @param pThis The VUSB sniffer instance.
*/
{
int rc = VINF_SUCCESS;
/* Update the block total length field. */
if (pcbTotalLength)
{
/* Write the data. */
pThis->cbBlockCur = 0;
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Starts a new block for capturing.
*
* @returns VBox status code.
* @param pThis The VUSB sniffer instance.
* @param pBlockHdr Pointer to the block header for the new block.
* @param cbData Amount of data added with this block.
*/
{
int rc = VINF_SUCCESS;
/* Validate we don't get called while another block is active. */
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Adds new data to the current block.
*
* @returns VBox status code.
* @param pThis The VUSB sniffer instance.
* @param pvData The data to add.
* @param cbData Amount of data to add.
*/
{
int rc = VINF_SUCCESS;
if (pv)
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Add a new option to the current block.
*
* @returns VBox status code.
* @param pThis The VUSB sniffer instance.
* @param u16OptionCode The option code identifying the type of option.
* @param pvOption Raw data for the option.
* @param cbOption Size of the optiob data.
*/
static int vusbSnifferAddOption(PVUSBSNIFFERINT pThis, uint16_t u16OptionCode, const void *pvOption, uint16_t cbOption)
{
int rc = VINF_SUCCESS;
if ( RT_SUCCESS(rc)
&& cbOption != 0)
{
if (RT_SUCCESS(rc))
{
/* Pad to 32bits. */
if (cbPad)
}
}
return rc;
}
const char *pszCaptureFilename, const char *pszDesc)
{
int rc = VINF_SUCCESS;
if (pThis)
{
pThis->cbBlockCur = 0;
pThis->cbBlockMax = 0;
rc = RTFileOpen(&pThis->hFile, pszCaptureFilename, RTFILE_O_DENY_NONE | RTFILE_O_CREATE | RTFILE_O_WRITE | RTFILE_O_READ);
if (RT_SUCCESS(rc))
{
/* Write header and link type blocks. */
/* Write the blocks. */
if (RT_SUCCESS(rc))
{
const char *pszOpt = RTBldCfgTargetDotArch();
}
if (RT_SUCCESS(rc))
{
char szTmp[512];
/* Build the OS code. */
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
}
}
else
}
if (RT_SUCCESS(rc))
{
/** @todo: Add product info. */
}
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
/* Write Interface descriptor block. */
if (RT_SUCCESS(rc))
{
Idb.u16Reserved = 0;
if (RT_SUCCESS(rc))
{
/* Add timestamp resolution option. */
&u8TsResolution, sizeof(u8TsResolution));
}
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
}
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Destroys the given VUSB sniffer instance.
*
* @returns nothing.
* @param hSniffer The sniffer instance to destroy.
*/
{
if (pThis->pbBlockData)
}
/**
* Records an VUSB event.
*
* @returns VBox status code.
* @param hSniffer The sniffer instance.
* @param pUrb The URB triggering the event.
* @param enmEvent The type of event to record.
*/
DECLHIDDEN(int) VUSBSnifferRecordEvent(VUSBSNIFFER hSniffer, PVUSBURB pUrb, VUSBSNIFFEREVENT enmEvent)
{
int rc = VINF_SUCCESS;
/* Start with the enhanced packet block. */
Epb.u32InterfaceId = 0;
switch (enmEvent)
{
case VUSBSNIFFEREVENT_SUBMIT:
break;
break;
break;
default:
}
{
case VUSBXFERTYPE_ISOC:
UsbHdr.u8TransferType = 0;
break;
case VUSBXFERTYPE_BULK:
break;
case VUSBXFERTYPE_INTR:
break;
case VUSBXFERTYPE_CTRL:
case VUSBXFERTYPE_MSG:
break;
default:
}
bool fRecordData = false;
&& enmEvent == VUSBSNIFFEREVENT_SUBMIT)
&& enmEvent == VUSBSNIFFEREVENT_COMPLETE)))
fRecordData = true;
UsbHdr.u8SetupFlag = pUrb->enmType == VUSBXFERTYPE_MSG || pUrb->enmType == VUSBXFERTYPE_CTRL ? 0 : 1;
UsbHdr.i32Interval = 0;
UsbHdr.i32StartFrame = 0;
UsbHdr.u32XferFlags = 0;
{
}
if (RT_SUCCESS(rc))
/* Add Isochronous descriptors now. */
{
}
/* Record data. */
if ( RT_SUCCESS(rc)
&& fRecordData)
if (RT_SUCCESS(rc))
return rc;
}