forked from node-pcap/node_pcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcap_session.cc
385 lines (317 loc) · 11.9 KB
/
pcap_session.cc
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <assert.h>
#include <pcap/pcap.h>
#include <sys/ioctl.h>
#include <cstring>
#include <string.h>
#include "pcap_session.h"
using namespace v8;
Persistent<Function> PcapSession::constructor;
PcapSession::PcapSession() {};
PcapSession::~PcapSession() {};
void PcapSession::Init(Handle<Object> exports) {
NanScope();
// Prepare constructor template
Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New);
tpl->SetClassName(NanNew("PcapSession"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
NODE_SET_PROTOTYPE_METHOD(tpl, "open_live", OpenLive);
NODE_SET_PROTOTYPE_METHOD(tpl, "open_offline", OpenOffline);
NODE_SET_PROTOTYPE_METHOD(tpl, "dispatch", Dispatch);
NODE_SET_PROTOTYPE_METHOD(tpl, "fileno", Fileno);
NODE_SET_PROTOTYPE_METHOD(tpl, "close", Close);
NODE_SET_PROTOTYPE_METHOD(tpl, "stats", Stats);
NODE_SET_PROTOTYPE_METHOD(tpl, "inject", Inject);
NanAssignPersistent(constructor, tpl->GetFunction());
exports->Set(NanNew("PcapSession"), tpl->GetFunction());
}
NAN_METHOD(PcapSession::New) {
NanScope();
if (args.IsConstructCall()) {
// Invoked as constructor: `new MyObject(...)`
PcapSession* obj = new PcapSession();
obj->Wrap(args.This());
NanReturnValue(args.This());
} else {
// Invoked as plain function `MyObject(...)`, turn into construct call.
Local<Function> cons = NanNew<Function>(constructor);
NanReturnValue(cons->NewInstance());
}
}
// PacketReady is called from within pcap, still on the stack of Dispatch. It should be called
// only one time per Dispatch, but sometimes it gets called 0 times. PacketReady invokes the
// JS callback associated with the dispatch() call in JS.
//
// Stack:
// 1. readWatcher.callback (pcap.js)
// 2. session.dispatch (pcap.js)
// 3. Dispatch (pcap_session.cc)
// 4. pcap_dispatch (libpcap)
// 5. PacketReady (pcap_session.cc)
// 6. session.dispatch callback (pcap.js)
//
void PcapSession::PacketReady(u_char *s, const struct pcap_pkthdr* pkthdr, const u_char* packet) {
NanScope();
PcapSession* session = (PcapSession *)s;
if (session->pcap_dump_handle != NULL) {
pcap_dump((u_char *) session->pcap_dump_handle, pkthdr, packet);
}
size_t copy_len = pkthdr->caplen;
if (copy_len > session->buffer_length) {
copy_len = session->buffer_length;
}
memcpy(session->buffer_data, packet, copy_len);
// copy header data to fixed offsets in second buffer from user
memcpy(session->header_data, &(pkthdr->ts.tv_sec), 4);
memcpy(session->header_data + 4, &(pkthdr->ts.tv_usec), 4);
memcpy(session->header_data + 8, &(pkthdr->caplen), 4);
memcpy(session->header_data + 12, &(pkthdr->len), 4);
TryCatch try_catch;
NanMakeCallback(NanGetCurrentContext()->Global(), NanNew(session->packet_ready_cb), 0, NULL);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
}
NAN_METHOD(PcapSession::Dispatch)
{
NanScope();
if (args.Length() != 2) {
NanThrowTypeError("Dispatch takes exactly two arguments");
NanReturnUndefined();
}
if (!node::Buffer::HasInstance(args[0])) {
NanThrowTypeError("First argument must be a buffer");
NanReturnUndefined();
}
if (!node::Buffer::HasInstance(args[1])) {
NanThrowTypeError("Second argument must be a buffer");
NanReturnUndefined();
}
PcapSession* session = ObjectWrap::Unwrap<PcapSession>(args.This());
Local<Object> buffer_obj = args[0]->ToObject();
session->buffer_data = node::Buffer::Data(buffer_obj);
session->buffer_length = node::Buffer::Length(buffer_obj);
Local<Object> header_obj = args[1]->ToObject();
session->header_data = node::Buffer::Data(header_obj);
int packet_count;
do {
packet_count = pcap_dispatch(session->pcap_handle, 1, PacketReady, (u_char *)session);
} while (packet_count > 0);
NanReturnValue(NanNew<Integer>(packet_count));
}
_NAN_METHOD_RETURN_TYPE PcapSession::Open(bool live, _NAN_METHOD_ARGS)
{
NanScope();
char errbuf[PCAP_ERRBUF_SIZE];
if (args.Length() == 6) {
if (!args[0]->IsString()) {
NanThrowTypeError("pcap Open: args[0] must be a String");
NanReturnUndefined();
}
if (!args[1]->IsString()) {
NanThrowTypeError("pcap Open: args[1] must be a String");
NanReturnUndefined();
}
if (!args[2]->IsInt32()) {
NanThrowTypeError("pcap Open: args[2] must be a Number");
NanReturnUndefined();
}
if (!args[3]->IsString()) {
NanThrowTypeError("pcap Open: args[3] must be a String");
NanReturnUndefined();
}
if (!args[4]->IsFunction()) {
NanThrowTypeError("pcap Open: args[4] must be a Function");
NanReturnUndefined();
}
if (!args[5]->IsBoolean()) {
NanThrowTypeError("pcap Open: args[5] must be a Boolean");
NanReturnUndefined();
}
} else {
NanThrowTypeError("pcap Open: expecting 6 arguments");
NanReturnUndefined();
}
NanUtf8String device(args[0]->ToString());
NanUtf8String filter(args[1]->ToString());
int buffer_size = args[2]->Int32Value();
NanUtf8String pcap_output_filename(args[3]->ToString());
PcapSession* session = ObjectWrap::Unwrap<PcapSession>(args.This());
NanAssignPersistent(session->packet_ready_cb, args[4].As<Function>());
session->pcap_dump_handle = NULL;
if (live) {
if (pcap_lookupnet((char *) *device, &session->net, &session->mask, errbuf) == -1) {
session->net = 0;
session->mask = 0;
fprintf(stderr, "warning: %s - this may not actually work\n", errbuf);
}
session->pcap_handle = pcap_create((char *) *device, errbuf);
if (session->pcap_handle == NULL) {
NanThrowError(errbuf);
NanReturnUndefined();
}
// 64KB is the max IPv4 packet size
if (pcap_set_snaplen(session->pcap_handle, 65535) != 0) {
NanThrowError("error setting snaplen");
NanReturnUndefined();
}
// always use promiscuous mode
if (pcap_set_promisc(session->pcap_handle, 1) != 0) {
NanThrowError("error setting promiscuous mode");
NanReturnUndefined();
}
// Try to set buffer size. Sometimes the OS has a lower limit that it will silently enforce.
if (pcap_set_buffer_size(session->pcap_handle, buffer_size) != 0) {
NanThrowError("error setting buffer size");
NanReturnUndefined();
}
// set "timeout" on read, even though we are also setting nonblock below. On Linux this is required.
if (pcap_set_timeout(session->pcap_handle, 1000) != 0) {
NanThrowError("error setting read timeout");
NanReturnUndefined();
}
// fixes a previous to-do that was here.
if (args.Length() == 6) {
if (args[5]->Int32Value()) {
if (pcap_set_rfmon(session->pcap_handle, 1) != 0) {
NanThrowError(pcap_geterr(session->pcap_handle));
NanReturnUndefined();
}
}
}
if (pcap_activate(session->pcap_handle) != 0) {
NanThrowError(pcap_geterr(session->pcap_handle));
NanReturnUndefined();
}
if (strlen((char *) *pcap_output_filename) > 0) {
session->pcap_dump_handle = pcap_dump_open(session->pcap_handle, (char *) *pcap_output_filename);
if (session->pcap_dump_handle == NULL) {
NanThrowError("error opening dump");
NanReturnUndefined();
}
}
if (pcap_setnonblock(session->pcap_handle, 1, errbuf) == -1) {
NanThrowError(errbuf);
NanReturnUndefined();
}
} else {
// Device is the path to the savefile
session->pcap_handle = pcap_open_offline((char *) *device, errbuf);
if (session->pcap_handle == NULL) {
NanThrowError(errbuf);
NanReturnUndefined();
}
}
if (filter.length() != 0) {
if (pcap_compile(session->pcap_handle, &session->fp, (char *) *filter, 1, session->net) == -1) {
NanThrowError(pcap_geterr(session->pcap_handle));
NanReturnUndefined();
}
if (pcap_setfilter(session->pcap_handle, &session->fp) == -1) {
NanThrowError(pcap_geterr(session->pcap_handle));
NanReturnUndefined();
}
pcap_freecode(&session->fp);
}
// Work around buffering bug in BPF on OSX 10.6 as of May 19, 2010
// This may result in dropped packets under load because it disables the (broken) buffer
// http://seclists.org/tcpdump/2010/q1/110
#if defined(__APPLE_CC__) || defined(__APPLE__)
#include <net/bpf.h>
int fd = pcap_get_selectable_fd(session->pcap_handle);
int v = 1;
ioctl(fd, BIOCIMMEDIATE, &v);
// TODO - check return value
#endif
int link_type = pcap_datalink(session->pcap_handle);
Local<Value> ret;
switch (link_type) {
case DLT_NULL:
ret = NanNew("LINKTYPE_NULL");
break;
case DLT_EN10MB: // most wifi interfaces pretend to be "ethernet"
ret = NanNew("LINKTYPE_ETHERNET");
break;
case DLT_IEEE802_11_RADIO: // 802.11 "monitor mode"
ret = NanNew("LINKTYPE_IEEE802_11_RADIO");
break;
case DLT_RAW: // "raw IP"
ret = NanNew("LINKTYPE_RAW");
break;
case DLT_LINUX_SLL: // "Linux cooked capture"
ret = NanNew("LINKTYPE_LINUX_SLL");
break;
default:
snprintf(errbuf, PCAP_ERRBUF_SIZE, "Unknown linktype %d", link_type);
ret = NanNew(errbuf);
break;
}
NanReturnValue(ret);
}
NAN_METHOD(PcapSession::OpenLive)
{
return Open(true, args);
}
NAN_METHOD(PcapSession::OpenOffline)
{
return Open(false, args);
}
NAN_METHOD(PcapSession::Close)
{
NanScope();
PcapSession* session = ObjectWrap::Unwrap<PcapSession>(args.This());
if (session->pcap_dump_handle != NULL) {
pcap_dump_close(session->pcap_dump_handle);
session->pcap_dump_handle = NULL;
}
pcap_close(session->pcap_handle);
NanDisposePersistent(session->packet_ready_cb);
NanReturnUndefined();
}
NAN_METHOD(PcapSession::Fileno)
{
NanScope();
PcapSession* session = ObjectWrap::Unwrap<PcapSession>(args.This());
int fd = pcap_get_selectable_fd(session->pcap_handle);
NanReturnValue(NanNew<Integer>(fd));
}
NAN_METHOD(PcapSession::Stats)
{
NanScope();
struct pcap_stat ps;
PcapSession* session = ObjectWrap::Unwrap<PcapSession>(args.This());
if (pcap_stats(session->pcap_handle, &ps) == -1) {
NanThrowError("Error in pcap_stats");
NanReturnUndefined();
// TODO - use pcap_geterr to figure out what the error was
}
Local<Object> stats_obj = NanNew<Object>();
stats_obj->Set(NanNew("ps_recv"), NanNew<Integer>(ps.ps_recv));
stats_obj->Set(NanNew("ps_drop"), NanNew<Integer>(ps.ps_drop));
stats_obj->Set(NanNew("ps_ifdrop"), NanNew<Integer>(ps.ps_ifdrop));
// ps_ifdrop may not be supported on this platform, but there's no good way to tell, is there?
NanReturnValue(stats_obj);
}
NAN_METHOD(PcapSession::Inject)
{
NanScope();
if (args.Length() != 1) {
NanThrowTypeError("Inject takes exactly one argument");
NanReturnUndefined();
}
if (!node::Buffer::HasInstance(args[0])) {
NanThrowTypeError("First argument must be a buffer");
NanReturnUndefined();
}
PcapSession* session = ObjectWrap::Unwrap<PcapSession>(args.This());
char * bufferData = NULL;
size_t bufferLength = 0;
Local<Object> buffer_obj = args[0]->ToObject();
bufferData = node::Buffer::Data(buffer_obj);
bufferLength = node::Buffer::Length(buffer_obj);
if (pcap_inject(session->pcap_handle, bufferData, bufferLength) != (int)bufferLength) {
NanThrowError("Pcap inject failed.");
NanReturnUndefined();
}
NanReturnUndefined();
}