Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement IP stream and document API. Still needs better tests.

  • Loading branch information...
commit 6d77bc924e782e9b5fad1a66f45410a0d73d1dcc 1 parent 487d238
@wanderview authored
View
4 .travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - "0.8"
+ - "0.9"
View
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2013, Benjamin J. Kelly ("Author")
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
38 README.md
@@ -0,0 +1,38 @@
+# ether-stream
+
+Object stream transform that parses IP headers.
+
+[![Build Status](https://travis-ci.org/wanderview/node-ip-stream.png)](https://travis-ci.org/wanderview/node-ip-stream)
+
+## Example
+
+```javascript
+var IpStream = require('ip-stream');
+var EtherStream = require('ether-stream');
+var PcapStream = require('pcap-stream');
+
+var pstream = new PcapStream(PCAP_FILE);
+var estream = new EtherStream();
+var ipstream = new IpStream();
+
+pstream.pipe(estream).pipe(ipstream);
+
+ipstream.on('readable', function() {
+ var msg = ipstream.read();
+
+ msg.ether.src === '12:34:56:65:43:21'; // Ethernet frame is still available
+
+ msg.ip.src === '1.1.1.1'; // IP header data is available at .ip property
+ msg.ip.dst === '2.2.2.2';
+ msg.ip.protocol === 'udp';
+
+ var payload = msg.data; // IP packet data is available at .data property
+});
+
+// Packets that cannot be parsed as IP are emitted with 'ignored' event
+ipstream.on('ignored', function(msg) {
+ console.log('Ignored message [' + msg + ']');
+});
+
+ipstream.read(0);
+```
View
29 package.json
@@ -0,0 +1,29 @@
+{
+ "name": "ip-stream",
+ "description": "Object stream transform that parses IP headers.",
+ "keywords": [
+ "IP",
+ "header",
+ "binary",
+ "parse",
+ "stream"
+ ],
+ "version": "0.0.0",
+ "author": "Ben Kelly <ben@wanderview.com>",
+ "license": "BSD",
+ "dependencies": {
+ "ip-header": "~0.1.0",
+ "readable-stream": "~0.3.0"
+ },
+ "devDependencies": {
+ "nodeunit": "~0.7.4"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git@github.com:wanderview/node-ip-stream.git"
+ },
+ "scripts": {
+ "test": "nodeunit test"
+ },
+ "main": "stream.js"
+}
View
79 stream.js
@@ -0,0 +1,79 @@
+// Copyright (c) 2013, Benjamin J. Kelly ("Author")
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+'use strict';
+
+module.exports = IpStream;
+
+var IpHeader = require('ip-header');
+var Transform = require('stream').Transform;
+if (!Transform) {
+ Transform = require('readable-stream/transform');
+}
+var util = require('util');
+
+util.inherits(IpStream, Transform);
+
+function IpStream(opts) {
+ var self = (this instanceof IpStream)
+ ? this
+ : Object.create(IpStream.prototype);
+
+ opts = opts || {};
+
+ if (opts.objectMode === false) {
+ throw new Error('IpStream requires objectMode; do not set ' +
+ 'option {objectMode: false}');
+ }
+ opts.objectMode = true;
+
+ Transform.call(self, opts);
+
+ return self;
+}
+
+IpStream.prototype._transform = function(msg, output, callback) {
+ var data = (msg instanceof Buffer) ? msg : msg.data;
+ var type = msg.ether ? msg.ether.type : 'ip';
+
+ if (type !== 'ip') {
+ this.emit('ignored', msg);
+ callback();
+ return;
+ }
+
+ try {
+ var iph = new IpHeader(msg.data);
+
+ // TODO: handle fragmentation
+
+ msg.ip = iph;
+ msg.data = msg.data.slice(iph.length);
+ output(msg);
+
+ } catch (error) {
+ this.emit('ignored', msg);
+ }
+
+ callback();
+}
View
33 test/test-stream.js
@@ -0,0 +1,33 @@
+// Copyright (c) 2013, Benjamin J. Kelly ("Author")
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+'use strict';
+
+var IpStream = require('../stream');
+
+module.exports.nop = function(test) {
+ test.expect(1);
+ var istream = new IpStream();
+ test.ok(istream instanceof IpStream);
+ test.done();
+};
Please sign in to comment.
Something went wrong with that request. Please try again.