[pgsqlclient-checkins] pgsqlclient_10/PgSqlClient.Security.Tls/source TlsNetworkStream.cs,1.4,1.5 Tl
Status: Inactive
Brought to you by:
carlosga_fb
From: <car...@us...> - 2003-10-09 19:50:41
|
Update of /cvsroot/pgsqlclient/pgsqlclient_10/PgSqlClient.Security.Tls/source In directory sc8-pr-cvs1:/tmp/cvs-serv7125 Modified Files: TlsNetworkStream.cs TlsSession.cs TlsSocket.cs Log Message: - Added better control of session state and socket/session/networkstream closing ( needs testing but seems to be the way to go ). Index: TlsNetworkStream.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/PgSqlClient.Security.Tls/source/TlsNetworkStream.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** TlsNetworkStream.cs 9 Oct 2003 10:21:45 -0000 1.4 --- TlsNetworkStream.cs 9 Oct 2003 19:50:34 -0000 1.5 *************** *** 24,28 **** namespace System.Security.Tls { ! public class TlsNetworkStream : Stream { #region PROPERTIES --- 24,28 ---- namespace System.Security.Tls { ! public class TlsNetworkStream : Stream, IDisposable { #region PROPERTIES *************** *** 32,35 **** --- 32,36 ---- private bool canRead; private bool canWrite; + private bool disposed; #endregion *************** *** 89,92 **** --- 90,143 ---- #endregion + #region DESTRUCTOR + + ~TlsNetworkStream() + { + this.Dispose(false); + } + + #endregion + + #region IDISPOSABLE + + void IDisposable.Dispose() + { + this.Dispose(true); + GC.SuppressFinalize(this); + } + + protected virtual void Dispose(bool disposing) + { + if (!disposed) + { + if (disposing) + { + if (this.socket != null) + { + if (this.ownsSocket) + { + try + { + this.socket.Shutdown(SocketShutdown.Both); + } + catch{} + finally + { + this.socket.Close(); + } + } + } + this.ownsSocket = false; + this.canRead = false; + this.canWrite = false; + this.socket = null; + } + + disposed = true; + } + } + + #endregion + #region PROTECTED_PROPERTIES *************** *** 129,132 **** --- 180,200 ---- public TlsNetworkStream(TlsSocket socket, FileAccess access, bool ownsSocket) { + if (socket == null) + { + throw new ArgumentNullException("socket is a null reference."); + } + if (!socket.Blocking) + { + throw new IOException("socket is in a nonblocking state."); + } + if (socket.SocketType != SocketType.Stream) + { + throw new IOException("The SocketType property of socket is not SocketType.Stream."); + } + if (!socket.Connected) + { + throw new IOException("socket is not connected."); + } + this.socket = socket; this.ownsSocket = ownsSocket; *************** *** 184,202 **** public override void Close() { ! if (socket != null) ! { ! if (this.ownsSocket) ! { ! try ! { ! socket.Shutdown(SocketShutdown.Both); ! } ! catch{} ! finally ! { ! socket.Close(); ! } ! } ! } } --- 252,256 ---- public override void Close() { ! ((IDisposable)this).Dispose(); } *************** *** 209,225 **** if (buffer == null) { ! throw new ArgumentNullException("buffer"); } ! if (offset < 0 || offset > buffer.Length) { ! throw new ArgumentOutOfRangeException("offset"); } ! if (size < 0 || size > (buffer.Length - offset)) { ! throw new ArgumentOutOfRangeException("size"); } ! if (socket == null) { ! throw new IOException(); } --- 263,291 ---- if (buffer == null) { ! throw new ArgumentNullException("buffer is a null reference."); } ! if (offset < 0) { ! throw new ArgumentOutOfRangeException("offset is less than 0."); } ! if (offset > buffer.Length) { ! throw new ArgumentOutOfRangeException("offset is greater than the length of buffer."); } ! if (size < 0) { ! throw new ArgumentOutOfRangeException("size is less than 0."); ! } ! if (size > (buffer.Length - offset)) ! { ! throw new ArgumentOutOfRangeException("size is less than the length of buffer minus the value of the offset parameter."); ! } ! if (disposed) ! { ! throw new ObjectDisposedException("The NetworkStream is closed."); ! } ! if (!socket.Connected) ! { ! throw new IOException("The underlying Socket is closed."); } *************** *** 252,270 **** if (buffer == null) { ! throw new ArgumentNullException("buffer"); } ! if (offset < 0 || offset > buffer.Length) { ! throw new ArgumentOutOfRangeException("offset"); } ! if (size < 0 || size > (buffer.Length - offset)) { ! throw new ArgumentOutOfRangeException("size"); } ! if (socket == null) { ! throw new IOException(); } ! try { --- 318,348 ---- if (buffer == null) { ! throw new ArgumentNullException("buffer is a null reference."); } ! if (offset < 0) { ! throw new ArgumentOutOfRangeException("offset is less than 0."); } ! if (offset > buffer.Length) { ! throw new ArgumentOutOfRangeException("offset is greater than the length of buffer."); } ! if (size < 0) { ! throw new ArgumentOutOfRangeException("size is less than 0."); } ! if (size > (buffer.Length - offset)) ! { ! throw new ArgumentOutOfRangeException("size is less than the length of buffer minus the value of the offset parameter."); ! } ! if (disposed) ! { ! throw new ObjectDisposedException("The NetworkStream is closed."); ! } ! if (!socket.Connected) ! { ! throw new IOException("The underlying Socket is closed."); ! } ! try { Index: TlsSession.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/PgSqlClient.Security.Tls/source/TlsSession.cs,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** TlsSession.cs 8 Oct 2003 21:57:16 -0000 1.23 --- TlsSession.cs 9 Oct 2003 19:50:34 -0000 1.24 *************** *** 27,31 **** using System.Security.Tls.Cryptography; using System.Security.Tls.Alerts; - // using System.Security.Tls.Handshake; namespace System.Security.Tls --- 27,30 ---- *************** *** 50,53 **** --- 49,53 ---- private TlsNetworkStream networkStream; private bool isSecure; + private TlsSessionState state; #endregion *************** *** 103,106 **** --- 103,111 ---- } + public TlsSessionState State + { + get { return state; } + } + #endregion *************** *** 113,117 **** this.context = new TlsSessionContext(); this.sessionId = new byte[0]; ! // Initialize socket for connection this.initializeSocket(); --- 118,122 ---- this.context = new TlsSessionContext(); this.sessionId = new byte[0]; ! // Initialize socket for connection this.initializeSocket(); *************** *** 137,140 **** --- 142,147 ---- internal TlsException CreateException(string message) { + this.state = TlsSessionState.Broken; + // Throw an exception will made the connection unavailable // for this both streams will be closed *************** *** 152,163 **** --- 159,173 ---- try { + this.state = TlsSessionState.OpeningSecure; this.socket.DoHandshake(); } catch (TlsException ex) { + this.state = TlsSessionState.Broken; throw ex; } catch (Exception ex) { + this.state = TlsSessionState.Broken; this.closeStreams(); throw ex; *************** *** 169,172 **** --- 179,184 ---- try { + this.state = TlsSessionState.Closing; + if (isSecure) { *************** *** 181,188 **** throw new TlsException("Invalid session termination"); } ! } } catch (Exception ex) { throw ex; } --- 193,201 ---- throw new TlsException("Invalid session termination"); } ! } } catch (Exception ex) { + this.state = TlsSessionState.Broken; throw ex; } *************** *** 191,194 **** --- 204,209 ---- // Close streams closeStreams(); + + this.state = TlsSessionState.Closed; } } *************** *** 217,234 **** private void initializeSocket() { ! // Initialize socket ! IPAddress hostadd = Dns.Resolve(settings.ServerName).AddressList[0]; ! IPEndPoint EPhost = new IPEndPoint(hostadd, settings.ServerPort); ! // Create the socket ! socket = new TlsSocket( ! this, ! AddressFamily.InterNetwork, ! SocketType.Stream, ! ProtocolType.IP); ! // Make the socket to connect to the Server ! socket.Connect(EPhost); ! networkStream = new TlsNetworkStream(socket, true); } --- 232,261 ---- private void initializeSocket() { ! try ! { ! this.state = TlsSessionState.Opening; ! // Initialize socket ! IPAddress hostadd = Dns.Resolve(settings.ServerName).AddressList[0]; ! IPEndPoint EPhost = new IPEndPoint(hostadd, settings.ServerPort); ! // Create the socket ! socket = new TlsSocket( ! this, ! AddressFamily.InterNetwork, ! SocketType.Stream, ! ProtocolType.IP); ! ! // Make the socket to connect to the Server ! socket.Connect(EPhost); ! networkStream = new TlsNetworkStream(socket, true); ! ! this.state = TlsSessionState.Open; ! } ! catch (Exception ex) ! { ! this.state = TlsSessionState.Broken; ! throw ex; ! } } *************** *** 239,246 **** // Close the socket and the networkStream ! if (!this.socket.Connected) ! { ! this.networkStream.Close(); ! } // Reset session information --- 266,270 ---- // Close the socket and the networkStream ! this.networkStream.Close(); // Reset session information *************** *** 249,253 **** this.handshakeFinished = false; this.context = new TlsSessionContext(); ! this.sessionId = new byte[0]; } --- 273,277 ---- this.handshakeFinished = false; this.context = new TlsSessionContext(); ! this.sessionId = new byte[0]; } Index: TlsSocket.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/PgSqlClient.Security.Tls/source/TlsSocket.cs,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** TlsSocket.cs 8 Oct 2003 20:28:13 -0000 1.10 --- TlsSocket.cs 9 Oct 2003 19:50:34 -0000 1.11 *************** *** 82,86 **** this.resetBuffer(); base.Close(); ! this.session.Close(); } --- 82,90 ---- this.resetBuffer(); base.Close(); ! if (this.session.State != TlsSessionState.Closing && ! this.session.State != TlsSessionState.Closed) ! { ! this.session.Close(); ! } } |