@@ -15,11 +15,13 @@ #include #include #ifndef HAVE_GETADDRINFO #include +#ifndef _WIN32 #include +#endif #endif #import "OFTCPSocket.h" #import "OFExceptions.h" @@ -28,19 +30,19 @@ #endif #ifndef HAVE_GETADDRINFO #import "OFThread.h" -static OFObject *lock = nil; +static OFMutex *mutex = nil; #endif @implementation OFTCPSocket #ifndef HAVE_GETADDRINFO + (void)initialize { if (self == [OFTCPSocket class]) - lock = [[OFObject alloc] init]; + mutex = [[OFMutex alloc] init]; } #endif - (void)dealloc { @@ -90,52 +92,58 @@ struct servent *se; struct sockaddr_in addr; uint16_t port; char **ip; - @synchronized (lock) { - if ((he = gethostbyname([node cString])) == NULL) - @throw [OFAddressTranslationFailedException - newWithClass: isa - andNode: node - andService: service]; - - if ((se = getservbyname([service cString], "TCP")) != NULL) - port = se->s_port; - else if ((port = htons(atoi([service cString]))) == 0) - @throw [OFAddressTranslationFailedException - newWithClass: isa - andNode: node - andService: service]; - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = port; - - if (he->h_addrtype != AF_INET || - (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) - @throw [OFConnectionFailedException - newWithClass: isa - andNode: node - andService: service]; - - for (ip = he->h_addr_list; *ip != NULL; ip++) { - memcpy(&addr.sin_addr.s_addr, *ip, he->h_length); - - if (connect(sock, (struct sockaddr*)&addr, - sizeof(addr)) == -1) - continue; - - connected = YES; - - break; - } - - if (!connected) { - close(sock); - sock = INVALID_SOCKET; - } + [mutex lock]; + + if ((he = gethostbyname([node cString])) == NULL) { + [mutex unlock]; + @throw [OFAddressTranslationFailedException + newWithClass: isa + andNode: node + andService: service]; + } + + if ((se = getservbyname([service cString], "TCP")) != NULL) + port = se->s_port; + else if ((port = htons(atoi([service cString]))) == 0) { + [mutex unlock]; + @throw [OFAddressTranslationFailedException + newWithClass: isa + andNode: node + andService: service]; + } + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = port; + + if (he->h_addrtype != AF_INET || + (sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { + [mutex unlock]; + @throw [OFConnectionFailedException + newWithClass: isa + andNode: node + andService: service]; + } + + for (ip = he->h_addr_list; *ip != NULL; ip++) { + memcpy(&addr.sin_addr.s_addr, *ip, he->h_length); + + if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) + continue; + + connected = YES; + break; + } + + [mutex unlock]; + + if (!connected) { + close(sock); + sock = INVALID_SOCKET; } #endif if (sock == INVALID_SOCKET) @throw [OFConnectionFailedException newWithClass: isa @@ -190,43 +198,51 @@ @throw [OFBindFailedException newWithClass: isa andNode: node andService: service andFamily: family]; - @synchronized (lock) { - if ((he = gethostbyname([node cString])) == NULL) - @throw [OFAddressTranslationFailedException - newWithClass: isa - andNode: node - andService: service]; - - if ((se = getservbyname([service cString], "TCP")) != NULL) - port = se->s_port; - else if ((port = htons(atoi([service cString]))) == 0) - @throw [OFAddressTranslationFailedException - newWithClass: isa - andNode: node - andService: service]; - - 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) - @throw [OFAddressTranslationFailedException - newWithClass: isa - andNode: node - andService: service]; - - memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length); - - if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) - @throw [OFBindFailedException newWithClass: isa - andNode: node - andService: service - andFamily: family]; - } + [mutex lock]; + + if ((he = gethostbyname([node cString])) == NULL) { + [mutex unlock]; + @throw [OFAddressTranslationFailedException + newWithClass: isa + andNode: node + andService: service]; + } + + if ((se = getservbyname([service cString], "TCP")) != NULL) + port = se->s_port; + else if ((port = htons(atoi([service cString]))) == 0) { + [mutex unlock]; + @throw [OFAddressTranslationFailedException + newWithClass: isa + andNode: node + andService: service]; + } + + 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) { + [mutex lock]; + @throw [OFAddressTranslationFailedException + newWithClass: isa + andNode: node + andService: service]; + } + + memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length); + + [mutex unlock]; + + if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) + @throw [OFBindFailedException newWithClass: isa + andNode: node + andService: service + andFamily: family]; #endif return self; }