Index: src/OFKernelEventObserver.m ================================================================== --- src/OFKernelEventObserver.m +++ src/OFKernelEventObserver.m @@ -47,10 +47,11 @@ #import "OFInitializationFailedException.h" #import "OFInvalidArgumentException.h" #import "OFOutOfRangeException.h" +#import "socket.h" #import "socket_helpers.h" enum { QUEUE_ADD = 0, QUEUE_REMOVE = 1, @@ -142,12 +143,12 @@ @throw [OFInitializationFailedException exceptionWithClass: [self class]]; # ifndef __wii__ cancelAddrLen = sizeof(_cancelAddr); - if (getsockname(_cancelFD[0], (struct sockaddr*)&_cancelAddr, - &cancelAddrLen)) + if (of_getsockname(_cancelFD[0], (struct sockaddr*)&_cancelAddr, + &cancelAddrLen) != 0) @throw [OFInitializationFailedException exceptionWithClass: [self class]]; # endif #endif Index: src/OFTCPSocket.m ================================================================== --- src/OFTCPSocket.m +++ src/OFTCPSocket.m @@ -43,12 +43,13 @@ #import "OFNotConnectedException.h" #import "OFNotImplementedException.h" #import "OFOutOfMemoryException.h" #import "OFSetOptionFailedException.h" -#import "resolver.h" +#import "socket.h" #import "socket_helpers.h" +#import "resolver.h" /* References for static linking */ void _references_to_categories_of_OFTCPSocket(void) { _OFTCPSocket_SOCKS5_reference = 1; @@ -442,11 +443,12 @@ if (port > 0) return port; #ifndef __wii__ addrLen = (socklen_t)sizeof(addr.storage); - if (getsockname(_socket, (struct sockaddr*)&addr.storage, &addrLen)) { + if (of_getsockname(_socket, (struct sockaddr*)&addr.storage, + &addrLen) != 0) { int errNo = of_socket_errno(); close(_socket); _socket = INVALID_SOCKET; Index: src/OFUDPSocket.m ================================================================== --- src/OFUDPSocket.m +++ src/OFUDPSocket.m @@ -33,12 +33,13 @@ #import "OFNotConnectedException.h" #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" #import "OFWriteFailedException.h" -#import "resolver.h" +#import "socket.h" #import "socket_helpers.h" +#import "resolver.h" #ifdef __wii__ static uint16_t freePort = 65532; #endif @@ -432,11 +433,12 @@ if (port > 0) return port; #ifndef __wii__ addrLen = (socklen_t)sizeof(addr.storage); - if (getsockname(_socket, (struct sockaddr*)&addr.storage, &addrLen)) { + if (of_getsockname(_socket, (struct sockaddr*)&addr.storage, + &addrLen) != 0) { int errNo = of_socket_errno(); close(_socket); _socket = INVALID_SOCKET; Index: src/socket.h ================================================================== --- src/socket.h +++ src/socket.h @@ -69,8 +69,12 @@ #ifdef __cplusplus extern "C" { #endif extern bool of_socket_init(void); extern int of_socket_errno(void); +# ifndef __wii__ +extern int of_getsockname(int socket, struct sockaddr *restrict address, + socklen_t *restrict address_len); +# endif #ifdef __cplusplus } #endif Index: src/socket.m ================================================================== --- src/socket.m +++ src/socket.m @@ -16,18 +16,20 @@ #include "config.h" #include -/* For some E* -> WSAE* defines */ -#import "OFException.h" +#import "OFException.h" /* For some E* -> WSAE* defines */ +#import "OFLockFailedException.h" +#import "OFUnlockFailedException.h" #import "socket.h" #ifdef OF_HAVE_THREADS # include "threading.h" static of_once_t onceControl = OF_ONCE_INIT; +static of_mutex_t mutex; #endif static bool initialized = false; static void init(void) @@ -39,10 +41,15 @@ return; #elif defined(__wii__) if (net_init() < 0) return; #endif + +#ifdef OF_HAVE_THREADS + if (!of_mutex_new(&mutex)) + return; +#endif initialized = true; } bool @@ -154,5 +161,29 @@ } return 0; #endif } + +#ifndef __wii__ +int +of_getsockname(int socket, struct sockaddr *restrict address, + socklen_t *restrict address_len) +{ + int ret; + +# ifdef OF_HAVE_THREADS + if (!of_mutex_lock(&mutex)) + @throw [OFLockFailedException exception]; + +# endif + + ret = getsockname(socket, address, address_len); + +# ifdef OF_HAVE_THREADS + if (!of_mutex_unlock(&mutex)) + @throw [OFUnlockFailedException exception]; +# endif + + return ret; +} +#endif