Permalink
Browse files

Added support for reading pcap files via createOfflineSession

  • Loading branch information...
1 parent f439965 commit 1c081652c358461442cee4b3ee8612d93bfdc42a @mattstevens mattstevens committed Aug 14, 2010
Showing with 71 additions and 38 deletions.
  1. +16 −4 pcap.js
  2. +55 −34 pcap_binding.cc
View
20 pcap.js
@@ -23,11 +23,17 @@ Pcap.prototype.findalldevs = function () {
return binding.findalldevs();
};
-Pcap.prototype.open_live = function (device, filter) {
+Pcap.prototype.open = function (live, device, filter) {
var me = this;
- this.device_name = device || binding.default_device();
- this.link_type = binding.open_live(this.device_name, filter || "");
+ if (live) {
+ this.device_name = device || binding.default_device();
+ this.link_type = binding.open_live(this.device_name, filter || "");
+ } else {
+ this.device_name = device;
+ this.link_type = binding.open_offline(device, filter || "");
+ }
+
this.fd = binding.fileno();
this.opened = true;
this.readWatcher = new process.IOWatcher();
@@ -68,7 +74,13 @@ exports.Pcap = Pcap;
exports.createSession = function (device, filter) {
var session = new Pcap();
- session.open_live(device, filter);
+ session.open(true, device, filter);
+ return session;
+};
+
+exports.createOfflineSession = function (path, filter) {
+ var session = new Pcap();
+ session.open(false, path, filter);
return session;
};
View
@@ -94,7 +94,7 @@ Dispatch(const Arguments& args)
}
Handle<Value>
-OpenLive(const Arguments& args)
+Open(bool live, const Arguments& args)
{
HandleScope scope;
char errbuf[PCAP_ERRBUF_SIZE];
@@ -105,46 +105,54 @@ OpenLive(const Arguments& args)
String::Utf8Value device(args[0]->ToString());
String::Utf8Value filter(args[1]->ToString());
- if (pcap_lookupnet((char *) *device, &net, &mask, errbuf) == -1) {
- net = 0;
- mask = 0;
- fprintf(stderr, "warning: %s - filtering may not work right\n", errbuf);
- }
+ if (live) {
+ if (pcap_lookupnet((char *) *device, &net, &mask, errbuf) == -1) {
+ net = 0;
+ mask = 0;
+ fprintf(stderr, "warning: %s - filtering may not work right\n", errbuf);
+ }
- pcap_handle = pcap_create((char *) *device, errbuf);
- if (pcap_handle == NULL) {
- return ThrowException(Exception::Error(String::New(errbuf)));
- }
+ pcap_handle = pcap_create((char *) *device, errbuf);
+ if (pcap_handle == NULL) {
+ return ThrowException(Exception::Error(String::New(errbuf)));
+ }
- // 64KB is the max IPv4 packet size
- if (pcap_set_snaplen(pcap_handle, 65535) != 0) {
- return ThrowException(Exception::Error(String::New("error setting snaplen")));
- }
+ // 64KB is the max IPv4 packet size
+ if (pcap_set_snaplen(pcap_handle, 65535) != 0) {
+ return ThrowException(Exception::Error(String::New("error setting snaplen")));
+ }
- // always use promiscuous mode
- if (pcap_set_promisc(pcap_handle, 1) != 0) {
- return ThrowException(Exception::Error(String::New("error setting promiscuous mode")));
- }
+ // always use promiscuous mode
+ if (pcap_set_promisc(pcap_handle, 1) != 0) {
+ return ThrowException(Exception::Error(String::New("error setting promiscuous mode")));
+ }
- // Try to set a 10MB buffer size. Sometimes the OS has a lower limit that it will silently enforce.
- // TODO - make this settable at runtime
- if (pcap_set_buffer_size(pcap_handle, 10 * 1024 * 1024) != 0) {
- return ThrowException(Exception::Error(String::New("error setting buffer size")));
- }
+ // Try to set a 10MB buffer size. Sometimes the OS has a lower limit that it will silently enforce.
+ // TODO - make this settable at runtime
+ if (pcap_set_buffer_size(pcap_handle, 10 * 1024 * 1024) != 0) {
+ return ThrowException(Exception::Error(String::New("error setting buffer size")));
+ }
- // set "timeout" on read, even though we are also setting nonblock below. On Linux this is required.
- if (pcap_set_timeout(pcap_handle, 1000) != 0) {
- return ThrowException(Exception::Error(String::New("error setting read timeout")));
- }
+ // set "timeout" on read, even though we are also setting nonblock below. On Linux this is required.
+ if (pcap_set_timeout(pcap_handle, 1000) != 0) {
+ return ThrowException(Exception::Error(String::New("error setting read timeout")));
+ }
- // TODO - pass in an option to enable rfmon on supported interfaces. Sadly, it can be a disruptive
- // operation, so we can't just always try to turn it on.
- // if (pcap_set_rfmon(pcap_handle, 1) != 0) {
- // return ThrowException(Exception::Error(String::New(pcap_geterr(pcap_handle))));
- // }
+ // TODO - pass in an option to enable rfmon on supported interfaces. Sadly, it can be a disruptive
+ // operation, so we can't just always try to turn it on.
+ // if (pcap_set_rfmon(pcap_handle, 1) != 0) {
+ // return ThrowException(Exception::Error(String::New(pcap_geterr(pcap_handle))));
+ // }
- if (pcap_activate(pcap_handle) != 0) {
- return ThrowException(Exception::Error(String::New(pcap_geterr(pcap_handle))));
+ if (pcap_activate(pcap_handle) != 0) {
+ return ThrowException(Exception::Error(String::New(pcap_geterr(pcap_handle))));
+ }
+ } else {
+ // Device is the path to the savefile
+ pcap_handle = pcap_open_offline((char *) *device, errbuf);
+ if (pcap_handle == NULL) {
+ return ThrowException(Exception::Error(String::New(errbuf)));
+ }
}
if (pcap_setnonblock(pcap_handle, 1, errbuf) == -1) {
@@ -193,6 +201,18 @@ OpenLive(const Arguments& args)
}
Handle<Value>
+OpenLive(const Arguments& args)
+{
+ return Open(true, args);
+}
+
+Handle<Value>
+OpenOffline(const Arguments& args)
+{
+ return Open(false, args);
+}
+
+Handle<Value>
FindAllDevs(const Arguments& args)
{
HandleScope scope;
@@ -345,6 +365,7 @@ extern "C" void init (Handle<Object> target)
target->Set(String::New("findalldevs"), FunctionTemplate::New(FindAllDevs)->GetFunction());
target->Set(String::New("open_live"), FunctionTemplate::New(OpenLive)->GetFunction());
+ target->Set(String::New("open_offline"), FunctionTemplate::New(OpenOffline)->GetFunction());
target->Set(String::New("dispatch"), FunctionTemplate::New(Dispatch)->GetFunction());
target->Set(String::New("fileno"), FunctionTemplate::New(Fileno)->GetFunction());
target->Set(String::New("close"), FunctionTemplate::New(Close)->GetFunction());

0 comments on commit 1c08165

Please sign in to comment.