-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpcap_dump.js
More file actions
79 lines (54 loc) · 2.07 KB
/
pcap_dump.js
File metadata and controls
79 lines (54 loc) · 2.07 KB
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
var util = require("util");
var events = require("events");
var binding = require("./build/Release/pcap_binding");
function PcapDumpSession(device_name, filter, buffer_size, outfile, is_monitor, number_of_packets_to_be_read) {
this.device_name = device_name;
this.filter = filter || "";
this.buffer_size = buffer_size;
this.outfile = outfile || "tmp.pcap";
this.is_monitor = Boolean(is_monitor);
this.packets_read = 0;
this.number_of_packets_to_be_read = number_of_packets_to_be_read || 1;
if (typeof this.buffer_size === "number" && !isNaN(this.buffer_size)) {
this.buffer_size = Math.round(this.buffer_size);
} else {
this.buffer_size = 10 * 1024 * 1024; // Default buffer size is 10MB
}
this.device_name = this.device_name || binding.default_device();
events.EventEmitter.call(this);
}
util.inherits(PcapDumpSession, events.EventEmitter);
exports.lib_version = binding.lib_version();
exports.findalldevs = function () {
return binding.findalldevs();
};
PcapDumpSession.prototype.startAsyncCapture = function () {
this.opened = true;
binding.pcap_dump_async(
this.device_name,
this.filter,
this.buffer_size,
this.outfile,
PcapDumpSession.prototype.on_packet.bind(this),
this.is_monitor,
this.number_of_packets_to_be_read,
PcapDumpSession.prototype.on_pcap_write_complete_async.bind(this)
);
};
PcapDumpSession.prototype.on_pcap_write_complete_async = function (err, packet_count) {
if (err) {
this.emit("pcap_write_error", err);
} else {
this.emit("pcap_write_complete_async", {
"packets_read": packet_count,
"fileName": this.outfile
});
}
};
PcapDumpSession.prototype.on_packet = function (packet) {
this.emit("packet", packet);
};
exports.PcapDumpSession = PcapDumpSession;
exports.createPcapDumpSession = function (device, filter, buffer_size, path, monitor, number_of_packets) {
return new PcapDumpSession(device, filter, buffer_size, path, monitor, number_of_packets);
};