-
Notifications
You must be signed in to change notification settings - Fork 307
/
ONInternetSocket.m
809 lines (639 loc) · 27.5 KB
/
ONInternetSocket.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
67
68
69
70
71
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
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
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
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
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
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// Copyright 1997-2017 Omni Development, Inc. All rights reserved.
//
// This software may only be used and reproduced according to the
// terms in the file OmniSourceLicense.html, which should be
// distributed with this project and can also be found at
// <http://www.omnigroup.com/developer/sourcecode/sourcelicense/>.
#import <OmniNetworking/ONInternetSocket.h>
#import <Foundation/Foundation.h>
#import <OmniBase/OmniBase.h>
#import <OmniBase/system.h>
#import "ONInternetSocket-Private.h"
#import <OmniNetworking/ONServiceEntry.h>
#import <OmniNetworking/ONHostAddress.h>
#import <OmniNetworking/ONHost.h>
#import <OmniNetworking/ONInterface.h>
#import <OmniNetworking/ONPortAddress.h>
RCS_ID("$Id$")
#ifdef OMNI_ASSERTIONS_ON
static BOOL is_mutex_locked(pthread_mutex_t *m);
#endif
@implementation ONInternetSocket
{
/* protocol family of socket, if socketFD is not -1 */
short socketPF;
int requestedLocalPort; // 0=any port, -1=not bound to a local address yet
}
BOOL ONSocketStateDebug = NO;
+ (int)ipProtocol;
{
OBRequestConcreteImplementation(self, _cmd);
return -1; // Not executed
}
+ (int)socketType;
{
OBRequestConcreteImplementation(self, _cmd);
return -1; // Not executed
}
+ (ONInternetSocket *)socket;
{
return [[[self alloc] _initWithSocketFD:-1 connected:NO] autorelease];
}
+ (ONInternetSocket *)socketWithConnectedFileDescriptor:(int)fd shouldClose:(BOOL)closeOnDealloc;
{
ONInternetSocket *s = [[self alloc] _initWithSocketFD:fd connected:YES];
s->flags.shouldNotCloseFD = closeOnDealloc ? 0 : 1;
return [s autorelease];
}
// Init and dealloc
- (void)dealloc;
{
pthread_mutex_lock(&socketLock);
if (socketFD != -1 && !flags.shouldNotCloseFD) {
#ifdef OMNI_ASSERTIONS_ON
int closeReturn =
#endif
close(socketFD);
OBASSERT(closeReturn == 0);
}
socketFD = -1;
[localAddress release];
[remoteAddress release];
[remoteHost release];
localAddress = nil;
remoteAddress = nil;
remoteHost = nil;
pthread_mutex_unlock(&socketLock);
pthread_mutex_destroy(&socketLock);
[super dealloc];
}
//
- (void)setLocalPortNumber;
{
// Bind to any available local port
[self setLocalPortNumber: 0];
}
- (void)setLocalPortNumber:(int)port;
{
ONSockaddrAny socketAddress;
int socketAddressLength;
NSException *pendingException;
pendingException = nil;
pthread_mutex_lock(&socketLock);
if (localAddress != nil) {
[localAddress release];
localAddress = nil;
}
if (port < 0) {
pthread_mutex_unlock(&socketLock);
return;
}
/* If we don't have a socket, go ahead and create one. We don't want to defer this because the caller will expect to get an error from this method if the port is already in use, and will expect us to allocate a port if they pass in '0' here --- so we have to actually call bind(). */
if (socketFD == -1) {
int desiredSocketAF;
requestedLocalPort = -1; /* prevent -createSocketFD: from trying to re-invoke us */
/* Default to binding to an IPv4 address. TODO: Can we make this more address-family-agnostic? */
/* Note that if we were to bind to the v6 wildcard address, then as long as there isn't a socket bound to the v4 wildcard address, we'll get the v4 traffic as well as the v6 traffic (with a v4-mapped-in-v6-address for the remote address; see "man 4 inet6"). That might be the right thing to do, except that it might confuse applications which only expect v4 remote addresses --- perhaps we should unmap v4-in-v6 addresses in ONHostAddress? That sounds like a mess... */
if (socketPF == PF_UNSPEC)
desiredSocketAF = AF_INET;
else
desiredSocketAF = ONAddressFamilyForProtocolFamily(socketPF);
NS_DURING {
[self _locked_createSocketFD:desiredSocketAF];
} NS_HANDLER {
pendingException = localException;
} NS_ENDHANDLER;
}
requestedLocalPort = port;
pthread_mutex_unlock(&socketLock);
if (pendingException)
[pendingException raise];
switch([self addressFamily]) {
case AF_INET:
bzero(&socketAddress, sizeof(socketAddress.ipv4));
socketAddressLength = sizeof(socketAddress.ipv4);
socketAddress.ipv4.sin_len = sizeof(socketAddress.ipv4);
socketAddress.ipv4.sin_family = AF_INET;
socketAddress.ipv4.sin_addr.s_addr = htonl(INADDR_ANY);
socketAddress.ipv4.sin_port = htons(port);
break;
case AF_INET6:
bzero(&socketAddress, sizeof(socketAddress.ipv6));
socketAddressLength = sizeof(socketAddress.ipv6);
socketAddress.ipv6.sin6_len = sizeof(socketAddress.ipv6);
socketAddress.ipv6.sin6_family = AF_INET6;
socketAddress.ipv6.sin6_addr = (struct in6_addr)IN6ADDR_ANY_INIT;
socketAddress.ipv6.sin6_port = htons(port);
break;
default:
[NSException raise:ONInternetSocketBindFailedExceptionName posixErrorNumber:EPFNOSUPPORT format:@"Unable to bind a socket: %s", strerror(EPFNOSUPPORT)];
return;
}
if (bind(socketFD, (struct sockaddr *)&socketAddress, socketAddressLength) == -1)
[NSException raise:ONInternetSocketBindFailedExceptionName posixErrorNumber:OMNI_ERRNO() format:@"Unable to bind a socket: %s", strerror(OMNI_ERRNO())];
}
- (void)setLocalPortNumber:(int)port allowingAddressReuse:(BOOL)reuse;
{
BOOL hadError;
hadError = NO;
flags.allowAddressReuse = reuse? 1 : 0;
if (socketFD != -1) {
int shouldReuse;
shouldReuse = reuse? 1 : 0;
if (setsockopt(socketFD, SOL_SOCKET, SO_REUSEADDR, &shouldReuse, sizeof(shouldReuse)) == -1)
hadError = YES;
shouldReuse = reuse? 1 : 0;
if (setsockopt(socketFD, SOL_SOCKET, SO_REUSEPORT, &shouldReuse, sizeof(shouldReuse)) == -1)
hadError = YES;
}
if (hadError)
[NSException raise:ONInternetSocketReuseSelectionFailedExceptionName posixErrorNumber:OMNI_ERRNO() format:@"Failed to set address reuse on socket: %s", strerror(OMNI_ERRNO())];
if (port >= 0)
[self setLocalPortNumber:port];
}
/* Utility function for retrieving a sockaddr and returning it as an ONPortAddress */
static ONPortAddress *getEndpointAddress(ONInternetSocket *self, int (*getaddr)(int, struct sockaddr *, ONSocketAddressLength *), NSException **exc)
{
ONSocketAddressLength addressLength;
ONSockaddrAny endpointAddress;
if (self->socketFD == -1)
return nil;
addressLength = sizeof(endpointAddress);
if (getaddr(self->socketFD, &(endpointAddress.generic), &addressLength) == -1) {
*exc = [NSException exceptionWithName:ONInternetSocketGetNameFailedExceptionName
posixErrorNumber:OMNI_ERRNO()
format:@"Unable to get socket name: %s", strerror(OMNI_ERRNO())];
return nil;
}
return [[[ONPortAddress alloc] initWithSocketAddress:&(endpointAddress.generic)] autorelease];
}
- (int)addressFamily
{
/* If necessary we can determine the family of a pre-existing socket by extracting it from the socket's local name */
if (socketPF == PF_UNSPEC && socketFD != -1) {
int socketAF = [[self localAddress] addressFamily];
socketPF = ONProtocolFamilyForAddressFamily(socketAF);
return socketAF;
}
return ONAddressFamilyForProtocolFamily(socketPF);
}
- (void)setAddressFamily:(int)newAddressFamily
{
if (newAddressFamily == PF_UNSPEC ||
newAddressFamily == [self addressFamily])
return;
if (socketFD != -1) {
// In the normal case, -setAddressFamily: will only be called before the socketFD is allocated.
// We don't really want to close and re-open the BSD socket, because that will lose connections, port assignments, etc. --- if, for some reason, it makes sense to be able to call -setAddressFamily: after the socket exists, then we can change this logic
[NSException raise:ONInternetSocketBindFailedExceptionName posixErrorNumber:EEXIST format:NSLocalizedStringFromTableInBundle(@"Unable to set address family of already-allocated socket", @"OmniNetworking", [NSBundle bundleForClass:[ONInternetSocket class]], @"error - attempted to change protocol family after the fact")];
}
/* Since we don't have a socket, we can just set the socketPF to tell us what kind of socket to create later */
OBASSERT(socketFD == -1);
socketPF = ONProtocolFamilyForAddressFamily(newAddressFamily);
}
- (ONPortAddress *)localAddress;
{
ONPortAddress *myLocalAddress;
NSException *exception = nil;
pthread_mutex_lock(&socketLock);
if (!localAddress)
localAddress = [getEndpointAddress(self, getsockname, &exception) retain];
myLocalAddress = [localAddress retain];
pthread_mutex_unlock(&socketLock);
if (exception)
[exception raise];
return [myLocalAddress autorelease];
}
- (unsigned short int)localAddressPort;
{
ONPortAddress *myLocalAddress = [self localAddress];
if (myLocalAddress == nil) {
if (requestedLocalPort > 0)
return requestedLocalPort;
else
return 0;
} else
return [myLocalAddress portNumber];
}
//
- (ONHost *)remoteAddressHost;
{
ONHost *myRemoteHost;
pthread_mutex_lock(&socketLock);
if (remoteHost) {
myRemoteHost = [remoteHost retain];
pthread_mutex_unlock(&socketLock);
return [myRemoteHost autorelease];
} else {
ONPortAddress *myRemoteAddress;
pthread_mutex_unlock(&socketLock);
/* This may take a while (to resolve the hostname) and may raise an exception, so we release the lock while we do it */
myRemoteAddress = [self remoteAddress];
myRemoteHost = myRemoteAddress? [ONHost hostForAddress:[myRemoteAddress hostAddress]] : nil;
pthread_mutex_lock(&socketLock);
if (remoteHost == nil && remoteAddress == myRemoteAddress)
remoteHost = [myRemoteHost retain];
pthread_mutex_unlock(&socketLock);
return myRemoteHost;
}
}
- (ONPortAddress *)remoteAddress;
{
ONPortAddress *myRemoteAddress;
NSException *exception = nil;
pthread_mutex_lock(&socketLock);
if (!remoteAddress) {
OBASSERT(remoteHost == nil);
remoteAddress = [getEndpointAddress(self, getpeername, &exception) retain];
}
myRemoteAddress = [remoteAddress retain];
pthread_mutex_unlock(&socketLock);
if (exception)
[exception raise];
return [myRemoteAddress autorelease];
}
- (unsigned short int)remoteAddressPort;
{
return [[self remoteAddress] portNumber];
}
//
- (ONInterface *)localInterface;
{
ONHostAddress *myAddress = [[self localAddress] hostAddress];
unsigned int triesRemaining = 2;
while (triesRemaining--) {
NSArray *interfaces = [ONInterface getInterfaces:triesRemaining == 0];
NSUInteger interfaceCount = [interfaces count];
while (interfaceCount--) {
ONInterface *interface = [interfaces objectAtIndex:interfaceCount];
// In the future, we might want to handle the case in which multiple network
// interfaces have the same IP address. In that case, we'd either need to
// return an array of the possible interfaces, or look at a destination address
// and the routing tables in order to determine which interface will get used.
// There still might be multiple interfaces that might be sharing the load,
// in which case it seems like we wouldn't be able to determine with any certainty
// which one would get used. The best solution would probably be to return an
// array of possibilities and let the caller work with that set (for example, in
// the case of the -maximumTransmissionUnit, they could just use the smallest unit.
if ([[interface addresses] containsObject:myAddress])
return interface;
}
}
[NSException raise:NSInternalInconsistencyException format:@"No interface found matching local address %@ for socket %@.", myAddress, self];
return nil; // We raise before reaching this line
}
//
- (void)connectToAddressFromArray:(NSArray *)portAddresses
{
NSUInteger addressCount, addressIndex;
NSException *firstTemporaryException;
firstTemporaryException = nil;
addressCount = [portAddresses count];
for(addressIndex = 0; addressIndex < addressCount; addressIndex ++) {
ONPortAddress *anAddress = [portAddresses objectAtIndex:addressIndex];
OBASSERT([anAddress isKindOfClass:[ONPortAddress class]]);
NS_DURING {
[self connectToPortAddress:anAddress];
} NS_HANDLER {
if (![[localException name] isEqualToString:
ONInternetSocketConnectTemporarilyFailedExceptionName])
[localException raise];
if (!firstTemporaryException)
firstTemporaryException = localException;
} NS_ENDHANDLER;
if (flags.connected)
return;
}
if (firstTemporaryException)
[firstTemporaryException raise];
else
[[NSException exceptionWithName:ONInternetSocketConnectFailedExceptionName reason:NSLocalizedStringFromTableInBundle(@"Unable to connect: no IP addresses to connect to", @"OmniNetworking", [NSBundle bundleForClass:[ONInternetSocket class]], @"error") userInfo:nil] raise];
}
- (void)connectToPortAddress:(ONPortAddress *)portAddress;
{
NSException *pendingException = nil;
const struct sockaddr *socketAddress;
BOOL connectSucceeded;
socketAddress = [portAddress portAddress];
OBPRECONDITION(!is_mutex_locked(&socketLock));
pthread_mutex_lock(&socketLock);
if (remoteAddress != nil) {
[remoteAddress release];
remoteAddress = nil;
}
if (remoteHost != nil) {
[remoteHost release];
remoteHost = nil;
}
/* If we have a socket of the wrong family, get rid of it */
if (socketFD != -1 && [self addressFamily] != (socketAddress->sa_family))
[self _locked_destroySocketFD];
/* Create a socket of the appropriate protocol family */
if (socketFD == -1) {
NS_DURING {
[self _locked_createSocketFD:socketAddress->sa_family];
} NS_HANDLER {
pendingException = localException;
} NS_ENDHANDLER;
}
pthread_mutex_unlock(&socketLock);
if (pendingException == nil) {
errno = 0;
connectSucceeded = connect(socketFD, socketAddress, socketAddress->sa_len) == 0;
if (ONSocketStateDebug)
NSLog(@"%@: connect(%@) --> %@", [self shortDescription], [portAddress description],
connectSucceeded ? @"Success" : [NSString stringWithFormat:@"Failure (errno=%d)", errno]);
} else {
connectSucceeded = NO;
if (ONSocketStateDebug)
NSLog(@"%@: connect(%@) skipped due to pending exception (%@)", [self shortDescription], [portAddress description], [pendingException name]);
}
if (connectSucceeded) {
flags.connected = YES;
} else {
pthread_mutex_lock(&socketLock);
// Check to see if the user aborted the connect()
if (flags.userAbort)
pendingException = [NSException exceptionWithName:ONInternetSocketUserAbortExceptionName reason:NSLocalizedStringFromTableInBundle(@"Connect aborted", @"OmniNetworking", [NSBundle bundleForClass:[ONInternetSocket class]], @"error - user (or other event) canceled attempt to connect to remote host") userInfo:nil];
[self _locked_destroySocketFD];
if (pendingException == nil)
switch (OMNI_ERRNO()) {
case ETIMEDOUT:
case ECONNREFUSED:
case ENETDOWN:
case ENETUNREACH:
case EHOSTDOWN:
case EADDRNOTAVAIL:
case EAFNOSUPPORT:
case EHOSTUNREACH:
pendingException = [NSException exceptionWithName:ONInternetSocketConnectTemporarilyFailedExceptionName posixErrorNumber:OMNI_ERRNO() format:NSLocalizedStringFromTableInBundle(@"Temporarily unable to connect to %@: %s", @"OmniNetworking", [NSBundle bundleForClass:[ONInternetSocket class]], @"error - one of ETIMEDOUT ECONNREFUSED ENETDOWN ENETUNREACH EHOSTDOWN or EHOSTUNREACH"), [portAddress description], strerror(OMNI_ERRNO())];
break;
default:
pendingException = [NSException exceptionWithName:ONInternetSocketConnectFailedExceptionName posixErrorNumber:OMNI_ERRNO() format:NSLocalizedStringFromTableInBundle(@"Unable to connect to %@: %s", @"OmniNetworking", [NSBundle bundleForClass:[ONInternetSocket class]], @"error - non-transient error when connecting to remote host"), portAddress, strerror(OMNI_ERRNO())];
break;
};
pthread_mutex_unlock(&socketLock);
if (ONSocketStateDebug)
NSLog(@"%@ %@: raising %@", [self shortDescription], NSStringFromSelector(_cmd), [pendingException name]);
[pendingException raise];
}
}
- (void)connectToHost:(ONHost *)host serviceEntry:(ONServiceEntry *)service;
{
[self connectToAddressFromArray:[host portAddressesForService:service]];
}
- (void)connectToHost:(ONHost *)host port:(unsigned short int)port;
{
NSArray *hostAddresses;
NSMutableArray *portAddresses;
NSUInteger addressCount, addressIndex;
/* Make an array of ONPortAddresses from the host's list of ONHostAddresses. TODO: This logic should really be in ONHost; perhaps by calling -portAddressesForService: with an anonymous numeric service object */
hostAddresses = [host addresses];
addressCount = [hostAddresses count];
portAddresses = [[NSMutableArray alloc] initWithCapacity:addressCount];
[portAddresses autorelease];
for (addressIndex = 0; addressIndex < addressCount; addressIndex ++) {
ONPortAddress *portAddress = [[ONPortAddress alloc] initWithHostAddress:[hostAddresses objectAtIndex:addressIndex] portNumber:port];
[portAddresses addObject:portAddress];
[portAddress release];
}
[self connectToAddressFromArray:portAddresses];
}
- (void)connectToAddress:(ONHostAddress *)hostAddress port:(unsigned short int)port;
{
[self connectToPortAddress:[[[ONPortAddress alloc] initWithHostAddress:hostAddress portNumber:port] autorelease]];
}
- (void)setNonBlocking:(BOOL)shouldBeNonBlocking;
{
fcntl(socketFD, F_SETFL, shouldBeNonBlocking ? O_NONBLOCK : 0x0);
flags.nonBlocking = shouldBeNonBlocking? 1 : 0;
}
- (BOOL)waitForInputWithTimeout:(NSTimeInterval)timeout;
{
struct timeval selectTimeout;
fd_set readfds;
int returnValue;
if (socketFD == -1) {
NSString *localizedErrorMsg = NSLocalizedStringFromTableInBundle(@"Attempted read from a non-connected socket", @"OmniNetworking", [NSBundle bundleForClass:[ONInternetSocket class]], @"error - socket is unxepectedly closed or not connected");
[[NSException exceptionWithName:ONInternetSocketNotConnectedExceptionName reason:localizedErrorMsg userInfo:nil] raise];
}
if (timeout < 0.0)
timeout = 0.0;
double usec, isec;
usec = modf(timeout, &isec);
selectTimeout.tv_sec = (unsigned int)isec;
selectTimeout.tv_usec = (unsigned int)floor(1.0e6 * usec);
FD_ZERO(&readfds);
FD_SET(socketFD, &readfds);
returnValue = select(socketFD + 1, &readfds, NULL, NULL, &selectTimeout);
switch (returnValue) {
case -1:
[NSException raise:ONInternetSocketReadFailedExceptionName posixErrorNumber:OMNI_ERRNO() format:NSLocalizedStringFromTableInBundle(@"Error waiting for input: %s", @"OmniNetworking", [NSBundle bundleForClass:[ONInternetSocket class]], @"error return from select()"), strerror(OMNI_ERRNO())];
case 0:
return NO;
default:
return FD_ISSET(socketFD, &readfds) != 0;
}
}
- (void)setAllowsBroadcast:(BOOL)shouldAllowBroadcast;
{
int allows;
// convert BOOL to an int
allows = shouldAllowBroadcast? 1 : 0;
// convert BOOL to a bit flag
flags.allowBroadcast = shouldAllowBroadcast? 1 : 0;
if (socketFD != -1) {
if (setsockopt(socketFD, SOL_SOCKET, SO_BROADCAST, (char *)&allows, sizeof(allows)) == -1)
[NSException raise:ONInternetSocketBroadcastSelectionFailedExceptionName posixErrorNumber:OMNI_ERRNO() format:@"Failed to set broadcast to %d on socket: %s", allows, strerror(OMNI_ERRNO())];
}
}
- (int)socketFD;
{
return socketFD;
}
- (BOOL)isConnected;
{
return (flags.connected > 0) ? YES : NO;
}
- (BOOL)didAbort;
{
return (flags.userAbort > 0) ? YES : NO;
}
- (BOOL)isWritable;
{
fd_set writeSet;
struct timeval timeout;
int writability;
if (socketFD == -1)
return NO;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
FD_ZERO(&writeSet);
FD_SET(socketFD, &writeSet);
writability = select(socketFD + 1, NULL, &writeSet, NULL, &timeout);
return (writability == 1);
}
- (BOOL)isReadable;
{
fd_set readSet;
struct timeval timeout;
if (socketFD == -1)
return NO;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
FD_ZERO(&readSet);
FD_SET(socketFD, &readSet);
return select(socketFD + 1, &readSet, NULL, NULL, &timeout) == 1;
}
// ONSocket subclass
- (void)abortSocket;
{
pthread_mutex_lock(&socketLock);
flags.userAbort = YES;
if (socketFD != -1)
[self _locked_destroySocketFD];
flags.connected = NO;
pthread_mutex_unlock(&socketLock);
}
// Debugging
- (NSMutableDictionary *)debugDictionary;
{
NSMutableDictionary *debugDictionary;
debugDictionary = [super debugDictionary];
if (socketFD != -1)
[debugDictionary setObject:[NSNumber numberWithInt:socketFD] forKey:@"socketFD"];
#define NOTE_FLAG(flag) [debugDictionary setObject:flags.flag ? @"YES" : @"NO" forKey:(NSString *)CFSTR(#flag)]
NOTE_FLAG(listening);
NOTE_FLAG(connected);
NOTE_FLAG(userAbort);
NOTE_FLAG(shouldNotCloseFD);
NOTE_FLAG(allowAddressReuse);
NOTE_FLAG(allowBroadcast);
NOTE_FLAG(nonBlocking);
if (socketPF != PF_UNSPEC)
[debugDictionary setObject:[NSNumber numberWithInt:socketPF] forKey:@"socketPF"];
if (requestedLocalPort != -1)
[debugDictionary setObject:[NSNumber numberWithInt:requestedLocalPort] forKey:@"requestedLocalPort"];
return debugDictionary;
}
@end
@implementation ONInternetSocket (SubclassAPI)
- _initWithSocketFD:(int)aSocketFD connected:(BOOL)isConnected;
{
if (!(self = [super init]))
return nil;
OBASSERT(!(isConnected && (aSocketFD == -1)));
socketFD = aSocketFD;
flags.connected = isConnected;
socketPF = PF_UNSPEC;
requestedLocalPort = -1;
pthread_mutex_init(&socketLock, NULL);
return self;
}
#ifdef OMNI_ASSERTIONS_ON
static BOOL is_mutex_locked(pthread_mutex_t *m)
{
int err = pthread_mutex_trylock(m);
if (err == EBUSY)
return YES;
if (err == 0) {
pthread_mutex_unlock(m);
return NO;
}
return NO;
}
#endif
- (void)ensureSocketFD:(int)af;
{
pthread_mutex_lock(&socketLock);
if (socketFD == -1) {
NS_DURING {
[self _locked_createSocketFD:af];
} NS_HANDLER {
if (socketFD != -1)
[self _locked_destroySocketFD];
pthread_mutex_unlock(&socketLock);
[localException raise];
} NS_ENDHANDLER;
}
pthread_mutex_unlock(&socketLock);
}
- (void)_locked_createSocketFD:(int)af;
{
int newSocketFD;
int protocolFamily;
OBPRECONDITION(is_mutex_locked(&socketLock));
OBPRECONDITION(socketFD == -1);
OBPRECONDITION(localAddress == nil);
OBPRECONDITION(remoteAddress == nil);
OBPRECONDITION(remoteHost == nil);
protocolFamily = ONProtocolFamilyForAddressFamily(af);
newSocketFD = socket(protocolFamily, [[self class] socketType], [[self class] ipProtocol]);
if (ONSocketStateDebug)
NSLog(@"%@: opened new fd %d", [self shortDescription], newSocketFD);
if (newSocketFD == -1) {
[NSException raise:ONInternetSocketConnectFailedExceptionName posixErrorNumber:OMNI_ERRNO() format:@"Unable to create a socket: %s", strerror(OMNI_ERRNO())];
return;
}
socketFD = newSocketFD;
socketPF = protocolFamily;
flags.shouldNotCloseFD = 0;
flags.listening = 0;
flags.connected = 0;
if (requestedLocalPort != -1 || flags.allowAddressReuse)
[self setLocalPortNumber:requestedLocalPort allowingAddressReuse:(flags.allowAddressReuse)? YES : NO];
if (flags.nonBlocking)
[self setNonBlocking:(flags.nonBlocking)? YES : NO];
if (flags.allowBroadcast)
[self setAllowsBroadcast:(flags.allowBroadcast)? YES : NO];
OBPOSTCONDITION(socketFD != -1);
}
- (void)_locked_destroySocketFD
{
int oldSocketFD;
OBPRECONDITION(is_mutex_locked(&socketLock));
oldSocketFD = socketFD;
if (ONSocketStateDebug)
NSLog(@"%@: releasing fd %d", [self shortDescription], oldSocketFD);
if (oldSocketFD == -1)
return;
socketFD = -1;
/* Unclear what this call to shutdown() is needed for; it used to be in -abortSocket. We need to call shutdown() after copying the fd into oldSocketFD, however, since shutdown() may cause another thread to wake up and start closing things, that being one of the few threadsafe operations on ONInternetSocket. */
/* TODO: Perhaps -abortSocket should merely call shutdown(), and not destroy the FD (or call this method)? In which case, -destroySocketFD wouldn't call shutdown() */
if (flags.connected && flags.userAbort)
shutdown(oldSocketFD, SHUT_RDWR); // disallow further sends and receives
if (!flags.shouldNotCloseFD) {
#ifdef OMNI_ASSERTIONS_ON
int closeReturn =
#endif
close(oldSocketFD);
OBASSERT(closeReturn == 0);
}
if (localAddress != nil) {
[localAddress release];
localAddress = nil;
}
if (remoteAddress != nil) {
[remoteAddress release];
remoteAddress = nil;
}
if (remoteHost != nil) {
[remoteHost release];
remoteHost = nil;
}
flags.listening = 0;
flags.connected = 0;
}
@end
NSString * const ONInternetSocketBroadcastSelectionFailedExceptionName = @"ONInternetSocketBroadcastSelectionFailedExceptionName";
NSString * const ONInternetSocketReuseSelectionFailedExceptionName = @"ONInternetSocketReuseSelectionFailedExceptionName";
NSString * const ONInternetSocketBindFailedExceptionName = @"ONInternetSocketBindFailedExceptionName";
NSString * const ONInternetSocketConnectFailedExceptionName = @"ONInternetSocketConnectFailedExceptionName";
NSString * const ONInternetSocketConnectTemporarilyFailedExceptionName = @"ONInternetSocketConnectTemporarilyFailedExceptionName";
NSString * const ONInternetSocketGetNameFailedExceptionName = @"ONInternetSocketGetNameFailedExceptionName";
NSString * const ONInternetSocketNotConnectedExceptionName = @"ONInternetSocketNotConnectedExceptionName";
NSString * const ONInternetSocketReadFailedExceptionName = @"ONInternetSocketReadFailedExceptionName";
NSString * const ONInternetSocketUserAbortExceptionName = @"ONInternetSocketUserAbortExceptionName";
NSString * const ONInternetSocketWriteFailedExceptionName = @"ONInternetSocketWriteFailedExceptionName";
NSString * const ONInternetSocketCloseFailedExceptionName = @"ONInternetSocketCloseFailedExceptionName";
NSString * const ONInternetSocketSetOptionFailedExceptionName = @"ONInternetSocketSetOptionFailedExceptionName";