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;
23 @implementation ITInetSocket
24 +(void)startAutoconnectingToService:(NSString*)type delegate:(id)d
26 NSNetServiceBrowser *browse = [[NSNetServiceBrowser alloc] init];
27 ITServiceBrowserDelegate *bd = [[ITServiceBrowserDelegate alloc] initWithDelegate:d];
29 [browse setDelegate:bd];
30 [browse searchForServicesOfType:[NSString stringWithFormat:@"._%@._tcp",type] inDomain:nil];
33 -(id)initWithFD:(int)fd delegate:(id)d
35 if (self = [super init])
37 state = ITInetSocketListening;
39 delegate = [d retain];
41 writePipe = [[ITByteStream alloc] init];
42 readPipe = [[ITByteStream alloc] init];
55 if (!sarr) freeaddrinfo(ai);
59 -(id)initWithDelegate:(id)d
61 if (self = [super init])
63 state = ITInetSocketDisconnected;
65 delegate = [d retain];
67 writePipe = [[ITByteStream alloc] init];
68 readPipe = [[ITByteStream alloc] init];
75 -(void) connectToHost:(NSString*)host onPort:(short)thePort
77 if (state == ITInetSocketDisconnected)
79 NSString *nport = [[NSNumber numberWithShort:thePort] stringValue];
80 [self doConnSetupWithHost:host namedPort:nport];
84 -(void) connectToHost:(NSString*)host onNamedPort:(NSString*)_port
86 if (state == ITInetSocketDisconnected)
88 [self doConnSetupWithHost:host namedPort:_port];
92 -(void) connectWithSockaddrArray:(NSArray*)arr
94 NSEnumerator *e = [arr objectEnumerator];
97 ai = malloc(sizeof(struct addrinfo));
99 while (d = [e nextObject])
101 struct sockaddr *s = (struct sockaddr*)[d bytes];
102 a->ai_family = s->sa_family;
104 a->ai_next = malloc(sizeof(struct addrinfo));
109 -(ITInetSocketState)state
115 @implementation ITInetSocket(Debugging)
116 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai
131 "\tAddr = {%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx}\n"
135 NSString *nsfmt = [NSString stringWithCString:cfmt];
136 NSMutableString *buf = [[[NSMutableString alloc] init] autorelease];
140 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)_ai->ai_addr;
141 [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];
143 while (_ai = _ai->ai_next);
144 [buf appendString:@"nil\n}"];
149 @implementation ITInetSocket(Private)
150 -(void)doConnSetupWithHost:(NSString*)host namedPort:(NSString*)namedPort
152 struct addrinfo hints;
154 const char *portNam = [namedPort cString], *hostCStr = [host cString];
157 hints.ai_family = PF_INET6;
158 hints.ai_socktype = SOCK_STREAM;
159 hints.ai_protocol = IPPROTO_TCP;
160 hints.ai_canonname = NULL;
161 hints.ai_addr = NULL;
162 hints.ai_next = NULL;
164 err = getaddrinfo(hostCStr,portNam,&hints,&ai);
165 if (err == EAI_NODATA) //it's a dotted-decimal IPv4 string, so we use v6compat stuff now
167 err = getaddrinfo([[NSString stringWithFormat:@"ffff::%s",hostCStr] cString],portNam,&hints,&ai);
169 NSLog([self dumpv6Addrinfo:ai]);