╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1720 ch: *Channel, 1721 packet: []const u8, 1722 extended: bool, 1723 ) !void { 1724 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1725 // uint32 recipient_channel 1726 // [if extended: uint32 type_code] 1727 // string data 1728 const payload = try parseChannelData(packet, extended, ch.local_id); 1729 const data_len: u32 = @intCast(payload.len); 1730 1731 if (data_len > ch.local_window) return error.WindowExceeded; 1732 ch.local_window -= data_len; 1733 try maybeRefillRecvWindow(io, allocator, sc_cipher, writer, ch); 1734 } 1735 1736 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1737 if (packet.len < 1) return error.EmptyPacket; 1738 var r = std.Io.Reader.fixed(packet[1..]); 1739 const recipient_channel = try r.takeInt(u32, .big); 1740 if (recipient_channel != expected_channel) return error.UnknownChannel; 1741 if (extended) { 1742 const data_type = try r.takeInt(u32, .big); 1743 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1744 } 1745 const data_len = try r.takeInt(u32, .big); 1746 // catches both data_len > remaining (would otherwise be a bounds-check 1747 // panic on the slice) and trailing junk after the declared payload. 1748 if (data_len != r.buffered().len) return error.InvalidChannelData; 1749 return r.buffered(); 1750 } 1751 1752 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1753 if (packet.len < 1) return error.EmptyPacket; 1754 var r = std.Io.Reader.fixed(packet[1..]); 1755 const recipient_channel = try r.takeInt(u32, .big); 1756 if (recipient_channel != expected_channel) return error.UnknownChannel; 1757 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1758 } 1759 1760 /// Send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1761 /// of the initial value. Used both for the pre-session discard path and by 1762 /// SessionCtx after it consumes a CHANNEL_DATA payload. 1763 fn maybeRefillRecvWindow( 1764 io: std.Io, 1765 allocator: std.mem.Allocator, 1766 sc_cipher: *Cipher, 1767 writer: *std.Io.Writer, 1768 ch: *Channel, 1769 ) !void { 1770 if (ch.local_window >= initial_recv_window / 2) return; 1771 const add = initial_recv_window - ch.local_window; 1772 ch.local_window += add; 1773 var adj: std.ArrayList(u8) = .empty; 1774 defer adj.deinit(allocator); 1775 try adj.append(allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1776 try writeU32(&adj, allocator, ch.remote_id); 1777 try writeU32(&adj, allocator, add); 1778 try sc_cipher.writePacket(io, allocator, writer, adj.items); 1779 } 1780 1781 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 1782 var bytes: [4]u8 = undefined; 1783 std.mem.writeInt(u32, &bytes, value, .big); 1784 try buf.appendSlice(allocator, &bytes); 1785 } 1786