-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
86 lines (78 loc) · 2.28 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
/**
* The Pcap writer.
*/
'use strict';
var _ = require('lodash');
var fs = require('fs');
var process = require('process');
var GlobalHeader = require('./lib/header/globalhdr');
var PacketHeader = require('./lib/header/packethdr');
var Constants = require('./lib/constants');
/**
* Write `data` to a `stream`. if the buffer is full will block
* until it's flushed and ready to be written again.
* [see](https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback)
*/
function lowWrite(stream, data) {
return new Promise((resolve, reject) => {
if (stream.write(data)) {
process.nextTick(resolve);
} else {
stream.once("drain", () => {
stream.off("error", reject);
resolve();
});
stream.once("error", reject);
}
});
}
/**
* Initialize new pcap writer.
* Also writes Global header in the file.
* @param {String} file Name of the file to be crated with file path.
*/
function PcapWriter(file, snaplen, linktype) {
this._fs = fs.createWriteStream(file);
this._fs.on('error', (err) => {
this.error = err;
});
var options = {};
if (snaplen) { options.snaplen = snaplen; }
if (linktype) { options.linktype = linktype; }
// write global header.
lowWrite(this._fs, new Buffer((new GlobalHeader(options)).toString(), Constants.HEADER_ENCODING));
}
/**
* Write new packet in file
* @param {Buffer} pkt Buffer containing data.
* @param {Number} ts Timestamp [optional].
*/
PcapWriter.prototype.writePacket = function(pkt, ts) {
if (!ts) { // if no timestamp is provided then default to current.
ts = (new Date()).getTime() * 1000;
}
var n = pkt.length;
var ph = new PacketHeader({
tv_sec: parseInt(parseInt(ts, Constants.INT_BASE) / Constants.M_SEC, Constants.INT_BASE),
tv_usec: parseInt(((ts / Constants.M_SEC) - parseInt(ts/Constants.M_SEC, Constants.INT_BASE)) *
Constants.M_SEC_F, Constants.INT_BASE),
caplen: n,
len: n
});
if(undefined == this.error) {
// write packet header
lowWrite(this._fs, new Buffer(ph.toString(), Constants.HEADER_ENCODING));
// write packet data
lowWrite(this._fs, pkt);
}
else {
throw this.error;
}
};
/**
* Close file stream.
*/
PcapWriter.prototype.close = function() {
return this._fs.end();
};
module.exports = PcapWriter;