# < Summary

Information

Class: | Renci.SshNet.Channels.ChannelDirectTcpip
Assembly: | Renci.SshNet
File(s): | \home\appveyor\projects\ssh-net\src\Renci.SshNet\Channels\ChannelDirectTcpip.cs

Line coverage
78%

Covered lines: | 110
Uncovered lines: | 30
Coverable lines: | 140
Total lines: | 317
Line coverage: | 78.5%

Branch coverage
75%

Covered branches: | 24
Total branches: | 32
Branch coverage: | 75%

Method coverage

Feature is only available for sponsors

Upgrade to PRO version

# Metrics

Method | Branch coverage | Cyclomatic complexity | Line coverage
--- | --- | --- | ---
.ctor(...) | 100% | 1 | 100%
get_ChannelType() | 100% | 1 | 0%
Open(...) | 50% | 4 | 76.47%
ForwardedPort_Closing(...) | 100% | 1 | 100%
Bind() | 50% | 2 | 71.42%
CloseSocket() | 100% | 4 | 100%
ShutdownSocket(...) | 66.66% | 6 | 77.77%
Close() | 100% | 2 | 100%
OnData(...) | 100% | 4 | 100%
OnOpenConfirmation(...) | 100% | 1 | 100%
OnOpenFailure(...) | 100% | 1 | 0%
OnEof() | 100% | 1 | 100%
OnErrorOccured(...) | 100% | 1 | 100%
OnDisconnected() | 100% | 1 | 0%
Dispose(...) | 70% | 10 | 63.33%

# File(s)

## \home\appveyor\projects\ssh-net\src\Renci.SshNet\Channels\ChannelDirectTcpip.cs

