wpcPacket.cpp
This sample demostrates how to use the WpcObj to capture packets.
#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif
#include <stdio.h>
#include <wpcobj.h>
void printOk(WpcPacket* pkt)
{
string res;
printf(" ok\n");
res = pkt->Dump();
printf("%s\n", res.c_str());
}
int main(int argc, char* argv[])
{
WpcPacket* pktEth;
WpcIpPacket* pktIp;
WpcTcpPacket* pktTcp;
WpcArpPacket* pktArp;
WpcIcmpPacket* pktIcmp;
WpcMacAddr mac;
string res;
int i;
mac.at(0) = 0x00;
mac.at(1) = 0x11;
mac.at(2) = 0x22;
mac.at(3) = 0x33;
mac.at(4) = 0x44;
mac.at(5) = 0x55;
try {
printf("Try to create empty packet...");
pktEth = new WpcPacket();
printOk(pktEth);
printf("Try to add src MAC to the packet...");
pktEth->eth_src(mac);
printOk(pktEth);
printf("Try to get destination MAC from the packet...");
mac = pktEth->eth_dst();
printf(" ok\n");
res = mac.ToString();
printf("%s\n\n", res.c_str());
printf("Try to resize the packet...");
pktEth->Resize(100);
printOk(pktEth);
printf("Try to set protocol and payload...");
pktEth->eth_proto(WPC_ETH_P_IP);
memcpy(pktEth->GetData(), "abcdef0123456789", 16);
printOk(pktEth);
printf("Try to direct acceess to packet buffer...");
for (i = 0; i < pktEth->Length(); i++) {
(*pktEth)[i] = (unsigned char)i;
}
printOk(pktEth);
printf("Try to cast the packet to ARP proto...");
pktArp = new WpcArpPacket(*pktEth);
printOk(pktArp);
delete pktArp;
printf("Try to cast the packet to IP proto...");
pktIp = new WpcIpPacket(*pktEth);
printOk(pktIp);
delete pktIp;
printf("Try to cast the packet to TCP proto...");
pktTcp = new WpcTcpPacket(*pktEth);
printOk(pktTcp);
delete pktTcp;
printf("Try to cast the packet to ICMP proto...");
pktIcmp = new WpcIcmpPacket(*pktEth);
printOk(pktIcmp);
delete pktIcmp;
}
catch (...) {
printf("Something strange happens! Exiting...\n");
}
return 0;
}