ObjFW  Diff

Differences From Artifact [77d11f6554]:

To Artifact [f2e2a2e8ce]:

  • File src/exceptions/OFAddressTranslationFailedException.m — part of check-in [62e2de30b9] at 2015-02-16 08:39:17 on branch trunk — Explicitly pass errno to exceptions

    The old behaviour where the exception would access errno directly on
    creation of the exception was very fragile. The two main problems with
    it were that sometimes it would pick up an errno even though none had
    been set and in other cases that when the exception was created errno
    had already been overridden.

    This also greatly increases errno handling on Win32, especially in
    conjunction with sockets. It can still be improved further, though. (user: js, size: 3351) [annotate] [blame] [check-ins using]


15
16
17
18
19
20
21

22

23








24











25
26
27
28
29


30









31















32
33
34
35
36
37
38
39
40
41
42









43
44
45
46
47
48
49
50
51
52
53
54




55
56
57

58




59
60




61
62
63
64





65



















66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 */

#include "config.h"

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


#import "common.h"










@implementation OFAddressTranslationFailedException











+ (instancetype)exceptionWithHost: (OFString*)host
{
	return [[[self alloc] initWithHost: host] autorelease];
}













- initWithHost: (OFString*)host















{
	self = [super init];

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










	return self;
}

- (void)dealloc
{
	[_host release];

	[super dealloc];
}

- (OFString*)description
{




	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];
	else
		return [OFString stringWithFormat:
		    @"An address could not be translated! " ERRFMT,





		    AT_ERRPARAM];



















}

- (OFString*)host
{
	OF_GETTER(_host, true)
}

- (int)errNo
{
#ifdef _WIN32
	return of_wsaerr_to_errno(_errNo);
#else
	return _errNo;
#endif
}
@end







>
|
>

>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>





>
>
|
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|
|





>
>
>
>
>
>
>
>
>












>
>
>
>
|
|
|
>
|
>
>
>
>
|
<
>
>
>
>
|


|
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>






<
<
<
<
<
<
<
<
<

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163









164
 */

#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_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];
		_error = error;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- 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 _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: %s",
		    _host, gai_strerror(_error)];
	else
		return [OFString stringWithFormat:
		    @"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)
}









@end