Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timestamp property on 'packet' listener #88

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ var linkType = c.open(device, filter, bufSize, buffer);

c.setMinBytes && c.setMinBytes(0);

c.on('packet', function(nbytes, trunc) {
c.on('packet', function(nbytes, trunc, ts) {
console.log('packet: length ' + nbytes + ' bytes, truncated? '
+ (trunc ? 'yes' : 'no'));
const timestamp = Number(`${ts.seconds}${Math.floor(ts.microseconds/1000)}`);
console.log(`timestamp: ${new Date(timestamp).toISOString()}`);

// raw packet data === buffer.slice(0, nbytes)

Expand Down
16 changes: 12 additions & 4 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,23 @@ class Pcap : public Nan::ObjectWrap {
}
memcpy(obj->buffer_data, pkt_data, copy_len);

Local<Value> emit_argv[3] = {
Local<Object> Timestamp;
Timestamp = Nan::New<Object>();
Timestamp->Set(Nan::New<String>("seconds").ToLocalChecked(),
Nan::New<Number>(pkt_hdr->ts.tv_sec));
Timestamp->Set(Nan::New<String>("microseconds").ToLocalChecked(),
Nan::New<Number>(pkt_hdr->ts.tv_usec));

Local<Value> emit_argv[4] = {
Nan::New<String>(packet_symbol),
Nan::New<Number>(copy_len),
Nan::New<Boolean>(truncated)
Nan::New<Boolean>(truncated),
Timestamp
};
Nan::MakeCallback(
Nan::New<Object>(obj->persistent()),
Nan::New<Function>(obj->Emit),
3,
4,
emit_argv
);
}
Expand Down Expand Up @@ -183,7 +191,7 @@ class Pcap : public Nan::ObjectWrap {
}
#else
static void cb_packets(uv_poll_t* handle, int status, int events) {
assert(status == 0);
// assert(status == 0);
Pcap *obj = (Pcap*)handle->data;
int packet_count;

Expand Down