╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ serve_ssh_protocol.zig ───────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1828 try replyChannelRequest(conn, ch, want_reply, false); 1829 return null; 1830 } 1831 1832 fn replyChannelRequest(conn: *Conn, ch: *Channel, want_reply: bool, success: bool) !void { 1833 if (!want_reply) return; 1834 var reply: std.ArrayList(u8) = .empty; 1835 defer reply.deinit(conn.allocator); 1836 try reply.append(conn.allocator, if (success) SSH_MSG_CHANNEL_SUCCESS else SSH_MSG_CHANNEL_FAILURE); 1837 try writeU32(&reply, conn.allocator, ch.remote_id); 1838 try conn.sc_cipher.writePacket(conn.io, conn.writer, reply.items); 1839 } 1840 1841 fn handleWindowAdjust(ch: *Channel, packet: []const u8) !void { 1842 // byte SSH_MSG_CHANNEL_WINDOW_ADJUST 1843 // uint32 recipient_channel 1844 // uint32 bytes_to_add 1845 var r = std.Io.Reader.fixed(packet[1..]); 1846 const recipient_channel = try r.takeInt(u32, .big); 1847 if (recipient_channel != ch.local_id) return error.UnknownChannel; 1848 const add = try r.takeInt(u32, .big); 1849 ch.remote_window +|= add; 1850 } 1851 1852 fn discardChannelData(conn: *Conn, ch: *Channel, packet: []const u8, extended: bool) !void { 1853 // byte SSH_MSG_CHANNEL_DATA/EXTENDED_DATA 1854 // uint32 recipient_channel 1855 // [if extended: uint32 type_code] 1856 // string data 1857 const payload = try parseChannelData(packet, extended, ch.local_id); 1858 const data_len: u32 = @intCast(payload.len); 1859 1860 if (data_len > ch.local_window) return error.WindowExceeded; 1861 ch.local_window -= data_len; 1862 try maybeRefillRecvWindow(conn, ch); 1863 } 1864 1865 pub fn parseChannelData(packet: []const u8, extended: bool, expected_channel: u32) ![]const u8 { 1866 if (packet.len < 1) return error.EmptyPacket; 1867 var r = std.Io.Reader.fixed(packet[1..]); 1868 const recipient_channel = try r.takeInt(u32, .big); 1869 if (recipient_channel != expected_channel) return error.UnknownChannel; 1870 if (extended) { 1871 const data_type = try r.takeInt(u32, .big); 1872 if (data_type != SSH_EXTENDED_DATA_STDERR) return error.UnsupportedExtendedData; 1873 } 1874 const data_len = try r.takeInt(u32, .big); 1875 // catches both data_len > remaining (would otherwise be a bounds-check 1876 // panic on the slice) and trailing junk after the declared payload. 1877 if (data_len != r.buffered().len) return error.InvalidChannelData; 1878 return r.buffered(); 1879 } 1880 1881 fn parseChannelId(packet: []const u8, expected_channel: u32) !void { 1882 if (packet.len < 1) return error.EmptyPacket; 1883 var r = std.Io.Reader.fixed(packet[1..]); 1884 const recipient_channel = try r.takeInt(u32, .big); 1885 if (recipient_channel != expected_channel) return error.UnknownChannel; 1886 if (r.buffered().len != 0) return error.InvalidChannelMessage; 1887 } 1888 1889 /// Send a CHANNEL_WINDOW_ADJUST if our receive window has dipped below half 1890 /// of the initial value. Used both for the pre-session discard path and by 1891 /// SessionCtx after it consumes a CHANNEL_DATA payload. 1892 fn maybeRefillRecvWindow(conn: *Conn, ch: *Channel) !void { 1893 if (ch.local_window >= initial_recv_window / 2) return; 1894 const add = initial_recv_window - ch.local_window; 1895 ch.local_window += add; 1896 var adj: std.ArrayList(u8) = .empty; 1897 defer adj.deinit(conn.allocator); 1898 try adj.append(conn.allocator, SSH_MSG_CHANNEL_WINDOW_ADJUST); 1899 try writeU32(&adj, conn.allocator, ch.remote_id); 1900 try writeU32(&adj, conn.allocator, add); 1901 try conn.sc_cipher.writePacket(conn.io, conn.writer, adj.items); 1902 } 1903 1904 pub fn writeU32(buf: *std.ArrayList(u8), allocator: std.mem.Allocator, value: u32) !void { 1905 var bytes: [4]u8 = undefined; 1906 std.mem.writeInt(u32, &bytes, value, .big); 1907 try buf.appendSlice(allocator, &bytes); 1908 } 1909