@@ -820,9 +820,9 @@ - (id)initWithDelegate:(id)aDelegate delegateQueue:(dispatch_queue_t)dq socketQu
820
820
{
821
821
NSString *assertMsg = @" The given socketQueue parameter must not be a concurrent queue." ;
822
822
823
- NSAssert (sq != dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0 ), @"%@", assertMsg);
824
- NSAssert (sq != dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0 ), @"%@", assertMsg);
825
- NSAssert (sq != dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), @"%@", assertMsg);
823
+ NSAssert1 (sq != dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0 ), @"%@", assertMsg);
824
+ NSAssert1 (sq != dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0 ), @"%@", assertMsg);
825
+ NSAssert1 (sq != dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), @"%@", assertMsg);
826
826
827
827
dispatch_retain (sq);
828
828
socketQueue = sq;
@@ -3183,6 +3183,24 @@ - (BOOL)isIPv6
3183
3183
}
3184
3184
}
3185
3185
3186
+ - (BOOL )isSecure
3187
+ {
3188
+ if (dispatch_get_current_queue () == socketQueue)
3189
+ {
3190
+ return (flags & kSocketSecure ) ? YES : NO ;
3191
+ }
3192
+ else
3193
+ {
3194
+ __block BOOL result;
3195
+
3196
+ dispatch_sync (socketQueue, ^{
3197
+ result = (flags & kSocketSecure ) ? YES : NO ;
3198
+ });
3199
+
3200
+ return result;
3201
+ }
3202
+ }
3203
+
3186
3204
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3187
3205
#pragma mark Utilities
3188
3206
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -3771,8 +3789,31 @@ - (void)doReadData
3771
3789
3772
3790
}
3773
3791
#else
3792
+
3774
3793
estimatedBytesAvailable = socketFDBytesAvailable + [sslReadBuffer length ];
3794
+
3795
+ if (flags & kSocketSecure )
3796
+ {
3797
+ // SecureTransport has an internal buffer of its own.
3798
+ // When we invoke SSLRead, it in turn invokes our lower level read IO function,
3799
+ // and reads data in encrypted chunks from the socket.
3800
+ // If we ask for a length of data from SSLRead that doesn't fall on the border of
3801
+ // one of these encrypted chunks, then the SSLRead function stores the extra
3802
+ // data in its own internal buffer.
3803
+ //
3804
+ // The SSLGetBufferedReadSize function will tell us the size of this internal buffer.
3805
+ // From the documentation:
3806
+ //
3807
+ // "This function does not block or cause any low-level read operations to occur."
3808
+
3809
+ size_t sslInternalBufSize = 0 ;
3810
+ SSLGetBufferedReadSize (sslContext, &sslInternalBufSize);
3811
+
3812
+ estimatedBytesAvailable += sslInternalBufSize;
3813
+ }
3814
+
3775
3815
hasBytesAvailable = (estimatedBytesAvailable > 0 );
3816
+
3776
3817
#endif
3777
3818
3778
3819
if ((hasBytesAvailable == NO ) && ([partialReadBuffer length ] == 0 ))
@@ -4038,15 +4079,22 @@ - (void)doReadData
4038
4079
4039
4080
if (result != noErr)
4040
4081
{
4041
- bytesRead = 0 ;
4042
-
4043
4082
if (result == errSSLWouldBlock)
4044
4083
waiting = YES ;
4045
4084
else
4046
4085
error = [self sslError: result];
4047
4086
4048
- if (readIntoPartialReadBuffer)
4049
- [partialReadBuffer setLength: 0 ];
4087
+ // It's possible that bytesRead > 0, yet the result is errSSLWouldBlock.
4088
+ // This happens when the SSLRead function is able to read some data,
4089
+ // but not the entire amount we requested.
4090
+
4091
+ if (bytesRead <= 0 )
4092
+ {
4093
+ bytesRead = 0 ;
4094
+
4095
+ if (readIntoPartialReadBuffer)
4096
+ [partialReadBuffer setLength: 0 ];
4097
+ }
4050
4098
}
4051
4099
4052
4100
// Do not modify socketFDBytesAvailable.
@@ -5233,22 +5281,24 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5233
5281
5234
5282
if (sslReadBufferLength > 0 )
5235
5283
{
5284
+ LogVerbose (@" %@ : Reading from SSL pre buffer..." , THIS_METHOD);
5285
+
5236
5286
size_t bytesToCopy = (size_t )((sslReadBufferLength > totalBytesLeft) ? totalBytesLeft : sslReadBufferLength);
5237
5287
5238
- LogVerbose (@" Copying %u bytes from sslReadBuffer" , (unsigned )bytesToCopy);
5288
+ LogVerbose (@" %@ : Copying %u bytes from sslReadBuffer" , THIS_METHOD , (unsigned )bytesToCopy);
5239
5289
5240
5290
memcpy (buffer, [sslReadBuffer mutableBytes ], bytesToCopy);
5241
5291
5242
5292
[sslReadBuffer replaceBytesInRange: NSMakeRange (0 , bytesToCopy) withBytes: NULL length: 0 ];
5243
5293
5244
- LogVerbose (@" sslReadBuffer.length = %lu " , (unsigned long )[sslReadBuffer length ]);
5294
+ LogVerbose (@" %@ : sslReadBuffer.length = %lu " , THIS_METHOD , (unsigned long )[sslReadBuffer length ]);
5245
5295
5246
5296
totalBytesLeft -= bytesToCopy;
5247
5297
totalBytesRead += bytesToCopy;
5248
5298
5249
5299
done = (totalBytesLeft == 0 );
5250
5300
5251
- if (done) LogVerbose (@" SSLRead complete " );
5301
+ if (done) LogVerbose (@" %@ : Complete " , THIS_METHOD );
5252
5302
}
5253
5303
5254
5304
//
@@ -5257,6 +5307,8 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5257
5307
5258
5308
if (!done && (socketFDBytesAvailable > 0 ))
5259
5309
{
5310
+ LogVerbose (@" %@ : Reading from socket..." , THIS_METHOD);
5311
+
5260
5312
int socketFD = (socket6FD == SOCKET_NULL) ? socket4FD : socket6FD;
5261
5313
5262
5314
BOOL readIntoPreBuffer;
@@ -5268,13 +5320,13 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5268
5320
// Read all available data from socket into sslReadBuffer.
5269
5321
// Then copy requested amount into dataBuffer.
5270
5322
5323
+ LogVerbose (@" %@ : Reading into sslReadBuffer..." , THIS_METHOD);
5324
+
5271
5325
if ([sslReadBuffer length ] < socketFDBytesAvailable)
5272
5326
{
5273
5327
[sslReadBuffer setLength: socketFDBytesAvailable];
5274
5328
}
5275
5329
5276
- LogVerbose (@" Reading into sslReadBuffer..." );
5277
-
5278
5330
readIntoPreBuffer = YES ;
5279
5331
bytesToRead = (size_t )socketFDBytesAvailable;
5280
5332
buf = [sslReadBuffer mutableBytes ];
@@ -5283,17 +5335,19 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5283
5335
{
5284
5336
// Read available data from socket directly into dataBuffer.
5285
5337
5338
+ LogVerbose (@" %@ : Reading directly into dataBuffer..." , THIS_METHOD);
5339
+
5286
5340
readIntoPreBuffer = NO ;
5287
5341
bytesToRead = totalBytesLeft;
5288
5342
buf = buffer + totalBytesRead;
5289
5343
}
5290
5344
5291
5345
ssize_t result = read (socketFD, buf, bytesToRead);
5292
- LogVerbose (@" read from socket = %i " , (int )result);
5346
+ LogVerbose (@" %@ : read from socket = %i " , THIS_METHOD , (int )result);
5293
5347
5294
5348
if (result < 0 )
5295
5349
{
5296
- LogVerbose (@" read errno = %i " , errno);
5350
+ LogVerbose (@" %@ : read errno = %i " , THIS_METHOD , errno);
5297
5351
5298
5352
if (errno != EWOULDBLOCK)
5299
5353
{
@@ -5330,7 +5384,7 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5330
5384
{
5331
5385
size_t bytesToCopy = MIN (totalBytesLeft, bytesReadFromSocket);
5332
5386
5333
- LogVerbose (@" Copying %u bytes from sslReadBuffer" , (unsigned )bytesToCopy);
5387
+ LogVerbose (@" %@ : Copying %u bytes out of sslReadBuffer" , THIS_METHOD , (unsigned )bytesToCopy);
5334
5388
5335
5389
memcpy (buffer + totalBytesRead, [sslReadBuffer bytes ], bytesToCopy);
5336
5390
@@ -5340,7 +5394,7 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5340
5394
totalBytesLeft -= bytesToCopy;
5341
5395
totalBytesRead += bytesToCopy;
5342
5396
5343
- LogVerbose (@" sslReadBuffer.length = %lu " , (unsigned long )[sslReadBuffer length ]);
5397
+ LogVerbose (@" %@ : sslReadBuffer.length = %lu " , THIS_METHOD , (unsigned long )[sslReadBuffer length ]);
5344
5398
}
5345
5399
else
5346
5400
{
@@ -5350,7 +5404,7 @@ - (OSStatus)sslReadWithBuffer:(void *)buffer length:(size_t *)bufferLength
5350
5404
5351
5405
done = (totalBytesLeft == 0 );
5352
5406
5353
- if (done) LogVerbose (@" SSLRead complete " );
5407
+ if (done) LogVerbose (@" %@ : Complete " , THIS_METHOD );
5354
5408
}
5355
5409
}
5356
5410
0 commit comments