Skip to content

Commit

Permalink
Convert the type code to a string. Throw for unknown codes since we m…
Browse files Browse the repository at this point in the history
…ay not parse others correctly.
  • Loading branch information
wanderview committed Feb 17, 2013
1 parent 7abb7b9 commit 977b7d9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var EtherFrame = require('ether-frame');
var ef = EtherFrame.fromBuffer(buf);
ef.src === '12:34:56:78'; // true
ef.dst === '98:76:54:32': // true
ef.type === EtherFrame.TYPE_IP; // true
ef.type === 'ip'; // true
ef.bytes === 14; // true
var payload = buf.slice(ef.bytes);
var buf = ef.toBuffer();
Expand Down
31 changes: 24 additions & 7 deletions ether.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ module.exports = EtherFrame;

var mac = require('mac-address');

EtherFrame.TYPE_IP = 0x0800;
EtherFrame.TYPE_ARP = 0x0806;
var TYPE_TO_STRING = {
0x0800: 'ip',
0x0806: 'arp'
};

var TYPE_FROM_STRING = {
ip: 0x0800,
arp: 0x0806
};

function EtherFrame(opts) {
if (opts instanceof Buffer) {
Expand All @@ -41,9 +48,9 @@ function EtherFrame(opts) {

opts = opts || {};

self.src = opts.src || '00:00:00:00:00:00';
self.dst = opts.dst || '00:00:00:00:00:00';
self.type = opts.type || EtherFrame.TYPE_IP;
self.src = opts.src || mac.ZERO;
self.dst = opts.dst || mac.ZERO;
self.type = opts.type || 'ip';
self.bytes = opts.bytes || ((mac.LENGTH * 2) + 2);

return self;
Expand All @@ -59,9 +66,14 @@ EtherFrame.fromBuffer = function(buf, offset) {
var src = mac.toString(buf, offset + bytes);
bytes += mac.LENGTH;

var type = buf.readUInt16BE(offset + bytes);
var typeCode = buf.readUInt16BE(offset + bytes);
bytes += 2;

var type = TYPE_TO_STRING[typeCode];
if (!type) {
throw(new Error('Unsupported type code [' + typeCode + ']'));
}

return new EtherFrame({ dst: dst, src: src, type: type, bytes: bytes });
};

Expand All @@ -75,7 +87,12 @@ EtherFrame.prototype.toBuffer = function(buf, offset) {
mac.toBuffer(this.src, buf, offset);
offset += mac.LENGTH;

buf.writeUInt16BE(this.type, offset);
var typeCode = TYPE_FROM_STRING[this.type];
if (typeof typeCode !== 'number') {
throw(new Error('Unsupported type [' + this.type + ']'));
}

buf.writeUInt16BE(typeCode, offset);
offset += 2;

return buf;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"author": "Ben Kelly <ben@wanderview.com>",
"license": "BSD",
"dependencies": {
"mac-address": "~0.2.0"
"mac-address": "~0.3.0"
},
"devDependencies": {
"nodeunit": "~0.7.4",
Expand Down
15 changes: 13 additions & 2 deletions test/test-ether.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports.fromBuffer = function(test) {
var ether = EtherFrame.fromBuffer(payload);
test.equal('00:50:56:e6:2d:02', ether.dst);
test.equal('00:0c:29:0d:06:56', ether.src);
test.equal(EtherFrame.TYPE_IP, ether.type);
test.equal('ip', ether.type);
test.done();
});
};
Expand All @@ -53,7 +53,7 @@ module.exports.fromBufferNew = function(test) {
var ether = new EtherFrame(payload);
test.equal('00:50:56:e6:2d:02', ether.dst);
test.equal('00:0c:29:0d:06:56', ether.src);
test.equal(EtherFrame.TYPE_IP, ether.type);
test.equal('ip', ether.type);
test.done();
});
};
Expand All @@ -72,3 +72,14 @@ module.exports.toBuffer = function(test) {
test.done();
});
};

module.exports.defaults = function(test) {
test.expect(3);

var ether = new EtherFrame();
test.equal('00:00:00:00:00:00', ether.dst);
test.equal('00:00:00:00:00:00', ether.src);
test.equal('ip', ether.type);

test.done();
};

0 comments on commit 977b7d9

Please sign in to comment.