Thread: [pgsqlclient-checkins] pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Protocol.Tls Context
Status: Inactive
Brought to you by:
carlosga_fb
From: Carlos G. Á. <car...@us...> - 2005-02-02 19:25:19
|
Update of /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Protocol.Tls In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12986 Modified Files: Context.cs RecordProtocol.cs SslClientStream.cs Log Message: Fixes for the hang problem (Sebastien Pouliot) Index: RecordProtocol.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** RecordProtocol.cs 31 Jan 2005 14:10:48 -0000 1.23 --- RecordProtocol.cs 2 Feb 2005 19:25:06 -0000 1.24 *************** *** 26,30 **** --- 26,32 ---- using System.Collections; using System.IO; + using System.Net; using System.Net.Sockets; + using System.Threading; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; *************** *** 74,77 **** --- 76,106 ---- #region Reveive Record Methods + internal bool WaitForData () + { + // Problem: NetworkStream.ReadByte blocks until a byte is received + // and (maybe) no byte will ever be received + if (this.innerStream is NetworkStream) + { + NetworkStream ns = (this.innerStream as NetworkStream); + int delay = 10; + + while (delay < Context.TIMEOUT) + { + if (ns.DataAvailable) + { + return true; + } + Thread.Sleep (delay); + delay *= 2; + } + + // Nothing to see, move along + return false; + } + + // We assume other type of stream have their own timeout capabilities + return true; + } + public byte[] ReceiveRecord() { *************** *** 83,86 **** --- 112,120 ---- } + if (!WaitForData()) + { + return null; + } + // Try to read the Record Content Type int type = this.innerStream.ReadByte(); *************** *** 152,160 **** { byte[] b = new byte[2]; - this.innerStream.Read(b, 0, b.Length); ! short val = BitConverter.ToInt16(b, 0); ! return System.Net.IPAddress.HostToNetworkOrder(val); } --- 186,196 ---- { byte[] b = new byte[2]; ! if (this.innerStream.Read (b, 0, b.Length) != 2) ! { ! throw new TlsException (AlertLevel.Fatal, AlertDescription.DecodeError); ! } ! return IPAddress.HostToNetworkOrder(BitConverter.ToInt16(b, 0)); } *************** *** 200,213 **** private byte[] ReadClientHelloV2() { ! int msgLength = this.innerStream.ReadByte(); // Read the message contents ! byte[] message = new byte[msgLength]; ! this.innerStream.Read(message, 0, message.Length); // Add them to a TlsStream TlsStream stream = new TlsStream(message); ! int msgType = stream.ReadByte(); if (msgType != 1) { --- 236,257 ---- private byte[] ReadClientHelloV2() { ! int msgLength = this.innerStream.ReadByte(); ! int msgType = -1; ! byte[] message = null; // Read the message contents ! if (msgLength > 0) ! { ! message = new byte[msgLength]; ! if (this.innerStream.Read(message, 0, message.Length) != msgLength) ! { ! ! } ! } // Add them to a TlsStream TlsStream stream = new TlsStream(message); ! msgType = stream.ReadByte(); if (msgType != 1) { *************** *** 264,269 **** private byte[] ReadStandardRecordBuffer() { ! short protocol = this.ReadShort(); ! short length = this.ReadShort(); // Read Record data --- 308,314 ---- private byte[] ReadStandardRecordBuffer() { ! short protocol = this.ReadShort(); ! short length = this.ReadShort(); ! int n = 0; // Read Record data *************** *** 272,276 **** while (received != length) { ! received += this.innerStream.Read(buffer, received, buffer.Length - received); } --- 317,336 ---- while (received != length) { ! int incoming = this.innerStream.Read(buffer, received, buffer.Length - received); ! ! // We risk an infinite loop is data isn't available ! if (incoming == 0) ! { ! if (!this.Wait(ref n)) ! { ! throw new TlsException(AlertLevel.Fatal, AlertDescription.CloseNotify); ! } ! } ! else ! { ! n = 0; // reset ! } ! ! received += incoming; } *************** *** 287,290 **** --- 347,368 ---- } + private bool Wait (ref int delay) + { + if (delay < 10) + { + delay = 10; + } + + if (delay >= Context.TIMEOUT) + { + return false; + } + + Thread.Sleep (delay); + delay *= 2; + + return true; + } + #endregion Index: Context.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Protocol.Tls/Context.cs,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** Context.cs 24 Sep 2004 21:33:34 -0000 1.11 --- Context.cs 2 Feb 2005 19:25:05 -0000 1.12 *************** *** 42,45 **** --- 42,46 ---- internal const short SSL3_PROTOCOL_CODE = (0x03 << 8) | 0x00; internal const long UNIX_BASE_TICKS = 621355968000000000; + internal const int TIMEOUT = 1000; #endregion Index: SslClientStream.cs =================================================================== RCS file: /cvsroot/pgsqlclient/pgsqlclient_10/Mono.Security/Mono.Security/Mono.Security.Protocol.Tls/SslClientStream.cs,v retrieving revision 1.23 retrieving revision 1.24 diff -C2 -d -r1.23 -r1.24 *** SslClientStream.cs 31 Jan 2005 14:11:29 -0000 1.23 --- SslClientStream.cs 2 Feb 2005 19:25:06 -0000 1.24 *************** *** 471,488 **** this.inputBuffer.Seek(position, SeekOrigin.Begin); } - else - { - if (record == null) - { - break; - } - } ! // TODO: Review if we need to check the Length ! // property of the innerStream for other types ! // of streams, to check that there are data available ! // for read ! if (this.innerStream is NetworkStream && ! !((NetworkStream)this.innerStream).DataAvailable) { break; --- 471,477 ---- this.inputBuffer.Seek(position, SeekOrigin.Begin); } ! if ((this.innerStream is NetworkStream && !((NetworkStream)this.innerStream).DataAvailable) || ! record == null) { break; *************** *** 491,496 **** } ! asyncResult = this.inputBuffer.BeginRead( ! buffer, offset, count, callback, state); } catch (TlsException ex) --- 480,484 ---- } ! asyncResult = this.inputBuffer.BeginRead(buffer, offset, count, callback, state); } catch (TlsException ex) |