Skip to content

Commit ed491cb

Browse files
committed
since being async instead of throwing Nan::Error we setErrorMessage and return; nodejs would be called back the error as the first argument
1 parent 3101eb4 commit ed491cb

File tree

4 files changed

+45
-42
lines changed

4 files changed

+45
-42
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ build
3131

3232
#OSX garbage
3333
.DS_Store
34-
examples/*.pcap
34+
examples/*.pcap
35+
.vscode/

examples/pcap_dump_test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33

44
var pcap = require("../pcap"),
55

6-
pcap_dump = new pcap.PcapDumpSession('en0', "ip proto \\tcp",10*1024*1024,"tmp91.pcap",false,5);
6+
pcap_dump = new pcap.PcapDumpSession('en0', "ip proto \\tcp",10*1024*1024,"tmp95.pcap",false,5);
77

88
pcap_dump.on('pcap_write_complete_async',function(message){
99
console.log("done.....",message);
1010
});
1111

12+
pcap_dump.on('pcap_write_error',function(message){
13+
console.log("pcap_write_error.....",message);
14+
});
15+
1216
//pcap_dump.start();
1317
pcap_dump.startAsyncCapture();

pcap_binding.cc

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,59 +50,61 @@ class PcapWorker : public AsyncWorker {
5050
net = 0;
5151
mask = 0;
5252
fprintf(stderr, "warning: %s - this may not actually work\n", errbuf);
53+
SetErrorMessage(errbuf);
54+
return;
5355
}
5456
pcap_handle = pcap_create(device.c_str(), errbuf);
5557
if (pcap_handle == NULL) {
56-
Nan::ThrowError(errbuf);
58+
SetErrorMessage(errbuf);
5759
return;
5860
}
5961

6062
// 64KB is the max IPv4 packet size
6163
if (pcap_set_snaplen(pcap_handle, 65535) != 0) {
62-
Nan::ThrowError("error setting snaplen");
64+
SetErrorMessage("error setting snaplen");
6365
return;
6466
}
6567

6668
// always use promiscuous mode
6769
if (pcap_set_promisc(pcap_handle, 1) != 0) {
68-
Nan::ThrowError("error setting promiscuous mode");
70+
SetErrorMessage("error setting promiscuous mode");
6971
return;
7072
}
7173

7274
// Try to set buffer size. Sometimes the OS has a lower limit that it will silently enforce.
7375
if (pcap_set_buffer_size(pcap_handle, buffer_size) != 0) {
74-
Nan::ThrowError("error setting buffer size");
76+
SetErrorMessage("error setting buffer size");
7577
return;
7678
}
7779

7880

7981
// set "timeout" on read, even though we are also setting nonblock below. On Linux this is required.
8082
if (pcap_set_timeout(pcap_handle, 1000) != 0) {
81-
Nan::ThrowError("error setting read timeout");
83+
SetErrorMessage("error setting read timeout");
8284
return;
8385
}
8486

8587
if (pcap_activate(pcap_handle) != 0) {
86-
Nan::ThrowError(pcap_geterr(pcap_handle));
88+
SetErrorMessage(pcap_geterr(pcap_handle));
8789
return;
8890
}
8991
if ((pcap_output_filename.size()) > 0) {
9092
pcap_dump_handle = pcap_dump_open(pcap_handle,pcap_output_filename.c_str());
9193
if (pcap_dump_handle == NULL) {
92-
Nan::ThrowError("error opening dump");
94+
SetErrorMessage("error opening dump");
9395
return;
9496
}
9597
}
9698

9799

98100
if (filter.size() != 0) {
99101
if (pcap_compile(pcap_handle, &fp, filter.c_str(), 1, net) == -1) {
100-
Nan::ThrowError(pcap_geterr(pcap_handle));
102+
SetErrorMessage(pcap_geterr(pcap_handle));
101103
return;
102104
}
103105

104106
if (pcap_setfilter(pcap_handle, &fp) == -1) {
105-
Nan::ThrowError(pcap_geterr(pcap_handle));
107+
SetErrorMessage(pcap_geterr(pcap_handle));
106108
return;
107109
}
108110

@@ -120,16 +122,6 @@ class PcapWorker : public AsyncWorker {
120122
}
121123
}
122124

123-
void HandleErrorCallback(){
124-
std::string error = std::string(errbuf);
125-
Local<Value> argv[] = {
126-
Nan::EmptyString()//TODO convert errbuf to error Message
127-
,Nan::Null()
128-
};
129-
130-
callback->Call(2, argv);
131-
}
132-
133125
// Executed when the async work is complete
134126
// this function will be run inside the main event loop
135127
// so it is safe to use V8 again

pcap_dump.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var binding = require("./build/Release/pcap_binding");
44

55

66
function PcapDumpSession(device_name, filter, buffer_size, outfile, is_monitor, number_of_packets_to_be_read) {
7-
7+
88
this.device_name = device_name;
99
this.filter = filter || "";
1010
this.buffer_size = buffer_size;
@@ -29,47 +29,53 @@ util.inherits(PcapDumpSession, events.EventEmitter);
2929

3030
exports.lib_version = binding.lib_version();
3131

32-
exports.findalldevs = function() {
32+
exports.findalldevs = function () {
3333
return binding.findalldevs();
3434
};
3535

3636

37-
PcapDumpSession.prototype.startAsyncCapture = function() {
38-
37+
PcapDumpSession.prototype.startAsyncCapture = function () {
38+
3939
this.opened = true;
4040
binding.create_pcap_dump_async(
41-
this.device_name,
42-
this.filter,
43-
this.buffer_size,
44-
this.outfile,
45-
PcapDumpSession.prototype.on_packet.bind(this),
46-
this.is_monitor,
47-
this.number_of_packets_to_be_read,
48-
PcapDumpSession.prototype.on_pcap_write_complete_async.bind(this)
41+
this.device_name,
42+
this.filter,
43+
this.buffer_size,
44+
this.outfile,
45+
PcapDumpSession.prototype.on_packet.bind(this),
46+
this.is_monitor,
47+
this.number_of_packets_to_be_read,
48+
PcapDumpSession.prototype.on_pcap_write_complete_async.bind(this)
4949
);
5050
};
5151

5252

53-
PcapDumpSession.prototype.close = function() {
53+
PcapDumpSession.prototype.close = function () {
5454
this.opened = false;
5555
this.session.close();
5656
};
5757

58-
PcapDumpSession.prototype.stats = function() {
58+
PcapDumpSession.prototype.stats = function () {
5959
return this.session.stats();
6060
};
6161

62-
PcapDumpSession.prototype.on_pcap_write_complete_async = function(err,packet_count) {
63-
this.emit("pcap_write_complete_async",{
64-
"packets_read":packet_count ,
65-
"fileName":this.outfile
62+
PcapDumpSession.prototype.on_pcap_write_complete_async = function (err, packet_count) {
63+
64+
if (err) {
65+
this.emit("pcap_write_error", err);
66+
67+
} else {
68+
this.emit("pcap_write_complete_async", {
69+
"packets_read": packet_count,
70+
"fileName": this.outfile
6671
});
72+
}
6773

6874
};
6975

7076

71-
PcapDumpSession.prototype.on_packet = function(packet) {
72-
this.emit("packet",packet);
77+
PcapDumpSession.prototype.on_packet = function (packet) {
78+
this.emit("packet", packet);
7379

7480
};
7581

@@ -79,6 +85,6 @@ PcapDumpSession.prototype.on_packet = function(packet) {
7985
exports.PcapDumpSession = PcapDumpSession;
8086

8187

82-
exports.createPcapDumpSession = function(device, filter, buffer_size, path, monitor, number_of_packets) {
88+
exports.createPcapDumpSession = function (device, filter, buffer_size, path, monitor, number_of_packets) {
8389
return new PcapDumpSession(device, filter, buffer_size, path, monitor, number_of_packets);
8490
};

0 commit comments

Comments
 (0)