From 9f78add64fea837ed377f6f9afdf611cd21fe5fa Mon Sep 17 00:00:00 2001 From: Xin Chen Date: Thu, 2 Jun 2022 08:52:06 -0700 Subject: [PATCH] Throw IOException when SslStream is not authenticated or disposed. --- .../Microsoft.Azure.Amqp.Android.csproj | 3 +- .../Amqp/Transport/TcpTransportListener.cs | 1 + .../Amqp/Transport/TlsTransport.cs | 36 +++++++++++++++++-- Microsoft.Azure.Amqp/Properties/Version.cs | 2 +- .../Test.Microsoft.Amqp.Android.csproj | 3 +- 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Microsoft.Azure.Amqp.Android/Microsoft.Azure.Amqp.Android.csproj b/Microsoft.Azure.Amqp.Android/Microsoft.Azure.Amqp.Android.csproj index e560e7f8..452174e5 100644 --- a/Microsoft.Azure.Amqp.Android/Microsoft.Azure.Amqp.Android.csproj +++ b/Microsoft.Azure.Amqp.Android/Microsoft.Azure.Amqp.Android.csproj @@ -14,8 +14,7 @@ 512 Resources\Resource.Designer.cs Off - True - v6.0 + v11.0 CS1734,CS1591 diff --git a/Microsoft.Azure.Amqp/Amqp/Transport/TcpTransportListener.cs b/Microsoft.Azure.Amqp/Amqp/Transport/TcpTransportListener.cs index 44dc491c..3b9ebd63 100644 --- a/Microsoft.Azure.Amqp/Amqp/Transport/TcpTransportListener.cs +++ b/Microsoft.Azure.Amqp/Amqp/Transport/TcpTransportListener.cs @@ -77,6 +77,7 @@ protected override void OnListen() for (int i = 0; i < addresses.Count; ++i) { this.listenSockets[i] = new Socket(addresses[i].AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true }; + this.listenSockets[i].SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); this.listenSockets[i].Bind(new IPEndPoint(addresses[i], this.transportSettings.Port)); this.listenSockets[i].Listen(this.transportSettings.TcpBacklog); diff --git a/Microsoft.Azure.Amqp/Amqp/Transport/TlsTransport.cs b/Microsoft.Azure.Amqp/Amqp/Transport/TlsTransport.cs index 69c593c1..81eea4a9 100644 --- a/Microsoft.Azure.Amqp/Amqp/Transport/TlsTransport.cs +++ b/Microsoft.Azure.Amqp/Amqp/Transport/TlsTransport.cs @@ -4,6 +4,7 @@ namespace Microsoft.Azure.Amqp.Transport { using System; + using System.IO; using System.Net.Security; using System.Security.Cryptography.X509Certificates; using Microsoft.Azure.Amqp.X509; @@ -78,7 +79,20 @@ public override bool WriteAsync(TransportAsyncCallbackArgs args) } } - IAsyncResult result = this.sslStream.BeginWrite(buffer.Array, buffer.Offset, buffer.Count, onWriteComplete, this); + IAsyncResult result; + try + { + result = this.sslStream.BeginWrite(buffer.Array, buffer.Offset, buffer.Count, onWriteComplete, this); + } + catch (ObjectDisposedException ode) + { + throw new IOException($"Transport '{this}' is closed", ode); + } + catch (InvalidOperationException ioe) + { + throw new IOException($"Transport '{this}' is valid for write operations.", ioe); + } + bool completedSynchronously = result.CompletedSynchronously; if (completedSynchronously) { @@ -94,7 +108,20 @@ public override bool ReadAsync(TransportAsyncCallbackArgs args) Fx.Assert(args.Buffer != null, "must have buffer to read"); Fx.Assert(this.readState.Args == null, "Cannot read when a read is still in progress"); this.readState.Args = args; - IAsyncResult result = this.sslStream.BeginRead(args.Buffer, args.Offset, args.Count, onReadComplete, this); + IAsyncResult result; + try + { + result = this.sslStream.BeginRead(args.Buffer, args.Offset, args.Count, onReadComplete, this); + } + catch (ObjectDisposedException ode) + { + throw new IOException($"Transport '{this}' is closed", ode); + } + catch (InvalidOperationException ioe) + { + throw new IOException($"Transport '{this}' is valid for read operations.", ioe); + } + bool completedSynchronously = result.CompletedSynchronously; if (completedSynchronously) { @@ -252,6 +279,11 @@ void HandleOperationComplete(IAsyncResult result, bool write, bool syncComplete) } catch (Exception exception) when (!Fx.IsFatal(exception)) { + if (exception is InvalidOperationException) + { + exception = new IOException($"Transport '{this}' is valid for IO operations.", exception); + } + args.Exception = exception; } diff --git a/Microsoft.Azure.Amqp/Properties/Version.cs b/Microsoft.Azure.Amqp/Properties/Version.cs index 72c5c70d..abf17e62 100644 --- a/Microsoft.Azure.Amqp/Properties/Version.cs +++ b/Microsoft.Azure.Amqp/Properties/Version.cs @@ -3,4 +3,4 @@ using System.Runtime.InteropServices; [assembly: AssemblyVersion("2.4.0.0")] -[assembly: AssemblyInformationalVersion("2.5.10")] +[assembly: AssemblyInformationalVersion("2.5.12")] diff --git a/test/Test.Microsoft.Amqp.Android/Test.Microsoft.Amqp.Android.csproj b/test/Test.Microsoft.Amqp.Android/Test.Microsoft.Amqp.Android.csproj index 7b057b89..54dc4764 100644 --- a/test/Test.Microsoft.Amqp.Android/Test.Microsoft.Amqp.Android.csproj +++ b/test/Test.Microsoft.Amqp.Android/Test.Microsoft.Amqp.Android.csproj @@ -18,8 +18,7 @@ Resources\Resource.Designer.cs Resource Off - True - v8.0 + v11.0 Properties\AndroidManifest.xml Resources Assets