Skip to content

Commit 6b422a1

Browse files
committed
added the pcap dump function
1 parent d920204 commit 6b422a1

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

pcap_session.cc

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void PcapSession::Init(Handle<Object> exports) {
2828
Nan::SetPrototypeMethod(tpl, "close", Close);
2929
Nan::SetPrototypeMethod(tpl, "stats", Stats);
3030
Nan::SetPrototypeMethod(tpl, "inject", Inject);
31+
Nan::SetPrototypeMethod(tpl, "create_pcapDump", CreatePcapDump);
3132

3233
constructor.Reset(tpl->GetFunction());
3334
exports->Set(Nan::New("PcapSession").ToLocalChecked(), tpl->GetFunction());
@@ -126,6 +127,7 @@ void PcapSession::Dispatch(const Nan::FunctionCallbackInfo<Value>& info)
126127

127128
void PcapSession::Open(bool live, const Nan::FunctionCallbackInfo<Value>& info)
128129
{
130+
//this.device_name, this.filter, this.buffer_size, this.outfile, packet_ready, this.is_monitor)
129131
Nan::HandleScope scope;
130132
char errbuf[PCAP_ERRBUF_SIZE];
131133

@@ -380,3 +382,121 @@ void PcapSession::Inject(const Nan::FunctionCallbackInfo<Value>& info)
380382
}
381383
return;
382384
}
385+
386+
void PcapSession::CreatePcapDump(const Nan::FunctionCallbackInfo<Value>& info)
387+
{
388+
//this.device_name, this.filter, this.buffer_size, this.outfile, packet_ready, this.is_monitor)
389+
Nan::HandleScope scope;
390+
char errbuf[PCAP_ERRBUF_SIZE];
391+
392+
if (info.Length() == 7) {
393+
if (!info[0]->IsString()) {
394+
Nan::ThrowTypeError("pcap Open: info[0] must be a String");
395+
return;
396+
}
397+
if (!info[1]->IsString()) {
398+
Nan::ThrowTypeError("pcap Open: info[1] must be a String");
399+
return;
400+
}
401+
if (!info[2]->IsInt32()) {
402+
Nan::ThrowTypeError("pcap Open: info[2] must be a Number");
403+
return;
404+
}
405+
if (!info[3]->IsString()) {
406+
Nan::ThrowTypeError("pcap Open: info[3] must be a String");
407+
return;
408+
}
409+
if (!info[4]->IsFunction()) {
410+
Nan::ThrowTypeError("pcap Open: info[4] must be a Function");
411+
return;
412+
}
413+
if (!info[5]->IsBoolean()) {
414+
Nan::ThrowTypeError("pcap Open: info[5] must be a Boolean");
415+
return;
416+
}
417+
if (!info[6]->IsInt32()) {
418+
Nan::ThrowTypeError("pcap Open: info[6] must be a Number");
419+
return;
420+
}
421+
} else {
422+
Nan::ThrowTypeError("pcap CreatePcapDump: expecting 7 arguments");
423+
return;
424+
}
425+
Nan::Utf8String device(info[0]->ToString());
426+
Nan::Utf8String filter(info[1]->ToString());
427+
int buffer_size = info[2]->Int32Value();
428+
Nan::Utf8String pcap_output_filename(info[3]->ToString());
429+
int noOfPackets = info[6]->Int32Value();
430+
431+
PcapSession* session = Nan::ObjectWrap::Unwrap<PcapSession>(info.This());
432+
433+
session->packet_ready_cb.Reset(info[4].As<Function>());
434+
session->pcap_dump_handle = NULL;
435+
436+
437+
if (pcap_lookupnet((char *) *device, &session->net, &session->mask, errbuf) == -1) {
438+
session->net = 0;
439+
session->mask = 0;
440+
fprintf(stderr, "warning: %s - this may not actually work\n", errbuf);
441+
}
442+
443+
session->pcap_handle = pcap_create((char *) *device, errbuf);
444+
if (session->pcap_handle == NULL) {
445+
Nan::ThrowError(errbuf);
446+
return;
447+
}
448+
449+
// 64KB is the max IPv4 packet size
450+
if (pcap_set_snaplen(session->pcap_handle, 65535) != 0) {
451+
Nan::ThrowError("error setting snaplen");
452+
return;
453+
}
454+
455+
// always use promiscuous mode
456+
if (pcap_set_promisc(session->pcap_handle, 1) != 0) {
457+
Nan::ThrowError("error setting promiscuous mode");
458+
return;
459+
}
460+
461+
// Try to set buffer size. Sometimes the OS has a lower limit that it will silently enforce.
462+
if (pcap_set_buffer_size(session->pcap_handle, buffer_size) != 0) {
463+
Nan::ThrowError("error setting buffer size");
464+
return;
465+
}
466+
467+
// set "timeout" on read, even though we are also setting nonblock below. On Linux this is required.
468+
if (pcap_set_timeout(session->pcap_handle, 1000) != 0) {
469+
Nan::ThrowError("error setting read timeout");
470+
return;
471+
}
472+
473+
474+
475+
if (pcap_activate(session->pcap_handle) != 0) {
476+
Nan::ThrowError(pcap_geterr(session->pcap_handle));
477+
return;
478+
}
479+
480+
if (strlen((char *) *pcap_output_filename) > 0) {
481+
session->pcap_dump_handle = pcap_dump_open(session->pcap_handle, (char *) *pcap_output_filename);
482+
if (session->pcap_dump_handle == NULL) {
483+
Nan::ThrowError("error opening dump");
484+
return;
485+
}
486+
}
487+
488+
489+
if (filter.length() != 0) {
490+
if (pcap_compile(session->pcap_handle, &session->fp, (char *) *filter, 1, session->net) == -1) {
491+
Nan::ThrowError(pcap_geterr(session->pcap_handle));
492+
return;
493+
}
494+
495+
if (pcap_setfilter(session->pcap_handle, &session->fp) == -1) {
496+
Nan::ThrowError(pcap_geterr(session->pcap_handle));
497+
return;
498+
}
499+
pcap_freecode(&session->fp);
500+
}
501+
502+
}

pcap_session.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class PcapSession : public Nan::ObjectWrap {
2121
static void Close(const Nan::FunctionCallbackInfo<v8::Value>& info);
2222
static void Stats(const Nan::FunctionCallbackInfo<v8::Value>& info);
2323
static void Inject(const Nan::FunctionCallbackInfo<v8::Value>& info);
24+
static void CreatePcapDump(const Nan::FunctionCallbackInfo<v8::Value>& info);
25+
2426
static void PacketReady(u_char *callback_p, const struct pcap_pkthdr* pkthdr, const u_char* packet);
2527

2628
Nan::Persistent<v8::Function> packet_ready_cb;

0 commit comments

Comments
 (0)