# < Summary

Information

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

Line coverage
80%

Covered lines: | 70
Uncovered lines: | 17
Coverable lines: | 87
Total lines: | 208
Line coverage: | 80.4%

Branch coverage
75%

Covered branches: | 12
Total branches: | 16
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%
Bind(...) | 50% | 2 | 64.7%
OnErrorOccured(...) | 100% | 1 | 0%
ForwardedPort_Closing(...) | 100% | 1 | 100%
ShutdownSocket(...) | 50% | 6 | 68.42%
CloseSocket() | 100% | 4 | 100%
Close() | 100% | 2 | 100%
OnData(...) | 100% | 2 | 100%

# File(s)

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

| # | Line |  | Line coverage
--- | --- | --- | --- | ---
|  | `1` |  | `using System;`
|  | `2` |  | `using System.Net;`
|  | `3` |  | `using System.Net.Sockets;`
|  | `4` |  | `using Renci.SshNet.Abstractions;`
|  | `5` |  | `using Renci.SshNet.Common;`
|  | `6` |  | `using Renci.SshNet.Messages.Connection;`
|  | `7` |  | ``
|  | `8` |  | `namespace Renci.SshNet.Channels`
|  | `9` |  | `{`
|  | `10` |  | `/// <summary>`
|  | `11` |  | `/// Implements "forwarded-tcpip" SSH channel.`
|  | `12` |  | `/// </summary>`
|  | `13` |  | `internal sealed class ChannelForwardedTcpip : ServerChannel, IChannelForwardedTcpip`
|  | `14` |  | `{`
| 17 | `15` |  | `private readonly object _socketShutdownAndCloseLock = new object();`
|  | `16` |  | `private Socket _socket;`
|  | `17` |  | `private IForwardedPort _forwardedPort;`
|  | `18` |  | ``
|  | `19` |  | `/// <summary>`
|  | `20` |  | `/// Initializes a new instance of the <see cref="ChannelForwardedTcpip"/> class.`
|  | `21` |  | `/// </summary>`
|  | `22` |  | `/// <param name="session">The session.</param>`
|  | `23` |  | `/// <param name="localChannelNumber">The local channel number.</param>`
|  | `24` |  | `/// <param name="localWindowSize">Size of the window.</param>`
|  | `25` |  | `/// <param name="localPacketSize">Size of the packet.</param>`
|  | `26` |  | `/// <param name="remoteChannelNumber">The remote channel number.</param>`
|  | `27` |  | `/// <param name="remoteWindowSize">The window size of the remote party.</param>`
|  | `28` |  | `/// <param name="remotePacketSize">The maximum size of a data packet that we can send to the remote party.</para`
|  | `29` |  | `internal ChannelForwardedTcpip(ISession session,`
|  | `30` |  | `uint localChannelNumber,`
|  | `31` |  | `uint localWindowSize,`
|  | `32` |  | `uint localPacketSize,`
|  | `33` |  | `uint remoteChannelNumber,`
|  | `34` |  | `uint remoteWindowSize,`
|  | `35` |  | `uint remotePacketSize)`
| 17 | `36` |  | `: base(session,`
| 17 | `37` |  | `localChannelNumber,`
| 17 | `38` |  | `localWindowSize,`
| 17 | `39` |  | `localPacketSize,`
| 17 | `40` |  | `remoteChannelNumber,`
| 17 | `41` |  | `remoteWindowSize,`
| 17 | `42` |  | `remotePacketSize)`
| 17 | `43` |  | `{`
| 17 | `44` |  | `}`
|  | `45` |  | ``
|  | `46` |  | `/// <summary>`
|  | `47` |  | `/// Gets the type of the channel.`
|  | `48` |  | `/// </summary>`
|  | `49` |  | `/// <value>`
|  | `50` |  | `/// The type of the channel.`
|  | `51` |  | `/// </value>`
|  | `52` |  | `public override ChannelTypes ChannelType`
|  | `53` |  | `{`
| 0 | `54` |  | `get { return ChannelTypes.ForwardedTcpip; }`
|  | `55` |  | `}`
|  | `56` |  | ``
|  | `57` |  | `/// <summary>`
|  | `58` |  | `/// Binds the channel to the specified endpoint.`
|  | `59` |  | `/// </summary>`
|  | `60` |  | `/// <param name="remoteEndpoint">The endpoint to connect to.</param>`
|  | `61` |  | `/// <param name="forwardedPort">The forwarded port for which the channel is opened.</param>`
|  | `62` |  | `public void Bind(IPEndPoint remoteEndpoint, IForwardedPort forwardedPort)`
| 17 | `63` |  | `{`
| 17 | `64` |  | `if (!IsConnected)`
| 0 | `65` |  | `{`
| 0 | `66` |  | `throw new SshException("Session is not connected.");`
|  | `67` |  | `}`
|  | `68` |  | ``
| 17 | `69` |  | `_forwardedPort = forwardedPort;`
| 17 | `70` |  | `_forwardedPort.Closing += ForwardedPort_Closing;`
|  | `71` |  | ``
|  | `72` |  | `// Try to connect to the socket`
|  | `73` |  | `try`
| 17 | `74` |  | `{`
| 17 | `75` |  | `_socket = SocketAbstraction.Connect(remoteEndpoint, ConnectionInfo.Timeout);`
|  | `76` |  | ``
|  | `77` |  | `// Send channel open confirmation message`
| 17 | `78` |  | `SendMessage(new ChannelOpenConfirmationMessage(RemoteChannelNumber, LocalWindowSize, LocalPacketSize, Lo`
| 17 | `79` |  | `}`
| 0 | `80` |  | `catch (Exception exp)`
| 0 | `81` |  | `{`
|  | `82` |  | `// Send channel open failure message`
| 0 | `83` |  | `SendMessage(new ChannelOpenFailureMessage(RemoteChannelNumber, exp.ToString(), ChannelOpenFailureMessage`
|  | `84` |  | ``
| 0 | `85` |  | `throw;`
|  | `86` |  | `}`
|  | `87` |  | ``
| 17 | `88` |  | `var buffer = new byte[RemotePacketSize];`
|  | `89` |  | ``
| 17 | `90` |  | `SocketAbstraction.ReadContinuous(_socket, buffer, 0, buffer.Length, SendData);`
| 17 | `91` |  | `}`
|  | `92` |  | ``
|  | `93` |  | `protected override void OnErrorOccured(Exception exp)`
| 0 | `94` |  | `{`
| 0 | `95` |  | `base.OnErrorOccured(exp);`
|  | `96` |  | ``
|  | `97` |  | `// signal to the server that we will not send anything anymore; this will also interrupt the`
|  | `98` |  | `// blocking receive in Bind if the server sends FIN/ACK in time`
|  | `99` |  | `//`
|  | `100` |  | `// if the FIN/ACK is not sent in time, the socket will be closed in Close(bool)`
| 0 | `101` |  | `ShutdownSocket(SocketShutdown.Send);`
| 0 | `102` |  | `}`
|  | `103` |  | ``
|  | `104` |  | `/// <summary>`
|  | `105` |  | `/// Occurs as the forwarded port is being stopped.`
|  | `106` |  | `/// </summary>`
|  | `107` |  | `private void ForwardedPort_Closing(object sender, EventArgs eventArgs)`
| 2 | `108` |  | `{`
|  | `109` |  | `// signal to the server that we will not send anything anymore; this will also interrupt the`
|  | `110` |  | `// blocking receive in Bind if the server sends FIN/ACK in time`
|  | `111` |  | `//`
|  | `112` |  | `// if the FIN/ACK is not sent in time, the socket will be closed in Close(bool)`
| 2 | `113` |  | `ShutdownSocket(SocketShutdown.Send);`
| 2 | `114` |  | `}`
|  | `115` |  | ``
|  | `116` |  | `/// <summary>`
|  | `117` |  | `/// Shuts down the socket.`
|  | `118` |  | `/// </summary>`
|  | `119` |  | `/// <param name="how">One of the <see cref="SocketShutdown"/> values that specifies the operation that will no l`
|  | `120` |  | `private void ShutdownSocket(SocketShutdown how)`
| 36 | `121` |  | `{`
| 36 | `122` |  | `if (_socket is null)`
| 0 | `123` |  | `{`
| 0 | `124` |  | `return;`
|  | `125` |  | `}`
|  | `126` |  | ``
| 36 | `127` |  | `lock (_socketShutdownAndCloseLock)`
| 36 | `128` |  | `{`
| 36 | `129` |  | `var socket = _socket;`
| 36 | `130` |  | `if (!socket.IsConnected())`
| 19 | `131` |  | `{`
| 19 | `132` |  | `return;`
|  | `133` |  | `}`
|  | `134` |  | ``
|  | `135` |  | `try`
| 17 | `136` |  | `{`
| 17 | `137` |  | `socket.Shutdown(how);`
| 17 | `138` |  | `}`
| 0 | `139` |  | `catch (SocketException ex)`
| 0 | `140` |  | `{`
|  | `141` |  | `// TODO: log as warning`
| 0 | `142` |  | `DiagnosticAbstraction.Log("Failure shutting down socket: " + ex);`
| 0 | `143` |  | `}`
| 17 | `144` |  | `}`
| 36 | `145` |  | `}`
|  | `146` |  | ``
|  | `147` |  | `/// <summary>`
|  | `148` |  | `/// Closes the socket, hereby interrupting the blocking receive in <see cref="Bind(IPEndPoint,IForwardedPort)"/>`
|  | `149` |  | `/// </summary>`
|  | `150` |  | `private void CloseSocket()`
| 34 | `151` |  | `{`
| 34 | `152` |  | `if (_socket is null)`
| 17 | `153` |  | `{`
| 17 | `154` |  | `return;`
|  | `155` |  | `}`
|  | `156` |  | ``
| 17 | `157` |  | `lock (_socketShutdownAndCloseLock)`
| 17 | `158` |  | `{`
| 17 | `159` |  | `var socket = _socket;`
| 17 | `160` |  | `if (socket != null)`
| 17 | `161` |  | `{`
| 17 | `162` |  | `_socket = null;`
| 17 | `163` |  | `socket.Dispose();`
| 17 | `164` |  | `}`
| 17 | `165` |  | `}`
| 34 | `166` |  | `}`
|  | `167` |  | ``
|  | `168` |  | `/// <summary>`
|  | `169` |  | `/// Closes the channel waiting for the SSH_MSG_CHANNEL_CLOSE message to be received from the server.`
|  | `170` |  | `/// </summary>`
|  | `171` |  | `protected override void Close()`
| 34 | `172` |  | `{`
| 34 | `173` |  | `var forwardedPort = _forwardedPort;`
| 34 | `174` |  | `if (forwardedPort != null)`
| 17 | `175` |  | `{`
| 17 | `176` |  | `forwardedPort.Closing -= ForwardedPort_Closing;`
| 17 | `177` |  | `_forwardedPort = null;`
| 17 | `178` |  | `}`
|  | `179` |  | ``
|  | `180` |  | `// signal to the server that we will not send anything anymore; this will also interrupt the`
|  | `181` |  | `// blocking receive in Bind if the server sends FIN/ACK in time`
|  | `182` |  | `//`
|  | `183` |  | `// if the FIN/ACK is not sent in time, the socket will be closed after the channel is closed`
| 34 | `184` |  | `ShutdownSocket(SocketShutdown.Send);`
|  | `185` |  | ``
|  | `186` |  | `// close the SSH channel, and mark the channel closed`
| 34 | `187` |  | `base.Close();`
|  | `188` |  | ``
|  | `189` |  | `// close the socket`
| 34 | `190` |  | `CloseSocket();`
| 34 | `191` |  | `}`
|  | `192` |  | ``
|  | `193` |  | `/// <summary>`
|  | `194` |  | `/// Called when channel data is received.`
|  | `195` |  | `/// </summary>`
|  | `196` |  | `/// <param name="data">The data.</param>`
|  | `197` |  | `protected override void OnData(byte[] data)`
| 2 | `198` |  | `{`
| 2 | `199` |  | `base.OnData(data);`
|  | `200` |  | ``
| 2 | `201` |  | `var socket = _socket;`
| 2 | `202` |  | `if (socket.IsConnected())`
| 2 | `203` |  | `{`
| 2 | `204` |  | `SocketAbstraction.Send(socket, data, 0, data.Length);`
| 2 | `205` |  | `}`
| 2 | `206` |  | `}`
|  | `207` |  | `}`
|  | `208` |  | `}`

# Methods/Properties

.ctor(Renci.SshNet.ISession,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32,System.UInt32)
get_ChannelType()
Bind(System.Net.IPEndPoint,Renci.SshNet.IForwardedPort)
OnErrorOccured(System.Exception)
ForwardedPort_Closing(System.Object,System.EventArgs)
ShutdownSocket(System.Net.Sockets.SocketShutdown)
CloseSocket()
Close()
OnData(System.Byte[])