@@ -2628,7 +2628,8 @@ - (void)closeWithError:(NSError *)error
2628
2628
#endif
2629
2629
#if SECURE_TRANSPORT_MAYBE_AVAILABLE
2630
2630
{
2631
- [sslReadBuffer setLength: 0 ];
2631
+ [sslPreBuffer reset ];
2632
+
2632
2633
if (sslContext)
2633
2634
{
2634
2635
// Getting a linker error here about the SSLx() functions?
@@ -4051,15 +4052,15 @@ - (void)flushSSLBuffers
4051
4052
4052
4053
// Figure out if there is any data available to be read
4053
4054
//
4054
- // socketFDBytesAvailable <- Number of encrypted bytes we haven't read from the bsd socket
4055
- // [sslReadBuffer length ] <- Number of encrypted bytes we've buffered from bsd socket
4056
- // sslInternalBufSize <- Number of decrypted bytes SecureTransport has buffered
4055
+ // socketFDBytesAvailable <- Number of encrypted bytes we haven't read from the bsd socket
4056
+ // [sslPreBuffer availableBytes ] <- Number of encrypted bytes we've buffered from bsd socket
4057
+ // sslInternalBufSize <- Number of decrypted bytes SecureTransport has buffered
4057
4058
//
4058
4059
// We call the variable "estimated" because we don't know how many decrypted bytes we'll get
4059
- // from the encrypted bytes in the sslReadBuffer .
4060
+ // from the encrypted bytes in the sslPreBuffer .
4060
4061
// However, we do know this is an upper bound on the estimation.
4061
4062
4062
- estimatedBytesAvailable = socketFDBytesAvailable + [sslReadBuffer length ];
4063
+ estimatedBytesAvailable = socketFDBytesAvailable + [sslPreBuffer availableBytes ];
4063
4064
4064
4065
size_t sslInternalBufSize = 0 ;
4065
4066
SSLGetBufferedReadSize (sslContext, &sslInternalBufSize);
@@ -4095,7 +4096,7 @@ - (void)flushSSLBuffers
4095
4096
[preBuffer didWrite: bytesRead];
4096
4097
}
4097
4098
4098
- LogVerbose (@" %@ - prebuffer.length = %lu " , THIS_METHOD, ( unsigned long )[ringBuffer availableBytes ]);
4099
+ LogVerbose (@" %@ - prebuffer.length = %zu " , THIS_METHOD, [preBuffer availableBytes ]);
4099
4100
4100
4101
if (result != noErr)
4101
4102
{
@@ -4202,9 +4203,9 @@ - (void)doReadData
4202
4203
// This has to do with the encypted packets that are coming across the TCP stream.
4203
4204
// But it's non-optimal to do a bunch of small reads from the BSD socket.
4204
4205
// So our SSLReadFunction reads all available data from the socket (optimizing the sys call)
4205
- // and may store excess in the sslReadBuffer .
4206
+ // and may store excess in the sslPreBuffer .
4206
4207
4207
- estimatedBytesAvailable += [sslReadBuffer length ];
4208
+ estimatedBytesAvailable += [sslPreBuffer availableBytes ];
4208
4209
4209
4210
// The second buffer is within SecureTransport.
4210
4211
// As mentioned earlier, there are encrypted packets coming across the TCP stream.
@@ -4327,7 +4328,7 @@ - (void)doReadData
4327
4328
// Remove the copied bytes from the preBuffer
4328
4329
[preBuffer didRead: bytesToCopy];
4329
4330
4330
- LogVerbose (@" copied(%lu ) preBufferLength(%lu )" , bytesToCopy, [preBuffer availableBytes ]);
4331
+ LogVerbose (@" copied(%lu ) preBufferLength(%zu )" , ( unsigned long ) bytesToCopy, [preBuffer availableBytes ]);
4331
4332
4332
4333
// Update totals
4333
4334
@@ -4607,10 +4608,12 @@ - (void)doReadData
4607
4608
// We just read a big chunk of data into the preBuffer
4608
4609
4609
4610
[preBuffer didWrite: bytesRead];
4611
+ LogVerbose (@" read data into preBuffer - preBuffer.length = %zu " , [preBuffer availableBytes ]);
4610
4612
4611
4613
// Search for the terminating sequence
4612
4614
4613
4615
bytesToRead = [currentRead readLengthForTermWithPreBuffer: preBuffer found: &done];
4616
+ LogVerbose (@" copying %lu bytes from preBuffer" , (unsigned long )bytesToRead);
4614
4617
4615
4618
// Ensure there's room on the read packet's buffer
4616
4619
@@ -4625,6 +4628,7 @@ - (void)doReadData
4625
4628
4626
4629
// Remove the copied bytes from the prebuffer
4627
4630
[preBuffer didRead: bytesToRead];
4631
+ LogVerbose (@" preBuffer.length = %zu " , [preBuffer availableBytes ]);
4628
4632
4629
4633
// Update totals
4630
4634
currentRead->bytesDone += bytesToRead;
@@ -4659,12 +4663,14 @@ - (void)doReadData
4659
4663
4660
4664
// Copy excess data into preBuffer
4661
4665
4666
+ LogVerbose (@" copying %ld overflow bytes into preBuffer" , (long )overflow);
4662
4667
[preBuffer ensureCapacityForWrite: overflow];
4663
4668
4664
4669
uint8_t *overflowBuffer = buffer + underflow;
4665
4670
memcpy ([preBuffer writeBuffer ], overflowBuffer, overflow);
4666
4671
4667
4672
[preBuffer didWrite: overflow];
4673
+ LogVerbose (@" preBuffer.length = %zu " , [preBuffer availableBytes ]);
4668
4674
4669
4675
// Note: The completeCurrentRead method will trim the buffer for us.
4670
4676
@@ -5794,7 +5800,7 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5794
5800
{
5795
5801
LogVerbose (@" sslReadWithBuffer:%p length:%lu " , buffer, (unsigned long )*bufferLength);
5796
5802
5797
- if ((socketFDBytesAvailable == 0 ) && ([sslReadBuffer length ] == 0 ))
5803
+ if ((socketFDBytesAvailable == 0 ) && ([sslPreBuffer availableBytes ] == 0 ))
5798
5804
{
5799
5805
LogVerbose (@" %@ - No data available to read..." , THIS_METHOD);
5800
5806
@@ -5819,25 +5825,24 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5819
5825
// STEP 1 : READ FROM SSL PRE BUFFER
5820
5826
//
5821
5827
5822
- NSUInteger sslReadBufferLength = [sslReadBuffer length ];
5828
+ size_t sslPreBufferLength = [sslPreBuffer availableBytes ];
5823
5829
5824
- if (sslReadBufferLength > 0 )
5830
+ if (sslPreBufferLength > 0 )
5825
5831
{
5826
5832
LogVerbose (@" %@ : Reading from SSL pre buffer..." , THIS_METHOD);
5827
5833
5828
5834
size_t bytesToCopy;
5829
- if (sslReadBufferLength > totalBytesLeftToBeRead)
5835
+ if (sslPreBufferLength > totalBytesLeftToBeRead)
5830
5836
bytesToCopy = totalBytesLeftToBeRead;
5831
5837
else
5832
- bytesToCopy = (size_t )sslReadBufferLength;
5833
-
5834
- LogVerbose (@" %@ : Copying %zu bytes from sslReadBuffer" , THIS_METHOD, bytesToCopy);
5838
+ bytesToCopy = sslPreBufferLength;
5835
5839
5836
- memcpy (buffer, [sslReadBuffer mutableBytes ] , bytesToCopy);
5840
+ LogVerbose ( @" %@ : Copying %zu bytes from sslPreBuffer " , THIS_METHOD , bytesToCopy);
5837
5841
5838
- [sslReadBuffer replaceBytesInRange: NSMakeRange (0 , bytesToCopy) withBytes: NULL length: 0 ];
5842
+ memcpy (buffer, [sslPreBuffer readBuffer ], bytesToCopy);
5843
+ [sslPreBuffer didRead: bytesToCopy];
5839
5844
5840
- LogVerbose (@" %@ : sslReadBuffer .length = %lu " , THIS_METHOD, ( unsigned long )[sslReadBuffer length ]);
5845
+ LogVerbose (@" %@ : sslPreBuffer .length = %zu " , THIS_METHOD, [sslPreBuffer availableBytes ]);
5841
5846
5842
5847
totalBytesRead += bytesToCopy;
5843
5848
totalBytesLeftToBeRead -= bytesToCopy;
@@ -5863,19 +5868,16 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5863
5868
5864
5869
if (socketFDBytesAvailable > totalBytesLeftToBeRead)
5865
5870
{
5866
- // Read all available data from socket into sslReadBuffer .
5871
+ // Read all available data from socket into sslPreBuffer .
5867
5872
// Then copy requested amount into dataBuffer.
5868
5873
5869
- LogVerbose (@" %@ : Reading into sslReadBuffer ..." , THIS_METHOD);
5874
+ LogVerbose (@" %@ : Reading into sslPreBuffer ..." , THIS_METHOD);
5870
5875
5871
- if ([sslReadBuffer length ] < socketFDBytesAvailable)
5872
- {
5873
- [sslReadBuffer setLength: socketFDBytesAvailable];
5874
- }
5876
+ [sslPreBuffer ensureCapacityForWrite: socketFDBytesAvailable];
5875
5877
5876
5878
readIntoPreBuffer = YES ;
5877
5879
bytesToRead = (size_t )socketFDBytesAvailable;
5878
- buf = [sslReadBuffer mutableBytes ];
5880
+ buf = [sslPreBuffer writeBuffer ];
5879
5881
}
5880
5882
else
5881
5883
{
@@ -5901,23 +5903,13 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5901
5903
}
5902
5904
5903
5905
socketFDBytesAvailable = 0 ;
5904
-
5905
- if (readIntoPreBuffer)
5906
- {
5907
- [sslReadBuffer setLength: 0 ];
5908
- }
5909
5906
}
5910
5907
else if (result == 0 )
5911
5908
{
5912
5909
LogVerbose (@" %@ : read EOF" , THIS_METHOD);
5913
5910
5914
5911
socketError = YES ;
5915
5912
socketFDBytesAvailable = 0 ;
5916
-
5917
- if (readIntoPreBuffer)
5918
- {
5919
- [sslReadBuffer setLength: 0 ];
5920
- }
5921
5913
}
5922
5914
else
5923
5915
{
@@ -5930,19 +5922,19 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5930
5922
5931
5923
if (readIntoPreBuffer)
5932
5924
{
5933
- size_t bytesToCopy = MIN (totalBytesLeftToBeRead, bytesReadFromSocket) ;
5925
+ [sslPreBuffer didWrite: bytesReadFromSocket] ;
5934
5926
5935
- LogVerbose ( @" %@ : Copying %zu bytes out of sslReadBuffer " , THIS_METHOD, bytesToCopy );
5927
+ size_t bytesToCopy = MIN (totalBytesLeftToBeRead, bytesReadFromSocket );
5936
5928
5937
- memcpy (( uint8_t *)buffer + totalBytesRead, [sslReadBuffer bytes ] , bytesToCopy);
5929
+ LogVerbose ( @" %@ : Copying %zu bytes out of sslPreBuffer " , THIS_METHOD , bytesToCopy);
5938
5930
5939
- [sslReadBuffer setLength: bytesReadFromSocket] ;
5940
- [sslReadBuffer replaceBytesInRange: NSMakeRange ( 0 , bytesToCopy) withBytes: NULL length: 0 ];
5931
+ memcpy (( uint8_t *)buffer + totalBytesRead, [sslPreBuffer readBuffer ], bytesToCopy) ;
5932
+ [sslPreBuffer didRead: bytesToCopy];
5941
5933
5942
5934
totalBytesRead += bytesToCopy;
5943
5935
totalBytesLeftToBeRead -= bytesToCopy;
5944
5936
5945
- LogVerbose (@" %@ : sslReadBuffer .length = %lu " , THIS_METHOD, ( unsigned long )[sslReadBuffer length ]);
5937
+ LogVerbose (@" %@ : sslPreBuffer .length = %zu " , THIS_METHOD, [sslPreBuffer availableBytes ]);
5946
5938
}
5947
5939
else
5948
5940
{
@@ -6392,22 +6384,22 @@ - (void)ssl_startTLS
6392
6384
}
6393
6385
#endif
6394
6386
6395
- // Setup the sslReadBuffer
6387
+ // Setup the sslPreBuffer
6396
6388
//
6397
- // Any data in the preBuffer needs to be moved into the sslReadBuffer ,
6389
+ // Any data in the preBuffer needs to be moved into the sslPreBuffer ,
6398
6390
// as this data is now part of the secure read stream.
6399
6391
6400
- sslReadBuffer = [[NSMutableData alloc ] init ];
6392
+ sslPreBuffer = [[GCDAsyncSocketPreBuffer alloc ] initWithCapacity: ( 1024 * 4 ) ];
6401
6393
6402
- uint8_t *preBuf;
6403
- size_t preBufLen;
6394
+ size_t preBufferLength = [preBuffer availableBytes ];
6404
6395
6405
- [preBuffer getReadBuffer: &preBuf availableBytes: &preBufLen];
6406
-
6407
- if (preBufLen > 0 )
6396
+ if (preBufferLength > 0 )
6408
6397
{
6409
- [sslReadBuffer appendBytes: preBuf length: preBufLen];
6410
- [preBuffer didRead: preBufLen];
6398
+ [sslPreBuffer ensureCapacityForWrite: preBufferLength];
6399
+
6400
+ memcpy ([sslPreBuffer writeBuffer ], [preBuffer readBuffer ], preBufferLength);
6401
+ [preBuffer didRead: preBufferLength];
6402
+ [sslPreBuffer didWrite: preBufferLength];
6411
6403
}
6412
6404
6413
6405
// Start the SSL Handshake process
0 commit comments