︙ | | | ︙ | |
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
|
#include <string.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#import "OFTCPSocket.h"
#import "OFDNSResolver.h"
#import "OFData.h"
#import "OFDate.h"
#import "OFIPSocketAsyncConnector.h"
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"
#import "OFSocket.h"
#import "OFSocket+Private.h"
#import "OFString.h"
#import "OFTCPSocketSOCKS5Connector.h"
#import "OFThread.h"
#import "OFAlreadyConnectedException.h"
#import "OFBindFailedException.h"
#import "OFGetOptionFailedException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFSetOptionFailedException.h"
static const OFRunLoopMode connectRunLoopMode =
@"OFTCPSocketConnectRunLoopMode";
static OFString *defaultSOCKS5Host = nil;
static uint16_t defaultSOCKS5Port = 1080;
@interface OFTCPSocket () <OFIPSocketAsyncConnecting>
@end
@interface OFTCPSocketConnectDelegate: OFObject <OFTCPSocketDelegate>
{
@public
bool _done;
id _exception;
|
>
<
|
|
|
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
|
#include <string.h>
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#import "OFTCPSocket.h"
#import "OFAsyncIPSocketConnector.h"
#import "OFDNSResolver.h"
#import "OFData.h"
#import "OFDate.h"
#import "OFRunLoop.h"
#import "OFRunLoop+Private.h"
#import "OFSocket.h"
#import "OFSocket+Private.h"
#import "OFString.h"
#import "OFTCPSocketSOCKS5Connector.h"
#import "OFThread.h"
#import "OFAlreadyConnectedException.h"
#import "OFBindIPSocketFailedException.h"
#import "OFGetOptionFailedException.h"
#import "OFNotImplementedException.h"
#import "OFNotOpenException.h"
#import "OFSetOptionFailedException.h"
static const OFRunLoopMode connectRunLoopMode =
@"OFTCPSocketConnectRunLoopMode";
static OFString *defaultSOCKS5Host = nil;
static uint16_t defaultSOCKS5Port = 1080;
@interface OFTCPSocket () <OFAsyncIPSocketConnecting>
@end
@interface OFTCPSocketConnectDelegate: OFObject <OFTCPSocketDelegate>
{
@public
bool _done;
id _exception;
|
︙ | | | ︙ | |
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
|
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
runLoopMode: (OFRunLoopMode)runLoopMode
{
void *pool = objc_autoreleasePoolPush();
id <OFTCPSocketDelegate> delegate;
if (_socket != OFInvalidSocketHandle)
@throw [OFAlreadyConnectedException exceptionWithSocket: self];
if (_SOCKS5Host != nil) {
delegate = [[[OFTCPSocketSOCKS5Connector alloc]
initWithSocket: self
host: host
port: port
delegate: _delegate
#ifdef OF_HAVE_BLOCKS
block: NULL
#endif
] autorelease];
host = _SOCKS5Host;
port = _SOCKS5Port;
} else
delegate = _delegate;
[[[[OFIPSocketAsyncConnector alloc]
initWithSocket: self
host: host
port: port
delegate: delegate
block: NULL
] autorelease] startWithRunLoopMode: runLoopMode];
|
<
<
<
|
|
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
|
- (void)asyncConnectToHost: (OFString *)host
port: (uint16_t)port
runLoopMode: (OFRunLoopMode)runLoopMode
{
void *pool = objc_autoreleasePoolPush();
id <OFTCPSocketDelegate> delegate;
if (_SOCKS5Host != nil) {
delegate = [[[OFTCPSocketSOCKS5Connector alloc]
initWithSocket: self
host: host
port: port
delegate: _delegate
#ifdef OF_HAVE_BLOCKS
block: NULL
#endif
] autorelease];
host = _SOCKS5Host;
port = _SOCKS5Port;
} else
delegate = _delegate;
[[[[OFAsyncIPSocketConnector alloc]
initWithSocket: self
host: host
port: port
delegate: delegate
block: NULL
] autorelease] startWithRunLoopMode: runLoopMode];
|
︙ | | | ︙ | |
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
|
port: (uint16_t)port
runLoopMode: (OFRunLoopMode)runLoopMode
block: (OFTCPSocketAsyncConnectBlock)block
{
void *pool = objc_autoreleasePoolPush();
id <OFTCPSocketDelegate> delegate = nil;
if (_socket != OFInvalidSocketHandle)
@throw [OFAlreadyConnectedException exceptionWithSocket: self];
if (_SOCKS5Host != nil) {
delegate = [[[OFTCPSocketSOCKS5Connector alloc]
initWithSocket: self
host: host
port: port
delegate: nil
block: block] autorelease];
host = _SOCKS5Host;
port = _SOCKS5Port;
}
[[[[OFIPSocketAsyncConnector alloc]
initWithSocket: self
host: host
port: port
delegate: delegate
block: (delegate == nil ? block : NULL)] autorelease]
startWithRunLoopMode: runLoopMode];
|
<
<
<
|
|
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
|
port: (uint16_t)port
runLoopMode: (OFRunLoopMode)runLoopMode
block: (OFTCPSocketAsyncConnectBlock)block
{
void *pool = objc_autoreleasePoolPush();
id <OFTCPSocketDelegate> delegate = nil;
if (_SOCKS5Host != nil) {
delegate = [[[OFTCPSocketSOCKS5Connector alloc]
initWithSocket: self
host: host
port: port
delegate: nil
block: block] autorelease];
host = _SOCKS5Host;
port = _SOCKS5Port;
}
[[[[OFAsyncIPSocketConnector alloc]
initWithSocket: self
host: host
port: port
delegate: delegate
block: (delegate == nil ? block : NULL)] autorelease]
startWithRunLoopMode: runLoopMode];
|
︙ | | | ︙ | |
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
object: self];
socketAddresses = [[OFThread DNSResolver]
resolveAddressesForHost: host
addressFamily: OFSocketAddressFamilyAny];
address = *(OFSocketAddress *)[socketAddresses itemAtIndex: 0];
OFSocketAddressSetPort(&address, port);
if ((_socket = socket(
((struct sockaddr *)&address.sockaddr)->sa_family,
SOCK_STREAM | SOCK_CLOEXEC, 0)) == OFInvalidSocketHandle)
@throw [OFBindFailedException
exceptionWithHost: host
port: port
socket: self
errNo: OFSocketErrNo()];
_canBlock = true;
|
|
|
|
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
object: self];
socketAddresses = [[OFThread DNSResolver]
resolveAddressesForHost: host
addressFamily: OFSocketAddressFamilyAny];
address = *(OFSocketAddress *)[socketAddresses itemAtIndex: 0];
OFSocketAddressSetIPPort(&address, port);
if ((_socket = socket(
((struct sockaddr *)&address.sockaddr)->sa_family,
SOCK_STREAM | SOCK_CLOEXEC, 0)) == OFInvalidSocketHandle)
@throw [OFBindIPSocketFailedException
exceptionWithHost: host
port: port
socket: self
errNo: OFSocketErrNo()];
_canBlock = true;
|
︙ | | | ︙ | |
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
|
if (bind(_socket, (struct sockaddr *)&address.sockaddr,
address.length) != 0) {
int errNo = OFSocketErrNo();
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindFailedException exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
#if defined(OF_HPUX) || defined(OF_WII) || defined(OF_NINTENDO_3DS)
} else {
for (;;) {
uint16_t rnd = 0;
int ret;
while (rnd < 1024)
rnd = (uint16_t)rand();
OFSocketAddressSetPort(&address, rnd);
if ((ret = bind(_socket,
(struct sockaddr *)&address.sockaddr,
address.length)) == 0) {
port = rnd;
break;
}
if (OFSocketErrNo() != EADDRINUSE) {
int errNo = OFSocketErrNo();
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindFailedException
exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
}
}
|
>
|
|
|
|
|
|
|
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
|
if (bind(_socket, (struct sockaddr *)&address.sockaddr,
address.length) != 0) {
int errNo = OFSocketErrNo();
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindIPSocketFailedException
exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
#if defined(OF_HPUX) || defined(OF_WII) || defined(OF_NINTENDO_3DS)
} else {
for (;;) {
uint16_t rnd = 0;
int ret;
while (rnd < 1024)
rnd = (uint16_t)rand();
OFSocketAddressSetIPPort(&address, rnd);
if ((ret = bind(_socket,
(struct sockaddr *)&address.sockaddr,
address.length)) == 0) {
port = rnd;
break;
}
if (OFSocketErrNo() != EADDRINUSE) {
int errNo = OFSocketErrNo();
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindIPSocketFailedException
exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
}
}
|
︙ | | | ︙ | |
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
|
if (OFGetSockName(_socket, (struct sockaddr *)&address.sockaddr,
&address.length) != 0) {
int errNo = OFSocketErrNo();
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindFailedException exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
switch (((struct sockaddr *)&address.sockaddr)->sa_family) {
case AF_INET:
return OFFromBigEndian16(address.sockaddr.in.sin_port);
# ifdef OF_HAVE_IPV6
case AF_INET6:
return OFFromBigEndian16(address.sockaddr.in6.sin6_port);
# endif
default:
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindFailedException exceptionWithHost: host
port: port
socket: self
errNo: EAFNOSUPPORT];
}
#else
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindFailedException exceptionWithHost: host
port: port
socket: self
errNo: EADDRNOTAVAIL];
#endif
}
#if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
- (void)setSendsKeepAlives: (bool)sendsKeepAlives
{
int v = sendsKeepAlives;
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
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
|
if (OFGetSockName(_socket, (struct sockaddr *)&address.sockaddr,
&address.length) != 0) {
int errNo = OFSocketErrNo();
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindIPSocketFailedException exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
switch (((struct sockaddr *)&address.sockaddr)->sa_family) {
case AF_INET:
return OFFromBigEndian16(address.sockaddr.in.sin_port);
# ifdef OF_HAVE_IPV6
case AF_INET6:
return OFFromBigEndian16(address.sockaddr.in6.sin6_port);
# endif
default:
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindIPSocketFailedException
exceptionWithHost: host
port: port
socket: self
errNo: EAFNOSUPPORT];
}
#else
closesocket(_socket);
_socket = OFInvalidSocketHandle;
@throw [OFBindIPSocketFailedException exceptionWithHost: host
port: port
socket: self
errNo: EADDRNOTAVAIL];
#endif
}
#if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
- (void)setSendsKeepAlives: (bool)sendsKeepAlives
{
int v = sendsKeepAlives;
|
︙ | | | ︙ | |