1 | /* $Id: VUSBSnifferVmx.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual USB Sniffer facility - VMX USBIO format.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2016-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/mem.h>
|
---|
35 | #include <iprt/buildconfig.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/system.h>
|
---|
38 | #include <iprt/time.h>
|
---|
39 |
|
---|
40 | #include "VUSBSnifferInternal.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Defined Constants And Macros *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 |
|
---|
47 |
|
---|
48 | /*********************************************************************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *********************************************************************************************************************************/
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * The internal VUSB sniffer state.
|
---|
54 | */
|
---|
55 | typedef struct VUSBSNIFFERFMTINT
|
---|
56 | {
|
---|
57 | /** Stream handle. */
|
---|
58 | PVUSBSNIFFERSTRM pStrm;
|
---|
59 | } VUSBSNIFFERFMTINT;
|
---|
60 |
|
---|
61 |
|
---|
62 | /*********************************************************************************************************************************
|
---|
63 | * Static Variables *
|
---|
64 | *********************************************************************************************************************************/
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Supported file extensions.
|
---|
68 | */
|
---|
69 | static const char *s_apszFileExts[] =
|
---|
70 | {
|
---|
71 | "vmx",
|
---|
72 | "vmware",
|
---|
73 | "usbio",
|
---|
74 | NULL
|
---|
75 | };
|
---|
76 |
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Month strings.
|
---|
80 | */
|
---|
81 | static const char *s_apszMonths[] =
|
---|
82 | {
|
---|
83 | "Jan",
|
---|
84 | "Feb",
|
---|
85 | "Mar",
|
---|
86 | "Apr",
|
---|
87 | "May",
|
---|
88 | "Jun",
|
---|
89 | "Jul",
|
---|
90 | "Aug",
|
---|
91 | "Sep",
|
---|
92 | "Oct",
|
---|
93 | "Nov",
|
---|
94 | "Dec"
|
---|
95 | };
|
---|
96 |
|
---|
97 |
|
---|
98 | /*********************************************************************************************************************************
|
---|
99 | * Internal Functions *
|
---|
100 | *********************************************************************************************************************************/
|
---|
101 |
|
---|
102 |
|
---|
103 | static int vusbSnifferFmtVmxLogData(PVUSBSNIFFERFMTINT pThis, PRTTIME pTime, uint8_t *pbBuf, size_t cbBuf)
|
---|
104 | {
|
---|
105 | int rc;
|
---|
106 | char szLineBuf[256];
|
---|
107 | size_t off = 0;
|
---|
108 |
|
---|
109 | do
|
---|
110 | {
|
---|
111 | size_t cch = RTStrPrintf(&szLineBuf[0], sizeof(szLineBuf),
|
---|
112 | "%s %02u %02u:%02u:%02u.%3.*u: vmx| USBIO: %03zx: %16.*Rhxs\n",
|
---|
113 | s_apszMonths[pTime->u8Month - 1], pTime->u8MonthDay,
|
---|
114 | pTime->u8Hour, pTime->u8Minute, pTime->u8Second, 3, pTime->u32Nanosecond,
|
---|
115 | off, RT_MIN(cbBuf - off, 16), pbBuf);
|
---|
116 | rc = pThis->pStrm->pfnWrite(pThis->pStrm, &szLineBuf[0], cch);
|
---|
117 | off += RT_MIN(cbBuf, 16);
|
---|
118 | pbBuf += RT_MIN(cbBuf, 16);
|
---|
119 | } while (RT_SUCCESS(rc) && off < cbBuf);
|
---|
120 |
|
---|
121 | return rc;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /** @interface_method_impl{VUSBSNIFFERFMT,pfnInit} */
|
---|
125 | static DECLCALLBACK(int) vusbSnifferFmtVmxInit(PVUSBSNIFFERFMTINT pThis, PVUSBSNIFFERSTRM pStrm)
|
---|
126 | {
|
---|
127 | pThis->pStrm = pStrm;
|
---|
128 | return VINF_SUCCESS;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /** @interface_method_impl{VUSBSNIFFERFMT,pfnDestroy} */
|
---|
133 | static DECLCALLBACK(void) vusbSnifferFmtVmxDestroy(PVUSBSNIFFERFMTINT pThis)
|
---|
134 | {
|
---|
135 | NOREF(pThis);
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | /** @interface_method_impl{VUSBSNIFFERFMT,pfnRecordEvent} */
|
---|
140 | static DECLCALLBACK(int) vusbSnifferFmtVmxRecordEvent(PVUSBSNIFFERFMTINT pThis, PVUSBURB pUrb, VUSBSNIFFEREVENT enmEvent)
|
---|
141 | {
|
---|
142 | RTTIMESPEC TimeNow;
|
---|
143 | RTTIME Time;
|
---|
144 | char szLineBuf[256];
|
---|
145 | const char *pszEvt = enmEvent == VUSBSNIFFEREVENT_SUBMIT ? "Down" : "Up";
|
---|
146 | uint8_t cIsocPkts = pUrb->enmType == VUSBXFERTYPE_ISOC ? pUrb->cIsocPkts : 0;
|
---|
147 |
|
---|
148 | if (pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
149 | return VINF_SUCCESS;
|
---|
150 |
|
---|
151 | RT_ZERO(szLineBuf);
|
---|
152 |
|
---|
153 | RTTimeNow(&TimeNow);
|
---|
154 | RTTimeExplode(&Time, &TimeNow);
|
---|
155 |
|
---|
156 | size_t cch = RTStrPrintf(&szLineBuf[0], sizeof(szLineBuf),
|
---|
157 | "%s %02u %02u:%02u:%02u.%3.*u: vmx| USBIO: %s dev=%u endpt=%x datalen=%u numPackets=%u status=%u 0\n",
|
---|
158 | s_apszMonths[Time.u8Month - 1], Time.u8MonthDay, Time.u8Hour, Time.u8Minute, Time.u8Second, 3, Time.u32Nanosecond,
|
---|
159 | pszEvt, pUrb->DstAddress, pUrb->EndPt | (pUrb->enmDir == VUSBDIRECTION_IN ? 0x80 : 0x00),
|
---|
160 | pUrb->cbData, cIsocPkts, pUrb->enmStatus);
|
---|
161 | int rc = pThis->pStrm->pfnWrite(pThis->pStrm, &szLineBuf[0], cch);
|
---|
162 | if (RT_SUCCESS(rc))
|
---|
163 | {
|
---|
164 | /* Log the data in the appropriate stage. */
|
---|
165 | if ( pUrb->enmType == VUSBXFERTYPE_CTRL
|
---|
166 | || pUrb->enmType == VUSBXFERTYPE_MSG)
|
---|
167 | {
|
---|
168 | if (enmEvent == VUSBSNIFFEREVENT_SUBMIT)
|
---|
169 | rc = vusbSnifferFmtVmxLogData(pThis, &Time, &pUrb->abData[0], sizeof(VUSBSETUP));
|
---|
170 | else if (enmEvent == VUSBSNIFFEREVENT_COMPLETE)
|
---|
171 | {
|
---|
172 | rc = vusbSnifferFmtVmxLogData(pThis, &Time, &pUrb->abData[0], sizeof(VUSBSETUP));
|
---|
173 | if ( RT_SUCCESS(rc)
|
---|
174 | && pUrb->cbData > sizeof(VUSBSETUP))
|
---|
175 | rc = vusbSnifferFmtVmxLogData(pThis, &Time, &pUrb->abData[sizeof(VUSBSETUP)], pUrb->cbData - sizeof(VUSBSETUP));
|
---|
176 | }
|
---|
177 | }
|
---|
178 | else
|
---|
179 | {
|
---|
180 | if ( enmEvent == VUSBSNIFFEREVENT_SUBMIT
|
---|
181 | && pUrb->enmDir == VUSBDIRECTION_OUT)
|
---|
182 | rc = vusbSnifferFmtVmxLogData(pThis, &Time, &pUrb->abData[0], pUrb->cbData);
|
---|
183 | else if ( enmEvent == VUSBSNIFFEREVENT_COMPLETE
|
---|
184 | && pUrb->enmDir == VUSBDIRECTION_IN)
|
---|
185 | rc = vusbSnifferFmtVmxLogData(pThis, &Time, &pUrb->abData[0], pUrb->cbData);
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | return rc;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * VUSB sniffer format writer.
|
---|
194 | */
|
---|
195 | const VUSBSNIFFERFMT g_VUsbSnifferFmtVmx =
|
---|
196 | {
|
---|
197 | /** szName */
|
---|
198 | "VMX",
|
---|
199 | /** pszDesc */
|
---|
200 | "VMX log format writer supported by vusb-analyzer: http://vusb-analyzer.sourceforge.net",
|
---|
201 | /** papszFileExts */
|
---|
202 | &s_apszFileExts[0],
|
---|
203 | /** cbFmt */
|
---|
204 | sizeof(VUSBSNIFFERFMTINT),
|
---|
205 | /** pfnInit */
|
---|
206 | vusbSnifferFmtVmxInit,
|
---|
207 | /** pfnDestroy */
|
---|
208 | vusbSnifferFmtVmxDestroy,
|
---|
209 | /** pfnRecordEvent */
|
---|
210 | vusbSnifferFmtVmxRecordEvent
|
---|
211 | };
|
---|
212 |
|
---|