5 // Created by Alexander Strange on Tue Feb 11 2003.
6 // Copyright (c) 2003 __MyCompanyName__. All rights reserved.
9 #import "ITInetSocket.h"
10 #import <sys/socket.h>
13 @interface ITInetSocket(Debugging)
14 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai;
17 @implementation ITInetSocket
18 -(id)initWithFD:(int)fd delegate:(id)d
20 if (self = [super init])
22 state = ITInetSocketListening;
24 delegate = [d retain];
26 writePipe = [[ITByteStream alloc] init];
27 readPipe = [[ITByteStream alloc] init];
33 -(id)initWithDelegate:(id)d
35 if (self = [super init])
37 state = ITInetSocketDisconnected;
39 delegate = [d retain];
41 writePipe = [[ITByteStream alloc] init];
42 readPipe = [[ITByteStream alloc] init];
48 -(void) connectToHost:(NSString*)host onPort:(short)thePort
50 if (state == ITInetSocketDisconnected)
52 struct addrinfo hints;
54 const char *portNam = [[[NSNumber numberWithShort:thePort] stringValue] cString], *hostCStr = [host cString];
57 hints.ai_family = PF_INET6;
58 hints.ai_socktype = SOCK_STREAM;
59 hints.ai_protocol = IPPROTO_TCP;
60 hints.ai_canonname = NULL;
64 err = getaddrinfo(hostCStr,portNam,&hints,&ai);
65 if (err == EAI_NODATA) //it's a dotted-decimal IPv4 string, so we use v6compat stuff now
67 err = getaddrinfo([[NSString stringWithFormat:@"ffff::%s",hostCStr] cString],portNam,&hints,&ai);
69 NSLog([self dumpv6Addrinfo:ai]);
73 -(ITInetSocketState)state
79 @implementation ITInetSocket(Debugging)
80 -(NSString*)dumpv6Addrinfo:(struct addrinfo *)_ai
95 "\tAddr = {%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx}\n"
99 NSString *nsfmt = [NSString stringWithCString:cfmt];
100 NSMutableString *buf = [[[NSMutableString alloc] init] autorelease];
104 struct sockaddr_in6 *sa = (struct sockaddr_in6 *)_ai->ai_addr;
105 [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];
107 while (_ai = _ai->ai_next);
108 [buf appendString:@"nil\n}"];