Skip to content

Commit

Permalink
Merge PR #221 from lo1tuma/seg-fault-on-close
Browse files Browse the repository at this point in the history
Fix segmentation fault on close
  • Loading branch information
roccomuso authored Nov 22, 2018
2 parents aec9d80 + c19d388 commit fb9dfc9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
7 changes: 5 additions & 2 deletions pcap.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,14 @@ PcapSession.prototype.on_packet_ready = function () {

PcapSession.prototype.close = function () {
this.opened = false;
this.session.close();

this.removeAllListeners();

if (this.is_live) {
this.readWatcher.stop();
}
// TODO - remove listeners so program will exit I guess?

this.session.close();
};

PcapSession.prototype.stats = function () {
Expand Down
29 changes: 28 additions & 1 deletion pcap_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ void PcapSession::Dispatch(const Nan::FunctionCallbackInfo<Value>& 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<Integer>(packet_count));
Expand Down Expand Up @@ -313,9 +317,16 @@ void PcapSession::Close(const Nan::FunctionCallbackInfo<Value>& 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<Value>& info)
Expand All @@ -324,6 +335,11 @@ void PcapSession::Fileno(const Nan::FunctionCallbackInfo<Value>& info)

PcapSession* session = Nan::ObjectWrap::Unwrap<PcapSession>(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<Integer>(fd));
Expand All @@ -337,6 +353,11 @@ void PcapSession::Stats(const Nan::FunctionCallbackInfo<Value>& info)

PcapSession* session = Nan::ObjectWrap::Unwrap<PcapSession>(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;
Expand Down Expand Up @@ -368,6 +389,12 @@ void PcapSession::Inject(const Nan::FunctionCallbackInfo<Value>& info)
}

PcapSession* session = Nan::ObjectWrap::Unwrap<PcapSession>(info.Holder());

if (session->pcap_handle == NULL) {
Nan::ThrowError("Error: pcap session already closed");
return;
}

char * bufferData = NULL;
size_t bufferLength = 0;
Local<Object> buffer_obj = info[0]->ToObject();
Expand Down
1 change: 1 addition & 0 deletions pcap_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class PcapSession : public Nan::ObjectWrap {
static void Stats(const Nan::FunctionCallbackInfo<v8::Value>& info);
static void Inject(const Nan::FunctionCallbackInfo<v8::Value>& info);
static void PacketReady(u_char *callback_p, const struct pcap_pkthdr* pkthdr, const u_char* packet);
static void FinalizeClose(PcapSession *session);

Nan::Persistent<v8::Function> packet_ready_cb;
static Nan::Persistent<v8::Function> constructor;
Expand Down

0 comments on commit fb9dfc9

Please sign in to comment.