/
pcaploop.cpp
333 lines (284 loc) · 9.1 KB
/
pcaploop.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "config.h"
#include "pcaploop.h"
#include "helper.h"
#include <base/llog.h>
#include <unordered_map>
#ifndef PCAPLOOP_USE_POLL
#if defined(_WIN32)
#define PCAPLOOP_USE_POLL 0
#else
#define PCAPLOOP_USE_POLL 1
#endif
#endif
typedef struct uv_pcap_interf_s uv_pcap_interf_t;
typedef void (*uv_pcap_interf_cb)(uv_pcap_interf_t *handle, const struct pcap_pkthdr *pkt_header, const u_char *packet);
typedef void (*uv_pcap_interf_close_cb)(uv_pcap_interf_t *handle);
static int uv_pcap_interf_init(uv_loop_t *loop, uv_pcap_interf_t *handle, uv_pcap_interf_cb cb, pcap_t *dev, uint8_t *mac);
static void uv_pcap_interf_close(uv_pcap_interf_t *handle, uv_close_cb cb);
static int uv_pcap_interf_sendpacket(uv_pcap_interf_t *handle, const u_char *data, int size);
static u_char EmptyMac[6] = {0,0,0,0,0,0};
struct uv_pcap_interf_s {
#if PCAPLOOP_USE_POLL
int fd;
uv_poll_t poll;
#else
uv_async_t get_packet_async;
uv_sem_t get_packet_sem;
uv_thread_t libpcap_thread;
const struct pcap_pkthdr *pkthdr;
const u_char *packet;
#endif
pcap_t *dev;
uv_pcap_interf_cb callback;
uint8_t mac[6];
void *data;
};
struct uv_pcap_inner {
std::unordered_map<uint64_t, uv_pcap_interf_t *> map;
uv_pcap_interf_t *interfaces;
int count;
};
static uint64_t mac2int(const uint8_t *mac)
{
uint64_t r = 0;
memcpy(&r, mac, 6);
return r;
}
static void int2mac(const uint64_t i, uint8_t *mac)
{
memcpy(mac, &i, 6);
}
static void set_filter(pcap_t *dev, const uint8_t *mac)
{
char filter[100];
static struct bpf_program bpf;
uint32_t mask = READ_NET32(str2ip(SUBNET_MASK), 0);
int num;
for (num = 0; mask != 0 && num < 32; num++) mask <<= 1;
snprintf(filter, sizeof(filter), "net %s/%d and not ether src %02x:%02x:%02x:%02x:%02x:%02x", SUBNET_NET, num,
mac[0],
mac[1],
mac[2],
mac[3],
mac[4],
mac[5]
);
// LLOG(LLOG_DEBUG, "filter: %s", filter);
pcap_compile(dev, &bpf, filter, 1, 0);
pcap_setfilter(dev, &bpf);
pcap_freecode(&bpf);
}
static void uv_pcap_callback(uv_pcap_interf_t *h, const struct pcap_pkthdr *pkt_header, const u_char *packet) {
uv_pcap_t *handle = (uv_pcap_t *)h->data;
auto key = mac2int(packet + 0);
handle->inner->map[key] = h;
handle->cb(handle, pkt_header, packet, h->mac);
}
int uv_pcap_sendpacket(uv_pcap_t *handle, const u_char *data, int size)
{
auto inner = handle->inner;
auto key = mac2int(data + 6);
auto map = inner->map;
auto search = map.find(key);
if (search != map.end()) {
auto item = search->second;
int ret = uv_pcap_interf_sendpacket(item, data, size);
if (ret != 0) {
LLOG(LLOG_DEBUG, "uv_pcap_interf_sendpacket failed %d", ret);
}
} else {
for (int i = 0; i < inner->count; i++) {
int ret = uv_pcap_interf_sendpacket(&inner->interfaces[i], data, size);
if (ret != 0) {
LLOG(LLOG_DEBUG, "uv_pcap_interf_sendpacket failed %d", ret);
}
}
// LLOG(LLOG_DEBUG, "cache not hit %llu", key);
}
return 0;
}
int uv_pcap_init(uv_loop_t *loop, uv_pcap_t *handle, uv_pcap_cb cb)
{
handle->cb = cb;
handle->inner = new uv_pcap_inner;
auto inner = handle->inner;
pcap_if_t *alldevs;
char err_buf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&alldevs, err_buf)) {
fprintf(stderr, "Error pcap_findalldevs: %s\n", err_buf);
exit(1);
}
int i = 0;
pcap_if_t *d;
for (d = alldevs; d; d = d->next) {
i++;
}
if (i == 0) {
fprintf(stderr, "Error pcap_findalldevs 0 item\n");
exit(1);
}
inner->interfaces = new uv_pcap_interf_t[i];
i = 0;
for (d = alldevs; d; d = d->next) {
pcap_t *dev;
int ret;
dev = pcap_open_live(d->name, 65535, 1, 500, err_buf);
if (!dev) {
LLOG(LLOG_DEBUG, "open %s fail", d->name);
continue;
}
auto datalink = pcap_datalink(dev);
if (datalink != DLT_EN10MB) {
LLOG(LLOG_DEBUG, "open %s fail: datalink(%d)", d->name, datalink);
goto fail_next;
}
uint8_t mac[6];
if (get_mac_address(d, dev, mac) != 0) {
LLOG(LLOG_DEBUG, "open %s fail: get mac", d->name);
goto fail_next;
}
if (memcmp(EmptyMac, mac, 6) == 0) {
LLOG(LLOG_DEBUG, "open %s fail: get all zero mac", d->name);
goto fail_next;
}
set_filter(dev, mac);
if (set_immediate_mode(dev) == -1) {
LLOG(LLOG_DEBUG, "open %s fail: set_immediate_mode %s", d->name, strerror(errno));
goto fail_next;
}
ret = uv_pcap_interf_init(loop, &inner->interfaces[i], uv_pcap_callback, dev, mac);
if (ret) {
LLOG(LLOG_DEBUG, "open %s fail: pcap init", d->name);
goto fail_next;
}
inner->interfaces[i].data = handle;
i++;
LLOG(LLOG_DEBUG, "open %s ok", d->name);
continue;
fail_next:
pcap_close(dev);
}
inner->count = i;
pcap_freealldevs(alldevs);
printf("pcap loop start\n");
return 0;
}
void uv_pcap_close(uv_pcap_t *handle)
{
auto inner = handle->inner;
inner->map.clear();
for (int i = 0; i < inner->count; i++) {
uv_pcap_interf_close(&inner->interfaces[i], NULL);
}
printf("pcap loop stop\n");
}
static int uv_pcap_interf_sendpacket(uv_pcap_interf_t *handle, const u_char *data, int size)
{
u_char old[6];
u_char *d = (u_char *)data;
CPY_MAC(old, d + 6);
CPY_MAC(d + 6, handle->mac);
int ret = pcap_sendpacket(handle->dev, data, size);
CPY_MAC(d + 6, old);
return ret;
}
#if PCAPLOOP_USE_POLL
static void poll_handler(uv_poll_t *handle, int status, int events);
static int uv_pcap_interf_init(uv_loop_t *loop, uv_pcap_interf_t *handle, uv_pcap_interf_cb cb, pcap_t *dev, uint8_t *mac)
{
int ret;
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_setnonblock(dev, 1, errbuf) == -1) {
LLOG(LLOG_ERROR, "setnonblock %s", errbuf);
}
handle->poll.data = handle;
handle->fd = pcap_get_selectable_fd(dev);
handle->dev = dev;
handle->callback = cb;
CPY_MAC(handle->mac, mac);
ret = uv_poll_init(loop, &handle->poll, handle->fd);
if (ret) return ret;
ret = uv_poll_start(&handle->poll, UV_READABLE, poll_handler);
if (ret) return ret;
return 0;
}
static void uv_pcap_interf_close(uv_pcap_interf_t *handle, uv_close_cb cb)
{
int ret = uv_poll_stop(&handle->poll);
if (ret) {
LLOG(LLOG_ERROR, "uv_poll_stop %d", ret);
}
}
static void poll_callback(u_char *data, const struct pcap_pkthdr *pkt_header, const u_char *packet)
{
uv_pcap_interf_t *handle = (uv_pcap_interf_t *)data;
handle->callback(handle, pkt_header, packet);
}
static void poll_handler(uv_poll_t *poll, int status, int events)
{
uv_pcap_interf_t *handle = (uv_pcap_interf_t *)poll->data;
int count;
if (events & UV_READABLE) {
do {
count = pcap_dispatch(handle->dev, 1, poll_callback, (u_char *)handle);
} while (count > 0);
}
}
#else
static void get_packet_async_cb(uv_async_t *async);
static void libpcap_thread_func(void *data);
static void libpcap_handler(u_char *data, const struct pcap_pkthdr *pkt_header, const u_char *packet);
int uv_pcap_interf_init(uv_loop_t *loop, uv_pcap_interf_t *handle, uv_pcap_interf_cb cb, pcap_t *dev, uint8_t *mac)
{
int ret;
ret = uv_async_init(loop, &handle->get_packet_async, get_packet_async_cb);
if (ret) return ret;
ret = uv_sem_init(&handle->get_packet_sem, 0);
if (ret) return ret;
handle->get_packet_async.data = handle;
handle->callback = cb;
handle->dev = dev;
CPY_MAC(handle->mac, mac);
ret = uv_thread_create(&handle->libpcap_thread, libpcap_thread_func, handle);
if (ret) {
LLOG(LLOG_ERROR, "uv_thread_create %d", ret);
return ret;
}
return 0;
}
void uv_pcap_interf_close(uv_pcap_interf_t *handle, uv_close_cb cb)
{
pcap_breakloop(handle->dev);
int ret = uv_thread_join(&handle->libpcap_thread);
if (ret) {
LLOG(LLOG_ERROR, "uv_thread_join %d", ret);
}
uv_close((uv_handle_t *)&handle->get_packet_async, cb);
}
static void get_packet_async_cb(uv_async_t *async)
{
uv_pcap_interf_t *handle = (uv_pcap_interf_t *)async->data;
handle->callback(handle, handle->pkthdr, handle->packet);
uv_sem_post(&handle->get_packet_sem);
}
static void libpcap_thread_func(void *data)
{
uv_pcap_interf_t *handle = (uv_pcap_interf_t *)data;
int ret = 0;
ret = pcap_loop(handle->dev, -1, libpcap_handler, (u_char *)data);
if (ret < 0 && ret != PCAP_ERROR_BREAK) {
LLOG(LLOG_ERROR, "pcap_loop %d", ret);
}
pcap_close(handle->dev);
}
static void libpcap_handler(u_char *data, const struct pcap_pkthdr *pkt_header, const u_char *packet)
{
uv_pcap_interf_t *handle = (uv_pcap_interf_t *)data;
handle->pkthdr = pkt_header;
handle->packet = packet;
if (uv_async_send(&handle->get_packet_async)) {
LLOG(LLOG_WARNING, "libpcap_handler uv_async_send");
}
uv_sem_wait(&handle->get_packet_sem);
}
#endif