This repository has been archived by the owner on Sep 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
87 lines (73 loc) · 2.06 KB
/
index.js
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
80
81
82
83
84
85
86
87
var events = require('events');
var binding = require('./build/Release/pcap');
function onPacket(handle, slab, start, len, pktinfo) {
var self = handle.owner;
if(!slab) return self.emit('error');
self.emit('packet', slab.slice(start, start + len), pktinfo);
}
function Pcap() {
events.EventEmitter.call(this);
this._handle = new binding.Pcap();
this._handle.owner = this;
this._handle.onpacket = onPacket;
this.isOpen = false;
this.isLive = false;
}
require('util').inherits(Pcap, events.EventEmitter);
Pcap.prototype._healthCheck = function() {
if (!this._handle)
throw new Error('Not running');
};
Pcap.prototype.setFilter = function(filter) {
this._healthCheck();
if(typeof filter == 'string')
this._handle.setFilter(filter)
}
Pcap.prototype.stats = function() {
this._healthCheck();
return this._handle.stats();
}
Pcap.prototype.dispatch = function(callback) {
this._healthCheck();
if(typeof callback === 'function')
this.on('packet', callback);
this._handle.dispatch();
this.isRunning = true;
}
Pcap.prototype.inject = function(buffer) {
this._healthCheck();
return this._handle.inject(buffer);
}
Pcap.prototype.close = function() {
this._healthCheck();
this._handle.close();
this._handle = null;
this.emit('close');
}
exports.createOnlineSession = function(device, promisc) {
var session = new Pcap();
session._handle.openOnline(device, promisc || false);
session.isOpen = true;
session.isLive = true;
return session;
}
/**
exports.createOfflineSession = function(file) {
var session = new Pcap();
session._handle.openOffline(file);
session.isOpen = true;
return session;
}
*/
exports.findAllDevices = binding.findAllDevices;
exports.libraryVersion = function() {
// get the library version for the first time
libraryVersion = binding.libraryVersion();
// override the libraryVersion() function
// to simply return the text provided by the binding
// there is no need to call C++ again for this instance
exports.libraryVersion = function() {
return libraryVersion;
}
return libraryVersion;
}