GCDAsyncSocket support IPv6

1.- (BOOL)connectWithAddress4:(NSData *)address4 address6:(NSData *)address6 error:(NSError **)errPtr

{

LogTrace();

NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");

LogVerbose(@"IPv4: %@:%hu", [[self class] hostFromAddress:address4], [[self class] portFromAddress:address4]);

LogVerbose(@"IPv6: %@:%hu", [[self class] hostFromAddress:address6], [[self class] portFromAddress:address6]);

//add codes blow

if(address6)

{

[self setIPv6Enabled:YES];

}

……

}

2.- (void)setIPv6Enabled:(BOOL)flag

{

// Note:YES means kIPv6Disabled is OFF

dispatch_block_t block = ^{

if (flag)

config |= kPreferIPv6;   //replace by this

else

config |= kIPv6Disabled;

};

......

}

3.GCDAsyncSocket.m

(BOOL)connectWithAddress4:(NSData *)address4 address6:(NSData *)address6 error:(NSError **)errPtr;

//add codes blow

...

if (address6) {

[self setIPv4PreferredOverIPv6:NO];

}

BOOL preferIPv6 = (config & kPreferIPv6) ? YES : NO;

...

(NSMutableArray *)lookupHost:(NSString *)host port:(uint16_t)port error:(NSError **)errPtr; find else if (res->ai_family == AF_INET6) branch, then replace by blow:

struct sockaddr_in6 *sockaddr = (struct sockaddr_in6 *)res->ai_addr;

in_port_t *portPtr = &sockaddr->sin6_port;

if ((portPtr != NULL) && (*portPtr == 0)) {

*portPtr = htons(port);

}

NSData *address6 = [NSData dataWithBytes:res->ai_addr length:res->ai_addrlen];

[addresses addObject:address6];

DONE

你可能感兴趣的:(GCDAsyncSocket support IPv6)