| # | Line |  | Line coverage
--- | --- | --- | --- | ---
|  | `1` |  | `using System;`
|  | `2` |  | `using System.Net;`
|  | `3` |  | `using System.Net.Sockets;`
|  | `4` |  | `using System.Threading;`
|  | `5` |  | `using Renci.SshNet.Abstractions;`
|  | `6` |  | `using Renci.SshNet.Common;`
|  | `7` |  | `using Renci.SshNet.Messages.Connection;`
|  | `8` |  | ``
|  | `9` |  | `namespace Renci.SshNet.Channels`
|  | `10` |  | `{`
|  | `11` |  | `/// <summary>`
|  | `12` |  | `/// Implements "direct-tcpip" SSH channel.`
|  | `13` |  | `/// </summary>`
|  | `14` |  | `internal sealed class ChannelDirectTcpip : ClientChannel, IChannelDirectTcpip`
|  | `15` |  | `{`
| 31 | `16` |  | `private readonly object _socketLock = new object();`
|  | `17` |  | ``
| 31 | `18` |  | `private EventWaitHandle _channelOpen = new AutoResetEvent(initialState: false);`
| 31 | `19` |  | `private EventWaitHandle _channelData = new AutoResetEvent(initialState: false);`
|  | `20` |  | `private IForwardedPort _forwardedPort;`
|  | `21` |  | `private Socket _socket;`
|  | `22` |  | ``
|  | `23` |  | `/// <summary>`
|  | `24` |  | `/// Initializes a new instance of the <see cref="ChannelDirectTcpip"/> class.`
|  | `25` |  | `/// </summary>`
|  | `26` |  | `/// <param name="session">The session.</param>`
|  | `27` |  | `/// <param name="localChannelNumber">The local channel number.</param>`
|  | `28` |  | `/// <param name="localWindowSize">Size of the window.</param>`
|  | `29` |  | `/// <param name="localPacketSize">Size of the packet.</param>`
|  | `30` |  | `public ChannelDirectTcpip(ISession session, uint localChannelNumber, uint localWindowSize, uint localPacketSize)`
| 31 | `31` |  | `: base(session, localChannelNumber, localWindowSize, localPacketSize)`
| 31 | `32` |  | `{`
| 31 | `33` |  | `}`
|  | `34` |  | ``
|  | `35` |  | `/// <summary>`
|  | `36` |  | `/// Gets the type of the channel.`
|  | `37` |  | `/// </summary>`
|  | `38` |  | `/// <value>`
|  | `39` |  | `/// The type of the channel.`
|  | `40` |  | `/// </value>`
|  | `41` |  | `public override ChannelTypes ChannelType`
|  | `42` |  | `{`
| 0 | `43` |  | `get { return ChannelTypes.DirectTcpip; }`
|  | `44` |  | `}`
|  | `45` |  | ``
|  | `46` |  | `public void Open(string remoteHost, uint port, IForwardedPort forwardedPort, Socket socket)`
| 31 | `47` |  | `{`
| 31 | `48` |  | `if (IsOpen)`
| 0 | `49` |  | `{`
| 0 | `50` |  | `throw new SshException("Channel is already open.");`
|  | `51` |  | `}`
|  | `52` |  | ``
| 31 | `53` |  | `if (!IsConnected)`
| 0 | `54` |  | `{`
| 0 | `55` |  | `throw new SshException("Session is not connected.");`
|  | `56` |  | `}`
|  | `57` |  | ``
| 31 | `58` |  | `_socket = socket;`
| 31 | `59` |  | `_forwardedPort = forwardedPort;`
| 31 | `60` |  | `_forwardedPort.Closing += ForwardedPort_Closing;`
|  | `61` |  | ``
| 31 | `62` |  | `var ep = (IPEndPoint) socket.RemoteEndPoint;`
|  | `63` |  | ``
|  | `64` |  | `// Open channel`
| 31 | `65` |  | `SendMessage(new ChannelOpenMessage(LocalChannelNumber,`
| 31 | `66` |  | `LocalWindowSize,`
| 31 | `67` |  | `LocalPacketSize,`
| 31 | `68` |  | `new DirectTcpipChannelInfo(remoteHost, port, ep.Address.ToString(), (uint`
|  | `69` |  | ``
|  | `70` |  | `// Wait for channel to open`
| 31 | `71` |  | `WaitOnHandle(_channelOpen);`
| 31 | `72` |  | `}`
|  | `73` |  | ``
|  | `74` |  | `/// <summary>`
|  | `75` |  | `/// Occurs as the forwarded port is being stopped.`
|  | `76` |  | `/// </summary>`
|  | `77` |  | `private void ForwardedPort_Closing(object sender, EventArgs eventArgs)`
| 10 | `78` |  | `{`
|  | `79` |  | `// signal to the client that we will not send anything anymore; this should also interrupt the`
|  | `80` |  | `// blocking receive in Bind if the client sends FIN/ACK in time`
| 10 | `81` |  | `ShutdownSocket(SocketShutdown.Send);`
|  | `82` |  | ``
|  | `83` |  | `// if the FIN/ACK is not sent in time by the remote client, then interrupt the blocking receive`
|  | `84` |  | `// by closing the socket`
| 10 | `85` |  | `CloseSocket();`
| 10 | `86` |  | `}`
|  | `87` |  | ``
|  | `88` |  | `/// <summary>`
|  | `89` |  | `/// Binds channel to remote host.`
|  | `90` |  | `/// </summary>`
|  | `91` |  | `public void Bind()`
| 31 | `92` |  | `{`
|  | `93` |  | `// Cannot bind if channel is not open`
| 31 | `94` |  | `if (!IsOpen)`
| 0 | `95` |  | `{`
| 0 | `96` |  | `return;`
|  | `97` |  | `}`
|  | `98` |  | ``
| 31 | `99` |  | `var buffer = new byte[RemotePacketSize];`
|  | `100` |  | ``
| 31 | `101` |  | `SocketAbstraction.ReadContinuous(_socket, buffer, 0, buffer.Length, SendData);`
|  | `102` |  | ``
|  | `103` |  | `// even though the client has disconnected, we still want to properly close the`
|  | `104` |  | `// channel`
|  | `105` |  | `//`
|  | `106` |  | `// we'll do this in in Close() - invoked through Dispose(bool) - that way we have`
|  | `107` |  | `// a single place from which we send an SSH_MSG_CHANNEL_EOF message and wait for`
|  | `108` |  | `// the SSH_MSG_CHANNEL_CLOSE message`
| 31 | `109` |  | `}`
|  | `110` |  | ``
|  | `111` |  | `/// <summary>`
|  | `112` |  | `/// Closes the socket, hereby interrupting the blocking receive in <see cref="Bind()"/>.`
|  | `113` |  | `/// </summary>`
|  | `114` |  | `private void CloseSocket()`
| 60 | `115` |  | `{`
| 60 | `116` |  | `if (_socket is null)`
| 29 | `117` |  | `{`
| 29 | `118` |  | `return;`
|  | `119` |  | `}`
|  | `120` |  | ``
| 31 | `121` |  | `lock (_socketLock)`
| 31 | `122` |  | `{`
| 31 | `123` |  | `if (_socket is null)`
| 3 | `124` |  | `{`
| 3 | `125` |  | `return;`
|  | `126` |  | `}`
|  | `127` |  | ``
|  | `128` |  | `// closing a socket actually disposes the socket, so we can safely dereference`
|  | `129` |  | `// the field to avoid entering the lock again later`
| 28 | `130` |  | `_socket.Dispose();`
| 28 | `131` |  | `_socket = null;`
| 28 | `132` |  | `}`
| 60 | `133` |  | `}`
|  | `134` |  | ``
|  | `135` |  | `/// <summary>`
|  | `136` |  | `/// Shuts down the socket.`
|  | `137` |  | `/// </summary>`
|  | `138` |  | `/// <param name="how">One of the <see cref="SocketShutdown"/> values that specifies the operation that will no l`
|  | `139` |  | `private void ShutdownSocket(SocketShutdown how)`
| 66 | `140` |  | `{`
| 66 | `141` |  | `if (_socket is null)`
| 12 | `142` |  | `{`
| 12 | `143` |  | `return;`
|  | `144` |  | `}`
|  | `145` |  | ``
| 54 | `146` |  | `lock (_socketLock)`
| 54 | `147` |  | `{`
| 54 | `148` |  | `if (!_socket.IsConnected())`
| 23 | `149` |  | `{`
| 23 | `150` |  | `return;`
|  | `151` |  | `}`
|  | `152` |  | ``
|  | `153` |  | `try`
| 31 | `154` |  | `{`
| 31 | `155` |  | `_socket.Shutdown(how);`
| 31 | `156` |  | `}`
| 0 | `157` |  | `catch (SocketException ex)`
| 0 | `158` |  | `{`
|  | `159` |  | `// TODO: log as warning`
| 0 | `160` |  | `DiagnosticAbstraction.Log("Failure shutting down socket: " + ex);`
| 0 | `161` |  | `}`
| 31 | `162` |  | `}`
| 66 | `163` |  | `}`
|  | `164` |  | ``
|  | `165` |  | `/// <summary>`
|  | `166` |  | `/// Closes the channel, waiting for the SSH_MSG_CHANNEL_CLOSE message to be received from the server.`
|  | `167` |  | `/// </summary>`
|  | `168` |  | `protected override void Close()`
| 50 | `169` |  | `{`
| 50 | `170` |  | `var forwardedPort = _forwardedPort;`
| 50 | `171` |  | `if (forwardedPort != null)`
| 25 | `172` |  | `{`
| 25 | `173` |  | `forwardedPort.Closing -= ForwardedPort_Closing;`
| 25 | `174` |  | `_forwardedPort = null;`
| 25 | `175` |  | `}`
|  | `176` |  | ``
|  | `177` |  | `// signal to the client that we will not send anything anymore; this will also interrupt the`
|  | `178` |  | `// blocking receive in Bind if the client sends FIN/ACK in time`
|  | `179` |  | `//`
|  | `180` |  | `// if the FIN/ACK is not sent in time, the socket will be closed after the channel is closed`
| 50 | `181` |  | `ShutdownSocket(SocketShutdown.Send);`
|  | `182` |  | ``
|  | `183` |  | `// close the SSH channel`
| 50 | `184` |  | `base.Close();`
|  | `185` |  | ``
|  | `186` |  | `// close the socket`
| 50 | `187` |  | `CloseSocket();`
| 50 | `188` |  | `}`
|  | `189` |  | ``
|  | `190` |  | `/// <summary>`
|  | `191` |  | `/// Called when channel data is received.`
|  | `192` |  | `/// </summary>`
|  | `193` |  | `/// <param name="data">The data.</param>`
|  | `194` |  | `protected override void OnData(byte[] data)`
| 8 | `195` |  | `{`
| 8 | `196` |  | `base.OnData(data);`
|  | `197` |  | ``
| 8 | `198` |  | `if (_socket != null)`
| 8 | `199` |  | `{`
| 8 | `200` |  | `lock (_socketLock)`
| 8 | `201` |  | `{`
| 8 | `202` |  | `if (_socket.IsConnected())`
| 8 | `203` |  | `{`
| 8 | `204` |  | `SocketAbstraction.Send(_socket, data, 0, data.Length);`
| 8 | `205` |  | `}`
| 8 | `206` |  | `}`
| 8 | `207` |  | `}`
| 8 | `208` |  | `}`
|  | `209` |  | ``
|  | `210` |  | `/// <summary>`
|  | `211` |  | `/// Called when channel is opened by the server.`
|  | `212` |  | `/// </summary>`
|  | `213` |  | `/// <param name="remoteChannelNumber">The remote channel number.</param>`
|  | `214` |  | `/// <param name="initialWindowSize">Initial size of the window.</param>`
|  | `215` |  | `/// <param name="maximumPacketSize">Maximum size of the packet.</param>`
|  | `216` |  | `protected override void OnOpenConfirmation(uint remoteChannelNumber, uint initialWindowSize, uint maximumPacketS`
| 31 | `217` |  | `{`
| 31 | `218` |  | `base.OnOpenConfirmation(remoteChannelNumber, initialWindowSize, maximumPacketSize);`
|  | `219` |  | ``
| 31 | `220` |  | `_ = _channelOpen.Set();`
| 31 | `221` |  | `}`
|  | `222` |  | ``
|  | `223` |  | `protected override void OnOpenFailure(uint reasonCode, string description, string language)`
| 0 | `224` |  | `{`
| 0 | `225` |  | `base.OnOpenFailure(reasonCode, description, language);`
|  | `226` |  | ``
| 0 | `227` |  | `_ = _channelOpen.Set();`
| 0 | `228` |  | `}`
|  | `229` |  | ``
|  | `230` |  | `/// <summary>`
|  | `231` |  | `/// Called when channel has no more data to receive.`
|  | `232` |  | `/// </summary>`
|  | `233` |  | `protected override void OnEof()`
| 3 | `234` |  | `{`
| 3 | `235` |  | `base.OnEof();`
|  | `236` |  | ``
|  | `237` |  | `// the channel will send no more data, and hence it does not make sense to receive`
|  | `238` |  | `// any more data from the client to send to the remote party (and we surely won't`
|  | `239` |  | `// send anything anymore)`
|  | `240` |  | `//`
|  | `241` |  | `// this will also interrupt the blocking receive in Bind()`
| 3 | `242` |  | `ShutdownSocket(SocketShutdown.Send);`
| 3 | `243` |  | `}`
|  | `244` |  | ``
|  | `245` |  | `/// <summary>`
|  | `246` |  | `/// Called whenever an unhandled <see cref="Exception"/> occurs in <see cref="Session"/> causing`
|  | `247` |  | `/// the message loop to be interrupted, or when an exception occurred processing a channel message.`
|  | `248` |  | `/// </summary>`
|  | `249` |  | `protected override void OnErrorOccured(Exception exp)`
| 3 | `250` |  | `{`
| 3 | `251` |  | `base.OnErrorOccured(exp);`
|  | `252` |  | ``
|  | `253` |  | `// signal to the client that we will not send anything anymore; this will also interrupt the`
|  | `254` |  | `// blocking receive in Bind if the client sends FIN/ACK in time`
|  | `255` |  | `//`
|  | `256` |  | `// if the FIN/ACK is not sent in time, the socket will be closed in Close(bool)`
| 3 | `257` |  | `ShutdownSocket(SocketShutdown.Send);`
| 3 | `258` |  | `}`
|  | `259` |  | ``
|  | `260` |  | `/// <summary>`
|  | `261` |  | `/// Called when the server wants to terminate the connection immmediately.`
|  | `262` |  | `/// </summary>`
|  | `263` |  | `/// <remarks>`
|  | `264` |  | `/// The sender MUST NOT send or receive any data after this message, and`
|  | `265` |  | `/// the recipient MUST NOT accept any data after receiving this message.`
|  | `266` |  | `/// </remarks>`
|  | `267` |  | `protected override void OnDisconnected()`
| 0 | `268` |  | `{`
| 0 | `269` |  | `base.OnDisconnected();`
|  | `270` |  | ``
|  | `271` |  | `// the channel will accept or send no more data, and hence it does not make sense`
|  | `272` |  | `// to accept any more data from the client (and we surely won't send anything`
|  | `273` |  | `// anymore)`
|  | `274` |  | `//`
|  | `275` |  | `// so lets signal to the client that we will not send or receive anything anymore`
|  | `276` |  | `// this will also interrupt the blocking receive in Bind()`
| 0 | `277` |  | `ShutdownSocket(SocketShutdown.Both);`
| 0 | `278` |  | `}`
|  | `279` |  | ``
|  | `280` |  | `protected override void Dispose(bool disposing)`
| 34 | `281` |  | `{`
|  | `282` |  | `// make sure we've unsubscribed from all session events and closed the channel`
|  | `283` |  | `// before we starting disposing`
| 34 | `284` |  | `base.Dispose(disposing);`
|  | `285` |  | ``
| 34 | `286` |  | `if (disposing)`
| 28 | `287` |  | `{`
| 28 | `288` |  | `if (_socket != null)`
| 0 | `289` |  | `{`
| 0 | `290` |  | `lock (_socketLock)`
| 0 | `291` |  | `{`
| 0 | `292` |  | `var socket = _socket;`
| 0 | `293` |  | `if (socket != null)`
| 0 | `294` |  | `{`
| 0 | `295` |  | `_socket = null;`
| 0 | `296` |  | `socket.Dispose();`
| 0 | `297` |  | `}`
| 0 | `298` |  | `}`
| 0 | `299` |  | `}`
|  | `300` |  | ``
| 28 | `301` |  | `var channelOpen = _channelOpen;`
| 28 | `302` |  | `if (channelOpen != null)`
| 25 | `303` |  | `{`
| 25 | `304` |  | `_channelOpen = null;`
| 25 | `305` |  | `channelOpen.Dispose();`
| 25 | `306` |  | `}`
|  | `307` |  | ``
| 28 | `308` |  | `var channelData = _channelData;`
| 28 | `309` |  | `if (channelData != null)`
| 25 | `310` |  | `{`
| 25 | `311` |  | `_channelData = null;`
| 25 | `312` |  | `channelData.Dispose();`
| 25 | `313` |  | `}`
| 28 | `314` |  | `}`
| 34 | `315` |  | `}`
|  | `316` |  | `}`
|  | `317` |  | `}`

# Methods/Properties

.ctor(Renci.SshNet.ISession,System.UInt32,System.UInt32,System.UInt32)
get_ChannelType()
Open(System.String,System.UInt32,Renci.SshNet.IForwardedPort,System.Net.Sockets.Socket)
ForwardedPort_Closing(System.Object,System.EventArgs)
Bind()
CloseSocket()
ShutdownSocket(System.Net.Sockets.SocketShutdown)
Close()
OnData(System.Byte[])
OnOpenConfirmation(System.UInt32,System.UInt32,System.UInt32)
OnOpenFailure(System.UInt32,System.String,System.String)
OnEof()
OnErrorOccured(System.Exception)
OnDisconnected()
Dispose(System.Boolean)