wpcPacket.cpp

This sample demostrates how to use the WpcObj to capture packets.

/*
    wpcobj is a C++ wrapper for libpcap (the packet capture library)
    Copyright (C) 2009 Positive Technologies

    This file is part of wpcobj

    wpcobj is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    wpcobj is is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with wpcobj is.  If not, see <http://www.gnu.org/licenses/>.
*/

#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;
}



 
©2009 Positive Technologies
 
Generated on Fri May 22 18:17:00 2009 for wpcObj by 
 
 
1.5.7
 
Get wpcObj at SourceForge.net. Fast, secure and Free Open Source software downloads