最近有个app因为ipv6的原因被拒
首先在本地搭建一个IPV6的测试环境,使用mac搭建详情请看 http://blog.csdn.net/yuwuchaio/article/details/51459705
结果发现udp 请求发出去了完全没有回应
用的是第三方的 AsyncUdpSocket
找了一下资料发现这个,里面的回复最后一条已经给出了答案https://github.com/robbiehanson/CocoaAsyncSocket/issues/405
ok, share my answer.
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
但是我用的并不是GCDAsyncUdpSocket 而是AsyncUdpSocket
就持续跟了一下AsyncUdpSocket.m文件,发现- (BOOL)sendData:(NSData *)data
toHost:(NSString *)host
port:(UInt16)port
withTimeout:(NSTimeInterval)timeout
tag:(long)tag
这个方法里面有点问题,我把ipv6和ipv4修改了一个位置,把ipv6放在前面了
- (BOOL)sendData:(NSData *)data
toHost:(NSString *)host
port:(UInt16)port
withTimeout:(NSTimeInterval)timeout
tag:(long)tag
{
if([data length] == 0) return NO;
if(theFlags & kForbidSendReceive) return NO;
if(theFlags & kDidClose) return NO;
// This method is only for non-connected sockets
if([self isConnected]) return NO;
NSData *address4 = nil, *address6 = nil;
[self convertForSendHost:host port:port intoAddress4:&address4 address6:&address6];
AsyncSendPacket *packet = nil;
if(address6 && theSocket6)
packet = [[AsyncSendPacket alloc] initWithData:data address:address6 timeout:timeout tag:tag];
else if(address4 && theSocket4)
packet = [[AsyncSendPacket alloc] initWithData:data address:address4 timeout:timeout tag:tag];
else
return NO;
[theSendQueue addObject:packet];
[self scheduleDequeueSend];
return YES;
}