-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrawsocket.cpp
35 lines (29 loc) · 918 Bytes
/
rawsocket.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
#include <cstdio>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if.h>
#include "rawsocket.h"
RawSocket::RawSocket(const QString& net_iface, QObject *parent)
: QAbstractSocket(QAbstractSocket::UnknownSocketType, parent),
mNetIface(net_iface)
{
/*
* Using linux raw socket
*/
int sock_fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sock_fd == -1) {
printf("[!] Failed to create socket\n");
return;
}
struct ifreq ifr;
memset((void*)&ifr, 0, sizeof(ifr));
snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), mNetIface.toStdString().c_str());
if (setsockopt(sock_fd, SOL_SOCKET, SO_BINDTODEVICE, (void*)&ifr, sizeof(ifr)) == -1) {
printf("[!] Failed to bind to %s\n", mNetIface);
return;
}
setSocketDescriptor(sock_fd);
}