|
| 1 | +import { CRC32 } from "@tsxper/crc32"; |
| 2 | + |
| 3 | +// From https://en.wikipedia.org/wiki/EtherType |
| 4 | +export const IP_PROTOCOL_TYPE = 0x0800; |
| 5 | +export const ARP_PROTOCOL_TYPE = 0x0806; |
| 6 | +export const IPV6_PROTOCOL_TYPE = 0x86dd; |
| 7 | + |
| 8 | +/// Medium Access Control (MAC) address |
| 9 | +export class MacAddress { |
| 10 | + // 6 bytes |
| 11 | + octets: Uint8Array; |
| 12 | + |
| 13 | + constructor(octets: Uint8Array) { |
| 14 | + if (octets.length !== 6) { |
| 15 | + throw new Error("Invalid MAC address"); |
| 16 | + } |
| 17 | + this.octets = octets; |
| 18 | + } |
| 19 | + |
| 20 | + // Parse MAC address from a string representation (00:1b:63:84:45:e6) |
| 21 | + static parse(addrString: string): MacAddress { |
| 22 | + const octets = new Uint8Array(6); |
| 23 | + addrString.split(":").forEach((octet, i) => { |
| 24 | + const octetInt = parseInt(octet, 16); |
| 25 | + if (isNaN(octetInt) || octetInt < 0 || octetInt > 255) { |
| 26 | + throw new Error(`Invalid MAC address: ${addrString}`); |
| 27 | + } |
| 28 | + octets[i] = octetInt; |
| 29 | + }); |
| 30 | + return new this(octets); |
| 31 | + } |
| 32 | + |
| 33 | + // Turn to string |
| 34 | + toString(): string { |
| 35 | + return Array.from(this.octets) |
| 36 | + .map((d) => d.toString(16)) |
| 37 | + .join(":"); |
| 38 | + } |
| 39 | + |
| 40 | + // Check if two MAC addresses are equal. |
| 41 | + equals(other: MacAddress): boolean { |
| 42 | + return this.octets.every((octet, index) => octet === other.octets[index]); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +const crc32 = new CRC32(); |
| 47 | + |
| 48 | +const MINIMUM_PAYLOAD_SIZE = 46; |
| 49 | + |
| 50 | +export class EthernetFrame { |
| 51 | + // Info taken from Computer Networking: A Top-Down Approach |
| 52 | + |
| 53 | + // 8 bytes |
| 54 | + // 7 bytes preamble and 1 byte start of frame delimiter |
| 55 | + // Used to synchronize the communication |
| 56 | + // TODO: should we mention this somewhere? |
| 57 | + // readonly preamble = new Uint8Array([ |
| 58 | + // 0b10101010, 0b10101010, 0b10101010, 0b10101010, 0b10101010, 0b10101010, |
| 59 | + // 0b10101010, 0b10101011, |
| 60 | + // ]); |
| 61 | + |
| 62 | + // 6 bytes |
| 63 | + // Destination MAC address |
| 64 | + destination: MacAddress; |
| 65 | + // 6 bytes |
| 66 | + // Source MAC address |
| 67 | + source: MacAddress; |
| 68 | + // 2 bytes |
| 69 | + // The payload's type |
| 70 | + type: number; |
| 71 | + // 46-1500 bytes |
| 72 | + // If the payload is smaller than 46 bytes, it is padded. |
| 73 | + // TODO: make this an interface |
| 74 | + // The payload |
| 75 | + payload: FramePayload; |
| 76 | + // 4 bytes |
| 77 | + // Cyclic Redundancy Check (CRC) |
| 78 | + get crc(): number { |
| 79 | + // Computation doesn't include preamble |
| 80 | + const frameBytes = this.toBytes({ |
| 81 | + withChecksum: false, |
| 82 | + }); |
| 83 | + return crc32.forBytes(frameBytes); |
| 84 | + } |
| 85 | + |
| 86 | + constructor( |
| 87 | + source: MacAddress, |
| 88 | + destination: MacAddress, |
| 89 | + payload: FramePayload, |
| 90 | + ) { |
| 91 | + this.destination = destination; |
| 92 | + this.source = source; |
| 93 | + this.type = payload.type(); |
| 94 | + this.payload = payload; |
| 95 | + } |
| 96 | + |
| 97 | + toBytes({ withChecksum = true }: { withChecksum?: boolean } = {}) { |
| 98 | + let checksum: number[] = []; |
| 99 | + if (withChecksum) { |
| 100 | + const crc = this.crc; |
| 101 | + checksum = [crc & 0xff, (crc >> 8) & 0xff, (crc >> 16) & 0xff, crc >> 24]; |
| 102 | + } |
| 103 | + let payload = this.payload.toBytes(); |
| 104 | + if (payload.length < MINIMUM_PAYLOAD_SIZE) { |
| 105 | + const padding = new Array(MINIMUM_PAYLOAD_SIZE - payload.length); |
| 106 | + payload = Uint8Array.from([...payload, ...padding]); |
| 107 | + } |
| 108 | + return Uint8Array.from([ |
| 109 | + ...this.destination.octets, |
| 110 | + ...this.source.octets, |
| 111 | + this.type >> 8, |
| 112 | + this.type & 0xff, |
| 113 | + ...payload, |
| 114 | + ...checksum, |
| 115 | + ]); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +export interface FramePayload { |
| 120 | + toBytes(): Uint8Array; |
| 121 | + type(): number; |
| 122 | +} |
0 commit comments