SSLStream读取无效数据+KB3147458 SSLStream错误(?)
本文关键字:SSLStream 错误 +KB3147458 读取 无效 数据 | 更新日期: 2024-09-20 06:54:56
当远程客户端没有发送任何内容时,SSLStream返回一些数据时,我遇到了问题。当服务器正在侦听新命令时,我遇到了这个问题。如果服务器没有接收到新的请求,ReadMessage()函数应该会由于SSLStream的读取超时而捕获IOException。当sslStream.Read()第二次执行时,似乎读取了客户端未发送的5个字节,就会出现问题。所以问题发生在这个序列中:
->ReadMessage()->sslstream。Read()->按预期捕获超时异常
->ReadMessage()->sslstream。Read()->未捕获超时异常,即使客户端没有发送任何,仍读取5字节
->ReadMessage()->sslstream。Read()->按预期捕获超时异常
->ReadMessage()->sslstream。Read()->未捕获超时异常,即使客户端未发送任何内容,仍读取5字节。。。
等等。
public void ClientHandle(object obj)
{
nRetry = MAX_RETRIES;
// Open connection with the client
if (Open() == OPEN_SUCCESS)
{
String request = ReadMessage();
String response = null;
// while loop for the incoming commands from client
while (!String.IsNullOrEmpty(request))
{
Console.WriteLine("[{0}] {1}", RemoteIPAddress, request);
response = Execute(request);
// If QUIT was received, close the connection with the client
if (response.Equals(QUIT_RESPONSE))
{
// Closing connection
Console.WriteLine("[{0}] {1}", RemoteIPAddress, response);
// Send QUIT_RESPONSE then return and close this thread
SendMessage(response);
break;
}
// If another command was received, send the response to the client
if (!response.StartsWith("TIMEOUT"))
{
// Reset nRetry
nRetry = MAX_RETRIES;
if (!SendMessage(response))
{
// Couldn't send message
Close();
break;
}
}
// Wait for new input request from client
request = ReadMessage();
// If nothing was received, SslStream timeout occurred
if (String.IsNullOrEmpty(request))
{
request = "TIMEOUT";
nRetry--;
if (nRetry == 0)
{
// Close everything
Console.WriteLine("Client is unreachable. Closing client connection.");
Close();
break;
}
else
{
continue;
}
}
}
Console.WriteLine("Stopped");
}
}
public String ReadMessage()
{
if (tcpClient != null)
{
int bytes = -1;
byte[] buffer = new byte[MESSAGE_SIZE];
try
{
bytes = sslStream.Read(buffer, 0, MESSAGE_SIZE);
}
catch (ObjectDisposedException)
{
// Streams were disposed
return String.Empty;
}
catch (IOException)
{
return String.Empty;
}
catch (Exception)
{
// Some other exception occured
return String.Empty;
}
if (bytes != MESSAGE_SIZE)
{
return String.Empty;
}
// Return string read from the stream
return Encoding.Unicode.GetString(buffer, 0, MESSAGE_SIZE).Replace("'0", String.Empty);
}
return String.Empty;
}
public bool SendMessage(String message)
{
if (tcpClient != null)
{
byte[] data = CreateMessage(message);
try
{
// Write command message to the stream and send it
sslStream.Write(data, 0, MESSAGE_SIZE);
sslStream.Flush();
}
catch (ObjectDisposedException)
{
// Streamers were disposed
return false;
}
catch (IOException)
{
// Error while trying to access streams or connection timedout
return false;
}
catch (Exception)
{
return false;
}
// Data sent successfully
return true;
}
return false;
}
private byte[] CreateMessage(String message)
{
byte[] data = new byte[MESSAGE_SIZE];
byte[] messageBytes = Encoding.Unicode.GetBytes(message);
// Can't exceed MESSAGE_SIZE parameter (max message size in bytes)
if (messageBytes.Length >= MESSAGE_SIZE)
{
throw new ArgumentOutOfRangeException("message", String.Format("Message string can't be longer than {0} bytes", MESSAGE_SIZE));
}
for (int i = 0; i < messageBytes.Length; i++)
{
data[i] = messageBytes[i];
}
for (int i = messageBytes.Length; i < MESSAGE_SIZE; i++)
{
data[i] = messageBytes[messageBytes.Length - 1];
}
return data;
}
客户端也使用相同的ReadMessage()、SendMessage()和CreateMessage()函数向服务器发送消息。MESSAGE_SIZE常量也是相同的,它被设置为2048。
问题是我在超时后重新使用了SSLStream。因此,我只需删除nRetry变量并设置更长的超时时间就解决了这个问题。MSDN的相关文章说SSLStream在超时异常后将返回垃圾(https://msdn.microsoft.com/en-us/library/system.net.security.sslstream(v=vs.110).aspx):
SslStream假设,当从内部流抛出一个IOException时,超时和任何其他IOException将被其调用方视为致命。超时后重新使用SslStream实例将返回垃圾。在这种情况下,应用程序应该关闭SslStream并抛出异常。
另一个问题是Windows更新KB3147458(Windows 4月10日的更新)更改了读取功能的行为。看起来SSLStream实现中的某些内容发生了更改,现在它每次返回两部分数据,一个字节和其余字节。事实上,MSDN文档并没有说Read()函数将在一步中返回所有请求的字节,并且所提供的示例使用do-while循环来读取确切的字节数。因此,我认为Read()函数不能保证一次读取所请求的确切字节数,可能需要更多的读取迭代。
SSLstream工作正常,因此不会断开。您只需要注意并使用do-while循环,并检查是否正确读取了所有字节。
我更改了这里显示的代码,以解决我遇到的错误。
public String ReadMessage()
{
if (tcpClient != null)
{
int bytes = -1, offset = 0;
byte[] buffer = new byte[MESSAGE_SIZE];
try
{
// perform multiple read iterations
// and check the number of bytes received
while (offset < MESSAGE_SIZE)
{
bytes = sslStream.Read(buffer, offset, MESSAGE_SIZE - offset);
offset += bytes;
if (bytes == 0)
{
return String.Empty;
}
}
}
catch (Exception)
{
// Some exception occured
return String.Empty;
}
if (offset != MESSAGE_SIZE)
{
return String.Empty;
}
// Return string read from the stream
return Encoding.Unicode.GetString(buffer, 0, MESSAGE_SIZE).Replace("'0", String.Empty);
}
return String.Empty;
}
关于SslStream在超时后在Read()上返回五个字节,这是因为SslStream类无法正常处理来自底层流的任何IOException,这一点如前所述。
SslStream假设,当从内部流抛出一个IOException时,超时和任何其他IOException将被其调用方视为致命。超时后重新使用SslStream实例将返回垃圾。在这种情况下,应用程序应该关闭SslStream并抛出异常。
https://msdn.microsoft.com/en-us/library/system.net.security.sslstream(v=vs.110).aspx
但是,您可以通过创建一个位于Tcp NetworkStream和SslStream之间的包装类来解决这个问题,该包装类捕获并抑制无害的超时异常,(似乎)不会丢失功能。
我在一个类似的线程上回答了这个问题的完整代码,在这里https://stackoverflow.com/a/48231248/8915494
关于Read()方法只返回每个Read()上的部分有效负载,您的答案已经正确地解决了这个问题。虽然这是SslStream的"最近"行为,但不幸的是,这是所有网络的预期行为,所有代码都需要创建某种形式的缓冲区来存储片段,直到您拥有完整的数据包。例如,如果数据超过1500字节(假设以太网传输,大多数以太网适配器的最大数据包大小),则很可能会接收到多个部分的数据,并且必须自己重新组装。
希望这有帮助