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