How to Test/Connect to IPv6 on Xamarin Mac

AllanChin.6924AllanChin.6924 USUniversity ✭✭✭

I have 2 network interfaces on my iMac, ethernet and wifi. There's a device with an IPv6 address connected to the wifi network. I want to test its reachability and ultimately connect to it. I can ping6 it, but only if I specify "en1" as the interface.

_[01:43 PM]~/Projects/Gotham_Trunk $ ping6 -I en1 -c 3 fe80::aee2:d3ff:feff:46c7
PING6(56=40+8+8 bytes) fe80::148a:e373:77b1:5c2b%en1 --> fe80::aee2:d3ff:feff:46c7
16 bytes from fe80::aee2:d3ff:feff:46c7%en1, icmp_seq=0 hlim=64 time=95.474 ms
16 bytes from fe80::aee2:d3ff:feff:46c7%en1, icmp_seq=1 hlim=64 time=610.481 ms
16 bytes from fe80::aee2:d3ff:feff:46c7%en1, icmp_seq=2 hlim=64 time=521.072 ms

--- fe80::aee2:d3ff:feff:46c7 ping6 statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/std-dev = 95.474/409.009/610.481/224.687 ms_

The Ping class returns status "Unknown". If I bind a Socket with each of the local endpoints, and try to ConnectAsync(), the 2 network endpoints fail with "No route to host". And finally, I can't seem to get the NetworkReachablity class to even work with just an IP address, much less with an IP address and a local endpoint address.

IPv4 works without have to go though any of this.

Does anybody know how I can get this to work?

I've attached an IPv6_Test app which exercises all 3 of the methods I described above. In the ViewController code, I have IP addresses hard-coded.

    //IPAddress ipAddress = IPAddress.Parse("15.1.195.196");
    IPAddress ipAddress = IPAddress.Parse("[fe80::aee2:d3ff:feff:46c7]");

Any help would be much appreciated.

Thanks

This is my dev environment.

=== Visual Studio Community 2019 for Mac ===

Version 8.1.5 (build 9)
Installation UUID: 52841cf1-dbf0-469c-a714-a263f1d0573b
GTK+ 2.24.23 (Raleigh theme)
Xamarin.Mac 5.6.0.25 (d16-0 / 50f75273)

Package version: 518010028

=== Mono Framework MDK ===

Runtime:
Mono 5.18.1.28 (2018-08/223ea7ef92e) (64-bit)
Package version: 518010028

=== NuGet ===

Version: 5.0.2.5988

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
2.1.11
2.1.9
2.0.5
2.0.0
SDK: /usr/local/share/dotnet/sdk/2.1.700/Sdks
SDK Versions:
2.1.700
2.1.505
2.1.4
2.0.0
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.18.1/lib/mono/msbuild/Current/bin/Sdks

=== Xamarin.Profiler ===

'/Applications/Xamarin Profiler.app' not found

=== Updater ===

Version: 11

=== Xamarin.Android ===

Version: 9.3.0.23 (Visual Studio Community)
Commit: HEAD/d0b48056f
Android SDK: /Users/allan/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
6.0 (API level 23)
7.1 (API level 25)
8.1 (API level 27)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 26.0.0
SDK Build Tools Version: 25.0.3

Build Information:
Mono: mono/mono/[email protected]
Java.Interop: xamarin/java.interop/[email protected]
LibZipSharp: grendello/LibZipSharp/[email protected]
LibZip: nih-at/libzip/[email protected]
ProGuard: xamarin/proguard/[email protected]
SQLite: xamarin/sqlite/[email protected]
Xamarin.Android Tools: xamarin/xamarin-android-tools/[email protected]

=== Microsoft Mobile OpenJDK ===

Java SDK: /Users/allan/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android Device Manager ===

Version: 1.2.0.44
Hash: aac645b
Branch: remotes/origin/d16-1
Build date: 2019-05-29 19:55:24 UTC

=== Xamarin Inspector ===

Version: 1.4.3
Hash: db27525
Branch: 1.4-release
Build date: Mon, 09 Jul 2018 21:20:18 GMT
Client compatibility: 1

=== Apple Developer Tools ===

Xcode 10.2.1 (14490.122)
Build 10G8

=== Xamarin.Mac ===

Version: 5.10.0.157 (Visual Studio Community)
Hash: 6bd94753
Branch: d16-1
Build date: 2019-06-12 17:28:48-0400

=== Xamarin.iOS ===

Version: 12.10.0.157 (Visual Studio Community)
Hash: 6bd94753
Branch: d16-1
Build date: 2019-06-12 17:28:47-0400

