ObjFW  Check-in [49b1989b63]

Overview
Comment:Move resolving functionality to resolver.m

This is in preparation for UDP sockets, so that they can share the
resolving functionality with TCP sockets.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 49b1989b63e4c6a14b47e456f88433cef5502ff5d4441481325203bf0ccbe7b5
User & Date: js on 2014-01-26 22:38:28
Other Links: manifest | tags
Context
2014-01-27
11:35
Generalize forgotten socket related exceptions check-in: be1fb59dfa user: js tags: trunk
2014-01-26
22:38
Move resolving functionality to resolver.m check-in: 49b1989b63 user: js tags: trunk
13:32
Include unistd.h in socket_helpers.h check-in: 32ad39007a user: js tags: trunk
Changes

Modified ObjFW.xcodeproj/project.pbxproj from [ecac0687dd] to [67cbedc96e].

292
293
294
295
296
297
298


299
300
301
302
303
304
305
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307







+
+







		4B6C8ADC17BD5C2E00B194F2 /* OFThread+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6C8AD517BD5C2E00B194F2 /* OFThread+Private.h */; };
		4B6C8ADD17BD5C2E00B194F2 /* OFTimer+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6C8AD617BD5C2E00B194F2 /* OFTimer+Private.h */; };
		4B6C8ADE17BD5C2E00B194F2 /* OFZIPArchiveEntry+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6C8AD717BD5C2E00B194F2 /* OFZIPArchiveEntry+Private.h */; };
		4B7161AD17A6FC7600B74970 /* OFHTTPResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7161AB17A6FC7600B74970 /* OFHTTPResponse.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B7161AE17A6FC7600B74970 /* OFHTTPResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B7161AC17A6FC7600B74970 /* OFHTTPResponse.m */; };
		4B745BA5168B25E600A6C20E /* OFSystemInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B745BA3168B25E600A6C20E /* OFSystemInfo.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B745BA6168B25E600A6C20E /* OFSystemInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B745BA4168B25E600A6C20E /* OFSystemInfo.m */; };
		4B7769ED1895C07D00D12284 /* resolver.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7769EB1895C07D00D12284 /* resolver.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B7769EE1895C07D00D12284 /* resolver.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B7769EC1895C07D00D12284 /* resolver.m */; };
		4B7DD58218942FE200990FD6 /* socket_helpers.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7DD58118942FE200990FD6 /* socket_helpers.h */; };
		4B7DD5851894358500990FD6 /* OFMoveItemFailedException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7DD5831894358400990FD6 /* OFMoveItemFailedException.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B7DD5861894358500990FD6 /* OFMoveItemFailedException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B7DD5841894358400990FD6 /* OFMoveItemFailedException.m */; };
		4B7DD58818943D4A00990FD6 /* socket.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7DD58718943D4A00990FD6 /* socket.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B7FF3B4133CED6200000324 /* OFConditionStillWaitingException.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B7FF3B2133CED6100000324 /* OFConditionStillWaitingException.h */; settings = {ATTRIBUTES = (Public, ); }; };
		4B7FF3B5133CED6200000324 /* OFConditionStillWaitingException.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B7FF3B3133CED6100000324 /* OFConditionStillWaitingException.m */; };
		4B837D7916829C5F007A3E83 /* block.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B837D7716829C5F007A3E83 /* block.h */; settings = {ATTRIBUTES = (Public, ); }; };
730
731
732
733
734
735
736


737
738
739
740
741
742
743
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747







+
+







		4B6EF6811235358D0076B512 /* TestsAppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TestsAppDelegate.m; path = tests/TestsAppDelegate.m; sourceTree = SOURCE_ROOT; };
		4B6EF684123535B60076B512 /* TestPlugin.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TestPlugin.m; path = tests/plugin/TestPlugin.m; sourceTree = SOURCE_ROOT; };
		4B6EF685123535C80076B512 /* test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = test.m; path = tests/objc_sync/test.m; sourceTree = SOURCE_ROOT; };
		4B7161AB17A6FC7600B74970 /* OFHTTPResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFHTTPResponse.h; path = src/OFHTTPResponse.h; sourceTree = "<group>"; };
		4B7161AC17A6FC7600B74970 /* OFHTTPResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFHTTPResponse.m; path = src/OFHTTPResponse.m; sourceTree = "<group>"; };
		4B745BA3168B25E600A6C20E /* OFSystemInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFSystemInfo.h; path = src/OFSystemInfo.h; sourceTree = "<group>"; };
		4B745BA4168B25E600A6C20E /* OFSystemInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFSystemInfo.m; path = src/OFSystemInfo.m; sourceTree = "<group>"; };
		4B7769EB1895C07D00D12284 /* resolver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = resolver.h; path = src/resolver.h; sourceTree = "<group>"; };
		4B7769EC1895C07D00D12284 /* resolver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = resolver.m; path = src/resolver.m; sourceTree = "<group>"; };
		4B7DD58118942FE200990FD6 /* socket_helpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = socket_helpers.h; path = src/socket_helpers.h; sourceTree = "<group>"; };
		4B7DD5831894358400990FD6 /* OFMoveItemFailedException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFMoveItemFailedException.h; path = src/exceptions/OFMoveItemFailedException.h; sourceTree = "<group>"; };
		4B7DD5841894358400990FD6 /* OFMoveItemFailedException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFMoveItemFailedException.m; path = src/exceptions/OFMoveItemFailedException.m; sourceTree = "<group>"; };
		4B7DD58718943D4A00990FD6 /* socket.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = socket.h; path = src/socket.h; sourceTree = "<group>"; };
		4B7DD58918944A7900990FD6 /* apple-forwarding-arm64.S */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.asm; name = "apple-forwarding-arm64.S"; path = "src/forwarding/apple-forwarding-arm64.S"; sourceTree = "<group>"; };
		4B7FF3B2133CED6100000324 /* OFConditionStillWaitingException.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OFConditionStillWaitingException.h; path = src/exceptions/OFConditionStillWaitingException.h; sourceTree = "<group>"; };
		4B7FF3B3133CED6100000324 /* OFConditionStillWaitingException.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OFConditionStillWaitingException.m; path = src/exceptions/OFConditionStillWaitingException.m; sourceTree = "<group>"; };
1348
1349
1350
1351
1352
1353
1354


1355
1356
1357
1358
1359
1360
1361
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367







+
+







				4BE17AD812FD744C002CEB0B /* foundation-compat.m */,
				4BBA36C511406AB700CBA3AC /* macros.h */,
				4BF1BCBF11C9663F0025511F /* objfw-defs.h.in */,
				4BB50DCF12F863C700C9393F /* of_asprintf.h */,
				4BB50DD012F863C700C9393F /* of_asprintf.m */,
				4BA355BC14879BF700442EF4 /* of_strptime.h */,
				4BA355B914879BDD00442EF4 /* of_strptime.m */,
				4B7769EB1895C07D00D12284 /* resolver.h */,
				4B7769EC1895C07D00D12284 /* resolver.m */,
				4B7DD58718943D4A00990FD6 /* socket.h */,
				4B7DD58118942FE200990FD6 /* socket_helpers.h */,
				4B67998B1099E7C50041064A /* threading.h */,
				4B67998C1099E7C50041064A /* unicode.h */,
				4BFBDD1610A0724800051AFB /* unicode.m */,
				4B6AF97210A8D42E0003FB0A /* windows_1252.m */,
			);
1557
1558
1559
1560
1561
1562
1563

1564
1565
1566
1567
1568
1569
1570
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577







+







				4B3D23E71337FCB000DD29B8 /* base64.h in Headers */,
				4B837D7916829C5F007A3E83 /* block.h in Headers */,
				4B837D7A16829C5F007A3E83 /* instance.h in Headers */,
				4B3D23E81337FCB000DD29B8 /* macros.h in Headers */,
				4BD98C03133814220048DD5B /* objfw-defs.h in Headers */,
				4B3D23E91337FCB000DD29B8 /* of_asprintf.h in Headers */,
				4BA355BD14879BF700442EF4 /* of_strptime.h in Headers */,
				4B7769ED1895C07D00D12284 /* resolver.h in Headers */,
				4B7DD58818943D4A00990FD6 /* socket.h in Headers */,
				4B3D23EA1337FCB000DD29B8 /* threading.h in Headers */,
				4B3D23EB1337FCB000DD29B8 /* unicode.h in Headers */,
				4B90B79E133AD87D00BD33CB /* OFAcceptFailedException.h in Headers */,
				4B90B7A0133AD87D00BD33CB /* OFAddressTranslationFailedException.h in Headers */,
				4B17FF7F133A2D17003E6DCD /* OFAllocFailedException.h in Headers */,
				4B90B78D133AD46700BD33CB /* OFAlreadyConnectedException.h in Headers */,
1925
1926
1927
1928
1929
1930
1931

1932
1933
1934
1935
1936
1937
1938
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946







