@@ -17,31 +17,87 @@ #include "config.h" #import "OFAddressTranslationFailedException.h" #import "OFString.h" -#import "common.h" +#import "OFInitializationFailedException.h" +#import "OFLockFailedException.h" +#import "OFUnlockFailedException.h" + +#include "socket_helpers.h" + +#if !defined(HAVE_GETADDRINFO) && defined(OF_HAVE_THREADS) +# include "threading.h" + +static of_mutex_t mutex; +#endif @implementation OFAddressTranslationFailedException +#if !defined(HAVE_GETADDRINFO) && defined(OF_HAVE_THREADS) ++ (void)initialize +{ + if (self != [OFAddressTranslationFailedException class]) + return; + + if (!of_mutex_new(&mutex)) + @throw [OFInitializationFailedException exception]; +} +#endif + + (instancetype)exceptionWithHost: (OFString*)host { return [[[self alloc] initWithHost: host] autorelease]; } ++ (instancetype)exceptionWithHost: (OFString*)host + error: (int)error +{ + return [[[self alloc] initWithHost: host + error: error] autorelease]; +} + ++ (instancetype)exceptionWithError: (int)error +{ + return [[[self alloc] initWithError: error] autorelease]; +} + +- initWithHost: (OFString*)host +{ + self = [super init]; + + @try { + _host = [host copy]; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} - initWithHost: (OFString*)host + error: (int)error { self = [super init]; @try { - _host = [host copy]; - _errNo = GET_AT_ERRNO; + _host = [host copy]; + _error = error; } @catch (id e) { [self release]; @throw e; } + return self; +} + +- initWithError: (int)error +{ + self = [super init]; + + _error = error; + return self; } - (void)dealloc { @@ -50,32 +106,59 @@ [super dealloc]; } - (OFString*)description { + /* FIXME: Add proper description for Win32 */ +#ifndef _WIN32 + if (_error == 0) { +#endif + if (_host != nil) + return [OFString stringWithFormat: + @"The host %@ could not be translated to an " + @"address!", + _host]; + else + return @"An address could not be translated!"; +#ifndef _WIN32 + } + +# ifdef HAVE_GETADDRINFO if (_host != nil) return [OFString stringWithFormat: - @"The host %@ could not be translated to an address. This " - @"means that either the host was not found, there was a " - @"problem with the name server, there was a problem with " - @"your network connection or you specified an invalid " - @"host. " ERRFMT, _host, AT_ERRPARAM]; + @"The host %@ could not be translated to an address: %s", + _host, gai_strerror(_error)]; else return [OFString stringWithFormat: - @"An address could not be translated! " ERRFMT, - AT_ERRPARAM]; + @"An address could not be translated: %s", + gai_strerror(_error)]; +# else +# ifdef OF_HAVE_THREADS + if (!of_mutex_lock(&mutex)) + @throw [OFLockFailedException exception]; + + @try { +# endif + if (_host != nil) + return [OFString stringWithFormat: + @"The host %@ could not be translated to an " + "address: %s", + _host, hstrerror(_error)]; + else + return [OFString stringWithFormat: + @"An address could not be translated: %s", + hstrerror(_error)]; +# ifdef OF_HAVE_THREADS + } @finally { + if (!of_mutex_unlock(&mutex)) + @throw [OFUnlockFailedException exception]; + } +# endif +# endif +#endif } - (OFString*)host { OF_GETTER(_host, true) } - -- (int)errNo -{ -#ifdef _WIN32 - return of_wsaerr_to_errno(_errNo); -#else - return _errNo; -#endif -} @end