╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1955 // string data 1956 const payload = try parseChannelData(packet, extended, ch.local_id); 1957 const data_len: u32 = @intCast(payload.len); 1958 1959 if (data_len > ch.local_window) return error.WindowExceeded; 1960 ch.local_window -= data_len; 1961 try maybeRefillRecvWindow(conn, ch, initial_recv_window); 1962 } 1963 1964 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1965 if (packet.len < 1) return error.EmptyPacket; 1966 var r = std.Io.Reader.fixed(packet[1..]); 1967 const recipient_channel = try r.takeInt(u32, .big); 1968 if (recipient_channel != expected_channel) return error.UnknownChannel; 1969 if (extended) { 1970 const data_type = try r.takeInt(u32, .big); 1971 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1972 } 1973 const data_len = try r.takeInt(u32, .big); 1974 // catches both data_len > remaining (would otherwise be a bounds-check 1975 // panic on the slice) and trailing junk after the declared payload. 1976 if (data_len != r.buffered().len) return error.InvalidChannelData; 1977 return r.buffered(); 1978 } 1979 1980 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1981 if (packet.len < 1) return error.EmptyPacket; 1982 var r = std.Io.Reader.fixed(packet[1..]); 1983 const recipient_channel = try r.takeInt(u32, .big); 1984 if (recipient_channel != expected_channel) return error.UnknownChannel; 1985 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1986 } 1987 1988 /// send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1989 /// of `budget`, the credit we're willing to have outstanding. used both for 1990 /// the pre-session discard path and by SessionCtx after it consumes a 1991 /// CHANNEL_DATA payload. 1992 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel, budget: u32) !void { 1993 if (ch.local_window >= budget / 2) return; 1994 const add = budget - ch.local_window; 1995 ch.local_window += add; 1996 var adj: std.ArrayList(u8) = .empty; 1997 defer adj.deinit(conn.allocator); 1998 try adj.append(conn.allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1999 try writeU32(&adj, conn.allocator, ch.remote_id); 2000 try writeU32(&adj, conn.allocator, add); 2001 try conn.sc_cipher.writePacket(conn.io, conn.writer, adj.items); 2002 } 2003 2004 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 2005 var bytes: [4]u8 = undefined; 2006 std.mem.writeInt(u32, &bytes, value, .big); 2007 try buf.appendSlice(allocator, &bytes); 2008 } 2009