default

IEEE 802.1Q VLAN tag encoding and decoding.

Examples

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);

Functions

f
vlanTag(): Coder<VlanTag>

Creates a coder for an IEEE 802.1Q VLAN tag.

Interfaces

I
VlanTag

Decoded IEEE 802.1Q VLAN tag — the TCI, the encapsulated EtherType, and the raw payload that follows.

Variables

v
TPID_8021Q: 33024

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.

v
VLAN_ID_PRIORITY_TAGGED: 0

Reserved vlanId meaning the frame carries no VLAN membership and the tag is present only to carry pcp / dei ("priority-tagged" frame).

v
VLAN_ID_RESERVED: 4095

Reserved vlanId indicating the tag carries implementation-specific information rather than a VLAN membership. Never configured as a real VLAN.

v
VLAN_TAG_SIZE: 4

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.