From 904b6a30c0153c96cbf2913aea7e5beba7b6590e Mon Sep 17 00:00:00 2001 From: Mathias Schreck Date: Tue, 26 Jul 2016 17:24:47 +0200 Subject: [PATCH 1/4] Stop watcher before closing the session --- pcap.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pcap.js b/pcap.js index cd31439..be1cc2f 100644 --- a/pcap.js +++ b/pcap.js @@ -101,11 +101,13 @@ PcapSession.prototype.on_packet_ready = function () { PcapSession.prototype.close = function () { this.opened = false; - this.session.close(); + if (this.is_live) { this.readWatcher.stop(); } // TODO - remove listeners so program will exit I guess? + + this.session.close(); }; PcapSession.prototype.stats = function () { From d722b5764e482c24b9ff1a47ddb02d68df02c806 Mon Sep 17 00:00:00 2001 From: Mathias Schreck Date: Tue, 26 Jul 2016 17:25:24 +0200 Subject: [PATCH 2/4] Remove all listeners --- pcap.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pcap.js b/pcap.js index be1cc2f..687ac6d 100644 --- a/pcap.js +++ b/pcap.js @@ -102,10 +102,11 @@ PcapSession.prototype.on_packet_ready = function () { PcapSession.prototype.close = function () { this.opened = false; + this.removeAllListeners(); + if (this.is_live) { this.readWatcher.stop(); } - // TODO - remove listeners so program will exit I guess? this.session.close(); }; From 9afac0cc61895b53cd917aaf00d00ded05d2df04 Mon Sep 17 00:00:00 2001 From: Mathias Schreck Date: Tue, 26 Jul 2016 17:25:47 +0200 Subject: [PATCH 3/4] Fix segmentation fault when calling close --- pcap_session.cc | 13 ++++++++++++- pcap_session.h | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pcap_session.cc b/pcap_session.cc index bdb030c..a708344 100644 --- a/pcap_session.cc +++ b/pcap_session.cc @@ -119,6 +119,10 @@ void PcapSession::Dispatch(const Nan::FunctionCallbackInfo& info) int packet_count; do { packet_count = pcap_dispatch(session->pcap_handle, 1, PacketReady, (u_char *)session); + + if (packet_count == -2) { + FinalizeClose(session); + } } while (packet_count > 0); info.GetReturnValue().Set(Nan::New(packet_count)); @@ -313,9 +317,16 @@ void PcapSession::Close(const Nan::FunctionCallbackInfo& info) session->pcap_dump_handle = NULL; } + if (session->pcap_handle != NULL) { + pcap_breakloop(session->pcap_handle); + } +} + +void PcapSession::FinalizeClose(PcapSession * session) { pcap_close(session->pcap_handle); + session->pcap_handle = NULL; + session->packet_ready_cb.Reset(); - return; } void PcapSession::Fileno(const Nan::FunctionCallbackInfo& info) diff --git a/pcap_session.h b/pcap_session.h index 57bbfc3..7630881 100644 --- a/pcap_session.h +++ b/pcap_session.h @@ -22,6 +22,7 @@ class PcapSession : public Nan::ObjectWrap { static void Stats(const Nan::FunctionCallbackInfo& info); static void Inject(const Nan::FunctionCallbackInfo& info); static void PacketReady(u_char *callback_p, const struct pcap_pkthdr* pkthdr, const u_char* packet); + static void FinalizeClose(PcapSession *session); Nan::Persistent packet_ready_cb; static Nan::Persistent constructor; From c19d3888e60afdf1f7605491e066c5d6db3febe9 Mon Sep 17 00:00:00 2001 From: Mathias Schreck Date: Tue, 26 Jul 2016 17:26:49 +0200 Subject: [PATCH 4/4] Throw if session is already closed --- pcap_session.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pcap_session.cc b/pcap_session.cc index a708344..8234975 100644 --- a/pcap_session.cc +++ b/pcap_session.cc @@ -335,6 +335,11 @@ void PcapSession::Fileno(const Nan::FunctionCallbackInfo& info) PcapSession* session = Nan::ObjectWrap::Unwrap(info.Holder()); + if (session->pcap_handle == NULL) { + Nan::ThrowError("Error: pcap session already closed"); + return; + } + int fd = pcap_get_selectable_fd(session->pcap_handle); info.GetReturnValue().Set(Nan::New(fd)); @@ -348,6 +353,11 @@ void PcapSession::Stats(const Nan::FunctionCallbackInfo& info) PcapSession* session = Nan::ObjectWrap::Unwrap(info.Holder()); + if (session->pcap_handle == NULL) { + Nan::ThrowError("Error: pcap session already closed"); + return; + } + if (pcap_stats(session->pcap_handle, &ps) == -1) { Nan::ThrowError("Error in pcap_stats"); return; @@ -379,6 +389,12 @@ void PcapSession::Inject(const Nan::FunctionCallbackInfo& info) } PcapSession* session = Nan::ObjectWrap::Unwrap(info.Holder()); + + if (session->pcap_handle == NULL) { + Nan::ThrowError("Error: pcap session already closed"); + return; + } + char * bufferData = NULL; size_t bufferLength = 0; Local buffer_obj = info[0]->ToObject();