Changeset 10757 in vbox for trunk/src/VBox/Devices
- Timestamp:
- Jul 18, 2008 8:23:50 PM (17 years ago)
- svn:sync-xref-src-repo-rev:
- 33510
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Devices/Network/testcase/tstIntNet-1.cpp
r10756 r10757 41 41 #include "../Pcap.h" 42 42 43 /******************************************************************************* 44 * Structures and Typedefs * 45 *******************************************************************************/ 46 #pragma pack(1) 47 48 struct MyEthHdr 49 { 50 PDMMAC DstMac; 51 PDMMAC SrcMac; 52 uint16_t u16Type; 53 }; 54 55 struct MyIpHdr 56 { 57 #ifdef RT_BIG_ENDIAN 58 unsigned int ip_v : 4; 59 unsigned int ip_hl : 4; 60 unsigned int ip_tos : 8; 61 unsigned int ip_len : 16; 62 #else 63 unsigned int ip_hl : 4; 64 unsigned int ip_v : 4; 65 unsigned int ip_tos : 8; 66 unsigned int ip_len : 16; 67 #endif 68 uint16_t ip_id; 69 uint16_t ip_off; 70 uint8_t ip_ttl; 71 uint8_t ip_p; 72 uint16_t ip_sum; 73 uint32_t ip_src; 74 uint32_t ip_dst; 75 /* more */ 76 uint32_t ip_options[1]; 77 }; 78 79 struct MyUdpHdr 80 { 81 uint16_t uh_sport; 82 uint16_t uh_dport; 83 uint16_t uh_ulen; 84 uint16_t uh_sum; 85 }; 86 87 struct MyDhcpMsg 88 { 89 uint8_t Op; 90 uint8_t HType; 91 uint8_t HLen; 92 uint8_t Hops; 93 uint32_t XID; 94 uint16_t Secs; 95 uint16_t Flags; 96 uint32_t CIAddr; 97 uint32_t YIAddr; 98 uint32_t SIAddr; 99 uint32_t GIAddr; 100 uint8_t CHAddr[16]; 101 uint8_t SName[64]; 102 uint8_t File[128]; 103 uint8_t abMagic[4]; 104 uint8_t DhcpOpt; 105 uint8_t DhcpLen; /* 1 */ 106 uint8_t DhcpReq; 107 uint8_t abOptions[57]; 108 }; 109 110 #pragma pack(0) 111 43 112 44 113 /******************************************************************************* … … 47 116 static int g_cErrors = 0; 48 117 static uint64_t g_StartTS = 0; 118 static uint32_t g_DhcpXID = 0; 119 static bool g_fDhcpReply = false; 49 120 50 121 … … 296 367 { 297 368 uint8_t abFrame[4096]; 298 299 #pragma pack(1) 300 301 struct MyEthHdr 302 { 303 PDMMAC DstMac; 304 PDMMAC SrcMac; 305 uint16_t u16Type; 306 } *pEthHdr = (struct MyEthHdr *)&abFrame[0]; 307 struct MyIpHdr 308 { 309 #ifdef RT_BIG_ENDIAN 310 unsigned int ip_v : 4; 311 unsigned int ip_hl : 4; 312 unsigned int ip_tos : 8; 313 unsigned int ip_len : 16; 314 #else 315 unsigned int ip_hl : 4; 316 unsigned int ip_v : 4; 317 unsigned int ip_tos : 8; 318 unsigned int ip_len : 16; 319 #endif 320 uint16_t ip_id; 321 uint16_t ip_off; 322 uint8_t ip_ttl; 323 uint8_t ip_p; 324 uint16_t ip_sum; 325 uint32_t ip_src; 326 uint32_t ip_dst; 327 /* more */ 328 uint32_t ip_options[1]; 329 } *pIpHdr = (struct MyIpHdr *)(pEthHdr + 1); 330 331 struct MyUdpHdr 332 { 333 uint16_t uh_sport; 334 uint16_t uh_dport; 335 uint16_t uh_ulen; 336 uint16_t uh_sum; 337 } *pUdpHdr = (struct MyUdpHdr *)(pIpHdr + 1); 338 339 struct MyDhcpMsg 340 { 341 uint8_t Op; 342 uint8_t HType; 343 uint8_t HLen; 344 uint8_t Hops; 345 uint32_t XID; 346 uint16_t Secs; 347 uint16_t Flags; 348 uint32_t CIAddr; 349 uint32_t YIAddr; 350 uint32_t SIAddr; 351 uint32_t GIAddr; 352 uint8_t CHAddr[16]; 353 uint8_t SName[64]; 354 uint8_t File[128]; 355 uint8_t abMagic[4]; 356 uint8_t DhcpOpt; 357 uint8_t DhcpLen; /* 1 */ 358 uint8_t DhcpReq; 359 uint8_t abOptions[57]; 360 } *pDhcpMsg = (struct MyDhcpMsg *)(pUdpHdr + 1); 361 362 #pragma pack(0) 369 struct MyEthHdr *pEthHdr = (struct MyEthHdr *)&abFrame[0]; 370 struct MyIpHdr *pIpHdr = (struct MyIpHdr *)(pEthHdr + 1); 371 struct MyUdpHdr *pUdpHdr = (struct MyUdpHdr *)(pIpHdr + 1); 372 struct MyDhcpMsg *pDhcpMsg = (struct MyDhcpMsg *)(pUdpHdr + 1); 363 373 364 374 /* … … 368 378 369 379 pDhcpMsg->Op = 1; /* request */ 370 pDhcpMsg->HType = 1; 380 pDhcpMsg->HType = 1; /* ethernet */ 371 381 pDhcpMsg->HLen = sizeof(PDMMAC); 372 382 pDhcpMsg->Hops = 0; 373 pDhcpMsg->XID = RTRandU32();383 pDhcpMsg->XID = g_DhcpXID = RTRandU32(); 374 384 pDhcpMsg->Secs = 0; 375 385 pDhcpMsg->Flags = 0; /* unicast */ //RT_H2BE_U16(0x8000); /* broadcast */ … … 456 466 * @param pFileRaw The file to write the raw data to (optional). 457 467 * @param pFileText The file to write a textual packet summary to (optional). 468 * @param pSrcMac Out MAC address. 458 469 */ 459 470 static void doPacketSniffing(INTNETIFHANDLE hIf, PSUPDRVSESSION pSession, PINTNETBUF pBuf, uint32_t cMillies, 460 PRTSTREAM pFileRaw, PRTSTREAM pFileText )471 PRTSTREAM pFileRaw, PRTSTREAM pFileText, PCPDMMAC pSrcMac) 461 472 { 462 473 /* … … 503 514 PcapStreamFrame(pFileRaw, g_StartTS, pvFrame, cbFrame, 0xffff); 504 515 516 struct MyEthHdr const *pEthHdr = (struct MyEthHdr const *)pvFrame; 505 517 if (pFileText) 506 RTStrmPrintf(pFileText, "%3RU64.%09u: cb=%04x dst=%.6Rhxs src=%.6Rhxs \n",518 RTStrmPrintf(pFileText, "%3RU64.%09u: cb=%04x dst=%.6Rhxs src=%.6Rhxs type=%04x%s\n", 507 519 NanoTS / 1000000000, (uint32_t)(NanoTS % 1000000000), 508 cbFrame, pvFrame, (uint8_t *)pvFrame + 6); 520 cbFrame, &pEthHdr->SrcMac, &pEthHdr->DstMac, RT_BE2H_U16(pEthHdr->u16Type), 521 !memcmp(&pEthHdr->DstMac, pSrcMac, sizeof(*pSrcMac)) ? " Mine!" : ""); 522 523 /* Loop for the DHCP reply. */ 524 if ( cbFrame > 64 525 && RT_BE2H_U16(pEthHdr->u16Type) == 0x0800 /* EtherType == IP */) 526 { 527 struct MyIpHdr const *pIpHdr = (struct MyIpHdr const *)(pEthHdr + 1); 528 struct MyUdpHdr const *pUdpHdr = (struct MyUdpHdr const *)((uint32_t *)pIpHdr + pIpHdr->ip_hl); 529 if ( pIpHdr->ip_p == 0x11 /*UDP*/ 530 && RT_BE2H_U16(pUdpHdr->uh_dport) == 68 /* bootp */ 531 && RT_BE2H_U16(pUdpHdr->uh_sport) == 67 /* bootps */) 532 { 533 struct MyDhcpMsg const *pDhcpMsg = (struct MyDhcpMsg const *)(pUdpHdr + 1); 534 if ( pDhcpMsg->Op == 2 /* boot reply */ 535 && pDhcpMsg->HType == 1 /* ethernet */ 536 && pDhcpMsg->HLen == sizeof(PDMMAC) 537 && (pDhcpMsg->XID == g_DhcpXID || !g_DhcpXID) 538 && !memcmp(&pDhcpMsg->CHAddr[0], pSrcMac, sizeof(*pSrcMac))) 539 { 540 g_fDhcpReply = true; 541 RTPrintf("tstIntNet-1: DHCP server reply! My IP: %d.%d.%d.%d\n", 542 RT_BYTE4(RT_BE2H_U32(pDhcpMsg->YIAddr)), 543 RT_BYTE3(RT_BE2H_U32(pDhcpMsg->YIAddr)), 544 RT_BYTE2(RT_BE2H_U32(pDhcpMsg->YIAddr)), 545 RT_BYTE1(RT_BE2H_U32(pDhcpMsg->YIAddr))); 546 } 547 } 548 } 509 549 } 510 550 else … … 777 817 */ 778 818 if (fSniffer) 779 doPacketSniffing(OpenReq.hIf, pSession, pBuf, cMillies, pFileRaw, pFileText); 819 { 820 doPacketSniffing(OpenReq.hIf, pSession, pBuf, cMillies, pFileRaw, pFileText, &SrcMac); 821 if (fXmitTest != g_fDhcpReply) 822 { 823 RTPrintf("tstIntNet-1: Error! The DHCP server didn't reply... (Perhaps you don't have one?)\n", rc); 824 g_cErrors++; 825 } 826 } 780 827 else 781 828 RTThreadSleep(cMillies);
Note:
See TracChangeset
for help on using the changeset viewer.