CocoaAsyncSocket|CocoaAsyncSocket (GCDAsyncSocket)适配IPv6
【CocoaAsyncSocket|CocoaAsyncSocket (GCDAsyncSocket)适配IPv6】底层采用了第三方的socket库:CocoaAsyncSocket,里面包含了GCDAsyncSocket.h和GCDAsyncSocket.m文件。阅读源码可以发现,GCDAsyncSocket中已经对ipv4和ipv6同时做了支持,但是为何在ipv6情况下会connect失败呢。根据代码执行过程可以发现,在方法
- (BOOL)connectWithAddress4:(NSData *)address4 address6:(NSData *)address6 error:(NSError **)errPtr;
中 BOOL useIPv6变量被置为NO,从而导致代码不执行ipv6的创建操作,而是执行ipv4的创建,从而导致连接始终失败。找到了问题的原因,下面就可以对其进行处理解决。对上文提到的方法进行修改。代码如下:
- (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]);
//增加的代码
if(address6)
{
[self setIPv6Enabled:YES];
}
……
}
另外在setIPv6Enable方法中做一些修改,代码如下:
- (void)setIPv6Enabled:(BOOL)flag
{
// Note:YES means kIPv6Disabled is OFFdispatch_block_t block = ^{if (flag)
config |= kPreferIPv6;
//修改后代码
else
config |= kIPv6Disabled;
};
if (dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey))
block();
else
dispatch_async(socketQueue, block);
}
然而,网上基本上所有人都是如上方法去适配,结果是丝毫不起。最后在一个以讹传讹的文章下的评论里,找到了正确答案。
GCDAsyncSocket.m中的
- (BOOL)connectWithAddress4:(NSData *)address4 address6:(NSData *)address6 error:(NSError **)errPtr;
方法中加入...
if (address6) {
[self setPreferIPv4OverIPv6:NO];
}
BOOL preferIPv6 = (config & kPreferIPv6) ? YES : NO;
...
下面这段是关键
然后在·
+ (NSMutableArray *)lookupHost:(NSString *)host port:(uint16_t)port error:(NSError **)errPtr;
方法,找到else if (res->ai_family == AF_INET6)
把方法体内全部替换为以下代码: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];
推荐阅读
- CocoaAsyncSocket源码分析---Read(上)
- iOS-Socket(原生)-下篇为CocoaAsyncSocket
- 使用GCDAsyncSocket应用在后台能接受消息吗?
- CocoaAsyncSocket实现FTP服务器