// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Connection identifiers beyond the first: RFC 9000 §5.1. //! //! A connection has more than one name, and both ends choose the names the //! other must use. §5.1.1: "when an endpoint issues a connection ID, it MUST //! accept packets that carry this connection ID for the duration of the //! connection or until its peer invalidates the connection ID". Each is //! numbered, and the numbers are how the two ends agree about which name they //! are talking about. //! //! ## Why more than one //! //! Two reasons, and only one of them is migration. §9.5 is the other and the //! subtler: a connection ID is a stable identifier visible in the clear on //! every packet, so an observer who sees one on two different paths knows //! they are the same connection and the same person. "An endpoint MUST NOT //! reuse a connection ID when sending from more than one local address" — and //! obeying that requires having a spare, which requires the peer to have //! issued one in advance. A pool of connection IDs is what makes changing //! network unobservable rather than merely possible. //! //! The first reason is the obvious one: an endpoint that has exhausted its //! pool cannot migrate at all, because §9 gives it nothing legal to address //! the new path with. //! //! ## All of ours are the same length //! //! §5.1 requires an endpoint to be able to find its own connection ID in a //! short header, which carries no length field: "an endpoint that uses //! multiple connection ID lengths... MUST NOT... " — more precisely, the //! endpoint must know the length before it can parse. Since an endpoint //! chooses its own, the simplest way to always know is to choose one length //! and keep it. A peer's IDs may be any length it likes; it has the same //! problem and its own solution. const std = @import("std"); const assert = std.debug.assert; /// §17.2: the longest connection ID version 1 permits. pub const max_len = 20; /// §10.3's token, which travels with every connection ID an endpoint issues. pub const token_len = 16; /// How many connection IDs each side of this table holds. /// /// §18.2's `active_connection_id_limit` is what an endpoint advertises, and /// "MUST be at least 2" — one to use and one to move to. Four is enough for a /// migration to have somewhere to go without making the table a place a peer /// can spend our memory: §5.1.1 permits limiting issuance precisely "to /// reduce the amount of per-path state it maintains, such as path validation /// status, as its peer might interact with it over as many paths as there are /// issued connection IDs". pub const capacity = 4; pub const Error = error{ /// §5.1.1: "if the number of active connection IDs exceeds the value /// advertised in its active_connection_id_limit transport parameter, an /// endpoint MUST close the connection with an error of type /// CONNECTION_ID_LIMIT_ERROR." ConnectionIdLimitError, /// §19.15: a length outside 1..20, or a Retire Prior To above the /// Sequence Number. FrameEncodingError, /// §19.15 and §19.16: a sequence number reused for a different /// identifier, a NEW_CONNECTION_ID from an endpoint using zero-length /// identifiers, or a RETIRE_CONNECTION_ID for something never issued. ProtocolViolation, /// The table is full of identifiers that cannot be retired yet. NoRoom, }; /// One connection ID and what travels with it. pub const Entry = struct { sequence: u64, bytes: [max_len]u8 = undefined, len: usize = 0, /// §10.3's stateless reset token, when the peer issued one with this /// identifier. /// /// Optional, and the optionality is load-bearing. The identifier the /// handshake used arrives without a token — a server names its token in /// the `stateless_reset_token` transport parameter instead, and a client /// cannot name one at all — so an entry with a placeholder value would be /// a token this endpoint checks against every arriving datagram without /// the peer ever having issued it. §10.3.1: "an endpoint MUST NOT check /// for any stateless reset tokens associated with connection IDs it has /// not used." A zero placeholder is exactly that, and it means any /// datagram ending in sixteen zero octets reads as a teardown. token: ?[token_len]u8 = null, in_use: bool = false, /// For an identifier this endpoint issued: whether the NEW_CONNECTION_ID /// that offered it was lost and is owed again (§13.3). resend: bool = false, /// The identifier itself, `len` octets of `bytes`. The slice points into /// the entry, so it is only good while the entry is: retiring an entry /// in a table, or reusing its slot, changes what it says. pub fn id(e: *const Entry) []const u8 { return e.bytes[0..e.len]; } test id { var e: Entry = .{ .sequence = 1, .len = 4 }; @memcpy(e.bytes[0..4], &[_]u8{ 0xde, 0xad, 0xbe, 0xef }); try std.testing.expectEqualSlices(u8, &.{ 0xde, 0xad, 0xbe, 0xef }, e.id()); } }; /// The identifiers this endpoint has issued and must therefore answer to. pub const Issued = struct { entries: [capacity]Entry = @splat(.{ .sequence = 0 }), /// The next sequence number to use. §5.1.1: "the sequence number on each /// newly issued connection ID MUST increase by 1", and the first one — /// the Source Connection ID of the handshake — is zero. next_sequence: u64 = 0, /// The length every identifier this endpoint issues will have, taken from /// the first. len: usize = 0, /// Records the identifier the handshake used, which is sequence zero, and /// fixes its length as the length of every identifier issued after it. /// `id` is copied; it may be empty, for an endpoint that uses zero-length /// identifiers, but not longer than `max_len`. pub fn init(id: []const u8) Issued { assert(id.len <= max_len); var issued: Issued = .{ .len = id.len }; // No token: §5.1.1's sequence-zero identifier is the Source // Connection ID of the handshake, and a token for it travels in the // `stateless_reset_token` transport parameter if it travels at all. _ = issued.add(id, null) catch unreachable; return issued; } test init { const issued: Issued = .init(&.{ 0xc0, 0xff, 0xee, 0x00 }); try std.testing.expectEqual(@as(usize, 4), issued.len); try std.testing.expect(issued.accepts(&.{ 0xc0, 0xff, 0xee, 0x00 })); try std.testing.expectEqual(@as(u64, 1), issued.next_sequence); } /// Adds an identifier, returning its sequence number: the next one, since /// §5.1.1 has each "increase by 1". `id` is copied, and `token` is the /// stateless reset token to send with it in NEW_CONNECTION_ID. /// /// Fails with `error.ProtocolViolation` if `id` is not the length every /// identifier this endpoint issues has (see the file's comment), and with /// `error.NoRoom` when all `capacity` slots hold identifiers the peer has /// not retired. Neither consumes a sequence number. pub fn add(issued: *Issued, id: []const u8, token: ?[token_len]u8) Error!u64 { if (id.len != issued.len) return error.ProtocolViolation; for (&issued.entries) |*entry| { if (entry.in_use) continue; entry.* = .{ .sequence = issued.next_sequence, .len = id.len, .token = token, .in_use = true, }; @memcpy(entry.bytes[0..id.len], id); issued.next_sequence += 1; return entry.sequence; } return error.NoRoom; } test add { var issued: Issued = .init(&.{ 1, 2, 3, 4 }); const token: [token_len]u8 = @splat(0x5a); try std.testing.expectEqual(@as(u64, 1), try issued.add(&.{ 5, 6, 7, 8 }, token)); try std.testing.expect(issued.accepts(&.{ 5, 6, 7, 8 })); // A different length is never issued. try std.testing.expectError(error.ProtocolViolation, issued.add(&.{ 9, 9 }, token)); } /// Whether a packet addressed to `id` belongs to this connection. /// /// §5.1.1: "any active connection ID is valid for use with the current /// connection at any time, in any packet type" — so this is every /// identifier that has not been retired, not merely the newest. pub fn accepts(issued: *const Issued, id: []const u8) bool { for (&issued.entries) |*entry| { if (!entry.in_use) continue; if (std.mem.eql(u8, entry.id(), id)) return true; } return false; } test accepts { var issued: Issued = .init(&.{ 1, 2, 3, 4 }); _ = try issued.add(&.{ 5, 6, 7, 8 }, @splat(0)); // Both the handshake's and the newer one. try std.testing.expect(issued.accepts(&.{ 1, 2, 3, 4 })); try std.testing.expect(issued.accepts(&.{ 5, 6, 7, 8 })); try std.testing.expect(!issued.accepts(&.{ 9, 9, 9, 9 })); } /// §19.16: the peer will not use this identifier again, so the slot is /// free and the peer is owed a replacement. /// /// "Receipt of a RETIRE_CONNECTION_ID frame containing a sequence number /// greater than any previously sent to the peer MUST be treated as a /// connection error of type PROTOCOL_VIOLATION" — a peer retiring /// something that was never issued is talking about a connection this is /// not. /// /// The identifier stops being accepted at once. The replacement is the /// caller's to issue, with `add`; nothing here records that it is owed. pub fn retire(issued: *Issued, sequence: u64) Error!void { if (sequence >= issued.next_sequence) return error.ProtocolViolation; for (&issued.entries) |*entry| { if (!entry.in_use or entry.sequence != sequence) continue; entry.* = .{ .sequence = 0 }; return; } // Already gone, which is ordinary: §19.16's frame can arrive twice. } test retire { var issued: Issued = .init(&.{ 1, 2, 3, 4 }); _ = try issued.add(&.{ 5, 6, 7, 8 }, @splat(0)); // RETIRE_CONNECTION_ID with sequence zero: the handshake's is done. try issued.retire(0); try std.testing.expect(!issued.accepts(&.{ 1, 2, 3, 4 })); try std.testing.expectEqual(@as(usize, 1), issued.active()); // Sequence 2 was never issued. try std.testing.expectError(error.ProtocolViolation, issued.retire(2)); } /// How many identifiers the peer currently has of ours, which is what /// §5.1.1 holds against the peer's `active_connection_id_limit`. pub fn active(issued: *const Issued) usize { var n: usize = 0; for (&issued.entries) |*entry| { if (entry.in_use) n += 1; } return n; } test active { var issued: Issued = .init(&.{ 1, 2, 3, 4 }); try std.testing.expectEqual(@as(usize, 1), issued.active()); _ = try issued.add(&.{ 5, 6, 7, 8 }, @splat(0)); try std.testing.expectEqual(@as(usize, 2), issued.active()); } /// Whether there is room to issue another: whether `add` would find a /// free slot. The table holds `capacity`, whatever limit the peer /// advertised, so a caller also has to compare `active` with that. pub fn hasRoom(issued: *const Issued) bool { for (&issued.entries) |*entry| { if (!entry.in_use) return true; } return false; } test hasRoom { var issued: Issued = .init(&.{ 1, 2, 3, 4 }); var n: u8 = 0; while (issued.hasRoom()) : (n += 1) _ = try issued.add(&.{ 5, 6, 7, n }, @splat(n)); try std.testing.expectEqual(@as(usize, capacity), issued.active()); try std.testing.expectError(error.NoRoom, issued.add(&.{ 9, 9, 9, 9 }, @splat(0))); } }; /// The identifiers the peer has issued, any of which may be used to address /// it. pub const Received = struct { entries: [capacity]Entry = @splat(.{ .sequence = 0 }), /// §19.15: "a receiver MUST ignore any Retire Prior To fields that do not /// increase the largest received Retire Prior To value." retire_prior_to: u64 = 0, /// The identifiers this endpoint has retired, and how far the /// RETIRE_CONNECTION_ID for each has got. /// /// §5.1.2: "an endpoint SHOULD allow for sending and tracking a number of /// RETIRE_CONNECTION_ID frames of at least twice the value of the /// active_connection_id_limit transport parameter", and "MUST NOT forget /// a connection ID without retiring it". Twice the table, then, and an /// error past that rather than a retirement quietly dropped. /// /// An acknowledged one is kept until its slot is wanted, which is what /// lets `offer` recognize a NEW_CONNECTION_ID repeated after the /// identifier it offers was retired. retirements: [2 * capacity]?Retirement = @splat(null), /// One past the highest sequence number the peer has issued so far, as /// far as this endpoint has heard: the "Next Sequence Number" a /// multipath PATH_CIDS_BLOCKED reports. next_sequence: u64 = 0, /// One identifier in `retirements`. pub const Retirement = struct { sequence: u64, state: enum { owed, sent, acknowledged }, }; /// Records the identifier the handshake used, sequence zero. `id` is /// copied, and has no stateless reset token, for the reason /// `Entry.token` gives. pub fn init(id: []const u8) Received { assert(id.len <= max_len); var received: Received = .{}; received.entries[0] = .{ .sequence = 0, .len = id.len, .in_use = true }; @memcpy(received.entries[0].bytes[0..id.len], id); received.next_sequence = 1; return received; } test init { const received: Received = .init(&.{ 0xaa, 0xbb, 0xcc, 0xdd }); try std.testing.expectEqualSlices(u8, &.{ 0xaa, 0xbb, 0xcc, 0xdd }, received.current().?); try std.testing.expectEqual(@as(?[token_len]u8, null), received.entries[0].token); } /// §19.15: one NEW_CONNECTION_ID frame. /// /// `limit` is this endpoint's own `active_connection_id_limit`, which is /// the number it promised to maintain and therefore the number above which /// it must close the connection rather than quietly keep more. /// /// An increased Retire Prior To retires the identifiers below it first, /// and owes a RETIRE_CONNECTION_ID for each (see `nextOwed`) -- which may /// include the one the caller is addressing the peer with, so `current` /// is worth asking again afterwards. An exact repeat of a frame already /// processed changes nothing. Every error is one the caller closes the /// connection with; on `error.ConnectionIdLimitError` the identifier has /// already been stored. pub fn offer( received: *Received, sequence: u64, retire_prior_to: u64, id: []const u8, token: [token_len]u8, limit: u64, ) Error!void { // §19.15: "Values less than 1 and greater than 20 are invalid and // MUST be treated as a connection error of type // FRAME_ENCODING_ERROR." if (id.len < 1 or id.len > max_len) return error.FrameEncodingError; // "Receiving a value in the Retire Prior To field that is greater // than that in the Sequence Number field MUST be treated as a // connection error of type FRAME_ENCODING_ERROR." if (retire_prior_to > sequence) return error.FrameEncodingError; received.next_sequence = @max(received.next_sequence, sequence +| 1); // §19.15: the same frame may arrive more than once and that "MUST NOT // be treated as a connection error". What is not ordinary is the same // sequence number naming a different identifier, or the same // identifier arriving under a different number — either means the // peer has lost track of what it issued. for (&received.entries) |*entry| { if (!entry.in_use) continue; if (entry.sequence == sequence) { if (!std.mem.eql(u8, entry.id(), id)) return error.ProtocolViolation; const known = entry.token orelse return error.ProtocolViolation; if (!std.mem.eql(u8, &known, &token)) return error.ProtocolViolation; return; } if (std.mem.eql(u8, entry.id(), id)) return error.ProtocolViolation; } // A frame offering an identifier this endpoint has since retired: // sent again because a packet was lost (§13.3), and arriving after // the identifier was used and given up. It stays retired. if (received.retiredAlready(sequence)) return; // §5.1.2: "upon receipt of an increased Retire Prior To field, the // peer MUST stop using the corresponding connection IDs and retire // them with RETIRE_CONNECTION_ID frames before adding the newly // provided connection ID". Before, so that the count cannot exceed // the limit in between. if (retire_prior_to > received.retire_prior_to) { received.retire_prior_to = retire_prior_to; for (&received.entries) |*entry| { if (!entry.in_use or entry.sequence >= retire_prior_to) continue; try received.owe(entry.sequence); entry.* = .{ .sequence = 0 }; } } // §19.15: "an endpoint that receives a NEW_CONNECTION_ID frame with a // sequence number smaller than the Retire Prior To field of a // previously received NEW_CONNECTION_ID frame MUST send a // corresponding RETIRE_CONNECTION_ID frame that retires the newly // received connection ID" — it is dead on arrival, so it is retired // rather than stored. if (sequence < received.retire_prior_to) return received.owe(sequence); for (&received.entries) |*entry| { if (entry.in_use) continue; entry.* = .{ .sequence = sequence, .len = id.len, .token = token, .in_use = true }; @memcpy(entry.bytes[0..id.len], id); if (received.active() > limit) return error.ConnectionIdLimitError; return; } // The table is full and nothing was retired to make room, which is // the peer exceeding the limit this endpoint advertised. return error.ConnectionIdLimitError; } test offer { var received: Received = .init(&.{ 1, 2, 3, 4 }); // NEW_CONNECTION_ID: sequence 1, Retire Prior To 1, with a token. try received.offer(1, 1, &.{ 5, 6, 7, 8 }, @splat(0x77), 2); // The handshake's identifier is retired, and the peer is owed word // of it. try std.testing.expectEqualSlices(u8, &.{ 5, 6, 7, 8 }, received.current().?); try std.testing.expectEqual(@as(?u64, 0), received.nextOwed()); } /// The identifier to address the peer with: the lowest-numbered one that /// has not been retired, or null if every one has been. /// /// The slice points into the table, and is good until that entry is /// retired -- by `retire`, or by an `offer` whose Retire Prior To passes /// it -- after which its slot may hold another identifier. pub fn current(received: *const Received) ?[]const u8 { var best: ?*const Entry = null; for (&received.entries) |*entry| { if (!entry.in_use) continue; if (best == null or entry.sequence < best.?.sequence) best = entry; } return if (best) |entry| entry.id() else null; } test current { var received: Received = .init(&.{ 1, 2, 3, 4 }); try received.offer(1, 0, &.{ 5, 6, 7, 8 }, @splat(1), 4); // Still the handshake's: sequence zero is the lowest. try std.testing.expectEqualSlices(u8, &.{ 1, 2, 3, 4 }, received.current().?); try received.retire(0); try std.testing.expectEqualSlices(u8, &.{ 5, 6, 7, 8 }, received.current().?); } /// Stops using one of the peer's identifiers, so that the peer can be /// told and can replace it: a RETIRE_CONNECTION_ID for `sequence` is /// owed from now on (see `nextOwed`). A sequence number not held is /// ignored. `error.ConnectionIdLimitError` is too many retirements /// still unacknowledged, and leaves the identifier in use. pub fn retire(received: *Received, sequence: u64) Error!void { for (&received.entries) |*entry| { if (!entry.in_use or entry.sequence != sequence) continue; try received.owe(sequence); entry.* = .{ .sequence = 0 }; return; } } test retire { var received: Received = .init(&.{ 1, 2, 3, 4 }); try received.offer(1, 0, &.{ 5, 6, 7, 8 }, @splat(1), 4); try received.retire(1); try std.testing.expectEqual(@as(usize, 1), received.active()); try std.testing.expectEqual(@as(?u64, 1), received.nextOwed()); } /// An identifier other than `in_use_id`, for a new path, or null if the /// peer has issued no other. /// /// §9.5: "an endpoint MUST NOT reuse a connection ID when sending from /// more than one local address", so migrating needs one of these and a /// migration with none available cannot legally happen. /// /// Only `in_use_id` is excluded: this does not know which identifiers /// have been used on other paths before, so a caller that has used /// several must retire the ones it is done with for this to answer only /// fresh ones. The slice lives as long as `current`'s does. pub fn spare(received: *const Received, in_use_id: []const u8) ?[]const u8 { for (&received.entries) |*entry| { if (!entry.in_use) continue; if (std.mem.eql(u8, entry.id(), in_use_id)) continue; return entry.id(); } return null; } test spare { var received: Received = .init(&.{ 1, 2, 3, 4 }); // The handshake's identifier is all there is: nowhere to migrate to. try std.testing.expectEqual(@as(?[]const u8, null), received.spare(received.current().?)); try received.offer(1, 0, &.{ 5, 6, 7, 8 }, @splat(1), 4); try std.testing.expectEqualSlices(u8, &.{ 5, 6, 7, 8 }, received.spare(received.current().?).?); } /// How many of the peer's identifiers this endpoint holds and has not /// retired: what §5.1.1 compares with this endpoint's own /// `active_connection_id_limit`. pub fn active(received: *const Received) usize { var n: usize = 0; for (&received.entries) |*entry| { if (entry.in_use) n += 1; } return n; } test active { var received: Received = .init(&.{ 1, 2, 3, 4 }); try std.testing.expectEqual(@as(usize, 1), received.active()); try received.offer(1, 0, &.{ 5, 6, 7, 8 }, @splat(1), 4); try std.testing.expectEqual(@as(usize, 2), received.active()); } /// The next sequence number a RETIRE_CONNECTION_ID frame is owed for. It /// stays owed until `sentRetire` says the frame went. pub fn nextOwed(received: *const Received) ?u64 { for (received.retirements) |maybe| { const r = maybe orelse continue; if (r.state == .owed) return r.sequence; } return null; } test nextOwed { var received: Received = .init(&.{ 1, 2, 3, 4 }); try std.testing.expectEqual(@as(?u64, null), received.nextOwed()); try received.retire(0); try std.testing.expectEqual(@as(?u64, 0), received.nextOwed()); } /// Notes that the RETIRE_CONNECTION_ID frame for `sequence` has gone out, /// so it is owed no longer. `lostRetire` owes it again if the packet /// carrying it is lost, and `ackedRetire` lets it go. pub fn sentRetire(received: *Received, sequence: u64) void { if (received.find(sequence)) |r| { if (r.state == .owed) r.state = .sent; } } test sentRetire { var received: Received = .init(&.{ 1, 2, 3, 4 }); try received.retire(0); const sequence = received.nextOwed().?; // ...RETIRE_CONNECTION_ID for `sequence` is sent... received.sentRetire(sequence); try std.testing.expectEqual(@as(?u64, null), received.nextOwed()); } /// The packet carrying the RETIRE_CONNECTION_ID for `sequence` was lost, /// so the frame is owed again: §13.3 has it "sent again" whenever that /// happens. Owing one already owed, or one already acknowledged by way /// of another packet, changes nothing. pub fn lostRetire(received: *Received, sequence: u64) void { if (received.find(sequence)) |r| { if (r.state == .sent) r.state = .owed; } } test lostRetire { var received: Received = .init(&.{ 1, 2, 3, 4 }); try received.retire(0); received.sentRetire(0); try std.testing.expectEqual(@as(?u64, null), received.nextOwed()); // ...the packet carrying it is lost... received.lostRetire(0); try std.testing.expectEqual(@as(?u64, 0), received.nextOwed()); } /// The peer has the RETIRE_CONNECTION_ID for `sequence`, so it no longer /// counts against §5.1.2's limit. pub fn ackedRetire(received: *Received, sequence: u64) void { if (received.find(sequence)) |r| r.state = .acknowledged; } test ackedRetire { var received: Received = .init(&.{ 1, 2, 3, 4 }); try received.retire(0); received.sentRetire(0); received.ackedRetire(0); // ...and a loss reported afterwards for another copy owes nothing. received.lostRetire(0); try std.testing.expectEqual(@as(?u64, null), received.nextOwed()); } fn find(received: *Received, sequence: u64) ?*Retirement { for (&received.retirements) |*slot| { if (slot.*) |*r| if (r.sequence == sequence) return r; } return null; } fn retiredAlready(received: *const Received, sequence: u64) bool { for (received.retirements) |maybe| { if (maybe) |r| if (r.sequence == sequence) return true; } return false; } fn owe(received: *Received, sequence: u64) Error!void { if (received.retiredAlready(sequence)) return; // An empty slot, or failing that an acknowledged one: the lowest, // being the one a repeated NEW_CONNECTION_ID is least likely still // to be on its way for. var oldest: ?*?Retirement = null; for (&received.retirements) |*slot| { const r = slot.* orelse { slot.* = .{ .sequence = sequence, .state = .owed }; return; }; if (r.state != .acknowledged) continue; if (oldest == null or r.sequence < oldest.?.*.?.sequence) oldest = slot; } const slot = oldest orelse return error.ConnectionIdLimitError; slot.* = .{ .sequence = sequence, .state = .owed }; } }; const testing = std.testing; test "RFC 9000 5.1.1: the handshake identifier is sequence zero" { var issued: Issued = .init(&.{ 1, 2, 3, 4 }); try testing.expectEqual(@as(u64, 1), issued.next_sequence); try testing.expectEqual(@as(usize, 1), issued.active()); try testing.expect(issued.accepts(&.{ 1, 2, 3, 4 })); try testing.expect(!issued.accepts(&.{ 9, 9, 9, 9 })); // "The sequence number on each newly issued connection ID MUST increase // by 1." try testing.expectEqual(@as(u64, 1), try issued.add(&.{ 5, 6, 7, 8 }, @splat(0xaa))); try testing.expectEqual(@as(u64, 2), try issued.add(&.{ 9, 10, 11, 12 }, @splat(0xbb))); // And every one of them is valid: "any active connection ID is valid for // use with the current connection at any time, in any packet type". try testing.expect(issued.accepts(&.{ 1, 2, 3, 4 })); try testing.expect(issued.accepts(&.{ 5, 6, 7, 8 })); try testing.expect(issued.accepts(&.{ 9, 10, 11, 12 })); } test "RFC 9000 5.1: every identifier an endpoint issues is the same length" { // A short header has no length field, so an endpoint has to know how long // its own identifier is before it can find it. Choosing one length and // keeping it is the simplest way always to know. var issued: Issued = .init(&.{ 1, 2, 3, 4 }); try testing.expectError(error.ProtocolViolation, issued.add(&.{ 1, 2 }, @splat(0))); try testing.expectError(error.ProtocolViolation, issued.add(&.{ 1, 2, 3, 4, 5 }, @splat(0))); } test "RFC 9000 19.16: a peer cannot retire what was never issued" { // "Receipt of a RETIRE_CONNECTION_ID frame containing a sequence number // greater than any previously sent to the peer MUST be treated as a // connection error of type PROTOCOL_VIOLATION." var issued: Issued = .init(&.{ 1, 2, 3, 4 }); _ = try issued.add(&.{ 5, 6, 7, 8 }, @splat(0)); try testing.expectError(error.ProtocolViolation, issued.retire(2)); try issued.retire(1); try testing.expect(!issued.accepts(&.{ 5, 6, 7, 8 })); // And twice is not an error: the frame can arrive more than once. try issued.retire(1); // The slot came back, so another can be issued in its place — which // §5.1.1 asks for: "an endpoint SHOULD supply a new connection ID when // the peer retires a connection ID". try testing.expect(issued.hasRoom()); } test "RFC 9000 19.15: the same frame twice is not an error, and a lie is" { var received: Received = .init(&.{ 1, 2, 3, 4 }); const token: [token_len]u8 = @splat(0x11); try received.offer(1, 0, &.{ 5, 6, 7, 8 }, token, 4); try testing.expectEqual(@as(usize, 2), received.active()); // "Receipt of the same frame multiple times MUST NOT be treated as a // connection error." try received.offer(1, 0, &.{ 5, 6, 7, 8 }, token, 4); try testing.expectEqual(@as(usize, 2), received.active()); // "If an endpoint receives a NEW_CONNECTION_ID frame that repeats a // previously issued connection ID with a different Stateless Reset Token // field value or a different Sequence Number field value, or if a // sequence number is used for different connection IDs, the endpoint MAY // treat that receipt as a connection error of type PROTOCOL_VIOLATION." try testing.expectError( error.ProtocolViolation, received.offer(1, 0, &.{ 5, 6, 7, 8 }, @splat(0x22), 4), ); try testing.expectError( error.ProtocolViolation, received.offer(1, 0, &.{ 9, 9, 9, 9 }, token, 4), ); try testing.expectError( error.ProtocolViolation, received.offer(2, 0, &.{ 5, 6, 7, 8 }, token, 4), ); } test "RFC 9000 19.15: a length or a retirement the frame cannot mean" { var received: Received = .init(&.{ 1, 2, 3, 4 }); const token: [token_len]u8 = @splat(0); // "Values less than 1 and greater than 20 are invalid." try testing.expectError(error.FrameEncodingError, received.offer(1, 0, &.{}, token, 4)); var too_long: [21]u8 = @splat(7); try testing.expectError(error.FrameEncodingError, received.offer(1, 0, &too_long, token, 4)); // "Receiving a value in the Retire Prior To field that is greater than // that in the Sequence Number field MUST be treated as a connection error // of type FRAME_ENCODING_ERROR." try testing.expectError(error.FrameEncodingError, received.offer(1, 2, &.{ 5, 6 }, token, 4)); } test "RFC 9000 5.1.2: Retire Prior To empties the pool before it refills it" { // "Upon receipt of an increased Retire Prior To field, the peer MUST stop // using the corresponding connection IDs and retire them with // RETIRE_CONNECTION_ID frames before adding the newly provided connection // ID to the set of active connection IDs. This ordering allows an // endpoint to replace all active connection IDs without the possibility // of a peer having no available connection IDs and without exceeding the // limit." var received: Received = .init(&.{ 1, 2, 3, 4 }); try received.offer(1, 0, &.{ 5, 6, 7, 8 }, @splat(1), 2); try testing.expectEqual(@as(usize, 2), received.active()); // A limit of two, and a third identifier that retires both of the others. // Without the ordering this would be three at once and a // CONNECTION_ID_LIMIT_ERROR. try received.offer(2, 2, &.{ 9, 10, 11, 12 }, @splat(2), 2); try testing.expectEqual(@as(usize, 1), received.active()); try testing.expectEqualSlices(u8, &.{ 9, 10, 11, 12 }, received.current().?); // And the retirements are owed to the peer, which is how it learns it may // issue more. try testing.expectEqual(@as(?u64, 0), received.nextOwed()); received.sentRetire(0); try testing.expectEqual(@as(?u64, 1), received.nextOwed()); received.sentRetire(1); try testing.expectEqual(@as(?u64, null), received.nextOwed()); // §19.15: a smaller Retire Prior To afterwards has no effect. try received.offer(3, 1, &.{ 13, 14, 15, 16 }, @splat(3), 2); try testing.expectEqual(@as(usize, 2), received.active()); } test "RFC 9000 19.15: an identifier already retired on arrival is retired, not stored" { // "An endpoint that receives a NEW_CONNECTION_ID frame with a sequence // number smaller than the Retire Prior To field of a previously received // NEW_CONNECTION_ID frame MUST send a corresponding RETIRE_CONNECTION_ID // frame that retires the newly received connection ID." var received: Received = .init(&.{ 1, 2, 3, 4 }); try received.offer(3, 3, &.{ 9, 9, 9, 9 }, @splat(9), 4); try testing.expectEqual(@as(usize, 1), received.active()); // Sequence 2 is below the watermark of 3, so it is dead on arrival. try received.offer(2, 0, &.{ 7, 7, 7, 7 }, @splat(7), 4); try testing.expectEqual(@as(usize, 1), received.active()); // The retirement for it is owed, along with the one for sequence zero. var owed: usize = 0; while (received.nextOwed()) |sequence| : (owed += 1) received.sentRetire(sequence); try testing.expectEqual(@as(usize, 2), owed); } test "RFC 9000 5.1.1: more identifiers than were promised ends the connection" { // "After processing a NEW_CONNECTION_ID frame and adding and retiring // active connection IDs, if the number of active connection IDs exceeds // the value advertised in its active_connection_id_limit transport // parameter, an endpoint MUST close the connection with an error of type // CONNECTION_ID_LIMIT_ERROR." var received: Received = .init(&.{ 1, 2, 3, 4 }); try received.offer(1, 0, &.{ 5, 6 }, @splat(1), 2); try testing.expectError( error.ConnectionIdLimitError, received.offer(2, 0, &.{ 7, 8 }, @splat(2), 2), ); } test "RFC 9000 9.5: migrating needs an identifier not already in use" { // "An endpoint MUST NOT reuse a connection ID when sending from more than // one local address" — because a connection ID is visible in the clear on // every packet, so the same one on two paths tells an observer they are // the same connection. A pool is what makes changing network // unobservable rather than merely possible. var received: Received = .init(&.{ 1, 2, 3, 4 }); try testing.expectEqual(@as(?[]const u8, null), received.spare(&.{ 1, 2, 3, 4 })); try received.offer(1, 0, &.{ 5, 6, 7, 8 }, @splat(1), 4); try testing.expectEqualSlices(u8, &.{ 5, 6, 7, 8 }, received.spare(&.{ 1, 2, 3, 4 }).?); } test "RFC 9000 5.1.2: no retirement is forgotten while the frames for others are unsent" { // "An endpoint MUST NOT forget a connection ID without retiring it, // though it MAY choose to treat having connection IDs in need of // retirement that exceed this limit as a connection error of type // CONNECTION_ID_LIMIT_ERROR." And the limit it means: "an endpoint // SHOULD allow for sending and tracking a number of RETIRE_CONNECTION_ID // frames of at least twice the value of the active_connection_id_limit // transport parameter." var received: Received = .init(&.{ 1, 2, 3, 4 }); try received.offer(1, 0, &.{ 5, 6 }, @splat(1), capacity); try received.offer(2, 0, &.{ 7, 8 }, @splat(2), capacity); try received.offer(3, 0, &.{ 9, 10 }, @splat(3), capacity); // Four retired at once, then a fifth, with no frame sent in between. try received.offer(4, 4, &.{ 11, 12 }, @splat(4), capacity); try received.offer(5, 5, &.{ 13, 14 }, @splat(5), capacity); var owed: usize = 0; while (received.nextOwed()) |sequence| : (owed += 1) received.sentRetire(sequence); try testing.expectEqual(@as(usize, 5), owed); // Twice the limit unsent and unacknowledged, and one more is an error // rather than an obligation quietly dropped. var sequence: u64 = 6; while (sequence <= 2 * capacity) : (sequence += 1) { try received.offer(sequence, sequence, &.{ 15, @intCast(sequence) }, @splat(@intCast(sequence)), capacity); } try testing.expectError( error.ConnectionIdLimitError, received.offer(sequence, sequence, &.{ 15, @intCast(sequence) }, @splat(@intCast(sequence)), capacity), ); } test "RFC 9000 5.1.2: an acknowledged retirement makes room for another" { var received: Received = .init(&.{ 1, 2, 3, 4 }); var sequence: u64 = 1; while (sequence < 40) : (sequence += 1) { try received.offer(sequence, sequence, &.{ 15, @intCast(sequence) }, @splat(@intCast(sequence)), capacity); while (received.nextOwed()) |owed| { received.sentRetire(owed); received.ackedRetire(owed); } } try testing.expectEqual(@as(usize, 1), received.active()); } test "RFC 9000 19.15: a repeated NEW_CONNECTION_ID does not bring back a retired identifier" { // A NEW_CONNECTION_ID is sent again when the packet carrying it is lost // (§13.3), so the same frame can arrive after this endpoint has already // used the identifier and retired it. Storing it again would put back an // identifier §5.1.2 says this endpoint has stopped using, and whose // retirement the peer may already have acted on. var received: Received = .init(&.{ 1, 2, 3, 4 }); try received.offer(1, 0, &.{ 5, 6, 7, 8 }, @splat(1), capacity); try received.retire(0); try received.offer(2, 0, &.{ 9, 10, 11, 12 }, @splat(2), capacity); try received.retire(1); while (received.nextOwed()) |sequence| { received.sentRetire(sequence); received.ackedRetire(sequence); } try testing.expectEqual(@as(usize, 1), received.active()); try received.offer(1, 0, &.{ 5, 6, 7, 8 }, @splat(1), capacity); try testing.expectEqual(@as(usize, 1), received.active()); try testing.expectEqualSlices(u8, &.{ 9, 10, 11, 12 }, received.current().?); try testing.expectEqual(@as(?u64, null), received.nextOwed()); }