@@ -22,10 +22,19 @@ #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 @@ -104,11 +113,11 @@ return; } data = [OFData dataWithItems: "\x05\x01\x00" count: 3]; - _SOCKS5State = OFSOCKS5StateSendAuthentication; + _SOCKS5State = stateSendAuthentication; [_socket asyncWriteData: data runLoopMode: [OFRunLoop currentRunLoop].currentMode]; } - (bool)stream: (OFStream *)sock @@ -129,11 +138,11 @@ } runLoopMode = [OFRunLoop currentRunLoop].currentMode; switch (_SOCKS5State) { - case OFSOCKS5StateReadVersion: + 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 = OFSOCKS5StateSendRequest; + _SOCKS5State = stateSendRequest; [_socket asyncWriteData: _request runLoopMode: runLoopMode]; return false; - case OFSOCKS5StateReadResponse: + 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 = OFSOCKS5StateReadAddress; + _SOCKS5State = stateReadAddress; [_socket asyncReadIntoBuffer: _buffer exactLength: 4 + 2 runLoopMode: runLoopMode]; return false; case 3: /* Domain name */ - _SOCKS5State = OFSOCKS5StateReadAddressLength; + _SOCKS5State = stateReadAddressLength; [_socket asyncReadIntoBuffer: _buffer exactLength: 1 runLoopMode: runLoopMode]; return false; case 4: /* IPv6 */ - _SOCKS5State = OFSOCKS5StateReadAddress; + _SOCKS5State = stateReadAddress; [_socket asyncReadIntoBuffer: _buffer exactLength: 16 + 2 runLoopMode: runLoopMode]; return false; default: @@ -245,17 +254,17 @@ [self didConnect]; return false; } return false; - case OFSOCKS5StateReadAddress: + case stateReadAddress: [self didConnect]; return false; - case OFSOCKS5StateReadAddressLength: + case stateReadAddressLength: addressLength = buffer; - _SOCKS5State = OFSOCKS5StateReadAddress; + _SOCKS5State = stateReadAddress; [_socket asyncReadIntoBuffer: _buffer exactLength: addressLength[0] + 2 runLoopMode: runLoopMode]; return false; default: @@ -278,21 +287,21 @@ } runLoopMode = [OFRunLoop currentRunLoop].currentMode; switch (_SOCKS5State) { - case OFSOCKS5StateSendAuthentication: - _SOCKS5State = OFSOCKS5StateReadVersion; + case stateSendAuthentication: + _SOCKS5State = stateReadVersion; [_socket asyncReadIntoBuffer: _buffer exactLength: 2 runLoopMode: runLoopMode]; return nil; - case OFSOCKS5StateSendRequest: + case stateSendRequest: [_request release]; _request = nil; - _SOCKS5State = OFSOCKS5StateReadResponse; + _SOCKS5State = stateReadResponse; [_socket asyncReadIntoBuffer: _buffer exactLength: 4 runLoopMode: runLoopMode]; return nil; default: