summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Schleifer <js@webkeks.org>2013-06-13 04:01:08 +0200
committerJonathan Schleifer <js@webkeks.org>2013-06-13 04:03:13 +0200
commitf88c58dfacba5fd40ddb9db6aabfb5f19d24fac1 (patch)
treeaf1455c3352579a5c30e41d2c27c4f23707a6f14
parent095b1f5a7ef94b7aff3d37129a43af4de796d966 (diff)
Don't bind to port 0 on the Wii.
Instead, a decreasing variable is used now. This should be safe, as nothing else should be binding ports while homebrew code is running. The only problem that could arise is when code manually binds to very high port numbers. This also solves the problem that getsockname() is not available on the Wii, as we know the port now anyway. And while we're at it, this also adds struct sockaddr_storage.
-rw-r--r--src/OFStreamObserver.m11
-rw-r--r--src/OFTCPSocket.m22
2 files changed, 30 insertions, 3 deletions
diff --git a/src/OFStreamObserver.m b/src/OFStreamObserver.m
index ff1ee014..f67aa1f3 100644
--- a/src/OFStreamObserver.m
+++ b/src/OFStreamObserver.m
@@ -62,7 +62,6 @@
# include <network.h>
# undef BOOL
# define bind(sock, addr, addrlen) net_bind(sock, addr, addrlen)
-# define getsockname(sock, addr, addrlen) net_getsockname(sock, addr, addrlen)
# define sendto(sock, buf, len, flags, addr, addrlen) \
net_sendto(sock, buf, len, flags, addr, addrlen)
# define socket(domain, type, proto) net_socket(domain, type, proto)
@@ -115,7 +114,9 @@ enum {
@try {
#ifndef OF_HAVE_PIPE
struct sockaddr_in cancelAddr2;
+# ifndef __wii__
socklen_t cancelAddrLen;
+# endif
#endif
_readStreams = [[OFMutableArray alloc] init];
@@ -146,17 +147,25 @@ enum {
_cancelAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
cancelAddr2 = _cancelAddr;
+# ifdef __wii__
+ /* The Wii does not accept port 0 as "choose any free port" */
+ _cancelAddr.sin_port = 65533;
+ cancelAddr2.sin_port = 65534;
+# endif
+
if (bind(_cancelFD[0], (struct sockaddr*)&_cancelAddr,
sizeof(_cancelAddr)) || bind(_cancelFD[1],
(struct sockaddr*)&cancelAddr2, sizeof(cancelAddr2)))
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
+# ifndef __wii__
cancelAddrLen = sizeof(_cancelAddr);
if (getsockname(_cancelFD[0], (struct sockaddr*)&_cancelAddr,
&cancelAddrLen))
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
+# endif
#endif
_maxFD = _cancelFD[0];
diff --git a/src/OFTCPSocket.m b/src/OFTCPSocket.m
index d5312b0a..8517286d 100644
--- a/src/OFTCPSocket.m
+++ b/src/OFTCPSocket.m
@@ -92,12 +92,17 @@ static OFMutex *mutex = nil;
# define close(sock) net_close(sock)
# define connect(sock, addr, addrlen) net_connect(sock, addr, addrlen)
# define gethostbyname(name) net_gethostbyname(name)
-# 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;
+
+struct sockaddr_storage {
+ u8 ss_len;
+ u8 ss_family;
+ u8 ss_data[14];
+};
#endif
/* References for static linking */
@@ -111,6 +116,10 @@ Class of_tls_socket_class = Nil;
static OFString *defaultSOCKS5Host = nil;
static uint16_t defaultSOCKS5Port = 1080;
+#ifdef __wii__
+static uint16_t freePort = 65532;
+#endif
+
#ifdef OF_HAVE_THREADS
@interface OFTCPSocket_ConnectThread: OFThread
{
@@ -540,7 +549,9 @@ static uint16_t defaultSOCKS5Port = 1080;
struct sockaddr_in6 in6;
#endif
} addr;
+#ifndef __wii__
socklen_t addrLen;
+#endif
if (_socket != INVALID_SOCKET)
@throw [OFAlreadyConnectedException
@@ -552,6 +563,11 @@ static uint16_t defaultSOCKS5Port = 1080;
exceptionWithClass: [self class]
selector: _cmd];
+#ifdef __wii__
+ if (port == 0)
+ port = freePort--;
+#endif
+
#ifdef HAVE_THREADSAFE_GETADDRINFO
struct addrinfo hints, *res;
char portCString[7];
@@ -655,6 +671,7 @@ static uint16_t defaultSOCKS5Port = 1080;
if (port > 0)
return port;
+#ifndef __wii__
addrLen = sizeof(addr.storage);
if (getsockname(_socket, (struct sockaddr*)&addr, &addrLen)) {
close(_socket);
@@ -667,9 +684,10 @@ static uint16_t defaultSOCKS5Port = 1080;
if (addr.storage.ss_family == AF_INET)
return OF_BSWAP16_IF_LE(addr.in.sin_port);
-#ifdef AF_INET6
+# ifdef AF_INET6
if (addr.storage.ss_family == AF_INET6)
return OF_BSWAP16_IF_LE(addr.in6.sin6_port);
+# endif
#endif
close(_socket);