-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPacketSniffer.cpp
74 lines (63 loc) · 1.69 KB
/
PacketSniffer.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include "PacketSniffer.h"
PacketSniffer::PacketSniffer(const std::string& if_name):
sockfd_(-1),
if_name_(if_name){
}
PacketSniffer::~PacketSniffer() {
if (sockfd_ > 0) {
close(sockfd_);
}
}
int PacketSniffer::Init(){
if ((sockfd_ = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP))) < 0) {
perror("build recv raw socket error!\n");
return -1;
}
struct ifreq ifstruct;
strcpy(ifstruct.ifr_name, if_name_.c_str());
if (ioctl(sockfd_, SIOCGIFINDEX, &ifstruct) < 0) {
perror("get inet index error!\n");
return -1;
}
memset(&addr_sll_, 0, sizeof(struct sockaddr_ll));
addr_sll_.sll_family = AF_PACKET;
addr_sll_.sll_ifindex = ifstruct.ifr_ifindex;
addr_sll_.sll_protocol = htons(ETH_P_IP);
if(bind(sockfd_, (struct sockaddr *) &addr_sll_, sizeof(addr_sll_)) == -1 ) {
perror("bind socket error!\n");
return -1;
}
return 0;
}
int PacketSniffer::SetPromisc(bool flag) {
struct ifreq ifr;
strcpy(ifr.ifr_name, if_name_.c_str());
if(ioctl(sockfd_, SIOCGIFFLAGS, &ifr) == -1) {
perror("iotcl get promisc flag error!\n");
return -1;
}
if (flag == true)
ifr.ifr_flags |= IFF_PROMISC;
else
ifr.ifr_flags &= ~IFF_PROMISC;
if(ioctl(sockfd_, SIOCSIFFLAGS, &ifr) == -1) {
perror("iotcl set promisc flag error!\n");
return -1;
}
return 0;
}
ssize_t PacketSniffer::RecvEthPacket(char*buf, size_t len) {
assert(sockfd_ > 0);
ssize_t bytes_recv = recv(sockfd_, buf, len, 0);
if (bytes_recv == -1)
perror("socket recved error!\n");
return bytes_recv;
}