5 // Created by Alexander Strange on Tue Feb 11 2003.
6 // Copyright (c) 2003 __MyCompanyName__. All rights reserved.
9 #import "ITInetSocket.h"
10 #import "ITServiceBrowserDelegate.h"
11 #import <sys/socket.h>
15 @interface ITInetSocket(Debugging)
16 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai;
19 @interface ITInetSocket(Private)
20 -(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort;
21 -(void)realDoConnection;
22 -(void)spinoffReadLoop;
23 -(void)socketReadLoop:(id)data;
26 @implementation ITInetSocket
27 +(void)startAutoconnectingToService:(NSString*)type delegate:(id <ITInetSocketDelegate,NSObject>)d
29 NSNetServiceBrowser *browse = [[NSNetServiceBrowser alloc] init];
30 ITServiceBrowserDelegate *bd = [[ITServiceBrowserDelegate alloc] initWithDelegate:d];
32 [browse setDelegate:bd];
33 [browse searchForServicesOfType:[NSString stringWithFormat:@"._%@._tcp",type] inDomain:nil];
36 -(id)initWithFD:(int)fd delegate:(id <ITInetSocketDelegate,NSObject>)d
38 if (self = [super init])
40 state = ITInetSocketListening;
42 delegate = [d retain];
44 writePipe = [[ITByteStream alloc] initWithDelegate:self];
45 readPipe = [[ITByteStream alloc] initWithDelegate:d];
49 actionflag = dieflag = 0;
54 -(id)initWithDelegate:(id <ITInetSocketDelegate,NSObject>)d
56 if (self = [super init])
58 state = ITInetSocketDisconnected;
60 delegate = [d retain];
62 writePipe = [[ITByteStream alloc] initWithDelegate:self];
63 readPipe = [[ITByteStream alloc] initWithDelegate:d];
67 actionflag = dieflag = 0;
79 if (!sarr) freeaddrinfo(ai);
83 -(void) connectToHost:(NSString*)host onPort:(short)thePort
85 if (state == ITInetSocketDisconnected)
87 NSString *nport = [[NSNumber numberWithShort:thePort] stringValue];
88 [self doConnSetupWithHost:host namedPort:nport];
92 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)_port
94 if (state == ITInetSocketDisconnected)
96 [self doConnSetupWithHost:host namedPort:_port];
100 -(void) connectWithSockaddrArray:(NSArray*)arr
102 if (state == ITInetSocketDisconnected)
104 NSEnumerator *e = [arr objectEnumerator];
107 ai = malloc(sizeof(struct addrinfo));
109 while (d = [e nextObject])
111 struct sockaddr *s = (struct sockaddr*)[d bytes];
112 a->ai_family = s->sa_family;
114 a->ai_next = malloc(sizeof(struct addrinfo));
118 [self realDoConnection];
124 NSLog(@"Got a disconnect");
128 -(void)retryConnection
130 ai_cur = ai_cur->ai_next?ai_cur->ai_next:ai_cur;
132 [self realDoConnection];
135 -(ITInetSocketState)state
139 -(id <ITInetSocketDelegate>)delegate
144 -(unsigned short)bufferSize
149 -(void)setBufferSize:(unsigned short)_bufs
154 -(void)newDataAdded:(ITByteStream*)sender
156 NSLog(@"writePipe got something");
158 do {} while (actionflag == 1);
159 NSLog(@"thread saw actionFlag");
163 @implementation ITInetSocket(Debugging)
164 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai
179 "\tAddr = {%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx}\n"
184 NSString *nsfmt = [NSString stringWithCString:cfmt];
185 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)_ai->ai_addr;
186 NSString *buf = [[NSMutableString alloc] initWithFormat:nsfmt,_ai->ai_flags,_ai->ai_family,_ai->ai_socktype,_ai->ai_protocol,_ai->ai_canonname?_ai->ai_canonname:"",sa->sin6_len,sa->sin6_family,sa->sin6_port,sa->sin6_flowinfo,sa->sin6_addr.__u6_addr.__u6_addr16[0],sa->sin6_addr.__u6_addr.__u6_addr16[1],sa->sin6_addr.__u6_addr.__u6_addr16[2],sa->sin6_addr.__u6_addr.__u6_addr16[3],sa->sin6_addr.__u6_addr.__u6_addr16[4],sa->sin6_addr.__u6_addr.__u6_addr16[5],sa->sin6_addr.__u6_addr.__u6_addr16[6],sa->sin6_addr.__u6_addr.__u6_addr16[7],sa->sin6_scope_id,_ai->ai_next?[self dumpv6Addrinfo:_ai->ai_next]:@"nil"];
192 @implementation ITInetSocket(Private)
193 -(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort
195 struct addrinfo hints;
197 const char *portNam = [namedPort cString], *hostCStr = [host cString];
198 state = ITInetSocketConnecting;
200 hints.ai_family = PF_UNSPEC;
201 hints.ai_socktype = SOCK_STREAM;
202 hints.ai_protocol = IPPROTO_TCP;
203 hints.ai_addrlen = 0;
204 hints.ai_canonname = NULL;
205 hints.ai_addr = NULL;
206 hints.ai_next = NULL;
208 err = getaddrinfo(hostCStr,portNam,&hints,&ai);
210 NSLog(@"%s, h %@ p %@",gai_strerror(err),host,namedPort);
211 NSLog(ai?[self dumpv6Addrinfo:ai]:@"");
213 [self realDoConnection];
216 -(void)realDoConnection
218 sockfd = socket(ai_cur->ai_addr->sa_family,SOCK_STREAM,IPPROTO_TCP);
219 [self spinoffReadLoop];
222 -(void)spinoffReadLoop
224 NSPort *p1 = [NSPort port], *p2 = [NSPort port];
225 NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:p1 sendPort:p2];
226 NSArray *par = [NSArray arrayWithObjects:p2,p1,nil];
227 [dcon setRootObject:delegate];
228 [NSThread detachNewThreadSelector:@selector(socketReadLoop:) toTarget:self withObject:par]; //spawn read thread
231 -(void)socketReadLoop:(id)data
233 NSAutoreleasePool *ap = [[NSAutoreleasePool alloc] init];
234 NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:[data objectAtIndex:0] sendPort:[data objectAtIndex:1]];
235 NSProxy *dp = [[dcon rootProxy] retain];
236 char *buf = malloc(bufs);
237 unsigned long readLen = 0;
239 [readPipe setDelegate:dp];
240 NSLog(@"Connecting");
241 err = connect(sockfd,ai_cur->ai_addr,ai_cur->ai_addrlen);
245 [(id)dp errorOccured:ITInetCouldNotConnect during:ITInetSocketConnecting onSocket:self];
248 NSLog(@"Sending finishedConnecting");
249 [(id)dp finishedConnecting:self];
251 state = ITInetSocketListening;
252 while (!actionflag && !dieflag)
254 readLen = recv(sockfd,buf,bufs,0);
255 state = ITInetSocketReading;
256 if (readLen == -1) {[(id)dp errorOccured:ITInetConnectionDropped during:ITInetSocketReading onSocket:self];goto dieaction;}
259 NSLog(@"Doing writeData to readPipe");
260 [readPipe writeBytes:buf len:readLen];
262 ap = [[NSAutoreleasePool alloc] init];
265 state = ITInetSocketListening;
271 state = ITInetSocketDisconnected;
283 state = ITInetSocketWriting;
284 NSLog(@"Emptying writePipe");
285 NSData *d = [writePipe readAllData];
286 write(sockfd,[d bytes],[d length]);
288 ap = [[NSAutoreleasePool alloc] init];