Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added support to dump the traffic to a file

  • Loading branch information...
commit cb331da5ef7310dc00d9fd9d9ea6564bf68cfe10 1 parent ba1b20e
Joe Ferner authored
Showing with 28 additions and 5 deletions.
  1. +3 −3 pcap.js
  2. +25 −2 pcap_binding.cc
View
6 pcap.js
@@ -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)) {
@@ -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();
View
27 pcap_binding.cc
@@ -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;
@@ -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;
@@ -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")));
}
@@ -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) {
@@ -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);
@@ -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();
Please sign in to comment.
Something went wrong with that request. Please try again.