tcpflow  1.6.1
About: tcpflow is a TCP/IP packet demultiplexer that captures data transmitted as part of TCP connections (flows), and stores the data in a way that is convenient for protocol analysis and debugging.
  Fossies Dox: tcpflow-1.6.1.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

sample.cpp
Go to the documentation of this file.
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 
struct timeval TimeVal
Definition: TimeVal.h:52
Definition: sample.cpp:7
virtual const char * name()
Definition: sample.cpp:11
virtual ~TestCB()
Definition: sample.cpp:10
virtual void PacketBegin(const WifiPacket &p, const u_char *pkt, size_t len, int origlen)
Definition: sample.cpp:12
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)
Definition: sample.cpp:59
virtual void Handle80211DataToAP(const WifiPacket &p, const mac_hdr_t *hdr, const u_char *rest, u_int len)
Definition: sample.cpp:28
virtual bool Check80211FCS(const WifiPacket &p)
Definition: sample.cpp:20
TestCB()
Definition: sample.cpp:9
virtual void Handle80211MgmtProbeRequest(const WifiPacket &p, const mgmt_header_t *hdr, const mgmt_body_t *body)
Definition: sample.cpp:37
virtual void Handle80211DataFromAP(const WifiPacket &p, const mac_hdr_t *hdr, const u_char *rest, u_int len)
Definition: sample.cpp:22
virtual void PacketEnd(const WifiPacket &p)
Definition: sample.cpp:16
virtual void Handle80211MgmtBeacon(const WifiPacket &p, const struct mgmt_header_t *hdr, const struct mgmt_body_t *body)
Definition: sample.cpp:43
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)
Definition: sample.cpp:49
void Run(WifipcapCallbacks *cbs, int maxpkts=0)
Definition: wifipcap.cpp:1774
#define PCAP_ERRBUF_SIZE
Definition: pcap_fake.h:23
int main(int argc, char **argv)
Definition: sample.cpp:74
const struct pcap_pkthdr * header
Definition: wifipcap.h:599
Definition: ip.h:168
struct in_addr src dst
Definition: ip.h:180
u_int16_t len
Definition: ip.h:172
Definition: ip6.h:194
MAC da
Definition: wifipcap.h:323
MAC sa
Definition: wifipcap.h:324
struct ssid_t ssid
Definition: wifipcap.h:486
struct timeval ts
Definition: pcap_fake.h:36
char ssid[33]
Definition: wifipcap.h:176
Definition: tcp.h:120
u_int16_t dport
Definition: tcp.h:122
u_int16_t sport
Definition: tcp.h:121
Definition: udp.h:91
u_int16_t sport
Definition: udp.h:92
u_int16_t dport
Definition: udp.h:93