╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2035 2036 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 2037 if (packet.len < 1) return error.EmptyPacket; 2038 var r = std.Io.Reader.fixed(packet[1..]); 2039 const recipient_channel = try r.takeInt(u32, .big); 2040 if (recipient_channel != expected_channel) return error.UnknownChannel; 2041 if (r.buffered().len != 0) return error.InvalidChannelMessage; 2042 } 2043 2044 /// send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 2045 /// of `budget`, the credit we're willing to have outstanding. used both for 2046 /// the pre-session discard path and by SessionCtx after it consumes a 2047 /// CHANNEL_DATA payload. 2048 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel, budget: u32) !void { 2049 if (ch.local_window >= budget / 2) return; 2050 const add = budget - ch.local_window; 2051 ch.local_window += add; 2052 var adj: std.ArrayList(u8) = .empty; 2053 defer adj.deinit(conn.allocator); 2054 try adj.append(conn.allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 2055 try writeU32(&adj, conn.allocator, ch.remote_id); 2056 try writeU32(&adj, conn.allocator, add); 2057 try conn.sc_cipher.writePacket(conn.io, conn.writer, adj.items); 2058 } 2059 2060 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 2061 var bytes: [4]u8 = undefined; 2062 std.mem.writeInt(u32, &bytes, value, .big); 2063 try buf.appendSlice(allocator, &bytes); 2064 } 2065 2066 test "pty dimensions have bounded axes and area, preserving normal and zero sizes" { 2067 for ([_]struct { width: u32, height: u32, expected: PtySize }{ 2068 .{ .width = 80, .height = 24, .expected = .{ .width_cells = 80, .height_cells = 24 } }, 2069 .{ .width = 0, .height = 0, .expected = .{ .width_cells = 0, .height_cells = 0 } }, 2070 .{ .width = 0xffffffff, .height = 0xffffffff, .expected = .{ .width_cells = 512, .height_cells = 128 } }, 2071 }) |case| { 2072 var bytes: [8]u8 = undefined; 2073 std.mem.writeInt(u32, bytes[0..4], case.width, .big); 2074 std.mem.writeInt(u32, bytes[4..8], case.height, .big); 2075 var reader = std.Io.Reader.fixed(&bytes); 2076 try std.testing.expectEqual(case.expected, try takePtySize(&reader)); 2077 } 2078 } 2079 2080 test "channel close before shell or exec is acknowledged" { 2081 const io = std.testing.io; 2082 const allocator = std.testing.allocator; 2083 const key = [_]u8{0x5a} ** 64; 2084 var encoded: [256]u8 = undefined; 2085 var encoded_writer = std.Io.Writer.fixed(&encoded); 2086 var sender = Cipher.init(&key, 0); 2087 var open: std.ArrayList(u8) = .empty; 2088 defer open.deinit(allocator); 2089 try open.append(allocator, SSH_MSG_CHANNEL_OPEN); 2090 try writeStringField(&open, allocator, "session"); 2091 try writeU32(&open, allocator, 7); 2092 try writeU32(&open, allocator, 32768); 2093 try writeU32(&open, allocator, 32768); 2094 try sender.writePacket(io, &encoded_writer, open.items); 2095 try sender.writePacket(io, &encoded_writer, &.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 0 }); 2096 var reader = std.Io.Reader.fixed(encoded_writer.buffered()); 2097 var output: [256]u8 = undefined; 2098 var writer = std.Io.Writer.fixed(&output); 2099 var conn = Conn{ 2100 .io = io, 2101 .allocator = allocator, 2102 .reader = &reader, 2103 .writer = &writer, 2104 .cs_cipher = Cipher.init(&key, 0), 2105 .sc_cipher = Cipher.init(&key, 0), 2106 .rekey = undefined, 2107 }; 2108 const Handler = struct { 2109 pub fn handleSession(_: *@This(), _: *SessionCtx, _: Request) !void { 2110 return error.UnexpectedSession; 2111 } 2112 }; 2113 var handler = Handler{}; 2114 try runChannelLayer(&conn, &([_]u8{0} ** fingerprint_len), &handler); 2115 var output_reader = std.Io.Reader.fixed(writer.buffered()); 2116 var receiver = Cipher.init(&key, 0); 2117 const confirmation = try receiver.readPacket(allocator, &output_reader); 2118 defer allocator.free(confirmation); 2119 try std.testing.expectEqual(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, confirmation[0]); 2120 const close = try receiver.readPacket(allocator, &output_reader); 2121 defer allocator.free(close); 2122 try std.testing.expectEqualSlices(u8, &.{ SSH_MSG_CHANNEL_CLOSE, 0, 0, 0, 7 }, close); 2123 try std.testing.expectEqual(0, output_reader.buffered().len); 2124 } 2125