ObjFW  Diff

Differences From Artifact [b15819e802]:

To Artifact [aaa2b87b7c]:


27
28
29
30
31
32
33

34
35
36
37
38
39
40
#include <string.h>

#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif

#import "OFTCPSocket.h"

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








>







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <string.h>

#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif

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

250
251
252
253
254
255
256
























































257
258
259
260
261
262
263

- (void)dealloc
{
	[_SOCKS5Host release];

	[super dealloc];
}

























































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







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







251
252
253
254
255
256
257
258
259
260
261
262
263
264
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
310
311
312
313
314
315
316
317
318
319
320

- (void)dealloc
{
	[_SOCKS5Host release];

	[super dealloc];
}

- (bool)of_createSocketForAddress: (const of_socket_address_t *)address
			    errNo: (int *)errNo
{
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
	int flags;
#endif

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

	if ((_socket = socket(address->sockaddr.sockaddr.sa_family,
	    SOCK_STREAM | SOCK_CLOEXEC, 0)) == INVALID_SOCKET) {
		*errNo = of_socket_errno();
		return false;
	}

#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
	if ((flags = fcntl(_socket, F_GETFD, 0)) != -1)
		fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
#endif

	return true;
}

- (bool)of_connectSocketToAddress: (const of_socket_address_t *)address
			    errNo: (int *)errNo
{
	if (_socket == INVALID_SOCKET)
		@throw [OFNotOpenException exceptionWithObject: self];

	if (connect(_socket, &address->sockaddr.sockaddr,
	    address->length) != 0) {
		*errNo = of_socket_errno();
		return false;
	}

	return true;
}

- (void)of_closeSocket
{
	closesocket(_socket);
	_socket = INVALID_SOCKET;
}

- (int)of_socketError
{
	int errNo;
	socklen_t len = sizeof(errNo);

	if (getsockopt(_socket, SOL_SOCKET, SO_ERROR, &errNo, &len) != 0)
		return of_socket_errno();

	return errNo;
}

- (void)connectToHost: (OFString *)host
		 port: (uint16_t)port
{
	OFString *destinationHost = host;
	uint16_t destinationPort = port;
	of_resolver_result_t **results, **iter;
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
310
311
		port = _SOCKS5Port;
	}

	results = of_resolve_host(host, port, SOCK_STREAM);

	for (iter = results; *iter != NULL; iter++) {
		of_resolver_result_t *result = *iter;
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
		int flags;
#endif

		if ((_socket = socket(result->family,


		    result->type | SOCK_CLOEXEC,

		    result->protocol)) == INVALID_SOCKET) {


			errNo = of_socket_errno();





			continue;
		}









		_blocking = true;

#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
		if ((flags = fcntl(_socket, F_GETFD, 0)) != -1)
			fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
#endif

		if (connect(_socket, result->address,
		    result->addressLength) == -1) {
			errNo = of_socket_errno();

			closesocket(_socket);
			_socket = INVALID_SOCKET;

			continue;
		}

		break;
	}

	of_resolver_free(results);







<
|
<

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


>
>
>
>
>
>
>
>



<
<
<
<
|
<
<
<
|
<
<
|







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
		port = _SOCKS5Port;
	}

	results = of_resolve_host(host, port, SOCK_STREAM);

	for (iter = results; *iter != NULL; iter++) {
		of_resolver_result_t *result = *iter;

		of_socket_address_t address;


		switch (result->family) {
		case AF_INET:
			address.family = OF_SOCKET_ADDRESS_FAMILY_IPV4;
			break;
		case AF_INET6:
			address.family = OF_SOCKET_ADDRESS_FAMILY_IPV6;
			break;
		default:
			errNo = EAFNOSUPPORT;
			continue;
		}

		if (result->addressLength > sizeof(address)) {
			errNo = EOVERFLOW;
			continue;
		}

		address.length = result->addressLength;
		memcpy(&address.sockaddr.sockaddr, result->address,
		    result->addressLength);

		if (![self of_createSocketForAddress: &address
					       errNo: &errNo])
			continue;

		_blocking = true;





		if (![self of_connectSocketToAddress: &address



					       errNo: &errNo]) {


			[self of_closeSocket];
			continue;
		}

		break;
	}

	of_resolver_free(results);