@@ -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 { @@ -104,21 +113,21 @@ return; } 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; @@ -129,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 @@ -155,14 +164,14 @@ port[0] = _port >> 8; port[1] = _port & 0xFF; [_request addItems: port count: 2]; - _SOCKS5State = OF_SOCKS5_STATE_SEND_REQUEST; + _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 @@ -217,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: @@ -245,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: @@ -267,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; @@ -278,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: