My Project
sniffer.h
1 #ifndef SNIFFER_H
2 #define SNIFFER_H
3 
4 #include <QObject>
5 #include <QString>
6 #include <QDebug>
7 #include "ip_packet.h"
8 #include <QWaitCondition>
9 #include <QMutex>
10 
11 #include <pcap.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <ctype.h>
16 #include <cstring>
17 #include <errno.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include "ethernet_hdr.h"
23 #include "ip_hdr.h"
24 #include "tcp_hdr.h"
25 #include "udp_hdr.h"
26 
27 /* default snap length (maximum bytes per packet to capture) */
28 #define SNAP_LEN 1518
29 
30 /* ethernet headers are always exactly 14 bytes [1] */
31 #define SIZE_ETHERNET 14
32 
33 #define IPV4 8
34 class Sniffer : public QObject
41 {
42  Q_OBJECT
43 
44 public:
45 
51  Sniffer() ;
52 
66  Sniffer(QWaitCondition *pw, QMutex *mx, int *ps) ;
67 
73  ~Sniffer() ;
74 
84  vector<ip_packet> * getPacketList() ;
85 
98  string format_payload(const char *payload, int len);
99 
114  string format_hex_ascii_line(const char *payload, int len, int offset);
115 
124  void find_devices(vector<string> &) ;
125 
133  void setDevice(string) ;
134 
135 public slots:
136  void process() ;
137 
138 signals:
139  void resultReady(unsigned long index);
140 
141 private:
142 
143  vector<ip_packet> packet_list ;
144  QWaitCondition *wait_pause ;
145  QMutex * mutex ;
146  string device ;
147  string filter ;
148  int *pause ;
149 
159  void print_payload(const u_char *payload, int len);
160 
172  void print_hex_ascii_line(const u_char *payload, int len, int offset);
173 
183  void
184  got_packet(const struct pcap_pkthdr *header, const u_char *packet);
185 
186 };
187 
188 #endif // SNIFFER_H
void got_packet(const struct pcap_pkthdr *header, const u_char *packet)
Disects the received packet. Takes out the info needed.
Definition: sniffer.cpp:394
string format_payload(const char *payload, int len)
Formats the payload from a byte stream into a string of ascci.
Definition: sniffer.cpp:342
~Sniffer()
Destructor, does nothing.
Definition: sniffer.cpp:35
void process()
The sniffer is run as a separate thread and the function process is the main function of the thread...
Definition: sniffer.cpp:45
void print_hex_ascii_line(const u_char *payload, int len, int offset)
Prints to std output the a payload in ascii.
Definition: sniffer.cpp:157
void print_payload(const u_char *payload, int len)
Prints the payload in ascii.
Definition: sniffer.cpp:278
void setDevice(string)
Sets the device to capture packets to dev.
Definition: sniffer.cpp:561
vector< ip_packet > * getPacketList()
Returns the packet list that contains the packets that are Ethernet -> IP -> (TCP|UDP) ...
Definition: sniffer.cpp:520
void find_devices(vector< string > &)
Find the network devices in the computer, and store them in vector devs.
Definition: sniffer.cpp:533
Sniffer()
Constructor, does nothing.
Definition: sniffer.cpp:8
string format_hex_ascii_line(const char *payload, int len, int offset)
Return string with the bytes of a payload line in ascii.
Definition: sniffer.cpp:219
The Sniffer class is the one that use the pcap library to extract the packet information. It discards any packet that is not Ethernet->IP->(TCP|UDP), and pass up to the GUI the packets that are Ethernet->IP->(TCP|UDP).
Definition: sniffer.h:40