More socket work
[ITFoundation.git] / ITInetSocket.m
1 //
2 //  ITInetSocket.m
3 //  ITFoundation
4 //
5 //  Created by Alexander Strange on Tue Feb 11 2003.
6 //  Copyright (c) 2003 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "ITInetSocket.h"
10 #import "ITServiceBrowserDelegate.h"
11 #import <sys/socket.h>
12 #import <arpa/inet.h>
13 #import <unistd.h>
14
15 @interface ITInetSocket(Debugging)
16 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai;
17 @end
18
19 @interface ITInetSocket(Private)
20 -(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort;
21 -(void)spinoffReadLoop;
22 -(void)socketReadLoop:(id)data;
23 @end
24
25 @implementation ITInetSocket
26 +(void)startAutoconnectingToService:(NSString*)type delegate:(id <ITInetSocketDelegate,NSObject>)d
27 {
28     NSNetServiceBrowser *browse = [[NSNetServiceBrowser alloc] init];
29     ITServiceBrowserDelegate *bd = [[ITServiceBrowserDelegate alloc] initWithDelegate:d];
30
31     [browse setDelegate:bd];
32     [browse searchForServicesOfType:[NSString stringWithFormat:@"._%@._tcp",type] inDomain:nil];
33 }
34
35 -(id)initWithFD:(int)fd delegate:(id <ITInetSocketDelegate,NSObject>)d
36 {
37     if (self = [super init])
38            {
39            state = ITInetSocketListening;
40            sockfd = fd;
41            delegate = [d retain];
42            port = 0;
43            writePipe = [[ITByteStream alloc] init];
44            readPipe = [[ITByteStream alloc] init];
45            ai = nil;
46            sarr = nil;
47            bufs = 512;
48            actionflag = dieflag = 0;
49            }
50     return self;
51 }
52
53 -(id)initWithDelegate:(id <ITInetSocketDelegate,NSObject>)d
54 {
55     if (self = [super init])
56            {
57            state = ITInetSocketDisconnected;
58            sockfd = -1;
59            delegate = [d retain];
60            port = 0;
61            writePipe = [[ITByteStream alloc] init];
62            readPipe = [[ITByteStream alloc] init];
63            ai = nil;
64            sarr = nil;
65            bufs = 512;
66            actionflag = dieflag = 0;
67            }
68     return self;
69 }
70
71
72 -(void) dealloc
73 {
74     shutdown(sockfd,2);
75     [delegate release];
76     [writePipe release];
77     [readPipe release];
78     if (!sarr) freeaddrinfo(ai);
79     [sarr release];
80 }
81
82 -(void) connectToHost:(NSString*)host onPort:(short)thePort
83 {
84     if (state == ITInetSocketDisconnected)
85            {
86            NSString *nport = [[NSNumber numberWithShort:thePort] stringValue];
87            [self doConnSetupWithHost:host namedPort:nport];
88            }
89 }
90
91 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)_port
92 {
93     if (state == ITInetSocketDisconnected)
94            {
95            [self doConnSetupWithHost:host namedPort:_port];
96            }
97 }
98
99 -(void) connectWithSockaddrArray:(NSArray*)arr
100 {
101     if (state == ITInetSocketDisconnected)
102            {
103            NSEnumerator *e = [arr objectEnumerator];
104            NSData *d;
105            struct addrinfo *a;
106            ai = malloc(sizeof(struct addrinfo));
107            a = ai;
108            while (d = [e nextObject])
109            {
110                   struct sockaddr *s = (struct sockaddr*)[d bytes];
111                   a->ai_family = s->sa_family;
112                   a->ai_addr = s;
113                   a->ai_next = malloc(sizeof(struct addrinfo));
114                   a = a->ai_next;
115            }
116            }
117 }
118
119 -(void)disconnect
120 {
121 }
122
123 -(void)retryConnection
124 {
125 }
126
127 -(ITInetSocketState)state
128 {
129     return state;
130 }
131 -(id <ITInetSocketDelegate>)delegate
132 {
133     return delegate;
134 }
135
136 -(unsigned short)bufferSize
137 {
138     return bufs;
139 }
140
141 -(void)setBufferSize:(unsigned short)_bufs
142 {
143     bufs = _bufs;
144 }
145 @end
146
147 @implementation ITInetSocket(Debugging)
148 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai
149 {
150     const char *cfmt =
151     "\{\n"
152     "Flags = %x\n"
153     "Family = %x\n"
154     "Socktype = %x\n"
155     "Protocol = %x\n"
156     "Canonname = %s\n"
157     "Sockaddr = \n"
158     "{\n"
159     "\tLength = %x\n"
160     "\tFamily = %x\n"
161     "\tPort = %d\n"
162     "\tFlowinfo = %x\n"
163     "\tAddr = {%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx}\n"
164     "\tScope = %x\n"
165     "}\n"
166     "Next = ";
167     NSString *nsfmt = [NSString stringWithCString:cfmt];
168     NSMutableString *buf = [[[NSMutableString alloc] init] autorelease];
169
170     do
171            {
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];
174            }
175     while (_ai = _ai->ai_next);
176     [buf appendString:@"nil\n}"];
177     return buf;
178 }
179 @end
180
181 @implementation ITInetSocket(Private)
182 -(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort
183 {
184     struct addrinfo hints;
185            int err;
186            const char *portNam = [namedPort cString], *hostCStr = [host cString];
187
188            hints.ai_flags = 0;
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;
195
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
198            {
199                   err = getaddrinfo([[NSString stringWithFormat:@"ffff::%s",hostCStr] cString],portNam,&hints,&ai);
200            }
201            NSLog([self dumpv6Addrinfo:ai]);
202 }
203
204 -(void)spinoffReadLoop
205 {
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
211 }
212
213 -(void)socketReadLoop:(id)data
214 {
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;
220
221 lstart:
222     do
223            {
224                   NSData *d = [NSData alloc];
225                   readLen = recv(sockfd,buf,bufs,0);
226                   [d initWithBytesNoCopy:buf length:readLen];
227                   [readPipe writeData:d];
228                   [d release];
229                   [dp dataRecieved:self];
230            }
231     while (!actionflag);
232     actionflag = 0;
233     if (dieflag)
234            {
235            free(buf);
236            [dcon release];
237            [ap release];
238            dieflag = 0;
239            return;
240            }
241
242     {
243            NSData *d = [writePipe readAllData];
244            write(sockfd,[d bytes],[d length]);
245            goto lstart;
246            }
247 }
248 @end