VirtualBox

Ignore:
Timestamp:
May 25, 2022 8:19:59 PM (3 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
151587
Message:

FE/VBoxIntnetPcap: noexcept fixes and some quick cleanups.

Location:
trunk/src/VBox/Frontends/VBoxIntnetPcap
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxIntnetPcap/Makefile.kmk

    r93115 r95107  
    22## @file
    33# Sub-makefile for the VBoxIntnetPcap
     4#
    45
    56#
     
    1920
    2021PROGRAMS += 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)
     22VBoxIntnetPcap_TEMPLATE := VBOXR3EXE
     23VBoxIntnetPcap_INCS     := \
     24        ../../NetworkServices/NetLib \
     25        ../../Devices/Network
     26VBoxIntnetPcap_SOURCES  := \
     27        VBoxIntnetPcap.cpp \
     28        ../../NetworkServices/NetLib/IntNetIf.cpp \
     29        ../../Devices/Network/Pcap.cpp
     30VBoxIntnetPcap_LIBS     = $(LIB_RUNTIME)
    3531# VBoxIntnetPcap_LIBS.solaris += socket nsl # XXX: when we add rpcapd support
    3632
    3733include $(FILE_KBUILD_SUB_FOOTER)
     34
  • trunk/src/VBox/Frontends/VBoxIntnetPcap/VBoxIntnetPcap.cpp

    r93115 r95107  
    1616 */
    1717
     18
     19/*********************************************************************************************************************************
     20*   Header Files                                                                                                                 *
     21*********************************************************************************************************************************/
    1822#include "IntNetIf.h"
    1923#include "Pcap.h"
     
    3034#include <VBox/version.h>
    3135
    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*********************************************************************************************************************************/
     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[] =
    4555{
    4656    { "--count",                'c',   RTGETOPT_REQ_UINT64 },
     
    5565main(int argc, char *argv[])
    5666{
    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);
    6368    if (RT_FAILURE(rc))
    6469        return RTMsgInitFailure(rc);
    6570
     71    /*
     72     * Parse options
     73     */
    6674    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;
    7080
    7181    int ch;
     
    7686        {
    7787            case 'c':           /* --count */
    78                 if (g_u64Count != 0)
    79                     return RTMsgErrorExit(RTEXITCODE_SYNTAX,
    80                                           "multiple --count options");
    8188                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;
    8591                break;
    8692
    8793            case 'i':           /* --network */
    88                 if (strNetworkName.isNotEmpty())
    89                     return RTMsgErrorExit(RTEXITCODE_SYNTAX,
    90                                           "multiple --network options");
    9194                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;
    9597                break;
    9698
    9799            case 's':           /* --snaplen */
    98                 if (g_cbSnapLen != 0)
    99                     return RTMsgErrorExit(RTEXITCODE_SYNTAX,
    100                                           "multiple --snaplen options");
    101100                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");
    104102                g_cbSnapLen = Val.u32;
    105103                break;
     
    110108
    111109            case 'w':           /* --write */
    112                 if (strPcapFile.isNotEmpty())
    113                     return RTMsgErrorExit(RTEXITCODE_SYNTAX,
    114                                           "multiple --write options");
    115110                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;
    119113                break;
    120114
     
    142136                return RTEXITCODE_SUCCESS;
    143137
     138            default:
    144139            case VINF_GETOPT_NOT_OPTION:
    145                 return RTMsgErrorExit(RTEXITCODE_SYNTAX,
    146                                       "unexpected non-option argument");
    147 
    148             default:
    149140                return RTGetOptPrintError(ch, &Val);
    150141        }
    151142    }
    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)
    167152        g_pStrmOut = g_pStdOut;
    168     }
    169153    else
    170154    {
    171         rc = RTStrmOpen(strPcapFile.c_str(), "wb", &g_pStrmOut);
     155        rc = RTStrmOpen(pszPcapFile, "wb", &g_pStrmOut);
    172156        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     */
    177163    g_net.setInputCallback(captureFrame, NULL);
    178164    g_net.setInputGSOCallback(captureGSO, NULL);
     
    183169     * typo...  beware.
    184170     */
    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);
    189174
    190175    rc = g_net.ifSetPromiscuous();
    191176    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     */
    196182    g_StartNanoTS = RTTimeNanoTS();
    197183    rc = PcapStreamHdr(g_pStrmOut, g_StartNanoTS);
    198184    if (RT_FAILURE(rc))
    199         return RTMsgErrorExit(RTEXITCODE_FAILURE,
    200                               "write: %Rrf", rc);
     185        return RTMsgErrorExitFailure("write: %Rrf", rc);
    201186    if (g_fPacketBuffered)
    202187        RTStrmFlush(g_pStrmOut);
    203188
    204189    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
     199static void checkCaptureLimit(void)
     200{
     201    if (g_cCountDown > 0)
     202    {
     203        if (g_cCountDown-- == 1)
    217204            g_net.ifAbort();
    218205    }
     
    220207
    221208
    222 void
    223 captureFrame(void *pvUser, void *pvFrame, uint32_t cbFrame)
    224 {
    225     int rc;
    226 
     209static DECLCALLBACK(void) captureFrame(void *pvUser, void *pvFrame, uint32_t cbFrame)
     210{
    227211    RT_NOREF(pvUser);
    228212
    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    {
    232216        RTMsgError("write: %Rrf", rc);
    233217        g_net.ifAbort();
     
    241225
    242226
    243 void
    244 captureGSO(void *pvUser, PCPDMNETWORKGSO pcGso, uint32_t cbFrame)
    245 {
    246     RT_NOREF(pvUser);
    247     RT_NOREF(pcGso, cbFrame);
     227static DECLCALLBACK(void) captureGSO(void *pvUser, PCPDMNETWORKGSO pcGso, uint32_t cbFrame)
     228{
     229    RT_NOREF(pvUser, pcGso, cbFrame);
    248230
    249231    checkCaptureLimit();
Note: See TracChangeset for help on using the changeset viewer.

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