VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxIntnetPcap/VBoxIntnetPcap.cpp@ 96313

Last change on this file since 96313 was 95107, checked in by vboxsync, 3 years ago

FE/VBoxIntnetPcap: noexcept fixes and some quick cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: VBoxIntnetPcap.cpp 95107 2022-05-25 20:19:59Z vboxsync $ */
2/** @file
3 * VBoxIntnetPcap - packet capture for VirtualBox internal networks
4 */
5
6/*
7 * Copyright (C) 2021-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include "IntNetIf.h"
23#include "Pcap.h"
24
25#include <iprt/buildconfig.h>
26#include <iprt/file.h>
27#include <iprt/getopt.h>
28#include <iprt/message.h>
29#include <iprt/process.h>
30#include <iprt/stream.h>
31
32#include <iprt/cpp/ministring.h>
33
34#include <VBox/version.h>
35
36
37/*********************************************************************************************************************************
38* Internal Functions *
39*********************************************************************************************************************************/
40static DECLCALLBACK(void) captureFrame(void *pvUser, void *pvFrame, uint32_t cbFrame);
41static DECLCALLBACK(void) captureGSO(void *pvUser, PCPDMNETWORKGSO pcGso, uint32_t cbFrame);
42
43
44/*********************************************************************************************************************************
45* Global Variables *
46*********************************************************************************************************************************/
47static IntNetIf g_net;
48static PRTSTREAM g_pStrmOut;
49static uint64_t g_StartNanoTS;
50static bool g_fPacketBuffered;
51static uint64_t g_cCountDown;
52static size_t g_cbSnapLen = 0xffff;
53
54static const RTGETOPTDEF g_aGetOptDef[] =
55{
56 { "--count", 'c', RTGETOPT_REQ_UINT64 },
57 { "--network", 'i', RTGETOPT_REQ_STRING },
58 { "--snaplen", 's', RTGETOPT_REQ_UINT32 },
59 { "--packet-buffered", 'U', RTGETOPT_REQ_NOTHING },
60 { "--write", 'w', RTGETOPT_REQ_STRING },
61};
62
63
64int
65main(int argc, char *argv[])
66{
67 int rc = RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB);
68 if (RT_FAILURE(rc))
69 return RTMsgInitFailure(rc);
70
71 /*
72 * Parse options
73 */
74 RTGETOPTSTATE State;
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;
80
81 int ch;
82 RTGETOPTUNION Val;
83 while ((ch = RTGetOpt(&State, &Val)) != 0)
84 {
85 switch (ch)
86 {
87 case 'c': /* --count */
88 if (Val.u64 == 0)
89 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--count must be greater than zero");
90 g_cCountDown = Val.u64;
91 break;
92
93 case 'i': /* --network */
94 if (Val.psz[0] == '\0')
95 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "empty --network option");
96 pszNetworkName = Val.psz;
97 break;
98
99 case 's': /* --snaplen */
100 if (Val.u32 == 0)
101 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--snaplen must be greater than zero");
102 g_cbSnapLen = Val.u32;
103 break;
104
105 case 'U': /* --packet-buffered */
106 g_fPacketBuffered = true;
107 break;
108
109 case 'w': /* --write */
110 if (Val.psz[0] == '\0')
111 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "empty --write option");
112 pszPcapFile = Val.psz;
113 break;
114
115
116 /*
117 * Standard options recognized by RTGetOpt()
118 */
119 case 'V': /* --version */
120 RTPrintf("%sr%u\n", RTBldCfgVersion(), RTBldCfgRevision());
121 return RTEXITCODE_SUCCESS;
122
123 case 'h': /* --help */
124 RTPrintf("%s Version %sr%u\n"
125 "(C) 2009-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
126 "All rights reserved.\n"
127 "\n"
128 "Usage: %s <options>\n"
129 "\n"
130 "Options:\n",
131 RTProcShortName(), RTBldCfgVersion(), RTBldCfgRevision(),
132 RTProcShortName());
133 for (size_t i = 0; i < RT_ELEMENTS(g_aGetOptDef); ++i)
134 RTPrintf(" -%c, %s\n",
135 g_aGetOptDef[i].iShort, g_aGetOptDef[i].pszLong);
136 return RTEXITCODE_SUCCESS;
137
138 default:
139 case VINF_GETOPT_NOT_OPTION:
140 return RTGetOptPrintError(ch, &Val);
141 }
142 }
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)
152 g_pStrmOut = g_pStdOut;
153 else
154 {
155 rc = RTStrmOpen(pszPcapFile, "wb", &g_pStrmOut);
156 if (RT_FAILURE(rc))
157 return RTMsgErrorExitFailure("%s: %Rrf", pszPcapFile, rc);
158 }
159
160 /*
161 * Configure the snooper.
162 */
163 g_net.setInputCallback(captureFrame, NULL);
164 g_net.setInputGSOCallback(captureGSO, NULL);
165
166 /*
167 * NB: There's currently no way to prevent an intnet from being
168 * created when one doesn't exist, so there's no way to catch a
169 * typo... beware.
170 */
171 rc = g_net.init(pszNetworkName);
172 if (RT_FAILURE(rc))
173 return RTMsgErrorExitFailure("%s: %Rrf", pszNetworkName, rc);
174
175 rc = g_net.ifSetPromiscuous();
176 if (RT_FAILURE(rc))
177 return RTMsgErrorExitFailure("%s: failed to set promiscuous mode: %Rrf", pszNetworkName, rc);
178
179 /*
180 * Snoop traffic.
181 */
182 g_StartNanoTS = RTTimeNanoTS();
183 rc = PcapStreamHdr(g_pStrmOut, g_StartNanoTS);
184 if (RT_FAILURE(rc))
185 return RTMsgErrorExitFailure("write: %Rrf", rc);
186 if (g_fPacketBuffered)
187 RTStrmFlush(g_pStrmOut);
188
189 g_net.ifPump();
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
199static void checkCaptureLimit(void)
200{
201 if (g_cCountDown > 0)
202 {
203 if (g_cCountDown-- == 1)
204 g_net.ifAbort();
205 }
206}
207
208
209static DECLCALLBACK(void) captureFrame(void *pvUser, void *pvFrame, uint32_t cbFrame)
210{
211 RT_NOREF(pvUser);
212
213 int rc = PcapStreamFrame(g_pStrmOut, g_StartNanoTS, pvFrame, cbFrame, g_cbSnapLen);
214 if (RT_FAILURE(rc))
215 {
216 RTMsgError("write: %Rrf", rc);
217 g_net.ifAbort();
218 }
219
220 if (g_fPacketBuffered)
221 RTStrmFlush(g_pStrmOut);
222
223 checkCaptureLimit();
224}
225
226
227static DECLCALLBACK(void) captureGSO(void *pvUser, PCPDMNETWORKGSO pcGso, uint32_t cbFrame)
228{
229 RT_NOREF(pvUser, pcGso, cbFrame);
230
231 checkCaptureLimit();
232}
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