Index: src/exceptions/common.h ================================================================== --- src/exceptions/common.h +++ src/exceptions/common.h @@ -24,21 +24,21 @@ #endif #ifndef _WIN32 # define GET_ERRNO errno # ifdef OF_HAVE_SOCKETS -# if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(HAVE_H_ERRNO) +# if !defined(HAVE_GETADDRINFO) && defined(HAVE_H_ERRNO) # define GET_AT_ERRNO h_errno # else # define GET_AT_ERRNO errno # endif # define GET_SOCK_ERRNO errno # endif # define ERRFMT @"Error string was: %s" # define ERRPARAM strerror(_errNo) # ifdef OF_HAVE_SOCKETS -# if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(HAVE_HSTRERROR) +# if !defined(HAVE_GETADDRINFO) && defined(HAVE_HSTRERROR) # define AT_ERRPARAM hstrerror(_errNo) # else # define AT_ERRPARAM strerror(_errNo) # endif # endif Index: src/resolver.m ================================================================== --- src/resolver.m +++ src/resolver.m @@ -69,51 +69,60 @@ snprintf(portCString, 7, "%" PRIu16, port); # if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS) if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; + + @try { # endif - - if (getaddrinfo([host UTF8String], portCString, &hints, &res0)) - @throw [OFAddressTranslationFailedException - exceptionWithHost: host]; - - count = 0; - for (res = res0; res != NULL; res = res->ai_next) - count++; - - if (count == 0) { - freeaddrinfo(res0); - @throw [OFAddressTranslationFailedException - exceptionWithHost: host]; - } - - if ((ret = calloc(count + 1, sizeof(*ret))) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: (count + 1) * sizeof(*ret)]; - - if ((results = malloc(count * sizeof(*results))) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: count * sizeof(*results)]; - - for (retIter = ret, resultsIter = results, res = res0; - res != NULL; retIter++, resultsIter++, res = res->ai_next) { - resultsIter->family = res->ai_family; - resultsIter->type = res->ai_socktype; - resultsIter->protocol = res->ai_protocol; - resultsIter->address = res->ai_addr; - resultsIter->addressLength = res->ai_addrlen; - - *retIter = resultsIter; - } - *retIter = NULL; - - ret[0]->private_ = res0; - + if (getaddrinfo([host UTF8String], portCString, &hints, &res0)) + @throw [OFAddressTranslationFailedException + exceptionWithHost: host]; + + count = 0; + for (res = res0; res != NULL; res = res->ai_next) + count++; + + if (count == 0) { + freeaddrinfo(res0); + @throw [OFAddressTranslationFailedException + exceptionWithHost: host]; + } + + if ((ret = calloc(count + 1, sizeof(*ret))) == NULL) { + freeaddrinfo(res0); + @throw [OFOutOfMemoryException + exceptionWithRequestedSize: + (count + 1) * sizeof(*ret)]; + } + + if ((results = malloc(count * sizeof(*results))) == NULL) { + freeaddrinfo(res0); + free(ret); + @throw [OFOutOfMemoryException + exceptionWithRequestedSize: + count * sizeof(*results)]; + } + + for (retIter = ret, resultsIter = results, res = res0; + res != NULL; retIter++, resultsIter++, res = res->ai_next) { + resultsIter->family = res->ai_family; + resultsIter->type = res->ai_socktype; + resultsIter->protocol = res->ai_protocol; + resultsIter->address = res->ai_addr; + resultsIter->addressLength = res->ai_addrlen; + + *retIter = resultsIter; + } + *retIter = NULL; + + ret[0]->private_ = res0; # if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS) - if (!of_mutex_unlock(&mutex)) - @throw [OFUnlockFailedException exception]; + } @finally { + if (!of_mutex_unlock(&mutex)) + @throw [OFUnlockFailedException exception]; + } # endif #else struct hostent *he; in_addr_t s_addr; char **ip; @@ -130,17 +139,22 @@ if ((ret = calloc(2, sizeof(*ret))) == NULL) @throw [OFOutOfMemoryException exceptionWithRequestedSize: 2 * sizeof(*ret)]; - if ((tmp = malloc(sizeof(*tmp))) == NULL) + if ((tmp = malloc(sizeof(*tmp))) == NULL) { + free(ret); @throw [OFOutOfMemoryException exceptionWithRequestedSize: sizeof(*tmp)]; + } - if ((addr = calloc(1, sizeof(*addr))) == NULL) + if ((addr = calloc(1, sizeof(*addr))) == NULL) { + free(ret); + free(tmp); @throw [OFOutOfMemoryException exceptionWithRequestedSize: sizeof(*addr)]; + } addr->sin_family = AF_INET; addr->sin_port = OF_BSWAP16_IF_LE(port); addr->sin_addr.s_addr = s_addr; @@ -157,66 +171,69 @@ } # ifdef OF_HAVE_THREADS if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; -# endif - - if ((he = gethostbyname([host UTF8String])) == NULL || - he->h_addrtype != AF_INET) { -# ifdef OF_HAVE_THREADS - if (!of_mutex_unlock(&mutex)) - @throw [OFUnlockFailedException exception]; -# endif - - @throw [OFAddressTranslationFailedException - exceptionWithHost: host]; - } - - count = 0; - for (ip = he->h_addr_list; *ip != NULL; ip++) - count++; - - if (count == 0) - @throw [OFAddressTranslationFailedException - exceptionWithHost: host]; - - if ((ret = calloc(count + 1, sizeof(*ret))) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: (count + 1) * sizeof(*ret)]; - - if ((results = malloc(count * sizeof(*results))) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: count * sizeof(*results)]; - - if ((addrs = calloc(count, sizeof(*addrs))) == NULL) - @throw [OFOutOfMemoryException - exceptionWithRequestedSize: count * sizeof(*addrs)]; - - for (retIter = ret, resultsIter = results, addrsIter = addrs, - ip = he->h_addr_list; *ip != NULL; retIter++, resultsIter++, - addrsIter++, ip++) { - addrsIter->sin_family = he->h_addrtype; - addrsIter->sin_port = OF_BSWAP16_IF_LE(port); - - if (he->h_length > sizeof(addrsIter->sin_addr.s_addr)) - @throw [OFOutOfRangeException exception]; - - memcpy(&addrsIter->sin_addr.s_addr, *ip, he->h_length); - - resultsIter->family = he->h_addrtype; - resultsIter->type = type; - resultsIter->protocol = 0; - resultsIter->address = (struct sockaddr*)addrsIter; - resultsIter->addressLength = sizeof(*addrsIter); - - *retIter = resultsIter; - } - -# ifdef OF_HAVE_THREADS - if (!of_mutex_unlock(&mutex)) - @throw [OFUnlockFailedException exception]; + + @try { +# endif + if ((he = gethostbyname([host UTF8String])) == NULL || + he->h_addrtype != AF_INET) + @throw [OFAddressTranslationFailedException + exceptionWithHost: host]; + + count = 0; + for (ip = he->h_addr_list; *ip != NULL; ip++) + count++; + + if (count == 0) + @throw [OFAddressTranslationFailedException + exceptionWithHost: host]; + + if ((ret = calloc(count + 1, sizeof(*ret))) == NULL) + @throw [OFOutOfMemoryException + exceptionWithRequestedSize: + (count + 1) * sizeof(*ret)]; + + if ((results = malloc(count * sizeof(*results))) == NULL) { + free(ret); + @throw [OFOutOfMemoryException + exceptionWithRequestedSize: + count * sizeof(*results)]; + } + + if ((addrs = calloc(count, sizeof(*addrs))) == NULL) { + free(ret); + free(results); + @throw [OFOutOfMemoryException + exceptionWithRequestedSize: count * sizeof(*addrs)]; + } + + for (retIter = ret, resultsIter = results, addrsIter = addrs, + ip = he->h_addr_list; *ip != NULL; + retIter++, resultsIter++, addrsIter++, ip++) { + addrsIter->sin_family = he->h_addrtype; + addrsIter->sin_port = OF_BSWAP16_IF_LE(port); + + if (he->h_length > sizeof(addrsIter->sin_addr.s_addr)) + @throw [OFOutOfRangeException exception]; + + memcpy(&addrsIter->sin_addr.s_addr, *ip, he->h_length); + + resultsIter->family = he->h_addrtype; + resultsIter->type = type; + resultsIter->protocol = 0; + resultsIter->address = (struct sockaddr*)addrsIter; + resultsIter->addressLength = sizeof(*addrsIter); + + *retIter = resultsIter; + } +# ifdef OF_HAVE_THREADS + } @finally { + if (!of_mutex_unlock(&mutex)) + @throw [OFUnlockFailedException exception]; + } # endif #endif return ret; } @@ -223,69 +240,85 @@ void of_address_to_string_and_port(struct sockaddr *address, socklen_t addressLength, OFString *__autoreleasing *host, uint16_t *port) { -#ifdef HAVE_THREADSAFE_GETADDRINFO +#ifdef HAVE_GETADDRINFO char hostCString[NI_MAXHOST]; char portCString[NI_MAXSERV]; - /* FIXME: Add NI_DGRAM for UDP? */ - if (getnameinfo(address, addressLength, hostCString, NI_MAXHOST, - portCString, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV)) - @throw [OFAddressTranslationFailedException exception]; - - if (host != NULL) - *host = [OFString stringWithUTF8String: hostCString]; - - if (port != NULL) { - char *endptr; - long tmp; - - if ((tmp = strtol(portCString, &endptr, 10)) > UINT16_MAX) - @throw [OFOutOfRangeException exception]; - - if (endptr != NULL && *endptr != '\0') - @throw [OFAddressTranslationFailedException exception]; - - *port = (uint16_t)tmp; - } +# if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS) + if (!of_mutex_lock(&mutex)) + @throw [OFLockFailedException exception]; + + @try { +# endif + /* FIXME: Add NI_DGRAM for UDP? */ + if (getnameinfo(address, addressLength, hostCString, NI_MAXHOST, + portCString, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV)) + @throw [OFAddressTranslationFailedException exception]; + + if (host != NULL) + *host = [OFString stringWithUTF8String: hostCString]; + + if (port != NULL) { + char *endptr; + long tmp; + + if ((tmp = strtol(portCString, &endptr, 10)) > + UINT16_MAX) + @throw [OFOutOfRangeException exception]; + + if (endptr != NULL && *endptr != '\0') + @throw [OFAddressTranslationFailedException + exception]; + + *port = (uint16_t)tmp; + } +# if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS) + } @finally { + if (!of_mutex_unlock(&mutex)) + @throw [OFUnlockFailedException exception]; + } +# endif #else char *hostCString; if (address->sa_family != AF_INET) @throw [OFInvalidArgumentException exception]; # if OF_HAVE_THREADS if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; -# endif - - if ((hostCString = inet_ntoa( - ((struct sockaddr_in*)(void*)address)->sin_addr)) == NULL) - @throw [OFAddressTranslationFailedException exception]; - - if (host != NULL) - *host = [OFString stringWithUTF8String: hostCString]; - - if (port != NULL) - *port = OF_BSWAP16_IF_LE( - ((struct sockaddr_in*)(void*)address)->sin_port); - -# if OF_HAVE_THREADS - if (!of_mutex_unlock(&mutex)) - @throw [OFUnlockFailedException exception]; + + @try { +# endif + if ((hostCString = inet_ntoa( + ((struct sockaddr_in*)(void*)address)->sin_addr)) == NULL) + @throw [OFAddressTranslationFailedException exception]; + + if (host != NULL) + *host = [OFString stringWithUTF8String: hostCString]; + + if (port != NULL) + *port = OF_BSWAP16_IF_LE( + ((struct sockaddr_in*)(void*)address)->sin_port); +# if OF_HAVE_THREADS + } @finally { + if (!of_mutex_unlock(&mutex)) + @throw [OFUnlockFailedException exception]; + } # endif #endif } void of_resolver_free(of_resolver_result_t **results) { -#ifdef HAVE_THREADSAFE_GETADDRINFO +#ifdef HAVE_GETADDRINFO freeaddrinfo(results[0]->private_); #else free(results[0]->address); #endif free(results[0]); free(results); } Index: src/socket_helpers.h ================================================================== --- src/socket_helpers.h +++ src/socket_helpers.h @@ -28,11 +28,11 @@ #ifndef INVALID_SOCKET # define INVALID_SOCKET -1 #endif -#ifdef HAVE_THREADSAFE_GETADDRINFO +#ifdef HAVE_GETADDRINFO # ifndef AI_NUMERICSERV # define AI_NUMERICSERV 0 # endif # ifndef AI_NUMERICHOST # define AI_NUMERICHOST 0