/// <summary> /// Construct an SSH2 channel stream. /// </summary> /// <param name="enclosingInstance">The channel that owns this stream.</param> /// <param name="observer">An observer to filter data messages.</param> protected internal SSH2ChannelStream(SSH2Channel enclosingInstance, PacketObserver observer) : base(enclosingInstance) { buffer = new byte[enclosingInstance.localwindow.available()]; this.observer = observer; this.channel = (SSH2Channel)enclosingInstance; }
internal DataWindow(SSH2Channel enclosingInstance, int windowsize, int packetsize) { InitBlock(enclosingInstance); this.windowsize = windowsize; this.packetsize = packetsize; }
private void InitBlock(SSH2Channel enclosingInstance) { this.enclosingInstance = enclosingInstance; }
/// <summary> /// Process a channel open request. /// </summary> /// <param name="type"></param> /// <param name="remoteid"></param> /// <param name="remotewindow"></param> /// <param name="remotepacket"></param> /// <param name="requestdata"></param> internal void ProcessChannelOpenRequest(System.String type, int remoteid, int remotewindow, int remotepacket, byte[] requestdata) { try { SSHPacket packet = GetPacket(); if (channelfactories.ContainsKey(type)) { try { SSH2Channel channel = ((ChannelFactory)channelfactories[type]).CreateChannel(type, requestdata); // Allocate a channel int localid = AllocateChannel(channel); if (localid > -1) { try { channel.Init(this, localid); byte[] responsedata = channel.Create(); packet.WriteByte(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); packet.WriteUINT32(remoteid); packet.WriteUINT32(localid); packet.WriteUINT32(channel.WindowSize); packet.WriteUINT32(channel.PacketSize); if (responsedata != null) { packet.WriteBytes(requestdata); } #if DEBUG System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_CHANNEL_OPEN_CONFIRMATION"); System.Diagnostics.Trace.WriteLine("Channelid=" + localid); System.Diagnostics.Trace.WriteLine("Remoteid=" + remoteid); #endif transport.SendMessage(packet); channel.Open(remoteid, remotewindow, remotepacket); return; } catch (SSHException ex) { #if DEBUG System.Diagnostics.Trace.WriteLine("Exception occured whilst opening channel"); System.Diagnostics.Trace.WriteLine(ex.StackTrace); #endif packet.WriteByte(SSH_MSG_CHANNEL_OPEN_FAILURE); packet.WriteUINT32(remoteid); packet.WriteUINT32(ChannelOpenException.CONNECT_FAILED); packet.WriteString(ex.Message); packet.WriteString(""); } } else { #if DEBUG System.Diagnostics.Trace.WriteLine("Maximum allowable open channel limit of " + MaximumNumChannels + " exceeded!"); #endif packet.WriteByte(SSH_MSG_CHANNEL_OPEN_FAILURE); packet.WriteUINT32(remoteid); packet.WriteUINT32(ChannelOpenException.RESOURCE_SHORTAGE); packet.WriteString("Maximum allowable open channel limit of " + MaximumNumChannels + " exceeded!"); packet.WriteString(""); } } catch (ChannelOpenException ex) { #if DEBUG System.Diagnostics.Trace.WriteLine("Channel open exception occured whilst opening channel"); System.Diagnostics.Trace.WriteLine(ex.StackTrace); #endif packet.WriteByte(SSH_MSG_CHANNEL_OPEN_FAILURE); packet.WriteUINT32(remoteid); packet.WriteUINT32(ex.Reason); packet.WriteString(ex.Message); packet.WriteString(""); } } else { #if DEBUG System.Diagnostics.Trace.WriteLine(type + " is not a supported channel type"); #endif packet.WriteByte(SSH_MSG_CHANNEL_OPEN_FAILURE); packet.WriteUINT32(remoteid); packet.WriteUINT32(ChannelOpenException.UNKNOWN_CHANNEL_TYPE); packet.WriteString(type + " is not a supported channel type!"); packet.WriteString(""); } #if DEBUG System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_CHANNEL_OPEN_FAILURE"); System.Diagnostics.Trace.WriteLine("Remoteid=" + remoteid); System.Diagnostics.Trace.WriteLine("Name=" + type); #endif transport.SendMessage(packet); } catch (System.IO.IOException ex1) { throw new SSHException(ex1.Message, SSHException.INTERNAL_ERROR); } }
/// <summary> /// Close a channel /// </summary> /// <param name="channel"></param> protected internal void CloseChannel(SSH2Channel channel) { FreeChannel(channel); }
/// <summary> /// Opens a channel /// </summary> /// <param name="channel"></param> /// <param name="requestdata"></param> public void OpenChannel(SSH2Channel channel, byte[] requestdata) { lock (this) { try { int channelid = AllocateChannel(channel); if (channelid == -1) { throw new ChannelOpenException("Maximum number of channels exceeded", ChannelOpenException.RESOURCE_SHORTAGE); } channel.Init(this, channelid); SSHPacket packet = GetPacket(); packet.WriteByte(SSH_MSG_CHANNEL_OPEN); packet.WriteString(channel.Name); packet.WriteUINT32(channel.ChannelID); packet.WriteUINT32(channel.WindowSize); packet.WriteUINT32(channel.PacketSize); if (requestdata != null) { packet.WriteBytes(requestdata); } #if DEBUG System.Diagnostics.Trace.WriteLine("Sending SSH_MSG_CHANNEL_OPEN"); System.Diagnostics.Trace.WriteLine("Channelid=" + channel.ChannelID); System.Diagnostics.Trace.WriteLine("Name=" + channel.Name); #endif transport.SendMessage(packet); packet = channel.MessageStore.NextMessage(CHANNEL_OPEN_RESPONSE_MESSAGES); #if DEBUG System.Diagnostics.Trace.WriteLine("Received reply to SSH_MSG_CHANNEL_OPEN"); channel.LogMessage(packet); #endif if (packet.MessageID == SSH_MSG_CHANNEL_OPEN_FAILURE) { FreeChannel(channel); int reason = (int)packet.ReadUINT32(); throw new ChannelOpenException(packet.ReadString(), reason); } else { int remoteid = (int)packet.ReadUINT32(); int remotewindow = (int)packet.ReadUINT32(); int remotepacket = (int)packet.ReadUINT32(); byte[] responsedata = new byte[packet.Available]; packet.ReadBytes(responsedata); channel.Open(remoteid, remotewindow, remotepacket, responsedata); return; } } catch (System.IO.IOException ex) { throw new SSHException(ex.Message, SSHException.INTERNAL_ERROR); } } }
/// <summary> /// Open a channel. /// </summary> /// <param name="channel"></param> /// <param name="requestdata"></param> public void OpenChannel(SSH2Channel channel, byte[] requestdata) { VerifyConnection(true); connection.OpenChannel(channel, requestdata); }