@@ -201,10 +201,18 @@ onNode: (OFString*)node withFamily: (int)family { if (sock != INVALID_SOCKET) @throw [OFAlreadyConnectedException newWithClass: isa]; + +#ifndef HAVE_THREADSAFE_GETADDRINFO + if (family != AF_INET) + @throw [OFBindFailedException newWithClass: isa + node: node + service: service + family: family]; +#endif if ((sock = socket(family, SOCK_STREAM, 0)) == INVALID_SOCKET) @throw [OFBindFailedException newWithClass: isa node: node service: service @@ -215,18 +223,23 @@ memset(&hints, 0, sizeof(struct addrinfo)); hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; - if (getaddrinfo([node cString], [service cString], &hints, &res)) + if (getaddrinfo([node cString], [service cString], &hints, &res)) { + close(sock); + sock = INVALID_SOCKET; @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; + } if (bind(sock, res->ai_addr, res->ai_addrlen) == -1) { freeaddrinfo(res); + close(sock); + sock = INVALID_SOCKET; @throw [OFBindFailedException newWithClass: isa node: node service: service family: family]; } @@ -236,16 +249,10 @@ struct hostent *he; struct servent *se; struct sockaddr_in addr; uint16_t port; - if (family != AF_INET) - @throw [OFBindFailedException newWithClass: isa - node: node - service: service - family: family]; - #ifdef OF_THREADS [mutex lock]; #endif if ((he = gethostbyname([node cString])) == NULL) { @@ -263,10 +270,12 @@ else if ((port = OF_BSWAP16_IF_LE(strtol([service cString], NULL, 10))) == 0) { #ifdef OF_THREADS [mutex unlock]; #endif + close(sock); + sock = INVALID_SOCKET; @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; } @@ -277,10 +286,12 @@ if (he->h_addrtype != AF_INET || he->h_addr_list[0] == NULL) { #ifdef OF_THREADS [mutex unlock]; #endif + close(sock); + sock = INVALID_SOCKET; @throw [OFAddressTranslationFailedException newWithClass: isa node: node service: service]; } @@ -289,15 +300,18 @@ #ifdef OF_THREADS [mutex unlock]; #endif - if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) + if (bind(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1) { + close(sock); + sock = INVALID_SOCKET; @throw [OFBindFailedException newWithClass: isa node: node service: service family: family]; + } #endif return self; }