=== Xamarin Designer ===

Version: 16.1.0.467
Hash: f1657e133
Branch: remotes/origin/d16-1-new-document-model
Build date: 2019-06-18 21:57:42 UTC

=== Build Information ===

Release ID: 801050009
Git revision: bd0ab28ba941b19b39322247db020dcd0fb305d0
Build date: 2019-07-03 17:15:21+00
Build branch: release-8.1
Xamarin extensions: 8cc25b5cb090e6c23b62a7901000c299977eb08d

=== Operating System ===

Mac OS X 10.14.5
Darwin 18.6.0 Darwin Kernel Version 18.6.0
Thu Apr 25 23:16:27 PDT 2019
root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64

Answers

  • mandelmandel ESXamarin Team Xamurai

    Hello,

    So, to being with, Ping cannot be used, the Ping class for mono does not support IPv6 (https://github.com/mono/mono/blob/master/mcs/class/System/System.Net.NetworkInformation/Ping.cs#L45). Therefore it is a route that we cannot follow.

    With my setup, I simplified you socket connect code (bellow) and I was able to connect with no problems providing a IPv6 address. So I suspect there must be something interesting in your setup that does not happen in mine :/

    public async Task<bool> TestConnectAsync ()
    {
        if (!Socket.SupportsIPv6) {
            Console.WriteLine ("Your system does not support IPV6!");
            return false;
        }
        var port = 10000; // this depends on your local configuration, you can use the network utility tools to test for open ports
        IPHostEntry entry = Dns.GetHostEntry (Dns.GetHostName ());
        var localAddresses = new List<IPAddress> (entry.AddressList);
        foreach (var address in localAddresses) {
            // check if the address is ipv6, else continue since we will get wrong argument exceptions with the currenct code.
            if (address.AddressFamily == AddressFamily.InterNetwork)
                continue; 
            try {
                var localEndPoint = new IPEndPoint (address, port);
                using (var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)) {
                    client.Bind (localEndPoint);
                    await client.ConnectAsync (address, port);
                    if (client.Connected)
                        return true;
    
                    Console.WriteLine ("failed to connect to " + localEndPoint.Address);
                }
            } catch (Exception ex) {
                Console.WriteLine ($"Excpetion received {ex.Message} for address {address}");
            }
        }
        return false;
    }
    

    Could you please try the following:

    1. Try to connect to the device via telnet, you can use IPv6 in telnet using the -6 flag > telnet -6 address port
    2. If you can connect via telnet, can you please try the simplified version of the code I posted?

    There is a different approach I can give you, which is using the new Network.framework from Apple. Miguel wrote a nice port of their sample here: https://github.com/migueldeicaza/NetCatNetwork which you can use a a reference to write a client. But I would consider this the last option.

    So, the way I see it:

    1. Forget about Ping, that does not support IPv6
    2. Try to telnet to the device, does it work, try my code. If my code is not working in your setup, there is an issue in the BCL from mono.
    3. Try the netcat application from Miguel using the Network.framework from Apple. Does is work and Sockets do not? We create an issue on mono with the specific settings you have and use the native framework we have bindings for.

    For reference, I used a simple echo tcp server written in python to test the socket connection:

    import socket
    import sys
    
    # Create a TCP/IP socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    # Bind the socket to the port
    server_address = ('localhost', 10000)
    print >>sys.stderr, 'starting up on %s port %s' % server_address
    sock.bind(server_address)
    # Listen for incoming connections
    sock.listen(1)
    
    while True:
        # Wait for a connection
        print >>sys.stderr, 'waiting for a connection'
        connection, client_address = sock.accept()
        try:
            print >>sys.stderr, 'connection from', client_address
    
            # Receive the data in small chunks and retransmit it
            while True:
                data = connection.recv(16)
                print >>sys.stderr, 'received "%s"' % data
                if data:
                    print >>sys.stderr, 'sending data back to the client'
                    connection.sendall(data)
                else:
                    print >>sys.stderr, 'no more data from', client_address
                    break
    
        finally:
            # Clean up the connection
               connection.close()
    

    PS: Sorry, looks like the forums decided to remove my previous comment.

  • AllanChin.6924AllanChin.6924 USUniversity ✭✭✭

    Hi Mandel,

    Thanks for your response. Here are the results of your recommended steps.

    1. ping6 still works, but telnet fails.

    [08:05 AM]~/Projects/Gotham_Trunk $ ping6 -I en1 -c 3 fe80::aee2:d3ff:feff:46c7
    PING6(56=40+8+8 bytes) fe80::1c04:92dc:b13:5797%en1 --> fe80::aee2:d3ff:feff:46c7
    16 bytes from fe80::aee2:d3ff:feff:46c7%en1, icmp_seq=0 hlim=64 time=461.173 ms
    16 bytes from fe80::aee2:d3ff:feff:46c7%en1, icmp_seq=1 hlim=64 time=72.142 ms
    16 bytes from fe80::aee2:d3ff:feff:46c7%en1, icmp_seq=2 hlim=64 time=602.725 ms

    --- fe80::aee2:d3ff:feff:46c7 ping6 statistics ---
    3 packets transmitted, 3 packets received, 0.0% packet loss
    round-trip min/avg/max/std-dev = 72.142/378.680/602.725/224.326 ms

    [08:05 AM]~/Projects/Gotham_Trunk $ telnet -6 fe80::aee2:d3ff:feff:46c7 8080
    Trying fe80::aee2:d3ff:feff:46c7...
    telnet: Unable to connect to remote host: No route to host
    [08:05 AM]~/Projects/Gotham_Trunk $

    1. Running your code yields this in the output window. Although, I don't see the point of this test since the attempts are trying to connect to local endpoints, rather than remote endpoints.

    Loaded assembly: /Users/allan/Projects/achin_Mac_tests/IPv6_Test/bin/Debug/IPv6_Test.app/Contents/MonoBundle/System.Core.dll [External]
    Excpetion received The requested address is not valid in this context for address fe80::18aa:3d84:c797:4050
    Excpetion received The requested address is not valid in this context for address fe80::1c04:92dc:b13:5797
    Excpetion received The requested address is not valid in this context for address fe80::34d4:33ff:fef9:91f7
    Excpetion received The requested address is not valid in this context for address fe80::7a45:6b23:a52b:1b0b
    ConnectAsync() return False

    1. And finally, as for Miguel's ported app, there seems to be an issue. My test gets this

    [08:25 AM]~/Projects/NetCatNetwork-master $ ./ncsharp -6 fe80::aee2:d3ff:feff:46c7 8080
    2019-08-02 08:26:50.440 ncsharp[16946:49507]
    Unhandled Exception:
    System.MethodAccessException: Method Mono.SystemDependencyProvider.Initialize()' is inaccessible from methodObjCRuntime.Runtime.Initialize(ObjCRuntime.Runtime/InitializationOptions*)'
    at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_throw_method_access(intptr,intptr)
    at ObjCRuntime.Runtime.Initialize (ObjCRuntime.Runtime+InitializationOptions* options) [0x00290] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    --- End of stack trace from previous location where exception was thrown ---

    at (wrapper managed-to-native) System.Object.wrapper_native_0x10e271d10()
    at ObjCRuntime.Runtime.EnsureInitialized () [0x00051] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at AppKit.NSApplication.Init () [0x00016] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at X.Main (System.String[] args) [0x00159] in :0
    [ERROR] FATAL UNHANDLED EXCEPTION: System.MethodAccessException: Method Mono.SystemDependencyProvider.Initialize()' is inaccessible from methodObjCRuntime.Runtime.Initialize(ObjCRuntime.Runtime/InitializationOptions*)'
    at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_throw_method_access(intptr,intptr)
    at ObjCRuntime.Runtime.Initialize (ObjCRuntime.Runtime+InitializationOptions* options) [0x00290] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    --- End of stack trace from previous location where exception was thrown ---

    at (wrapper managed-to-native) System.Object.wrapper_native_0x10e271d10()
    at ObjCRuntime.Runtime.EnsureInitialized () [0x00051] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at AppKit.NSApplication.Init () [0x00016] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at X.Main (System.String[] args) [0x00159] in :0

    which is the same when I try to connect to google.

    [08:26 AM]~/Projects/NetCatNetwork-master $ ./ncsharp www.google.com 80
    2019-08-02 08:28:02.685 ncsharp[16950:49793]
    Unhandled Exception:
    System.MethodAccessException: Method Mono.SystemDependencyProvider.Initialize()' is inaccessible from methodObjCRuntime.Runtime.Initialize(ObjCRuntime.Runtime/InitializationOptions*)'
    at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_throw_method_access(intptr,intptr)
    at ObjCRuntime.Runtime.Initialize (ObjCRuntime.Runtime+InitializationOptions* options) [0x00290] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    --- End of stack trace from previous location where exception was thrown ---

    at (wrapper managed-to-native) System.Object.wrapper_native_0x108234d10()
    at ObjCRuntime.Runtime.EnsureInitialized () [0x00051] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at AppKit.NSApplication.Init () [0x00016] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at X.Main (System.String[] args) [0x00159] in :0
    [ERROR] FATAL UNHANDLED EXCEPTION: System.MethodAccessException: Method Mono.SystemDependencyProvider.Initialize()' is inaccessible from methodObjCRuntime.Runtime.Initialize(ObjCRuntime.Runtime/InitializationOptions*)'
    at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_throw_method_access(intptr,intptr)
    at ObjCRuntime.Runtime.Initialize (ObjCRuntime.Runtime+InitializationOptions* options) [0x00290] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    --- End of stack trace from previous location where exception was thrown ---

    at (wrapper managed-to-native) System.Object.wrapper_native_0x108234d10()
    at ObjCRuntime.Runtime.EnsureInitialized () [0x00051] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at AppKit.NSApplication.Init () [0x00016] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at X.Main (System.String[] args) [0x00159] in :0

    [08:28 AM]~/Projects/NetCatNetwork-master $ ./ncsharp www.google.com https
    2019-08-02 08:28:18.694 ncsharp[16951:49867]
    Unhandled Exception:
    System.MethodAccessException: Method Mono.SystemDependencyProvider.Initialize()' is inaccessible from methodObjCRuntime.Runtime.Initialize(ObjCRuntime.Runtime/InitializationOptions*)'
    at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_throw_method_access(intptr,intptr)
    at ObjCRuntime.Runtime.Initialize (ObjCRuntime.Runtime+InitializationOptions* options) [0x00290] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    --- End of stack trace from previous location where exception was thrown ---

    at (wrapper managed-to-native) System.Object.wrapper_native_0x108c5ad10()
    at ObjCRuntime.Runtime.EnsureInitialized () [0x00051] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at AppKit.NSApplication.Init () [0x00016] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at X.Main (System.String[] args) [0x00159] in :0
    [ERROR] FATAL UNHANDLED EXCEPTION: System.MethodAccessException: Method Mono.SystemDependencyProvider.Initialize()' is inaccessible from methodObjCRuntime.Runtime.Initialize(ObjCRuntime.Runtime/InitializationOptions*)'
    at (wrapper managed-to-native) System.Object.__icall_wrapper_mono_throw_method_access(intptr,intptr)
    at ObjCRuntime.Runtime.Initialize (ObjCRuntime.Runtime+InitializationOptions* options) [0x00290] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    --- End of stack trace from previous location where exception was thrown ---

    at (wrapper managed-to-native) System.Object.wrapper_native_0x108c5ad10()
    at ObjCRuntime.Runtime.EnsureInitialized () [0x00051] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at AppKit.NSApplication.Init () [0x00016] in <5c5a988d72064428b8b9bf4944acf6ef>:0
    at X.Main (System.String[] args) [0x00159] in :0
    [08:28 AM]~/Projects/NetCatNetwork-master $

  • mandelmandel ESXamarin Team Xamurai

    Hmm that is unfortunate. A few things we can do:

    1. Localhost telnet testing. Can you run the sample tcp echo server I posted in there on you localhost and try to connect to it.
    2. I did know we were trying to connect to local address, is a test I wanted to see. Looks like the IPv6 address are not being correctly routed. Can you please let me know the setup of you network? Is it a mix network? Do you have an IPv6 address for your local machine in the System Preferences -> Network
    3. I will create a fix for Miguels port and will post it here.

    Please gather as much info as possible about the network setup, we can also try to use the IPv6 on a diff framework to see if that works (I am quite good with Qt and we can use it as a reference) if needed and move from there.

  • AllanChin.6924AllanChin.6924 USUniversity ✭✭✭

    Well, normally I have an ethernet and a wifi connection on my iMac. But, I just ran a very simplified test where the ethernet was disconnected, and the Wifi router was disconnected from any broadband. So, the Wifi router was just a router, and it's operating at 5 GHz. Ping6 was still successfully sending and receiving packets.

    Running my TH produced the same results, "No route to host"

    I've also attached some screenshots of our router configuration. I tried with, and without the firewall.

    As for the echo tcp server script, it waited for over a minute "waiting for a connection".

    [09:18 AM]~/Projects $ python echoserver.py
    starting up on localhost port 10000
    waiting for a connection
    ^CTraceback (most recent call last):
    File "echoserver.py", line 17, in
    connection, client_address = sock.accept()
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 206, in accept
    sock, addr = self._sock.accept()
    KeyboardInterrupt
    [09:20 AM]~/Projects $

Sign In or Register to comment.