+







				4B8B025A17BBA7C7009ED983 /* OFZIPArchiveEntry.m in Sources */,
				4B3D23B31337FC0D00DD29B8 /* base64.m in Sources */,
				4BB52CC717B8EA7F00B7EBF5 /* codepage_437.m in Sources */,
				4B3D23B41337FC0D00DD29B8 /* iso_8859_15.m in Sources */,
				4B3D23B51337FC0D00DD29B8 /* foundation-compat.m in Sources */,
				4B3D23EE1337FFD000DD29B8 /* of_asprintf.m in Sources */,
				4BA355BA14879BDD00442EF4 /* of_strptime.m in Sources */,
				4B7769EE1895C07D00D12284 /* resolver.m in Sources */,
				4B3D23B91337FC0D00DD29B8 /* unicode.m in Sources */,
				4B3D23BA1337FC0D00DD29B8 /* windows_1252.m in Sources */,
				4B90B79F133AD87D00BD33CB /* OFAcceptFailedException.m in Sources */,
				4B90B7A1133AD87D00BD33CB /* OFAddressTranslationFailedException.m in Sources */,
				4B17FF80133A2D17003E6DCD /* OFAllocFailedException.m in Sources */,
				4B90B78E133AD46700BD33CB /* OFAlreadyConnectedException.m in Sources */,
				4B90B7A3133AD87D00BD33CB /* OFBindFailedException.m in Sources */,

Modified src/Makefile from [70f0f86643] to [fbe12e8eb2].

81
82
83
84
85
86
87
88


89
90
91
92
93
94
95
81
82
83
84
85
86
87

88
89
90
91
92
93
94
95
96







-
+
+







SRCS_PLUGINS = OFPlugin.m
SRCS_SOCKETS = OFHTTPClient.m		\
	       OFHTTPRequest.m		\
	       OFHTTPResponse.m		\
	       OFHTTPServer.m		\
	       OFStreamObserver.m	\
	       OFStreamSocket.m		\
	       OFTCPSocket.m
	       OFTCPSocket.m		\
	       resolver.m
SRCS_THREADS = OFCondition.m		\
	       OFMutex.m		\
	       OFRecursiveMutex.m	\
	       OFThreadPool.m

INCLUDES_SOCKETS = socket.h
INCLUDES_THREADS = threading.h

Modified src/OFTCPSocket.h from [4b861449bf] to [4d85340515].

53
54
55
56
57
58
59
60
61


62
63
64
65
66
67
68
53
54
55
56
57
58
59


60
61
62
63
64
65
66
67
68







-
-
+
+







 *
 * To connect to a server, create a socket and connect it.
 * To create a server, create a socket, bind it and listen on it.
 */
@interface OFTCPSocket: OFStreamSocket
{
	bool _listening;
	struct sockaddr_storage _sockAddr;
	socklen_t _sockAddrLen;
	struct sockaddr *_address;
	socklen_t _addressLength;
	OFString *_SOCKS5Host;
	uint16_t _SOCKS5Port;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, getter=isListening) bool listening;
@property (copy) OFString *SOCKS5Host;

Modified src/OFTCPSocket.m from [3aa4076627] to [4b99095be9].

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
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







+
+











-






+




+


-
-
-
-
-
-
-








#define _WIN32_WINNT 0x0501
#define __NO_EXT_QNX

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <assert.h>

#import "OFTCPSocket.h"
#import "OFTCPSocket+SOCKS5.h"
#import "OFString.h"
#import "OFThread.h"
#import "OFTimer.h"
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"

#import "OFAcceptFailedException.h"
#import "OFAlreadyConnectedException.h"
#import "OFAddressTranslationFailedException.h"
#import "OFBindFailedException.h"
#import "OFConnectionFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFListenFailedException.h"
#import "OFNotConnectedException.h"
#import "OFNotImplementedException.h"
#import "OFOutOfMemoryException.h"
#import "OFSetOptionFailedException.h"

#import "autorelease.h"
#import "macros.h"
#import "resolver.h"
#import "socket_helpers.h"

#if defined(OF_HAVE_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
# import "OFMutex.h"
# import "OFDataArray.h"

static OFMutex *mutex = nil;
#endif

/* References for static linking */
void _references_to_categories_of_OFTCPSocket(void)
{
	_OFTCPSocket_SOCKS5_reference = 1;
}

Class of_tls_socket_class = Nil;
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
195
196
197
198
199
200
201








202
203
204
205
206
207
208







-
-
-
-
-
-
-
-








	return nil;
}
@end
#endif

@implementation OFTCPSocket
#if defined(OF_HAVE_THREADS) && !defined(HAVE_THREADSAFE_GETADDRINFO)
+ (void)initialize
{
	if (self == [OFTCPSocket class])
		mutex = [[OFMutex alloc] init];
}
#endif

+ (void)setSOCKS5Host: (OFString*)host
{
	id old = defaultSOCKS5Host;
	defaultSOCKS5Host = [host copy];
	[old release];
}

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
223
224
225
226
227
228
229

230
231
232
233
234
235
236







-








- init
{
	self = [super init];

	@try {
		_socket = INVALID_SOCKET;
		_sockAddrLen = sizeof(struct sockaddr_storage);
		_SOCKS5Host = [defaultSOCKS5Host copy];
		_SOCKS5Port = defaultSOCKS5Port;
	} @catch (id e) {
		[self release];
		@throw e;
	}

278
279
280
281
282
283
284

285
286
287
288
289
290
291
292
293
294
295
296
297

298
299
300
301
302
303
304

305
306
307
308

309
310
311
312


313
314
315


316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282



283
284






285




286
287



288
289
290
291

292
293
294
295
296
297
298
299
300
301










302






























































































303
304
305
306
307
308
309







+










-
-
-
+

-
-
-
-
-
-
+
-
-
-
-
+

-
-
-
+
+


-
+
+








-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







}

- (void)connectToHost: (OFString*)host
		 port: (uint16_t)port
{
	OFString *destinationHost = host;
	uint16_t destinationPort = port;
	of_resolver_result_t **results, **iter;

	if (_socket != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException exceptionWithSocket: self];

	if (_SOCKS5Host != nil) {
		/* Connect to the SOCKS5 proxy instead */
		host = _SOCKS5Host;
		port = _SOCKS5Port;
	}

#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints, *res, *res0;
	char portCString[7];
	results = of_resolve_host(host, port, SOCK_STREAM);

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_NUMERICSERV;
	snprintf(portCString, 7, "%" PRIu16, port);

	for (iter = results; *iter != NULL; iter++) {
	if (getaddrinfo([host UTF8String], portCString, &hints, &res0))
		@throw [OFAddressTranslationFailedException
		    exceptionWithHost: host
			       socket: self];
		of_resolver_result_t *result = *iter;

	for (res = res0; res != NULL; res = res->ai_next) {
		if ((_socket = socket(res->ai_family, res->ai_socktype,
		    res->ai_protocol)) == INVALID_SOCKET)
		if ((_socket = socket(result->family, result->type,
		    result->protocol)) == INVALID_SOCKET)
			continue;

		if (connect(_socket, res->ai_addr, res->ai_addrlen) == -1) {
		if (connect(_socket, result->address,
		    result->addressLength) == -1) {
			close(_socket);
			_socket = INVALID_SOCKET;
			continue;
		}

		break;
	}

	freeaddrinfo(res0);
#else
	bool connected = false;
	struct hostent *he;
	struct sockaddr_in addr;
	char **ip;
# ifdef OF_HAVE_THREADS
	OFDataArray *addrlist;
# endif

	of_resolver_free(results);
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_port = OF_BSWAP16_IF_LE(port);

	if ((addr.sin_addr.s_addr = inet_addr([host UTF8String])) !=
	    (in_addr_t)(-1)) {
		if ((_socket = socket(AF_INET, SOCK_STREAM,
		    0)) == INVALID_SOCKET) {
			@throw [OFConnectionFailedException
			    exceptionWithHost: host
					 port: port
				       socket: self];
		}

		if (connect(_socket, (struct sockaddr*)&addr,
		    sizeof(addr)) == -1) {
			close(_socket);
			_socket = INVALID_SOCKET;
			@throw [OFConnectionFailedException
			    exceptionWithHost: host
					 port: port
				       socket: self];
		}

		if (_SOCKS5Host != nil)
			[self OF_SOCKS5ConnectToHost: destinationHost
						port: destinationPort];

		return;
	}

# ifdef OF_HAVE_THREADS
	addrlist = [[OFDataArray alloc] initWithItemSize: sizeof(char**)];
	[mutex lock];
# endif

	if ((he = gethostbyname([host UTF8String])) == NULL) {
# ifdef OF_HAVE_THREADS
		[addrlist release];
		[mutex unlock];
# endif
		@throw [OFAddressTranslationFailedException
		    exceptionWithHost: host
			       socket: self];
	}

	if (he->h_addrtype != AF_INET ||
	    (_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
# ifdef OF_HAVE_THREADS
		[addrlist release];
		[mutex unlock];
# endif
		@throw [OFConnectionFailedException exceptionWithHost: host
								 port: port
							       socket: self];
	}

# ifdef OF_HAVE_THREADS
	@try {
		for (ip = he->h_addr_list; *ip != NULL; ip++)
			[addrlist addItem: ip];

		/* Add the terminating NULL */
		[addrlist addItem: ip];
	} @catch (id e) {
		[addrlist release];
		@throw e;
	} @finally {
		[mutex unlock];
	}

	for (ip = [addrlist items]; *ip != NULL; ip++) {
# else
	for (ip = he->h_addr_list; *ip != NULL; ip++) {
# endif
		memcpy(&addr.sin_addr.s_addr, *ip, he->h_length);

		if (connect(_socket, (struct sockaddr*)&addr,
		    sizeof(addr)) == -1)
			continue;

		connected = true;
		break;
	}

# ifdef OF_HAVE_THREADS
	[addrlist release];
# endif

	if (!connected) {
		close(_socket);
		_socket = INVALID_SOCKET;
	}
#endif

	if (_socket == INVALID_SOCKET)
		@throw [OFConnectionFailedException exceptionWithHost: host
								 port: port
							       socket: self];

	if (_SOCKS5Host != nil)
473
474
475
476
477
478
479

480

481
482
483
484

485
486

487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507

508
509
510
511
512
513

514
515
516
517
518
519
520
521
522
523





524
525
526
527




528
529
530
531
532
533
534
535
536








537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567

568
569
570
571
572

573
574
575
576
577

578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
348
349
350
351
352
353
354
355
356
357
358
359
360

361
362

363
364

365
366
367
368
369
370
371
372
373
374
375
376
377
378
379




380






381










382
383
384
385
386
387



388
389
390
391
392








393
394
395
396
397
398
399
400































401





402





403












404
405
406
407
408
409
410







+

+



-
+

-
+

-















-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+

-
-
-
+
+
+
+

-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-







}
# endif
#endif

- (uint16_t)bindToHost: (OFString*)host
		  port: (uint16_t)port
{
	of_resolver_result_t **results;
	const int one = 1;
#ifndef __wii__
	union {
		struct sockaddr_storage storage;
		struct sockaddr_in in;
#ifdef AF_INET6
# ifdef AF_INET6
		struct sockaddr_in6 in6;
#endif
# endif
	} addr;
#ifndef __wii__
	socklen_t addrLen;
#endif

	if (_socket != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException exceptionWithSocket: self];

	if (_SOCKS5Host != nil)
		@throw [OFNotImplementedException exceptionWithSelector: _cmd
								 object: self];

#ifdef __wii__
	if (port == 0)
		port = freePort--;
#endif

#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints, *res;
	char portCString[7];

	results = of_resolve_host(host, port, SOCK_STREAM);
	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_NUMERICSERV | AI_PASSIVE;
	snprintf(portCString, 7, "%" PRIu16, port);

	@try {
	if (getaddrinfo([host UTF8String], portCString, &hints, &res))
		@throw [OFAddressTranslationFailedException
		    exceptionWithHost: host
			       socket: self];

	if ((_socket = socket(res->ai_family, SOCK_STREAM,
	    0)) == INVALID_SOCKET)
		@throw [OFBindFailedException exceptionWithHost: host
							   port: port
							 socket: self];
		if ((_socket = socket(results[0]->family, results[0]->type,
		    results[0]->protocol)) == INVALID_SOCKET)
			@throw [OFBindFailedException exceptionWithHost: host
								   port: port
								 socket: self];

	if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&one,
	    sizeof(one)))
		@throw [OFSetOptionFailedException exceptionWithStream: self];
		if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR,
		    (const char*)&one, sizeof(one)))
			@throw [OFSetOptionFailedException
			    exceptionWithStream: self];

	if (bind(_socket, res->ai_addr, res->ai_addrlen) == -1) {
		freeaddrinfo(res);
		close(_socket);
		_socket = INVALID_SOCKET;
		@throw [OFBindFailedException exceptionWithHost: host
							   port: port
							 socket: self];
	}
		if (bind(_socket, results[0]->address,
		    results[0]->addressLength) == -1) {
			close(_socket);
			_socket = INVALID_SOCKET;
			@throw [OFBindFailedException exceptionWithHost: host
								   port: port
								 socket: self];
		}

	freeaddrinfo(res);
#else
	memset(&addr, 0, sizeof(addr));
	addr.in.sin_family = AF_INET;
	addr.in.sin_port = OF_BSWAP16_IF_LE(port);

	if ((addr.in.sin_addr.s_addr = inet_addr([host UTF8String])) ==
	    (in_addr_t)(-1)) {
# ifdef OF_HAVE_THREADS
		[mutex lock];
		@try {
# endif
			struct hostent *he;

			if ((he = gethostbyname([host UTF8String])) == NULL)
				@throw [OFAddressTranslationFailedException
				    exceptionWithHost: host
					       socket: self];

			if (he->h_addrtype != AF_INET ||
			    he->h_addr_list[0] == NULL) {
				@throw [OFAddressTranslationFailedException
				    exceptionWithHost: host
					       socket: self];
			}

			memcpy(&addr.in.sin_addr.s_addr, he->h_addr_list[0],
			    he->h_length);
# ifdef OF_HAVE_THREADS
		} @finally {
	} @finally {
			[mutex unlock];
		}
# endif
	}

		of_resolver_free(results);
	if ((_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
		@throw [OFBindFailedException exceptionWithHost: host
							   port: port
							 socket: self];

	}
	if (setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&one,
	    sizeof(one)))
		@throw [OFSetOptionFailedException exceptionWithStream: self];

	if (bind(_socket, (struct sockaddr*)&addr.in, sizeof(addr.in)) == -1) {
		close(_socket);
		_socket = INVALID_SOCKET;
		@throw [OFBindFailedException exceptionWithHost: host
							   port: port
							 socket: self];
	}
#endif

	if (port > 0)
		return port;

#ifndef __wii__
	addrLen = sizeof(addr.storage);
	if (getsockname(_socket, (struct sockaddr*)&addr, &addrLen)) {
633
634
635
636
637
638
639




640

641
642

643












644
645
646
647
648
649
650
447
448
449
450
451
452
453
454
455
456
457

458


459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479







+
+
+
+
-
+
-
-
+

+
+
+
+
+
+
+
+
+
+
+
+







	_listening = true;
}

- (instancetype)accept
{
	OFTCPSocket *client = [[[[self class] alloc] init] autorelease];

	client->_address = [client
	    allocMemoryWithSize: sizeof(struct sockaddr_storage)];
	client->_addressLength = sizeof(struct sockaddr_storage);

	if ((client->_socket = accept(_socket,
	if ((client->_socket = accept(_socket, client->_address,
	    (struct sockaddr*)&client->_sockAddr,
	    &client->_sockAddrLen)) == INVALID_SOCKET)
	   &client->_addressLength)) == INVALID_SOCKET)
		@throw [OFAcceptFailedException exceptionWithSocket: self];

	assert(client->_addressLength <= sizeof(struct sockaddr_storage));

	if (client->_addressLength != sizeof(struct sockaddr_storage)) {
		@try {
			client->_address = [client
			    resizeMemory: client->_address
				    size: client->_addressLength];
		} @catch (OFOutOfMemoryException *e) {
			/* We don't care, as we only made it smaller */
		}
	}

	return client;
}

- (void)asyncAcceptWithTarget: (id)target
		     selector: (SEL)selector
{
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700

701
702
703

704
705
706
707
708

709
710
711

712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
496
497
498
499
500
501
502


503
504
505






















506



507





508



509


510
511
512
513
514
515
516
517
518
519
520
521

522
523







-
-



-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
+
-
-
-
+
-
-












-



	if (setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
		@throw [OFSetOptionFailedException exceptionWithStream: self];
}

- (OFString*)remoteAddress
{
	char *host;

	if (_socket == INVALID_SOCKET)
		@throw [OFNotConnectedException exceptionWithSocket: self];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	host = [self allocMemoryWithSize: NI_MAXHOST];

	@try {
		if (getnameinfo((struct sockaddr*)&_sockAddr, _sockAddrLen,
		    host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV))
			@throw [OFAddressTranslationFailedException
			    exceptionWithSocket: self];

		return [OFString stringWithUTF8String: host];
	} @finally {
		[self freeMemory: host];
	}
#else
# ifdef OF_HAVE_THREADS
	[mutex lock];

	@try {
# endif
		host = inet_ntoa(((struct sockaddr_in*)&_sockAddr)->sin_addr);

		if (host == NULL)
	if (_address == NULL)
			@throw [OFAddressTranslationFailedException
			    exceptionWithSocket: self];

		@throw [OFInvalidArgumentException exception];
		return [OFString stringWithUTF8String: host];
# ifdef OF_HAVE_THREADS
	} @finally {
		[mutex unlock];
	}

# endif
#endif

	return of_address_to_string(_address, _addressLength);
	/* Get rid of a warning, never reached anyway */
	OF_ENSURE(0);
}

- (bool)isListening
{
	return _listening;
}

- (void)close
{
	[super close];

	_listening = false;
	_sockAddrLen = sizeof(struct sockaddr_storage);
}
@end

Modified src/ObjFW.h from [23c3f28047] to [87583c222f].

178
179
180
181
182
183
184



178
179
180
181
182
183
184
185
186
187







+
+
+
#endif

#import "autorelease.h"
#import "asprintf.h"
#import "base64.h"
#import "of_asprintf.h"
#import "of_strptime.h"
#ifdef OF_HAVE_SOCKETS
# import "resolver.h"
#endif

Modified src/exceptions/OFAddressTranslationFailedException.h from [7cb02c2c3b] to [d6b57c8d5d].

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
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







-






-



-
-
-
-
-
-
-
-








-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-








-
-
-
-
-
-
-
-
-
-







-
-
-
-
-
-
-







#endif

/*!
 * @brief An exception indicating the translation of an address failed.
 */
@interface OFAddressTranslationFailedException: OFException
{
	id _socket;
	OFString *_host;
	int _errNo;
}

#ifdef OF_HAVE_PROPERTIES
@property (readonly, copy) OFString *host;
@property (readonly, retain) id socket;
@property (readonly) int errNo;
#endif

/*!
 * @brief Creates a new, autoreleased address translation failed exception.
 *
 * @param socket The socket which could not translate the address
 * @return A new, autoreleased address translation failed exception
 */
+ (instancetype)exceptionWithSocket: (id)socket;

/*!
 * @brief Creates a new, autoreleased address translation failed exception.
 *
 * @param host The host for which translation was requested
 * @return A new, autoreleased address translation failed exception
 */
+ (instancetype)exceptionWithHost: (OFString*)host;

/*!
 * @brief Creates a new, autoreleased address translation failed exception.
 *
 * @param host The host for which translation was requested
 * @param socket The socket which could not translate the address
 * @return A new, autoreleased address translation failed exception
 */
+ (instancetype)exceptionWithHost: (OFString*)host
			   socket: (id)socket;

/*!
 * @brief Initializes an already allocated address translation failed exception.
 *
 * @param socket The socket which could not translate the address
 * @return An initialized address translation failed exception
 */
- initWithSocket: (id)socket;

/*!
 * @brief Initializes an already allocated address translation failed exception.
 *
 * @param host The host for which translation was requested
 * @return An initialized address translation failed exception
 */
- initWithHost: (OFString*)host;

/*!
 * @brief Initializes an already allocated address translation failed exception.
 *
 * @param host The host for which translation was requested
 * @param socket The socket which could not translate the address
 * @return An initialized address translation failed exception
 */
- initWithHost: (OFString*)host
	socket: (id)socket;

/*!
 * @brief Returns the host for which the address translation was requested.
 *
 * @return The host for which the address translation was requested
 */
- (OFString*)host;

/*!
 * @brief Returns the socket which could not translate the address.
 *
 * @return The socket which could not translate the address
 */
- (id)socket;

/*!
 * @brief Returns the errno from when the exception was created.
 *
 * @return The errno from when the exception was created
 */
- (int)errNo;
@end

Modified src/exceptions/OFAddressTranslationFailedException.m from [9b225aa65d] to [978b1ac512].

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
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







-
-
-
-
-





-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-


-
-
-
-
-
-
-



-
+
-
-
+











-






-
+
-
-
-
-
-
-
-
-
-
-
-
-
-

















-
-
-
-
-









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

#import "common.h"
#import "macros.h"

@implementation OFAddressTranslationFailedException
+ (instancetype)exceptionWithSocket: (id)socket
{
	return [[[self alloc] initWithSocket: socket] autorelease];
}

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

+ (instancetype)exceptionWithHost: (OFString*)host
			   socket: (id)socket
{

	return [[[self alloc] initWithHost: host
				    socket: socket] autorelease];
}

- init
{
	OF_INVALID_INIT_METHOD
}

- initWithSocket: (id)socket
{
	return [self initWithHost: nil
			   socket: socket];
}

- initWithHost: (OFString*)host
{
	return [self initWithHost: host
			   socket: nil];
}

- initWithHost: (OFString*)host
	socket: (id)socket
{
	self = [super init];

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

	return self;
}

- (void)dealloc
{
	[_host release];
	[_socket release];

	[super dealloc];
}

- (OFString*)description
{
	if (_host != nil && _socket != nil)
	if (_host != nil)
		return [OFString stringWithFormat:
		    @"The host %@ could not be translated to an address for a "
		    @"socket of type %@. 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,
		    [_socket class], AT_ERRPARAM];
	else if (_socket != nil)
		return [OFString stringWithFormat:
		    @"An address could not be translated for a socket of type "
		    @"%@! " ERRFMT, [_socket class],
		    AT_ERRPARAM];
	else 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)
}

- (id)socket
{
	OF_GETTER(_socket, true)
}

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

Added src/resolver.h version [5eef0c526a].






































































1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014
 *   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.
 */

#import "OFString.h"

#import "socket.h"

/*! @file */

/*!
 * @brief A struct representing one result from the resolver.
 */
typedef struct of_resolver_result_t {
	int family, type, protocol;
	struct sockaddr *address;
	socklen_t addressLength;
	void *private;
} of_resolver_result_t;

#ifdef __cplusplus
extern "C" {
#endif
/*!
 * @brief Resolves the specified host.
 *
 * @param host The host to resolve
 * @param port The port that should be inserted into the resulting address
 *	       struct
 * @param protocol The protocol that should be inserted into the resulting
 *		   address struct
 *
 * @return An array of results. The list is terminated by NULL and should be
 *	   free'd after use.
 */
extern of_resolver_result_t** of_resolve_host(OFString *host, uint16_t port,
    int protocol);

/*!
 * @brief Converts the specified address to a string.
 *
 * @param address The address to convert to a string
 * @param addressLength The length of the address to convert to a string
 * @return The address as a string
 */
extern OFString* of_address_to_string(struct sockaddr *address,
    socklen_t addressLength);

/*!
 * @brief Frees the results returned by @ref of_resolve_host.
 *
 * @param results The results returned by @ref of_resolve_host
 */
extern void of_resolver_free(of_resolver_result_t **results);
#ifdef __cplusplus
}
#endif

Added src/resolver.m version [cc5718b725].






































































































































































































































































1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
/*
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014
 *   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"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>

#import "macros.h"
#import "resolver.h"

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

#import "OFAddressTranslationFailedException.h"
#import "OFInitializationFailedException.h"
#import "OFInvalidArgumentException.h"
#import "OFOutOfMemoryException.h"
#import "OFOutOfRangeException.h"
#if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS)
# import "OFLockFailedException.h"
# import "OFUnlockFailedException.h"
#endif

#import "socket_helpers.h"

#if !defined(HAVE_THREADSAFE_GETADDRINFO) && defined(OF_HAVE_THREADS)
static of_mutex_t mutex;

static void __attribute__((constructor))
init(void)
{
	if (!of_mutex_new(&mutex))
		@throw [OFInitializationFailedException exception];
}
#endif

of_resolver_result_t**
of_resolve_host(OFString *host, uint16_t port, int type)
{
	of_resolver_result_t **ret, **retIter;
	of_resolver_result_t *results, *resultsIter;
	size_t count;
#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints = { 0 }, *res, *res0;
	char portCString[7];

	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = type;
	hints.ai_flags = AI_NUMERICSERV;
	snprintf(portCString, 7, "%" PRIu16, port);

	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;
		resultsIter->private = NULL;

		*retIter = resultsIter;
	}
	*retIter = NULL;

	ret[0]->private = res0;
#else
	struct hostent *he;
	in_addr_t s_addr;
	char **ip;
	struct sockaddr_in *addrs, *addrsIter;

	/*
	 * If the host is an IP address, don't try resolving it.
	 * On the Wii for example, the resolver will return an error if you
	 * specify an IP address.
	 */
	if ((s_addr = inet_addr([host UTF8String])) != INADDR_NONE) {
		of_resolver_result_t *tmp;
		struct sockaddr_in *addr;

		if ((ret = calloc(2, sizeof(*ret))) == NULL)
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: 2 * sizeof(*ret)];

		if ((tmp = malloc(sizeof(*tmp))) == NULL)
			@throw [OFOutOfMemoryException
			    exceptionWithRequestedSize: sizeof(*tmp)];

		if ((addr = calloc(1, sizeof(*addr))) == NULL)
			@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;

		tmp->family = AF_INET;
		tmp->type = type;
		tmp->protocol = 0;
		tmp->address = (struct sockaddr*)addr;
		tmp->addressLength = sizeof(*addr);

		ret[0] = tmp;
		ret[1] = NULL;

		return ret;
	}

# 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];
# endif
#endif

	return ret;
}

