More socket work
[ITFoundation.git] / ITInetSocket.m
index 0be64d5..4e40628 100755 (executable)
@@ -7,14 +7,29 @@
 //
 
 #import "ITInetSocket.h"
+#import "ITServiceBrowserDelegate.h"
 #import <sys/socket.h>
 #import <arpa/inet.h>
 
+
 @interface ITInetSocket(Debugging)
 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai;
 @end
 
+@interface ITInetSocket(Private)
+-(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort;
+@end
+
 @implementation ITInetSocket
++(void)startAutoconnectingToService:(NSString*)type delegate:(id)d
+{
+    NSNetServiceBrowser *browse = [[NSNetServiceBrowser alloc] init];
+    ITServiceBrowserDelegate *bd = [[ITServiceBrowserDelegate alloc] initWithDelegate:d];
+
+    [browse setDelegate:bd];
+    [browse searchForServicesOfType:[NSString stringWithFormat:@"._%@._tcp",type] inDomain:nil];
+}
+
 -(id)initWithFD:(int)fd delegate:(id)d
 {
     if (self = [super init])
           writePipe = [[ITByteStream alloc] init];
           readPipe = [[ITByteStream alloc] init];
           ai = nil;
+          sarr = nil;
           }
     return self;
 }
 
+-(void) dealloc
+{
+    shutdown(sockfd,2);
+    [delegate release];
+    [writePipe release];
+    [readPipe release];
+    if (!sarr) freeaddrinfo(ai);
+    [sarr release];
+}
+
 -(id)initWithDelegate:(id)d
 {
     if (self = [super init])
@@ -41,6 +67,7 @@
           writePipe = [[ITByteStream alloc] init];
           readPipe = [[ITByteStream alloc] init];
           ai = nil;
+          sarr = nil;
           }
     return self;
 }
 {
     if (state == ITInetSocketDisconnected)
           {
-          struct addrinfo hints;
-          int err;
-          const char *portNam = [[[NSNumber numberWithShort:thePort] stringValue] cString], *hostCStr = [host cString];
-
-          hints.ai_flags = 0;
-          hints.ai_family = PF_INET6;
-          hints.ai_socktype = SOCK_STREAM;
-          hints.ai_protocol = IPPROTO_TCP;
-          hints.ai_canonname = NULL;
-          hints.ai_addr = NULL;
-          hints.ai_next = NULL;
+          NSString *nport = [[NSNumber numberWithShort:thePort] stringValue];
+          [self doConnSetupWithHost:host namedPort:nport];
+          }
+}
 
-          err = getaddrinfo(hostCStr,portNam,&hints,&ai);
-          if (err == EAI_NODATA) //it's a dotted-decimal IPv4 string, so we use v6compat stuff now
+-(void) connectToHost:(NSString*)host onNamedPort:(NSString*)_port
+{
+    if (state == ITInetSocketDisconnected)
           {
-                 err = getaddrinfo([[NSString stringWithFormat:@"ffff::%s",hostCStr] cString],portNam,&hints,&ai);
+          [self doConnSetupWithHost:host namedPort:_port];
           }
-          NSLog([self dumpv6Addrinfo:ai]);
+}
+
+-(void) connectWithSockaddrArray:(NSArray*)arr
+{
+    NSEnumerator *e = [arr objectEnumerator];
+    NSData *d;
+    struct addrinfo *a;
+    ai = malloc(sizeof(struct addrinfo));
+    a = ai;
+    while (d = [e nextObject])
+          {
+          struct sockaddr *s = (struct sockaddr*)[d bytes];
+          a->ai_family = s->sa_family;
+          a->ai_addr = s;
+          a->ai_next = malloc(sizeof(struct addrinfo));
+          a = a->ai_next;
           }
 }
 
     "\tFamily = %x\n"
     "\tPort = %d\n"
     "\tFlowinfo = %x\n"
-    "\tAddr = {%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx}\n"
+    "\tAddr = {%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx}\n"
     "\tScope = %x\n"
     "}\n"
     "Next = ";
     [buf appendString:@"nil\n}"];
     return buf;
 }
+@end
+
+@implementation ITInetSocket(Private)
+-(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort
+{
+    struct addrinfo hints;
+          int err;
+          const char *portNam = [namedPort cString], *hostCStr = [host cString];
+
+          hints.ai_flags = 0;
+          hints.ai_family = PF_INET6;
+          hints.ai_socktype = SOCK_STREAM;
+          hints.ai_protocol = IPPROTO_TCP;
+          hints.ai_canonname = NULL;
+          hints.ai_addr = NULL;
+          hints.ai_next = NULL;
+
+          err = getaddrinfo(hostCStr,portNam,&hints,&ai);
+          if (err == EAI_NODATA) //it's a dotted-decimal IPv4 string, so we use v6compat stuff now
+          {
+                 err = getaddrinfo([[NSString stringWithFormat:@"ffff::%s",hostCStr] cString],portNam,&hints,&ai);
+          }
+          NSLog([self dumpv6Addrinfo:ai]);
+}
 @end
\ No newline at end of file