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