/// <summary> /// Binds the channel to the specified endpoint. /// </summary> /// <param name="remoteEndpoint">The endpoint to connect to.</param> /// <param name="forwardedPort">The forwarded port for which the channel is opened.</param> public void Bind(IPEndPoint remoteEndpoint, IForwardedPort forwardedPort) { if (!IsConnected) { throw new SshException("Session is not connected."); } _forwardedPort = forwardedPort; _forwardedPort.Closing += ForwardedPort_Closing; // Try to connect to the socket try { _socket = SocketAbstraction.Connect(remoteEndpoint, ConnectionInfo.Timeout); // send channel open confirmation message SendMessage(new ChannelOpenConfirmationMessage(RemoteChannelNumber, LocalWindowSize, LocalPacketSize, LocalChannelNumber)); } catch (Exception exp) { // send channel open failure message SendMessage(new ChannelOpenFailureMessage(RemoteChannelNumber, exp.ToString(), ChannelOpenFailureMessage.ConnectFailed, "en")); throw; } var buffer = new byte[RemotePacketSize]; SocketAbstraction.ReadContinuous(_socket, buffer, 0, buffer.Length, SendData); }
/// <summary> /// Closes the channel, optionally waiting for the SSH_MSG_CHANNEL_CLOSE message to /// be received from the server. /// </summary> /// <param name="wait"><c>true</c> to wait for the SSH_MSG_CHANNEL_CLOSE message to be received from the server; otherwise, <c>false</c>.</param> protected override void Close(bool wait) { #if DEBUG_GERT Console.WriteLine("ID: " + Thread.CurrentThread.ManagedThreadId + " | ChannelForwardedTcpip.Close"); #endif // DEBUG_GERT var forwardedPort = _forwardedPort; if (forwardedPort != null) { forwardedPort.Closing -= ForwardedPort_Closing; _forwardedPort = null; } // signal to the server that we will not send anything anymore; this will also interrupt the // blocking receive in Bind if the server sends FIN/ACK in time // // if the FIN/ACK is not sent in time, the socket will be closed after the channel is closed ShutdownSocket(SocketShutdown.Send); // close the SSH channel, and mark the channel closed base.Close(wait); // close the socket CloseSocket(); }
public void Open(string remoteHost, uint port, IForwardedPort forwardedPort, Socket socket) { if (IsOpen) throw new SshException("Channel is already open."); if (!IsConnected) throw new SshException("Session is not connected."); _socket = socket; _forwardedPort = forwardedPort; _forwardedPort.Closing += ForwardedPort_Closing; var ep = (IPEndPoint) socket.RemoteEndPoint; #if DEBUG_GERT Console.WriteLine("ID: " + Thread.CurrentThread.ManagedThreadId + " | ChannelOpenMessage send '" + LocalChannelNumber + "' | " + DateTime.Now.ToString("hh:mm:ss.fff")); #endif // DEBUG_GERT // open channel SendMessage(new ChannelOpenMessage(LocalChannelNumber, LocalWindowSize, LocalPacketSize, new DirectTcpipChannelInfo(remoteHost, port, ep.Address.ToString(), (uint) ep.Port))); #if DEBUG_GERT Console.WriteLine("ID: " + Thread.CurrentThread.ManagedThreadId + " | ChannelOpenMessage sent '" + LocalChannelNumber + "' | " + DateTime.Now.ToString("hh:mm:ss.fff")); #endif // DEBUG_GERT // Wait for channel to open WaitOnHandle(_channelOpen); #if DEBUG_GERT Console.WriteLine("ID: " + Thread.CurrentThread.ManagedThreadId + " | ChannelOpenMessage flagged '" + LocalChannelNumber + "' | " + DateTime.Now.ToString("hh:mm:ss.fff")); #endif // DEBUG_GERT }
public void Open(string remoteHost, uint port, IForwardedPort forwardedPort, Socket socket) { if (IsOpen) throw new SshException("Channel is already open."); if (!IsConnected) throw new SshException("Session is not connected."); _socket = socket; _forwardedPort = forwardedPort; _forwardedPort.Closing += ForwardedPort_Closing; var ep = (IPEndPoint) socket.RemoteEndPoint; // open channel SendMessage(new ChannelOpenMessage(LocalChannelNumber, LocalWindowSize, LocalPacketSize, new DirectTcpipChannelInfo(remoteHost, port, ep.Address.ToString(), (uint) ep.Port))); // Wait for channel to open WaitOnHandle(_channelOpen); }
/// <summary> /// Closes the channel, optionally waiting for the SSH_MSG_CHANNEL_CLOSE message to /// be received from the server. /// </summary> /// <param name="wait"><c>true</c> to wait for the SSH_MSG_CHANNEL_CLOSE message to be received from the server; otherwise, <c>false</c>.</param> protected override void Close(bool wait) { if (_forwardedPort != null) { _forwardedPort.Closing -= ForwardedPort_Closing; _forwardedPort = null; } // signal to the server that we will not send anything anymore; this will also interrupt the // blocking receive in Bind if the server sends FIN/ACK in time // // if the FIN/ACK is not sent in time, the socket will be closed after the channel is closed ShutdownSocket(SocketShutdown.Send); // close the SSH channel, and mark the channel closed base.Close(wait); // close the socket CloseSocket(); }
public void Open(string remoteHost, uint port, IForwardedPort forwardedPort, Socket socket) { if (IsOpen) { throw new SshException("Channel is already open."); } if (!IsConnected) { throw new SshException("Session is not connected."); } _socket = socket; _forwardedPort = forwardedPort; _forwardedPort.Closing += ForwardedPort_Closing; var ep = (IPEndPoint)socket.RemoteEndPoint; // open channel SendMessage(new ChannelOpenMessage(LocalChannelNumber, LocalWindowSize, LocalPacketSize, new DirectTcpipChannelInfo(remoteHost, port, ep.Address.ToString(), (uint)ep.Port))); // Wait for channel to open WaitOnHandle(_channelOpen); }
public void Open(string remoteHost, uint port, IForwardedPort forwardedPort, Socket socket) { if (IsOpen) { throw new SshException("Channel is already open."); } if (!IsConnected) { throw new SshException("Session is not connected."); } _socket = socket; _forwardedPort = forwardedPort; _forwardedPort.Closing += ForwardedPort_Closing; var ep = (IPEndPoint)socket.RemoteEndPoint; #if DEBUG_GERT Console.WriteLine("ID: " + Thread.CurrentThread.ManagedThreadId + " | ChannelOpenMessage send '" + LocalChannelNumber + "' | " + DateTime.Now.ToString("hh:mm:ss.fff")); #endif // DEBUG_GERT // open channel SendMessage(new ChannelOpenMessage(LocalChannelNumber, LocalWindowSize, LocalPacketSize, new DirectTcpipChannelInfo(remoteHost, port, ep.Address.ToString(), (uint)ep.Port))); #if DEBUG_GERT Console.WriteLine("ID: " + Thread.CurrentThread.ManagedThreadId + " | ChannelOpenMessage sent '" + LocalChannelNumber + "' | " + DateTime.Now.ToString("hh:mm:ss.fff")); #endif // DEBUG_GERT // Wait for channel to open WaitOnHandle(_channelOpen); #if DEBUG_GERT Console.WriteLine("ID: " + Thread.CurrentThread.ManagedThreadId + " | ChannelOpenMessage flagged '" + LocalChannelNumber + "' | " + DateTime.Now.ToString("hh:mm:ss.fff")); #endif // DEBUG_GERT }
/// <summary> /// Binds the channel to the specified endpoint. /// </summary> /// <param name="remoteEndpoint">The endpoint to connect to.</param> /// <param name="forwardedPort">The forwarded port for which the channel is opened.</param> public void Bind(IPEndPoint remoteEndpoint, IForwardedPort forwardedPort) { byte[] buffer; if (!IsConnected) { throw new SshException("Session is not connected."); } _forwardedPort = forwardedPort; _forwardedPort.Closing += ForwardedPort_Closing; // Try to connect to the socket try { // Get buffer in memory for data exchange buffer = new byte[RemotePacketSize]; OpenSocket(remoteEndpoint); // send channel open confirmation message SendMessage(new ChannelOpenConfirmationMessage(RemoteChannelNumber, LocalWindowSize, LocalPacketSize, LocalChannelNumber)); } catch (Exception exp) { // send channel open failure message SendMessage(new ChannelOpenFailureMessage(RemoteChannelNumber, exp.ToString(), ChannelOpenFailureMessage.ConnectFailed, "en")); throw; } // Start reading data from the port and send to channel while (_socket != null && _socket.Connected) { try { var read = 0; InternalSocketReceive(buffer, ref read); if (read > 0) { SendMessage(new ChannelDataMessage(RemoteChannelNumber, buffer.Take(read).ToArray())); } else { // server quit sending break; } } catch (SocketException exp) { if (exp.SocketErrorCode == SocketError.WouldBlock || exp.SocketErrorCode == SocketError.IOPending || exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable) { // socket buffer is probably empty, wait and try again Thread.Sleep(30); } else if (exp.SocketErrorCode == SocketError.ConnectionAborted || exp.SocketErrorCode == SocketError.Interrupted) { break; } else { throw; // throw any other error } } } }
/// <summary> /// Closes the channel, optionally waiting for the SSH_MSG_CHANNEL_CLOSE message to /// be received from the server. /// </summary> /// <param name="wait"><c>true</c> to wait for the SSH_MSG_CHANNEL_CLOSE message to be received from the server; otherwise, <c>false</c>.</param> protected override void Close(bool wait) { if (_forwardedPort != null) { _forwardedPort.Closing -= ForwardedPort_Closing; _forwardedPort = null; } // signal to the client that we will not send anything anymore; this will also interrupt the // blocking receive in Bind if the client sends FIN/ACK in time // // if the FIN/ACK is not sent in time, the socket will be closed after the channel is closed ShutdownSocket(SocketShutdown.Send); // close the SSH channel, and mark the channel closed base.Close(wait); // close the socket CloseSocket(); }
/// <summary> /// Binds the channel to the specified endpoint. /// </summary> /// <param name="remoteEndpoint">The endpoint to connect to.</param> /// <param name="forwardedPort">The forwarded port for which the channel is opened.</param> public void Bind(IPEndPoint remoteEndpoint, IForwardedPort forwardedPort) { byte[] buffer; if (!IsConnected) { throw new SshException("Session is not connected."); } _forwardedPort = forwardedPort; _forwardedPort.Closing += ForwardedPort_Closing; // Try to connect to the socket try { // Get buffer in memory for data exchange buffer = new byte[RemotePacketSize]; OpenSocket(remoteEndpoint); // send channel open confirmation message SendMessage(new ChannelOpenConfirmationMessage(RemoteChannelNumber, LocalWindowSize, LocalPacketSize, LocalChannelNumber)); } catch (Exception exp) { // send channel open failure message SendMessage(new ChannelOpenFailureMessage(RemoteChannelNumber, exp.ToString(), ChannelOpenFailureMessage.ConnectFailed, "en")); throw; } // Start reading data from the port and send to channel while (_socket != null && _socket.Connected) { try { var read = 0; InternalSocketReceive(buffer, ref read); if (read > 0) { SendMessage(new ChannelDataMessage(RemoteChannelNumber, buffer.Take(read).ToArray())); } else { // server quit sending break; } } catch (SocketException exp) { if (exp.SocketErrorCode == SocketError.WouldBlock || exp.SocketErrorCode == SocketError.IOPending || exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable) { // socket buffer is probably empty, wait and try again Thread.Sleep(30); } else if (exp.SocketErrorCode == SocketError.ConnectionAborted || exp.SocketErrorCode == SocketError.Interrupted) { break; } else throw; // throw any other error } } }