╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1653 } 1654 1655 fn discardChannelData( 1656 io: std.Io, 1657 allocator: std.mem.Allocator, 1658 sc_cipher: *Cipher, 1659 writer: *std.Io.Writer, 1660 ch: *Channel, 1661 packet: []const u8, 1662 extended: bool, 1663 ) !void { 1664 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1665 // uint32 recipient_channel 1666 // [if extended: uint32 type_code] 1667 // string data 1668 const payload = try parseChannelData(packet, extended, ch.local_id); 1669 const data_len: u32 = @intCast(payload.len); 1670 1671 if (data_len > ch.local_window) return error.WindowExceeded; 1672 ch.local_window -= data_len; 1673 try maybeRefillRecvWindow(io, allocator, sc_cipher, writer, ch); 1674 } 1675 1676 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1677 if (packet.len < 1) return error.EmptyPacket; 1678 var r = std.Io.Reader.fixed(packet[1..]); 1679 const recipient_channel = try r.takeInt(u32, .big); 1680 if (recipient_channel != expected_channel) return error.UnknownChannel; 1681 if (extended) { 1682 const data_type = try r.takeInt(u32, .big); 1683 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1684 } 1685 const data_len = try r.takeInt(u32, .big); 1686 // catches both data_len > remaining (would otherwise be a bounds-check 1687 // panic on the slice) and trailing junk after the declared payload. 1688 if (data_len != r.buffered().len) return error.InvalidChannelData; 1689 return r.buffered(); 1690 } 1691 1692 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1693 if (packet.len < 1) return error.EmptyPacket; 1694 var r = std.Io.Reader.fixed(packet[1..]); 1695 const recipient_channel = try r.takeInt(u32, .big); 1696 if (recipient_channel != expected_channel) return error.UnknownChannel; 1697 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1698 } 1699 1700 /// Send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1701 /// of the initial value. Used both for the pre-session discard path and by 1702 /// SessionCtx after it consumes a CHANNEL_DATA payload. 1703 fn maybeRefillRecvWindow( 1704 io: std.Io, 1705 allocator: std.mem.Allocator, 1706 sc_cipher: *Cipher, 1707 writer: *std.Io.Writer, 1708 ch: *Channel, 1709 ) !void { 1710 if (ch.local_window >= initial_recv_window / 2) return; 1711 const add = initial_recv_window - ch.local_window; 1712 ch.local_window += add; 1713 var adj: std.ArrayList(u8) = .empty; 1714 defer adj.deinit(allocator); 1715 try adj.append(allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1716 try writeU32(&adj, allocator, ch.remote_id); 1717 try writeU32(&adj, allocator, add); 1718 try sc_cipher.writePacket(io, allocator, writer, adj.items); 1719 } 1720 1721 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 1722 var bytes: [4]u8 = undefined; 1723 std.mem.writeInt(u32, &bytes, value, .big); 1724 try buf.appendSlice(allocator, &bytes); 1725 } 1726