ObjFW  Artifact [55432953fa]

Artifact 55432953fa9972a00dd33326187fce68277511c9f6b6cfbee1ac618918985c21:

  • File src/exceptions/OFAddressTranslationFailedException.m — part of check-in [48980f2297] at 2015-11-29 11:43:05 on branch trunk — Make properties a requirement and clean up code

    This increases the required GCC version from 4.0 to 4.6 (exception:
    Apple GCC, which already supports this with >= 4.0 starting with OS X
    10.5). Since even GCC 4.6 is really old by now, there is no point in
    still supporting something even older and making the code ugly because
    of that. While some hardware and OS support was dropped from GCC 4.6
    compared to GCC 4.0, there is nothing in there that would be an
    interesting target with the exception of BeOS maybe - but a port to BeOS
    can also be achieved using the Haiku support. The other dropped OSes are
    mostly old versions of OSes while newer ones are still being supported
    (and those newer versions of those OSes still support the same
    hardware). (user: js, size: 3755) [annotate] [blame] [check-ins using]


/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
 * the packaging of this file.
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#import "OFAddressTranslationFailedException.h"
#import "OFString.h"

#import "OFInitializationFailedException.h"
#import "OFLockFailedException.h"
#import "OFUnlockFailedException.h"

#include "socket_helpers.h"

#if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS)
# include "threading.h"

static of_mutex_t mutex;
#endif

@implementation OFAddressTranslationFailedException
@synthesize host = _host;

#if !defined(HAVE_THREADSAFE_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;
}

- (instancetype)initWithHost: (OFString*)host
		       error: (int)error
{
	self = [super init];

	@try {
		_host = [host copy];
		_error = error;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (instancetype)initWithError: (int)error
{
	self = [super init];

	_error = error;

	return self;
}

- (void)dealloc
{
	[_host release];

	[super dealloc];
}

- (OFString*)description
{
	/* FIXME: Add proper description for Win32 */
#ifndef OF_WINDOWS
	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 OF_WINDOWS
	}

# ifdef HAVE_GETADDRINFO
#  if defined(OF_HAVE_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
	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, gai_strerror(_error)];
		else
			return [OFString stringWithFormat:
			    @"An address could not be translated: %s",
			    gai_strerror(_error)];
#  if defined(OF_HAVE_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
	} @finally {
		if (!of_mutex_unlock(&mutex))
			@throw [OFUnlockFailedException exception];
	}
#  endif
# 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
}
@end