Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: raphaelr/node_pcap-ffi
base: 2c2e33c61e
...
head fork: raphaelr/node_pcap-ffi
compare: 8f3657591c
  • 6 commits
  • 15 files changed
  • 0 commit comments
  • 1 contributor
View
35 doc/data/css/site.css
@@ -0,0 +1,35 @@
+body {
+ margin-top: 10px;
+ margin-bottom: 10px;
+}
+
+header {
+ font-size: 1.5em;
+ border-bottom: 1px solid #ddd;
+}
+
+nav {
+ padding-top: 0.5em;
+ font-size: 1.25em;
+}
+
+nav li {
+ margin-top: 1em;
+}
+
+nav > ul:first-child {
+ margin-top: -1em;
+}
+
+nav > ul > li > ul {
+ list-style-type: square;
+}
+
+nav > ul > li > ul > li > ul {
+ list-style-type: circle;
+}
+
+footer {
+ border-top: 1px solid #ddd;
+ text-align: center;
+}
View
12 doc/data/template.html
@@ -1,15 +1,19 @@
<!DOCTYPE html>
<html>
<head>
- <title>pcap-ffi: ${title}</title>
<meta charset="utf-8">
+ <title>${title}</title>
<link rel="stylesheet" href="css/blueprint/screen.css" type="text/css" media="screen, projection">
<link rel="stylesheet" href="css/blueprint/print.css" type="text/css" media="print">
- <!--[if lt IE 8]> <link rel="stylesheet" href="css/blueprint/ie.css" type="text/css" media="screen, projection"> <![endif]-->
+ <!--[if lt IE 8]><link rel="stylesheet" href="css/blueprint/ie.css" type="text/css" media="screen, projection"><![endif]-->
+ <link rel="stylesheet" href="css/site.css" type="text/css">
</head>
<body>
- <div id="container">
- <article class="span-24">{{html content}}</article>
+ <div class="container">
+ <header class="span-24"><h1>pcap-ffi</h1></header>
+ <nav class="span-6 border">{{html navigation}}</nav>
+ <article class="span-18 last">{{html content}}</article>
+ <footer class="span-24">pcap-ffi (c) 2012 Raphael Robatsch</footer>
</div>
</body>
</html>
View
5 doc/input/_navigation.md
@@ -0,0 +1,5 @@
+* [pcap-ffi](index.html)
+* [API](api.index.html)
+ * [Opening and closing](api.open-close.html)
+* Development
+ * [Coding Style](dev.coding-style.html)
View
9 doc/input/api/index.md
@@ -0,0 +1,9 @@
+API Reference
+=============
+This section details the public facing API of `pcap-ffi`. The following conventions
+are used throughout this section:
+
+* `pcap` is the result of `require('pcap-ffi')`.
+* `handle` is a handle returned by the [`pcap.open*`](api.open-close.html) functions
+* Optional functions parameters are written in `[brackets]`. The default value is
+ specified in the parameter documentation.
View
44 doc/input/api/open-close.md
@@ -0,0 +1,44 @@
+Opening and closing devices
+===========================
+
+## pcap.openLive
+ var handle = pcap.openLive(name, [snaplen], [promiscuous], [timeout]);
+
+Opens a live capture from a physical network interface.
+
+* **`name`**: Name of the device to open, e.g. `eth0` on GNU/Linux-based systems.
+* `snaplen`: Maximum capture length of one packet.
+ Default: `65535`
+* `promiscuous`: A truthy value for this parameter enables promicuous mode, i.e.
+ the interface will capture all packets, even those addressed to other MAC
+ addresses. The effect of this parameter depends on the operating system and type
+ of interface used.
+ Default: `false`
+* `timeout`: Time to allow the capture driver to capture more packets before sending it
+ to the application in one chunk, minimizing CPU usage. A lower value therefore decreases
+ latency but increases CPU usage. A value of `0` allows the capture driver to read
+ as much packets as it can store in it's buffer before returning.
+ Default: `0`
+
+## pcap.openOffline
+ var handle = pcap.openOffline(filename);
+
+Opens a capture file which was previously recorded.
+
+* `filename`: Filename of the capture file.
+
+## pcap.openDead
+ var handle = pcap.openDead([linktype], [snaplen]);
+
+Opens a dead capture. Useful only for testing purposes.
+
+* `linktype`: Data-Link type of the device, see `bpf.h` in your libpcap distribution.
+ Default: `1` (Ethernet)
+* `snaplen`: Maximum capture length of one packet.
+ Default: `65535`
+
+## handle.close
+ handle.close();
+
+Closes a handle returned by the `pcap.open*` functions. Multiple calls to this function
+are ignored.
View
0  doc/input/coding-style.md → doc/input/dev/coding-style.md
File renamed without changes
View
4 doc/input/index.md
@@ -1,5 +1,5 @@
-Documentation Index
-===================
+pcap-ffi
+========
pcap-ffi provides a binding to [libpcap](http://www.tcpdump.org/) for node.js. Unlike node_pcap
it is written in pure JavaScript, it relies on [node-ffi](https://github.com/rbranson/node-ffi)
View
9 lib/handle.js
@@ -1,9 +1,14 @@
-var raw = require('./raw');
+var raw = require('./raw'),
+ PcapError = require('./error'),
+ ffi = require('node-ffi');
var Handle = module.exports = function(ptr) {
this.ptr = ptr;
};
Handle.prototype.close = function() {
- raw.pcap_close(this.ptr);
+ if(!this.ptr.isNull()) {
+ raw.pcap_close(this.ptr);
+ this.ptr = ffi.Pointer.NULL;
+ }
};
View
2  lib/pcap-ffi.js
@@ -4,7 +4,7 @@ var pcap = module.exports = {
raw: require('./raw')
};
-copyProperties(pcap, require('./open_close'));
+copyProperties(pcap, require('./static'));
pcap.systemNameToPcapName = pcap.platform.pcapName;
function copyProperties(dst, src) {
View
4 lib/raw.js
@@ -3,7 +3,9 @@ var ffi = require('node-ffi'),
var raw = module.exports = new ffi.Library(platform.PCAP_LIBRARY, {
"pcap_close": ['void', ['pointer']],
- "pcap_open_live": ['pointer', ['string', 'int', 'int', 'int', 'pointer']]
+ "pcap_open_live": ['pointer', ['string', 'int', 'int', 'int', 'pointer']],
+ "pcap_open_dead": ['pointer', ['int', 'int']],
+ "pcap_open_offline": ['pointer', ['string', 'pointer']]
});
raw.createErrbuf = function() {
View
16 lib/open_close.js → lib/static.js
@@ -10,4 +10,18 @@ pcap.openLive = function(name, snaplen, promisc, timeout) {
throw new PcapError(errbuf.getCString());
}
return new Handle(ptr);
-}
+};
+
+pcap.openDead = function(linktype, snaplen) {
+ var ptr = raw.pcap_open_dead(linktype || 1, snaplen || 65535);
+ return new Handle(ptr);
+};
+
+pcap.openOffline = function(filename) {
+ var errbuf = raw.createErrbuf();
+ var ptr = raw.pcap_open_offline(filename.toString(), errbuf);
+ if(ptr.isNull()) {
+ throw new PcapError(errbuf.getCString());
+ }
+ return new Handle(ptr);
+};
View
7 package.json
@@ -14,14 +14,11 @@
"devDependencies": {
"expect.js": "~0.1",
"mocha": "~0.14",
- "glob": "~3.1",
- "jqtpl": "~1.0",
- "rimraf": "~2.0",
- "showdown": "~0.0"
+ "nanodoc": "~0.3"
},
"scripts": {
"test": "node ./node_modules/mocha/bin/_mocha",
- "doc": "node ./script/doc"
+ "doc": "node ./node_modules/nanodoc/cli.js"
}
}
View
BIN  test/data/capture.pcap
Binary file not shown
View
33 test/open.js
@@ -0,0 +1,33 @@
+var expect = require('expect.js'),
+ path = require('path'),
+ pcap = require('../');
+
+describe('open', function() {
+ var handle;
+
+ afterEach(function() {
+ if(handle) handle.close();
+ });
+
+ function expectHandleToBeOk()
+ {
+ expect(handle).to.be.ok();
+ expect(handle.ptr).to.be.ok();
+ expect(handle.ptr.isNull()).not.to.be.ok();
+ }
+
+ it('should return a handle when calling openDead', function() {
+ handle = pcap.openDead();
+ expectHandleToBeOk();
+ });
+
+ it('should return a handle when calling openOffline', function() {
+ handle = pcap.openOffline(path.join(__dirname, 'data/capture.pcap'));
+ expectHandleToBeOk();
+ });
+
+ it('should throw an PcapError if openOffline called with an invalid file', function() {
+ expect(function() { pcap.openOffline(path.join(__dirname, 'data/DONTEXIST')).close(); }).
+ to.throwException(function(e) { expect(e).to.be.a(pcap.PcapError); });
+ });
+});
View
10 test/platform.js
@@ -1,8 +1,12 @@
-var expect = require('expect.js');
-var platform = require('../').platform;
+var expect = require('expect.js'),
+ pcap = require('../');
describe('platform', function() {
it('should have a string property "PCAP_LIBRARY"', function() {
- expect(platform.PCAP_LIBRARY).to.be.a('string');
+ expect(pcap.platform.PCAP_LIBRARY).to.be.a('string');
+ });
+
+ it('should have a function "systemNameToPcapName"', function() {
+ expect(pcap.systemNameToPcapName).to.be.a('function');
});
});

No commit comments for this range

Something went wrong with that request. Please try again.