@@ -22,23 +22,24 @@ # endif #endif #import "OFTCPSocket.h" #import "OFExceptions.h" +#import "OFMacros.h" #ifndef INVALID_SOCKET #define INVALID_SOCKET -1 #endif -#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_THREADSAFE_GETADDRINFO) +#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) #import "OFThread.h" static OFMutex *mutex = nil; #endif @implementation OFTCPSocket -#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_THREADSAFE_GETADDRINFO) +#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) + (void)initialize { if (self == [OFTCPSocket class]) mutex = [[OFMutex alloc] init]; } @@ -67,16 +68,16 @@ memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; -#ifndef HAVE_THREADSAFE_GETADDRINFO +#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) [mutex lock]; #endif if (getaddrinfo([node cString], [service cString], &hints, &res0)) { -#ifndef HAVE_THREADSAFE_GETADDRINFO +#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) [mutex unlock]; #endif @throw [OFAddressTranslationFailedException newWithClass: isa node: node @@ -97,11 +98,11 @@ break; } freeaddrinfo(res0); -#ifndef HAVE_THREADSAFE_GETADDRINFO +#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) [mutex unlock]; #endif #else BOOL connected = NO; struct hostent *he; @@ -108,24 +109,30 @@ struct servent *se; struct sockaddr_in addr; uint16_t port; char **ip; +#ifdef OF_THREADS [mutex lock]; +#endif if ((he = gethostbyname([node cString])) == NULL) { +#ifdef OF_THREADS [mutex unlock]; +#endif @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; } if ((se = getservbyname([service cString], "TCP")) != NULL) port = se->s_port; else if ((port = OF_BSWAP16_IF_LE(atoi([service cString]))) == 0) { +#ifdef OF_THREADS [mutex unlock]; +#endif @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; } @@ -134,11 +141,13 @@ addr.sin_family = AF_INET; addr.sin_port = port; if (he->h_addrtype != AF_INET || (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { +#ifdef OF_THREADS [mutex unlock]; +#endif @throw [OFConnectionFailedException newWithClass: isa node: node service: service]; } @@ -151,11 +160,13 @@ connected = YES; break; } +#ifdef OF_THREADS [mutex unlock]; +#endif if (!connected) { close(sock); sock = INVALID_SOCKET; } @@ -187,25 +198,40 @@ memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; - if (getaddrinfo([node cString], [service cString], &hints, &res)) +#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) + [mutex lock]; +#endif + + if (getaddrinfo([node cString], [service cString], &hints, &res)) { +#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) + [mutex unlock]; +#endif @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; + } if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) { freeaddrinfo(res); +#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) + [mutex unlock]; +#endif @throw [OFBindFailedException newWithClass: isa node: node service: service family: family]; } freeaddrinfo(res); + +#if defined(OF_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO) + [mutex unlock]; +#endif #else struct hostent *he; struct servent *se; struct sockaddr_in addr; uint16_t port; @@ -214,24 +240,30 @@ @throw [OFBindFailedException newWithClass: isa node: node service: service family: family]; +#ifdef OF_THREADS [mutex lock]; +#endif if ((he = gethostbyname([node cString])) == NULL) { +#ifdef OF_THREADS [mutex unlock]; +#endif @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; } if ((se = getservbyname([service cString], "TCP")) != NULL) port = se->s_port; else if ((port = OF_BSWAP16_IF_LE(atoi([service cString]))) == 0) { +#ifdef OF_THREADS [mutex unlock]; +#endif @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; } @@ -239,20 +271,24 @@ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = port; if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) { +#ifdef OF_THREADS [mutex unlock]; +#endif @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; } memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length); +#ifdef OF_THREADS [mutex unlock]; +#endif if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) @throw [OFBindFailedException newWithClass: isa node: node service: service