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)spinoffReadLoop;
22 -(void)socketReadLoop:(id)data;
25 @implementation ITInetSocket
26 +(void)startAutoconnectingToService:(NSString*)type delegate:(id <ITInetSocketDelegate,NSObject>)d
28 NSNetServiceBrowser *browse = [[NSNetServiceBrowser alloc] init];
29 ITServiceBrowserDelegate *bd = [[ITServiceBrowserDelegate alloc] initWithDelegate:d];
31 [browse setDelegate:bd];
32 [browse searchForServicesOfType:[NSString stringWithFormat:@"._%@._tcp",type] inDomain:nil];
35 -(id)initWithFD:(int)fd delegate:(id <ITInetSocketDelegate,NSObject>)d
37 if (self = [super init])
39 state = ITInetSocketListening;
41 delegate = [d retain];
43 writePipe = [[ITByteStream alloc] init];
44 readPipe = [[ITByteStream alloc] init];
48 actionflag = dieflag = 0;
53 -(id)initWithDelegate:(id <ITInetSocketDelegate,NSObject>)d
55 if (self = [super init])
57 state = ITInetSocketDisconnected;
59 delegate = [d retain];
61 writePipe = [[ITByteStream alloc] init];
62 readPipe = [[ITByteStream alloc] init];
66 actionflag = dieflag = 0;
78 if (!sarr) freeaddrinfo(ai);
82 -(void) connectToHost:(NSString*)host onPort:(short)thePort
84 if (state == ITInetSocketDisconnected)
86 NSString *nport = [[NSNumber numberWithShort:thePort] stringValue];
87 [self doConnSetupWithHost:host namedPort:nport];
91 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)_port
93 if (state == ITInetSocketDisconnected)
95 [self doConnSetupWithHost:host namedPort:_port];
99 -(void) connectWithSockaddrArray:(NSArray*)arr
101 if (state == ITInetSocketDisconnected)
103 NSEnumerator *e = [arr objectEnumerator];
106 ai = malloc(sizeof(struct addrinfo));
108 while (d = [e nextObject])
110 struct sockaddr *s = (struct sockaddr*)[d bytes];
111 a->ai_family = s->sa_family;
113 a->ai_next = malloc(sizeof(struct addrinfo));
123 -(void)retryConnection
127 -(ITInetSocketState)state
131 -(id <ITInetSocketDelegate>)delegate
136 -(unsigned short)bufferSize
141 -(void)setBufferSize:(unsigned short)_bufs
147 @implementation ITInetSocket(Debugging)
148 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai
163 "\tAddr = {%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx}\n"
167 NSString *nsfmt = [NSString stringWithCString:cfmt];
168 NSMutableString *buf = [[[NSMutableString alloc] init] autorelease];
172 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)_ai->ai_addr;
173 [buf appendFormat: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];
175 while (_ai = _ai->ai_next);
176 [buf appendString:@"nil\n}"];
181 @implementation ITInetSocket(Private)
182 -(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort
184 struct addrinfo hints;
186 const char *portNam = [namedPort cString], *hostCStr = [host cString];
189 hints.ai_family = PF_INET6;
190 hints.ai_socktype = SOCK_STREAM;
191 hints.ai_protocol = IPPROTO_TCP;
192 hints.ai_canonname = NULL;
193 hints.ai_addr = NULL;
194 hints.ai_next = NULL;
196 err = getaddrinfo(hostCStr,portNam,&hints,&ai);
197 if (err == EAI_NODATA) //it's a dotted-decimal IPv4 string, so we use v6compat stuff now
199 err = getaddrinfo([[NSString stringWithFormat:@"ffff::%s",hostCStr] cString],portNam,&hints,&ai);
201 NSLog([self dumpv6Addrinfo:ai]);
204 -(void)spinoffReadLoop
206 NSPort *p1 = [NSPort port], *p2 = [NSPort port];
207 NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:p1 sendPort:p2];
208 NSArray *par = [NSArray arrayWithObjects:p2,p1,nil];
209 [dcon setRootObject:delegate];
210 [NSThread detachNewThreadSelector:@selector(socketReadLoop:) toTarget:self withObject:par]; //spawn read thread
213 -(void)socketReadLoop:(id)data
215 NSAutoreleasePool *ap = [[NSAutoreleasePool alloc] init];
216 NSConnection *dcon = [[NSConnection alloc] initWithReceivePort:[data objectAtIndex:0] sendPort:[data objectAtIndex:1]];
217 NSProxy *dp = [dcon rootProxy];
218 char *buf = malloc(bufs);
219 unsigned long readLen = 0;
224 NSData *d = [NSData alloc];
225 readLen = recv(sockfd,buf,bufs,0);
226 [d initWithBytesNoCopy:buf length:readLen];
227 [readPipe writeData:d];
229 [dp dataRecieved:self];
243 NSData *d = [writePipe readAllData];
244 write(sockfd,[d bytes],[d length]);