Index: src/SSLSocket.h ================================================================== --- src/SSLSocket.h +++ src/SSLSocket.h @@ -31,11 +31,10 @@ @class X509Certificate; @interface SSLSocket: OFTCPSocket { SSL *_SSL; - id _delegate; OFString *_certificateFile, *_privateKeyFile; const char *_privateKeyPassphrase; bool _certificateVerificationEnabled; bool _requestClientCertificatesEnabled; } Index: src/SSLSocket.m ================================================================== --- src/SSLSocket.m +++ src/SSLSocket.m @@ -1,7 +1,7 @@ /* - * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 + * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 * Jonathan Schleifer * Copyright (c) 2011, Florian Zeitz * Copyright (c) 2011, Jos Kuijpers * * https://heap.zone/git/objopenssl.git @@ -94,44 +94,39 @@ - (void)SSL_startTLSWithExpectedHost: (OFString *)host port: (uint16_t)port; - (void)SSL_super_close; @end -@interface SSLSocket_ConnectContext: OFObject +@interface SSLSocket_ConnectDelegate: OFObject { + SSLSocket *_socket; OFString *_host; uint16_t _port; - id _target; - SEL _selector; - id _context; + id _delegate; } -- (instancetype)initWithHost: (OFString *)host - port: (uint16_t)port - target: (id)target - selector: (SEL)selector - context: (id)context; -- (void)socketDidConnect: (SSLSocket *)sock - context: (id)context - exception: (id)exception; +- (instancetype)initWithSocket: (SSLSocket *)sock + host: (OFString *)host + port: (uint16_t)port + delegate: (id )delegate; @end -@implementation SSLSocket_ConnectContext -- (instancetype)initWithHost: (OFString *)host - port: (uint16_t)port - target: (id)target - selector: (SEL)selector - context: (id)context +@implementation SSLSocket_ConnectDelegate +- (instancetype)initWithSocket: (SSLSocket *)sock + host: (OFString *)host + port: (uint16_t)port + delegate: (id )delegate { self = [super init]; @try { + _socket = [sock retain]; _host = [host copy]; _port = port; - _target = [target retain]; - _selector = selector; - _context = [context retain]; + _delegate = [delegate retain]; + + [_socket setDelegate: self]; } @catch (id e) { [self release]; @throw e; } @@ -138,41 +133,58 @@ return self; } - (void)dealloc { - [_host release]; - [_target release]; - [_context release]; + if ([_socket delegate] == self) + [_socket setDelegate: _delegate]; + + [_socket release]; + [_delegate release]; [super dealloc]; } -- (void)socketDidConnect: (SSLSocket *)sock - context: (id)context - exception: (id)exception -{ - void (*func)(id, SEL, OFTCPSocket *, id, id) = - (void (*)(id, SEL, OFTCPSocket *, id, id)) - [_target methodForSelector: _selector]; - - if (exception == nil) { - @try { - [sock SSL_startTLSWithExpectedHost: _host - port: _port]; - } @catch (id e) { - func(_target, _selector, sock, _context, e); - return; - } - } - - func(_target, _selector, sock, _context, exception); +- (void)socket: (OF_KINDOF(OFTCPSocket *))sock + didConnectToHost: (OFString *)host + port: (uint16_t)port +{ + @try { + [sock SSL_startTLSWithExpectedHost: _host + port: _port]; + } @catch (id e) { + [_socket setDelegate: _delegate]; + [_delegate socket: sock + didFailToConnectWithException: e + host: host + port: port]; + return; + } + + [_socket setDelegate: _delegate]; + [_delegate socket: sock + didConnectToHost: host + port: port]; +} + +- (void)socket: (OF_KINDOF(OFTCPSocket *))sock + didFailToConnectWithException: (id)exception + host: (OFString *)host + port: (uint16_t)port +{ + [_socket setDelegate: _delegate]; + + return [_delegate socket: sock + didFailToConnectWithException: exception + host: host + port: port]; } @end @implementation SSLSocket -@synthesize delegate = _delegate, certificateFile = _certificateFile; +@dynamic delegate; +@synthesize certificateFile = _certificateFile; @synthesize privateKeyFile = _privateKeyFile; @synthesize privateKeyPassphrase = _privateKeyPassphrase; @synthesize certificateVerificationEnabled = _certificateVerificationEnabled; @synthesize requestClientCertificatesEnabled = _requestClientCertificatesEnabled; @@ -357,30 +369,22 @@ } - (void)asyncConnectToHost: (OFString *)host port: (uint16_t)port runLoopMode: (of_run_loop_mode_t)runLoopMode - target: (id)target - selector: (SEL)selector - context: (id)userContext { void *pool = objc_autoreleasePoolPush(); - SSLSocket_ConnectContext *context; - - context = [[[SSLSocket_ConnectContext alloc] - initWithHost: host - port: port - target: target - selector: selector - context: userContext] autorelease]; + SSLSocket_ConnectDelegate *connectDelegate; + + connectDelegate = [[[SSLSocket_ConnectDelegate alloc] + initWithSocket: self + host: host + port: port + delegate: _delegate] autorelease]; [super asyncConnectToHost: host port: port - runLoopMode: runLoopMode - target: context - selector: @selector(socketDidConnect:context: - exception:) - context: nil]; + runLoopMode: runLoopMode]; objc_autoreleasePoolPop(pool); } #ifdef OF_HAVE_BLOCKS