@@ -31,10 +31,11 @@ # include # include #endif #import "OFTCPSocket.h" +#import "OFTCPSocket+SOCKS5.h" #import "OFString.h" #import "OFAcceptFailedException.h" #import "OFAlreadyConnectedException.h" #import "OFAddressTranslationFailedException.h" @@ -41,10 +42,11 @@ #import "OFBindFailedException.h" #import "OFConnectionFailedException.h" #import "OFInvalidArgumentException.h" #import "OFListenFailedException.h" #import "OFNotConnectedException.h" +#import "OFNotImplementedException.h" #import "OFSetOptionFailedException.h" #import "macros.h" #ifndef INVALID_SOCKET @@ -59,10 +61,16 @@ #endif #ifdef _WIN32 # define close(sock) closesocket(sock) #endif + +/* References for static linking */ +void _references_to_categories_of_OFTCPSocket(void) +{ + _OFTCPSocket_SOCKS5_reference = 1; +} @implementation OFTCPSocket #if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) + (void)initialize { @@ -75,20 +83,50 @@ { self = [super init]; sock = INVALID_SOCKET; sockAddr = NULL; + SOCKS5Port = 1080; return self; } + +- (void)setSOCKS5Host: (OFString*)host +{ + OF_SETTER(SOCKS5Host, host, YES, YES) +} + +- (OFString*)SOCKS5Host +{ + OF_GETTER(SOCKS5Host, YES) +} + +- (void)setSOCKS5Port: (uint16_t)port +{ + SOCKS5Port = port; +} + +- (uint16_t)SOCKS5Port +{ + return SOCKS5Port; +} - (void)connectToHost: (OFString*)host port: (uint16_t)port { + OFString *destinationHost = host; + uint16_t destinationPort = port; + if (sock != INVALID_SOCKET) @throw [OFAlreadyConnectedException exceptionWithClass: isa socket: self]; + + if (SOCKS5Host != nil) { + /* Connect to the SOCKS5 proxy instead */ + host = SOCKS5Host; + port = SOCKS5Port; + } #ifdef HAVE_THREADSAFE_GETADDRINFO struct addrinfo hints, *res, *res0; char portCString[7]; @@ -199,10 +237,14 @@ if (sock == INVALID_SOCKET) @throw [OFConnectionFailedException exceptionWithClass: isa socket: self host: host port: port]; + + if (SOCKS5Host != nil) + [self _SOCKS5ConnectToHost: destinationHost + port: destinationPort]; } - (uint16_t)bindToHost: (OFString*)host port: (uint16_t)port { @@ -215,10 +257,14 @@ if (sock != INVALID_SOCKET) @throw [OFAlreadyConnectedException exceptionWithClass: isa socket: self]; + if (SOCKS5Host != nil) + @throw [OFNotImplementedException exceptionWithClass: isa + selector: _cmd]; + #ifdef HAVE_THREADSAFE_GETADDRINFO struct addrinfo hints, *res; char portCString[7]; memset(&hints, 0, sizeof(struct addrinfo));