"Fossies" - the Fresh Open Source Software Archive

Member "tcpflow-1.6.1/src/wifipcap/sample.cpp" (19 Feb 2021, 3852 Bytes) of package /linux/misc/tcpflow-1.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sample.cpp" see the Fossies "Dox" file reference documentation.

    1 #include <iostream>
    2 #include "wifipcap.h"
    3 
    4 /* Demonstration of how to process pcap packets with a simple callback class */
    5 
    6 class TestCB : public WifipcapCallbacks
    7 {
    8 public:
    9     TestCB(){}
   10     virtual ~TestCB(){};
   11     virtual const char *name()  {return "TestCB";} // override with your own name!
   12     virtual void PacketBegin(const WifiPacket &p, const u_char *pkt, size_t len, int origlen)  {
   13         TimeVal t(p.header->ts);
   14         std::cout << &t << " {";
   15     }
   16     virtual void PacketEnd(const WifiPacket &p )    {
   17         std::cout << "}" << std::endl;
   18     }
   19  
   20     virtual bool Check80211FCS(const WifiPacket &p )  { return true; } // please calculate FCS
   21 
   22     virtual void Handle80211DataFromAP(const WifiPacket &p, const mac_hdr_t *hdr, const u_char *rest, u_int len)      {
   23         std::cout << "802.11 data:\t" 
   24                       << hdr->sa << " -> " 
   25                       << hdr->da << "\t" 
   26                       << len ;
   27     }
   28     virtual void Handle80211DataToAP(const WifiPacket &p, const mac_hdr_t *hdr, const u_char *rest, u_int len) 
   29     {
   30         std::cout << "802.11 data:\t" 
   31                   << hdr->sa << " -> " 
   32                   << hdr->da << "\t" 
   33                   << len ;
   34     }
   35 
   36 
   37     virtual void Handle80211MgmtProbeRequest(const WifiPacket &p,  const mgmt_header_t *hdr, const mgmt_body_t *body)  {
   38         std::cout << "802.11 mgmt:\t" 
   39                   << hdr->sa << "\tprobe\t\"" 
   40                   << body->ssid.ssid << "\"" ;
   41     }
   42 
   43     virtual void Handle80211MgmtBeacon(const WifiPacket &p,  const struct mgmt_header_t *hdr, const struct mgmt_body_t *body)    {
   44         std::cout << "802.11 mgmt:\t" 
   45                   << hdr->sa << "\tbeacon\t\"" 
   46                   << body->ssid.ssid << "\"" ;
   47     }
   48 
   49     virtual void HandleTCP(const WifiPacket &p,  const ip4_hdr_t *ip4h, const ip6_hdr_t *ip6h, const tcp_hdr_t *hdr, const u_char *options, int optlen, const u_char *rest, u_int len)  {
   50     if (ip4h && hdr)
   51         std::cout << "tcp/ip:     \t" 
   52                       << ip4h->src << ":" << hdr->sport << " -> " 
   53                       << ip4h->dst << ":" << hdr->dport 
   54                       << "\t" << ip4h->len ;
   55     else
   56         std::cout << "tcp/ip:     \t" << "[truncated]" ;
   57     }   
   58 
   59     virtual void HandleUDP(const WifiPacket &p,  const ip4_hdr_t *ip4h, const ip6_hdr_t *ip6h, const udp_hdr_t *hdr, const u_char *rest, u_int len)  {
   60         if (ip4h && hdr)
   61             std::cout << "udp/ip:     \t" 
   62                       << ip4h->src << ":" << hdr->sport << " -> " 
   63                       << ip4h->dst << ":" << hdr->dport 
   64                       << "\t" << ip4h->len ;
   65         else
   66             std::cout << " " << "udp/ip:     \t" << "[truncated]" ;
   67     }
   68 };
   69 
   70 
   71 /**
   72  * usage: test <pcap_trace_file>
   73  */
   74 int main(int argc, char **argv)
   75 {
   76     if (argc == 1) {
   77         pcap_if_t *alldevs;
   78         pcap_if_t *d;
   79         int i=0;
   80         char errbuf[PCAP_ERRBUF_SIZE];
   81     
   82         /* Retrieve the device list from the local machine */
   83         if (pcap_findalldevs(&alldevs, errbuf) == -1)        {
   84             fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);
   85             exit(1);
   86         }
   87     
   88         /* Print the list */
   89         for(d= alldevs; d != NULL; d= d->next)        {
   90             printf("%d. %s", ++i, d->name);
   91             if (d->description)
   92                 printf(" (%s)\n", d->description);
   93             else
   94                 printf(" (No description available)\n");
   95         }
   96     
   97         if (i == 0)        {
   98             printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
   99             return 1;
  100         }
  101 
  102         /* We don't need any more the device list. Free it */
  103         pcap_freealldevs(alldevs);
  104     return 1;
  105     }
  106 
  107     bool live = argc == 3 && atoi(argv[2]) == 1;
  108     Wifipcap *wcap = new Wifipcap(argv[1], live);
  109     wcap->Run(new TestCB());
  110     return 0;
  111 }
  112