Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -95,10 +95,11 @@ # define getsockname(sock, addr, addrlen) net_getsockname(sock, addr, addrlen) # define listen(sock, backlog) net_listen(sock, backlog) # define setsockopt(sock, level, name, value, len) \ net_setsockopt(sock, level, name, value, len) # define socket(domain, type, proto) net_socket(domain, type, proto) +typedef u32 in_addr_t; #endif /* References for static linking */ void _references_to_categories_of_OFTCPSocket(void) { @@ -373,11 +374,46 @@ struct hostent *he; struct sockaddr_in addr; char **ip; # ifdef OF_HAVE_THREADS OFDataArray *addrlist; +# endif + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = OF_BSWAP16_IF_LE(port); + + if ((addr.sin_addr.s_addr = inet_addr([host cStringWithEncoding: + OF_STRING_ENCODING_NATIVE])) != (in_addr_t)(-1)) { + if ((_socket = socket(AF_INET, SOCK_STREAM, + 0)) == INVALID_SOCKET) { + @throw [OFConnectionFailedException + exceptionWithClass: [self class] + socket: self + host: host + port: port]; + } + + if (connect(_socket, (struct sockaddr*)&addr, + sizeof(addr)) == -1) { + close(_socket); + _socket = INVALID_SOCKET; + @throw [OFConnectionFailedException + exceptionWithClass: [self class] + socket: self + host: host + port: port]; + } + + if (_SOCKS5Host != nil) + [self OF_SOCKS5ConnectToHost: destinationHost + port: destinationPort]; + + return; + } +# ifdef OF_HAVE_THREADS addrlist = [[OFDataArray alloc] initWithItemSize: sizeof(char**)]; [mutex lock]; # endif if ((he = gethostbyname([host cStringWithEncoding: @@ -390,14 +426,10 @@ exceptionWithClass: [self class] socket: self host: host]; } - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = OF_BSWAP16_IF_LE(port); - if (he->h_addrtype != AF_INET || (_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { # ifdef OF_HAVE_THREADS [addrlist release]; [mutex unlock]; @@ -560,46 +592,46 @@ port: port]; } freeaddrinfo(res); #else - struct hostent *he; - -# ifdef OF_HAVE_THREADS - [mutex lock]; -# endif - - if ((he = gethostbyname([host cStringWithEncoding: - OF_STRING_ENCODING_NATIVE])) == NULL) { -# ifdef OF_HAVE_THREADS - [mutex unlock]; -# endif - @throw [OFAddressTranslationFailedException - exceptionWithClass: [self class] - socket: self - host: host]; - } - memset(&addr, 0, sizeof(addr)); addr.in.sin_family = AF_INET; addr.in.sin_port = OF_BSWAP16_IF_LE(port); - if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) { -# ifdef OF_HAVE_THREADS - [mutex unlock]; -# endif - @throw [OFAddressTranslationFailedException - exceptionWithClass: [self class] - socket: self - host: host]; - } - - memcpy(&addr.in.sin_addr.s_addr, he->h_addr_list[0], he->h_length); - -# ifdef OF_HAVE_THREADS - [mutex unlock]; -# endif + if ((addr.in.sin_addr.s_addr = inet_addr([host cStringWithEncoding: + OF_STRING_ENCODING_NATIVE])) == (in_addr_t)(-1)) { +# ifdef OF_HAVE_THREADS + [mutex lock]; + @try { +# endif + struct hostent *he; + + if ((he = gethostbyname([host cStringWithEncoding: + OF_STRING_ENCODING_NATIVE])) == NULL) + @throw [OFAddressTranslationFailedException + exceptionWithClass: [self class] + socket: self + host: host]; + + if (he->h_addrtype != AF_INET || + he->h_addr_list[0] == NULL) { + @throw [OFAddressTranslationFailedException + exceptionWithClass: [self class] + socket: self + host: host]; + } + + memcpy(&addr.in.sin_addr.s_addr, he->h_addr_list[0], + he->h_length); +# ifdef OF_HAVE_THREADS + } @finally { + [mutex unlock]; + } +# endif + } + if ((_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) @throw [OFBindFailedException exceptionWithClass: [self class] socket: self host: host port: port]; Index: tests/TestsAppDelegate.m ================================================================== --- tests/TestsAppDelegate.m +++ tests/TestsAppDelegate.m @@ -89,11 +89,11 @@ [[OFApplication sharedApplication] delegate]; OFString *string = [OFString stringWithFormat: @"\nRuntime error: Unhandled exception:\n%@\n", e]; OFString *backtrace = [OFString stringWithFormat: @"\nBacktrace:\n %@\n\n", - [[e backtrace] componentsJoinedByString: @"\n "]; + [[e backtrace] componentsJoinedByString: @"\n "]]; [delegate outputString: string inColor: RED]; [delegate outputString: backtrace inColor: RED];