IEEE 802.1Q VLAN tag encoding and decoding.
Round-trip a tagged frame carrying an IPv4 payload
Round-trip a tagged frame carrying an IPv4 payload
import { assertEquals } from "@std/assert"; import { vlanTag, VLAN_TAG_SIZE } from "@binstruct/vlan"; const coder = vlanTag(); const tag = { tci: { pcp: 5, dei: 0, vlanId: 100 }, etherType: 0x0800, payload: new Uint8Array([0x45, 0x00, 0x00, 0x14]), }; const buffer = new Uint8Array(VLAN_TAG_SIZE + tag.payload.length); const written = coder.encode(tag, buffer); const [decoded, read] = coder.decode(buffer); assertEquals(written, read); assertEquals(decoded.tci, tag.tci); assertEquals(decoded.etherType, tag.etherType); assertEquals(decoded.payload, tag.payload);
Creates a coder for an IEEE 802.1Q VLAN tag.
Decoded IEEE 802.1Q VLAN tag — the TCI, the encapsulated EtherType, and the raw payload that follows.
EtherType assigned to IEEE 802.1Q tagged frames (0x8100). The value an
Ethernet II frame's etherType field carries when it is followed by a
VLAN tag rather than the payload's real EtherType.
Reserved vlanId meaning the frame carries no VLAN membership and the tag
is present only to carry pcp / dei ("priority-tagged" frame).
Reserved vlanId indicating the tag carries implementation-specific
information rather than a VLAN membership. Never configured as a real
VLAN.
Size in bytes of a VLAN tag: the 2-byte TCI (pcp + dei + vlanId)
plus the 2-byte encapsulated EtherType. Does not include the payload that
follows, which is variable-length.