@@ -27,18 +27,18 @@ #ifndef INVALID_SOCKET #define INVALID_SOCKET -1 #endif -#ifndef HAVE_GETADDRINFO +#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_THREADSAFE_GETADDRINFO) #import "OFThread.h" static OFMutex *mutex = nil; #endif @implementation OFTCPSocket -#ifndef HAVE_GETADDRINFO +#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_THREADSAFE_GETADDRINFO) + (void)initialize { if (self == [OFTCPSocket class]) mutex = [[OFMutex alloc] init]; } @@ -50,10 +50,14 @@ close(sock); [super dealloc]; } +/* + * FIXME: Maybe we could copy the result of the name lookup and release the + * lock so that we don't keep the lock during connection attemps. + */ - connectToService: (OFString*)service onNode: (OFString*)node { if (sock != INVALID_SOCKET) @throw [OFAlreadyConnectedException newWithClass: isa]; @@ -63,15 +67,23 @@ memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; - if (getaddrinfo([node cString], [service cString], &hints, &res0)) +#ifndef HAVE_THREADSAFE_GETADDRINFO + [mutex lock]; +#endif + + if (getaddrinfo([node cString], [service cString], &hints, &res0)) { +#ifndef HAVE_THREADSAFE_GETADDRINFO + [mutex unlock]; +#endif @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; + } for (res = res0; res != NULL; res = res->ai_next) { if ((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == INVALID_SOCKET) continue; @@ -84,10 +96,14 @@ break; } freeaddrinfo(res0); + +#ifndef HAVE_THREADSAFE_GETADDRINFO + [mutex unlock]; +#endif #else BOOL connected = NO; struct hostent *he; struct servent *se; struct sockaddr_in addr;