OFString*
of_address_to_string(struct sockaddr *address, socklen_t addressLength)
{
#ifdef HAVE_THREADSAFE_GETADDRINFO
	char host[NI_MAXHOST];

	if (getnameinfo(address, addressLength, host, NI_MAXHOST, NULL, 0,
	    NI_NUMERICHOST | NI_NUMERICSERV))
		@throw [OFAddressTranslationFailedException exception];

	return [OFString stringWithUTF8String: host];
#else
	OFString *ret;
	char *host;

	if (address->sa_family != AF_INET)
		@throw [OFInvalidArgumentException exception];

# if OF_HAVE_THREADS
	if (!of_mutex_lock(&mutex))
		@throw [OFLockFailedException exception];
# endif

	host = inet_ntoa(((struct sockaddr_in*)(void*)address)->sin_addr);
	if (host == NULL)
		@throw [OFAddressTranslationFailedException exception];

	ret = [OFString stringWithUTF8String: host];

# if OF_HAVE_THREADS
	if (!of_mutex_unlock(&mutex))
		@throw [OFUnlockFailedException exception];
# endif

	return ret;
#endif
}

void
of_resolver_free(of_resolver_result_t **results)
{
#ifdef HAVE_THREADSAFE_GETADDRINFO
	freeaddrinfo(results[0]->private);
#else
	free(results[0]->address);
#endif
	free(results[0]);
	free(results);
}

Modified src/socket_helpers.h from [7262da62a1] to [6a92767d43].

34
35
36
37
38
39
40




41
42
43
44
45
46
47
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51







+
+
+
+







# ifndef AI_NUMERICSERV
#  define AI_NUMERICSERV 0
# endif
# ifndef AI_NUMERICHOST
#  define AI_NUMERICHOST 0
# endif
#endif

#ifndef INADDR_NONE
# define INADDR_NONE ((in_addr_t)-1)
#endif

#ifndef SOMAXCONN
# define SOMAXCONN 32
#endif

#ifdef _WIN32
# define close(sock) closesocket(sock)