Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Will the NAT64(IPv6-Only Networks) does not support ? #405

Closed
tanshio opened this issue Feb 7, 2016 · 8 comments
Closed

Will the NAT64(IPv6-Only Networks) does not support ? #405

tanshio opened this issue Feb 7, 2016 · 8 comments

Comments

@tanshio
Copy link

tanshio commented Feb 7, 2016

Hi.

I use GCDAsyncSoclet.
Will the NAT64(IPv6-Only Networks) does not support ?
I did the work in accordance with the procedure.

719_your_app_and_next_generation_networks_pdf
link

But can't move CocoaAsyncSocket.

[socket connectToHost:SERVER onPort:PORT error:&err]

lost connect Error Domain=NSPOSIXErrorDomain Code=51 "Network is unreachable" UserInfo={NSLocalizedDescription=Network is unreachable, NSLocalizedFailureReason=Error in connect() function}

Won't you please,help me.

@ogkent
Copy link

ogkent commented Feb 9, 2016

Set IPv4PreferredOverIPv6 = NO. See issue #315 for more info.

@tanshio
Copy link
Author

tanshio commented Feb 10, 2016

Thanks.

IPv4PreferredOverIPv6 = NO

It was already set.

But,IP Address can't work.

Domain
[socket2 connectToHost:@"example.com" onPort:8084 error:&err]

Log

GCDAsyncSocket: Dispatching DNS lookup...
GCDAsyncSocket: lookupHost:port:error:
GCDAsyncSocket: readDataWithTimeout:buffer:bufferOffset:maxLength:tag:
GCDAsyncSocket: maybeDequeueRead
GCDAsyncSocket: lookup:didSucceedWithAddress4:address6:
GCDAsyncSocket: connectWithAddress4:address6:error:
GCDAsyncSocket: IPv4: xxx.xxx.xxx.xxx:8084
GCDAsyncSocket: IPv6: xx:xxxx::xxxx:xxxx:8084
GCDAsyncSocket: Creating IPv6 socket
GCDAsyncSocket: Binding socket...

It worked.

But

IP
[socket2 connectToHost:@"xxx.xxx.xxx.xxx" onPort:8084 error:&err]

Log

GCDAsyncSocket: Dispatching DNS lookup...
GCDAsyncSocket: lookupHost:port:error:
GCDAsyncSocket: readDataWithTimeout:buffer:bufferOffset:maxLength:tag:
GCDAsyncSocket: maybeDequeueRead
GCDAsyncSocket: lookup:didSucceedWithAddress4:address6:
GCDAsyncSocket: connectWithAddress4:address6:error:
GCDAsyncSocket: IPv4: xxx.xxx.xxx.xxx:0
GCDAsyncSocket: IPv6: xx:xxxx::xxxx:xxxx:0
GCDAsyncSocket: Creating IPv6 socket
GCDAsyncSocket: Binding socket...

Port Number set 0.
Why?

@ogkent
Copy link

ogkent commented Feb 10, 2016

Looks like you're trying to use an IPv4 address. Of course IPv6 won't work. You need an IPv6 address, e.g. 2001:db8:85a3:0:0:8a2e:370:7334.

@tanshio
Copy link
Author

tanshio commented Feb 16, 2016

Thanks.

I created method.

-(NSString *)convertHostToAddress:(NSString *)host {

    NSError *err = nil;

    NSMutableArray *addresses = [GCDAsyncSocket lookupHost:host port:0 error:&err];

    NSLog(@"address%@",addresses);

    NSData *address4 = nil;
    NSData *address6 = nil;

    for (NSData *address in addresses)
    {
        if (!address4 && [GCDAsyncSocket isIPv4Address:address])
        {
            address4 = address;
        }
        else if (!address6 && [GCDAsyncSocket isIPv6Address:address])
        {
            address6 = address;
        }
    }

    NSString *ip;

    if (address6) {
        NSLog(@"ipv6%@",[GCDAsyncSocket hostFromAddress:address6]);
        ip = [GCDAsyncSocket hostFromAddress:address6];
    }else {
        NSLog(@"ipv4%@",[GCDAsyncSocket hostFromAddress:address4]);
        ip = [GCDAsyncSocket hostFromAddress:address4];
    }

    return ip;

}

@tanshio tanshio closed this as completed Feb 16, 2016
@newacct
Copy link

newacct commented Mar 19, 2016

You shouldn't need to do that because connectToHost: already calls lookupHost:

@jadynJT
Copy link

jadynJT commented Jun 22, 2016

great

@JeroldLiu777
Copy link

JeroldLiu777 commented Nov 28, 2016

ok, share my answer.works for me.

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;
...

4.
+ (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

@ashishgupta301
Copy link

In the following method we get address4 and address6,
+(BOOL)connectWithAddress4:(NSData *)address4 address6:(NSData *)address6 error:(NSError **)errPtr

so not working for IPV4. Working fine with IPV6

Any idea about it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants