@@ -22,18 +22,27 @@ #import "OFData.h" #import "OFRunLoop.h" #import "OFString.h" #import "OFConnectionFailedException.h" + +enum { + stateSendAuthentication = 1, + stateReadVersion, + stateSendRequest, + stateReadResponse, + stateReadAddress, + stateReadAddressLength, +}; @implementation OFTCPSocketSOCKS5Connector - (instancetype)initWithSocket: (OFTCPSocket *)sock host: (OFString *)host port: (uint16_t)port delegate: (id )delegate #ifdef OF_HAVE_BLOCKS - block: (of_tcp_socket_async_connect_block_t)block + block: (OFTCPSocketAsyncConnectBlock)block #endif { self = [super init]; @try { @@ -80,11 +89,11 @@ _block(_exception); else { #endif if ([_delegate respondsToSelector: @selector(socket:didConnectToHost:port:exception:)]) - [_delegate socket: _socket + [_delegate socket: _socket didConnectToHost: _host port: _port exception: _exception]; #ifdef OF_HAVE_BLOCKS } @@ -102,24 +111,23 @@ _exception = [exception retain]; [self didConnect]; return; } - data = [OFData dataWithItems: "\x05\x01\x00" - count: 3]; + data = [OFData dataWithItems: "\x05\x01\x00" count: 3]; - _SOCKS5State = OF_SOCKS5_STATE_SEND_AUTHENTICATION; + _SOCKS5State = stateSendAuthentication; [_socket asyncWriteData: data runLoopMode: [OFRunLoop currentRunLoop].currentMode]; } - (bool)stream: (OFStream *)sock didReadIntoBuffer: (void *)buffer length: (size_t)length exception: (id)exception { - of_run_loop_mode_t runLoopMode; + OFRunLoopMode runLoopMode; unsigned char *SOCKSVersion; uint8_t hostLength; unsigned char port[2]; unsigned char *response, *addressLength; @@ -130,11 +138,11 @@ } runLoopMode = [OFRunLoop currentRunLoop].currentMode; switch (_SOCKS5State) { - case OF_SOCKS5_STATE_READ_VERSION: + case stateReadVersion: SOCKSVersion = buffer; if (SOCKSVersion[0] != 5 || SOCKSVersion[1] != 0) { _exception = [[OFConnectionFailedException alloc] initWithHost: _host @@ -146,28 +154,24 @@ } [_request release]; _request = [[OFMutableData alloc] init]; - [_request addItems: "\x05\x01\x00\x03" - count: 4]; + [_request addItems: "\x05\x01\x00\x03" count: 4]; hostLength = (uint8_t)_host.UTF8StringLength; [_request addItem: &hostLength]; - [_request addItems: _host.UTF8String - count: hostLength]; + [_request addItems: _host.UTF8String count: hostLength]; port[0] = _port >> 8; port[1] = _port & 0xFF; - [_request addItems: port - count: 2]; + [_request addItems: port count: 2]; - _SOCKS5State = OF_SOCKS5_STATE_SEND_REQUEST; - [_socket asyncWriteData: _request - runLoopMode: runLoopMode]; + _SOCKS5State = stateSendRequest; + [_socket asyncWriteData: _request runLoopMode: runLoopMode]; return false; - case OF_SOCKS5_STATE_READ_RESPONSE: + case stateReadResponse: response = buffer; if (response[0] != 5 || response[2] != 0) { _exception = [[OFConnectionFailedException alloc] initWithHost: _host @@ -222,23 +226,23 @@ } /* Skip the rest of the response */ switch (response[3]) { case 1: /* IPv4 */ - _SOCKS5State = OF_SOCKS5_STATE_READ_ADDRESS; + _SOCKS5State = stateReadAddress; [_socket asyncReadIntoBuffer: _buffer exactLength: 4 + 2 runLoopMode: runLoopMode]; return false; case 3: /* Domain name */ - _SOCKS5State = OF_SOCKS5_STATE_READ_ADDRESS_LENGTH; + _SOCKS5State = stateReadAddressLength; [_socket asyncReadIntoBuffer: _buffer exactLength: 1 runLoopMode: runLoopMode]; return false; case 4: /* IPv6 */ - _SOCKS5State = OF_SOCKS5_STATE_READ_ADDRESS; + _SOCKS5State = stateReadAddress; [_socket asyncReadIntoBuffer: _buffer exactLength: 16 + 2 runLoopMode: runLoopMode]; return false; default: @@ -250,17 +254,17 @@ [self didConnect]; return false; } return false; - case OF_SOCKS5_STATE_READ_ADDRESS: + case stateReadAddress: [self didConnect]; return false; - case OF_SOCKS5_STATE_READ_ADDRESS_LENGTH: + case stateReadAddressLength: addressLength = buffer; - _SOCKS5State = OF_SOCKS5_STATE_READ_ADDRESS; + _SOCKS5State = stateReadAddress; [_socket asyncReadIntoBuffer: _buffer exactLength: addressLength[0] + 2 runLoopMode: runLoopMode]; return false; default: @@ -272,11 +276,11 @@ - (OFData *)stream: (OFStream *)sock didWriteData: (OFData *)data bytesWritten: (size_t)bytesWritten exception: (id)exception { - of_run_loop_mode_t runLoopMode; + OFRunLoopMode runLoopMode; if (exception != nil) { _exception = [exception retain]; [self didConnect]; return nil; @@ -283,21 +287,21 @@ } runLoopMode = [OFRunLoop currentRunLoop].currentMode; switch (_SOCKS5State) { - case OF_SOCKS5_STATE_SEND_AUTHENTICATION: - _SOCKS5State = OF_SOCKS5_STATE_READ_VERSION; + case stateSendAuthentication: + _SOCKS5State = stateReadVersion; [_socket asyncReadIntoBuffer: _buffer exactLength: 2 runLoopMode: runLoopMode]; return nil; - case OF_SOCKS5_STATE_SEND_REQUEST: + case stateSendRequest: [_request release]; _request = nil; - _SOCKS5State = OF_SOCKS5_STATE_READ_RESPONSE; + _SOCKS5State = stateReadResponse; [_socket asyncReadIntoBuffer: _buffer exactLength: 4 runLoopMode: runLoopMode]; return nil; default: