︙ | | | ︙ | |
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
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
|
|
|
|
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
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)tryNextAddressWithRunLoopMode: (of_run_loop_mode_t)runLoopMode;
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
socketAddresses: (OFData *)socketAddresses
context: (id)context
exception: (id)exception;
- (void)startWithRunLoopMode: (of_run_loop_mode_t)runLoopMode;
- (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
|
︙ | | | ︙ | |
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
|
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);
|
|
>
|
|
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
|
exception: (id)exception
{
if (exception != nil) {
if (_socketAddressesIndex >= [_socketAddresses count]) {
_exception = [exception retain];
[self didConnect];
} else {
[self tryNextAddressWithRunLoopMode:
[[OFRunLoop currentRunLoop] currentMode]];
}
return;
}
if (_SOCKS5Host != nil)
[self sendSOCKS5Request];
else
[self didConnect];
}
- (void)tryNextAddressWithRunLoopMode: (of_run_loop_mode_t)runLoopMode
{
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);
|
︙ | | | ︙ | |
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
|
port: _port
socket: _socket
errNo: errNo];
[self didConnect];
return;
}
[self tryNextAddress];
return;
}
[_socket setBlocking: false];
if (![_socket of_connectSocketToAddress: &address
errNo: &errNo]) {
if (errNo == EINPROGRESS) {
SEL selector = @selector(socketDidConnect:context:
exception:);
of_run_loop_mode_t mode = of_run_loop_mode_default;
[OFRunLoop of_addAsyncConnectForTCPSocket: _socket
mode: mode
target: self
selector: selector
context: nil];
return;
} else {
[_socket of_closeSocket];
if (_socketAddressesIndex >= [_socketAddresses count]) {
_exception = [[OFConnectionFailedException
alloc] initWithHost: _host
port: _port
socket: _socket
errNo: errNo];
[self didConnect];
return;
}
[self tryNextAddress];
return;
}
}
[self didConnect];
}
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
socketAddresses: (OFData *)socketAddresses
context: (id)context
exception: (id)exception
{
if (exception != nil) {
_exception = [exception retain];
[self didConnect];
return;
}
_socketAddresses = [socketAddresses copy];
[self tryNextAddress];
}
- (void)start
{
OFString *host;
uint16_t port;
if (_SOCKS5Host != nil) {
if ([_host UTF8StringLength] > 255)
@throw [OFOutOfRangeException exception];
|
|
<
|
|
>
|
>
|
|
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
|
port: _port
socket: _socket
errNo: errNo];
[self didConnect];
return;
}
[self tryNextAddressWithRunLoopMode: runLoopMode];
return;
}
[_socket setBlocking: false];
if (![_socket of_connectSocketToAddress: &address
errNo: &errNo]) {
if (errNo == EINPROGRESS) {
SEL selector = @selector(socketDidConnect:context:
exception:);
[OFRunLoop of_addAsyncConnectForTCPSocket: _socket
mode: runLoopMode
target: self
selector: selector
context: nil];
return;
} else {
[_socket of_closeSocket];
if (_socketAddressesIndex >= [_socketAddresses count]) {
_exception = [[OFConnectionFailedException
alloc] initWithHost: _host
port: _port
socket: _socket
errNo: errNo];
[self didConnect];
return;
}
[self tryNextAddressWithRunLoopMode: runLoopMode];
return;
}
}
[self didConnect];
}
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
socketAddresses: (OFData *)socketAddresses
context: (id)context
exception: (id)exception
{
if (exception != nil) {
_exception = [exception retain];
[self didConnect];
return;
}
_socketAddresses = [socketAddresses copy];
[self tryNextAddressWithRunLoopMode:
[[OFRunLoop currentRunLoop] currentMode]];
}
- (void)startWithRunLoopMode: (of_run_loop_mode_t)runLoopMode
{
OFString *host;
uint16_t port;
if (_SOCKS5Host != nil) {
if ([_host UTF8StringLength] > 255)
@throw [OFOutOfRangeException exception];
|
︙ | | | ︙ | |
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
|
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;
}
|
|
>
>
>
>
|
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
|
of_socket_address_parse_ip(host, port);
_socketAddresses = [[OFData alloc]
initWithItems: &address
itemSize: sizeof(address)
count: 1];
[self tryNextAddressWithRunLoopMode: runLoopMode];
return;
} @catch (OFInvalidFormatException *e) {
}
[[OFThread DNSResolver]
asyncResolveSocketAddressesForHost: host
addressFamily: OF_SOCKET_ADDRESS_FAMILY_ANY
runLoopMode: runLoopMode
target: self
selector: @selector(resolver:
didResolveDomainName:
socketAddresses:context:
exception:)
context: nil];
}
- (void)sendSOCKS5Request
{
[_socket asyncWriteBuffer: "\x05\x01\x00"
length: 3
runLoopMode: [[OFRunLoop currentRunLoop] currentMode]
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
runLoopMode: [[OFRunLoop currentRunLoop] currentMode]
target: self
selector: @selector(socket:didReadSOCKSVersion:
length:context:exception:)
context: nil];
return 0;
}
|
︙ | | | ︙ | |
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
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;
}
|
>
|
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
port[1] = _port & 0xFF;
[request addItems: port
count: 2];
/* Use request as context to retain it */
[_socket asyncWriteBuffer: [request items]
length: [request count]
runLoopMode: [[OFRunLoop currentRunLoop] currentMode]
target: self
selector: @selector(socket:didSendSOCKS5Request:
bytesWritten:context:exception:)
context: request];
return false;
}
|
︙ | | | ︙ | |
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
|
_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) {
|
>
>
>
|
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
|
_exception = [exception retain];
[self didConnect];
return 0;
}
[_socket asyncReadIntoBuffer: _buffer
exactLength: 4
runLoopMode: [[OFRunLoop currentRunLoop] currentMode]
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
{
of_run_loop_mode_t runLoopMode;
if (exception != nil) {
_exception = [exception retain];
[self didConnect];
return false;
}
if (response[0] != 5 || response[2] != 0) {
|
︙ | | | ︙ | |
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
|
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:
|
>
>
>
>
>
|
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
|
port: _port
socket: _socket
errNo: errNo];
[self didConnect];
return false;
}
runLoopMode = [[OFRunLoop currentRunLoop] currentMode];
/* Skip the rest of the response */
switch (response[3]) {
case 1: /* IPv4 */
[_socket asyncReadIntoBuffer: _buffer
exactLength: 4 + 2
runLoopMode: runLoopMode
target: self
selector: @selector(socket:
didReadSOCKS5Address:length:
context:exception:)
context: nil];
return false;
case 3: /* Domain name */
[_socket asyncReadIntoBuffer: _buffer
exactLength: 1
runLoopMode: runLoopMode
target: self
selector: @selector(socket:
didReadSOCKS5AddressLength:
length:context:exception:)
context: nil];
return false;
case 4: /* IPv6 */
[_socket asyncReadIntoBuffer: _buffer
exactLength: 16 + 2
runLoopMode: runLoopMode
target: self
selector: @selector(socket:
didReadSOCKS5Address:length:
context:exception:)
context: nil];
return false;
default:
|
︙ | | | ︙ | |
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
_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
|
>
|
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
_exception = [exception retain];
[self didConnect];
return false;
}
[_socket asyncReadIntoBuffer: _buffer
exactLength: addressLength[0] + 2
runLoopMode: [[OFRunLoop currentRunLoop] currentMode]
target: self
selector: @selector(socket:didReadSOCKS5Address:
length:context:exception:)
context: nil];
return false;
}
@end
|
︙ | | | ︙ | |
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
840
841
842
|
}
- (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
port: (uint16_t)port
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
|
}
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
target: (id)target
selector: (SEL)selector
context: (id)context
{
[self asyncConnectToHost: host
port: port
runLoopMode: of_run_loop_mode_default
target: target
selector: selector
context: context];
}
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
runLoopMode: (of_run_loop_mode_t)runLoopMode
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]
startWithRunLoopMode: runLoopMode];
objc_autoreleasePoolPop(pool);
}
#ifdef OF_HAVE_BLOCKS
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
block: (of_tcp_socket_async_connect_block_t)block
{
[self asyncConnectToHost: host
port: port
runLoopMode: of_run_loop_mode_default
block: block];
}
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
runLoopMode: (of_run_loop_mode_t)runLoopMode
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]
startWithRunLoopMode: runLoopMode];
objc_autoreleasePoolPop(pool);
}
#endif
- (uint16_t)bindToHost: (OFString *)host
port: (uint16_t)port
|
︙ | | | ︙ | |
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
|
return client;
}
- (void)asyncAcceptWithTarget: (id)target
selector: (SEL)selector
context: (id)context
{
[OFRunLoop of_addAsyncAcceptForTCPSocket: self
mode: of_run_loop_mode_default
target: target
selector: selector
context: context];
}
#ifdef OF_HAVE_BLOCKS
- (void)asyncAcceptWithBlock: (of_tcp_socket_async_accept_block_t)block
{
[OFRunLoop of_addAsyncAcceptForTCPSocket: self
mode: of_run_loop_mode_default
block: block];
}
#endif
- (const of_socket_address_t *)remoteAddress
{
if (_socket == INVALID_SOCKET)
|
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
|
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
|
return client;
}
- (void)asyncAcceptWithTarget: (id)target
selector: (SEL)selector
context: (id)context
{
[self asyncAcceptWithRunLoopMode: of_run_loop_mode_default
target: target
selector: selector
context: context];
}
- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode
target: (id)target
selector: (SEL)selector
context: (id)context
{
[OFRunLoop of_addAsyncAcceptForTCPSocket: self
mode: runLoopMode
target: target
selector: selector
context: context];
}
#ifdef OF_HAVE_BLOCKS
- (void)asyncAcceptWithBlock: (of_tcp_socket_async_accept_block_t)block
{
[self asyncAcceptWithRunLoopMode: of_run_loop_mode_default
block: block];
}
- (void)asyncAcceptWithRunLoopMode: (of_run_loop_mode_t)runLoopMode
block: (of_tcp_socket_async_accept_block_t)block
{
[OFRunLoop of_addAsyncAcceptForTCPSocket: self
mode: runLoopMode
block: block];
}
#endif
- (const of_socket_address_t *)remoteAddress
{
if (_socket == INVALID_SOCKET)
|
︙ | | | ︙ | |