Conversion between IPv6 addresses and their network-order wire bytes.
This module reads an address straight out of a packet buffer and writes it
straight back into one, with no string round-trip. It is the byte-form
counterpart of the ipv6 submodule: addressv6FromBytes is to
parseAddressv6 what addressv6ToBytes is to stringifyAddressv6.
Addresses keep the numeric representation of the rest of the package — a
bigint holding a 128-bit unsigned integer. Bytes are a conversion, not a
second representation.
Byte order
Always network order (big-endian), on both the read and the write side. There is no option, because an IP address has exactly one wire order.
Widths and offsets
Both functions are fixed at 16 bytes, and offset selects where in the
buffer that span sits, never how wide it is. A span that runs past the end
of the buffer throws a RangeError rather than reading undefined and
quietly decoding as ::.
IPv4-mapped byte sequences (::ffff:x.x.x.x) stay 128-bit values here; they
are never unwrapped to a number.
For IPv4 see bytesv4; for the
version-detecting pair see bytes.
Decode an address out of an IPv6 header
Decode an address out of an IPv6 header
import { assertEquals } from "@std/assert"; import { addressv6FromBytes } from "@hertzg/ip/bytesv6"; import { stringifyAddressv6 } from "@hertzg/ip/addressv6"; // deno-fmt-ignore const packet = new Uint8Array([ 0x60, 0x00, 0x00, 0x00, 0x00, 0x14, 0x06, 0x40, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, ]); assertEquals(stringifyAddressv6(addressv6FromBytes(packet, 8)), "2001:db8::1");
Assemble an IPv6 header in place
Assemble an IPv6 header in place
import { assertEquals } from "@std/assert"; import { addressv6FromBytes, addressv6ToBytes } from "@hertzg/ip/bytesv6"; import { parseAddressv6 } from "@hertzg/ip/addressv6"; const frame = new Uint8Array(40); addressv6ToBytes(parseAddressv6("2001:db8::1").address, frame, 8); addressv6ToBytes(parseAddressv6("2001:db8::2").address, frame, 24); assertEquals(addressv6FromBytes(frame, 24), parseAddressv6("2001:db8::2").address);
Reads a 16-byte IPv6 address from a buffer.
Writes a 16-byte IPv6 address, either into a fresh buffer or into one you supply.
Usage
import * as mod from "bytesv6.ts";