Changeset 95107 in vbox for trunk/src/VBox/Frontends/VBoxIntnetPcap
- Timestamp:
- May 25, 2022 8:19:59 PM (3 years ago)
- svn:sync-xref-src-repo-rev:
- 151587
- Location:
- trunk/src/VBox/Frontends/VBoxIntnetPcap
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VBoxIntnetPcap/Makefile.kmk
r93115 r95107 2 2 ## @file 3 3 # Sub-makefile for the VBoxIntnetPcap 4 # 4 5 5 6 # … … 19 20 20 21 PROGRAMS += VBoxIntnetPcap 21 22 VBoxIntnetPcap_TEMPLATE = VBOXR3EXE 23 24 VBoxIntnetPcap_SOURCES = # 25 VBoxIntnetPcap_SOURCES += VBoxIntnetPcap.cpp 26 VBoxIntnetPcap_SOURCES += ../../NetworkServices/NetLib/IntNetIf.cpp 27 VBoxIntnetPcap_SOURCES += ../../Devices/Network/Pcap.cpp 28 29 VBoxIntnetPcap_INCS += ../../NetworkServices/NetLib 30 VBoxIntnetPcap_INCS += ../../Devices/Network 31 32 VBoxIntnetPcap_LDFLAGS.win = /SUBSYSTEM:windows 33 34 VBoxIntnetPcap_LIBS += $(LIB_RUNTIME) 22 VBoxIntnetPcap_TEMPLATE := VBOXR3EXE 23 VBoxIntnetPcap_INCS := \ 24 ../../NetworkServices/NetLib \ 25 ../../Devices/Network 26 VBoxIntnetPcap_SOURCES := \ 27 VBoxIntnetPcap.cpp \ 28 ../../NetworkServices/NetLib/IntNetIf.cpp \ 29 ../../Devices/Network/Pcap.cpp 30 VBoxIntnetPcap_LIBS = $(LIB_RUNTIME) 35 31 # VBoxIntnetPcap_LIBS.solaris += socket nsl # XXX: when we add rpcapd support 36 32 37 33 include $(FILE_KBUILD_SUB_FOOTER) 34 -
trunk/src/VBox/Frontends/VBoxIntnetPcap/VBoxIntnetPcap.cpp
r93115 r95107 16 16 */ 17 17 18 19 /********************************************************************************************************************************* 20 * Header Files * 21 *********************************************************************************************************************************/ 18 22 #include "IntNetIf.h" 19 23 #include "Pcap.h" … … 30 34 #include <VBox/version.h> 31 35 32 void captureFrame(void *pvUser, void *pvFrame, uint32_t cbFrame); 33 void captureGSO(void *pvUser, PCPDMNETWORKGSO pcGso, uint32_t cbFrame); 34 void checkCaptureLimit(); 35 36 IntNetIf g_net; 37 PRTSTREAM g_pStrmOut; 38 uint64_t g_StartNanoTS; 39 bool g_fPacketBuffered; 40 uint64_t g_u64Count; 41 size_t g_cbSnapLen; 42 43 44 RTGETOPTDEF g_aGetOptDef[] = 36 37 /********************************************************************************************************************************* 38 * Internal Functions * 39 *********************************************************************************************************************************/ 40 static DECLCALLBACK(void) captureFrame(void *pvUser, void *pvFrame, uint32_t cbFrame); 41 static DECLCALLBACK(void) captureGSO(void *pvUser, PCPDMNETWORKGSO pcGso, uint32_t cbFrame); 42 43 44 /********************************************************************************************************************************* 45 * Global Variables * 46 *********************************************************************************************************************************/ 47 static IntNetIf g_net; 48 static PRTSTREAM g_pStrmOut; 49 static uint64_t g_StartNanoTS; 50 static bool g_fPacketBuffered; 51 static uint64_t g_cCountDown; 52 static size_t g_cbSnapLen = 0xffff; 53 54 static const RTGETOPTDEF g_aGetOptDef[] = 45 55 { 46 56 { "--count", 'c', RTGETOPT_REQ_UINT64 }, … … 55 65 main(int argc, char *argv[]) 56 66 { 57 int rc; 58 59 RTCString strNetworkName; 60 RTCString strPcapFile; 61 62 rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB); 67 int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB); 63 68 if (RT_FAILURE(rc)) 64 69 return RTMsgInitFailure(rc); 65 70 71 /* 72 * Parse options 73 */ 66 74 RTGETOPTSTATE State; 67 rc = RTGetOptInit(&State, argc, argv, 68 g_aGetOptDef, RT_ELEMENTS(g_aGetOptDef), 69 1, 0); 75 rc = RTGetOptInit(&State, argc, argv, g_aGetOptDef, RT_ELEMENTS(g_aGetOptDef), 1, 0); 76 AssertRC(rc); 77 78 const char *pszNetworkName = NULL; 79 const char *pszPcapFile = NULL; 70 80 71 81 int ch; … … 76 86 { 77 87 case 'c': /* --count */ 78 if (g_u64Count != 0)79 return RTMsgErrorExit(RTEXITCODE_SYNTAX,80 "multiple --count options");81 88 if (Val.u64 == 0) 82 return RTMsgErrorExit(RTEXITCODE_SYNTAX, 83 "--count must be greater than zero"); 84 g_u64Count = Val.u64; 89 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--count must be greater than zero"); 90 g_cCountDown = Val.u64; 85 91 break; 86 92 87 93 case 'i': /* --network */ 88 if (strNetworkName.isNotEmpty())89 return RTMsgErrorExit(RTEXITCODE_SYNTAX,90 "multiple --network options");91 94 if (Val.psz[0] == '\0') 92 return RTMsgErrorExit(RTEXITCODE_SYNTAX, 93 "empty --network option"); 94 strNetworkName = Val.psz; 95 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "empty --network option"); 96 pszNetworkName = Val.psz; 95 97 break; 96 98 97 99 case 's': /* --snaplen */ 98 if (g_cbSnapLen != 0)99 return RTMsgErrorExit(RTEXITCODE_SYNTAX,100 "multiple --snaplen options");101 100 if (Val.u32 == 0) 102 return RTMsgErrorExit(RTEXITCODE_SYNTAX, 103 "--snaplen must be greater than zero"); 101 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--snaplen must be greater than zero"); 104 102 g_cbSnapLen = Val.u32; 105 103 break; … … 110 108 111 109 case 'w': /* --write */ 112 if (strPcapFile.isNotEmpty())113 return RTMsgErrorExit(RTEXITCODE_SYNTAX,114 "multiple --write options");115 110 if (Val.psz[0] == '\0') 116 return RTMsgErrorExit(RTEXITCODE_SYNTAX, 117 "empty --write option"); 118 strPcapFile = Val.psz; 111 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "empty --write option"); 112 pszPcapFile = Val.psz; 119 113 break; 120 114 … … 142 136 return RTEXITCODE_SUCCESS; 143 137 138 default: 144 139 case VINF_GETOPT_NOT_OPTION: 145 return RTMsgErrorExit(RTEXITCODE_SYNTAX,146 "unexpected non-option argument");147 148 default:149 140 return RTGetOptPrintError(ch, &Val); 150 141 } 151 142 } 152 153 if (strNetworkName.isEmpty()) 154 return RTMsgErrorExit(RTEXITCODE_SYNTAX, 155 "missing --network option"); 156 157 if (strPcapFile.isEmpty()) 158 return RTMsgErrorExit(RTEXITCODE_SYNTAX, 159 "missing --write option"); 160 161 if (g_cbSnapLen == 0) 162 g_cbSnapLen = 0xffff; 163 164 165 if (strPcapFile == "-") 166 { 143 if (!pszNetworkName) 144 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No network specified. Please use the --network option"); 145 if (!pszPcapFile) 146 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No output file specified. Please use the --write option"); 147 148 /* 149 * Open the output file. 150 */ 151 if (strcmp(pszPcapFile, "-") == 0) 167 152 g_pStrmOut = g_pStdOut; 168 }169 153 else 170 154 { 171 rc = RTStrmOpen( strPcapFile.c_str(), "wb", &g_pStrmOut);155 rc = RTStrmOpen(pszPcapFile, "wb", &g_pStrmOut); 172 156 if (RT_FAILURE(rc)) 173 return RTMsgErrorExit(RTEXITCODE_FAILURE, 174 "%s: %Rrf", strPcapFile.c_str(), rc); 175 } 176 157 return RTMsgErrorExitFailure("%s: %Rrf", pszPcapFile, rc); 158 } 159 160 /* 161 * Configure the snooper. 162 */ 177 163 g_net.setInputCallback(captureFrame, NULL); 178 164 g_net.setInputGSOCallback(captureGSO, NULL); … … 183 169 * typo... beware. 184 170 */ 185 rc = g_net.init(strNetworkName); 186 if (RT_FAILURE(rc)) 187 return RTMsgErrorExit(RTEXITCODE_FAILURE, 188 "%s: %Rrf", strNetworkName.c_str(), rc); 171 rc = g_net.init(pszNetworkName); 172 if (RT_FAILURE(rc)) 173 return RTMsgErrorExitFailure("%s: %Rrf", pszNetworkName, rc); 189 174 190 175 rc = g_net.ifSetPromiscuous(); 191 176 if (RT_FAILURE(rc)) 192 return RTMsgErrorExit(RTEXITCODE_FAILURE, 193 "%s: failed to set promiscuous mode: %Rrf", 194 strNetworkName.c_str(), rc); 195 177 return RTMsgErrorExitFailure("%s: failed to set promiscuous mode: %Rrf", pszNetworkName, rc); 178 179 /* 180 * Snoop traffic. 181 */ 196 182 g_StartNanoTS = RTTimeNanoTS(); 197 183 rc = PcapStreamHdr(g_pStrmOut, g_StartNanoTS); 198 184 if (RT_FAILURE(rc)) 199 return RTMsgErrorExit(RTEXITCODE_FAILURE, 200 "write: %Rrf", rc); 185 return RTMsgErrorExitFailure("write: %Rrf", rc); 201 186 if (g_fPacketBuffered) 202 187 RTStrmFlush(g_pStrmOut); 203 188 204 189 g_net.ifPump(); 205 RTStrmClose(g_pStrmOut); 206 207 return RTEXITCODE_SUCCESS; 208 } 209 210 211 void 212 checkCaptureLimit() 213 { 214 if (g_u64Count > 0) 215 { 216 if (g_u64Count-- == 1) 190 191 RTEXITCODE rcExit = RT_SUCCESS(RTStrmError(g_pStrmOut)) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE; 192 rc = RTStrmClose(g_pStrmOut); 193 if (RT_FAILURE(rc)) 194 rcExit = RTMsgErrorExitFailure("close: %Rrf", rc); 195 return rcExit; 196 } 197 198 199 static void checkCaptureLimit(void) 200 { 201 if (g_cCountDown > 0) 202 { 203 if (g_cCountDown-- == 1) 217 204 g_net.ifAbort(); 218 205 } … … 220 207 221 208 222 void 223 captureFrame(void *pvUser, void *pvFrame, uint32_t cbFrame) 224 { 225 int rc; 226 209 static DECLCALLBACK(void) captureFrame(void *pvUser, void *pvFrame, uint32_t cbFrame) 210 { 227 211 RT_NOREF(pvUser); 228 212 229 rc = PcapStreamFrame(g_pStrmOut, g_StartNanoTS,230 pvFrame, cbFrame, g_cbSnapLen);231 if (RT_FAILURE(rc)){213 int rc = PcapStreamFrame(g_pStrmOut, g_StartNanoTS, pvFrame, cbFrame, g_cbSnapLen); 214 if (RT_FAILURE(rc)) 215 { 232 216 RTMsgError("write: %Rrf", rc); 233 217 g_net.ifAbort(); … … 241 225 242 226 243 void 244 captureGSO(void *pvUser, PCPDMNETWORKGSO pcGso, uint32_t cbFrame) 245 { 246 RT_NOREF(pvUser); 247 RT_NOREF(pcGso, cbFrame); 227 static DECLCALLBACK(void) captureGSO(void *pvUser, PCPDMNETWORKGSO pcGso, uint32_t cbFrame) 228 { 229 RT_NOREF(pvUser, pcGso, cbFrame); 248 230 249 231 checkCaptureLimit();
Note:
See TracChangeset
for help on using the changeset viewer.