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
|
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
-
-
-
-
-
-
-
-
-
-
-
-
|
- (void)dealloc
{
[_SOCKS5Host release];
[super dealloc];
}
- (void)close
{
[super close];
#ifdef OF_WII
if (_port > 0) {
of_socket_port_free(_port, SOCK_STREAM);
_port = 0;
}
#endif
}
- (void)connectToHost: (OFString*)host
port: (uint16_t)port
{
OFString *destinationHost = host;
uint16_t destinationPort = port;
of_resolver_result_t **results, **iter;
int errNo = 0;
|
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
|
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
|
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
|
#endif
- (uint16_t)bindToHost: (OFString*)host
port: (uint16_t)port
{
of_resolver_result_t **results;
const int one = 1;
#ifndef OF_WII
#if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
union {
struct sockaddr_storage storage;
struct sockaddr_in in;
# ifdef AF_INET6
# ifdef HAVE_IPV6
struct sockaddr_in6 in6;
# endif
} addr;
socklen_t addrLen;
#endif
if (_socket != INVALID_SOCKET)
@throw [OFAlreadyConnectedException exceptionWithSocket: self];
if (_SOCKS5Host != nil)
@throw [OFNotImplementedException exceptionWithSelector: _cmd
object: self];
#ifdef OF_WII
if (port == 0)
port = of_socket_port_find(SOCK_STREAM);
else if (!of_socket_port_register(port, SOCK_STREAM))
@throw [OFBindFailedException exceptionWithHost: host
port: port
socket: self
errNo: EADDRINUSE];
#endif
@try {
results = of_resolve_host(host, port, SOCK_STREAM);
results = of_resolve_host(host, port, SOCK_STREAM);
} @catch (id e) {
#ifdef OF_WII
of_socket_port_free(port, SOCK_STREAM);
#endif
@throw e;
}
@try {
#if SOCK_CLOEXEC == 0 && defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
int flags;
#endif
if ((_socket = socket(results[0]->family,
results[0]->type | SOCK_CLOEXEC,
|
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
|
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
|
+
+
+
-
-
-
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
+
+
-
-
+
|
if ((flags = fcntl(_socket, F_GETFD, 0)) != -1)
fcntl(_socket, F_SETFD, flags | FD_CLOEXEC);
#endif
setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR,
(const char*)&one, (socklen_t)sizeof(one));
#if defined(OF_WII) || defined(OF_NINTENDO_3DS)
if (port != 0) {
#endif
if (bind(_socket, results[0]->address,
results[0]->addressLength) == -1) {
int errNo = of_socket_errno();
if (bind(_socket, results[0]->address,
results[0]->addressLength) != 0) {
int errNo = of_socket_errno();
close(_socket);
_socket = INVALID_SOCKET;
close(_socket);
_socket = INVALID_SOCKET;
@throw [OFBindFailedException exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
} @catch (id e) {
#ifdef OF_WII
of_socket_port_free(port, SOCK_STREAM);
#endif
@throw e;
@throw [OFBindFailedException
exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
#if defined(OF_WII) || defined(OF_NINTENDO_3DS)
} else {
for (;;) {
uint16_t rnd = 0;
int ret;
while (rnd < 1024)
rnd = (uint16_t)rand();
switch (results[0]->family) {
case AF_INET:
((struct sockaddr_in*)
results[0]->address)->sin_port =
OF_BSWAP16_IF_LE(rnd);
break;
# ifdef HAVE_IPV6
case AF_INET6:
((struct sockaddr_in6*)
results[0]->address)->sin6_port =
OF_BSWAP16_IF_LE(rnd);
break;
# endif
default:
@throw [OFInvalidArgumentException
exception];
}
ret = bind(_socket, results[0]->address,
results[0]->addressLength);
if (ret == 0) {
port = rnd;
break;
}
if (of_socket_errno() != EADDRINUSE) {
int errNo = of_socket_errno();
close(_socket);
_socket = INVALID_SOCKET;
@throw [OFBindFailedException
exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
}
}
#endif
} @finally {
of_resolver_free(results);
}
if (port > 0) {
if (port > 0)
#ifdef OF_WII
_port = port;
#endif
return port;
}
#if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
#ifndef OF_WII
addrLen = (socklen_t)sizeof(addr.storage);
if (of_getsockname(_socket, (struct sockaddr*)&addr.storage,
&addrLen) != 0) {
int errNo = of_socket_errno();
close(_socket);
_socket = INVALID_SOCKET;
@throw [OFBindFailedException exceptionWithHost: host
port: port
socket: self
errNo: errNo];
}
if (addr.storage.ss_family == AF_INET)
return OF_BSWAP16_IF_LE(addr.in.sin_port);
# ifdef AF_INET6
# ifdef HAVE_IPV6
if (addr.storage.ss_family == AF_INET6)
return OF_BSWAP16_IF_LE(addr.in6.sin6_port);
# endif
#endif
close(_socket);
_socket = INVALID_SOCKET;
|
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
|
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
|
+
-
-
-
-
-
-
-
+
-
-
+
+
-
-
-
-
-
-
-
-
+
|
}
- (bool)isListening
{
return _listening;
}
#if !defined(OF_WII) && !defined(OF_NINTENDO_3DS)
- (void)setKeepAliveEnabled: (bool)enabled
{
int v = enabled;
if (setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE,
(char*)&v, (socklen_t)sizeof(v)) != 0)
@throw [OFSetOptionFailedException
exceptionWithStream: self
errNo: of_socket_errno()];
#ifdef OF_WII
_keepAliveEnabled = enabled;
#endif
}
- (bool)isKeepAliveEnabled
{
#ifndef OF_WII
int v;
socklen_t len = sizeof(v);
if (getsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE,
(char*)&v, &len) != 0 || len != sizeof(v))
@throw [OFGetOptionFailedException
exceptionWithStream: self
errNo: of_socket_errno()];
return v;
#else
return _keepAliveEnabled;
}
#endif
}
#ifndef OF_WII
- (void)setTCPNoDelayEnabled: (bool)enabled
{
int v = enabled;
if (setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY,
(char*)&v, (socklen_t)sizeof(v)) != 0)
@throw [OFSetOptionFailedException
exceptionWithStream: self
errNo: of_socket_errno()];
#ifdef OF_WII
_TCPNoDelayEnabled = enabled;
#endif
}
- (bool)isTCPNoDelayEnabled
{
#ifndef OF_WII
int v;
socklen_t len = sizeof(v);
if (getsockopt(_socket, IPPROTO_TCP, TCP_NODELAY,
(char*)&v, &len) != 0 || len != sizeof(v))
@throw [OFGetOptionFailedException
exceptionWithStream: self
errNo: of_socket_errno()];
return v;
#else
return _TCPNoDelayEnabled;
#endif
}
#endif
@end
|