-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathethernet_packet.js
More file actions
90 lines (81 loc) · 2.57 KB
/
ethernet_packet.js
File metadata and controls
90 lines (81 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var EthernetAddr = require("./ethernet_addr");
var IPv4 = require("./ipv4");
var IPv6 = require("./ipv6");
var Arp = require("./arp");
var Vlan = require("./vlan");
function EthernetPacket(emitter) {
this.emitter = emitter;
this.dhost = null;
this.shost = null;
this.ethertype = null;
this.vlan = null;
this.payload = null;
}
EthernetPacket.prototype.decode = function (raw_packet, offset) {
this.dhost = new EthernetAddr(raw_packet, offset);
offset += 6;
this.shost = new EthernetAddr(raw_packet, offset);
offset += 6;
this.ethertype = raw_packet.readUInt16BE(offset, true);
offset += 2;
if (this.ethertype === 0x8100) { // VLAN-tagged (802.1Q)
this.vlan = new Vlan().decode(raw_packet, offset);
offset += 2;
// Update the ethertype
this.ethertype = raw_packet.readUInt16BE(offset, true);
offset += 2;
}
if (this.ethertype < 1536) {
// this packet is actually some 802.3 type without an ethertype
this.ethertype = 0;
} else {
// http://en.wikipedia.org/wiki/EtherType
switch (this.ethertype) {
case 0x800: // IPv4
this.payload = new IPv4(this.emitter).decode(raw_packet, offset);
break;
case 0x806: // ARP
this.payload = new Arp(this.emitter).decode(raw_packet, offset);
break;
case 0x86dd: // IPv6 - http://en.wikipedia.org/wiki/IPv6
this.payload = new IPv6(this.emitter).decode(raw_packet, offset);
break;
case 0x88cc: // LLDP - http://en.wikipedia.org/wiki/Link_Layer_Discovery_Protocol
this.payload = "need to implement LLDP";
break;
default:
console.log("node_pcap: EthernetFrame() - Don't know how to decode ethertype " + this.ethertype);
}
}
return this;
};
EthernetPacket.prototype.decoderName = "ethernet-packet";
EthernetPacket.prototype.eventsOnDecode = false;
EthernetPacket.prototype.toString = function () {
var ret = this.shost + " -> " + this.dhost;
if (this.vlan) {
ret += " vlan " + this.vlan;
}
switch (this.ethertype) {
case 0x800:
ret += " IPv4";
break;
case 0x806:
ret += " ARP";
break;
case 0x86dd:
ret += " IPv6";
break;
case 0x88cc:
ret += " LLDP";
break;
default:
ret += " ethertype " + this.ethertype;
}
var payload = '';
if (this.payload !== null) {
payload = this.payload.toString();
}
return ret + " " + payload;
};
module.exports = EthernetPacket;