summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Schleifer <js@webkeks.org>2013-06-13 03:30:48 +0200
committerJonathan Schleifer <js@webkeks.org>2013-06-13 03:59:45 +0200
commit095b1f5a7ef94b7aff3d37129a43af4de796d966 (patch)
tree1027ecbca224c3201e565b833ff9ab64b1d2b02e
parent87d3cc9fd107be93b6a41627c0993707d1309ff6 (diff)
Try inet_addr() before gethostbyname().
gethostbyname() is not required to accept an IP address and does not do so on the Wii.
-rw-r--r--src/OFTCPSocket.m96
-rw-r--r--tests/TestsAppDelegate.m2
2 files changed, 65 insertions, 33 deletions
diff --git a/src/OFTCPSocket.m b/src/OFTCPSocket.m
index b7ba01de..d5312b0a 100644
--- a/src/OFTCPSocket.m
+++ b/src/OFTCPSocket.m
@@ -97,6 +97,7 @@ static OFMutex *mutex = nil;
# 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 */
@@ -375,7 +376,42 @@ static uint16_t defaultSOCKS5Port = 1080;
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
@@ -392,10 +428,6 @@ static uint16_t defaultSOCKS5Port = 1080;
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
@@ -562,42 +594,42 @@ static uint16_t defaultSOCKS5Port = 1080;
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) {
+ if ((addr.in.sin_addr.s_addr = inet_addr([host cStringWithEncoding:
+ OF_STRING_ENCODING_NATIVE])) == (in_addr_t)(-1)) {
# ifdef OF_HAVE_THREADS
- [mutex unlock];
+ [mutex lock];
+ @try {
# 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);
-
+ 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
- [mutex unlock];
+ } @finally {
+ [mutex unlock];
+ }
# endif
+ }
+
if ((_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
@throw [OFBindFailedException exceptionWithClass: [self class]
socket: self
diff --git a/tests/TestsAppDelegate.m b/tests/TestsAppDelegate.m
index 0c3e02e7..0b51430d 100644
--- a/tests/TestsAppDelegate.m
+++ b/tests/TestsAppDelegate.m
@@ -91,7 +91,7 @@ main(int argc, char *argv[])
@"\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];