︙ | | |
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
-
+
|
_socket = [sock retain];
_host = [host copy];
_port = port;
_SOCKS5Host = [SOCKS5Host copy];
_SOCKS5Port = SOCKS5Port;
_delegate = [delegate retain];
[_socket setDelegate: self];
_socket.delegate = self;
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
|
︙ | | |
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
|
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
|
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
|
return self;
}
#endif
- (void)dealloc
{
#ifdef OF_HAVE_BLOCKS
if (_block != NULL)
if (_block == NULL)
#endif
if ([_socket delegate] == self)
[_socket setDelegate: _delegate];
if (_socket.delegate == self)
_socket.delegate = _delegate;
[_socket release];
[_host release];
[_SOCKS5Host release];
[_delegate release];
#ifdef OF_HAVE_BLOCKS
[_block release];
#endif
[_exception release];
[_socketAddresses release];
[_request release];
[super dealloc];
}
- (void)didConnect
{
if (_exception == nil)
[_socket setBlocking: true];
_socket.blocking = true;
#ifdef OF_HAVE_BLOCKS
if (_block != NULL)
_block(_socket, _exception);
else {
#endif
[_socket setDelegate: _delegate];
_socket.delegate = _delegate;
if ([_delegate respondsToSelector:
@selector(socket:didConnectToHost:port:exception:)])
[_delegate socket: _socket
didConnectToHost: _host
port: _port
exception: _exception];
#ifdef OF_HAVE_BLOCKS
}
#endif
}
- (void)of_socketDidConnect: (OF_KINDOF(OFTCPSocket *))sock
- (void)of_socketDidConnect: (OFTCPSocket *)sock
exception: (id)exception
{
if (exception != nil) {
if (_socketAddressesIndex >= [_socketAddresses count]) {
if (_socketAddressesIndex >= _socketAddresses.count) {
_exception = [exception retain];
[self didConnect];
} else {
[self tryNextAddressWithRunLoopMode:
[[OFRunLoop currentRunLoop] currentMode]];
[OFRunLoop currentRunLoop].currentMode];
}
return;
}
if (_SOCKS5Host != nil)
[self sendSOCKS5Request];
|
︙ | | |
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
-
+
|
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]) {
if (_socketAddressesIndex >= _socketAddresses.count) {
_exception = [[OFConnectionFailedException alloc]
initWithHost: _host
port: _port
socket: _socket
errNo: errNo];
[self didConnect];
return;
|
︙ | | |
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
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
|
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
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
|
-
+
-
+
-
+
-
+
-
+
-
+
|
* Additionally, on Wii, there is no getsockopt(), so it would not be
* possible to get the error (or success) after connecting anyway.
*
* So for now, connecting is blocking on Wii and 3DS.
*
* FIXME: Use a different thread as a work around.
*/
[_socket setBlocking: true];
_socket.blocking = true;
#else
[_socket setBlocking: false];
_socket.blocking = false;
#endif
if (![_socket of_connectSocketToAddress: &address
errNo: &errNo]) {
#if !defined(OF_NINTENDO_3DS) && !defined(OF_WII)
if (errNo == EINPROGRESS) {
[OFRunLoop of_addAsyncConnectForTCPSocket: _socket
mode: runLoopMode
delegate: self];
return;
} else {
#endif
[_socket of_closeSocket];
if (_socketAddressesIndex >= [_socketAddresses count]) {
if (_socketAddressesIndex >= _socketAddresses.count) {
_exception = [[OFConnectionFailedException
alloc] initWithHost: _host
port: _port
socket: _socket
errNo: errNo];
[self didConnect];
return;
}
[self tryNextAddressWithRunLoopMode: runLoopMode];
return;
#if !defined(OF_NINTENDO_3DS) && !defined(OF_WII)
}
#endif
}
#if defined(OF_NINTENDO_3DS) || defined(OF_WII)
[_socket setBlocking: false];
_socket.blocking = false;
#endif
[self didConnect];
}
- (void)resolver: (OFDNSResolver *)resolver
didResolveDomainName: (OFString *)domainName
socketAddresses: (OFData *)socketAddresses
exception: (id)exception
{
if (exception != nil) {
_exception = [exception retain];
[self didConnect];
return;
}
_socketAddresses = [socketAddresses copy];
[self tryNextAddressWithRunLoopMode:
[[OFRunLoop currentRunLoop] currentMode]];
[OFRunLoop currentRunLoop].currentMode];
}
- (void)startWithRunLoopMode: (of_run_loop_mode_t)runLoopMode
{
OFString *host;
uint16_t port;
if (_SOCKS5Host != nil) {
if ([_host UTF8StringLength] > 255)
if (_host.UTF8StringLength > 255)
@throw [OFOutOfRangeException exception];
host = _SOCKS5Host;
port = _SOCKS5Port;
} else {
host = _host;
port = _port;
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
- (void)sendSOCKS5Request
{
OFData *data = [OFData dataWithItems: "\x05\x01\x00"
count: 3];
_SOCKS5State = SOCKS5_STATE_SEND_AUTHENTICATION;
[_socket asyncWriteData: data
runLoopMode: [[OFRunLoop currentRunLoop] currentMode]];
runLoopMode: [OFRunLoop currentRunLoop].currentMode];
}
- (bool)stream: (OF_KINDOF(OFStream *))sock
- (bool)stream: (OFStream *)sock
didReadIntoBuffer: (void *)buffer
length: (size_t)length
exception: (id)exception
{
of_run_loop_mode_t runLoopMode;
unsigned char *SOCKSVersion;
uint8_t hostLength;
unsigned char port[2];
unsigned char *response, *addressLength;
if (exception != nil) {
_exception = [exception retain];
[self didConnect];
return false;
}
runLoopMode = [[OFRunLoop currentRunLoop] currentMode];
runLoopMode = [OFRunLoop currentRunLoop].currentMode;
switch (_SOCKS5State) {
case SOCKS5_STATE_READ_VERSION:
SOCKSVersion = buffer;
if (SOCKSVersion[0] != 5 || SOCKSVersion[1] != 0) {
_exception = [[OFConnectionFailedException alloc]
|
︙ | | |
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
-
+
-
+
-
+
|
[_request release];
_request = [[OFMutableData alloc] init];
[_request addItems: "\x05\x01\x00\x03"
count: 4];
hostLength = (uint8_t)[_host UTF8StringLength];
hostLength = (uint8_t)_host.UTF8StringLength;
[_request addItem: &hostLength];
[_request addItems: [_host UTF8String]
[_request addItems: _host.UTF8String
count: hostLength];
port[0] = _port >> 8;
port[1] = _port & 0xFF;
[_request addItems: port
count: 2];
count: 2];
_SOCKS5State = SOCKS5_STATE_SEND_REQUEST;
[_socket asyncWriteData: _request
runLoopMode: runLoopMode];
return false;
case SOCKS5_STATE_READ_RESPONSE:
response = buffer;
|
︙ | | |
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
|
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
|
-
+
-
+
|
return false;
default:
assert(0);
return false;
}
}
- (OFData *)stream: (OF_KINDOF(OFStream *))sock
- (OFData *)stream: (OFStream *)sock
didWriteData: (OFData *)data
bytesWritten: (size_t)bytesWritten
exception: (id)exception
{
of_run_loop_mode_t runLoopMode;
if (exception != nil) {
_exception = [exception retain];
[self didConnect];
return nil;
}
runLoopMode = [[OFRunLoop currentRunLoop] currentMode];
runLoopMode = [OFRunLoop currentRunLoop].currentMode;
switch (_SOCKS5State) {
case SOCKS5_STATE_SEND_AUTHENTICATION:
_SOCKS5State = SOCKS5_STATE_READ_VERSION;
[_socket asyncReadIntoBuffer: _buffer
exactLength: 2
runLoopMode: runLoopMode];
|
︙ | | |
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
-
+
|
- (void)dealloc
{
[_exception release];
[super dealloc];
}
- (void)socket: (OF_KINDOF(OFTCPSocket *))sock
- (void)socket: (OFTCPSocket *)sock
didConnectToHost: (OFString *)host
port: (uint16_t)port
exception: (id)exception
{
_done = true;
_exception = [exception retain];
}
|
︙ | | |
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
729
730
731
732
733
734
|
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
729
730
731
732
733
734
|
-
+
-
+
|
{
void *pool = objc_autoreleasePoolPush();
id <OFTCPSocketDelegate> delegate = [_delegate retain];
OFTCPSocket_ConnectDelegate *connectDelegate =
[[[OFTCPSocket_ConnectDelegate alloc] init] autorelease];
OFRunLoop *runLoop = [OFRunLoop currentRunLoop];
[self setDelegate: connectDelegate];
self.delegate = connectDelegate;
[self asyncConnectToHost: host
port: port
runLoopMode: connectRunLoopMode];
while (!connectDelegate->_done)
[runLoop runMode: connectRunLoopMode
beforeDate: nil];
/* Cleanup */
[runLoop runMode: connectRunLoopMode
beforeDate: [OFDate date]];
if (connectDelegate->_exception != nil)
@throw connectDelegate->_exception;
[self setDelegate: delegate];
self.delegate = delegate;
objc_autoreleasePoolPop(pool);
}
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
{
|
︙ | | |