forked from pandax381/microps
-
Notifications
You must be signed in to change notification settings - Fork 1
/
raw_socket.c
147 lines (132 loc) · 3.44 KB
/
raw_socket.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <poll.h>
#include "util.h"
#include "raw.h"
struct raw_socket_priv {
int fd;
};
static int
raw_socket_open (struct rawdev *dev) {
struct raw_socket_priv *priv;
struct ifreq ifr;
struct sockaddr_ll sockaddr;
priv = malloc(sizeof(struct raw_socket_priv));
if (!priv) {
fprintf(stderr, "malloc: failure\n");
goto ERROR;
}
priv->fd = socket(PF_PACKET, SOCK_RAW, hton16(ETH_P_ALL));
if (priv->fd == -1) {
perror("socket");
goto ERROR;
}
strncpy(ifr.ifr_name, dev->name, sizeof(ifr.ifr_name) - 1);
if (ioctl(priv->fd, SIOCGIFINDEX, &ifr) == -1) {
perror("ioctl [SIOCGIFINDEX]");
goto ERROR;
}
memset(&sockaddr, 0x00, sizeof(sockaddr));
sockaddr.sll_family = AF_PACKET;
sockaddr.sll_protocol = hton16(ETH_P_ALL);
sockaddr.sll_ifindex = ifr.ifr_ifindex;
if (bind(priv->fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) {
perror("bind");
goto ERROR;
}
if (ioctl(priv->fd, SIOCGIFFLAGS, &ifr) == -1) {
perror("ioctl [SIOCGIFFLAGS]");
goto ERROR;
}
ifr.ifr_flags = ifr.ifr_flags | IFF_PROMISC;
if (ioctl(priv->fd, SIOCSIFFLAGS, &ifr) == -1) {
perror("ioctl [SIOCSIFFLAGS]");
goto ERROR;
}
dev->priv = priv;
return 0;
ERROR:
if (priv) {
if (priv->fd == -1) {
close(priv->fd);
}
free(priv);
}
return -1;
}
static void
raw_socket_close (struct rawdev *dev) {
struct raw_socket_priv *priv;
priv = (struct raw_socket_priv *)dev->priv;
if (priv) {
if (priv->fd != -1) {
close(priv->fd);
}
free(priv);
}
dev->priv = NULL;
}
static void
raw_socket_rx (struct rawdev *dev, void (*callback)(uint8_t *, size_t, void *), void *arg, int timeout) {
struct raw_socket_priv *priv;
struct pollfd pfd;
ssize_t ret, length;
uint8_t buffer[2048];
priv = (struct raw_socket_priv *)dev->priv;
pfd.fd = priv->fd;
pfd.events = POLLIN;
ret = poll(&pfd, 1, timeout);
if (ret <= 0) {
return;
}
length = read(priv->fd, buffer, sizeof(buffer));
if (length <= 0) {
return;
}
callback(buffer, length, arg);
}
static ssize_t
raw_socket_tx (struct rawdev *dev, const uint8_t *buffer, size_t length) {
struct raw_socket_priv *priv;
priv = (struct raw_socket_priv *)dev->priv;
return write(priv->fd, buffer, length);
}
static int
raw_socket_addr (struct rawdev *dev, uint8_t *dst, size_t size) {
int soc;
struct ifreq ifr;
soc = socket(AF_INET, SOCK_DGRAM, 0);
if (soc == -1) {
perror("socket");
return -1;
}
ifr.ifr_addr.sa_family = AF_INET;
strncpy(ifr.ifr_name, dev->name, sizeof(ifr.ifr_name) - 1);
if (ioctl(soc, SIOCGIFHWADDR, &ifr) == -1) {
perror("ioctl");
close(soc);
return -1;
}
memcpy(dst, ifr.ifr_hwaddr.sa_data, size);
close(soc);
return 0;
}
static struct rawdev_ops raw_socket_ops = {
.open = raw_socket_open,
.close = raw_socket_close,
.rx = raw_socket_rx,
.tx = raw_socket_tx,
.addr = raw_socket_addr,
};
int
raw_socket_init (void) {
return rawdev_register(RAWDEV_TYPE_SOCKET, &raw_socket_ops);
}