-
Notifications
You must be signed in to change notification settings - Fork 0
/
t_sniffer.cpp
75 lines (62 loc) · 2.07 KB
/
t_sniffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Created by The-Funk on 3/4/18.
//
#include "t_sniffer.h"
#include <iostream>
using namespace std;
using namespace Tins;
t_sniffer::t_sniffer() {
//Set immediate mode to keep things fast
this->config.set_immediate_mode(true);
this->config.set_promisc_mode(true);
}
//Set interface, if none set use default
void t_sniffer::setIntf(string intf) {
if(intf != "default") {
this->intf = intf;
cout << "Interface name manually set to: " << intf << endl;
}
else {
NetworkInterface iface = NetworkInterface::default_interface();
this->intf = iface.name();
cout << "Interface name set to default: " << iface.name() << endl;
}
}
void t_sniffer::setTarget(string target) {
//Set ip src filter
this->config.set_filter("ip src" + target);
}
void t_sniffer::setPromisc(bool promisc) {
//Set promiscuity on interface
this->config.set_promisc_mode(promisc);
}
//Capture packets and run handlePacket template functor on each, sequentially, until functor returns false
void t_sniffer::startSniff() {
this->OnOffSwitch = true;
Sniffer sniffer(this->intf, this->config);
int count = 0;
bool linkTypeKnown = false;
sniffer.sniff_loop([&](Packet& packet) {
if(this->OnOffSwitch && count < 10) {
if (!linkTypeKnown && packet.pdu()->find_pdu<EthernetII>()) {
cout << "This is an Ethernet interface." << endl;
linkTypeKnown = true;
}
else if (!linkTypeKnown && packet.pdu()->find_pdu<Dot11>()) {
cout << "This is an 802.11 wifi interface." << endl;
linkTypeKnown = true;
}
if (packet.pdu()->find_pdu<IP>()) {
// Just print timestamp's seconds and IP source address
cout << "At: " << packet.timestamp().seconds() << " - " << packet.pdu()->rfind_pdu<IP>().src_addr()
<< endl;
}
count++;
return true;
}
return false;
});
}
void t_sniffer::stopSniff() {
this->OnOffSwitch = false;
}