ObjFW  Diff

Differences From Artifact [e3252f358c]:

To Artifact [1abea7673b]:


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
static uint16_t defaultSOCKS5Port = 1080;

@interface OFTCPSocket_ConnectContext: OFObject
{
	OFTCPSocket *_socket;
	OFString *_host;
	uint16_t _port;


	id _target;
	SEL _selector;
	id _context;
#ifdef OF_HAVE_BLOCKS
	of_tcp_socket_async_connect_block_t _block;
#endif
	id _exception;
	OFData *_socketAddresses;
	size_t _socketAddressesIndex;


}

- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port


			target: (id)target
		      selector: (SEL)selector
		       context: (id)context;
#ifdef OF_HAVE_BLOCKS
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port


			 block: (of_tcp_socket_async_connect_block_t)block;
#endif
- (void)didConnect;
- (void)socketDidConnect: (OFTCPSocket *)sock
		 context: (id)context
	       exception: (id)exception;
- (void)tryNextAddress;
-	(void)resolver: (OFDNSResolver *)resolver
  didResolveDomainName: (OFString *)domainName
       socketAddresses: (OFData *)socketAddresses
	       context: (id)context
	     exception: (id)exception;
- (void)start;































@end

@implementation OFTCPSocket_ConnectContext
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port


			target: (id)target
		      selector: (SEL)selector
		       context: (id)context
{
	self = [super init];

	@try {
		_socket = [sock retain];
		_host = [host copy];
		_port = port;


		_target = [target retain];
		_selector = selector;
		_context = [context retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

#ifdef OF_HAVE_BLOCKS
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port


			 block: (of_tcp_socket_async_connect_block_t)block
{
	self = [super init];

	@try {
		_socket = [sock retain];
		_host = [host copy];
		_port = port;


		_block = [block copy];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
#endif

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

	[_target release];
	[_context release];
#ifdef OF_HAVE_BLOCKS
	[_block release];
#endif
	[_exception release];
	[_socketAddresses release];







>
>









>
>





>
>







>
>













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






>
>










>
>















>
>








>
>














>







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
static uint16_t defaultSOCKS5Port = 1080;

@interface OFTCPSocket_ConnectContext: OFObject
{
	OFTCPSocket *_socket;
	OFString *_host;
	uint16_t _port;
	OFString *_SOCKS5Host;
	uint16_t _SOCKS5Port;
	id _target;
	SEL _selector;
	id _context;
#ifdef OF_HAVE_BLOCKS
	of_tcp_socket_async_connect_block_t _block;
#endif
	id _exception;
	OFData *_socketAddresses;
	size_t _socketAddressesIndex;
	/* Longest read is domain name (max 255 bytes) + port */
	unsigned char _buffer[257];
}

- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port
		    SOCKS5Host: (OFString *)SOCKS5Host
		    SOCKS5Port: (uint16_t)SOCKS5Port
			target: (id)target
		      selector: (SEL)selector
		       context: (id)context;
#ifdef OF_HAVE_BLOCKS
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port
		    SOCKS5Host: (OFString *)SOCKS5Host
		    SOCKS5Port: (uint16_t)SOCKS5Port
			 block: (of_tcp_socket_async_connect_block_t)block;
#endif
- (void)didConnect;
- (void)socketDidConnect: (OFTCPSocket *)sock
		 context: (id)context
	       exception: (id)exception;
- (void)tryNextAddress;
-	(void)resolver: (OFDNSResolver *)resolver
  didResolveDomainName: (OFString *)domainName
       socketAddresses: (OFData *)socketAddresses
	       context: (id)context
	     exception: (id)exception;
- (void)start;
- (void)sendSOCKS5Request;
-	       (size_t)socket: (OFTCPSocket *)sock
  didSendSOCKS5Authentication: (const void *)request
		 bytesWritten: (size_t)bytesWritten
		      context: (id)context
		    exception: (id)exception;
-	 (bool)socket: (OFTCPSocket *)sock
  didReadSOCKSVersion: (unsigned char *)SOCKSVersion
	       length: (size_t)length
	      context: (id)context
	    exception: (id)exception;
-	(size_t)socket: (OFTCPSocket *)sock
  didSendSOCKS5Request: (const void *)request
	  bytesWritten: (size_t)bytesWritten
	       context: (id)context
	     exception: (id)exception;
-	   (bool)socket: (OFTCPSocket *)sock
  didReadSOCKS5Response: (unsigned char *)response
		 length: (size_t)length
		context: (id)context
	      exception: (id)exception;
-	  (bool)socket: (OFTCPSocket *)sock
  didReadSOCKS5Address: (unsigned char *)address
		length: (size_t)length
	       context: (id)context
	     exception: (id)exception;
-		(bool)socket: (OFTCPSocket *)sock
  didReadSOCKS5AddressLength: (unsigned char *)addressLength
		      length: (size_t)length
		     context: (id)context
		   exception: (id)exception;
@end

@implementation OFTCPSocket_ConnectContext
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port
		    SOCKS5Host: (OFString *)SOCKS5Host
		    SOCKS5Port: (uint16_t)SOCKS5Port
			target: (id)target
		      selector: (SEL)selector
		       context: (id)context
{
	self = [super init];

	@try {
		_socket = [sock retain];
		_host = [host copy];
		_port = port;
		_SOCKS5Host = [SOCKS5Host copy];
		_SOCKS5Port = SOCKS5Port;
		_target = [target retain];
		_selector = selector;
		_context = [context retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

#ifdef OF_HAVE_BLOCKS
- (instancetype)initWithSocket: (OFTCPSocket *)sock
			  host: (OFString *)host
			  port: (uint16_t)port
		    SOCKS5Host: (OFString *)SOCKS5Host
		    SOCKS5Port: (uint16_t)SOCKS5Port
			 block: (of_tcp_socket_async_connect_block_t)block
{
	self = [super init];

	@try {
		_socket = [sock retain];
		_host = [host copy];
		_port = port;
		_SOCKS5Host = [SOCKS5Host copy];
		_SOCKS5Port = SOCKS5Port;
		_block = [block copy];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}
#endif

- (void)dealloc
{
	[_socket release];
	[_host release];
	[_SOCKS5Host release];
	[_target release];
	[_context release];
#ifdef OF_HAVE_BLOCKS
	[_block release];
#endif
	[_exception release];
	[_socketAddresses release];
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
}

- (void)socketDidConnect: (OFTCPSocket *)sock
		 context: (id)context
	       exception: (id)exception
{
	if (exception != nil) {
		if (_socketAddressesIndex >= [_socketAddresses count])
			_exception = [exception retain];

		else {
			[self tryNextAddress];


			return;
		}
	}



	[self didConnect];
}

- (void)tryNextAddress
{
	of_socket_address_t address = *(const of_socket_address_t *)
	    [_socketAddresses itemAtIndex: _socketAddressesIndex++];
	int errNo;




	of_socket_address_set_port(&address, _port);

	if (![_socket of_createSocketForAddress: &address
					  errNo: &errNo]) {
		if (_socketAddressesIndex >= [_socketAddresses count]) {
			_exception = [[OFConnectionFailedException alloc]
			    initWithHost: _host
				    port: _port







|

>
|

>
>
|
|
|
|
>
>
|








>
>
>
|







240
241
242
243
244
245
246
247
248
249
250
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
}

- (void)socketDidConnect: (OFTCPSocket *)sock
		 context: (id)context
	       exception: (id)exception
{
	if (exception != nil) {
		if (_socketAddressesIndex >= [_socketAddresses count]) {
			_exception = [exception retain];
			[self didConnect];
		} else {
			[self tryNextAddress];
		}

		return;
	}

	if (_SOCKS5Host != nil)
		[self sendSOCKS5Request];
	else
		[self didConnect];
}

- (void)tryNextAddress
{
	of_socket_address_t address = *(const of_socket_address_t *)
	    [_socketAddresses itemAtIndex: _socketAddressesIndex++];
	int errNo;

	if (_SOCKS5Host != nil)
		of_socket_address_set_port(&address, _SOCKS5Port);
	else
		of_socket_address_set_port(&address, _port);

	if (![_socket of_createSocketForAddress: &address
					  errNo: &errNo]) {
		if (_socketAddressesIndex >= [_socketAddresses count]) {
			_exception = [[OFConnectionFailedException alloc]
			    initWithHost: _host
				    port: _port
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

	_socketAddresses = [socketAddresses copy];
	[self tryNextAddress];
}

- (void)start
{














	@try {
		of_socket_address_t address =
		    of_socket_address_parse_ip(_host, _port);

		_socketAddresses = [[OFData alloc]
		    initWithItems: &address
			 itemSize: sizeof(address)
			    count: 1];

		[self tryNextAddress];
		return;
	} @catch (OFInvalidFormatException *e) {
	}

	[[OFThread DNSResolver]
	    asyncResolveSocketAddressesForHost: _host
					target: self
				      selector: @selector(resolver:
						    didResolveDomainName:
						    socketAddresses:context:
						    exception:)
				       context: nil];
}
















































































































































































































































@end

@implementation OFTCPSocket
@synthesize SOCKS5Host = _SOCKS5Host, SOCKS5Port = _SOCKS5Port;

+ (void)setSOCKS5Host: (OFString *)host
{







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


|












|







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







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
435
436
437
438
439
440
441
442
443
444
445
446
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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625

	_socketAddresses = [socketAddresses copy];
	[self tryNextAddress];
}

- (void)start
{
	OFString *host;
	uint16_t port;

	if (_SOCKS5Host != nil) {
		if ([_host UTF8StringLength] > 255)
			@throw [OFOutOfRangeException exception];

		host = _SOCKS5Host;
		port = _SOCKS5Port;
	} else {
		host = _host;
		port = _port;
	}

	@try {
		of_socket_address_t address =
		    of_socket_address_parse_ip(host, port);

		_socketAddresses = [[OFData alloc]
		    initWithItems: &address
			 itemSize: sizeof(address)
			    count: 1];

		[self tryNextAddress];
		return;
	} @catch (OFInvalidFormatException *e) {
	}

	[[OFThread DNSResolver]
	    asyncResolveSocketAddressesForHost: host
					target: self
				      selector: @selector(resolver:
						    didResolveDomainName:
						    socketAddresses:context:
						    exception:)
				       context: nil];
}

- (void)sendSOCKS5Request
{
	[_socket asyncWriteBuffer: "\x05\x01\x00"
			   length: 3
			   target: self
			 selector: @selector(socket:didSendSOCKS5Authentication:
				       bytesWritten:context:exception:)
			  context: nil];
}

-	       (size_t)socket: (OFTCPSocket *)sock
  didSendSOCKS5Authentication: (const void *)request
		 bytesWritten: (size_t)bytesWritten
		      context: (id)context
		    exception: (id)exception
{
	if (exception != nil) {
		_exception = [exception retain];
		[self didConnect];
		return 0;
	}

	[_socket asyncReadIntoBuffer: _buffer
			 exactLength: 2
			      target: self
			    selector: @selector(socket:didReadSOCKSVersion:
					  length:context:exception:)
			     context: nil];

	return 0;
}

-	 (bool)socket: (OFTCPSocket *)sock
  didReadSOCKSVersion: (unsigned char *)SOCKSVersion
	       length: (size_t)length
	      context: (id)context
	    exception: (id)exception
{
	OFMutableData *request;
	uint8_t hostLength;
	unsigned char port[2];

	if (exception != nil) {
		_exception = [exception retain];
		[self didConnect];
		return false;
	}

	if (SOCKSVersion[0] != 5 || SOCKSVersion[1] != 0) {
		_exception = [[OFConnectionFailedException alloc]
		    initWithHost: _host
			    port: _port
			  socket: self
			   errNo: EPROTONOSUPPORT];
		[self didConnect];
		return false;
	}

	request = [OFMutableData data];
	[request addItems: "\x05\x01\x00\x03"
		    count: 4];

	hostLength = (uint8_t)[_host UTF8StringLength];
	[request addItem: &hostLength];
	[request addItems: [_host UTF8String]
		    count: hostLength];

	port[0] = _port >> 8;
	port[1] = _port & 0xFF;
	[request addItems: port
		    count: 2];

	/* Use request as context to retain it */
	[_socket asyncWriteBuffer: [request items]
			   length: [request count]
			   target: self
			 selector: @selector(socket:didSendSOCKS5Request:
				       bytesWritten:context:exception:)
			  context: request];

	return false;
}

-	(size_t)socket: (OFTCPSocket *)sock
  didSendSOCKS5Request: (const void *)request
	  bytesWritten: (size_t)bytesWritten
	       context: (id)context
	     exception: (id)exception
{
	if (exception != nil) {
		_exception = [exception retain];
		[self didConnect];
		return 0;
	}

	[_socket asyncReadIntoBuffer: _buffer
			 exactLength: 4
			      target: self
			    selector: @selector(socket:didReadSOCKS5Response:
					  length:context:exception:)
			     context: nil];

	return 0;
}

-	   (bool)socket: (OFTCPSocket *)sock
  didReadSOCKS5Response: (unsigned char *)response
		 length: (size_t)length
		context: (id)context
	      exception: (id)exception
{
	if (exception != nil) {
		_exception = [exception retain];
		[self didConnect];
		return false;
	}

	if (response[0] != 5 || response[2] != 0) {
		_exception = [[OFConnectionFailedException alloc]
		    initWithHost: _host
			    port: _port
			  socket: self
			   errNo: EPROTONOSUPPORT];
		[self didConnect];
		return false;
	}

	if (response[1] != 0) {
		int errNo;

		switch (response[1]) {
		case 0x02:
			errNo = EACCES;
			break;
		case 0x03:
			errNo = ENETUNREACH;
			break;
		case 0x04:
			errNo = EHOSTUNREACH;
			break;
		case 0x05:
			errNo = ECONNREFUSED;
			break;
		case 0x06:
			errNo = ETIMEDOUT;
			break;
		case 0x07:
			errNo = EPROTONOSUPPORT;
			break;
		case 0x08:
			errNo = EAFNOSUPPORT;
			break;
		default:
			errNo = EPROTO;
			break;
		}

		_exception = [[OFConnectionFailedException alloc]
		    initWithHost: _host
			    port: _port
			  socket: _socket
			   errNo: errNo];
		[self didConnect];
		return false;
	}

	/* Skip the rest of the response */
	switch (response[3]) {
	case 1: /* IPv4 */
		[_socket asyncReadIntoBuffer: _buffer
				 exactLength: 4 + 2
				      target: self
				    selector: @selector(socket:
						  didReadSOCKS5Address:length:
						  context:exception:)
				     context: nil];
		return false;
	case 3: /* Domain name */
		[_socket asyncReadIntoBuffer: _buffer
				 exactLength: 1
				      target: self
				    selector: @selector(socket:
						  didReadSOCKS5AddressLength:
						  length:context:exception:)
				     context: nil];
		return false;
	case 4: /* IPv6 */
		[_socket asyncReadIntoBuffer: _buffer
				 exactLength: 16 + 2
				      target: self
				    selector: @selector(socket:
						  didReadSOCKS5Address:length:
						  context:exception:)
				     context: nil];
		return false;
	default:
		_exception = [[OFConnectionFailedException alloc]
		    initWithHost: _host
			    port: _port
			  socket: self
			   errNo: EPROTONOSUPPORT];
		[self didConnect];
		return false;
	}

	return false;
}

-	  (bool)socket: (OFTCPSocket *)sock
  didReadSOCKS5Address: (unsigned char *)address
		length: (size_t)length
	       context: (id)context
	     exception: (id)exception
{
	_exception = [exception retain];
	[self didConnect];
	return false;
}

-		(bool)socket: (OFTCPSocket *)sock
  didReadSOCKS5AddressLength: (unsigned char *)addressLength
		      length: (size_t)length
		     context: (id)context
		   exception: (id)exception
{
	if (exception != nil) {
		_exception = [exception retain];
		[self didConnect];
		return false;
	}

	[_socket asyncReadIntoBuffer: _buffer
			 exactLength: addressLength[0] + 2
			      target: self
			    selector: @selector(socket:didReadSOCKS5Address:
					  length:context:exception:)
			     context: nil];
	return false;
}
@end

@implementation OFTCPSocket
@synthesize SOCKS5Host = _SOCKS5Host, SOCKS5Port = _SOCKS5Port;

+ (void)setSOCKS5Host: (OFString *)host
{
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

- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
		    target: (id)target
		  selector: (SEL)selector
		   context: (id)context
{
	/* TODO: Support SOCKS5 */
	void *pool = objc_autoreleasePoolPush();

	[[[[OFTCPSocket_ConnectContext alloc]
	    initWithSocket: self
		      host: host
		      port: port


		    target: target
		  selector: selector
		   context: context] autorelease] start];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_BLOCKS
- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
		     block: (of_tcp_socket_async_connect_block_t)block
{
	/* TODO: Support SOCKS5 */
	void *pool = objc_autoreleasePoolPush();

	[[[[OFTCPSocket_ConnectContext alloc]
	    initWithSocket: self
		      host: host
		      port: port


		     block: block] autorelease] start];

	objc_autoreleasePoolPop(pool);
}
#endif

- (uint16_t)bindToHost: (OFString *)host







<






>
>












<






>
>







798
799
800
801
802
803
804

805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824

825
826
827
828
829
830
831
832
833
834
835
836
837
838
839

- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
		    target: (id)target
		  selector: (SEL)selector
		   context: (id)context
{

	void *pool = objc_autoreleasePoolPush();

	[[[[OFTCPSocket_ConnectContext alloc]
	    initWithSocket: self
		      host: host
		      port: port
		SOCKS5Host: _SOCKS5Host
		SOCKS5Port: _SOCKS5Port
		    target: target
		  selector: selector
		   context: context] autorelease] start];

	objc_autoreleasePoolPop(pool);
}

#ifdef OF_HAVE_BLOCKS
- (void)asyncConnectToHost: (OFString *)host
		      port: (uint16_t)port
		     block: (of_tcp_socket_async_connect_block_t)block
{

	void *pool = objc_autoreleasePoolPush();

	[[[[OFTCPSocket_ConnectContext alloc]
	    initWithSocket: self
		      host: host
		      port: port
		SOCKS5Host: _SOCKS5Host
		SOCKS5Port: _SOCKS5Port
		     block: block] autorelease] start];

	objc_autoreleasePoolPop(pool);
}
#endif

- (uint16_t)bindToHost: (OFString *)host