Skip to content

Commit

Permalink
added support to dump the traffic to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Ferner committed Nov 17, 2011
1 parent ba1b20e commit cb331da
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
6 changes: 3 additions & 3 deletions pcap.js
Expand Up @@ -30,7 +30,7 @@ Pcap.prototype.findalldevs = function () {
return binding.findalldevs();
};

Pcap.prototype.open = function (live, device, filter, buffer_size) {
Pcap.prototype.open = function (live, device, filter, buffer_size, pcap_output_filename) {
var me = this;

if (typeof buffer_size === 'number' && !isNaN(buffer_size)) {
Expand All @@ -41,10 +41,10 @@ Pcap.prototype.open = function (live, device, filter, buffer_size) {

if (live) {
this.device_name = device || binding.default_device();
this.link_type = binding.open_live(this.device_name, filter || "", this.buffer_size);
this.link_type = binding.open_live(this.device_name, filter || "", this.buffer_size, pcap_output_filename || "");
} else {
this.device_name = device;
this.link_type = binding.open_offline(this.device_name, filter || "", this.buffer_size);
this.link_type = binding.open_offline(this.device_name, filter || "", this.buffer_size, pcap_output_filename || "");
}

this.fd = binding.fileno();
Expand Down
27 changes: 25 additions & 2 deletions pcap_binding.cc
Expand Up @@ -22,6 +22,7 @@ struct bpf_program fp;
bpf_u_int32 mask;
bpf_u_int32 net;
pcap_t *pcap_handle;
pcap_dumper_t *pcap_dump_handle;

// buffer data and length are global. To support more than one pcap session, we'll need a class container.
char *buffer_data;
Expand All @@ -42,6 +43,10 @@ size_t buffer_length;
void PacketReady(u_char *callback_p, const struct pcap_pkthdr* pkthdr, const u_char* packet) {
HandleScope scope;

if (pcap_dump_handle != NULL) {
pcap_dump((u_char *) pcap_dump_handle, pkthdr, packet);
}

Local<Function> * callback = (Local<Function>*)callback_p;

size_t copy_len = pkthdr->caplen;
Expand Down Expand Up @@ -112,7 +117,7 @@ Open(bool live, const Arguments& args)
HandleScope scope;
char errbuf[PCAP_ERRBUF_SIZE];

if (args.Length() == 3) {
if (args.Length() == 4) {
if (!args[0]->IsString()) {
return ThrowException(Exception::TypeError(String::New("pcap Open: args[0] must be a String")));
}
Expand All @@ -122,12 +127,16 @@ Open(bool live, const Arguments& args)
if (!args[2]->IsInt32()) {
return ThrowException(Exception::TypeError(String::New("pcap Open: args[2] must be a Number")));
}
if (!args[3]->IsString()) {
return ThrowException(Exception::TypeError(String::New("pcap Open: args[3] must be a String")));
}
} else {
return ThrowException(Exception::TypeError(String::New("pcap Open: expecting 3 arguments")));
return ThrowException(Exception::TypeError(String::New("pcap Open: expecting 4 arguments")));
}
String::Utf8Value device(args[0]->ToString());
String::Utf8Value filter(args[1]->ToString());
int buffer_size = args[2]->Int32Value();
String::Utf8Value pcap_output_filename(args[3]->ToString());

if (live) {
if (pcap_lookupnet((char *) *device, &net, &mask, errbuf) == -1) {
Expand Down Expand Up @@ -170,6 +179,15 @@ Open(bool live, const Arguments& args)
if (pcap_activate(pcap_handle) != 0) {
return ThrowException(Exception::Error(String::New(pcap_geterr(pcap_handle))));
}

pcap_dump_handle = NULL;
if (strlen((char *) *pcap_output_filename) > 0) {
pcap_dump_handle = pcap_dump_open(pcap_handle, (char *) *pcap_output_filename);
if (pcap_dump_handle == NULL) {
return ThrowException(Exception::Error(String::New("error opening dump")));
}
}

} else {
// Device is the path to the savefile
pcap_handle = pcap_open_offline((char *) *device, errbuf);
Expand Down Expand Up @@ -303,6 +321,11 @@ Close(const Arguments& args)
{
HandleScope scope;

if (pcap_dump_handle != NULL) {
pcap_dump_close(pcap_dump_handle);
pcap_dump_handle = NULL;
}

pcap_close(pcap_handle);

return Undefined();
Expand Down

0 comments on commit cb331da

Please sign in to comment.