VirtualBox

Changeset 11045 in vbox


Ignore:
Timestamp:
Jul 31, 2008 6:43:49 PM (17 years ago)
Author:
vboxsync
Message:

tstIntNet-1: Test the RTNetIPv4 validation code when sniffing and sending stuff.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Network/testcase/tstIntNet-1.cpp

    r10983 r11045  
    5050static uint32_t g_DhcpXID = 0;
    5151static bool     g_fDhcpReply = false;
    52 
     52static uint32_t g_cOtherPkts = 0;
     53static uint32_t g_cArpPkts = 0;
     54static uint32_t g_cIpv4Pkts = 0;
     55static uint32_t     g_cUdpPkts = 0;
     56static uint32_t         g_cDhcpPkts = 0;
     57static uint32_t     g_cTcpPkts = 0;
     58
     59
     60/**
     61 * Error reporting wrapper.
     62 *
     63 * @param   pErrStrm        The stream to write the error message to. Can be NULL.
     64 * @param   pszFormat       The message format string.
     65 * @param   ...             Format arguments.
     66 */
     67static void tstIntNetError(PRTSTREAM pErrStrm, const char *pszFormat, ...)
     68{
     69    if (!pErrStrm)
     70        pErrStrm = g_pStdOut;
     71
     72    va_list va;
     73    va_start(va, pszFormat);
     74    RTStrmPrintf(pErrStrm, "tstIntNet-1: ERROR - ");
     75    RTStrmPrintfV(pErrStrm, pszFormat, va);
     76    va_end(va);
     77
     78    g_cErrors++;
     79}
     80
     81
     82/**
     83 * Parses a frame an runs in thru the RTNet validation code so it gets
     84 * some exercise.
     85 *
     86 * @param   pvFrame     Pointer to the ethernet frame.
     87 * @param   cbFrame     The size of the ethernet frame.
     88 * @param   pErrStrm    The error stream.
     89 */
     90static void tstIntNetTestFrame(void const *pvFrame, size_t cbFrame, PRTSTREAM pErrStrm)
     91{
     92    /*
     93     * Ethernet header.
     94     */
     95    PCRTNETETHERHDR pEtherHdr = (PCRTNETETHERHDR)pvFrame;
     96    if (cbFrame <= sizeof(*pEtherHdr))
     97        return tstIntNetError(pErrStrm, "cbFrame=%#x <= %#x (ether)\n", cbFrame, sizeof(*pEtherHdr));
     98    ssize_t cbLeft = cbFrame - sizeof(*pEtherHdr);
     99    uint8_t const *pbCur = (uint8_t const *)(pEtherHdr + 1);
     100
     101    switch (RT_BE2H_U16(pEtherHdr->EtherType))
     102    {
     103        case RTNET_ETHERTYPE_ARP:
     104        {
     105            g_cArpPkts++;
     106
     107            break;
     108        }
     109
     110        case RTNET_ETHERTYPE_IPV4:
     111        {
     112            g_cIpv4Pkts++;
     113
     114            PCRTNETIPV4 pIpHdr = (PCRTNETIPV4)pbCur;
     115            if (!RTNetIPv4IsHdrValid(pIpHdr, cbLeft, cbLeft))
     116                return tstIntNetError(pErrStrm, "RTNetIPv4IsHdrValid failed\n");
     117            pbCur += pIpHdr->ip_hl * 4;
     118            cbLeft -= pIpHdr->ip_hl * 4;
     119            AssertFatal(cbLeft >= 0);
     120
     121            switch (pIpHdr->ip_p)
     122            {
     123                case RTNETIPV4_PROT_ICMP:
     124                {
     125                    break;
     126                }
     127
     128                case RTNETIPV4_PROT_UDP:
     129                {
     130                    g_cUdpPkts++;
     131                    PCRTNETUDP pUdpHdr = (PCRTNETUDP)pbCur;
     132                    if (!RTNetIPv4IsUDPValid(pIpHdr, pUdpHdr, pUdpHdr + 1, cbLeft))
     133                        return tstIntNetError(pErrStrm, "RTNetIPv4IsUDPValid failed\n");
     134                    pbCur += sizeof(*pUdpHdr);
     135                    cbLeft -= sizeof(*pUdpHdr);
     136
     137                    if (RT_BE2H_U16(pUdpHdr->uh_dport) == RTNETIPV4_PORT_BOOTPS)
     138                    {
     139                        g_cDhcpPkts++;
     140
     141                    }
     142                    break;
     143                }
     144
     145                case RTNETIPV4_PROT_TCP:
     146                {
     147                    g_cTcpPkts++;
     148                    PCRTNETTCP pTcpHdr = (PCRTNETTCP)pbCur;
     149                    if (!RTNetIPv4IsTCPValid(pIpHdr, pTcpHdr, cbLeft, NULL, cbLeft))
     150                        return tstIntNetError(pErrStrm, "RTNetIPv4IsTCPValid failed\n");
     151                    break;
     152                }
     153            }
     154            break;
     155        }
     156
     157        //case RTNET_ETHERTYPE_IPV6:
     158        default:
     159            g_cOtherPkts++;
     160            break;
     161    }
     162}
    53163
    54164
     
    155265 * @param   cbFrame         The size of it.
    156266 * @param   pFileRaw        The file to write the raw data to (optional).
     267 * @param   pFileText       The file to write a textual packet summary to (optional).
    157268 */
    158 static void doXmitFrame(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, void *pvFrame, size_t cbFrame, PRTSTREAM pFileRaw)
     269static void doXmitFrame(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, void *pvFrame, size_t cbFrame, PRTSTREAM pFileRaw, PRTSTREAM pFileText)
    159270{
    160271    /*
    161      * Calcuate and append the checksum.
    162      */
    163     uint32_t u32Crc = RTCrc32(pvFrame, cbFrame);
    164     u32Crc = RT_H2LE_U32(u32Crc); /* huh? */
    165     memcpy((uint8_t *)pvFrame + cbFrame, &u32Crc, sizeof(u32Crc));
    166     cbFrame += sizeof(u32Crc);
     272     * Log it.
     273     */
     274    if (pFileText)
     275    {
     276        PCRTNETETHERHDR pEthHdr = (PCRTNETETHERHDR)pvFrame;
     277        uint64_t    NanoTS = RTTimeNanoTS() - g_StartTS;
     278        RTStrmPrintf(pFileText, "%3RU64.%09u: cb=%04x dst=%.6Rhxs src=%.6Rhxs type=%04x Send!\n",
     279                     NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000),
     280                     cbFrame, &pEthHdr->SrcMac, &pEthHdr->DstMac, RT_BE2H_U16(pEthHdr->EtherType));
     281    }
     282
     283    /*
     284     * Run in thru the frame validator to test the RTNet code.
     285     */
     286    tstIntNetTestFrame(pvFrame, cbFrame, pFileText);
    167287
    168288    /*
     
    207327 * @param   pSrcMac         The mac address to use as source.
    208328 * @param   pFileRaw        The file to write the raw data to (optional).
     329 * @param   pFileText       The file to write a textual packet summary to (optional).
    209330 */
    210 static void doXmitText(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, PCPDMMAC pSrcMac, PRTSTREAM pFileRaw)
     331static void doXmitTest(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, PCPDMMAC pSrcMac, PRTSTREAM pFileRaw, PRTSTREAM pFileText)
    211332{
    212333    uint8_t abFrame[4096];
     
    293414    pEthHdr->EtherType = RT_H2BE_U16(RTNET_ETHERTYPE_IPV4); /* IP */
    294415
    295     doXmitFrame(hIf, pSession, pBuf, &abFrame[0], (uint8_t *)(pDhcpMsg + 1) - (uint8_t *)&abFrame[0], pFileRaw);
     416    doXmitFrame(hIf, pSession, pBuf, &abFrame[0], (uint8_t *)(pDhcpMsg + 1) - (uint8_t *)&abFrame[0], pFileRaw, pFileText);
    296417}
    297418
     
    360481                                 cbFrame, &pEthHdr->SrcMac, &pEthHdr->DstMac, RT_BE2H_U16(pEthHdr->EtherType),
    361482                                 !memcmp(&pEthHdr->DstMac, pSrcMac, sizeof(*pSrcMac)) ? " Mine!" : "");
     483                tstIntNetTestFrame(pvFrame, cbFrame, pFileText);
    362484
    363485                /* Loop for the DHCP reply. */
     
    409531                 pBuf->cStatYieldsNok.c
    410532                 );
     533    RTStrmPrintf(pFileText ? pFileText : g_pStdOut,
     534                 "%3RU64.%09u: cOtherPkts=%RU32 cArpPkts=%RU32 cIpv4Pkts=%RU32 cTcpPkts=%RU32 cUdpPkts=%RU32 cDhcpPkts=%RU32\n",
     535                 NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000),
     536                 g_cOtherPkts, g_cArpPkts, g_cIpv4Pkts, g_cTcpPkts, g_cUdpPkts, g_cDhcpPkts);
    411537}
    412538
     
    669795                     */
    670796                    if (fXmitTest)
    671                         doXmitText(OpenReq.hIf, pSession, pBuf, &SrcMac, pFileRaw);
     797                        doXmitTest(OpenReq.hIf, pSession, pBuf, &SrcMac, pFileRaw, pFileText);
    672798
    673799                    /*
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