[BTLS]: Improve error handling. (#4317)
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsContext.cs
index 051656ce0536f5bff3e4a82fc9f2ed76d344f823..5bdac307dc4cadee764cf594332aa87d369d7dc1 100644 (file)
@@ -156,9 +156,20 @@ namespace Mono.Btls
 
                static Exception GetException (MonoBtlsSslError status)
                {
-                       var error = MonoBtlsError.GetError ();
+                       string file;
+                       int line;
+                       var error = MonoBtlsError.GetError (out file, out line);
+                       if (error == null)
+                               return new MonoBtlsException (status);
+
                        var text = MonoBtlsError.GetErrorString (error);
-                       return new MonoBtlsException ("{0} {1}", status, text);
+
+                       string message;
+                       if (file != null)
+                               message = string.Format ("{0} {1}\n  at {2}:{3}", status, text, file, line);
+                       else
+                               message = string.Format ("{0} {1}", status, text);
+                       return new MonoBtlsException (message);
                }
 
                public override bool ProcessHandshake ()
@@ -270,11 +281,13 @@ namespace Mono.Btls
 
                        var cipher = (CipherSuiteCode)ssl.GetCipher ();
                        var protocol = (TlsProtocolCode)ssl.GetVersion ();
+                       var serverName = ssl.GetServerName ();
                        Debug ("GET CONNECTION INFO: {0:x}:{0} {1:x}:{1} {2}", cipher, protocol, (TlsProtocolCode)protocol);
 
                        connectionInfo = new MonoTlsConnectionInfo {
                                CipherSuiteCode = cipher,
-                               ProtocolVersion = GetProtocol (protocol)
+                               ProtocolVersion = GetProtocol (protocol),
+                               PeerDomainName = serverName
                        };
                }
 
@@ -358,7 +371,23 @@ namespace Mono.Btls
                public override void Close ()
                {
                        Debug ("Close!");
-                       ssl.Dispose ();
+
+                       if (ssl != null) {
+                               ssl.Dispose ();
+                               ssl = null;
+                       }
+                       if (ctx != null) {
+                               ctx.Dispose ();
+                               ctx = null;
+                       }
+                       if (bio != null) {
+                               bio.Dispose ();
+                               bio = null;
+                       }
+                       if (errbio != null) {
+                               errbio.Dispose ();
+                               errbio = null;
+                       }
                }
 
                void Dispose<T> (ref T disposable)