Open lafrank opened 8 years ago
I found RFC6242 describing Netconf over SSH and chunked framing. The problem is that SSH.Net somehow split data - or whatever does splitting - and as a result the first call to OnDataReceived does not contain the full payload, only part of it.
I suggest modifying NetconfSession.OnDataReceived() to capture the full frame payload before parsing it
// code omitted
else if (_usingFramingProtocol)
{
_fullFrame.Append(chunk);
if (Regex.IsMatch(chunk, @"\n##\n"))
{
int position = 0;
string payload = _fullFrame.ToString();
for (;;)
{
var match = Regex.Match(payload.Substring(position), @"\n#(?<length>\d+)\n");
if (!match.Success)
{
break;
}
int fractionLength = Convert.ToInt32(match.Groups["length"].Value);
_rpcReply.Append(payload, position + match.Index + match.Length, fractionLength);
position += match.Index + match.Length + fractionLength;
}
_fullFrame.Clear();
_rpcReplyReceived.Set();
}
}
// code omitted
An in class declaration add _fullFrame as below :
namespace Renci.SshNet.NetConf
{
internal class NetConfSession : SubsystemSession, INetConfSession
{
private const string Prompt = "]]>]]>";
private readonly StringBuilder _data = new StringBuilder();
private bool _usingFramingProtocol;
private EventWaitHandle _serverCapabilitiesConfirmed = new AutoResetEvent(false);
private EventWaitHandle _rpcReplyReceived = new AutoResetEvent(false);
private StringBuilder _rpcReply = new StringBuilder();
private StringBuilder _fullFrame = new StringBuilder();
This modification preserves original functionality but fixes the issue with split payload.
When an rpc-reply message is received, this exception is thrown in NetconfSession.OnDataReceived event handler. The cause is that the received data length is 1015 byte, and the decoded message string contains a magic number at the beginning as below:
1585
<?xml version="1.0"?>