diff --git a/.github/workflows/auto-format.yml b/.github/workflows/auto-format.yml index e33cece..b75ad82 100644 --- a/.github/workflows/auto-format.yml +++ b/.github/workflows/auto-format.yml @@ -9,9 +9,6 @@ jobs: - name: Checkout repo uses: actions/checkout@v2 - - name: Install clang-format - run: sudo apt-get install clang-format - - name: Format code run: ./build.sh format-code diff --git a/build.sh b/build.sh index 5060ccc..ade9729 100755 --- a/build.sh +++ b/build.sh @@ -408,6 +408,28 @@ host-pinvoke() cp -f $CACHE_DIR/lib/libzt.* $LIB_OUTPUT_DIR echo -e "\n - Build cache : $CACHE_DIR\n - Build output : $BUILD_OUTPUT_DIR\n" $TREE $TARGET_BUILD_DIR + + # Test C# + if [[ $2 = *"test"* ]]; then + if [[ -z "${alice_path}" ]]; then + echo "Please set necessary environment variables for test" + exit 0 + fi + # TODO: This should eventually be converted to a proper dotnet project + # Build C# managed API library + # -doc:$LIB_OUTPUT_DIR/ZeroTier.Sockets.xml + csc -target:library -out:$LIB_OUTPUT_DIR/ZeroTier.Sockets.dll src/bindings/csharp/*.cs + # Build selftest + mkdir -p $BIN_OUTPUT_DIR + csc -out:$BIN_OUTPUT_DIR/selftest.exe -reference:$LIB_OUTPUT_DIR/ZeroTier.Sockets.dll test/selftest.cs + # Copy shared libraries into bin directory so they can be discovered by dlopen + cp $LIB_OUTPUT_DIR/* $BIN_OUTPUT_DIR + # Start Alice as server + MONO_THREADS_SUSPEND=preemptive; mono --debug "$BIN_OUTPUT_DIR/selftest.exe" server $alice_path $testnet $port4 & + sleep 5 + # Start Bob as client + MONO_THREADS_SUSPEND=preemptive; mono --debug "$BIN_OUTPUT_DIR/selftest.exe" client $bob_path $testnet $alice_ip4 $port4 & + fi } # Build shared library with Java JNI wrapper symbols exported (.jar) @@ -560,7 +582,7 @@ format-code() if [[ ! $(which clang-format) = "" ]]; then # Eventually: find . -path ./ext -prune -false -o -type f \( -iname \*.c -o -iname \*.h -o -iname \*.cpp -o -iname \*.hpp \) -exec clang-format -i {} \; - clang-format -i include/*.h \ + clang-format-11 -i include/*.h \ src/*.c \ src/*.cpp \ src/*.hpp \ @@ -597,37 +619,6 @@ test-c() "$BIN_OUTPUT_DIR/selftest-c" $bob_path $testnet $port4 $alice_ip4 $port6 $alice_ip6 & } -# Test C# API -test-cs() -{ - if [[ -z "${alice_path}" ]]; then - echo "Please set necessary environment variables for test" - exit 0 - fi - ARTIFACT="pinvoke" - # Default to debug so asserts aren't optimized out - BUILD_TYPE=${1:-debug} - TARGET_BUILD_DIR=$DEFAULT_HOST_BIN_OUTPUT_DIR-$ARTIFACT-$BUILD_TYPE - LIB_OUTPUT_DIR=$TARGET_BUILD_DIR/lib - BIN_OUTPUT_DIR=$TARGET_BUILD_DIR/bin - rm -rf $TARGET_BUILD_DIR - # Build C shared library exporting C# symbols - host-pinvoke $1 - # TODO: This should eventually be converted to a proper dotnet project - # Build C# managed API library - csc -target:library -doc:$LIB_OUTPUT_DIR/ZeroTier.Sockets.xml -out:$LIB_OUTPUT_DIR/ZeroTier.Sockets.dll src/bindings/csharp/*.cs - # Build selftest - mkdir -p $BIN_OUTPUT_DIR - csc -out:$BIN_OUTPUT_DIR/selftest.exe -reference:$LIB_OUTPUT_DIR/ZeroTier.Sockets.dll test/selftest.cs - # Copy shared libraries into bin directory so they can be discovered by dlopen - cp $LIB_OUTPUT_DIR/* $BIN_OUTPUT_DIR - # Start Alice as server - mono --debug "$BIN_OUTPUT_DIR/selftest.exe" $alice_path $testnet $port4 $port6 & - sleep 10 - # Start Bob as client - mono --debug "$BIN_OUTPUT_DIR/selftest.exe" $bob_path $testnet $port4 $alice_ip4 $port6 $alice_ip6 & -} - # Recursive deep clean clean() { diff --git a/examples/csharp/example.cs b/examples/csharp/example.cs index 80b6c70..7722aaf 100644 --- a/examples/csharp/example.cs +++ b/examples/csharp/example.cs @@ -2,265 +2,265 @@ using System; using System.Threading; using System.Net; -using System.Net.Sockets; // For SocketType, etc -using System.Text; // For Encoding +using System.Net.Sockets; +using System.Text; -/** - * - * Namespaces explained: - * - * ZeroTier.Core (API to control a ZeroTier Node) - * -> class ZeroTier.Core.Node - * -> class ZeroTier.Core.Event - * - * ZeroTier.Sockets (Socket API similar to System.Net.Sockets) - * -> class ZeroTier.Sockets.Socket - * -> class ZeroTier.Sockets.SocketException - * - * ZeroTier.Central (upcoming) - * - */ using ZeroTier; public class ExampleApp { + // ZeroTier Node instance - ZeroTier.Core.Node node; + ZeroTier.Core.Node node; - /** - * Initialize and start ZeroTier - */ - public void StartZeroTier(string configFilePath, ushort servicePort, ulong networkId) - { - node = new ZeroTier.Core.Node(configFilePath, OnZeroTierEvent, servicePort); - node.Start(); // Network activity only begins after calling Start() + // Initialize and start ZeroTier - /* How you do this next part is up to you, but essentially we're waiting for the node - to signal to us via OnZeroTierEvent(ZeroTier.Core.Event) that it has access to the - internet and is able to talk to one of our root servers. As a convenience you can just - periodically check Node.IsOnline() instead of looking for the event via the callback. */ - while (!node.IsOnline()) { Thread.Sleep(100); } + public void StartZeroTier(string configFilePath, ulong networkId) + { + node = new ZeroTier.Core.Node(); - /* After the node comes online you may now join/leave networks. You will receive - notifications via the callback function regarding the status of your join request as well - as any subsequent network-related events such as the assignment of an IP address, added - or removed routes, etc. */ - node.Join(networkId); + // (OPTIONAL) Initialize node - /* Note that ZeroTier.Sockets.Socket calls will fail if there are no routes available, for this - reason we should wait to make those calls until the node has indicated to us that at - least one network has been joined successfully. */ - while (!node.HasRoutes()) { Thread.Sleep(100); } - } + node.InitFromStorage(configFilePath); + node.InitAllowNetworkCaching(false); + node.InitAllowPeerCaching(true); + // node.InitAllowIdentityCaching(true); + // node.InitAllowWorldCaching(false); + node.InitSetEventHandler(OnZeroTierEvent); + node.InitSetPort(0); // Will randomly attempt ports if set to 0 - /** - * Stop ZeroTier - */ - public void StopZeroTier() - { - node.Stop(); - } + // (OPTIONAL) Set custom signed roots - /** - * Your application should process event messages and return control as soon as possible. Blocking - * or otherwise time-consuming operations are not reccomended here. - */ - public void OnZeroTierEvent(ZeroTier.Core.Event e) - { - Console.WriteLine("Event.eventCode = {0} ({1})", e.EventCode, e.EventName); + // In this case we only allow ZeroTier to contact our Amsterdam root server + // To see examples of how to generate and sign roots definitions see docs.zerotier.com - if (e.EventCode == ZeroTier.Constants.EVENT_NODE_ONLINE) { - Console.WriteLine("Node is online"); - Console.WriteLine(" - Address (NodeId): " + node.NodeId.ToString("x16")); - } + var rootsData = new byte[] { + 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0xea, 0xc9, 0x0a, 0x00, 0x00, 0x01, 0x6c, 0xe3, 0xe2, 0x39, 0x55, 0x74, + 0xeb, 0x27, 0x9d, 0xc9, 0xe7, 0x5a, 0x52, 0xbb, 0x91, 0x8f, 0xf7, 0x43, 0x3c, 0xbf, 0x77, 0x5a, 0x4b, 0x57, + 0xb4, 0xe1, 0xe9, 0xa1, 0x01, 0x61, 0x3d, 0x25, 0x35, 0x60, 0xcb, 0xe3, 0x30, 0x18, 0x1e, 0x6e, 0x44, 0xef, + 0x93, 0x89, 0xa0, 0x19, 0xb8, 0x7b, 0x36, 0x0b, 0x92, 0xff, 0x0f, 0x1b, 0xbe, 0x56, 0x5a, 0x46, 0x91, 0x36, + 0xf1, 0xd4, 0x5c, 0x09, 0x05, 0xe5, 0xf5, 0xfb, 0xba, 0xe8, 0x13, 0x2d, 0x47, 0xa8, 0xe4, 0x1b, 0xa5, 0x1c, + 0xcf, 0xb0, 0x2f, 0x27, 0x7e, 0x95, 0xa0, 0xdd, 0x49, 0xe1, 0x7d, 0xc0, 0x7e, 0x6d, 0xe3, 0x25, 0x91, 0x96, + 0xc2, 0x55, 0xf9, 0x20, 0x6d, 0x2a, 0x5e, 0x1b, 0x41, 0xcb, 0x1f, 0x8d, 0x57, 0x27, 0x69, 0x3e, 0xcc, 0x7f, + 0x0b, 0x36, 0x54, 0x6b, 0xd3, 0x80, 0x78, 0xf6, 0xd0, 0xec, 0xb4, 0x31, 0x6b, 0x87, 0x1b, 0x50, 0x08, 0xe4, + 0x0b, 0xa9, 0xd4, 0xfd, 0x37, 0x79, 0x14, 0x6a, 0xf5, 0x12, 0xf2, 0x45, 0x39, 0xca, 0x23, 0x00, 0x39, 0xbc, + 0xa3, 0x1e, 0xa8, 0x4e, 0x23, 0x2d, 0xc8, 0xdb, 0x9b, 0x0e, 0x52, 0x1b, 0x8d, 0x02, 0x72, 0x01, 0x99, 0x2f, + 0xcf, 0x1d, 0xb7, 0x00, 0x20, 0x6e, 0xd5, 0x93, 0x50, 0xb3, 0x19, 0x16, 0xf7, 0x49, 0xa1, 0xf8, 0x5d, 0xff, + 0xb3, 0xa8, 0x78, 0x7d, 0xcb, 0xf8, 0x3b, 0x8c, 0x6e, 0x94, 0x48, 0xd4, 0xe3, 0xea, 0x0e, 0x33, 0x69, 0x30, + 0x1b, 0xe7, 0x16, 0xc3, 0x60, 0x93, 0x44, 0xa9, 0xd1, 0x53, 0x38, 0x50, 0xfb, 0x44, 0x60, 0xc5, 0x0a, 0xf4, + 0x33, 0x22, 0xbc, 0xfc, 0x8e, 0x13, 0xd3, 0x30, 0x1a, 0x1f, 0x10, 0x03, 0xce, 0xb6, 0x00, 0x02, 0x04, 0xc3, + 0xb5, 0xad, 0x9f, 0x27, 0x09, 0x06, 0x2a, 0x02, 0x6e, 0xa0, 0xc0, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x27, 0x09 + }; + node.InitSetRoots(rootsData, rootsData.Length); - if (e.EventCode == ZeroTier.Constants.EVENT_NETWORK_OK) { - Console.WriteLine(" - Network ID: " + e.networkDetails.networkId.ToString("x16")); - } - } + node.Start(); // Network activity only begins after calling Start() + while (! node.Online) { + Thread.Sleep(50); + } - /** - * Example server - */ - public void YourServer(IPEndPoint localEndPoint) { - string data = null; + Console.WriteLine("Id : " + node.IdString); + Console.WriteLine("Version : " + node.Version); + Console.WriteLine("PrimaryPort : " + node.PrimaryPort); + Console.WriteLine("SecondaryPort : " + node.SecondaryPort); + Console.WriteLine("TertiaryPort : " + node.TertiaryPort); - // Data buffer for incoming data. - byte[] bytes = new Byte[1024]; + node.Join(networkId); + Console.WriteLine("Waiting for join to complete..."); + while (node.Networks.Count == 0) { + Thread.Sleep(50); + } - Console.WriteLine(localEndPoint.ToString()); - ZeroTier.Sockets.Socket listener = new ZeroTier.Sockets.Socket(AddressFamily.InterNetwork, - SocketType.Stream, ProtocolType.Tcp ); + // Wait until we've joined the network and we have routes + addresses + Console.WriteLine("Waiting for network to become transport ready..."); + while (! node.IsNetworkTransportReady(networkId)) { + Thread.Sleep(50); + } - // Bind the socket to the local endpoint and - // listen for incoming connections. + Console.WriteLine("Num of assigned addresses : " + node.GetNetworkAddresses(networkId).Count); + foreach (IPAddress addr in node.GetNetworkAddresses(networkId)) { + Console.WriteLine(" - Address: " + addr); + } - try { - listener.Bind(localEndPoint); - listener.Listen(10); + Console.WriteLine("Num of routes : " + node.GetNetworkRoutes(networkId).Count); + foreach (ZeroTier.Core.RouteInfo route in node.GetNetworkRoutes(networkId)) { + Console.WriteLine( + " - Route: target={0} via={1} flags={2} metric={3}", + route.Target.ToString(), + route.Via.ToString(), + route.Flags, + route.Metric); + } + } - // Start listening for connections. - while (true) { - Console.WriteLine("Waiting for a connection..."); - // Program is suspended while waiting for an incoming connection. - bool nonblocking = true; + /** + * Stop ZeroTier + */ + public void StopZeroTier() + { + node.Free(); + } - ZeroTier.Sockets.Socket handler; + /** + * (OPTIONAL) + * + * Your application should process event messages and return control as soon as possible. Blocking + * or otherwise time-consuming operations are not recommended here. + */ + public void OnZeroTierEvent(ZeroTier.Core.Event e) + { + Console.WriteLine("Event.Code = {0} ({1})", e.Code, e.Name); + /* + if (e.Code == ZeroTier.Constants.EVENT_NODE_ONLINE) { + Console.WriteLine("Node is online"); + Console.WriteLine(" - Address (NodeId): " + node.Id.ToString("x16")); + } - if (nonblocking) { // Non-blocking style Accept() loop using Poll() - Console.WriteLine("Starting non-blocking Accept() loop..."); - listener.Blocking = false; - // loop - int timeout = 100000; // microseconds (1 second) - while (true) { - Console.WriteLine("Polling... (for data or incoming connections)"); - if (listener.Poll(timeout, SelectMode.SelectRead)) { - Console.WriteLine("Detected event (SelectRead). Accepting..."); - handler = listener.Accept(); - break; - } - //Thread.Sleep(5); - } - } - else { // Blocking style - Console.WriteLine("Starting blocking Accept() call..."); - handler = listener.Accept(); - } - data = null; - Console.WriteLine("Accepted connection from: " + handler.RemoteEndPoint.ToString()); + if (e.Code == ZeroTier.Constants.EVENT_NETWORK_OK) { + Console.WriteLine(" - Network ID: " + e.NetworkInfo.Id.ToString("x16")); + } + */ + } - // handler.ReceiveTimeout = 1000; + /** + * Example server + */ + public void SocketServer(IPEndPoint localEndPoint) + { + string data = null; - // An incoming connection needs to be processed. - while (true) { - int bytesRec = 0; - try { - Console.WriteLine("Receiving..."); - bytesRec = handler.Receive(bytes); - } - catch (ZeroTier.Sockets.SocketException e) - { - Console.WriteLine("ServiceErrorCode={0} SocketErrorCode={1}", - e.ServiceErrorCode, e.SocketErrorCode); - } - if (bytesRec > 0) { - Console.WriteLine("Bytes received: {0}", bytesRec); - data = Encoding.ASCII.GetString(bytes,0,bytesRec); - Console.WriteLine( "Text received : {0}", data); - //break; - // Echo the data back to the client. - byte[] msg = Encoding.ASCII.GetBytes(data); - handler.Send(msg); - } - else - { - System.GC.Collect(); - Console.WriteLine("No data..."); - } - } + // Data buffer for incoming data. + byte[] bytes = new Byte[1024]; - handler.Shutdown(SocketShutdown.Both); - handler.Close(); - } + Console.WriteLine(localEndPoint.ToString()); + ZeroTier.Sockets.Socket listener = + new ZeroTier.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - } catch (ZeroTier.Sockets.SocketException e) { - Console.WriteLine(e); - Console.WriteLine("ServiceErrorCode={0} SocketErrorCode={1}", - e.ServiceErrorCode, e.SocketErrorCode); - } + // Bind the socket to the local endpoint and + // listen for incoming connections. - Console.WriteLine("\nPress ENTER to continue..."); - Console.Read(); - } + try { + listener.Bind(localEndPoint); + listener.Listen(10); + ZeroTier.Sockets.Socket handler; + Console.WriteLine("Server: Accepting..."); + handler = listener.Accept(); - /** - * Example client - */ - public void YourClient(IPEndPoint remoteServerEndPoint) { - // Data buffer for incoming data. - byte[] bytes = new byte[1024]; + data = null; + Console.WriteLine("Server: Accepted connection from: " + handler.RemoteEndPoint.ToString()); - // Connect to a remote device. - try { - // Create a TCP/IP socket. - ZeroTier.Sockets.Socket sender = new ZeroTier.Sockets.Socket(AddressFamily.InterNetwork, - SocketType.Stream, ProtocolType.Tcp ); + for (int i = 0; i < 4; i++) { + int bytesRec = 0; + try { + Console.WriteLine("Server: Receiving..."); + bytesRec = handler.Receive(bytes); + } + catch (ZeroTier.Sockets.SocketException e) { + Console.WriteLine( + "ServiceErrorCode={0} SocketErrorCode={1}", + e.ServiceErrorCode, + e.SocketErrorCode); + } + if (bytesRec > 0) { + Console.WriteLine("Server: Bytes received: {0}", bytesRec); + data = Encoding.ASCII.GetString(bytes, 0, bytesRec); + Console.WriteLine("Server: Text received : {0}", data); + Thread.Sleep(1000); + // Echo the data back to the client. + byte[] msg = Encoding.ASCII.GetBytes(data); + handler.Send(msg); + Thread.Sleep(1000); + } + } + // Release the socket. + handler.Shutdown(SocketShutdown.Both); + handler.Close(); + } + catch (ZeroTier.Sockets.SocketException e) { + Console.WriteLine(e); + Console.WriteLine("ServiceErrorCode={0} SocketErrorCode={1}", e.ServiceErrorCode, e.SocketErrorCode); + } + } - // Connect the socket to the remote endpoint. Catch any errors. - try { - Console.WriteLine("Socket connecting to {0}...", - remoteServerEndPoint.ToString()); + /** + * Example client + */ + public void SocketClient(IPEndPoint remoteServerEndPoint) + { + // Data buffer for incoming data. + byte[] bytes = new byte[1024]; - sender.Connect(remoteServerEndPoint); + // Connect to a remote device. + try { + // Create a TCP/IP socket. + ZeroTier.Sockets.Socket sender = + new ZeroTier.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); - Console.WriteLine("Socket connected to {0}", - sender.RemoteEndPoint.ToString()); + try { + Console.WriteLine("Client: Connecting to {0}...", remoteServerEndPoint.ToString()); + sender.Connect(remoteServerEndPoint); + Console.WriteLine("Client: Connected to {0}", sender.RemoteEndPoint.ToString()); - // Encode the data string into a byte array. - byte[] msg = Encoding.ASCII.GetBytes("This is a test"); - - // Send the data through the socket. - int bytesSent = sender.Send(msg); - - // Receive the response from the remote device. - int bytesRec = sender.Receive(bytes); - Console.WriteLine("Echoed test = {0}", - Encoding.ASCII.GetString(bytes,0,bytesRec)); - - // Release the socket. - sender.Shutdown(SocketShutdown.Both); - sender.Close(); - - } catch (ArgumentNullException ane) { - Console.WriteLine("ArgumentNullException : {0}",ane.ToString()); - } catch (ZeroTier.Sockets.SocketException e) { - Console.WriteLine(e); - Console.WriteLine("ServiceErrorCode={0} SocketErrorCode={1}", e.ServiceErrorCode, e.SocketErrorCode); - } - } catch (Exception e) { - Console.WriteLine( e.ToString()); - } - } + // Encode the data string into a byte array. + for (int i = 0; i < 4; i++) { + byte[] msg = Encoding.ASCII.GetBytes("This is a test"); + int bytesSent = sender.Send(msg); + Console.WriteLine("Client: Sent ({0}) bytes", bytesSent); + Thread.Sleep(1000); + int bytesRec = sender.Receive(bytes); + Console.WriteLine("Client: Echoing {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec)); + Thread.Sleep(1000); + } + // Release the socket. + sender.Shutdown(SocketShutdown.Both); + sender.Close(); + } + catch (ArgumentNullException ane) { + Console.WriteLine("ArgumentNullException : {0}", ane.ToString()); + } + catch (ZeroTier.Sockets.SocketException e) { + Console.WriteLine(e); + Console.WriteLine("ServiceErrorCode={0} SocketErrorCode={1}", e.ServiceErrorCode, e.SocketErrorCode); + } + } + catch (Exception e) { + Console.WriteLine(e.ToString()); + } + } } -public class example -{ - static int Main(string[] args) - { - if (args.Length < 5 || args.Length > 6) - { - Console.WriteLine("\nPlease specify either client or server mode and required arguments:"); - Console.WriteLine(" Usage: example server "); - Console.WriteLine(" Usage: example client \n"); - return 1; - } - string configFilePath = args[1]; - ushort servicePort = (ushort)Int16.Parse(args[2]); - ulong networkId = (ulong)Int64.Parse(args[3], System.Globalization.NumberStyles.HexNumber); +public class example { + static int Main(string[] args) + { + if (args.Length < 4 || args.Length > 5) { + Console.WriteLine("\nPlease specify either client or server mode and required arguments:"); + Console.WriteLine(" Usage: example server "); + Console.WriteLine(" Usage: example client \n"); + return 1; + } + string configFilePath = args[1]; + ulong networkId = (ulong)Int64.Parse(args[2], System.Globalization.NumberStyles.HexNumber); - ExampleApp exampleApp = new ExampleApp(); + ExampleApp exampleApp = new ExampleApp(); - if (args[0].Equals("server")) - { - Console.WriteLine("Server mode..."); - ushort serverPort = (ushort)Int16.Parse(args[4]); - exampleApp.StartZeroTier(configFilePath, servicePort, networkId); - IPAddress ipAddress = IPAddress.Parse("0.0.0.0"); - IPEndPoint localEndPoint = new IPEndPoint(ipAddress, serverPort); - exampleApp.YourServer(localEndPoint); - } + if (args[0].Equals("server")) { + Console.WriteLine("Server mode..."); + ushort serverPort = (ushort)Int16.Parse(args[3]); + exampleApp.StartZeroTier(configFilePath, networkId); + IPAddress ipAddress = IPAddress.Parse("0.0.0.0"); + IPEndPoint localEndPoint = new IPEndPoint(ipAddress, serverPort); + exampleApp.SocketServer(localEndPoint); + } - if (args[0].Equals("client")) - { - Console.WriteLine("Client mode..."); - string serverIP = args[4]; - int port = Int16.Parse(args[5]); - IPAddress ipAddress = IPAddress.Parse(serverIP); - IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, port); - exampleApp.StartZeroTier(configFilePath, servicePort, networkId); - exampleApp.YourClient(remoteEndPoint); - } - exampleApp.StopZeroTier(); - return 0; - } + if (args[0].Equals("client")) { + Console.WriteLine("Client mode..."); + string serverIP = args[3]; + int port = Int16.Parse(args[4]); + IPAddress ipAddress = IPAddress.Parse(serverIP); + IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, port); + exampleApp.StartZeroTier(configFilePath, networkId); + exampleApp.SocketClient(remoteEndPoint); + } + exampleApp.StopZeroTier(); + return 0; + } } - diff --git a/src/bindings/csharp/AddressInfo.cs b/src/bindings/csharp/AddressInfo.cs new file mode 100755 index 0000000..d8d9a32 --- /dev/null +++ b/src/bindings/csharp/AddressInfo.cs @@ -0,0 +1,43 @@ +/* + * Copyright (c)2013-2021 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2026-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +using System.Net; + +using ZeroTier; + +namespace ZeroTier.Core +{ + public class AddressInfo { + public AddressInfo(IPAddress addr, ulong net_id) + { + _addr = addr; + _net_id = net_id; + } + + public ulong _net_id; + public IPAddress _addr; + + public IPAddress Address + { + get { + return _addr; + } + } + public ulong NetworkId + { + get { + return _net_id; + } + } + } +} diff --git a/src/bindings/csharp/CSharpSockets.cxx b/src/bindings/csharp/CSharpSockets.cxx new file mode 100644 index 0000000..e5fef39 --- /dev/null +++ b/src/bindings/csharp/CSharpSockets.cxx @@ -0,0 +1,2094 @@ +/* + * Copyright (c)2013-2021 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2026-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +#ifndef SWIGCSHARP +#define SWIGCSHARP +#endif + +#ifdef __cplusplus +/* SwigValueWrapper is described in swig.swg */ +template class SwigValueWrapper { + struct SwigMovePointer { + T* ptr; + SwigMovePointer(T* p) : ptr(p) + { + } + ~SwigMovePointer() + { + delete ptr; + } + SwigMovePointer& operator=(SwigMovePointer& rhs) + { + T* oldptr = ptr; + ptr = 0; + delete oldptr; + ptr = rhs.ptr; + rhs.ptr = 0; + return *this; + } + } pointer; + SwigValueWrapper& operator=(const SwigValueWrapper& rhs); + SwigValueWrapper(const SwigValueWrapper& rhs); + + public: + SwigValueWrapper() : pointer(0) + { + } + SwigValueWrapper& operator=(const T& t) + { + SwigMovePointer tmp(new T(t)); + pointer = tmp; + return *this; + } + operator T&() const + { + return *pointer.ptr; + } + T* operator&() + { + return pointer.ptr; + } +}; + +template T SwigValueInit() +{ + return T(); +} +#endif + +/* ----------------------------------------------------------------------------- + * This section contains generic SWIG labels for method/variable + * declarations/attributes, and other compiler dependent labels. + * ----------------------------------------------------------------------------- */ + +/* template workaround for compilers that cannot correctly implement the C++ standard */ +#ifndef SWIGTEMPLATEDISAMBIGUATOR +#if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560) +#define SWIGTEMPLATEDISAMBIGUATOR template +#elif defined(__HP_aCC) +/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */ +/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */ +#define SWIGTEMPLATEDISAMBIGUATOR template +#else +#define SWIGTEMPLATEDISAMBIGUATOR +#endif +#endif + +/* inline attribute */ +#ifndef SWIGINLINE +#if defined(__cplusplus) || (defined(__GNUC__) && ! defined(__STRICT_ANSI__)) +#define SWIGINLINE inline +#else +#define SWIGINLINE +#endif +#endif + +/* attribute recognised by some compilers to avoid 'unused' warnings */ +#ifndef SWIGUNUSED +#if defined(__GNUC__) +#if ! (defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) +#define SWIGUNUSED __attribute__((__unused__)) +#else +#define SWIGUNUSED +#endif +#elif defined(__ICC) +#define SWIGUNUSED __attribute__((__unused__)) +#else +#define SWIGUNUSED +#endif +#endif + +#ifndef SWIG_MSC_UNSUPPRESS_4505 +#if defined(_MSC_VER) +#pragma warning(disable : 4505) /* unreferenced local function has been removed */ +#endif +#endif + +#ifndef SWIGUNUSEDPARM +#ifdef __cplusplus +#define SWIGUNUSEDPARM(p) +#else +#define SWIGUNUSEDPARM(p) p SWIGUNUSED +#endif +#endif + +/* internal SWIG method */ +#ifndef SWIGINTERN +#define SWIGINTERN static SWIGUNUSED +#endif + +/* internal inline SWIG method */ +#ifndef SWIGINTERNINLINE +#define SWIGINTERNINLINE SWIGINTERN SWIGINLINE +#endif + +/* exporting methods */ +#if defined(__GNUC__) +#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) +#ifndef GCC_HASCLASSVISIBILITY +#define GCC_HASCLASSVISIBILITY +#endif +#endif +#endif + +#ifndef SWIGEXPORT +#if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) +#if defined(STATIC_LINKED) +#define SWIGEXPORT +#else +#define SWIGEXPORT __declspec(dllexport) +#endif +#else +#if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY) +#define SWIGEXPORT __attribute__((visibility("default"))) +#else +#define SWIGEXPORT +#endif +#endif +#endif + +/* calling conventions for Windows */ +#ifndef SWIGSTDCALL +#if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) +#define SWIGSTDCALL __stdcall +#else +#define SWIGSTDCALL +#endif +#endif + +/* Deal with Microsoft's attempt at deprecating C standard runtime functions */ +#if ! defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && ! defined(_CRT_SECURE_NO_DEPRECATE) +#define _CRT_SECURE_NO_DEPRECATE +#endif + +/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */ +#if ! defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && ! defined(_SCL_SECURE_NO_DEPRECATE) +#define _SCL_SECURE_NO_DEPRECATE +#endif + +/* Deal with Apple's deprecated 'AssertMacros.h' from Carbon-framework */ +#if defined(__APPLE__) && ! defined(__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES) +#define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0 +#endif + +/* Intel's compiler complains if a variable which was never initialised is + * cast to void, which is a common idiom which we use to indicate that we + * are aware a variable isn't used. So we just silence that warning. + * See: https://github.com/swig/swig/issues/192 for more discussion. + */ +#ifdef __INTEL_COMPILER +#pragma warning disable 592 +#endif + +#include +#include +#include + +/* Support for throwing C# exceptions from C/C++. There are two types: + * Exceptions that take a message and ArgumentExceptions that take a message and a parameter name. */ +typedef enum { + SWIG_CSharpApplicationException, + SWIG_CSharpArithmeticException, + SWIG_CSharpDivideByZeroException, + SWIG_CSharpIndexOutOfRangeException, + SWIG_CSharpInvalidCastException, + SWIG_CSharpInvalidOperationException, + SWIG_CSharpIOException, + SWIG_CSharpNullReferenceException, + SWIG_CSharpOutOfMemoryException, + SWIG_CSharpOverflowException, + SWIG_CSharpSystemException +} SWIG_CSharpExceptionCodes; + +typedef enum { + SWIG_CSharpArgumentException, + SWIG_CSharpArgumentNullException, + SWIG_CSharpArgumentOutOfRangeException +} SWIG_CSharpExceptionArgumentCodes; + +typedef void(SWIGSTDCALL* SWIG_CSharpExceptionCallback_t)(const char*); +typedef void(SWIGSTDCALL* SWIG_CSharpExceptionArgumentCallback_t)(const char*, const char*); + +typedef struct { + SWIG_CSharpExceptionCodes code; + SWIG_CSharpExceptionCallback_t callback; +} SWIG_CSharpException_t; + +typedef struct { + SWIG_CSharpExceptionArgumentCodes code; + SWIG_CSharpExceptionArgumentCallback_t callback; +} SWIG_CSharpExceptionArgument_t; + +static SWIG_CSharpException_t SWIG_csharp_exceptions[] = { + { SWIG_CSharpApplicationException, NULL }, { SWIG_CSharpArithmeticException, NULL }, + { SWIG_CSharpDivideByZeroException, NULL }, { SWIG_CSharpIndexOutOfRangeException, NULL }, + { SWIG_CSharpInvalidCastException, NULL }, { SWIG_CSharpInvalidOperationException, NULL }, + { SWIG_CSharpIOException, NULL }, { SWIG_CSharpNullReferenceException, NULL }, + { SWIG_CSharpOutOfMemoryException, NULL }, { SWIG_CSharpOverflowException, NULL }, + { SWIG_CSharpSystemException, NULL } +}; + +static SWIG_CSharpExceptionArgument_t SWIG_csharp_exceptions_argument[] = { { SWIG_CSharpArgumentException, NULL }, + { SWIG_CSharpArgumentNullException, NULL }, + { SWIG_CSharpArgumentOutOfRangeException, + NULL } }; + +static void SWIGUNUSED SWIG_CSharpSetPendingException(SWIG_CSharpExceptionCodes code, const char* msg) +{ + SWIG_CSharpExceptionCallback_t callback = SWIG_csharp_exceptions[SWIG_CSharpApplicationException].callback; + if ((size_t)code < sizeof(SWIG_csharp_exceptions) / sizeof(SWIG_CSharpException_t)) { + callback = SWIG_csharp_exceptions[code].callback; + } + callback(msg); +} + +static void SWIGUNUSED +SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpExceptionArgumentCodes code, const char* msg, const char* param_name) +{ + SWIG_CSharpExceptionArgumentCallback_t callback = + SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentException].callback; + if ((size_t)code < sizeof(SWIG_csharp_exceptions_argument) / sizeof(SWIG_CSharpExceptionArgument_t)) { + callback = SWIG_csharp_exceptions_argument[code].callback; + } + callback(msg, param_name); +} + +#ifdef __cplusplus +extern "C" +#endif + SWIGEXPORT void SWIGSTDCALL + SWIGRegisterExceptionCallbacks_zt( + SWIG_CSharpExceptionCallback_t applicationCallback, + SWIG_CSharpExceptionCallback_t arithmeticCallback, + SWIG_CSharpExceptionCallback_t divideByZeroCallback, + SWIG_CSharpExceptionCallback_t indexOutOfRangeCallback, + SWIG_CSharpExceptionCallback_t invalidCastCallback, + SWIG_CSharpExceptionCallback_t invalidOperationCallback, + SWIG_CSharpExceptionCallback_t ioCallback, + SWIG_CSharpExceptionCallback_t nullReferenceCallback, + SWIG_CSharpExceptionCallback_t outOfMemoryCallback, + SWIG_CSharpExceptionCallback_t overflowCallback, + SWIG_CSharpExceptionCallback_t systemCallback) +{ + SWIG_csharp_exceptions[SWIG_CSharpApplicationException].callback = applicationCallback; + SWIG_csharp_exceptions[SWIG_CSharpArithmeticException].callback = arithmeticCallback; + SWIG_csharp_exceptions[SWIG_CSharpDivideByZeroException].callback = divideByZeroCallback; + SWIG_csharp_exceptions[SWIG_CSharpIndexOutOfRangeException].callback = indexOutOfRangeCallback; + SWIG_csharp_exceptions[SWIG_CSharpInvalidCastException].callback = invalidCastCallback; + SWIG_csharp_exceptions[SWIG_CSharpInvalidOperationException].callback = invalidOperationCallback; + SWIG_csharp_exceptions[SWIG_CSharpIOException].callback = ioCallback; + SWIG_csharp_exceptions[SWIG_CSharpNullReferenceException].callback = nullReferenceCallback; + SWIG_csharp_exceptions[SWIG_CSharpOutOfMemoryException].callback = outOfMemoryCallback; + SWIG_csharp_exceptions[SWIG_CSharpOverflowException].callback = overflowCallback; + SWIG_csharp_exceptions[SWIG_CSharpSystemException].callback = systemCallback; +} + +#ifdef __cplusplus +extern "C" +#endif + SWIGEXPORT void SWIGSTDCALL + SWIGRegisterExceptionArgumentCallbacks_zt( + SWIG_CSharpExceptionArgumentCallback_t argumentCallback, + SWIG_CSharpExceptionArgumentCallback_t argumentNullCallback, + SWIG_CSharpExceptionArgumentCallback_t argumentOutOfRangeCallback) +{ + SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentException].callback = argumentCallback; + SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentNullException].callback = argumentNullCallback; + SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentOutOfRangeException].callback = argumentOutOfRangeCallback; +} + +/* Callback for returning strings to C# without leaking memory */ +typedef char*(SWIGSTDCALL* SWIG_CSharpStringHelperCallback)(const char*); +static SWIG_CSharpStringHelperCallback SWIG_csharp_string_callback = NULL; + +#ifdef __cplusplus +extern "C" +#endif + SWIGEXPORT void SWIGSTDCALL + SWIGRegisterStringCallback_zt(SWIG_CSharpStringHelperCallback callback) +{ + SWIG_csharp_string_callback = callback; +} + +/* Contract support */ + +#define SWIG_contract_assert(nullreturn, expr, msg) \ + if (! (expr)) { \ + SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, msg, ""); \ + return nullreturn; \ + } \ + else + +/* Errors in SWIG */ +#define SWIG_UnknownError -1 +#define SWIG_IOError -2 +#define SWIG_RuntimeError -3 +#define SWIG_IndexError -4 +#define SWIG_TypeError -5 +#define SWIG_DivisionByZero -6 +#define SWIG_OverflowError -7 +#define SWIG_SyntaxError -8 +#define SWIG_ValueError -9 +#define SWIG_SystemError -10 +#define SWIG_AttributeError -11 +#define SWIG_MemoryError -12 +#define SWIG_NullReferenceError -13 + +#include +#include +#include +#include +#include +#include + +SWIGINTERN void SWIG_CSharpException(int code, const char* msg) +{ + if (code == SWIG_ValueError) { + SWIG_CSharpExceptionArgumentCodes exception_code = SWIG_CSharpArgumentOutOfRangeException; + SWIG_CSharpSetPendingExceptionArgument(exception_code, msg, 0); + } + else { + SWIG_CSharpExceptionCodes exception_code = SWIG_CSharpApplicationException; + switch (code) { + case SWIG_MemoryError: + exception_code = SWIG_CSharpOutOfMemoryException; + break; + case SWIG_IndexError: + exception_code = SWIG_CSharpIndexOutOfRangeException; + break; + case SWIG_DivisionByZero: + exception_code = SWIG_CSharpDivideByZeroException; + break; + case SWIG_IOError: + exception_code = SWIG_CSharpIOException; + break; + case SWIG_OverflowError: + exception_code = SWIG_CSharpOverflowException; + break; + case SWIG_RuntimeError: + case SWIG_TypeError: + case SWIG_SyntaxError: + case SWIG_SystemError: + case SWIG_UnknownError: + default: + exception_code = SWIG_CSharpApplicationException; + break; + } + SWIG_CSharpSetPendingException(exception_code, msg); + } +} + +#include "../../../include/ZeroTierSockets.h" + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_central_set_access_mode(char jarg1) +{ + int jresult; + int8_t arg1; + int result; + arg1 = (int8_t)jarg1; + result = (int)zts_central_set_access_mode(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_central_set_verbose(char jarg1) +{ + int jresult; + int8_t arg1; + int result; + arg1 = (int8_t)jarg1; + result = (int)zts_central_set_verbose(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT void SWIGSTDCALL CSharp_zts_central_clear_resp_buf() +{ + zts_central_clear_resp_buf(); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_central_init(char* jarg1, char* jarg2, char* jarg3, unsigned short jarg4) +{ + int jresult; + char* arg1 = (char*)0; + char* arg2 = (char*)0; + char* arg3 = (char*)0; + uint32_t arg4; + int result; + arg1 = (char*)jarg1; + arg2 = (char*)jarg2; + arg3 = (char*)jarg3; + arg4 = (uint32_t)jarg4; + result = (int)zts_central_init((char const*)arg1, (char const*)arg2, arg3, arg4); + jresult = result; + return jresult; +} + +SWIGEXPORT void SWIGSTDCALL CSharp_zts_central_cleanup() +{ + zts_central_cleanup(); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_central_get_last_resp_buf(char* jarg1, int jarg2) +{ + int jresult; + char* arg1 = (char*)0; + int arg2; + int result; + arg1 = (char*)jarg1; + arg2 = (int)jarg2; + result = (int)zts_central_get_last_resp_buf(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_central_status_get(void* jarg1) +{ + int jresult; + int* arg1 = (int*)0; + int result; + arg1 = (int*)jarg1; + result = (int)zts_central_status_get(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_central_self_get(void* jarg1) +{ + int jresult; + int* arg1 = (int*)0; + int result; + arg1 = (int*)jarg1; + result = (int)zts_central_self_get(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_central_net_get(void* jarg1, unsigned long long jarg2) +{ + int jresult; + int* arg1 = (int*)0; + uint64_t arg2; + int result; + arg1 = (int*)jarg1; + arg2 = (uint64_t)jarg2; + result = (int)zts_central_net_get(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_central_net_update(void* jarg1, unsigned long long jarg2) +{ + int jresult; + int* arg1 = (int*)0; + uint64_t arg2; + int result; + arg1 = (int*)jarg1; + arg2 = (uint64_t)jarg2; + result = (int)zts_central_net_update(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_central_net_delete(void* jarg1, unsigned long long jarg2) +{ + int jresult; + int* arg1 = (int*)0; + uint64_t arg2; + int result; + arg1 = (int*)jarg1; + arg2 = (uint64_t)jarg2; + result = (int)zts_central_net_delete(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_central_net_get_all(void* jarg1) +{ + int jresult; + int* arg1 = (int*)0; + int result; + arg1 = (int*)jarg1; + result = (int)zts_central_net_get_all(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL +CSharp_zts_central_member_get(void* jarg1, unsigned long long jarg2, unsigned long long jarg3) +{ + int jresult; + int* arg1 = (int*)0; + uint64_t arg2; + uint64_t arg3; + int result; + arg1 = (int*)jarg1; + arg2 = (uint64_t)jarg2; + arg3 = (uint64_t)jarg3; + result = (int)zts_central_member_get(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL +CSharp_zts_central_member_update(void* jarg1, unsigned long long jarg2, unsigned long long jarg3, char* jarg4) +{ + int jresult; + int* arg1 = (int*)0; + uint64_t arg2; + uint64_t arg3; + char* arg4 = (char*)0; + int result; + arg1 = (int*)jarg1; + arg2 = (uint64_t)jarg2; + arg3 = (uint64_t)jarg3; + arg4 = (char*)jarg4; + result = (int)zts_central_member_update(arg1, arg2, arg3, arg4); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL +CSharp_zts_central_node_auth(void* jarg1, unsigned long long jarg2, unsigned long long jarg3, unsigned char jarg4) +{ + int jresult; + int* arg1 = (int*)0; + uint64_t arg2; + uint64_t arg3; + uint8_t arg4; + int result; + arg1 = (int*)jarg1; + arg2 = (uint64_t)jarg2; + arg3 = (uint64_t)jarg3; + arg4 = (uint8_t)jarg4; + result = (int)zts_central_node_auth(arg1, arg2, arg3, arg4); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_central_net_get_members(void* jarg1, unsigned long long jarg2) +{ + int jresult; + int* arg1 = (int*)0; + uint64_t arg2; + int result; + arg1 = (int*)jarg1; + arg2 = (uint64_t)jarg2; + result = (int)zts_central_net_get_members(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_id_new(char* jarg1, void* jarg2) +{ + int jresult; + char* arg1 = (char*)0; + unsigned int* arg2 = (unsigned int*)0; + int result; + arg1 = (char*)jarg1; + arg2 = (unsigned int*)jarg2; + result = (int)zts_id_new(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_id_pair_is_valid(char* jarg1, int jarg2) +{ + int jresult; + char* arg1 = (char*)0; + int arg2; + int result; + arg1 = (char*)jarg1; + arg2 = (int)jarg2; + result = (int)zts_id_pair_is_valid((char const*)arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_init_from_storage(char* jarg1) +{ + int jresult; + char* arg1 = (char*)0; + int result; + arg1 = (char*)jarg1; + result = (int)zts_init_from_storage((char const*)arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_init_from_memory(char* jarg1, unsigned short jarg2) +{ + int jresult; + char* arg1 = (char*)0; + uint16_t arg2; + int result; + arg1 = (char*)jarg1; + arg2 = (uint16_t)jarg2; + result = (int)zts_init_from_memory((char const*)arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_init_set_event_handler(void* jarg1) +{ + int jresult; + CppCallback arg1 = (CppCallback)0; + int result; + arg1 = (CppCallback)jarg1; + result = (int)zts_init_set_event_handler(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_init_blacklist_if(char* jarg1, int jarg2) +{ + int jresult; + char* arg1 = (char*)0; + int arg2; + int result; + arg1 = (char*)jarg1; + arg2 = (int)jarg2; + result = (int)zts_init_blacklist_if((char const*)arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_init_set_roots(void* jarg1, int jarg2) +{ + int jresult; + char* arg1 = (char*)0; + int arg2; + int result; + arg1 = (char*)jarg1; + arg2 = (int)jarg2; + result = (int)zts_init_set_roots((char const*)arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_init_set_port(unsigned short jarg1) +{ + int jresult; + unsigned short arg1; + int result; + arg1 = (unsigned short)jarg1; + result = (int)zts_init_set_port(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_init_allow_net_cache(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_init_allow_net_cache(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_init_allow_peer_cache(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_init_allow_peer_cache(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_addr_is_assigned(unsigned long long jarg1, int jarg2) +{ + int jresult; + uint64_t arg1; + int arg2; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (int)jarg2; + result = (int)zts_addr_is_assigned(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_addr_get(unsigned long long jarg1, int jarg2, void* jarg3) +{ + int jresult; + uint64_t arg1; + int arg2; + zts_sockaddr_storage* arg3 = (zts_sockaddr_storage*)0; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (int)jarg2; + arg3 = (zts_sockaddr_storage*)jarg3; + result = (int)zts_addr_get(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_addr_get_str(unsigned long long jarg1, int jarg2, char* jarg3, int jarg4) +{ + int jresult; + uint64_t arg1; + int arg2; + char* arg3 = (char*)0; + int arg4; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (int)jarg2; + arg3 = (char*)jarg3; + arg4 = (int)jarg4; + result = (int)zts_addr_get_str(arg1, arg2, arg3, arg4); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_addr_get_all(unsigned long long jarg1, void* jarg2, void* jarg3) +{ + int jresult; + uint64_t arg1; + zts_sockaddr_storage* arg2 = (zts_sockaddr_storage*)0; + unsigned int* arg3 = (unsigned int*)0; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (zts_sockaddr_storage*)jarg2; + arg3 = (unsigned int*)jarg3; + result = (int)zts_addr_get_all(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL +CSharp_zts_addr_compute_6plane(unsigned long long jarg1, unsigned long long jarg2, void* jarg3) +{ + int jresult; + uint64_t arg1; + uint64_t arg2; + zts_sockaddr_storage* arg3 = (zts_sockaddr_storage*)0; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (uint64_t)jarg2; + arg3 = (zts_sockaddr_storage*)jarg3; + result = (int)zts_addr_compute_6plane(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL +CSharp_zts_addr_compute_rfc4193(unsigned long long jarg1, unsigned long long jarg2, void* jarg3) +{ + int jresult; + uint64_t arg1; + uint64_t arg2; + zts_sockaddr_storage* arg3 = (zts_sockaddr_storage*)0; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (uint64_t)jarg2; + arg3 = (zts_sockaddr_storage*)jarg3; + result = (int)zts_addr_compute_rfc4193(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL +CSharp_zts_addr_compute_rfc4193_str(unsigned long long jarg1, unsigned long long jarg2, char* jarg3, int jarg4) +{ + int jresult; + uint64_t arg1; + uint64_t arg2; + char* arg3 = (char*)0; + int arg4; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (uint64_t)jarg2; + arg3 = (char*)jarg3; + arg4 = (int)jarg4; + result = (int)zts_addr_compute_rfc4193_str(arg1, arg2, arg3, arg4); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL +CSharp_zts_addr_compute_6plane_str(unsigned long long jarg1, unsigned long long jarg2, char* jarg3, int jarg4) +{ + int jresult; + uint64_t arg1; + uint64_t arg2; + char* arg3 = (char*)0; + int arg4; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (uint64_t)jarg2; + arg3 = (char*)jarg3; + arg4 = (int)jarg4; + result = (int)zts_addr_compute_6plane_str(arg1, arg2, arg3, arg4); + jresult = result; + return jresult; +} + +SWIGEXPORT unsigned long long SWIGSTDCALL CSharp_zts_net_compute_adhoc_id(unsigned short jarg1, unsigned short jarg2) +{ + unsigned long long jresult; + uint16_t arg1; + uint16_t arg2; + uint64_t result; + arg1 = (uint16_t)jarg1; + arg2 = (uint16_t)jarg2; + result = zts_net_compute_adhoc_id(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_net_join(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_net_join(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_net_leave(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_net_leave(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_net_transport_is_ready(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_net_transport_is_ready(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT unsigned long long SWIGSTDCALL CSharp_zts_net_get_mac(unsigned long long jarg1) +{ + unsigned long long jresult; + uint64_t arg1; + uint64_t result; + arg1 = (uint64_t)jarg1; + result = zts_net_get_mac(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_net_get_mac_str(unsigned long long jarg1, char* jarg2, int jarg3) +{ + int jresult; + uint64_t arg1; + char* arg2 = (char*)0; + int arg3; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (char*)jarg2; + arg3 = (int)jarg3; + result = (int)zts_net_get_mac_str(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_net_get_broadcast(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_net_get_broadcast(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_net_get_mtu(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_net_get_mtu(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_net_get_name(unsigned long long jarg1, char* jarg2, int jarg3) +{ + int jresult; + uint64_t arg1; + char* arg2 = (char*)0; + int arg3; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (char*)jarg2; + arg3 = (int)jarg3; + result = (int)zts_net_get_name(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_net_get_status(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_net_get_status(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_net_get_type(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_net_get_type(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_route_is_assigned(unsigned long long jarg1, int jarg2) +{ + int jresult; + uint64_t arg1; + int arg2; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (int)jarg2; + result = (int)zts_route_is_assigned(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_node_start() +{ + int jresult; + int result; + + result = (int)zts_node_start(); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_node_is_online() +{ + int jresult; + int result; + + result = (int)zts_node_is_online(); + jresult = result; + return jresult; +} + +SWIGEXPORT unsigned long long SWIGSTDCALL CSharp_zts_node_get_id() +{ + unsigned long long jresult; + uint64_t result; + + result = zts_node_get_id(); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_node_get_id_pair(char* jarg1, void* jarg2) +{ + int jresult; + char* arg1 = (char*)0; + unsigned int* arg2 = (unsigned int*)0; + int result; + arg1 = (char*)jarg1; + arg2 = (unsigned int*)jarg2; + result = (int)zts_node_get_id_pair(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_node_get_port() +{ + int jresult; + int result; + + result = (int)zts_node_get_port(); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_node_stop() +{ + int jresult; + int result; + + result = (int)zts_node_stop(); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_node_free() +{ + int jresult; + int result; + + result = (int)zts_node_free(); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_moon_orbit(unsigned long long jarg1, unsigned long long jarg2) +{ + int jresult; + uint64_t arg1; + uint64_t arg2; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (uint64_t)jarg2; + result = (int)zts_moon_orbit(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_moon_deorbit(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_moon_deorbit(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_stats_get_all(void* jarg1) +{ + int jresult; + zts_stats_counter_t* arg1 = (zts_stats_counter_t*)0; + int result; + arg1 = (zts_stats_counter_t*)jarg1; + result = (int)zts_stats_get_all(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_socket(int jarg1, int jarg2, int jarg3) +{ + int jresult; + int arg1; + int arg2; + int arg3; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + arg3 = (int)jarg3; + result = (int)zts_socket(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_connect(int jarg1, zts_sockaddr* jarg2, unsigned short jarg3) +{ + int jresult; + int arg1; + zts_sockaddr* arg2 = (zts_sockaddr*)0; + zts_socklen_t arg3; + int result; + arg1 = (int)jarg1; + arg2 = (zts_sockaddr*)jarg2; + arg3 = (zts_socklen_t)jarg3; + result = (int)zts_connect(arg1, (zts_sockaddr const*)arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_connect(int jarg1, char* jarg2, int jarg3, int jarg4) +{ + int jresult; + int arg1; + char* arg2 = (char*)0; + int arg3; + int arg4; + int result; + arg1 = (int)jarg1; + arg2 = (char*)jarg2; + arg3 = (int)jarg3; + arg4 = (int)jarg4; + result = (int)zts_simple_connect(arg1, (char const*)arg2, arg3, arg4); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_bind(int jarg1, zts_sockaddr* jarg2, unsigned short jarg3) +{ + int jresult; + int arg1; + zts_sockaddr* arg2 = (zts_sockaddr*)0; + zts_socklen_t arg3; + int result; + arg1 = (int)jarg1; + arg2 = (zts_sockaddr*)jarg2; + arg3 = (zts_socklen_t)jarg3; + result = (int)zts_bind(arg1, (zts_sockaddr const*)arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_bind(int jarg1, char* jarg2, int jarg3) +{ + int jresult; + int arg1; + char* arg2 = (char*)0; + int arg3; + int result; + arg1 = (int)jarg1; + arg2 = (char*)jarg2; + arg3 = (int)jarg3; + result = (int)zts_simple_bind(arg1, (char const*)arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_listen(int jarg1, int jarg2) +{ + int jresult; + int arg1; + int arg2; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + result = (int)zts_listen(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_accept(int jarg1, zts_sockaddr* jarg2, void* jarg3) +{ + int jresult; + int arg1; + zts_sockaddr* arg2 = (zts_sockaddr*)0; + zts_socklen_t* arg3 = (zts_socklen_t*)0; + int result; + arg1 = (int)jarg1; + arg2 = (zts_sockaddr*)jarg2; + arg3 = (zts_socklen_t*)jarg3; + result = (int)zts_accept(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_accept(int jarg1, char* jarg2, int jarg3, void* jarg4) +{ + int arg1; + char* arg2 = (char*)0; + int arg3; + unsigned short* arg4 = (unsigned short*)0; + arg1 = (int)jarg1; + arg2 = (char*)jarg2; + arg3 = (int)jarg3; + arg4 = (unsigned short*)jarg4; + return zts_simple_accept(arg1, arg2, arg3, arg4); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_tcp_client(char* jarg1, int jarg2) +{ + int jresult; + char* arg1 = (char*)0; + int arg2; + int result; + arg1 = (char*)jarg1; + arg2 = (int)jarg2; + result = (int)zts_simple_tcp_client((char const*)arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_tcp_server(char* jarg1, int jarg2, char* jarg3, int jarg4, void* jarg5) +{ + char* arg1 = (char*)0; + int arg2; + char* arg3 = (char*)0; + int arg4; + unsigned short* arg5 = (unsigned short*)0; + arg1 = (char*)jarg1; + arg2 = (int)jarg2; + arg3 = (char*)jarg3; + arg4 = (int)jarg4; + arg5 = (unsigned short*)jarg5; + return zts_simple_tcp_server((char const*)arg1, arg2, arg3, arg4, arg5); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_udp_server(char* jarg1, int jarg2) +{ + int jresult; + char* arg1 = (char*)0; + int arg2; + int result; + arg1 = (char*)jarg1; + arg2 = (int)jarg2; + result = (int)zts_simple_udp_server((char const*)arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_udp_client(char* jarg1) +{ + int jresult; + char* arg1 = (char*)0; + int result; + arg1 = (char*)jarg1; + result = (int)zts_simple_udp_client((char const*)arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_setsockopt(int jarg1, int jarg2, int jarg3, void* jarg4, unsigned short jarg5) +{ + int jresult; + int arg1; + int arg2; + int arg3; + void* arg4 = (void*)0; + zts_socklen_t arg5; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + arg3 = (int)jarg3; + arg4 = (void*)jarg4; + arg5 = (zts_socklen_t)jarg5; + result = (int)zts_setsockopt(arg1, arg2, arg3, (void const*)arg4, arg5); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_getsockopt(int jarg1, int jarg2, int jarg3, void* jarg4, void* jarg5) +{ + int jresult; + int arg1; + int arg2; + int arg3; + void* arg4 = (void*)0; + zts_socklen_t* arg5 = (zts_socklen_t*)0; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + arg3 = (int)jarg3; + arg4 = (void*)jarg4; + arg5 = (zts_socklen_t*)jarg5; + result = (int)zts_getsockopt(arg1, arg2, arg3, arg4, arg5); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_getsockname(int jarg1, zts_sockaddr* jarg2, void* jarg3) +{ + int jresult; + int arg1; + zts_sockaddr* arg2 = (zts_sockaddr*)0; + zts_socklen_t* arg3 = (zts_socklen_t*)0; + int result; + arg1 = (int)jarg1; + arg2 = (zts_sockaddr*)jarg2; + arg3 = (zts_socklen_t*)jarg3; + result = (int)zts_getsockname(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_getpeername(int jarg1, zts_sockaddr* jarg2, void* jarg3) +{ + int jresult; + int arg1; + zts_sockaddr* arg2 = (zts_sockaddr*)0; + zts_socklen_t* arg3 = (zts_socklen_t*)0; + int result; + arg1 = (int)jarg1; + arg2 = (zts_sockaddr*)jarg2; + arg3 = (zts_socklen_t*)jarg3; + result = (int)zts_getpeername(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_close(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_close(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_select(int jarg1, void* jarg2, void* jarg3, void* jarg4, void* jarg5) +{ + int jresult; + int arg1; + zts_fd_set* arg2 = (zts_fd_set*)0; + zts_fd_set* arg3 = (zts_fd_set*)0; + zts_fd_set* arg4 = (zts_fd_set*)0; + zts_timeval* arg5 = (zts_timeval*)0; + int result; + arg1 = (int)jarg1; + arg2 = (zts_fd_set*)jarg2; + arg3 = (zts_fd_set*)jarg3; + arg4 = (zts_fd_set*)jarg4; + arg5 = (zts_timeval*)jarg5; + result = (int)zts_select(arg1, arg2, arg3, arg4, arg5); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_fcntl(int jarg1, int jarg2, int jarg3) +{ + int jresult; + int arg1; + int arg2; + int arg3; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + arg3 = (int)jarg3; + result = (int)zts_fcntl(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_poll(void* jarg1, unsigned int jarg2, int jarg3) +{ + int jresult; + zts_pollfd* arg1 = (zts_pollfd*)0; + zts_nfds_t arg2; + int arg3; + int result; + arg1 = (zts_pollfd*)jarg1; + arg2 = (zts_nfds_t)jarg2; + arg3 = (int)jarg3; + result = (int)zts_poll(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_ioctl(int jarg1, unsigned long jarg2, void* jarg3) +{ + int jresult; + int arg1; + unsigned long arg2; + void* arg3 = (void*)0; + int result; + arg1 = (int)jarg1; + arg2 = (unsigned long)jarg2; + arg3 = (void*)jarg3; + result = (int)zts_ioctl(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_send(int jarg1, void* jarg2, unsigned long jarg3, int jarg4) +{ + int arg1; + void* arg2 = (void*)0; + size_t arg3; + int arg4; + arg1 = (int)jarg1; + arg2 = (void*)jarg2; + arg3 = (size_t)jarg3; + arg4 = (int)jarg4; + return zts_send(arg1, (void const*)arg2, arg3, arg4); +} + +SWIGEXPORT int SWIGSTDCALL +CSharp_zts_sendto(int jarg1, void* jarg2, unsigned long jarg3, int jarg4, zts_sockaddr* jarg5, unsigned short jarg6) +{ + int arg1; + void* arg2 = (void*)0; + size_t arg3; + int arg4; + zts_sockaddr* arg5 = (zts_sockaddr*)0; + zts_socklen_t arg6; + arg1 = (int)jarg1; + arg2 = (void*)jarg2; + arg3 = (size_t)jarg3; + arg4 = (int)jarg4; + arg5 = (zts_sockaddr*)jarg5; + arg6 = (zts_socklen_t)jarg6; + return zts_sendto(arg1, (void const*)arg2, arg3, arg4, (zts_sockaddr const*)arg5, arg6); +} + +SWIGEXPORT void* SWIGSTDCALL CSharp_new_zts_iovec() +{ + void* jresult; + zts_iovec* result = 0; + + result = (zts_iovec*)new zts_iovec(); + jresult = (void*)result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_sendmsg(int jarg1, void* jarg2, int jarg3) +{ + int arg1; + zts_msghdr* arg2 = (zts_msghdr*)0; + int arg3; + arg1 = (int)jarg1; + arg2 = (zts_msghdr*)jarg2; + arg3 = (int)jarg3; + return zts_sendmsg(arg1, (zts_msghdr const*)arg2, arg3); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_recv(int jarg1, void* jarg2, unsigned long jarg3, int jarg4) +{ + int arg1; + void* arg2 = (void*)0; + size_t arg3; + int arg4; + arg1 = (int)jarg1; + arg2 = (void*)jarg2; + arg3 = (size_t)jarg3; + arg4 = (int)jarg4; + return zts_recv(arg1, arg2, arg3, arg4); +} + +SWIGEXPORT int SWIGSTDCALL +CSharp_zts_recvfrom(int jarg1, void* jarg2, unsigned long jarg3, int jarg4, zts_sockaddr* jarg5, void* jarg6) +{ + int arg1; + void* arg2 = (void*)0; + size_t arg3; + int arg4; + zts_sockaddr* arg5 = (zts_sockaddr*)0; + zts_socklen_t* arg6 = (zts_socklen_t*)0; + arg1 = (int)jarg1; + arg2 = (void*)jarg2; + arg3 = (size_t)jarg3; + arg4 = (int)jarg4; + arg5 = (zts_sockaddr*)jarg5; + arg6 = (zts_socklen_t*)jarg6; + return zts_recvfrom(arg1, arg2, arg3, arg4, arg5, arg6); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_recvmsg(int jarg1, void* jarg2, int jarg3) +{ + int arg1; + zts_msghdr* arg2 = (zts_msghdr*)0; + int arg3; + arg1 = (int)jarg1; + arg2 = (zts_msghdr*)jarg2; + arg3 = (int)jarg3; + return zts_recvmsg(arg1, arg2, arg3); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_read(int jarg1, void* jarg2, unsigned long jarg3) +{ + int arg1; + void* arg2 = (void*)0; + size_t arg3; + arg1 = (int)jarg1; + arg2 = (void*)jarg2; + arg3 = (size_t)jarg3; + return zts_read(arg1, arg2, arg3); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_readv(int jarg1, void* jarg2, int jarg3) +{ + int arg1; + zts_iovec* arg2 = (zts_iovec*)0; + int arg3; + arg1 = (int)jarg1; + arg2 = (zts_iovec*)jarg2; + arg3 = (int)jarg3; + return zts_readv(arg1, (zts_iovec const*)arg2, arg3); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_write(int jarg1, void* jarg2, unsigned long jarg3) +{ + int arg1; + void* arg2 = (void*)0; + size_t arg3; + arg1 = (int)jarg1; + arg2 = (void*)jarg2; + arg3 = (size_t)jarg3; + return zts_write(arg1, (void const*)arg2, arg3); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_writev(int jarg1, void* jarg2, int jarg3) +{ + int arg1; + zts_iovec* arg2 = (zts_iovec*)0; + int arg3; + arg1 = (int)jarg1; + arg2 = (zts_iovec*)jarg2; + arg3 = (int)jarg3; + return zts_writev(arg1, (zts_iovec const*)arg2, arg3); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_shutdown(int jarg1, int jarg2) +{ + int jresult; + int arg1; + int arg2; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + result = (int)zts_shutdown(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_set_no_delay(int jarg1, int jarg2) +{ + int jresult; + int arg1; + int arg2; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + result = (int)zts_simple_set_no_delay(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_get_no_delay(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_simple_get_no_delay(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_set_linger(int jarg1, int jarg2, int jarg3) +{ + int jresult; + int arg1; + int arg2; + int arg3; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + arg3 = (int)jarg3; + result = (int)zts_simple_set_linger(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_get_linger_enabled(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_simple_get_linger_enabled(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_get_linger_value(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_simple_get_linger_value(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_set_reuse_addr(int jarg1, int jarg2) +{ + int jresult; + int arg1; + int arg2; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + result = (int)zts_simple_set_reuse_addr(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_get_reuse_addr(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_simple_get_reuse_addr(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_set_recv_timeout(int jarg1, int jarg2, int jarg3) +{ + int jresult; + int arg1; + int arg2; + int arg3; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + arg3 = (int)jarg3; + result = (int)zts_simple_set_recv_timeout(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_get_recv_timeout(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_simple_get_recv_timeout(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_set_send_timeout(int jarg1, int jarg2, int jarg3) +{ + int jresult; + int arg1; + int arg2; + int arg3; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + arg3 = (int)jarg3; + result = (int)zts_simple_set_send_timeout(arg1, arg2, arg3); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_get_send_timeout(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_simple_get_send_timeout(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_set_send_buf_size(int jarg1, int jarg2) +{ + int jresult; + int arg1; + int arg2; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + result = (int)zts_simple_set_send_buf_size(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_get_send_buf_size(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_simple_get_send_buf_size(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_set_recv_buf_size(int jarg1, int jarg2) +{ + int jresult; + int arg1; + int arg2; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + result = (int)zts_simple_set_recv_buf_size(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_get_recv_buf_size(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_simple_get_recv_buf_size(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_set_ttl(int jarg1, int jarg2) +{ + int jresult; + int arg1; + int arg2; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + result = (int)zts_simple_set_ttl(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_get_ttl(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_simple_get_ttl(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_set_blocking(int jarg1, int jarg2) +{ + int jresult; + int arg1; + int arg2; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + result = (int)zts_simple_set_blocking(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_get_blocking(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_simple_get_blocking(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_set_keepalive(int jarg1, int jarg2) +{ + int jresult; + int arg1; + int arg2; + int result; + arg1 = (int)jarg1; + arg2 = (int)jarg2; + result = (int)zts_simple_set_keepalive(arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_simple_get_keepalive(int jarg1) +{ + int jresult; + int arg1; + int result; + arg1 = (int)jarg1; + result = (int)zts_simple_get_keepalive(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT void* SWIGSTDCALL CSharp_zts_gethostbyname(char* jarg1) +{ + void* jresult; + char* arg1 = (char*)0; + zts_hostent* result = 0; + arg1 = (char*)jarg1; + result = (zts_hostent*)zts_gethostbyname((char const*)arg1); + jresult = (void*)result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_dns_set_server(unsigned char jarg1, void* jarg2) +{ + int jresult; + uint8_t arg1; + zts_ip_addr* arg2 = (zts_ip_addr*)0; + int result; + arg1 = (uint8_t)jarg1; + arg2 = (zts_ip_addr*)jarg2; + result = (int)zts_dns_set_server(arg1, (zts_ip_addr const*)arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT void* SWIGSTDCALL CSharp_zts_dns_get_server(unsigned char jarg1) +{ + void* jresult; + uint8_t arg1; + zts_ip_addr* result = 0; + arg1 = (uint8_t)jarg1; + result = (zts_ip_addr*)zts_dns_get_server(arg1); + jresult = (void*)result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_core_lock_obtain() +{ + int jresult; + int result; + + result = (int)zts_core_lock_obtain(); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_core_lock_release() +{ + int jresult; + int result; + + result = (int)zts_core_lock_release(); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_core_query_addr_count(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_core_query_addr_count(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_core_query_addr(unsigned long long jarg1, int jarg2, char* jarg3, int jarg4) +{ + int jresult; + uint64_t arg1; + int arg2; + char* arg3 = (char*)0; + int arg4; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (int)jarg2; + arg3 = (char*)jarg3; + arg4 = (int)jarg4; + result = (int)zts_core_query_addr(arg1, arg2, arg3, arg4); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_core_query_route_count(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_core_query_route_count(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_core_query_route( + unsigned long long jarg1, + int jarg2, + char* jarg3, + char* jarg4, + int jarg5, + void* jarg6, + void* jarg7) +{ + int jresult; + uint64_t arg1; + int arg2; + char* arg3 = (char*)0; + char* arg4 = (char*)0; + int arg5; + uint16_t* arg6 = (uint16_t*)0; + uint16_t* arg7 = (uint16_t*)0; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (int)jarg2; + arg3 = (char*)jarg3; + arg4 = (char*)jarg4; + arg5 = (int)jarg5; + arg6 = (uint16_t*)jarg6; + arg7 = (uint16_t*)jarg7; + result = (int)zts_core_query_route(arg1, arg2, arg3, arg4, arg5, arg6, arg7); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_core_query_path_count(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_core_query_path_count(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_core_query_path(unsigned long long jarg1, int jarg2, char* jarg3, int jarg4) +{ + int jresult; + uint64_t arg1; + int arg2; + char* arg3 = (char*)0; + int arg4; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (int)jarg2; + arg3 = (char*)jarg3; + arg4 = (int)jarg4; + result = (int)zts_core_query_path(arg1, arg2, arg3, arg4); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_core_query_mc_count(unsigned long long jarg1) +{ + int jresult; + uint64_t arg1; + int result; + arg1 = (uint64_t)jarg1; + result = (int)zts_core_query_mc_count(arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_core_query_mc(unsigned long long jarg1, int jarg2, void* jarg3, void* jarg4) +{ + int jresult; + uint64_t arg1; + int arg2; + uint64_t* arg3 = (uint64_t*)0; + uint32_t* arg4 = (uint32_t*)0; + int result; + arg1 = (uint64_t)jarg1; + arg2 = (int)jarg2; + arg3 = (uint64_t*)jarg3; + arg4 = (uint32_t*)jarg4; + result = (int)zts_core_query_mc(arg1, arg2, arg3, arg4); + jresult = result; + return jresult; +} + +SWIGEXPORT void SWIGSTDCALL CSharp_zts_util_delay(long jarg1) +{ + long arg1; + arg1 = (long)jarg1; + zts_util_delay(arg1); +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_util_get_ip_family(char* jarg1) +{ + int jresult; + char* arg1 = (char*)0; + int result; + arg1 = (char*)jarg1; + result = (int)zts_util_get_ip_family((char const*)arg1); + jresult = result; + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_util_ipstr_to_saddr(char* jarg1, int jarg2, zts_sockaddr* jarg3, void* jarg4) +{ + int jresult; + char* arg1 = (char*)0; + int arg2; + zts_sockaddr* arg3 = (zts_sockaddr*)0; + zts_socklen_t* arg4 = (zts_socklen_t*)0; + int result; + arg1 = (char*)jarg1; + arg2 = (int)jarg2; + arg3 = (zts_sockaddr*)jarg3; + arg4 = (zts_socklen_t*)jarg4; + result = (int)zts_util_ipstr_to_saddr((char const*)arg1, arg2, arg3, arg4); + jresult = result; + return jresult; +} + +SWIGEXPORT char* SWIGSTDCALL CSharp_zts_ipaddr_ntoa(void* jarg1) +{ + char* jresult; + zts_ip_addr* arg1 = (zts_ip_addr*)0; + char* result = 0; + arg1 = (zts_ip_addr*)jarg1; + result = (char*)zts_ipaddr_ntoa((zts_ip_addr const*)arg1); + jresult = SWIG_csharp_string_callback((const char*)result); + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_ipaddr_aton(char* jarg1, void* jarg2) +{ + int jresult; + char* arg1 = (char*)0; + zts_ip_addr* arg2 = (zts_ip_addr*)0; + int result; + arg1 = (char*)jarg1; + arg2 = (zts_ip_addr*)jarg2; + result = (int)zts_ipaddr_aton((char const*)arg1, arg2); + jresult = result; + return jresult; +} + +SWIGEXPORT char* SWIGSTDCALL CSharp_zts_inet_ntop(int jarg1, void* jarg2, char* jarg3, unsigned short jarg4) +{ + char* jresult; + int arg1; + void* arg2 = (void*)0; + char* arg3 = (char*)0; + zts_socklen_t arg4; + char* result = 0; + arg1 = (int)jarg1; + arg2 = (void*)jarg2; + arg3 = (char*)jarg3; + arg4 = (zts_socklen_t)jarg4; + result = (char*)zts_inet_ntop(arg1, (void const*)arg2, arg3, arg4); + jresult = SWIG_csharp_string_callback((const char*)result); + return jresult; +} + +SWIGEXPORT int SWIGSTDCALL CSharp_zts_inet_pton(int jarg1, char* jarg2, void* jarg3) +{ + int jresult; + int arg1; + char* arg2 = (char*)0; + void* arg3 = (void*)0; + int result; + arg1 = (int)jarg1; + arg2 = (char*)jarg2; + arg3 = (void*)jarg3; + result = (int)zts_inet_pton(arg1, (char const*)arg2, arg3); + jresult = result; + return jresult; +} + +#ifdef __cplusplus +} +#endif diff --git a/src/bindings/csharp/Constants.cs b/src/bindings/csharp/Constants.cs old mode 100644 new mode 100755 index e7c6882..3af477f --- a/src/bindings/csharp/Constants.cs +++ b/src/bindings/csharp/Constants.cs @@ -4,7 +4,7 @@ * Use of this software is governed by the Business Source License included * in the LICENSE.TXT file in the project's root directory. * - * Change Date: 2025-01-01 + * Change Date: 2026-01-01 * * On the date above, in accordance with the Business Source License, use * of this software will be governed by version 2.0 of the Apache License. @@ -13,341 +13,246 @@ using ZeroTier; -namespace ZeroTier -{ - public class Constants - { - // General error codes - public static readonly int ERR_OK = 0; - public static readonly int ERR_SOCKET = -1; - public static readonly int ERR_SERVICE = -2; - public static readonly int ERR_ARG = -3; - public static readonly int ERR_NO_RESULT = -4; - public static readonly int ERR_GENERAL = -5; - - // Node events - public static readonly short EVENT_NODE_UP = 200; - public static readonly short EVENT_NODE_ONLINE = 201; - public static readonly short EVENT_NODE_OFFLINE = 202; - public static readonly short EVENT_NODE_DOWN = 203; - public static readonly short EVENT_NODE_IDENTITY_COLLISION = 204; - public static readonly short EVENT_NODE_UNRECOVERABLE_ERROR = 205; - public static readonly short EVENT_NODE_NORMAL_TERMINATION = 206; - - // Network events - public static readonly short EVENT_NETWORK_NOT_FOUND = 210; - public static readonly short EVENT_NETWORK_CLIENT_TOO_OLD = 211; - public static readonly short EVENT_NETWORK_REQ_CONFIG = 212; - public static readonly short EVENT_NETWORK_OK = 213; - public static readonly short EVENT_NETWORK_ACCESS_DENIED = 214; - public static readonly short EVENT_NETWORK_READY_IP4 = 215; - public static readonly short EVENT_NETWORK_READY_IP6 = 216; - public static readonly short EVENT_NETWORK_READY_IP4_IP6 = 217; - public static readonly short EVENT_NETWORK_DOWN = 218; - public static readonly short EVENT_NETWORK_UPDATE = 219; - - // Network Stack events - public static readonly short EVENT_STACK_UP = 220; - public static readonly short EVENT_STACK_DOWN = 221; - - // lwIP netif events - public static readonly short EVENT_NETIF_UP = 230; - public static readonly short EVENT_NETIF_DOWN = 231; - public static readonly short EVENT_NETIF_REMOVED = 232; - public static readonly short EVENT_NETIF_LINK_UP = 233; - public static readonly short EVENT_NETIF_LINK_DOWN = 234; - - // Peer events - public static readonly short EVENT_PEER_DIRECT = 240; - public static readonly short EVENT_PEER_RELAY = 241; - public static readonly short EVENT_PEER_UNREACHABLE = 242; - public static readonly short EVENT_PEER_PATH_DISCOVERED = 243; - public static readonly short EVENT_PEER_PATH_DEAD = 244; - - // Route events - public static readonly short EVENT_ROUTE_ADDED = 250; - public static readonly short EVENT_ROUTE_REMOVED = 251; - - // Address events - public static readonly short EVENT_ADDR_ADDED_IP4 = 260; - public static readonly short EVENT_ADDR_REMOVED_IP4 = 261; - public static readonly short EVENT_ADDR_ADDED_IP6 = 262; - public static readonly short EVENT_ADDR_REMOVED_IP6 = 263; +namespace ZeroTier { +public class Constants { + public static readonly short ERR_OK = 0; + public static readonly short ERR_SOCKET = -1; + public static readonly short ERR_SERVICE = -2; + public static readonly short ERR_ARG = -3; + public static readonly short ERR_NO_RESULT = -4; + public static readonly short ERR_GENERAL = -5; - // Socket error codes - public static readonly short EPERM = 1; /* Operation not permitted */ - public static readonly short ENOENT = 2; /* No such file or directory */ - public static readonly short ESRCH = 3; /* No such process */ - public static readonly short EINTR = 4; /* Interrupted system call */ - public static readonly short EIO = 5; /* I/O error */ - public static readonly short ENXIO = 6; /* No such device or address */ - public static readonly short E2BIG = 7; /* Arg list too long */ - public static readonly short ENOEXEC = 8; /* Exec format error */ - public static readonly short EBADF = 9; /* Bad file number */ - public static readonly short ECHILD = 10; /* No child processes */ - public static readonly short EAGAIN = 11; /* Try again */ - public static readonly short ENOMEM = 12; /* Out of memory */ - public static readonly short EACCES = 13; /* Permission denied */ - public static readonly short EFAULT = 14; /* Bad address */ - public static readonly short ENOTBLK = 15; /* Block device required */ - public static readonly short EBUSY = 16; /* Device or resource busy */ - public static readonly short EEXIST = 17; /* File exists */ - public static readonly short EXDEV = 18; /* Cross-device link */ - public static readonly short ENODEV = 19; /* No such device */ - public static readonly short ENOTDIR = 20; /* Not a directory */ - public static readonly short EISDIR = 21; /* Is a directory */ - public static readonly short EINVAL = 22; /* Invalid argument */ - public static readonly short ENFILE = 23; /* File table overflow */ - public static readonly short EMFILE = 24; /* Too many open files */ - public static readonly short ENOTTY = 25; /* Not a typewriter */ - public static readonly short ETXTBSY = 26; /* Text file busy */ - public static readonly short EFBIG = 27; /* File too large */ - public static readonly short ENOSPC = 28; /* No space left on device */ - public static readonly short ESPIPE = 29; /* Illegal seek */ - public static readonly short EROFS = 30; /* Read-only file system */ - public static readonly short EMLINK = 31; /* Too many links */ - public static readonly short EPIPE = 32; /* Broken pipe */ - public static readonly short EDOM = 33; /* Math argument out of domain of func */ - public static readonly short ERANGE = 34; /* Math result not representable */ - public static readonly short EDEADLK = 35; /* Resource deadlock would occur */ - public static readonly short ENAMETOOLONG = 36; /* File name too long */ - public static readonly short ENOLCK = 37; /* No record locks available */ - public static readonly short ENOSYS = 38; /* Function not implemented */ - public static readonly short ENOTEMPTY = 39; /* Directory not empty */ - public static readonly short ELOOP = 40; /* Too many symbolic links encountered */ - public static readonly short EWOULDBLOCK = EAGAIN; /* Operation would block */ - public static readonly short ENOMSG = 42; /* No message of desired type */ - public static readonly short EIDRM = 43; /* Identifier removed */ - public static readonly short ECHRNG = 44; /* Channel number out of range */ - public static readonly short EL2NSYNC = 45; /* Level 2 not synchronized */ - public static readonly short EL3HLT = 46; /* Level 3 halted */ - public static readonly short EL3RST = 47; /* Level 3 reset */ - public static readonly short ELNRNG = 48; /* Link number out of range */ - public static readonly short EUNATCH = 49; /* Protocol driver not attached */ - public static readonly short ENOCSI = 50; /* No CSI structure available */ - public static readonly short EL2HLT = 51; /* Level 2 halted */ - public static readonly short EBADE = 52; /* Invalid exchange */ - public static readonly short EBADR = 53; /* Invalid request descriptor */ - public static readonly short EXFULL = 54; /* Exchange full */ - public static readonly short ENOANO = 55; /* No anode */ - public static readonly short EBADRQC = 56; /* Invalid request code */ - public static readonly short EBADSLT = 57; /* Invalid slot */ + public static readonly short EVENT_NODE_UP = 200; + public static readonly short EVENT_NODE_ONLINE = 201; + public static readonly short EVENT_NODE_OFFLINE = 202; + public static readonly short EVENT_NODE_DOWN = 203; + public static readonly short ZTS_EVENT_NODE_FATAL_ERROR = 204; - public static readonly short EDEADLOCK = EDEADLK; + public static readonly short EVENT_NETWORK_NOT_FOUND = 210; + public static readonly short EVENT_NETWORK_CLIENT_TOO_OLD = 211; + public static readonly short EVENT_NETWORK_REQ_CONFIG = 212; + public static readonly short EVENT_NETWORK_OK = 213; + public static readonly short EVENT_NETWORK_ACCESS_DENIED = 214; + public static readonly short EVENT_NETWORK_READY_IP4 = 215; + public static readonly short EVENT_NETWORK_READY_IP6 = 216; + public static readonly short EVENT_NETWORK_READY_IP4_IP6 = 217; + public static readonly short EVENT_NETWORK_DOWN = 218; + public static readonly short EVENT_NETWORK_UPDATE = 219; - public static readonly short EBFONT = 59; /* Bad font file format */ - public static readonly short ENOSTR = 60; /* Device not a stream */ - public static readonly short ENODATA = 61; /* No data available */ - public static readonly short ETIME = 62; /* Timer expired */ - public static readonly short ENOSR = 63; /* Out of streams resources */ - public static readonly short ENONET = 64; /* Machine is not on the network */ - public static readonly short ENOPKG = 65; /* Package not installed */ - public static readonly short EREMOTE = 66; /* Object is remote */ - public static readonly short ENOLINK = 67; /* Link has been severed */ - public static readonly short EADV = 68; /* Advertise error */ - public static readonly short ESRMNT = 69; /* Srmount error */ - public static readonly short ECOMM = 70; /* Communication error on send */ - public static readonly short EPROTO = 71; /* Protocol error */ - public static readonly short EMULTIHOP = 72; /* Multihop attempted */ - public static readonly short EDOTDOT = 73; /* RFS specific error */ - public static readonly short EBADMSG = 74; /* Not a data message */ - public static readonly short EOVERFLOW = 75; /* Value too large for defined data type */ - public static readonly short ENOTUNIQ = 76; /* Name not unique on network */ - public static readonly short EBADFD = 77; /* File descriptor in bad state */ - public static readonly short EREMCHG = 78; /* Remote address changed */ - public static readonly short ELIBACC = 79; /* Can not access a needed shared library */ - public static readonly short ELIBBAD = 80; /* Accessing a corrupted shared library */ - public static readonly short ELIBSCN = 81; /* .lib section in a.out corrupted */ - public static readonly short ELIBMAX = 82; /* Attempting to link in too many shared libraries */ - public static readonly short ELIBEXEC = 83; /* Cannot exec a shared library directly */ - public static readonly short EILSEQ = 84; /* Illegal byte sequence */ - public static readonly short ERESTART = 85; /* Interrupted system call should be restarted */ - public static readonly short ESTRPIPE = 86; /* Streams pipe error */ - public static readonly short EUSERS = 87; /* Too many users */ - public static readonly short ENOTSOCK = 88; /* Socket operation on non-socket */ - public static readonly short EDESTADDRREQ = 89; /* Destination address required */ - public static readonly short EMSGSIZE = 90; /* Message too long */ - public static readonly short EPROTOTYPE = 91; /* Protocol wrong type for socket */ - public static readonly short ENOPROTOOPT = 92; /* Protocol not available */ - public static readonly short EPROTONOSUPPORT = 93; /* Protocol not supported */ - public static readonly short ESOCKTNOSUPPORT = 94; /* Socket type not supported */ - public static readonly short EOPNOTSUPP = 95; /* Operation not supported on transport endpoint */ - public static readonly short EPFNOSUPPORT = 96; /* Protocol family not supported */ - public static readonly short EAFNOSUPPORT = 97; /* Address family not supported by protocol */ - public static readonly short EADDRINUSE = 98; /* Address already in use */ - public static readonly short EADDRNOTAVAIL = 99; /* Cannot assign requested address */ - public static readonly short ENETDOWN = 100; /* Network is down */ - public static readonly short ENETUNREACH = 101; /* Network is unreachable */ - public static readonly short ENETRESET = 102; /* Network dropped connection because of reset */ - public static readonly short ECONNABORTED = 103; /* Software caused connection abort */ - public static readonly short ECONNRESET = 104; /* Connection reset by peer */ - public static readonly short ENOBUFS = 105; /* No buffer space available */ - public static readonly short EISCONN = 106; /* Transport endpoint is already connected */ - public static readonly short ENOTCONN = 107; /* Transport endpoint is not connected */ - public static readonly short ESHUTDOWN = 108; /* Cannot send after transport endpoint shutdown */ - public static readonly short ETOOMANYREFS = 109; /* Too many references: cannot splice */ - public static readonly short ETIMEDOUT = 110; /* Connection timed out */ - public static readonly short ECONNREFUSED = 111; /* Connection refused */ - public static readonly short EHOSTDOWN = 112; /* Host is down */ - public static readonly short EHOSTUNREACH = 113; /* No route to host */ - public static readonly short EALREADY = 114; /* Operation already in progress */ - public static readonly short EINPROGRESS = 115; /* Operation now in progress */ - public static readonly short ESTALE = 116; /* Stale NFS file handle */ - public static readonly short EUCLEAN = 117; /* Structure needs cleaning */ - public static readonly short ENOTNAM = 118; /* Not a XENIX named type file */ - public static readonly short ENAVAIL = 119; /* No XENIX semaphores available */ - public static readonly short EISNAM = 120; /* Is a named type file */ - public static readonly short EREMOTEIO = 121; /* Remote I/O error */ - public static readonly short EDQUOT = 122; /* Quota exceeded */ - public static readonly short ENOMEDIUM = 123; /* No medium found */ - public static readonly short EMEDIUMTYPE = 124; /* Wrong medium type */ + public static readonly short EVENT_STACK_UP = 220; + public static readonly short EVENT_STACK_DOWN = 221; - public static readonly short INET_ADDRSTRLEN = 16; - public static readonly short INET6_ADDRSTRLEN = 46; + public static readonly short EVENT_NETIF_UP = 230; + public static readonly short EVENT_NETIF_DOWN = 231; + public static readonly short EVENT_NETIF_REMOVED = 232; + public static readonly short EVENT_NETIF_LINK_UP = 233; + public static readonly short EVENT_NETIF_LINK_DOWN = 234; - /** 255.255.255.255 */ - //public static readonly uint IPADDR_NONE =((uint32_t)0xffffffffUL); - /** 127.0.0.1 */ - //public static readonly uint IPADDR_LOOPBACK =((uint32_t)0x7f000001UL); - /** 0.0.0.0 */ - //public static readonly uint IPADDR_ANY =((uint32_t)0x00000000UL); - /** 255.255.255.255 */ - //public static readonly uint IPADDR_BROADCAST =((uint32_t)0xffffffffUL); + public static readonly short EVENT_PEER_DIRECT = 240; + public static readonly short EVENT_PEER_RELAY = 241; + public static readonly short EVENT_PEER_UNREACHABLE = 242; + public static readonly short EVENT_PEER_PATH_DISCOVERED = 243; + public static readonly short EVENT_PEER_PATH_DEAD = 244; - /** 255.255.255.255 */ - //public static readonly uint INADDR_NONE =IPADDR_NONE; - /** 127.0.0.1 */ - //public static readonly uint INADDR_LOOPBACK =IPADDR_LOOPBACK; - /** 0.0.0.0 */ - //public static readonly uint INADDR_ANY =IPADDR_ANY; - /** 255.255.255.255 */ - //public static readonly uint INADDR_BROADCAST =IPADDR_BROADCAST; + public static readonly short EVENT_ROUTE_ADDED = 250; + public static readonly short EVENT_ROUTE_REMOVED = 251; - // Socket protocol types - public static readonly short SOCK_STREAM = 0x0001; - public static readonly short SOCK_DGRAM = 0x0002; - public static readonly short SOCK_RAW = 0x0003; - // Socket family types - public static readonly short AF_UNSPEC = 0x0000; - public static readonly short AF_INET = 0x0002; - public static readonly short AF_INET6 = 0x000a; - public static readonly short PF_INET = AF_INET; - public static readonly short PF_INET6 = AF_INET6; - public static readonly short PF_UNSPEC = AF_UNSPEC; - // Protocol command types - public static readonly short IPPROTO_IP = 0x0000; - public static readonly short IPPROTO_ICMP = 0x0001; - public static readonly short IPPROTO_TCP = 0x0006; - public static readonly short IPPROTO_UDP = 0x0011; - public static readonly short IPPROTO_IPV6 = 0x0029; - public static readonly short IPPROTO_ICMPV6 = 0x003a; - public static readonly short IPPROTO_UDPLITE = 0x0088; - public static readonly short IPPROTO_RAW = 0x00ff; - // send() and recv() flags - public static readonly short MSG_PEEK = 0x0001; - public static readonly short MSG_WAITALL = 0x0002; // NOT YET SUPPORTED - public static readonly short MSG_OOB = 0x0004; // NOT YET SUPPORTED - public static readonly short MSG_DONTWAIT = 0x0008; - public static readonly short MSG_MORE = 0x0010; + public static readonly short EVENT_ADDR_ADDED_IP4 = 260; + public static readonly short EVENT_ADDR_REMOVED_IP4 = 261; + public static readonly short EVENT_ADDR_ADDED_IP6 = 262; + public static readonly short EVENT_ADDR_REMOVED_IP6 = 263; - // Macros for defining ioctl() command values - /* - public static readonly ulong IOCPARM_MASK = 0x7fU; - public static readonly ulong IOC_VOID = 0x20000000UL; - public static readonly ulong IOC_OUT = 0x40000000UL; - public static readonly ulong IOC_IN = 0x80000000UL; - public static readonly ulong IOC_INOUT = (IOC_IN | IOC_OUT); - public static readonly ulong IO(x,y) = (IOC_VOID | ((x)<<8)|(y)); - public static readonly ulong IOR(x,y,t) = (IOC_OUT | (((long)sizeof(t) & IOCPARM_MASK)<<16) | ((x)<<8) | (y)); - public static readonly ulong IOW(x,y,t) = (IOC_IN | (((long)sizeof(t) & IOCPARM_MASK)<<16) | ((x)<<8) | (y)); - // ioctl() commands - public static readonly ulong FIONREAD = IOR('f', 127, unsigned long); - public static readonly ulong FIONBIO = IOW('f', 126, unsigned long); - */ + public static readonly short EVENT_STORE_IDENTITY_SECRET = 270; + public static readonly short EVENT_STORE_IDENTITY_PUBLIC = 271; + public static readonly short EVENT_STORE_PLANET = 272; + public static readonly short EVENT_STORE_PEER = 273; + public static readonly short EVENT_STORE_NETWORK = 274; - // Socket level option number - public static readonly short SOL_SOCKET = 0x0fff; - // Socket options - public static readonly short SO_DEBUG = 0x0001; // NOT YET SUPPORTED - public static readonly short SO_ACCEPTCONN = 0x0002; - public static readonly short SO_REUSEADDR = 0x0004; - public static readonly short SO_KEEPALIVE = 0x0008; - public static readonly short SO_DONTROUTE = 0x0010; // NOT YET SUPPORTED - public static readonly short SO_BROADCAST = 0x0020; - public static readonly short SO_USELOOPBACK = 0x0040; // NOT YET SUPPORTED - public static readonly short SO_LINGER = 0x0080; - public static readonly short SO_DONTLINGER = ((short)(~SO_LINGER)); - public static readonly short SO_OOBINLINE = 0x0100; // NOT YET SUPPORTED - public static readonly short SO_REUSEPORT = 0x0200; // NOT YET SUPPORTED - public static readonly short SO_SNDBUF = 0x1001; // NOT YET SUPPORTED - public static readonly short SO_RCVBUF = 0x1002; - public static readonly short SO_SNDLOWAT = 0x1003; // NOT YET SUPPORTED - public static readonly short SO_RCVLOWAT = 0x1004; // NOT YET SUPPORTED - public static readonly short SO_SNDTIMEO = 0x1005; - public static readonly short SO_RCVTIMEO = 0x1006; - public static readonly short SO_ERROR = 0x1007; - public static readonly short SO_TYPE = 0x1008; - public static readonly short SO_CONTIMEO = 0x1009; - public static readonly short SO_NO_CHECK = 0x100a; - public static readonly short SO_BINDTODEVICE = 0x100b; - // IPPROTO_IP options - public static readonly short IP_TOS = 0x0001; - public static readonly short IP_TTL = 0x0002; - public static readonly short IP_PKTINFO = 0x0008; - // IPPROTO_TCP options - public static readonly short TCP_NODELAY = 0x0001; - public static readonly short TCP_KEEPALIVE = 0x0002; - public static readonly short TCP_KEEPIDLE = 0x0003; - public static readonly short TCP_KEEPINTVL = 0x0004; - public static readonly short TCP_KEEPCNT = 0x0005; - // IPPROTO_IPV6 options - public static readonly short IPV6_CHECKSUM = 0x0007; // RFC3542: calculate and insert the ICMPv6 checksum for raw sockets. - public static readonly short IPV6_V6ONLY = 0x001b; // RFC3493: boolean control to restrict AF_INET6 sockets to IPv6 communications only. - // UDPLITE options - public static readonly short UDPLITE_SEND_CSCOV = 0x01; // sender checksum coverage - public static readonly short UDPLITE_RECV_CSCOV = 0x02; // minimal receiver checksum coverage - // UDPLITE options - public static readonly short IP_MULTICAST_TTL = 5; - public static readonly short IP_MULTICAST_IF = 6; - public static readonly short IP_MULTICAST_LOOP = 7; + // Socket error codes + public static readonly short EPERM = 1; + public static readonly short ENOENT = 2; + public static readonly short ESRCH = 3; + public static readonly short EINTR = 4; + public static readonly short EIO = 5; + public static readonly short ENXIO = 6; + public static readonly short EBADF = 9; + public static readonly short EAGAIN = 11; + public static readonly short EWOULDBLOCK = 11; + public static readonly short ENOMEM = 12; + public static readonly short EACCES = 13; + public static readonly short EFAULT = 14; + public static readonly short EBUSY = 16; + public static readonly short EEXIST = 17; + public static readonly short ENODEV = 19; + public static readonly short EINVAL = 22; + public static readonly short ENFILE = 23; + public static readonly short EMFILE = 24; + public static readonly short ENOSYS = 38; + public static readonly short ENOTSOCK = 88; + public static readonly short EDESTADDRREQ = 89; + public static readonly short EMSGSIZE = 90; + public static readonly short EPROTOTYPE = 91; + public static readonly short ENOPROTOOPT = 92; + public static readonly short EPROTONOSUPPORT = 93; + public static readonly short ESOCKTNOSUPPORT = 94; + public static readonly short EOPNOTSUPP = 95; + public static readonly short EPFNOSUPPORT = 96; + public static readonly short EAFNOSUPPORT = 97; + public static readonly short EADDRINUSE = 98; + public static readonly short EADDRNOTAVAIL = 99; + public static readonly short ENETDOWN = 100; + public static readonly short ENETUNREACH = 101; + public static readonly short ECONNABORTED = 103; + public static readonly short ECONNRESET = 104; + public static readonly short ENOBUFS = 105; + public static readonly short EISCONN = 106; + public static readonly short ENOTCONN = 107; + public static readonly short ETIMEDOUT = 110; + public static readonly short EHOSTUNREACH = 113; + public static readonly short EALREADY = 114; + public static readonly short EINPROGRESS = 115; - // Multicast options - public static readonly short IP_ADD_MEMBERSHIP = 3; - public static readonly short IP_DROP_MEMBERSHIP = 4; + // Common constants + public static readonly short MAC_ADDRSTRLEN = 18; + public static readonly short INET_ADDRSTRLEN = 16; + public static readonly short INET6_ADDRSTRLEN = 46; + public static readonly short IP_MAX_STR_LEN = 46; + public static readonly short STORE_DATA_LEN = 4096; + public static readonly short MAX_NETWORK_SHORT_NAME_LENGTH = 127; + public static readonly short MAX_NETWORK_ROUTES = 32; + public static readonly short MAX_ASSIGNED_ADDRESSES = 16; + public static readonly short MAX_PEER_NETWORK_PATHS = 16; + public static readonly short MAX_MULTICAST_SUBSCRIPTIONS = 1024; - public static readonly short IPV6_JOIN_GROUP = 12; - public static readonly short IPV6_ADD_MEMBERSHIP = IPV6_JOIN_GROUP; - public static readonly short IPV6_LEAVE_GROUP = 13; - public static readonly short IPV6_DROP_MEMBERSHIP = IPV6_LEAVE_GROUP; + // Peer roles + public static readonly byte PEER_ROLE_LEAF = 0; + public static readonly byte PEER_ROLE_MOON = 1; + public static readonly byte PEER_ROLE_PLANET = 2; - // Polling options - public static readonly short POLLIN = 0x001; - public static readonly short POLLOUT = 0x002; - public static readonly short POLLERR = 0x004; - public static readonly short POLLNVAL = 0x008; - // Below values are unimplemented - public static readonly short POLLRDNORM = 0x010; - public static readonly short POLLRDBAND = 0x020; - public static readonly short POLLPRI = 0x040; - public static readonly short POLLWRNORM = 0x080; - public static readonly short POLLWRBAND = 0x100; - public static readonly short POLLHUP = 0x200; + // Network status codes + public static readonly byte NETWORK_STATUS_REQUESTING_CONFIGURATION = 0; + public static readonly byte NETWORK_STATUS_OK = 1; + public static readonly byte NETWORK_STATUS_ACCESS_DENIED = 2; + public static readonly byte NETWORK_STATUS_NOT_FOUND = 3; + public static readonly byte NETWORK_STATUS_PORT_ERROR = 4; + public static readonly byte NETWORK_STATUS_CLIENT_TOO_OLD = 5; - public static readonly short F_GETFL = 0x0003; - public static readonly short F_SETFL = 0x0004; + // + public static readonly byte NETWORK_TYPE_PRIVATE = 0; + public static readonly byte NETWORK_TYPE_PUBLIC = 1; - // File status flags and file access modes for fnctl, these are bits in an int. - public static readonly short O_NONBLOCK = 1; - public static readonly short O_NDELAY = O_NONBLOCK; - public static readonly short O_RDONLY = 2; - public static readonly short O_WRONLY = 4; - public static readonly short O_RDWR = (short)(O_RDONLY|O_WRONLY); + // Socket protocol types + public static readonly short SOCK_STREAM = 0x0001; + public static readonly short SOCK_DGRAM = 0x0002; + public static readonly short SOCK_RAW = 0x0003; + // Socket family types + public static readonly short AF_UNSPEC = 0x0000; + public static readonly short AF_INET = 0x0002; + public static readonly short AF_INET6 = 0x000a; + public static readonly short PF_INET = AF_INET; + public static readonly short PF_INET6 = AF_INET6; + public static readonly short PF_UNSPEC = AF_UNSPEC; + // Protocol command types + public static readonly short IPPROTO_IP = 0x0000; + public static readonly short IPPROTO_ICMP = 0x0001; + public static readonly short IPPROTO_TCP = 0x0006; + public static readonly short IPPROTO_UDP = 0x0011; + public static readonly short IPPROTO_IPV6 = 0x0029; + public static readonly short IPPROTO_ICMPV6 = 0x003a; + public static readonly short IPPROTO_UDPLITE = 0x0088; + public static readonly short IPPROTO_RAW = 0x00ff; + // send() and recv() flags + public static readonly short MSG_PEEK = 0x0001; + public static readonly short MSG_WAITALL = 0x0002; // NOT YET SUPPORTED + public static readonly short MSG_OOB = 0x0004; // NOT YET SUPPORTED + public static readonly short MSG_DONTWAIT = 0x0008; + public static readonly short MSG_MORE = 0x0010; - public static readonly short MSG_TRUNC = 0x04; - public static readonly short MSG_CTRUNC = 0x08; + // Socket level option number + public static readonly short SOL_SOCKET = 0x0fff; + // Socket options + public static readonly short SO_DEBUG = 0x0001; // NOT YET SUPPORTED + public static readonly short SO_ACCEPTCONN = 0x0002; + public static readonly short SO_REUSEADDR = 0x0004; + public static readonly short SO_KEEPALIVE = 0x0008; + public static readonly short SO_DONTROUTE = 0x0010; // NOT YET SUPPORTED + public static readonly short SO_BROADCAST = 0x0020; + public static readonly short SO_USELOOPBACK = 0x0040; // NOT YET SUPPORTED + public static readonly short SO_LINGER = 0x0080; + public static readonly short SO_DONTLINGER = ((short)(~SO_LINGER)); + public static readonly short SO_OOBINLINE = 0x0100; // NOT YET SUPPORTED + public static readonly short SO_REUSEPORT = 0x0200; // NOT YET SUPPORTED + public static readonly short SO_SNDBUF = 0x1001; // NOT YET SUPPORTED + public static readonly short SO_RCVBUF = 0x1002; + public static readonly short SO_SNDLOWAT = 0x1003; // NOT YET SUPPORTED + public static readonly short SO_RCVLOWAT = 0x1004; // NOT YET SUPPORTED + public static readonly short SO_SNDTIMEO = 0x1005; + public static readonly short SO_RCVTIMEO = 0x1006; + public static readonly short SO_ERROR = 0x1007; + public static readonly short SO_TYPE = 0x1008; + public static readonly short SO_CONTIMEO = 0x1009; + public static readonly short SO_NO_CHECK = 0x100a; + public static readonly short SO_BINDTODEVICE = 0x100b; + // IPPROTO_IP options + public static readonly short IP_TOS = 0x0001; + public static readonly short IP_TTL = 0x0002; + public static readonly short IP_PKTINFO = 0x0008; + // IPPROTO_TCP options + public static readonly short TCP_NODELAY = 0x0001; + public static readonly short TCP_KEEPALIVE = 0x0002; + public static readonly short TCP_KEEPIDLE = 0x0003; + public static readonly short TCP_KEEPINTVL = 0x0004; + public static readonly short TCP_KEEPCNT = 0x0005; + // IPPROTO_IPV6 options + public static readonly short IPV6_CHECKSUM = + 0x0007; // RFC3542: calculate and insert the ICMPv6 checksum for raw sockets. + public static readonly short IPV6_V6ONLY = + 0x001b; // RFC3493: boolean control to restrict AF_INET6 sockets to IPv6 communications only. + // UDPLITE options + public static readonly short UDPLITE_SEND_CSCOV = 0x01; // sender checksum coverage + public static readonly short UDPLITE_RECV_CSCOV = 0x02; // minimal receiver checksum coverage + // UDPLITE options + public static readonly short IP_MULTICAST_TTL = 5; + public static readonly short IP_MULTICAST_IF = 6; + public static readonly short IP_MULTICAST_LOOP = 7; - public static readonly short SHUT_RD = 0x0; - public static readonly short SHUT_WR = 0x1; - public static readonly short SHUT_RDWR = 0x2; - } -} \ No newline at end of file + // Multicast options + public static readonly short IP_ADD_MEMBERSHIP = 3; + public static readonly short IP_DROP_MEMBERSHIP = 4; + + public static readonly short IPV6_JOIN_GROUP = 12; + public static readonly short IPV6_ADD_MEMBERSHIP = IPV6_JOIN_GROUP; + public static readonly short IPV6_LEAVE_GROUP = 13; + public static readonly short IPV6_DROP_MEMBERSHIP = IPV6_LEAVE_GROUP; + + // Polling options + public static readonly short POLLIN = 0x001; + public static readonly short POLLOUT = 0x002; + public static readonly short POLLERR = 0x004; + public static readonly short POLLNVAL = 0x008; + // Below values are unimplemented + public static readonly short POLLRDNORM = 0x010; + public static readonly short POLLRDBAND = 0x020; + public static readonly short POLLPRI = 0x040; + public static readonly short POLLWRNORM = 0x080; + public static readonly short POLLWRBAND = 0x100; + public static readonly short POLLHUP = 0x200; + + public static readonly short F_GETFL = 0x0003; + public static readonly short F_SETFL = 0x0004; + + // File status flags and file access modes for fnctl, these are bits in an int. + public static readonly short O_NONBLOCK = 1; + public static readonly short O_NDELAY = O_NONBLOCK; + public static readonly short O_RDONLY = 2; + public static readonly short O_WRONLY = 4; + public static readonly short O_RDWR = (short)(O_RDONLY | O_WRONLY); + + public static readonly short MSG_TRUNC = 0x04; + public static readonly short MSG_CTRUNC = 0x08; + + public static readonly short SHUT_RD = 0x0; + public static readonly short SHUT_WR = 0x1; + public static readonly short SHUT_RDWR = 0x2; +} +} diff --git a/src/bindings/csharp/Event.cs b/src/bindings/csharp/Event.cs old mode 100644 new mode 100755 index f5ed712..8de58d7 --- a/src/bindings/csharp/Event.cs +++ b/src/bindings/csharp/Event.cs @@ -4,7 +4,7 @@ * Use of this software is governed by the Business Source License included * in the LICENSE.TXT file in the project's root directory. * - * Change Date: 2025-01-01 + * Change Date: 2026-01-01 * * On the date above, in accordance with the Business Source License, use * of this software will be governed by version 2.0 of the Apache License. @@ -17,140 +17,13 @@ using ZeroTier; namespace ZeroTier.Core { - /* Convenience structures for exposing lower level operational details to the user. - These structures do not have the same memory layout or content as those found in - include/ZeroTierSockets.h */ - - /// - /// Structure containing information about the local Node. - /// - public class NodeDetails - { - // Node ID - public ulong nodeId; - - /** - * The port used by the service to send and receive - * all encapsulated traffic - */ - public ushort primaryPort; - public ushort secondaryPort; - public ushort tertiaryPort; - - /** - * ZT version - */ - public byte versionMajor; - public byte versionMinor; - public byte versionRev; - } - - public class MulticastSubscription - { - ulong macAddress; - uint AdditionalDistinguishingInformation; - } - - /// - /// Structure containing information about virtual networks. - /// - public class NetworkDetails - { - public ulong networkId; - public ulong macAddress; - public string networkName; - //public byte status; - public byte type; - public ushort mtu; - public bool bridgingAllowed; - public bool broadcastEnabled; - //public int portError; - public IPAddress[] assignedAddresses; - public IPAddress[] routes; - public MulticastSubscription[] multicastSubscroptions; - } - - /// - /// Structure containing state information about the low-level ethernet driver. - /// - public class NetifDetails - { - public ulong networkId; - public ulong macAddress; - public int mtu; - } - - /// - /// Structure containing routing information for networks. - /// - public class RouteDetails - { - public EndPoint target; - public EndPoint via; - public ushort flags; - public ushort metric; - } - - /// - /// Structure containing information about remote peer reachability. - /// - public class PeerDetails - { - ulong nodeId; - byte versionMajor; - byte versionMinor; - byte versionRev; - int latency; - byte role; - IPAddress[] paths; - } - - /// - /// Structure containing information about assigned addresses. - /// - public class AddrDetails - { - ulong networkId; - IPAddress address; - } - - /// - /// Class used to convey details of a low-level network event to the user. - /// - public class Event - { - int _eventCode; - string _eventName; - - public NodeDetails nodeDetails; - public NetworkDetails networkDetails; - public NetifDetails netifDetails; - public RouteDetails routeDetails; - public PeerDetails peerDetails; - public AddrDetails addrDetails; - - public Event(int eventCode, string eventName) - { - _eventCode = eventCode; - _eventName = eventName; - nodeDetails = null; - networkDetails = null; - netifDetails = null; - routeDetails = null; - peerDetails = null; - addrDetails = null; - } - - public int EventCode { - get { - return _eventCode; - } - } - - public string EventName { - get { - return _eventName; - } - } - } -} \ No newline at end of file + public class Event { + public NodeInfo NodeInfo { get; set; } + public NetworkInfo NetworkInfo { get; set; } + public RouteInfo RouteInfo { get; set; } + public PeerInfo PeerInfo { get; set; } + public AddressInfo AddressInfo { get; set; } + public int Code { get; set; } + public string Name { get; set; } + } +} diff --git a/src/bindings/csharp/MulticastInfo.cs b/src/bindings/csharp/MulticastInfo.cs new file mode 100755 index 0000000..e5ea16e --- /dev/null +++ b/src/bindings/csharp/MulticastInfo.cs @@ -0,0 +1,24 @@ +/* + * Copyright (c)2013-2021 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2026-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +using System.Net; + +using ZeroTier; + +namespace ZeroTier.Core +{ + public class MulticastInfo { + public ulong MACAddress; + public uint additionalDistinguishingInformation; + } +} diff --git a/src/bindings/csharp/NetworkInfo.cs b/src/bindings/csharp/NetworkInfo.cs new file mode 100755 index 0000000..24223d1 --- /dev/null +++ b/src/bindings/csharp/NetworkInfo.cs @@ -0,0 +1,67 @@ +/* + * Copyright (c)2013-2021 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2026-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +using System.Net; +using System.Collections.Concurrent; +using System.Collections.Generic; + +using ZeroTier; + +namespace ZeroTier.Core +{ + public class NetworkInfo { + public ulong Id { get; set; } + public ulong MACAddress; + public string Name; + public int Status; + public int Type; + public uint MTU; + public int DHCP; + public bool Bridge; + public bool BroadcastEnabled; + internal bool transportReady; // Synthetic value + + public bool IsPrivate + { + get { + return Type == Constants.NETWORK_TYPE_PRIVATE; + } + } + + public bool IsPublic + { + get { + return Type == Constants.NETWORK_TYPE_PUBLIC; + } + } + + internal ConcurrentDictionary _addrs = new ConcurrentDictionary(); + + public ICollection Addresses + { + get { + return _addrs.Values; + } + } + + internal ConcurrentDictionary _routes = + new ConcurrentDictionary(); + + public ICollection Routes + { + get { + return _routes.Values; + } + } + } +} diff --git a/src/bindings/csharp/Node.cs b/src/bindings/csharp/Node.cs old mode 100644 new mode 100755 index 7411129..fb84c8d --- a/src/bindings/csharp/Node.cs +++ b/src/bindings/csharp/Node.cs @@ -4,15 +4,19 @@ * Use of this software is governed by the Business Source License included * in the LICENSE.TXT file in the project's root directory. * - * Change Date: 2025-01-01 + * Change Date: 2026-01-01 * * On the date above, in accordance with the Business Source License, use * of this software will be governed by version 2.0 of the Apache License. */ /****/ -using System.Runtime.InteropServices; using System; +using System.Net; +using System.Net.Sockets; +using System.Collections.Generic; +using System.Collections.Concurrent; +using System.Runtime.InteropServices; using ZeroTier; @@ -20,501 +24,825 @@ using ZeroTier; [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void CSharpCallbackWithStruct(IntPtr msgPtr); -/// -/// ZeroTier SDK -/// namespace ZeroTier.Core { - public delegate void ZeroTierManagedEventCallback(ZeroTier.Core.Event nodeEvent); + public delegate void ZeroTierManagedEventCallback(ZeroTier.Core.Event nodeEvent); - /// - /// ZeroTier Node - Virtual network subsystem - /// - public class Node - { - static ulong _nodeId = 0x0; - static bool _isOnline = false; - static bool _joinedAtLeastOneNetwork = false; - static bool _has_ipv4_routes = false; - static bool _has_ipv6_routes = false; - static bool _hasBeenFreed = false; - string _configFilePath; - ushort _servicePort; - static ZeroTierManagedEventCallback _managedCallback; - CSharpCallbackWithStruct _unmanagedCallback; + public class Node { + static ulong _id = 0x0; + static ushort _primaryPort; + static ushort _secondaryPort; + static ushort _tertiaryPort; + static int _versionMajor; + static int _versionMinor; + static int _versionRev; + static bool _isOnline = false; + static bool _hasBeenFreed = false; + string _configFilePath; + ushort _servicePort; - // Callback used internally to ferry events from the C++ layer - static void OnZeroTierEvent(IntPtr msgPtr) - { - // Marshal the callback message pointer to a structure that we can inspect - zts_callback_msg msg = - (zts_callback_msg)Marshal.PtrToStructure(msgPtr, typeof(zts_callback_msg)); + static ZeroTierManagedEventCallback _managedCallback; + CSharpCallbackWithStruct _unmanagedCallback; - ZeroTier.Core.Event newEvent = null; + ConcurrentDictionary _networks = + new ConcurrentDictionary(); + ConcurrentDictionary _peers = new ConcurrentDictionary(); - // Node events - if (msg.eventCode == Constants.EVENT_NODE_UP) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NODE_UP"); - } - if (msg.eventCode == Constants.EVENT_NODE_ONLINE) { - _isOnline = true; - // Marshal the node details pointer to a structure - zts_node_details details = - (zts_node_details)Marshal.PtrToStructure(msg.node, typeof(zts_node_details)); - _nodeId = details.address; - newEvent = new ZeroTier.Core.Event(msg.eventCode, "EVENT_NODE_ONLINE"); - } - if (msg.eventCode == Constants.EVENT_NODE_OFFLINE) { - _isOnline = false; - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NODE_OFFLINE"); - } - if (msg.eventCode == Constants.EVENT_NODE_NORMAL_TERMINATION) { - _isOnline = false; - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NODE_NORMAL_TERMINATION"); - } - if (msg.eventCode == Constants.EVENT_NODE_DOWN) { - _isOnline = false; - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NODE_DOWN"); - } - if (msg.eventCode == Constants.EVENT_NODE_IDENTITY_COLLISION) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NODE_IDENTITY_COLLISION"); - _isOnline = false; - } - if (msg.eventCode == Constants.EVENT_NODE_UNRECOVERABLE_ERROR) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NODE_UNRECOVERABLE_ERROR"); - _isOnline = false; - } + public Node() + { + ClearNode(); + } - // Network events - if (msg.eventCode == Constants.EVENT_NETWORK_NOT_FOUND) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_NOT_FOUND"); - } - if (msg.eventCode == Constants.EVENT_NETWORK_REQ_CONFIG) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_REQ_CONFIG"); - } - if (msg.eventCode == Constants.EVENT_NETWORK_ACCESS_DENIED) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_ACCESS_DENIED"); - } - if (msg.eventCode == Constants.EVENT_NETWORK_READY_IP4) { - zts_network_details unmanagedDetails = - (zts_network_details)Marshal.PtrToStructure(msg.network, typeof(zts_network_details)); - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_READY_IP4"); - newEvent.networkDetails = new NetworkDetails(); - newEvent.networkDetails.networkId = unmanagedDetails.nwid; - _joinedAtLeastOneNetwork = true; - _has_ipv4_routes = true; - } - if (msg.eventCode == Constants.EVENT_NETWORK_READY_IP6) { - zts_network_details unmanagedDetails = - (zts_network_details)Marshal.PtrToStructure(msg.network, typeof(zts_network_details)); - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_READY_IP6"); - newEvent.networkDetails = new NetworkDetails(); - newEvent.networkDetails.networkId = unmanagedDetails.nwid; - _joinedAtLeastOneNetwork = true; - _has_ipv6_routes = true; - } - if (msg.eventCode == Constants.EVENT_NETWORK_DOWN) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_DOWN"); - } - if (msg.eventCode == Constants.EVENT_NETWORK_CLIENT_TOO_OLD) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_CLIENT_TOO_OLD"); - } - if (msg.eventCode == Constants.EVENT_NETWORK_REQ_CONFIG) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_REQ_CONFIG"); - } - if (msg.eventCode == Constants.EVENT_NETWORK_OK) { - zts_network_details unmanagedDetails = - (zts_network_details)Marshal.PtrToStructure(msg.network, typeof(zts_network_details)); - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_OK"); - newEvent.networkDetails = new NetworkDetails(); - newEvent.networkDetails.networkId = unmanagedDetails.nwid; - } - if (msg.eventCode == Constants.EVENT_NETWORK_ACCESS_DENIED) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_ACCESS_DENIED"); - } - if (msg.eventCode == Constants.EVENT_NETWORK_READY_IP4_IP6) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_READY_IP4_IP6"); - } - if (msg.eventCode == Constants.EVENT_NETWORK_UPDATE) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_UPDATE"); - } + void ClearNode() + { + _id = 0x0; + _primaryPort = 0; + _secondaryPort = 0; + _tertiaryPort = 0; + _versionMajor = 0; + _versionMinor = 0; + _versionRev = 0; + _isOnline = false; + _configFilePath = string.Empty; + _servicePort = 0; + _networks.Clear(); + _peers.Clear(); + } - // Stack events - if (msg.eventCode == Constants.EVENT_STACK_UP) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_STACK_UP"); - } - if (msg.eventCode == Constants.EVENT_STACK_DOWN) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_STACK_DOWN"); - } + public int InitFromStorage(string configFilePath) + { + if (String.IsNullOrEmpty(configFilePath)) { + throw new ArgumentNullException("configFilePath"); + } + int res = Constants.ERR_OK; + Console.WriteLine("path = " + configFilePath); + if ((res = zts_init_from_storage(configFilePath)) == Constants.ERR_OK) { + _configFilePath = configFilePath; + } + return res; + } - // Address events - if (msg.eventCode == Constants.EVENT_ADDR_ADDED_IP4) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_ADDR_ADDED_IP4"); - } - if (msg.eventCode == Constants.EVENT_ADDR_ADDED_IP6) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_ADDR_ADDED_IP6"); - } - if (msg.eventCode == Constants.EVENT_ADDR_REMOVED_IP4) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_ADDR_REMOVED_IP4"); - } - if (msg.eventCode == Constants.EVENT_ADDR_REMOVED_IP6) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_ADDR_REMOVED_IP6"); - } - // peer events - if (msg.eventCode == Constants.EVENT_PEER_DIRECT) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_PEER_DIRECT"); - } - if (msg.eventCode == Constants.EVENT_PEER_RELAY) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_PEER_RELAY"); - } - // newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_PEER_UNREACHABLE"); - if (msg.eventCode == Constants.EVENT_PEER_PATH_DISCOVERED) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_PEER_PATH_DISCOVERED"); - } - if (msg.eventCode == Constants.EVENT_PEER_PATH_DEAD) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_PEER_PATH_DEAD"); - } + public int InitSetEventHandler(ZeroTierManagedEventCallback managedCallback) + { + if (managedCallback == null) { + throw new ArgumentNullException("managedCallback"); + } + int res = Constants.ERR_OK; + if ((res = zts_init_set_event_handler(OnZeroTierEvent)) == Constants.ERR_OK) { + _unmanagedCallback = OnZeroTierEvent; + _managedCallback = new ZeroTierManagedEventCallback(managedCallback); + } + return res; + } - // Route events - if (msg.eventCode == Constants.EVENT_ROUTE_ADDED) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_ROUTE_ADDED"); - } - if (msg.eventCode == Constants.EVENT_ROUTE_REMOVED) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_ROUTE_REMOVED"); - } + public int InitSetPort(UInt16 port) + { + int res = Constants.ERR_OK; + if ((res = zts_init_set_port(port)) == Constants.ERR_OK) { + _servicePort = port; + } + return res; + } - // Netif events - if (msg.eventCode == Constants.EVENT_NETIF_UP) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETIF_UP"); - } - if (msg.eventCode == Constants.EVENT_NETIF_DOWN) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETIF_DOWN"); - } - if (msg.eventCode == Constants.EVENT_NETIF_REMOVED) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETIF_REMOVED"); - } - if (msg.eventCode == Constants.EVENT_NETIF_LINK_UP) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETIF_LINK_UP"); - } - if (msg.eventCode == Constants.EVENT_NETIF_LINK_DOWN) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETIF_LINK_DOWN"); - } - if (msg.eventCode == Constants.EVENT_ADDR_REMOVED_IP6) { - newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_ADDR_REMOVED_IP6"); - } + public int InitSetRoots(byte[] roots_data, int len) + { + IntPtr unmanagedPointer = Marshal.AllocHGlobal(roots_data.Length); + Marshal.Copy(roots_data, 0, unmanagedPointer, roots_data.Length); + int res = zts_init_set_roots(unmanagedPointer, len); + Marshal.FreeHGlobal(unmanagedPointer); + return res; + } - // Pass the converted Event to the managed callback (visible to user) - if (newEvent != null) { - _managedCallback(newEvent); - } - } + public int InitAllowNetworkCaching(bool allowed) + { + return zts_init_allow_net_cache(Convert.ToByte(allowed)); + } - /// - /// Creates a new Node. Start this object by calling Start(). - /// - /// Where keys and config files will be stored on the filesystem - /// Where you would like to receive ZeroTier event notifications - /// The port ZeroTier will use to send its encrypted - /// - public Node(string configFilePath, ZeroTierManagedEventCallback managedCallback, UInt16 servicePort) - { - if (String.IsNullOrEmpty(configFilePath)) { - throw new ArgumentNullException("configFilePath"); - } - if (managedCallback == null) { - throw new ArgumentNullException("managedCallback"); - } - _nodeId = 0x0; - _configFilePath = configFilePath; - _servicePort = servicePort; - _managedCallback = new ZeroTierManagedEventCallback(managedCallback); - } + public int InitAllowPeerCaching(bool allowed) + { + return zts_init_allow_peer_cache(Convert.ToByte(allowed)); + } - /// - /// Starts the ZeroTier node/service - /// - /// - public int Start() - { - if (_hasBeenFreed == true) { - throw new ObjectDisposedException("ZeroTier Node has previously been freed. Restart application to create new instance."); - } - _unmanagedCallback = OnZeroTierEvent; - return zts_start(_configFilePath,_unmanagedCallback,_servicePort); - } + void OnZeroTierEvent(IntPtr msgPtr) + { + zts_event_msg_t msg = (zts_event_msg_t)Marshal.PtrToStructure(msgPtr, typeof(zts_event_msg_t)); + ZeroTier.Core.Event newEvent = null; - /// - /// Frees (most) resources used by ZeroTier. ZeroTier may not be started again after this call. - /// - /// - public int Free() - { - _nodeId = 0x0; - _hasBeenFreed = true; - return zts_free(); - } + // Node - /// - /// Stop all ZeroTier service activity. The service may be started again with Start(). - /// - /// - public int Stop() - { - _nodeId = 0x0; - return zts_stop(); - } + if (msg.node != IntPtr.Zero) { + zts_node_info_t details = (zts_node_info_t)Marshal.PtrToStructure(msg.node, typeof(zts_node_info_t)); + newEvent = new ZeroTier.Core.Event(); + newEvent.Code = msg.event_code; + _id = details.node_id; + _primaryPort = details.primary_port; + _secondaryPort = details.secondary_port; + _tertiaryPort = details.tertiary_port; + _versionMajor = details.ver_major; + _versionMinor = details.ver_minor; + _versionRev = details.ver_rev; + _isOnline = Convert.ToBoolean(zts_node_is_online()); - /// - /// Restarts the ZeroTier service, internal stack and driver. (Mostly used for debugging.) - /// - /// - public int Restart() - { - _nodeId = 0x0; - return zts_restart(); - } + if (msg.event_code == Constants.EVENT_NODE_UP) { + newEvent.Name = "EVENT_NODE_UP"; + } + if (msg.event_code == Constants.EVENT_NODE_ONLINE) { + newEvent.Name = "EVENT_NODE_ONLINE"; + } + if (msg.event_code == Constants.EVENT_NODE_OFFLINE) { + newEvent.Name = "EVENT_NODE_OFFLINE"; + } + if (msg.event_code == Constants.EVENT_NODE_DOWN) { + newEvent.Name = "EVENT_NODE_DOWN"; + } + if (msg.event_code == Constants.ZTS_EVENT_NODE_FATAL_ERROR) { + newEvent.Name = "EVENT_NODE_FATAL_ERROR"; + } + } - /// - /// Requests to join a ZeroTier network. Remember to authorize your node/device. - /// - /// Network ID - /// - public int Join(ulong nwid) - { - return zts_join(nwid); - } + // Network - /// - /// Leaves a ZeroTier network. - /// - /// - /// - public int Leave(ulong nwid) - { - return zts_leave(nwid); - } + if (msg.network != IntPtr.Zero) { + zts_net_info_t net_info = (zts_net_info_t)Marshal.PtrToStructure(msg.network, typeof(zts_net_info_t)); + newEvent = new ZeroTier.Core.Event(); + newEvent.Code = msg.event_code; - /// - /// Returns whether the Node is online (able to reach the internet.) - /// - /// - public bool IsOnline() - { - return _isOnline; - } + // Update network info as long as we aren't tearing down the network + if (msg.event_code != Constants.EVENT_NETWORK_DOWN) { + ulong networkId = net_info.net_id; + NetworkInfo ni = _networks.GetOrAdd(networkId, new NetworkInfo()); - /// - /// Returns whether routing information is available from at least one ZeroTier network. - /// - /// - public bool HasRoutes() - { - return _joinedAtLeastOneNetwork; - } + newEvent.NetworkInfo = ni; + newEvent.NetworkInfo.Id = net_info.net_id; + newEvent.NetworkInfo.MACAddress = net_info.mac; + newEvent.NetworkInfo.Name = System.Text.Encoding.Default.GetString(net_info.name); + newEvent.NetworkInfo.Status = net_info.status; + newEvent.NetworkInfo.Type = net_info.type; + newEvent.NetworkInfo.MTU = net_info.mtu; + newEvent.NetworkInfo.DHCP = net_info.dhcp; + newEvent.NetworkInfo.Bridge = Convert.ToBoolean(net_info.bridge); + newEvent.NetworkInfo.BroadcastEnabled = Convert.ToBoolean(net_info.broadcast_enabled); + zts_core_lock_obtain(); - public bool HasIPv4Routes - { - get { return _has_ipv4_routes; } - } + // Get assigned addresses - public bool HasIPv6Routes - { - get { return _has_ipv6_routes; } - } + ConcurrentDictionary newAddrsDict = + new ConcurrentDictionary(); + IntPtr addrBuffer = Marshal.AllocHGlobal(ZeroTier.Constants.INET6_ADDRSTRLEN); + int addr_count = zts_core_query_addr_count(networkId); - public ulong NodeId - { - get { - return _nodeId; - } - } + for (int idx = 0; idx < addr_count; idx++) { + zts_core_query_addr(networkId, idx, addrBuffer, ZeroTier.Constants.INET6_ADDRSTRLEN); + // Convert buffer to managed string + string str = Marshal.PtrToStringAnsi(addrBuffer); + IPAddress addr = IPAddress.Parse(str); + newAddrsDict[addr.ToString()] = addr; + } - /* Structures and functions used internally to communicate with - lower-level C API defined in include/ZeroTierSockets.h */ + // Update addresses in NetworkInfo object - [DllImport("libzt", EntryPoint="CSharp_zts_start")] - static extern int zts_start(string arg1, CSharpCallbackWithStruct arg2, ushort arg3); + // TODO: This update block works but could use a re-think, I think. + // Step 1. Remove addresses not present in new concurrent dict. + if (! ni._addrs.IsEmpty) { + foreach (string key in ni._addrs.Keys) { + if (! newAddrsDict.Keys.Contains(key)) { + ni._addrs.TryRemove(key, out _); + } + } + } + else { + ni._addrs = newAddrsDict; + } + // Step 2. Add addresses not present in existing concurrent dict. + foreach (string key in newAddrsDict.Keys) { + if (! ni._addrs.Keys.Contains(key)) { + ni._addrs[key] = newAddrsDict[key]; + } + } - [DllImport("libzt", EntryPoint="CSharp_zts_stop")] - static extern int zts_stop(); + Marshal.FreeHGlobal(addrBuffer); + addrBuffer = IntPtr.Zero; - [DllImport("libzt", EntryPoint="CSharp_zts_restart")] - static extern int zts_restart(); + // Get managed routes - [DllImport("libzt", EntryPoint="CSharp_zts_free")] - static extern int zts_free(); + ConcurrentDictionary newRoutesDict = + new ConcurrentDictionary(); + IntPtr targetBuffer = Marshal.AllocHGlobal(ZeroTier.Constants.INET6_ADDRSTRLEN); + IntPtr viaBuffer = Marshal.AllocHGlobal(ZeroTier.Constants.INET6_ADDRSTRLEN); - [DllImport("libzt", EntryPoint="CSharp_zts_join")] - static extern int zts_join(ulong arg1); + int route_count = zts_core_query_route_count(networkId); - [DllImport("libzt", EntryPoint="CSharp_zts_leave")] - static extern int zts_leave(ulong arg1); + ushort flags = 0, metric = 0; - [DllImport("libzt", EntryPoint="CSharp_zts_allow_network_caching")] - static extern int zts_allow_network_caching(byte arg1); + for (int idx = 0; idx < route_count; idx++) { + zts_core_query_route( + networkId, + idx, + targetBuffer, + viaBuffer, + ZeroTier.Constants.INET6_ADDRSTRLEN, + ref flags, + ref metric); - [DllImport("libzt", EntryPoint="CSharp_zts_allow_peer_caching")] - static extern int zts_allow_peer_caching(byte arg1); + // Convert buffer to managed string - [DllImport("libzt", EntryPoint="CSharp_zts_allow_local_conf")] - static extern int zts_allow_local_conf(byte arg1); + try { + string targetStr = Marshal.PtrToStringAnsi(targetBuffer); + IPAddress targetAddr = IPAddress.Parse(targetStr); + string viaStr = Marshal.PtrToStringAnsi(viaBuffer); + IPAddress viaAddr = IPAddress.Parse(viaStr); + RouteInfo route = new RouteInfo(targetAddr, viaAddr, flags, metric); + // Add to NetworkInfo object + newRoutesDict[targetStr] = route; + } + catch { + Console.WriteLine("error while parsing route"); + } + } - [DllImport("libzt", EntryPoint="CSharp_zts_orbit")] - static extern int zts_orbit(ulong arg1, ulong arg2); + // TODO: This update block works but could use a re-think, I think. + // Step 1. Remove routes not present in new concurrent dict. + if (! ni._routes.IsEmpty) { + foreach (string key in ni._routes.Keys) { + if (! newRoutesDict.Keys.Contains(key)) { + ni._routes.TryRemove(key, out _); + } + } + } + else { + ni._routes = newRoutesDict; + } + // Step 2. Add routes not present in existing concurrent dict. + foreach (string key in newRoutesDict.Keys) { + if (! ni._routes.Keys.Contains(key)) { + ni._routes[key] = newRoutesDict[key]; + } + } - [DllImport("libzt", EntryPoint="CSharp_zts_deorbit")] - static extern int zts_deorbit(ulong arg1); + Marshal.FreeHGlobal(targetBuffer); + Marshal.FreeHGlobal(viaBuffer); + targetBuffer = IntPtr.Zero; + viaBuffer = IntPtr.Zero; - [DllImport("libzt", EntryPoint="CSharp_zts_get_6plane_addr")] - static extern int zts_get_6plane_addr(IntPtr arg1, ulong arg2, ulong arg3); + // Get multicast subscriptions - [DllImport("libzt", EntryPoint="CSharp_zts_get_rfc4193_addr")] - static extern int zts_get_rfc4193_addr(IntPtr arg1, ulong arg2, ulong arg3); + zts_core_lock_release(); - [DllImport("libzt", EntryPoint="CSharp_zts_generate_adhoc_nwid_from_range")] - static extern ulong zts_generate_adhoc_nwid_from_range(ushort arg1, ushort arg2); + // Update synthetic "readiness" value + ni.transportReady = (route_count > 0) && (addr_count > 0) ? true : false; + } // EVENT_NETWORK_DOWN - [DllImport("libzt", EntryPoint="CSharp_zts_delay_ms")] - static extern void zts_delay_ms(int arg1); + if (msg.event_code == Constants.EVENT_NETWORK_NOT_FOUND) { + newEvent.Name = "EVENT_NETWORK_NOT_FOUND " + net_info.net_id.ToString("x16"); + } + if (msg.event_code == Constants.EVENT_NETWORK_REQ_CONFIG) { + newEvent.Name = "EVENT_NETWORK_REQ_CONFIG " + net_info.net_id.ToString("x16"); + } + if (msg.event_code == Constants.EVENT_NETWORK_ACCESS_DENIED) { + newEvent.Name = "EVENT_NETWORK_ACCESS_DENIED " + net_info.net_id.ToString("x16"); + } + if (msg.event_code == Constants.EVENT_NETWORK_READY_IP4) { + newEvent.Name = "EVENT_NETWORK_READY_IP4 " + net_info.net_id.ToString("x16"); + } + if (msg.event_code == Constants.EVENT_NETWORK_READY_IP6) { + newEvent.Name = "EVENT_NETWORK_READY_IP6 " + net_info.net_id.ToString("x16"); + } + if (msg.event_code == Constants.EVENT_NETWORK_DOWN) { + newEvent.Name = "EVENT_NETWORK_DOWN " + net_info.net_id.ToString("x16"); + } + if (msg.event_code == Constants.EVENT_NETWORK_CLIENT_TOO_OLD) { + newEvent.Name = "EVENT_NETWORK_CLIENT_TOO_OLD " + net_info.net_id.ToString("x16"); + } + if (msg.event_code == Constants.EVENT_NETWORK_REQ_CONFIG) { + newEvent.Name = "EVENT_NETWORK_REQ_CONFIG " + net_info.net_id.ToString("x16"); + } + if (msg.event_code == Constants.EVENT_NETWORK_OK) { + newEvent.Name = "EVENT_NETWORK_OK " + net_info.net_id.ToString("x16"); + } + if (msg.event_code == Constants.EVENT_NETWORK_ACCESS_DENIED) { + newEvent.Name = "EVENT_NETWORK_ACCESS_DENIED " + net_info.net_id.ToString("x16"); + } + if (msg.event_code == Constants.EVENT_NETWORK_READY_IP4_IP6) { + newEvent.Name = "EVENT_NETWORK_READY_IP4_IP6 " + net_info.net_id.ToString("x16"); + } + if (msg.event_code == Constants.EVENT_NETWORK_UPDATE) { + newEvent.Name = "EVENT_NETWORK_UPDATE " + net_info.net_id.ToString("x16"); + } + } - [DllImport("libzt", EntryPoint="CSharp_zts_errno_get")] - static extern int zts_errno_get(); + // Route - [StructLayout(LayoutKind.Sequential)] - struct zts_node_details - { - public ulong address; - } + if (msg.route != IntPtr.Zero) { + zts_route_info_t route_info = + (zts_route_info_t)Marshal.PtrToStructure(msg.route, typeof(zts_route_info_t)); + newEvent = new ZeroTier.Core.Event(); + newEvent.Code = msg.event_code; + newEvent.RouteInfo = default; // new RouteInfo(); - /** - * Virtual network configuration - */ - [StructLayout(LayoutKind.Sequential)] - struct zts_network_details - { - /** - * 64-bit ZeroTier network ID - */ - public ulong nwid; + if (msg.event_code == Constants.EVENT_ROUTE_ADDED) { + newEvent.Name = "EVENT_ROUTE_ADDED"; + } + if (msg.event_code == Constants.EVENT_ROUTE_REMOVED) { + newEvent.Name = "EVENT_ROUTE_REMOVED"; + } + } - /** - * Ethernet MAC (48 bits) that should be assigned to port - */ - public ulong mac; + // Peer - /** - * Network name (from network configuration master) - */ - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)] - public byte[] name; + if (msg.peer != IntPtr.Zero) { + zts_peer_info_t peer_info = (zts_peer_info_t)Marshal.PtrToStructure(msg.peer, typeof(zts_peer_info_t)); + newEvent = new ZeroTier.Core.Event(); + newEvent.Code = msg.event_code; + newEvent.PeerInfo = default; // new PeerInfo(); - /** - * Network configuration request status - */ - public byte status; // ? + if (peer_info.role == Constants.PEER_ROLE_PLANET) { + Console.WriteLine("ROOT"); + } + if (msg.event_code == Constants.EVENT_PEER_DIRECT) { + newEvent.Name = "EVENT_PEER_DIRECT"; + } + if (msg.event_code == Constants.EVENT_PEER_RELAY) { + newEvent.Name = "EVENT_PEER_RELAY"; + } + // newEvent = new ZeroTier.Core.Event(msg.event_code,"EVENT_PEER_UNREACHABLE"); + if (msg.event_code == Constants.EVENT_PEER_PATH_DISCOVERED) { + newEvent.Name = "EVENT_PEER_PATH_DISCOVERED"; + } + if (msg.event_code == Constants.EVENT_PEER_PATH_DEAD) { + newEvent.Name = "EVENT_PEER_PATH_DEAD"; + } + } - /** - * Network type - */ - public byte type; // ? + // Address - /** - * Maximum interface MTU - */ - public uint mtu; + if (msg.addr != IntPtr.Zero) { + zts_addr_info_t unmanagedDetails = + (zts_addr_info_t)Marshal.PtrToStructure(msg.addr, typeof(zts_addr_info_t)); + newEvent = new ZeroTier.Core.Event(); + newEvent.Code = msg.event_code; + newEvent.AddressInfo = default; // new AddressInfo(); - /** - * If nonzero, the network this port belongs to indicates DHCP availability - * - * This is a suggestion. The underlying implementation is free to ignore it - * for security or other reasons. This is simply a netconf parameter that - * means 'DHCP is available on this network.' - */ - public int dhcp; + if (msg.event_code == Constants.EVENT_ADDR_ADDED_IP4) { + newEvent.Name = "EVENT_ADDR_ADDED_IP4"; + } + if (msg.event_code == Constants.EVENT_ADDR_ADDED_IP6) { + newEvent.Name = "EVENT_ADDR_ADDED_IP6"; + } + if (msg.event_code == Constants.EVENT_ADDR_REMOVED_IP4) { + newEvent.Name = "EVENT_ADDR_REMOVED_IP4"; + } + if (msg.event_code == Constants.EVENT_ADDR_REMOVED_IP6) { + newEvent.Name = "EVENT_ADDR_REMOVED_IP6"; + } + } - /** - * If nonzero, this port is allowed to bridge to other networks - * - * This is informational. If this is false (0), bridged packets will simply - * be dropped and bridging won't work. - */ - public int bridge; + // Storage - /** - * If nonzero, this network supports and allows broadcast (ff:ff:ff:ff:ff:ff) traffic - */ - public int broadcastEnabled; + if (msg.cache != IntPtr.Zero) { + newEvent = new ZeroTier.Core.Event(); + newEvent.Code = msg.event_code; + newEvent.AddressInfo = default; // new AddressInfo(); - /** - * If the network is in PORT_ERROR state, this is the (negative) error code most recently reported - */ - public int portError; + if (msg.event_code == Constants.EVENT_STORE_IDENTITY_SECRET) { + newEvent.Name = "EVENT_STORE_IDENTITY_SECRET"; + } + if (msg.event_code == Constants.EVENT_STORE_IDENTITY_PUBLIC) { + newEvent.Name = "EVENT_STORE_IDENTITY_PUBLIC"; + } + if (msg.event_code == Constants.EVENT_STORE_PLANET) { + newEvent.Name = "EVENT_STORE_PLANET"; + } + if (msg.event_code == Constants.EVENT_STORE_PEER) { + newEvent.Name = "EVENT_STORE_PEER"; + } + if (msg.event_code == Constants.EVENT_STORE_NETWORK) { + newEvent.Name = "EVENT_STORE_NETWORK"; + } + } - /** - * Revision number as reported by controller or 0 if still waiting for config - */ - public ulong netconfRevision; + // Pass the converted Event to the managed callback (visible to user) + if (newEvent != null) { + _managedCallback(newEvent); + } + } - /** - * Number of assigned addresses - */ - public uint assignedAddressCount; + public List Networks + { + get { + return new List(_networks.Values); + } + } - /** - * ZeroTier-assigned addresses (in sockaddr_storage structures) - * - * For IP, the port number of the sockaddr_XX structure contains the number - * of bits in the address netmask. Only the IP address and port are used. - * Other fields like interface number can be ignored. - * - * This is only used for ZeroTier-managed address assignments sent by the - * virtual network's configuration master. - */ - //struct zts_sockaddr_storage assignedAddresses[ZTS_MAX_ZT_ASSIGNED_ADDRESSES]; + public List Peers + { + get { + return new List(_peers.Values); + } + } - /** - * Number of ZT-pushed routes - */ - public uint routeCount; + public bool IsNetworkTransportReady(ulong networkId) + { + try { + return _networks[networkId].transportReady + && (_networks[networkId].Status == Constants.NETWORK_STATUS_OK); + } + catch (KeyNotFoundException) { + return false; + } + } - /** - * Routes (excluding those implied by assigned addresses and their masks) - */ - //ZTS_VirtualNetworkRoute routes[ZTS_MAX_NETWORK_ROUTES]; + public List GetNetworkAddresses(ulong networkId) + { + try { + return new List(_networks[networkId].Addresses); + } + catch (KeyNotFoundException) { + } + return new List(); + } - /** - * Number of multicast groups subscribed - */ - public uint multicastSubscriptionCount; + public List GetNetworkRoutes(ulong networkId) + { + try { + return new List(_networks[networkId].Routes); + } + catch (KeyNotFoundException) { + } + return new List(); + } - /** - * Multicast groups to which this network's device is subscribed - */ - //struct { - // uint64_t mac; /* MAC in lower 48 bits */ - // uint32_t adi; /* Additional distinguishing information, usually zero except for IPv4 ARP groups */ - //} multicastSubscriptions[ZTS_MAX_MULTICAST_SUBSCRIPTIONS]; - } + /* + public NetworkInfo GetNetwork(ulong networkId) + { + try { + return _networks[networkId]; + } catch (KeyNotFoundException) { - [StructLayout(LayoutKind.Sequential)] - struct zts_callback_msg - { - public short eventCode; - //[MarshalAs(UnmanagedType.LPStruct, SizeConst = 4)] - public IntPtr node; - public IntPtr network; - } + } + return null; + } + */ + public int Start() + { + if (_hasBeenFreed == true) { + throw new ObjectDisposedException( + "ZeroTier Node has previously been freed. Restart application to create new instance."); + } + return zts_node_start(); + } - /// - /// Gets the value of errno from the unmanaged region - /// - /// - public static int ErrNo { - get { - return zts_errno_get(); - } - } - } + public int Free() + { + _id = 0x0; + _hasBeenFreed = true; + ClearNode(); + return zts_node_free(); + } + + public int Stop() + { + _id = 0x0; + ClearNode(); + return zts_node_stop(); + } + + public int Join(ulong networkId) + { + /* The NetworkInfo object will only be added to _networks and populated when a + response from the controller is received */ + return zts_net_join(networkId); + } + + public int Leave(ulong networkId) + { + int res = Constants.ERR_OK; + if (zts_net_leave(networkId) == Constants.ERR_OK) { + _networks.TryRemove(networkId, out _); + } + return res; + } + + public bool Online + { + get { + return _isOnline; + } + } + + public ulong Id + { + get { + return _id; + } + } + + public string IdString + { + get { + return _id.ToString("x16").TrimStart('0'); + } + } + + public string IdStr + { + get { + return string.Format("{0}", _id.ToString("x16")); + } + } + + public ushort PrimaryPort + { + get { + return _primaryPort; + } + } + + public ushort SecondaryPort + { + get { + return _secondaryPort; + } + } + + public ushort TertiaryPort + { + get { + return _tertiaryPort; + } + } + + public string Version + { + get { + return string.Format("{0}.{1}.{2}", _versionMajor, _versionMinor, _versionRev); + } + } + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_id_new")] static extern int + zts_id_new(string arg1, global::System.Runtime.InteropServices.HandleRef arg2); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_id_pair_is_valid")] + static extern int zts_id_pair_is_valid(string arg1, int arg2); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_init_from_storage")] + static extern int zts_init_from_storage(string arg1); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_init_from_memory")] + static extern int zts_init_from_memory(string arg1, ushort arg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_init_set_event_handler")] + static extern int zts_init_set_event_handler(CSharpCallbackWithStruct arg1); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_init_blacklist_if")] + static extern int zts_init_blacklist_if(string arg1, int arg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_init_set_roots")] + static extern int zts_init_set_roots(IntPtr roots_data, int len); + + [DllImport("libzt", EntryPoint = "CSharp_zts_init_set_port")] + static extern int zts_init_set_port(ushort arg1); + + // Core query API + + [DllImport("libzt", EntryPoint = "CSharp_zts_core_lock_obtain")] + static extern int zts_core_lock_obtain(); + + [DllImport("libzt", EntryPoint = "CSharp_zts_core_lock_release")] + static extern int zts_core_lock_release(); + + [DllImport("libzt", EntryPoint = "CSharp_zts_core_query_addr_count")] + static extern int zts_core_query_addr_count(ulong net_id); + + [DllImport("libzt", EntryPoint = "CSharp_zts_core_query_addr")] + static extern int zts_core_query_addr(ulong net_id, int idx, IntPtr dst, int len); + + [DllImport("libzt", EntryPoint = "CSharp_zts_core_query_route_count")] + static extern int zts_core_query_route_count(ulong net_id); + + [DllImport("libzt", EntryPoint = "CSharp_zts_core_query_route")] + static extern int zts_core_query_route( + ulong net_id, + int idx, + IntPtr target, + IntPtr via, + int len, + ref ushort flags, + ref ushort metric); + + [DllImport("libzt", EntryPoint = "CSharp_zts_core_query_path_count")] + static extern int zts_core_query_path_count(ulong peer_id); + + [DllImport("libzt", EntryPoint = "CSharp_zts_core_query_path")] + static extern int zts_core_query_path(ulong peer_id, int idx, IntPtr dst, int len); + + [DllImport("libzt", EntryPoint = "CSharp_zts_core_query_mc_count")] + static extern int zts_core_query_mc_count(ulong net_id); + + [DllImport("libzt", EntryPoint = "CSharp_zts_core_query_mc")] + static extern int zts_core_query_mc(ulong net_id, int idx, ref ulong mac, ref uint adi); + + // init + + [DllImport("libzt", EntryPoint = "CSharp_zts_init_allow_net_cache")] + static extern int zts_init_allow_net_cache(int arg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_init_allow_peer_cache")] + static extern int zts_init_allow_peer_cache(int arg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_init_clear")] + static extern int zts_init_clear(); + + // addr + + [DllImport("libzt", EntryPoint = "CSharp_zts_addr_is_assigned")] + static extern int zts_addr_is_assigned(ulong arg1, int arg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_addr_get")] + static extern int zts_addr_get(ulong arg1, int arg2, global::System.Runtime.InteropServices.HandleRef arg3); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_addr_get_str")] + static extern int zts_addr_get_str(ulong arg1, int arg2, IntPtr arg3, int arg4); + + [DllImport("libzt", EntryPoint = "CSharp_zts_addr_get_all")] + static extern int zts_addr_get_all( + ulong arg1, + global::System.Runtime.InteropServices.HandleRef arg2, + global::System.Runtime.InteropServices.HandleRef arg3); + + [DllImport("libzt", EntryPoint = "CSharp_zts_addr_compute_6plane")] + static extern int + zts_addr_compute_6plane(ulong arg1, ulong arg2, global::System.Runtime.InteropServices.HandleRef arg3); + + [DllImport("libzt", EntryPoint = "CSharp_zts_addr_compute_rfc4193")] + static extern int + zts_addr_compute_rfc4193(ulong arg1, ulong arg2, global::System.Runtime.InteropServices.HandleRef arg3); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_addr_compute_rfc4193_str")] + static extern int zts_addr_compute_rfc4193_str(ulong arg1, ulong arg2, string arg3, int arg4); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_addr_compute_6plane_str")] + static extern int zts_addr_compute_6plane_str(ulong arg1, ulong arg2, string arg3, int arg4); + + /* + [DllImport("libzt", EntryPoint="CSharp_zts_get_6plane_addr")] + static extern int zts_get_6plane_addr(IntPtr arg1, ulong arg2, ulong arg3); + + [DllImport("libzt", EntryPoint="CSharp_zts_get_rfc4193_addr")] + static extern int zts_get_rfc4193_addr(IntPtr arg1, ulong arg2, ulong arg3); + */ + + // net + + [DllImport("libzt", EntryPoint = "CSharp_zts_net_compute_adhoc_id")] + public static extern ulong zts_net_compute_adhoc_id(ushort arg1, ushort arg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_net_join")] + static extern int zts_net_join(ulong arg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_net_leave")] + static extern int zts_net_leave(ulong arg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_net_transport_is_ready")] + static extern int zts_net_transport_is_ready(ulong arg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_net_get_mac")] + public static extern ulong zts_net_get_mac(ulong arg1); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_net_get_mac_str")] + static extern int zts_net_get_mac_str(ulong arg1, string arg2, int arg3); + + [DllImport("libzt", EntryPoint = "CSharp_zts_net_get_broadcast")] + static extern int zts_net_get_broadcast(ulong arg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_net_get_mtu")] + static extern int zts_net_get_mtu(ulong arg1); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_net_get_name")] + static extern int zts_net_get_name(ulong arg1, string arg2, int arg3); + + [DllImport("libzt", EntryPoint = "CSharp_zts_net_get_status")] + static extern int zts_net_get_status(ulong arg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_net_get_type")] + static extern int zts_net_get_type(ulong arg1); + + // route + + [DllImport("libzt", EntryPoint = "CSharp_zts_route_is_assigned")] + static extern int zts_route_is_assigned(ulong arg1, int arg2); + + // node + + [DllImport("libzt", EntryPoint = "CSharp_zts_node_start")] + static extern int zts_node_start(); + + [DllImport("libzt", EntryPoint = "CSharp_zts_node_is_online")] + static extern int zts_node_is_online(); + + [DllImport("libzt", EntryPoint = "CSharp_zts_node_get_id")] + public static extern ulong zts_node_get_id(); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_node_get_id_pair")] + static extern int zts_node_get_id_pair(string arg1, global::System.Runtime.InteropServices.HandleRef arg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_node_get_port")] + static extern int zts_node_get_port(); + + [DllImport("libzt", EntryPoint = "CSharp_zts_node_stop")] + static extern int zts_node_stop(); + + [DllImport("libzt", EntryPoint = "CSharp_zts_node_free")] + static extern int zts_node_free(); + + // moon + + [DllImport("libzt", EntryPoint = "CSharp_zts_moon_orbit")] + static extern int zts_moon_orbit(ulong arg1, ulong arg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_moon_deorbit")] + static extern int zts_moon_deorbit(ulong arg1); + + // util + + [DllImport("libzt", EntryPoint = "CSharp_zts_util_delay")] + static extern void zts_util_delay(int arg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_errno_get")] + static extern int zts_errno_get(); + + [StructLayout(LayoutKind.Sequential)] + struct zts_node_info_t { + public ulong node_id; + public ushort primary_port; + public ushort secondary_port; + public ushort tertiary_port; + public byte ver_major; + public byte ver_minor; + public byte ver_rev; + } + + [StructLayout(LayoutKind.Sequential)] + struct zts_net_info_t { + public ulong net_id; + public ulong mac; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)] + public byte[] name; + public int status; + public int type; + public uint mtu; + public int dhcp; + public int bridge; + public int broadcast_enabled; + public int port_error; + public ulong netconf_rev; + // address, routes, and multicast subs are retrieved using zts_core_ API + } + + [StructLayout(LayoutKind.Sequential)] + struct zts_route_info_t { + IntPtr target; + IntPtr via; + ushort flags; + ushort metric; + } + + [StructLayout(LayoutKind.Sequential)] + struct zts_addr_info_t { + ulong nwid; + IntPtr addr; + } + + [StructLayout(LayoutKind.Sequential)] + struct zts_peer_info_t { + public ulong address; + public int ver_major; + public int ver_minor; + public int ver_rev; + public int latency; + public byte role; + // TODO + } + + [StructLayout(LayoutKind.Sequential)] + struct zts_event_msg_t { + public short event_code; + public IntPtr node; + public IntPtr network; + public IntPtr netif; + public IntPtr route; + public IntPtr peer; + public IntPtr addr; + public IntPtr cache; + public int len; + } + + public static int ErrNo + { + get { + return zts_errno_get(); + } + } + } } diff --git a/src/bindings/csharp/NodeInfo.cs b/src/bindings/csharp/NodeInfo.cs new file mode 100755 index 0000000..d08ca5c --- /dev/null +++ b/src/bindings/csharp/NodeInfo.cs @@ -0,0 +1,46 @@ +/* + * Copyright (c)2013-2021 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2026-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +using System.Net; + +using ZeroTier; + +namespace ZeroTier.Core +{ + /* Convenience structures for exposing lower level operational details to the user. + These structures do not have the same memory layout or content as those found in + include/ZeroTierSockets.h */ + + /// + /// Structure containing information about the local Node. + /// + public class NodeInfo { + // Node ID + public ulong Id { get; set; } + + /** + * The port used by the service to send and receive + * all encapsulated traffic + */ + public ushort PrimaryPort { get; set; } + public ushort SecondaryPort { get; set; } + public ushort TertiaryPort { get; set; } + + /** + * ZT version + */ + public byte VersionMajor { get; set; } + public byte VersionMinor { get; set; } + public byte VersionRev { get; set; } + } +} diff --git a/src/bindings/csharp/PeerInfo.cs b/src/bindings/csharp/PeerInfo.cs new file mode 100755 index 0000000..d7f5d7a --- /dev/null +++ b/src/bindings/csharp/PeerInfo.cs @@ -0,0 +1,68 @@ +/* + * Copyright (c)2013-2021 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2026-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +using System.Net; +using System.Collections.Concurrent; +using System.Collections.Generic; + +using ZeroTier; + +namespace ZeroTier.Core +{ + public class PeerInfo { + public PeerInfo(ulong id, short versionMajor, short versionMinor, short versionRev, byte role) + { + _id = id; + _versionMajor = versionMajor; + _versionMinor = versionMinor; + _versionRev = versionRev; + _role = role; + } + + ulong _id; + short _versionMajor; + short _versionMinor; + short _versionRev; + byte _role; + + internal ConcurrentDictionary _paths = new ConcurrentDictionary(); + + public ICollection Paths + { + get { + return _paths.Values; + } + } + + public ulong Id + { + get { + return _id; + } + } + + public byte Role + { + get { + return _role; + } + } + + public string Version + { + get { + return string.Format("{0}.{1}.{2}", _versionMajor, _versionMinor, _versionRev); + } + } + } +} diff --git a/src/bindings/csharp/README.md b/src/bindings/csharp/README.md old mode 100644 new mode 100755 diff --git a/src/bindings/csharp/RouteInfo.cs b/src/bindings/csharp/RouteInfo.cs new file mode 100755 index 0000000..41ae22d --- /dev/null +++ b/src/bindings/csharp/RouteInfo.cs @@ -0,0 +1,59 @@ +/* + * Copyright (c)2013-2021 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2026-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +using System.Net; + +using ZeroTier; + +namespace ZeroTier.Core +{ + public class RouteInfo { + public RouteInfo(IPAddress target, IPAddress via, ushort flags, ushort metric) + { + _target = target; + _via = via; + _flags = flags; + _metric = metric; + } + + public IPAddress _target; + public IPAddress _via; + public ushort _flags; + public ushort _metric; + + public IPAddress Target + { + get { + return _target; + } + } + public IPAddress Via + { + get { + return _via; + } + } + public ushort Flags + { + get { + return _flags; + } + } + public ushort Metric + { + get { + return _metric; + } + } + } +} diff --git a/src/bindings/csharp/Socket.cs b/src/bindings/csharp/Socket.cs old mode 100644 new mode 100755 index 1a4739a..3663515 --- a/src/bindings/csharp/Socket.cs +++ b/src/bindings/csharp/Socket.cs @@ -4,659 +4,809 @@ * Use of this software is governed by the Business Source License included * in the LICENSE.TXT file in the project's root directory. * - * Change Date: 2025-01-01 + * Change Date: 2026-01-01 * * On the date above, in accordance with the Business Source License, use * of this software will be governed by version 2.0 of the Apache License. */ /****/ -using System; // For ObjectDisposedException -using System.Net; // For IPEndPoint -using System.Net.Sockets; // For ZeroTier.Sockets.SocketException +using System; +using System.Net; +using System.Net.Sockets; using System.Runtime.InteropServices; using ZeroTier; -/// -/// ZeroTier SDK -/// namespace ZeroTier.Sockets { - /// - /// ZeroTier Socket - An lwIP socket mediated over a ZeroTier virtual link - /// - public class Socket - { - /// No error. - public static readonly int ZTS_ERR_OK = 0; - /// Socket error, see Socket.ErrNo() for additional context. - public static readonly int ZTS_ERR_SOCKET = -1; - /// You probably did something at the wrong time. - public static readonly int ZTS_ERR_SERVICE = -2; - /// Invalid argument. - public static readonly int ZTS_ERR_ARG = -3; - /// No result. (not necessarily an error.) - public static readonly int ZTS_ERR_NO_RESULT = -4; - /// Consider filing a bug report. - public static readonly int ZTS_ERR_GENERAL = -5; - - int _fd; - bool _isClosed; - bool _isListening; - bool _isBlocking; - bool _isBound; - bool _isConnected; - - int _connectTimeout = 30000; - - AddressFamily _socketFamily; - SocketType _socketType; - ProtocolType _socketProtocol; - - internal EndPoint _localEndPoint; - internal EndPoint _remoteEndPoint; - - private void InitializeInternalFlags() - { - _isClosed = false; - _isListening = false; - _isBlocking = false; - } - - public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) - { - int family = -1; - int type = -1; - int protocol = -1; - // Map .NET socket parameters to ZeroTier equivalents - switch (addressFamily) - { - case AddressFamily.InterNetwork: - family = Constants.AF_INET; - break; - case AddressFamily.InterNetworkV6: - family = Constants.AF_INET6; - break; - case AddressFamily.Unknown: - family = Constants.AF_UNSPEC; - break; - } - switch (socketType) - { - case SocketType.Stream: - type = Constants.SOCK_STREAM; - break; - case SocketType.Dgram: - type = Constants.SOCK_DGRAM; - break; - } - switch (protocolType) - { - case ProtocolType.Udp: - protocol = Constants.IPPROTO_UDP; - break; - case ProtocolType.Tcp: - protocol = Constants.IPPROTO_TCP; - break; - case ProtocolType.Unspecified: - protocol = 0; // ? - break; - } - if ((_fd = zts_socket(family, type, protocol)) < 0) - { - throw new ZeroTier.Sockets.SocketException((int)_fd); - } - _socketFamily = addressFamily; - _socketType = socketType; - _socketProtocol = protocolType; - InitializeInternalFlags(); - } - - private Socket(int fileDescriptor, - AddressFamily addressFamily, - SocketType socketType, - ProtocolType protocolType, - EndPoint localEndPoint, - EndPoint remoteEndPoint) - { - _socketFamily = addressFamily; - _socketType = socketType; - _socketProtocol = protocolType; - _localEndPoint = localEndPoint; - _remoteEndPoint = remoteEndPoint; - _fd = fileDescriptor; - InitializeInternalFlags(); - } - - public void Connect(IPEndPoint remoteEndPoint) - { - if (_isClosed) { - throw new ObjectDisposedException("Socket has been closed"); - } - if (_fd < 0) { - // Invalid file descriptor - throw new ZeroTier.Sockets.SocketException((int)Constants.ERR_SOCKET); - } - if (remoteEndPoint == null) { - throw new ArgumentNullException("remoteEndPoint"); - } - int err = Constants.ERR_OK; - if (remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) { - err = zts_connect_easy( - _fd, - Constants.AF_INET, - remoteEndPoint.Address.ToString(), - (ushort)remoteEndPoint.Port, - _connectTimeout); - } - if (remoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6) { - Console.WriteLine("going to connect to: " + remoteEndPoint.ToString()); - err = zts_connect_easy( - _fd, - Constants.AF_INET6, - remoteEndPoint.Address.ToString(), - (ushort)remoteEndPoint.Port, - _connectTimeout); - } - if (err < 0) { - throw new ZeroTier.Sockets.SocketException(err, ZeroTier.Core.Node.ErrNo); - } - _remoteEndPoint = remoteEndPoint; - _isConnected = true; - } - - public void Bind(IPEndPoint localEndPoint) - { - if (_isClosed) { - throw new ObjectDisposedException("Socket has been closed"); - } - if (_fd < 0) { - // Invalid file descriptor - throw new ZeroTier.Sockets.SocketException((int)Constants.ERR_SOCKET); - } - if (localEndPoint == null) { - throw new ArgumentNullException("localEndPoint"); - } - int err = Constants.ERR_OK; - if (localEndPoint.AddressFamily == AddressFamily.InterNetwork) { - err = zts_bind_easy( - _fd, - Constants.AF_INET, - "0.0.0.0", - (ushort)localEndPoint.Port); - } - if (localEndPoint.AddressFamily == AddressFamily.InterNetworkV6) { - // Todo: detect IPAddress.IPv6Any - err = zts_bind_easy( - _fd, - Constants.AF_INET6, - "::", - (ushort)localEndPoint.Port); - } - if (err < 0) { - throw new ZeroTier.Sockets.SocketException((int)err); - } - _localEndPoint = localEndPoint; - _isBound = true; - } - - public void Listen(int backlog) - { - if (_isClosed) { - throw new ObjectDisposedException("Socket has been closed"); - } - if (_fd < 0) { - // Invalid file descriptor - throw new ZeroTier.Sockets.SocketException((int)Constants.ERR_SOCKET); - } - int err = Constants.ERR_OK; - if ((err = zts_listen(_fd, backlog)) < 0) { - // Invalid backlog value perhaps? - throw new ZeroTier.Sockets.SocketException((int)Constants.ERR_SOCKET); - } - _isListening = true; - } - - public Socket Accept() - { - if (_isClosed) { - throw new ObjectDisposedException("Socket has been closed"); - } - if (_fd < 0) { - // Invalid file descriptor - throw new ZeroTier.Sockets.SocketException((int)Constants.ERR_SOCKET); - } - if (_isListening == false) { - throw new InvalidOperationException( - "Socket is not in a listening state. Call Listen() first"); - } - IntPtr lpBuffer = Marshal.AllocHGlobal(ZeroTier.Constants.INET6_ADDRSTRLEN); - int port = 0; - int accepted_fd = zts_accept_easy( - _fd, lpBuffer, ZeroTier.Constants.INET6_ADDRSTRLEN, ref port); - // Convert buffer to managed string - string str = Marshal.PtrToStringAnsi(lpBuffer); - Marshal.FreeHGlobal(lpBuffer); - lpBuffer = IntPtr.Zero; - IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Parse(str), port); - Console.WriteLine("clientEndPoint = " + clientEndPoint.ToString()); - // Create new socket by providing file descriptor returned from zts_accept call. - Socket clientSocket = new Socket( - accepted_fd, _socketFamily, _socketType, _socketProtocol, _localEndPoint, clientEndPoint); - return clientSocket; - } - - public void Shutdown(SocketShutdown how) - { - if (_isClosed) { - throw new ObjectDisposedException("Socket has been closed"); - } - int ztHow = 0; - switch (how) - { - case SocketShutdown.Receive: - ztHow = Constants.O_RDONLY; - break; - case SocketShutdown.Send: - ztHow = Constants.O_WRONLY; - break; - case SocketShutdown.Both: - ztHow = Constants.O_RDWR; - break; - } - zts_shutdown(_fd, ztHow); - } - - public void Close() - { - if (_isClosed) { - throw new ObjectDisposedException("Socket has already been closed"); - } - zts_close(_fd); - _isClosed = true; - } - - public bool Blocking - { - get { return Convert.ToBoolean(zts_get_blocking(_fd)); } - set { zts_set_blocking(_fd, Convert.ToInt32(value)); } - } - - public bool Poll(int microSeconds, System.Net.Sockets.SelectMode mode) - { - if (_isClosed) { - throw new ObjectDisposedException("Socket has been closed"); - } - zts_pollfd poll_set = new zts_pollfd(); - poll_set.fd = _fd; - if (mode == SelectMode.SelectRead) { - poll_set.events = (short)((byte)ZeroTier.Constants.POLLIN); - } - if (mode == SelectMode.SelectWrite) { - poll_set.events = (short)((byte)ZeroTier.Constants.POLLOUT); - } - if (mode == SelectMode.SelectError) { - poll_set.events = (short)((byte)ZeroTier.Constants.POLLERR | - (byte)ZeroTier.Constants.POLLNVAL); - } - IntPtr poll_fd_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(zts_pollfd))); - Marshal.StructureToPtr(poll_set, poll_fd_ptr, false); - int result = 0; - int timeout_ms = (microSeconds / 1000); - uint numfds = 1; - if ((result = zts_poll(poll_fd_ptr, numfds, timeout_ms)) < 0) { - throw new ZeroTier.Sockets.SocketException(result, ZeroTier.Core.Node.ErrNo); - } - poll_set = (zts_pollfd)Marshal.PtrToStructure(poll_fd_ptr, typeof(zts_pollfd)); - if (result != 0) { - if (mode == SelectMode.SelectRead) { - result = Convert.ToInt32(((byte)poll_set.revents - & (byte)ZeroTier.Constants.POLLIN) != 0); - } - if (mode == SelectMode.SelectWrite) { - result = Convert.ToInt32(((byte)poll_set.revents - & (byte)ZeroTier.Constants.POLLOUT) != 0); - } - if (mode == SelectMode.SelectError) { - result = Convert.ToInt32(((poll_set.revents - & (byte)ZeroTier.Constants.POLLERR) != 0) || - ((poll_set.revents & (byte)ZeroTier.Constants.POLLNVAL) != 0)); - } - } - Marshal.FreeHGlobal(poll_fd_ptr); - return result > 0; - } - - public Int32 Send(Byte[] buffer) - { - if (_isClosed) { - throw new ObjectDisposedException("Socket has been closed"); - } - if (_fd < 0) { - throw new ZeroTier.Sockets.SocketException((int)ZeroTier.Constants.ERR_SOCKET); - } - if (buffer == null) { - throw new ArgumentNullException("buffer"); - } - int flags = 0; - IntPtr bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); - return zts_send(_fd, bufferPtr, (uint)Buffer.ByteLength(buffer), (int)flags); - } - - public Int32 Receive(Byte[] buffer) - { - if (_isClosed) { - throw new ObjectDisposedException("Socket has been closed"); - } - if (_fd < 0) { - throw new ZeroTier.Sockets.SocketException((int)ZeroTier.Constants.ERR_SOCKET); - } - if (buffer == null) { - throw new ArgumentNullException("buffer"); - } - int flags = 0; - IntPtr bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); - return zts_recv(_fd, bufferPtr, (uint)Buffer.ByteLength(buffer), (int)flags); - } - - public int ReceiveTimeout - { - get { return zts_get_recv_timeout(_fd); } - // TODO: microseconds - set { zts_set_recv_timeout(_fd, value, 0); } - } - - public int SendTimeout - { - get { return zts_get_send_timeout(_fd); } - // TODO: microseconds - set { zts_set_send_timeout(_fd, value, 0); } - } - - public int ConnectTimeout - { - get { return _connectTimeout; } - set { _connectTimeout = value;} - } - - public int ReceiveBufferSize - { - get { return zts_get_recv_buf_size(_fd); } - set { zts_set_recv_buf_size(_fd, value); } - } - - public int SendBufferSize - { - get { return zts_get_send_buf_size(_fd); } - set { zts_set_send_buf_size(_fd, value); } - } - - public short Ttl - { - get { return Convert.ToInt16(zts_get_ttl(_fd)); } - set { zts_set_ttl(_fd, value); } - } - - public LingerOption LingerState - { - get { - LingerOption lo = new LingerOption( - Convert.ToBoolean(zts_get_linger_enabled(_fd)), zts_get_linger_value(_fd)); - return lo; - } - set { - zts_set_linger(_fd, Convert.ToInt32(value.Enabled), value.LingerTime); - } - } - - public bool NoDelay - { - get { return Convert.ToBoolean(zts_get_no_delay(_fd)); } - set { zts_set_no_delay(_fd, Convert.ToInt32(value)); } - } - - public bool KeepAlive - { - get { return Convert.ToBoolean(zts_get_keepalive(_fd)); } - set { zts_set_keepalive(_fd, Convert.ToInt32(value)); } - } - - public bool Connected { get { return _isConnected; } } - - public bool IsBound { get { return _isBound; } } - - public AddressFamily AddressFamily { get { return _socketFamily; } } - - public SocketType SocketType { get { return _socketType; } } - - public ProtocolType ProtocolType { get { return _socketProtocol; } } + public class Socket { + /// No error. + public static readonly int ZTS_ERR_OK = 0; + /// Socket error, see Socket.ErrNo() for additional context. + public static readonly int ZTS_ERR_SOCKET = -1; + /// You probably did something at the wrong time. + public static readonly int ZTS_ERR_SERVICE = -2; + /// Invalid argument. + public static readonly int ZTS_ERR_ARG = -3; + /// No result. (not necessarily an error.) + public static readonly int ZTS_ERR_NO_RESULT = -4; + /// Consider filing a bug report. + public static readonly int ZTS_ERR_GENERAL = -5; + + int _fd; + bool _isClosed; + bool _isListening; + bool _isBound; + bool _isConnected; + + int _connectTimeout = 30000; + + AddressFamily _socketFamily; + SocketType _socketType; + ProtocolType _socketProtocol; + + internal EndPoint _localEndPoint; + internal EndPoint _remoteEndPoint; + + private void InitializeInternalFlags() + { + _isClosed = false; + _isListening = false; + } + + public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType) + { + int family = -1; + int type = -1; + int protocol = -1; + // Map .NET socket parameters to ZeroTier equivalents + switch (addressFamily) { + case AddressFamily.InterNetwork: + family = Constants.AF_INET; + break; + case AddressFamily.InterNetworkV6: + family = Constants.AF_INET6; + break; + case AddressFamily.Unknown: + family = Constants.AF_UNSPEC; + break; + } + switch (socketType) { + case SocketType.Stream: + type = Constants.SOCK_STREAM; + break; + case SocketType.Dgram: + type = Constants.SOCK_DGRAM; + break; + } + switch (protocolType) { + case ProtocolType.Udp: + protocol = Constants.IPPROTO_UDP; + break; + case ProtocolType.Tcp: + protocol = Constants.IPPROTO_TCP; + break; + case ProtocolType.Unspecified: + protocol = 0; // ? + break; + } + if ((_fd = zts_socket(family, type, protocol)) < 0) { + throw new ZeroTier.Sockets.SocketException((int)_fd); + } + _socketFamily = addressFamily; + _socketType = socketType; + _socketProtocol = protocolType; + InitializeInternalFlags(); + } + + private Socket( + int fileDescriptor, + AddressFamily addressFamily, + SocketType socketType, + ProtocolType protocolType, + EndPoint localEndPoint, + EndPoint remoteEndPoint) + { + _socketFamily = addressFamily; + _socketType = socketType; + _socketProtocol = protocolType; + _localEndPoint = localEndPoint; + _remoteEndPoint = remoteEndPoint; + _fd = fileDescriptor; + InitializeInternalFlags(); + } + + public void Connect(IPEndPoint remoteEndPoint) + { + if (_isClosed) { + throw new ObjectDisposedException("Socket has been closed"); + } + if (_fd < 0) { + // Invalid file descriptor + throw new ZeroTier.Sockets.SocketException((int)Constants.ERR_SOCKET); + } + if (remoteEndPoint == null) { + throw new ArgumentNullException("remoteEndPoint"); + } + int err = zts_simple_connect( + _fd, + remoteEndPoint.Address.ToString(), + (ushort)remoteEndPoint.Port, + _connectTimeout); + if (err < 0) { + throw new ZeroTier.Sockets.SocketException(err, ZeroTier.Core.Node.ErrNo); + } + _remoteEndPoint = remoteEndPoint; + _isConnected = true; + } + + public void Bind(IPEndPoint localEndPoint) + { + if (_isClosed) { + throw new ObjectDisposedException("Socket has been closed"); + } + if (_fd < 0) { + // Invalid file descriptor + throw new ZeroTier.Sockets.SocketException((int)Constants.ERR_SOCKET); + } + if (localEndPoint == null) { + throw new ArgumentNullException("localEndPoint"); + } + int err = Constants.ERR_OK; + if (localEndPoint.AddressFamily == AddressFamily.InterNetwork) { + err = zts_simple_bind(_fd, "0.0.0.0", (ushort)localEndPoint.Port); + } + if (localEndPoint.AddressFamily == AddressFamily.InterNetworkV6) { + // Todo: detect IPAddress.IPv6Any + err = zts_simple_bind(_fd, "::", (ushort)localEndPoint.Port); + } + if (err < 0) { + throw new ZeroTier.Sockets.SocketException((int)err); + } + _localEndPoint = localEndPoint; + _isBound = true; + } + + public void Listen(int backlog) + { + if (_isClosed) { + throw new ObjectDisposedException("Socket has been closed"); + } + if (_fd < 0) { + // Invalid file descriptor + throw new ZeroTier.Sockets.SocketException((int)Constants.ERR_SOCKET); + } + int err = Constants.ERR_OK; + if ((err = zts_listen(_fd, backlog)) < 0) { + // Invalid backlog value perhaps? + throw new ZeroTier.Sockets.SocketException((int)Constants.ERR_SOCKET); + } + _isListening = true; + } + + public Socket Accept() + { + if (_isClosed) { + throw new ObjectDisposedException("Socket has been closed"); + } + if (_fd < 0) { + // Invalid file descriptor + throw new ZeroTier.Sockets.SocketException((int)Constants.ERR_SOCKET); + } + if (_isListening == false) { + throw new InvalidOperationException("Socket is not in a listening state. Call Listen() first"); + } + IntPtr lpBuffer = Marshal.AllocHGlobal(ZeroTier.Constants.INET6_ADDRSTRLEN); + int port = 0; + int accepted_fd = zts_simple_accept(_fd, lpBuffer, ZeroTier.Constants.INET6_ADDRSTRLEN, ref port); + // Convert buffer to managed string + string str = Marshal.PtrToStringAnsi(lpBuffer); + Marshal.FreeHGlobal(lpBuffer); + lpBuffer = IntPtr.Zero; + IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Parse(str), port); + Console.WriteLine("clientEndPoint = " + clientEndPoint.ToString()); + // Create new socket by providing file descriptor returned from zts_accept call. + Socket clientSocket = + new Socket(accepted_fd, _socketFamily, _socketType, _socketProtocol, _localEndPoint, clientEndPoint); + return clientSocket; + } + + public void Shutdown(SocketShutdown how) + { + if (_isClosed) { + throw new ObjectDisposedException("Socket has been closed"); + } + int ztHow = 0; + switch (how) { + case SocketShutdown.Receive: + ztHow = Constants.O_RDONLY; + break; + case SocketShutdown.Send: + ztHow = Constants.O_WRONLY; + break; + case SocketShutdown.Both: + ztHow = Constants.O_RDWR; + break; + } + zts_shutdown(_fd, ztHow); + } + + public void Close() + { + if (_isClosed) { + throw new ObjectDisposedException("Socket has already been closed"); + } + zts_close(_fd); + _isClosed = true; + } + + public bool Blocking + { + get { + return Convert.ToBoolean(zts_simple_get_blocking(_fd)); + } + set { + zts_simple_set_blocking(_fd, Convert.ToInt32(value)); + } + } + + public bool Poll(int microSeconds, System.Net.Sockets.SelectMode mode) + { + if (_isClosed) { + throw new ObjectDisposedException("Socket has been closed"); + } + zts_pollfd poll_set = new zts_pollfd(); + poll_set.fd = _fd; + if (mode == SelectMode.SelectRead) { + poll_set.events = (short)((byte)ZeroTier.Constants.POLLIN); + } + if (mode == SelectMode.SelectWrite) { + poll_set.events = (short)((byte)ZeroTier.Constants.POLLOUT); + } + if (mode == SelectMode.SelectError) { + poll_set.events = (short)((byte)ZeroTier.Constants.POLLERR | (byte)ZeroTier.Constants.POLLNVAL); + } + IntPtr poll_fd_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(zts_pollfd))); + Marshal.StructureToPtr(poll_set, poll_fd_ptr, false); + int result = 0; + int timeout_ms = (microSeconds / 1000); + uint numfds = 1; + if ((result = zts_poll(poll_fd_ptr, numfds, timeout_ms)) < 0) { + throw new ZeroTier.Sockets.SocketException(result, ZeroTier.Core.Node.ErrNo); + } + poll_set = (zts_pollfd)Marshal.PtrToStructure(poll_fd_ptr, typeof(zts_pollfd)); + if (result != 0) { + if (mode == SelectMode.SelectRead) { + result = Convert.ToInt32(((byte)poll_set.revents & (byte)ZeroTier.Constants.POLLIN) != 0); + } + if (mode == SelectMode.SelectWrite) { + result = Convert.ToInt32(((byte)poll_set.revents & (byte)ZeroTier.Constants.POLLOUT) != 0); + } + if (mode == SelectMode.SelectError) { + result = Convert.ToInt32( + ((poll_set.revents & (byte)ZeroTier.Constants.POLLERR) != 0) + || ((poll_set.revents & (byte)ZeroTier.Constants.POLLNVAL) != 0)); + } + } + Marshal.FreeHGlobal(poll_fd_ptr); + return result > 0; + } + + public Int32 Send(Byte[] buffer) + { + if (_isClosed) { + throw new ObjectDisposedException("Socket has been closed"); + } + if (_fd < 0) { + throw new ZeroTier.Sockets.SocketException((int)ZeroTier.Constants.ERR_SOCKET); + } + if (buffer == null) { + throw new ArgumentNullException("buffer"); + } + int flags = 0; + IntPtr bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); + return zts_send(_fd, bufferPtr, (uint)Buffer.ByteLength(buffer), (int)flags); + } + + public Int32 Receive(Byte[] buffer) + { + if (_isClosed) { + throw new ObjectDisposedException("Socket has been closed"); + } + if (_fd < 0) { + throw new ZeroTier.Sockets.SocketException((int)ZeroTier.Constants.ERR_SOCKET); + } + if (buffer == null) { + throw new ArgumentNullException("buffer"); + } + int flags = 0; + IntPtr bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0); + return zts_recv(_fd, bufferPtr, (uint)Buffer.ByteLength(buffer), (int)flags); + } + + public int ReceiveTimeout + { + get { + return zts_get_recv_timeout(_fd); + } + // TODO: microseconds + set { + zts_set_recv_timeout(_fd, value, 0); + } + } + + public int SendTimeout + { + get { + return zts_get_send_timeout(_fd); + } + // TODO: microseconds + set { + zts_set_send_timeout(_fd, value, 0); + } + } + + public int ConnectTimeout + { + get { + return _connectTimeout; + } + set { + _connectTimeout = value; + } + } + + public int ReceiveBufferSize + { + get { + return zts_get_recv_buf_size(_fd); + } + set { + zts_set_recv_buf_size(_fd, value); + } + } + + public int SendBufferSize + { + get { + return zts_get_send_buf_size(_fd); + } + set { + zts_set_send_buf_size(_fd, value); + } + } + + public short Ttl + { + get { + return Convert.ToInt16(zts_get_ttl(_fd)); + } + set { + zts_set_ttl(_fd, value); + } + } + + public LingerOption LingerState + { + get { + LingerOption lo = + new LingerOption(Convert.ToBoolean(zts_get_linger_enabled(_fd)), zts_get_linger_value(_fd)); + return lo; + } + set { + zts_set_linger(_fd, Convert.ToInt32(value.Enabled), value.LingerTime); + } + } + + public bool NoDelay + { + get { + return Convert.ToBoolean(zts_get_no_delay(_fd)); + } + set { + zts_set_no_delay(_fd, Convert.ToInt32(value)); + } + } + + public bool KeepAlive + { + get { + return Convert.ToBoolean(zts_get_keepalive(_fd)); + } + set { + zts_set_keepalive(_fd, Convert.ToInt32(value)); + } + } + + public bool Connected + { + get { + return _isConnected; + } + } + + public bool IsBound + { + get { + return _isBound; + } + } + + public AddressFamily AddressFamily + { + get { + return _socketFamily; + } + } + + public SocketType SocketType + { + get { + return _socketType; + } + } + + public ProtocolType ProtocolType + { + get { + return _socketProtocol; + } + } + + /* .NET has moved to OSSupportsIPv* but libzt isn't an OS so we keep this old convention */ + public static bool SupportsIPv4 + { + get { + return true; + } + } + + /* .NET has moved to OSSupportsIPv* but libzt isn't an OS so we keep this old convention */ + public static bool SupportsIPv6 + { + get { + return true; + } + } + + public EndPoint RemoteEndPoint + { + get { + return _remoteEndPoint; + } + } + + public EndPoint LocalEndPoint + { + get { + return _localEndPoint; + } + } + + /* Structures and functions used internally to communicate with + lower-level C API defined in include/ZeroTierSockets.h */ + + [DllImport( + "libzt", + CharSet = CharSet.Ansi, + EntryPoint = "CSharp_zts_gethostbyname")] public static extern global::System.IntPtr + zts_gethostbyname(string jarg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_select")] + static extern int zts_select( + int jarg1, + global::System.Runtime.InteropServices.HandleRef jarg2, + global::System.Runtime.InteropServices.HandleRef jarg3, + global::System.Runtime.InteropServices.HandleRef jarg4, + global::System.Runtime.InteropServices.HandleRef jarg5); + + [DllImport("libzt", EntryPoint = "CSharp_zts_get_all_stats")] + static extern int zts_get_all_stats(IntPtr arg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_get_protocol_stats")] + static extern int zts_get_protocol_stats(int arg1, IntPtr arg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_socket")] + static extern int zts_socket(int arg1, int arg2, int arg3); + + [DllImport("libzt", EntryPoint = "CSharp_zts_connect")] + static extern int zts_connect(int arg1, IntPtr arg2, ushort arg3); - /* .NET has moved to OSSupportsIPv* but libzt isn't an OS so we keep this old convention */ - public static bool SupportsIPv4 { get { return true; } } + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_connect_easy")] + static extern int zts_connect_easy(int arg1, int arg2, string arg3, ushort arg4, int arg5); - /* .NET has moved to OSSupportsIPv* but libzt isn't an OS so we keep this old convention */ - public static bool SupportsIPv6 { get { return true; } } + [DllImport("libzt", EntryPoint = "CSharp_zts_bind")] + static extern int zts_bind(int arg1, IntPtr arg2, ushort arg3); - public EndPoint RemoteEndPoint { get { return _remoteEndPoint; } } + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_bind_easy")] + static extern int zts_bind_easy(int arg1, int arg2, string arg3, ushort arg4); - public EndPoint LocalEndPoint { get { return _localEndPoint; } } + [DllImport("libzt", EntryPoint = "CSharp_zts_listen")] + static extern int zts_listen(int arg1, int arg2); - /* Structures and functions used internally to communicate with - lower-level C API defined in include/ZeroTierSockets.h */ + [DllImport("libzt", EntryPoint = "CSharp_zts_accept")] + static extern int zts_accept(int arg1, IntPtr arg2, IntPtr arg3); - [DllImport("libzt", EntryPoint="CSharp_zts_get_all_stats")] - static extern int zts_get_all_stats(IntPtr arg1); + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_accept_easy")] + static extern int zts_accept_easy(int arg1, IntPtr remoteAddrStr, int arg2, ref int arg3); - [DllImport("libzt", EntryPoint="CSharp_zts_get_protocol_stats")] - static extern int zts_get_protocol_stats(int arg1, IntPtr arg2); + [DllImport("libzt", EntryPoint = "CSharp_zts_setsockopt")] + static extern int zts_setsockopt(int arg1, int arg2, int arg3, IntPtr arg4, ushort arg5); - [DllImport("libzt", EntryPoint="CSharp_zts_socket")] - static extern int zts_socket(int arg1, int arg2, int arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_getsockopt")] + static extern int zts_getsockopt(int arg1, int arg2, int arg3, IntPtr arg4, IntPtr arg5); - [DllImport("libzt", EntryPoint="CSharp_zts_connect")] - static extern int zts_connect(int arg1, IntPtr arg2, ushort arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_getsockname")] + static extern int zts_getsockname(int arg1, IntPtr arg2, IntPtr arg3); - [DllImport("libzt", CharSet=CharSet.Ansi, EntryPoint="CSharp_zts_connect_easy")] - static extern int zts_connect_easy(int arg1, int arg2, string arg3, ushort arg4, int arg5); + [DllImport("libzt", EntryPoint = "CSharp_zts_getpeername")] + static extern int zts_getpeername(int arg1, IntPtr arg2, IntPtr arg3); - [DllImport("libzt", EntryPoint="CSharp_zts_bind")] - static extern int zts_bind(int arg1, IntPtr arg2, ushort arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_close")] + static extern int zts_close(int arg1); - [DllImport("libzt", CharSet=CharSet.Ansi, EntryPoint="CSharp_zts_bind_easy")] - static extern int zts_bind_easy(int arg1, int arg2, string arg3, ushort arg4); + [DllImport("libzt", EntryPoint = "CSharp_zts_fcntl")] + static extern int zts_fcntl(int arg1, int arg2, int arg3); - [DllImport("libzt", EntryPoint="CSharp_zts_listen")] - static extern int zts_listen(int arg1, int arg2); + [DllImport("libzt", EntryPoint = "CSharp_zts_poll")] + static extern int zts_poll(IntPtr arg1, uint arg2, int arg3); - [DllImport("libzt", EntryPoint="CSharp_zts_accept")] - static extern int zts_accept(int arg1, IntPtr arg2, IntPtr arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_ioctl")] + static extern int zts_ioctl(int arg1, uint arg2, IntPtr arg3); - [DllImport("libzt", CharSet=CharSet.Ansi, EntryPoint="CSharp_zts_accept_easy")] - static extern int zts_accept_easy(int arg1, IntPtr remoteAddrStr, int arg2, ref int arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_send")] + static extern int zts_send(int arg1, IntPtr arg2, uint arg3, int arg4); - [DllImport("libzt", EntryPoint="CSharp_zts_setsockopt")] - static extern int zts_setsockopt(int arg1, int arg2, int arg3, IntPtr arg4, ushort arg5); + [DllImport("libzt", EntryPoint = "CSharp_zts_sendto")] + static extern int zts_sendto(int arg1, IntPtr arg2, uint arg3, int arg4, IntPtr arg5, ushort arg6); - [DllImport("libzt", EntryPoint="CSharp_zts_getsockopt")] - static extern int zts_getsockopt(int arg1, int arg2, int arg3, IntPtr arg4, IntPtr arg5); + [DllImport("libzt", EntryPoint = "CSharp_zts_sendmsg")] + static extern int zts_sendmsg(int arg1, IntPtr arg2, int arg3); - [DllImport("libzt", EntryPoint="CSharp_zts_getsockname")] - static extern int zts_getsockname(int arg1, IntPtr arg2, IntPtr arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_recv")] + static extern int zts_recv(int arg1, IntPtr arg2, uint arg3, int arg4); - [DllImport("libzt", EntryPoint="CSharp_zts_getpeername")] - static extern int zts_getpeername(int arg1, IntPtr arg2, IntPtr arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_recvfrom")] + static extern int zts_recvfrom(int arg1, IntPtr arg2, uint arg3, int arg4, IntPtr arg5, IntPtr arg6); - [DllImport("libzt", EntryPoint="CSharp_zts_close")] - static extern int zts_close(int arg1); + [DllImport("libzt", EntryPoint = "CSharp_zts_recvmsg")] + static extern int zts_recvmsg(int arg1, IntPtr arg2, int arg3); - [DllImport("libzt", EntryPoint="CSharp_zts_fcntl")] - static extern int zts_fcntl(int arg1, int arg2, int arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_read")] + static extern int zts_read(int arg1, IntPtr arg2, uint arg3); - [DllImport("libzt", EntryPoint="CSharp_zts_poll")] - static extern int zts_poll(IntPtr arg1, uint arg2, int arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_readv")] + static extern int zts_readv(int arg1, IntPtr arg2, int arg3); - [DllImport("libzt", EntryPoint="CSharp_zts_ioctl")] - static extern int zts_ioctl(int arg1, uint arg2, IntPtr arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_write")] + static extern int zts_write(int arg1, IntPtr arg2, uint arg3); - [DllImport("libzt", EntryPoint="CSharp_zts_send")] - static extern int zts_send(int arg1, IntPtr arg2, uint arg3, int arg4); + [DllImport("libzt", EntryPoint = "CSharp_zts_writev")] + static extern int zts_writev(int arg1, IntPtr arg2, int arg3); - [DllImport("libzt", EntryPoint="CSharp_zts_sendto")] - static extern int zts_sendto(int arg1, IntPtr arg2, uint arg3, int arg4, IntPtr arg5, ushort arg6); + [DllImport("libzt", EntryPoint = "CSharp_zts_shutdown")] + static extern int zts_shutdown(int arg1, int arg2); - [DllImport("libzt", EntryPoint="CSharp_zts_sendmsg")] - static extern int zts_sendmsg(int arg1, IntPtr arg2, int arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_set_no_delay")] + static extern int zts_set_no_delay(int fd, int enabled); - [DllImport("libzt", EntryPoint="CSharp_zts_recv")] - static extern int zts_recv(int arg1, IntPtr arg2, uint arg3, int arg4); + [DllImport("libzt", EntryPoint = "CSharp_zts_get_no_delay")] + static extern int zts_get_no_delay(int fd); - [DllImport("libzt", EntryPoint="CSharp_zts_recvfrom")] - static extern int zts_recvfrom(int arg1, IntPtr arg2, uint arg3, int arg4, IntPtr arg5, IntPtr arg6); + [DllImport("libzt", EntryPoint = "CSharp_zts_set_linger")] + static extern int zts_set_linger(int fd, int enabled, int value); - [DllImport("libzt", EntryPoint="CSharp_zts_recvmsg")] - static extern int zts_recvmsg(int arg1, IntPtr arg2, int arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_get_linger_enabled")] + static extern int zts_get_linger_enabled(int fd); - [DllImport("libzt", EntryPoint="CSharp_zts_read")] - static extern int zts_read(int arg1, IntPtr arg2, uint arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_get_linger_value")] + static extern int zts_get_linger_value(int fd); - [DllImport("libzt", EntryPoint="CSharp_zts_readv")] - static extern int zts_readv(int arg1, IntPtr arg2, int arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_set_reuse_addr")] + static extern int zts_set_reuse_addr(int fd, int enabled); - [DllImport("libzt", EntryPoint="CSharp_zts_write")] - static extern int zts_write(int arg1, IntPtr arg2, uint arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_get_reuse_addr")] + static extern int zts_get_reuse_addr(int fd); - [DllImport("libzt", EntryPoint="CSharp_zts_writev")] - static extern int zts_writev(int arg1, IntPtr arg2, int arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_set_recv_timeout")] + static extern int zts_set_recv_timeout(int fd, int seconds, int microseconds); - [DllImport("libzt", EntryPoint="CSharp_zts_shutdown")] - static extern int zts_shutdown(int arg1, int arg2); + [DllImport("libzt", EntryPoint = "CSharp_zts_get_recv_timeout")] + static extern int zts_get_recv_timeout(int fd); - [DllImport("libzt", EntryPoint="CSharp_zts_set_no_delay")] - static extern int zts_set_no_delay(int fd, int enabled); + [DllImport("libzt", EntryPoint = "CSharp_zts_set_send_timeout")] + static extern int zts_set_send_timeout(int fd, int seconds, int microseconds); - [DllImport("libzt", EntryPoint="CSharp_zts_get_no_delay")] - static extern int zts_get_no_delay(int fd); + [DllImport("libzt", EntryPoint = "CSharp_zts_get_send_timeout")] + static extern int zts_get_send_timeout(int fd); - [DllImport("libzt", EntryPoint="CSharp_zts_set_linger")] - static extern int zts_set_linger(int fd, int enabled, int value); + [DllImport("libzt", EntryPoint = "CSharp_zts_set_send_buf_size")] + static extern int zts_set_send_buf_size(int fd, int size); - [DllImport("libzt", EntryPoint="CSharp_zts_get_linger_enabled")] - static extern int zts_get_linger_enabled(int fd); + [DllImport("libzt", EntryPoint = "CSharp_zts_get_send_buf_size")] + static extern int zts_get_send_buf_size(int fd); - [DllImport("libzt", EntryPoint="CSharp_zts_get_linger_value")] - static extern int zts_get_linger_value(int fd); + [DllImport("libzt", EntryPoint = "CSharp_zts_set_recv_buf_size")] + static extern int zts_set_recv_buf_size(int fd, int size); - [DllImport("libzt", EntryPoint="CSharp_zts_set_reuse_addr")] - static extern int zts_set_reuse_addr(int fd, int enabled); + [DllImport("libzt", EntryPoint = "CSharp_zts_get_recv_buf_size")] + static extern int zts_get_recv_buf_size(int fd); - [DllImport("libzt", EntryPoint="CSharp_zts_get_reuse_addr")] - static extern int zts_get_reuse_addr(int fd); + [DllImport("libzt", EntryPoint = "CSharp_zts_set_ttl")] + static extern int zts_set_ttl(int fd, int ttl); - [DllImport("libzt", EntryPoint="CSharp_zts_set_recv_timeout")] - static extern int zts_set_recv_timeout(int fd, int seconds, int microseconds); + [DllImport("libzt", EntryPoint = "CSharp_zts_get_ttl")] + static extern int zts_get_ttl(int fd); - [DllImport("libzt", EntryPoint="CSharp_zts_get_recv_timeout")] - static extern int zts_get_recv_timeout(int fd); + [DllImport("libzt", EntryPoint = "CSharp_zts_set_blocking")] + static extern int zts_set_blocking(int fd, int enabled); - [DllImport("libzt", EntryPoint="CSharp_zts_set_send_timeout")] - static extern int zts_set_send_timeout(int fd, int seconds, int microseconds); + [DllImport("libzt", EntryPoint = "CSharp_zts_get_blocking")] + static extern int zts_get_blocking(int fd); - [DllImport("libzt", EntryPoint="CSharp_zts_get_send_timeout")] - static extern int zts_get_send_timeout(int fd); + [DllImport("libzt", EntryPoint = "CSharp_zts_set_keepalive")] + static extern int zts_set_keepalive(int fd, int enabled); - [DllImport("libzt", EntryPoint="CSharp_zts_set_send_buf_size")] - static extern int zts_set_send_buf_size(int fd, int size); + [DllImport("libzt", EntryPoint = "CSharp_zts_get_keepalive")] + static extern int zts_get_keepalive(int fd); - [DllImport("libzt", EntryPoint="CSharp_zts_get_send_buf_size")] - static extern int zts_get_send_buf_size(int fd); + [DllImport("libzt", EntryPoint = "CSharp_zts_add_dns_nameserver")] + static extern int zts_add_dns_nameserver(IntPtr arg1); - [DllImport("libzt", EntryPoint="CSharp_zts_set_recv_buf_size")] - static extern int zts_set_recv_buf_size(int fd, int size); + [DllImport("libzt", EntryPoint = "CSharp_zts_del_dns_nameserver")] + static extern int zts_del_dns_nameserver(IntPtr arg1); - [DllImport("libzt", EntryPoint="CSharp_zts_get_recv_buf_size")] - static extern int zts_get_recv_buf_size(int fd); + [DllImport("libzt", EntryPoint = "CSharp_zts_errno_get")] + static extern int zts_errno_get(); - [DllImport("libzt", EntryPoint="CSharp_zts_set_ttl")] - static extern int zts_set_ttl(int fd, int ttl); + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_simple_accept")] + static extern int zts_simple_accept(int jarg1, IntPtr jarg2, int jarg3, ref int jarg4); - [DllImport("libzt", EntryPoint="CSharp_zts_get_ttl")] - static extern int zts_get_ttl(int fd); + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_simple_tcp_client")] + static extern int zts_simple_tcp_client(string jarg1, int jarg2); - [DllImport("libzt", EntryPoint="CSharp_zts_set_blocking")] - static extern int zts_set_blocking(int fd, int enabled); + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_simple_tcp_server")] + static extern int zts_simple_tcp_server( + string jarg1, + int jarg2, + string jarg3, + int jarg4, + global::System.Runtime.InteropServices.HandleRef jarg5); - [DllImport("libzt", EntryPoint="CSharp_zts_get_blocking")] - static extern int zts_get_blocking(int fd); + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_simple_udp_server")] + static extern int zts_simple_udp_server(string jarg1, int jarg2); - [DllImport("libzt", EntryPoint="CSharp_zts_set_keepalive")] - static extern int zts_set_keepalive(int fd, int enabled); + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_simple_udp_client")] + static extern int zts_simple_udp_client(string jarg1); - [DllImport("libzt", EntryPoint="CSharp_zts_get_keepalive")] - static extern int zts_get_keepalive(int fd); + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_simple_bind")] + static extern int zts_simple_bind(int jarg1, string jarg2, int jarg3); - [DllImport("libzt", EntryPoint="CSharp_zts_add_dns_nameserver")] - static extern int zts_add_dns_nameserver(IntPtr arg1); + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_simple_connect")] + static extern int zts_simple_connect(int jarg1, string jarg2, int jarg3, int jarg4); - [DllImport("libzt", EntryPoint="CSharp_zts_del_dns_nameserver")] - static extern int zts_del_dns_nameserver(IntPtr arg1); + [DllImport("libzt", EntryPoint = "CSharp_zts_stats_get_all")] + static extern int zts_stats_get_all(global::System.Runtime.InteropServices.HandleRef jarg1); - [DllImport("libzt", EntryPoint="CSharp_zts_inet_ntop")] - static extern string zts_inet_ntop(int arg1, IntPtr arg2, string arg3, ushort arg4); + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_set_no_delay")] + static extern int zts_simple_set_no_delay(int jarg1, int jarg2); - [DllImport("libzt", EntryPoint="CSharp_zts_inet_pton")] - static extern int zts_inet_pton(int arg1, string arg2, IntPtr arg3); + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_get_no_delay")] + static extern int zts_simple_get_no_delay(int jarg1); - [DllImport("libzt", EntryPoint="CSharp_zts_errno_get")] - static extern int zts_errno_get(); + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_set_linger")] + static extern int zts_simple_set_linger(int jarg1, int jarg2, int jarg3); - /// The value of errno for the low-level socket layer - public static int ErrNo { - get { - return zts_errno_get(); - } - } + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_get_linger_enabled")] + static extern int zts_simple_get_linger_enabled(int jarg1); - [StructLayout(LayoutKind.Sequential)] - struct zts_sockaddr - { - public byte sa_len; - public byte sa_family; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)] - public byte[] sa_data; - } + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_get_linger_value")] + static extern int zts_simple_get_linger_value(int jarg1); - [StructLayout(LayoutKind.Sequential)] - struct zts_in_addr - { - public uint s_addr; - } + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_set_reuse_addr")] + static extern int zts_simple_set_reuse_addr(int jarg1, int jarg2); - [StructLayout(LayoutKind.Sequential)] - struct zts_sockaddr_in - { - public byte sin_len; - public byte sin_family; - public short sin_port; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] - public byte[] sin_addr; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] - public char[] sin_zero; // SIN_ZERO_LEN - } - - [StructLayout(LayoutKind.Sequential)] - struct zts_pollfd - { - public int fd; - public short events; - public short revents; - } - - [StructLayout(LayoutKind.Sequential)] - struct zts_timeval - { - public long tv_sec; - public long tv_usec; - } - } + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_get_reuse_addr")] + static extern int zts_simple_get_reuse_addr(int jarg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_set_recv_timeout")] + static extern int zts_simple_set_recv_timeout(int jarg1, int jarg2, int jarg3); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_get_recv_timeout")] + static extern int zts_simple_get_recv_timeout(int jarg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_set_send_timeout")] + static extern int zts_simple_set_send_timeout(int jarg1, int jarg2, int jarg3); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_get_send_timeout")] + static extern int zts_simple_get_send_timeout(int jarg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_set_send_buf_size")] + static extern int zts_simple_set_send_buf_size(int jarg1, int jarg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_get_send_buf_size")] + static extern int zts_simple_get_send_buf_size(int jarg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_set_recv_buf_size")] + static extern int zts_simple_set_recv_buf_size(int jarg1, int jarg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_get_recv_buf_size")] + static extern int zts_simple_get_recv_buf_size(int jarg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_set_ttl")] + static extern int zts_simple_set_ttl(int jarg1, int jarg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_get_ttl")] + static extern int zts_simple_get_ttl(int jarg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_set_blocking")] + static extern int zts_simple_set_blocking(int jarg1, int jarg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_get_blocking")] + static extern int zts_simple_get_blocking(int jarg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_set_keepalive")] + static extern int zts_simple_set_keepalive(int jarg1, int jarg2); + + [DllImport("libzt", EntryPoint = "CSharp_zts_simple_get_keepalive")] + static extern int zts_simple_get_keepalive(int jarg1); + + [DllImport("libzt", EntryPoint = "CSharp_zts_util_delay")] + public static extern void zts_util_delay(int jarg1); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_util_get_ip_family")] + static extern int zts_util_get_ip_family(string jarg1); + + [DllImport("libzt", CharSet = CharSet.Ansi, EntryPoint = "CSharp_zts_util_ipstr_to_saddr")] + static extern int zts_util_ipstr_to_saddr( + string jarg1, + int jarg2, + global::System.Runtime.InteropServices.HandleRef jarg3, + global::System.Runtime.InteropServices.HandleRef jarg4); + + /// The value of errno for the low-level socket layer + public static int ErrNo + { + get { + return zts_errno_get(); + } + } + + [StructLayout(LayoutKind.Sequential)] struct zts_sockaddr { + public byte sa_len; + public byte sa_family; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)] + public byte[] sa_data; + } + + [StructLayout(LayoutKind.Sequential)] + struct zts_in_addr { + public uint s_addr; + } + + [StructLayout(LayoutKind.Sequential)] + struct zts_sockaddr_in { + public byte sin_len; + public byte sin_family; + public short sin_port; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)] + public byte[] sin_addr; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] + public char[] sin_zero; // SIN_ZERO_LEN + } + + [StructLayout(LayoutKind.Sequential)] + struct zts_pollfd { + public int fd; + public short events; + public short revents; + } + + [StructLayout(LayoutKind.Sequential)] + struct zts_timeval { + public long tv_sec; + public long tv_usec; + } + } } diff --git a/src/bindings/csharp/SocketException.cs b/src/bindings/csharp/SocketException.cs old mode 100644 new mode 100755 index 3a15568..0c66328 --- a/src/bindings/csharp/SocketException.cs +++ b/src/bindings/csharp/SocketException.cs @@ -4,7 +4,7 @@ * Use of this software is governed by the Business Source License included * in the LICENSE.TXT file in the project's root directory. * - * Change Date: 2025-01-01 + * Change Date: 2026-01-01 * * On the date above, in accordance with the Business Source License, use * of this software will be governed by version 2.0 of the Apache License. @@ -15,24 +15,26 @@ using System; namespace ZeroTier.Sockets { - /// Exception class for ZeroTier service and low-level socket errors - public class SocketException : Exception - { - public SocketException(int _serviceErrorCode) - : base(String.Format("ServiceErrorCode={0} (See Constants.cs for error code meanings)", _serviceErrorCode)) - { - ServiceErrorCode = _serviceErrorCode; - } - public SocketException(int _serviceErrorCode, int _socketErrorCode) - : base(String.Format("ServiceErrorCode={0}, SocketErrorCode={1} (See Constants.cs for error code meanings)", _serviceErrorCode, _socketErrorCode)) - { - ServiceErrorCode = _serviceErrorCode; - SocketErrorCode = _socketErrorCode; - } - /// High-level service error code. See Constants.cs - public int ServiceErrorCode { get; private set; } + /// Exception class for ZeroTier service and low-level socket errors + public class SocketException : Exception { + public SocketException(int _serviceErrorCode) + : base(String.Format("ServiceErrorCode={0} (See Constants.cs for error code meanings)", _serviceErrorCode)) + { + ServiceErrorCode = _serviceErrorCode; + } + public SocketException(int _serviceErrorCode, int _socketErrorCode) + : base(String.Format( + "ServiceErrorCode={0}, SocketErrorCode={1} (See Constants.cs for error code meanings)", + _serviceErrorCode, + _socketErrorCode)) + { + ServiceErrorCode = _serviceErrorCode; + SocketErrorCode = _socketErrorCode; + } + /// High-level service error code. See Constants.cs + public int ServiceErrorCode { get; private set; } - /// Low-level socket error code. See Constants.cs - public int SocketErrorCode { get; private set; } - } -} \ No newline at end of file + /// Low-level socket error code. See Constants.cs + public int SocketErrorCode { get; private set; } + } +} diff --git a/src/bindings/csharp/zt.i b/src/bindings/csharp/zt.i old mode 100644 new mode 100755 index b2e752c..4289515 --- a/src/bindings/csharp/zt.i +++ b/src/bindings/csharp/zt.i @@ -27,18 +27,18 @@ %ignore zts_sockaddr_storage; %ignore zts_sockaddr_in6; -%ignore zts_callback_msg; -%ignore zts_node_details; -%ignore zts_network_details; -%ignore zts_netif_details; -%ignore zts_virtual_network_route; -%ignore zts_peer_details; -%ignore zts_addr_details; +%ignore zts_event_msg_t; +%ignore zts_node_info_t; +%ignore zts_net_info_t; +%ignore zts_netif_info_t; +%ignore zts_route_info_t; +%ignore zts_peer_info_t; +%ignore zts_addr_info_t; -#define ZTS_CSHARP=1 +#define ZTS_ENABLE_PINVOKE 1 %{ -#include "../../include/ZeroTierSockets.h" +#include "../../../include/ZeroTierSockets.h" %} // Typemap for our event callback @@ -52,4 +52,4 @@ %enddef %cs_callback(CppCallback, CSharpCallbackWithStruct) -%include "../../include/ZeroTierSockets.h" +%include "../../../include/ZeroTierSockets.h" diff --git a/src/bindings/csharp/zt_wrap.cpp b/src/bindings/csharp/zt_wrap.cpp deleted file mode 100644 index 31865be..0000000 --- a/src/bindings/csharp/zt_wrap.cpp +++ /dev/null @@ -1,1377 +0,0 @@ -/* - * Copyright (c)2013-2020 ZeroTier, Inc. - * - * Use of this software is governed by the Business Source License included - * in the LICENSE.TXT file in the project's root directory. - * - * Change Date: 2024-01-01 - * - * On the date above, in accordance with the Business Source License, use - * of this software will be governed by version 2.0 of the Apache License. - */ -/****/ - -#ifndef SWIGCSHARP -#define SWIGCSHARP -#endif - - - -#ifdef __cplusplus -/* SwigValueWrapper is described in swig.swg */ -template class SwigValueWrapper { - struct SwigMovePointer { - T *ptr; - SwigMovePointer(T *p) : ptr(p) { } - ~SwigMovePointer() { delete ptr; } - SwigMovePointer& operator=(SwigMovePointer& rhs) { T* oldptr = ptr; ptr = 0; delete oldptr; ptr = rhs.ptr; rhs.ptr = 0; return *this; } - } pointer; - SwigValueWrapper& operator=(const SwigValueWrapper& rhs); - SwigValueWrapper(const SwigValueWrapper& rhs); -public: - SwigValueWrapper() : pointer(0) { } - SwigValueWrapper& operator=(const T& t) { SwigMovePointer tmp(new T(t)); pointer = tmp; return *this; } - operator T&() const { return *pointer.ptr; } - T *operator&() { return pointer.ptr; } -}; - -template T SwigValueInit() { - return T(); -} -#endif - -/* ----------------------------------------------------------------------------- - * This section contains generic SWIG labels for method/variable - * declarations/attributes, and other compiler dependent labels. - * ----------------------------------------------------------------------------- */ - -/* template workaround for compilers that cannot correctly implement the C++ standard */ -#ifndef SWIGTEMPLATEDISAMBIGUATOR -# if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560) -# define SWIGTEMPLATEDISAMBIGUATOR template -# elif defined(__HP_aCC) -/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */ -/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */ -# define SWIGTEMPLATEDISAMBIGUATOR template -# else -# define SWIGTEMPLATEDISAMBIGUATOR -# endif -#endif - -/* inline attribute */ -#ifndef SWIGINLINE -# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__)) -# define SWIGINLINE inline -# else -# define SWIGINLINE -# endif -#endif - -/* attribute recognised by some compilers to avoid 'unused' warnings */ -#ifndef SWIGUNUSED -# if defined(__GNUC__) -# if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) -# define SWIGUNUSED __attribute__ ((__unused__)) -# else -# define SWIGUNUSED -# endif -# elif defined(__ICC) -# define SWIGUNUSED __attribute__ ((__unused__)) -# else -# define SWIGUNUSED -# endif -#endif - -#ifndef SWIG_MSC_UNSUPPRESS_4505 -# if defined(_MSC_VER) -# pragma warning(disable : 4505) /* unreferenced local function has been removed */ -# endif -#endif - -#ifndef SWIGUNUSEDPARM -# ifdef __cplusplus -# define SWIGUNUSEDPARM(p) -# else -# define SWIGUNUSEDPARM(p) p SWIGUNUSED -# endif -#endif - -/* internal SWIG method */ -#ifndef SWIGINTERN -# define SWIGINTERN static SWIGUNUSED -#endif - -/* internal inline SWIG method */ -#ifndef SWIGINTERNINLINE -# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE -#endif - -/* exporting methods */ -#if defined(__GNUC__) -# if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) -# ifndef GCC_HASCLASSVISIBILITY -# define GCC_HASCLASSVISIBILITY -# endif -# endif -#endif - -#ifndef SWIGEXPORT -# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) -# if defined(STATIC_LINKED) -# define SWIGEXPORT -# else -# define SWIGEXPORT __declspec(dllexport) -# endif -# else -# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY) -# define SWIGEXPORT __attribute__ ((visibility("default"))) -# else -# define SWIGEXPORT -# endif -# endif -#endif - -/* calling conventions for Windows */ -#ifndef SWIGSTDCALL -# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) -# define SWIGSTDCALL __stdcall -# else -# define SWIGSTDCALL -# endif -#endif - -/* Deal with Microsoft's attempt at deprecating C standard runtime functions */ -#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE) -# define _CRT_SECURE_NO_DEPRECATE -#endif - -/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */ -#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE) -# define _SCL_SECURE_NO_DEPRECATE -#endif - -/* Deal with Apple's deprecated 'AssertMacros.h' from Carbon-framework */ -#if defined(__APPLE__) && !defined(__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES) -# define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0 -#endif - -/* Intel's compiler complains if a variable which was never initialised is - * cast to void, which is a common idiom which we use to indicate that we - * are aware a variable isn't used. So we just silence that warning. - * See: https://github.com/swig/swig/issues/192 for more discussion. - */ -#ifdef __INTEL_COMPILER -# pragma warning disable 592 -#endif - - -#include -#include -#include - - -/* Support for throwing C# exceptions from C/C++. There are two types: - * Exceptions that take a message and ArgumentExceptions that take a message and a parameter name. */ -typedef enum { - SWIG_CSharpApplicationException, - SWIG_CSharpArithmeticException, - SWIG_CSharpDivideByZeroException, - SWIG_CSharpIndexOutOfRangeException, - SWIG_CSharpInvalidCastException, - SWIG_CSharpInvalidOperationException, - SWIG_CSharpIOException, - SWIG_CSharpNullReferenceException, - SWIG_CSharpOutOfMemoryException, - SWIG_CSharpOverflowException, - SWIG_CSharpSystemException -} SWIG_CSharpExceptionCodes; - -typedef enum { - SWIG_CSharpArgumentException, - SWIG_CSharpArgumentNullException, - SWIG_CSharpArgumentOutOfRangeException -} SWIG_CSharpExceptionArgumentCodes; - -typedef void (SWIGSTDCALL* SWIG_CSharpExceptionCallback_t)(const char *); -typedef void (SWIGSTDCALL* SWIG_CSharpExceptionArgumentCallback_t)(const char *, const char *); - -typedef struct { - SWIG_CSharpExceptionCodes code; - SWIG_CSharpExceptionCallback_t callback; -} SWIG_CSharpException_t; - -typedef struct { - SWIG_CSharpExceptionArgumentCodes code; - SWIG_CSharpExceptionArgumentCallback_t callback; -} SWIG_CSharpExceptionArgument_t; - -static SWIG_CSharpException_t SWIG_csharp_exceptions[] = { - { SWIG_CSharpApplicationException, NULL }, - { SWIG_CSharpArithmeticException, NULL }, - { SWIG_CSharpDivideByZeroException, NULL }, - { SWIG_CSharpIndexOutOfRangeException, NULL }, - { SWIG_CSharpInvalidCastException, NULL }, - { SWIG_CSharpInvalidOperationException, NULL }, - { SWIG_CSharpIOException, NULL }, - { SWIG_CSharpNullReferenceException, NULL }, - { SWIG_CSharpOutOfMemoryException, NULL }, - { SWIG_CSharpOverflowException, NULL }, - { SWIG_CSharpSystemException, NULL } -}; - -static SWIG_CSharpExceptionArgument_t SWIG_csharp_exceptions_argument[] = { - { SWIG_CSharpArgumentException, NULL }, - { SWIG_CSharpArgumentNullException, NULL }, - { SWIG_CSharpArgumentOutOfRangeException, NULL } -}; - -static void SWIGUNUSED SWIG_CSharpSetPendingException(SWIG_CSharpExceptionCodes code, const char *msg) { - SWIG_CSharpExceptionCallback_t callback = SWIG_csharp_exceptions[SWIG_CSharpApplicationException].callback; - if ((size_t)code < sizeof(SWIG_csharp_exceptions)/sizeof(SWIG_CSharpException_t)) { - callback = SWIG_csharp_exceptions[code].callback; - } - callback(msg); -} - -static void SWIGUNUSED SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpExceptionArgumentCodes code, const char *msg, const char *param_name) { - SWIG_CSharpExceptionArgumentCallback_t callback = SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentException].callback; - if ((size_t)code < sizeof(SWIG_csharp_exceptions_argument)/sizeof(SWIG_CSharpExceptionArgument_t)) { - callback = SWIG_csharp_exceptions_argument[code].callback; - } - callback(msg, param_name); -} - - -#ifdef __cplusplus -extern "C" -#endif -SWIGEXPORT void SWIGSTDCALL SWIGRegisterExceptionCallbacks_zt( - SWIG_CSharpExceptionCallback_t applicationCallback, - SWIG_CSharpExceptionCallback_t arithmeticCallback, - SWIG_CSharpExceptionCallback_t divideByZeroCallback, - SWIG_CSharpExceptionCallback_t indexOutOfRangeCallback, - SWIG_CSharpExceptionCallback_t invalidCastCallback, - SWIG_CSharpExceptionCallback_t invalidOperationCallback, - SWIG_CSharpExceptionCallback_t ioCallback, - SWIG_CSharpExceptionCallback_t nullReferenceCallback, - SWIG_CSharpExceptionCallback_t outOfMemoryCallback, - SWIG_CSharpExceptionCallback_t overflowCallback, - SWIG_CSharpExceptionCallback_t systemCallback) { - SWIG_csharp_exceptions[SWIG_CSharpApplicationException].callback = applicationCallback; - SWIG_csharp_exceptions[SWIG_CSharpArithmeticException].callback = arithmeticCallback; - SWIG_csharp_exceptions[SWIG_CSharpDivideByZeroException].callback = divideByZeroCallback; - SWIG_csharp_exceptions[SWIG_CSharpIndexOutOfRangeException].callback = indexOutOfRangeCallback; - SWIG_csharp_exceptions[SWIG_CSharpInvalidCastException].callback = invalidCastCallback; - SWIG_csharp_exceptions[SWIG_CSharpInvalidOperationException].callback = invalidOperationCallback; - SWIG_csharp_exceptions[SWIG_CSharpIOException].callback = ioCallback; - SWIG_csharp_exceptions[SWIG_CSharpNullReferenceException].callback = nullReferenceCallback; - SWIG_csharp_exceptions[SWIG_CSharpOutOfMemoryException].callback = outOfMemoryCallback; - SWIG_csharp_exceptions[SWIG_CSharpOverflowException].callback = overflowCallback; - SWIG_csharp_exceptions[SWIG_CSharpSystemException].callback = systemCallback; -} - -#ifdef __cplusplus -extern "C" -#endif -SWIGEXPORT void SWIGSTDCALL SWIGRegisterExceptionArgumentCallbacks_zt( - SWIG_CSharpExceptionArgumentCallback_t argumentCallback, - SWIG_CSharpExceptionArgumentCallback_t argumentNullCallback, - SWIG_CSharpExceptionArgumentCallback_t argumentOutOfRangeCallback) { - SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentException].callback = argumentCallback; - SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentNullException].callback = argumentNullCallback; - SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentOutOfRangeException].callback = argumentOutOfRangeCallback; -} - - -/* Callback for returning strings to C# without leaking memory */ -typedef char * (SWIGSTDCALL* SWIG_CSharpStringHelperCallback)(const char *); -static SWIG_CSharpStringHelperCallback SWIG_csharp_string_callback = NULL; - - -#ifdef __cplusplus -extern "C" -#endif -SWIGEXPORT void SWIGSTDCALL SWIGRegisterStringCallback_zt(SWIG_CSharpStringHelperCallback callback) { - SWIG_csharp_string_callback = callback; -} - - -/* Contract support */ - -#define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) {SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, msg, ""); return nullreturn; } else - -/* Errors in SWIG */ -#define SWIG_UnknownError -1 -#define SWIG_IOError -2 -#define SWIG_RuntimeError -3 -#define SWIG_IndexError -4 -#define SWIG_TypeError -5 -#define SWIG_DivisionByZero -6 -#define SWIG_OverflowError -7 -#define SWIG_SyntaxError -8 -#define SWIG_ValueError -9 -#define SWIG_SystemError -10 -#define SWIG_AttributeError -11 -#define SWIG_MemoryError -12 -#define SWIG_NullReferenceError -13 - - - - -#include -#include - - -#include - - -#include -#include -#include - - -#include -#include -#include - - -SWIGINTERN void SWIG_CSharpException(int code, const char *msg) { - if (code == SWIG_ValueError) { - SWIG_CSharpExceptionArgumentCodes exception_code = SWIG_CSharpArgumentOutOfRangeException; - SWIG_CSharpSetPendingExceptionArgument(exception_code, msg, 0); - } else { - SWIG_CSharpExceptionCodes exception_code = SWIG_CSharpApplicationException; - switch(code) { - case SWIG_MemoryError: - exception_code = SWIG_CSharpOutOfMemoryException; - break; - case SWIG_IndexError: - exception_code = SWIG_CSharpIndexOutOfRangeException; - break; - case SWIG_DivisionByZero: - exception_code = SWIG_CSharpDivideByZeroException; - break; - case SWIG_IOError: - exception_code = SWIG_CSharpIOException; - break; - case SWIG_OverflowError: - exception_code = SWIG_CSharpOverflowException; - break; - case SWIG_RuntimeError: - case SWIG_TypeError: - case SWIG_SyntaxError: - case SWIG_SystemError: - case SWIG_UnknownError: - default: - exception_code = SWIG_CSharpApplicationException; - break; - } - SWIG_CSharpSetPendingException(exception_code, msg); - } -} - - -#include -#include - - -#include - - -#include "../../include/ZeroTierSockets.h" - - -#ifdef __cplusplus -extern "C" { -#endif - -SWIGEXPORT void SWIGSTDCALL CSharp_zts_errno_set(int jarg1) { - int arg1; - - arg1 = (int)jarg1; - zts_errno = arg1; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_errno_get() { - int jresult; - int result; - - result = (int)zts_errno; - jresult = result; - return jresult; -} - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_allow_network_caching(unsigned char jarg1) { - int jresult; - uint8_t arg1; - int result; - - arg1 = (uint8_t)jarg1; - result = (int)zts_allow_network_caching(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_allow_peer_caching(unsigned char jarg1) { - int jresult; - uint8_t arg1; - int result; - - arg1 = (uint8_t)jarg1; - result = (int)zts_allow_peer_caching(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_allow_local_conf(unsigned char jarg1) { - int jresult; - uint8_t arg1; - int result; - - arg1 = (uint8_t)jarg1; - result = (int)zts_allow_local_conf(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_start(char * jarg1, void * jarg2, unsigned short jarg3) { - int jresult; - char *arg1 = (char *) 0; - CppCallback arg2 = (CppCallback) 0; - uint16_t arg3; - int result; - - arg1 = (char *)jarg1; - arg2 = (CppCallback)jarg2; - arg3 = (uint16_t)jarg3; - result = (int)zts_start((char const *)arg1,arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_stop() { - int jresult; - int result; - - result = (int)zts_stop(); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_restart() { - int jresult; - int result; - - result = (int)zts_restart(); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_free() { - int jresult; - int result; - - result = (int)zts_free(); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_join(unsigned long long jarg1) { - int jresult; - uint64_t arg1; - int result; - - arg1 = (uint64_t)jarg1; - result = (int)zts_join(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_leave(unsigned long long jarg1) { - int jresult; - uint64_t arg1; - int result; - - arg1 = (uint64_t)jarg1; - result = (int)zts_leave(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_orbit(unsigned long long jarg1, unsigned long long jarg2) { - int jresult; - uint64_t arg1; - uint64_t arg2; - int result; - - arg1 = (uint64_t)jarg1; - arg2 = (uint64_t)jarg2; - result = (int)zts_orbit(arg1,arg2); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_deorbit(unsigned long long jarg1) { - int jresult; - uint64_t arg1; - int result; - - arg1 = (uint64_t)jarg1; - result = (int)zts_deorbit(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_6plane_addr(void * jarg1, unsigned long long jarg2, unsigned long long jarg3) { - int jresult; - zts_sockaddr_storage *arg1 = (zts_sockaddr_storage *) 0; - uint64_t arg2; - uint64_t arg3; - int result; - - arg1 = (zts_sockaddr_storage *)jarg1; - arg2 = (uint64_t)jarg2; - arg3 = (uint64_t)jarg3; - result = (int)zts_get_6plane_addr(arg1,arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_rfc4193_addr(void * jarg1, unsigned long long jarg2, unsigned long long jarg3) { - int jresult; - zts_sockaddr_storage *arg1 = (zts_sockaddr_storage *) 0; - uint64_t arg2; - uint64_t arg3; - int result; - - arg1 = (zts_sockaddr_storage *)jarg1; - arg2 = (uint64_t)jarg2; - arg3 = (uint64_t)jarg3; - result = (int)zts_get_rfc4193_addr(arg1,arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT unsigned long long SWIGSTDCALL CSharp_zts_generate_adhoc_nwid_from_range(unsigned short jarg1, unsigned short jarg2) { - unsigned long long jresult; - uint16_t arg1; - uint16_t arg2; - uint64_t result; - - arg1 = (uint16_t)jarg1; - arg2 = (uint16_t)jarg2; - result = zts_generate_adhoc_nwid_from_range(arg1,arg2); - jresult = result; - return jresult; -} - - -SWIGEXPORT void SWIGSTDCALL CSharp_zts_delay_ms(long jarg1) { - long arg1; - - arg1 = (long)jarg1; - zts_delay_ms(arg1); -} - -/* -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_all_stats(void * jarg1) { - int jresult; - zts_stats *arg1 = (zts_stats *) 0; - int result; - - arg1 = (zts_stats *)jarg1; - result = (int)zts_get_all_stats(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_protocol_stats(int jarg1, void * jarg2) { - int jresult; - int arg1; - void *arg2 = (void *) 0; - int result; - - arg1 = (int)jarg1; - arg2 = (void *)jarg2; - result = (int)zts_get_protocol_stats(arg1,arg2); - jresult = result; - return jresult; -} -*/ - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_socket(int jarg1, int jarg2, int jarg3) { - int jresult; - int arg1; - int arg2; - int arg3; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - arg3 = (int)jarg3; - result = (int)zts_socket(arg1,arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_connect(int jarg1, zts_sockaddr* jarg2, unsigned short jarg3) { - int jresult; - int arg1; - zts_sockaddr *arg2 = (zts_sockaddr *) 0; - zts_socklen_t arg3; - int result; - - arg1 = (int)jarg1; - arg2 = (zts_sockaddr *)jarg2; - arg3 = (zts_socklen_t)jarg3; - result = (int)zts_connect(arg1,(zts_sockaddr const *)arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_connect_easy(int jarg1, int jarg2, char* jarg3, unsigned short jarg4, int jarg5) { - int jresult; - int result; - result = (int)zts_connect_easy(jarg1,jarg2,jarg3,jarg4,jarg5); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_bind(int jarg1, zts_sockaddr* jarg2, unsigned short jarg3) { - int jresult; - int arg1; - zts_sockaddr *arg2 = (zts_sockaddr *) 0; - zts_socklen_t arg3; - int result; - - arg1 = (int)jarg1; - arg2 = (zts_sockaddr *)jarg2; - arg3 = (zts_socklen_t)jarg3; - result = (int)zts_bind(arg1,(zts_sockaddr const *)arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_bind_easy(int jarg1, int jarg2, char* jarg3, unsigned short jarg4) { - int jresult; - int result; - result = (int)zts_bind_easy(jarg1,jarg2,jarg3,jarg4); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_listen(int jarg1, int jarg2) { - int jresult; - int arg1; - int arg2; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - result = (int)zts_listen(arg1,arg2); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_accept(int jarg1, zts_sockaddr* jarg2, int * jarg3) { - int jresult; - int arg1; - zts_sockaddr *arg2 = (zts_sockaddr *) 0; - zts_socklen_t *arg3 = (zts_socklen_t *) 0; - int result; - - arg1 = (int)jarg1; - arg2 = (zts_sockaddr *)jarg2; - arg3 = (zts_socklen_t *)jarg3; - result = (int)zts_accept(arg1,arg2,arg3); - jresult = result; - return jresult; -} - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_accept_easy(int jarg1, char* remoteAddrStr, int jarg2, int *jarg3) -{ - return zts_accept_easy(jarg1, remoteAddrStr, jarg2, jarg3); -} - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_setsockopt(int jarg1, int jarg2, int jarg3, void * jarg4, unsigned short jarg5) { - int jresult; - int arg1; - int arg2; - int arg3; - void *arg4 = (void *) 0; - zts_socklen_t arg5; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - arg3 = (int)jarg3; - arg4 = (void *)jarg4; - arg5 = (zts_socklen_t)jarg5; - result = (int)zts_setsockopt(arg1,arg2,arg3,(void const *)arg4,arg5); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_getsockopt(int jarg1, int jarg2, int jarg3, void * jarg4, void * jarg5) { - int jresult; - int arg1; - int arg2; - int arg3; - void *arg4 = (void *) 0; - zts_socklen_t *arg5 = (zts_socklen_t *) 0; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - arg3 = (int)jarg3; - arg4 = (void *)jarg4; - arg5 = (zts_socklen_t *)jarg5; - result = (int)zts_getsockopt(arg1,arg2,arg3,arg4,arg5); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_getsockname(int jarg1, zts_sockaddr* jarg2, void * jarg3) { - int jresult; - int arg1; - zts_sockaddr *arg2 = (zts_sockaddr *) 0; - zts_socklen_t *arg3 = (zts_socklen_t *) 0; - int result; - - arg1 = (int)jarg1; - arg2 = (zts_sockaddr *)jarg2; - arg3 = (zts_socklen_t *)jarg3; - result = (int)zts_getsockname(arg1,arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_getpeername(int jarg1, zts_sockaddr* jarg2, void * jarg3) { - int jresult; - int arg1; - zts_sockaddr *arg2 = (zts_sockaddr *) 0; - zts_socklen_t *arg3 = (zts_socklen_t *) 0; - int result; - - arg1 = (int)jarg1; - arg2 = (zts_sockaddr *)jarg2; - arg3 = (zts_socklen_t *)jarg3; - result = (int)zts_getpeername(arg1,arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_close(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_close(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_select(int jarg1, void * jarg2, void * jarg3, void * jarg4, void * jarg5) { - int jresult; - int arg1; - zts_fd_set *arg2 = (zts_fd_set *) 0; - zts_fd_set *arg3 = (zts_fd_set *) 0; - zts_fd_set *arg4 = (zts_fd_set *) 0; - zts_timeval *arg5 = (zts_timeval *) 0; - int result; - - arg1 = (int)jarg1; - arg2 = (zts_fd_set *)jarg2; - arg3 = (zts_fd_set *)jarg3; - arg4 = (zts_fd_set *)jarg4; - arg5 = (zts_timeval *)jarg5; - result = (int)zts_select(arg1,arg2,arg3,arg4,arg5); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_fcntl(int jarg1, int jarg2, int jarg3) { - int jresult; - int arg1; - int arg2; - int arg3; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - arg3 = (int)jarg3; - result = (int)zts_fcntl(arg1,arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_poll(void * jarg1, unsigned int jarg2, int jarg3) { - int jresult; - zts_pollfd *arg1 = (zts_pollfd *) 0; - zts_nfds_t arg2; - int arg3; - int result; - - arg1 = (zts_pollfd *)jarg1; - arg2 = (zts_nfds_t)jarg2; - arg3 = (int)jarg3; - result = (int)zts_poll(arg1,arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_ioctl(int jarg1, unsigned long jarg2, void * jarg3) { - int jresult; - int arg1; - unsigned long arg2; - void *arg3 = (void *) 0; - int result; - - arg1 = (int)jarg1; - arg2 = (unsigned long)jarg2; - arg3 = (void *)jarg3; - result = (int)zts_ioctl(arg1,arg2,arg3); - jresult = result; - return jresult; -} - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_send(int jarg1, void * jarg2, unsigned long jarg3, int jarg4) { - int arg1; - void *arg2 = (void *) 0; - size_t arg3; - int arg4; - ssize_t result; - - arg1 = (int)jarg1; - arg2 = (void *)jarg2; - arg3 = (size_t)jarg3; - arg4 = (int)jarg4; - result = zts_send(arg1,(void const *)arg2,arg3,arg4); - return result; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_sendto(int jarg1, void * jarg2, unsigned long jarg3, int jarg4, zts_sockaddr* jarg5, unsigned short jarg6) { - int arg1; - void *arg2 = (void *) 0; - size_t arg3; - int arg4; - zts_sockaddr *arg5 = (zts_sockaddr *) 0; - zts_socklen_t arg6; - ssize_t result; - - arg1 = (int)jarg1; - arg2 = (void *)jarg2; - arg3 = (size_t)jarg3; - arg4 = (int)jarg4; - arg5 = (zts_sockaddr *)jarg5; - arg6 = (zts_socklen_t)jarg6; - result = zts_sendto(arg1,(void const *)arg2,arg3,arg4,(zts_sockaddr const *)arg5,arg6); - return result; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_sendmsg(int jarg1, void * jarg2, int jarg3) { - int arg1; - zts_msghdr *arg2 = (zts_msghdr *) 0; - int arg3; - ssize_t result; - - arg1 = (int)jarg1; - arg2 = (zts_msghdr *)jarg2; - arg3 = (int)jarg3; - result = zts_sendmsg(arg1,(zts_msghdr const *)arg2,arg3); - return result; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_recv(int jarg1, void * jarg2, unsigned long jarg3, int jarg4) { - int arg1; - void *arg2 = (void *) 0; - size_t arg3; - int arg4; - ssize_t result; - - arg1 = (int)jarg1; - arg2 = (void *)jarg2; - arg3 = (size_t)jarg3; - arg4 = (int)jarg4; - result = zts_recv(arg1,arg2,arg3,arg4); - return result; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_recvfrom(int jarg1, void * jarg2, unsigned long jarg3, int jarg4, zts_sockaddr* jarg5, void * jarg6) { - int arg1; - void *arg2 = (void *) 0; - size_t arg3; - int arg4; - zts_sockaddr *arg5 = (zts_sockaddr *) 0; - zts_socklen_t *arg6 = (zts_socklen_t *) 0; - ssize_t result; - - arg1 = (int)jarg1; - arg2 = (void *)jarg2; - arg3 = (size_t)jarg3; - arg4 = (int)jarg4; - arg5 = (zts_sockaddr *)jarg5; - arg6 = (zts_socklen_t *)jarg6; - result = zts_recvfrom(arg1,arg2,arg3,arg4,arg5,arg6); - return result; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_recvmsg(int jarg1, void * jarg2, int jarg3) { - int arg1; - zts_msghdr *arg2 = (zts_msghdr *) 0; - int arg3; - ssize_t result; - - arg1 = (int)jarg1; - arg2 = (zts_msghdr *)jarg2; - arg3 = (int)jarg3; - result = zts_recvmsg(arg1,arg2,arg3); - return result; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_read(int jarg1, void * jarg2, unsigned long jarg3) { - int arg1; - void *arg2 = (void *) 0; - size_t arg3; - ssize_t result; - - arg1 = (int)jarg1; - arg2 = (void *)jarg2; - arg3 = (size_t)jarg3; - result = zts_read(arg1,arg2,arg3); - return result; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_readv(int jarg1, void * jarg2, int jarg3) { - int arg1; - zts_iovec *arg2 = (zts_iovec *) 0; - int arg3; - ssize_t result; - - arg1 = (int)jarg1; - arg2 = (zts_iovec *)jarg2; - arg3 = (int)jarg3; - result = zts_readv(arg1,(zts_iovec const *)arg2,arg3); - return result; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_write(int jarg1, void * jarg2, unsigned long jarg3) { - int arg1; - void *arg2 = (void *) 0; - size_t arg3; - ssize_t result; - - arg1 = (int)jarg1; - arg2 = (void *)jarg2; - arg3 = (size_t)jarg3; - result = zts_write(arg1,(void const *)arg2,arg3); - return result; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_writev(int jarg1, void * jarg2, int jarg3) { - int arg1; - zts_iovec *arg2 = (zts_iovec *) 0; - int arg3; - ssize_t result; - - arg1 = (int)jarg1; - arg2 = (zts_iovec *)jarg2; - arg3 = (int)jarg3; - result = zts_writev(arg1,(zts_iovec const *)arg2,arg3); - return result; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_shutdown(int jarg1, int jarg2) { - int jresult; - int arg1; - int arg2; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - result = (int)zts_shutdown(arg1,arg2); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_set_no_delay(int jarg1, int jarg2) { - int jresult; - int arg1; - int arg2; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - result = (int)zts_set_no_delay(arg1,arg2); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_no_delay(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_get_no_delay(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_set_linger(int jarg1, int jarg2, int jarg3) { - int jresult; - int arg1; - int arg2; - int arg3; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - arg3 = (int)jarg3; - result = (int)zts_set_linger(arg1,arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_linger_enabled(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_get_linger_enabled(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_linger_value(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_get_linger_value(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_set_reuse_addr(int jarg1, int jarg2) { - int jresult; - int arg1; - int arg2; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - result = (int)zts_set_reuse_addr(arg1,arg2); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_reuse_addr(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_get_reuse_addr(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_set_recv_timeout(int jarg1, int jarg2, int jarg3) { - int jresult; - int arg1; - int arg2; - int arg3; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - arg3 = (int)jarg3; - result = (int)zts_set_recv_timeout(arg1,arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_recv_timeout(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_get_recv_timeout(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_set_send_timeout(int jarg1, int jarg2, int jarg3) { - int jresult; - int arg1; - int arg2; - int arg3; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - arg3 = (int)jarg3; - result = (int)zts_set_send_timeout(arg1,arg2,arg3); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_send_timeout(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_get_send_timeout(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_set_send_buf_size(int jarg1, int jarg2) { - int jresult; - int arg1; - int arg2; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - result = (int)zts_set_send_buf_size(arg1,arg2); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_send_buf_size(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_get_send_buf_size(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_set_recv_buf_size(int jarg1, int jarg2) { - int jresult; - int arg1; - int arg2; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - result = (int)zts_set_recv_buf_size(arg1,arg2); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_recv_buf_size(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_get_recv_buf_size(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_set_ttl(int jarg1, int jarg2) { - int jresult; - int arg1; - int arg2; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - result = (int)zts_set_ttl(arg1,arg2); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_ttl(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_get_ttl(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_set_blocking(int jarg1, int jarg2) { - int jresult; - int arg1; - int arg2; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - result = (int)zts_set_blocking(arg1,arg2); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_blocking(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_get_blocking(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_set_keepalive(int jarg1, int jarg2) { - int jresult; - int arg1; - int arg2; - int result; - - arg1 = (int)jarg1; - arg2 = (int)jarg2; - result = (int)zts_set_keepalive(arg1,arg2); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_get_keepalive(int jarg1) { - int jresult; - int arg1; - int result; - - arg1 = (int)jarg1; - result = (int)zts_get_keepalive(arg1); - jresult = result; - return jresult; -} - -/* -SWIGEXPORT int SWIGSTDCALL CSharp_zts_dns_set_server(zts_sockaddr* jarg1) { - int jresult; - zts_sockaddr *arg1 = (zts_sockaddr *) 0; - int result; - - arg1 = (zts_sockaddr *)jarg1; - result = (int)zts_add_dns_nameserver(arg1); - jresult = result; - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_dns_get_server(zts_sockaddr* jarg1) { - int jresult; - zts_sockaddr *arg1 = (zts_sockaddr *) 0; - int result; - - arg1 = (zts_sockaddr *)jarg1; - result = (int)zts_del_dns_nameserver(arg1); - jresult = result; - return jresult; -} -*/ - - -SWIGEXPORT char * SWIGSTDCALL CSharp_zts_inet_ntop(int jarg1, void * jarg2, char * jarg3, unsigned short jarg4) { - char * jresult; - int arg1; - void *arg2 = (void *) 0; - char *arg3 = (char *) 0; - zts_socklen_t arg4; - char *result = 0; - - arg1 = (int)jarg1; - arg2 = (void *)jarg2; - arg3 = (char *)jarg3; - arg4 = (zts_socklen_t)jarg4; - result = (char *)zts_inet_ntop(arg1,(void const *)arg2,arg3,arg4); - jresult = SWIG_csharp_string_callback((const char *)result); - return jresult; -} - - -SWIGEXPORT int SWIGSTDCALL CSharp_zts_inet_pton(int jarg1, char * jarg2, void * jarg3) { - int jresult; - int arg1; - char *arg2 = (char *) 0; - void *arg3 = (void *) 0; - int result; - - arg1 = (int)jarg1; - arg2 = (char *)jarg2; - arg3 = (void *)jarg3; - result = (int)zts_inet_pton(arg1,(char const *)arg2,arg3); - jresult = result; - return jresult; -} - -#ifdef __cplusplus -} -#endif - diff --git a/test/selftest.cs b/test/selftest.cs new file mode 100644 index 0000000..7722aaf --- /dev/null +++ b/test/selftest.cs @@ -0,0 +1,266 @@ + +using System; +using System.Threading; +using System.Net; +using System.Net.Sockets; +using System.Text; + +using ZeroTier; + +public class ExampleApp { + // ZeroTier Node instance + + ZeroTier.Core.Node node; + + // Initialize and start ZeroTier + + public void StartZeroTier(string configFilePath, ulong networkId) + { + node = new ZeroTier.Core.Node(); + + // (OPTIONAL) Initialize node + + node.InitFromStorage(configFilePath); + node.InitAllowNetworkCaching(false); + node.InitAllowPeerCaching(true); + // node.InitAllowIdentityCaching(true); + // node.InitAllowWorldCaching(false); + node.InitSetEventHandler(OnZeroTierEvent); + node.InitSetPort(0); // Will randomly attempt ports if set to 0 + + // (OPTIONAL) Set custom signed roots + + // In this case we only allow ZeroTier to contact our Amsterdam root server + // To see examples of how to generate and sign roots definitions see docs.zerotier.com + + var rootsData = new byte[] { + 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0xea, 0xc9, 0x0a, 0x00, 0x00, 0x01, 0x6c, 0xe3, 0xe2, 0x39, 0x55, 0x74, + 0xeb, 0x27, 0x9d, 0xc9, 0xe7, 0x5a, 0x52, 0xbb, 0x91, 0x8f, 0xf7, 0x43, 0x3c, 0xbf, 0x77, 0x5a, 0x4b, 0x57, + 0xb4, 0xe1, 0xe9, 0xa1, 0x01, 0x61, 0x3d, 0x25, 0x35, 0x60, 0xcb, 0xe3, 0x30, 0x18, 0x1e, 0x6e, 0x44, 0xef, + 0x93, 0x89, 0xa0, 0x19, 0xb8, 0x7b, 0x36, 0x0b, 0x92, 0xff, 0x0f, 0x1b, 0xbe, 0x56, 0x5a, 0x46, 0x91, 0x36, + 0xf1, 0xd4, 0x5c, 0x09, 0x05, 0xe5, 0xf5, 0xfb, 0xba, 0xe8, 0x13, 0x2d, 0x47, 0xa8, 0xe4, 0x1b, 0xa5, 0x1c, + 0xcf, 0xb0, 0x2f, 0x27, 0x7e, 0x95, 0xa0, 0xdd, 0x49, 0xe1, 0x7d, 0xc0, 0x7e, 0x6d, 0xe3, 0x25, 0x91, 0x96, + 0xc2, 0x55, 0xf9, 0x20, 0x6d, 0x2a, 0x5e, 0x1b, 0x41, 0xcb, 0x1f, 0x8d, 0x57, 0x27, 0x69, 0x3e, 0xcc, 0x7f, + 0x0b, 0x36, 0x54, 0x6b, 0xd3, 0x80, 0x78, 0xf6, 0xd0, 0xec, 0xb4, 0x31, 0x6b, 0x87, 0x1b, 0x50, 0x08, 0xe4, + 0x0b, 0xa9, 0xd4, 0xfd, 0x37, 0x79, 0x14, 0x6a, 0xf5, 0x12, 0xf2, 0x45, 0x39, 0xca, 0x23, 0x00, 0x39, 0xbc, + 0xa3, 0x1e, 0xa8, 0x4e, 0x23, 0x2d, 0xc8, 0xdb, 0x9b, 0x0e, 0x52, 0x1b, 0x8d, 0x02, 0x72, 0x01, 0x99, 0x2f, + 0xcf, 0x1d, 0xb7, 0x00, 0x20, 0x6e, 0xd5, 0x93, 0x50, 0xb3, 0x19, 0x16, 0xf7, 0x49, 0xa1, 0xf8, 0x5d, 0xff, + 0xb3, 0xa8, 0x78, 0x7d, 0xcb, 0xf8, 0x3b, 0x8c, 0x6e, 0x94, 0x48, 0xd4, 0xe3, 0xea, 0x0e, 0x33, 0x69, 0x30, + 0x1b, 0xe7, 0x16, 0xc3, 0x60, 0x93, 0x44, 0xa9, 0xd1, 0x53, 0x38, 0x50, 0xfb, 0x44, 0x60, 0xc5, 0x0a, 0xf4, + 0x33, 0x22, 0xbc, 0xfc, 0x8e, 0x13, 0xd3, 0x30, 0x1a, 0x1f, 0x10, 0x03, 0xce, 0xb6, 0x00, 0x02, 0x04, 0xc3, + 0xb5, 0xad, 0x9f, 0x27, 0x09, 0x06, 0x2a, 0x02, 0x6e, 0xa0, 0xc0, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x27, 0x09 + }; + node.InitSetRoots(rootsData, rootsData.Length); + + node.Start(); // Network activity only begins after calling Start() + while (! node.Online) { + Thread.Sleep(50); + } + + Console.WriteLine("Id : " + node.IdString); + Console.WriteLine("Version : " + node.Version); + Console.WriteLine("PrimaryPort : " + node.PrimaryPort); + Console.WriteLine("SecondaryPort : " + node.SecondaryPort); + Console.WriteLine("TertiaryPort : " + node.TertiaryPort); + + node.Join(networkId); + Console.WriteLine("Waiting for join to complete..."); + while (node.Networks.Count == 0) { + Thread.Sleep(50); + } + + // Wait until we've joined the network and we have routes + addresses + Console.WriteLine("Waiting for network to become transport ready..."); + while (! node.IsNetworkTransportReady(networkId)) { + Thread.Sleep(50); + } + + Console.WriteLine("Num of assigned addresses : " + node.GetNetworkAddresses(networkId).Count); + foreach (IPAddress addr in node.GetNetworkAddresses(networkId)) { + Console.WriteLine(" - Address: " + addr); + } + + Console.WriteLine("Num of routes : " + node.GetNetworkRoutes(networkId).Count); + foreach (ZeroTier.Core.RouteInfo route in node.GetNetworkRoutes(networkId)) { + Console.WriteLine( + " - Route: target={0} via={1} flags={2} metric={3}", + route.Target.ToString(), + route.Via.ToString(), + route.Flags, + route.Metric); + } + } + + /** + * Stop ZeroTier + */ + public void StopZeroTier() + { + node.Free(); + } + + /** + * (OPTIONAL) + * + * Your application should process event messages and return control as soon as possible. Blocking + * or otherwise time-consuming operations are not recommended here. + */ + public void OnZeroTierEvent(ZeroTier.Core.Event e) + { + Console.WriteLine("Event.Code = {0} ({1})", e.Code, e.Name); + /* + if (e.Code == ZeroTier.Constants.EVENT_NODE_ONLINE) { + Console.WriteLine("Node is online"); + Console.WriteLine(" - Address (NodeId): " + node.Id.ToString("x16")); + } + + if (e.Code == ZeroTier.Constants.EVENT_NETWORK_OK) { + Console.WriteLine(" - Network ID: " + e.NetworkInfo.Id.ToString("x16")); + } + */ + } + + /** + * Example server + */ + public void SocketServer(IPEndPoint localEndPoint) + { + string data = null; + + // Data buffer for incoming data. + byte[] bytes = new Byte[1024]; + + Console.WriteLine(localEndPoint.ToString()); + ZeroTier.Sockets.Socket listener = + new ZeroTier.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + // Bind the socket to the local endpoint and + // listen for incoming connections. + + try { + listener.Bind(localEndPoint); + listener.Listen(10); + ZeroTier.Sockets.Socket handler; + Console.WriteLine("Server: Accepting..."); + handler = listener.Accept(); + + data = null; + Console.WriteLine("Server: Accepted connection from: " + handler.RemoteEndPoint.ToString()); + + for (int i = 0; i < 4; i++) { + int bytesRec = 0; + try { + Console.WriteLine("Server: Receiving..."); + bytesRec = handler.Receive(bytes); + } + catch (ZeroTier.Sockets.SocketException e) { + Console.WriteLine( + "ServiceErrorCode={0} SocketErrorCode={1}", + e.ServiceErrorCode, + e.SocketErrorCode); + } + if (bytesRec > 0) { + Console.WriteLine("Server: Bytes received: {0}", bytesRec); + data = Encoding.ASCII.GetString(bytes, 0, bytesRec); + Console.WriteLine("Server: Text received : {0}", data); + Thread.Sleep(1000); + // Echo the data back to the client. + byte[] msg = Encoding.ASCII.GetBytes(data); + handler.Send(msg); + Thread.Sleep(1000); + } + } + // Release the socket. + handler.Shutdown(SocketShutdown.Both); + handler.Close(); + } + catch (ZeroTier.Sockets.SocketException e) { + Console.WriteLine(e); + Console.WriteLine("ServiceErrorCode={0} SocketErrorCode={1}", e.ServiceErrorCode, e.SocketErrorCode); + } + } + + /** + * Example client + */ + public void SocketClient(IPEndPoint remoteServerEndPoint) + { + // Data buffer for incoming data. + byte[] bytes = new byte[1024]; + + // Connect to a remote device. + try { + // Create a TCP/IP socket. + ZeroTier.Sockets.Socket sender = + new ZeroTier.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + + try { + Console.WriteLine("Client: Connecting to {0}...", remoteServerEndPoint.ToString()); + sender.Connect(remoteServerEndPoint); + Console.WriteLine("Client: Connected to {0}", sender.RemoteEndPoint.ToString()); + + // Encode the data string into a byte array. + for (int i = 0; i < 4; i++) { + byte[] msg = Encoding.ASCII.GetBytes("This is a test"); + int bytesSent = sender.Send(msg); + Console.WriteLine("Client: Sent ({0}) bytes", bytesSent); + Thread.Sleep(1000); + int bytesRec = sender.Receive(bytes); + Console.WriteLine("Client: Echoing {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec)); + Thread.Sleep(1000); + } + // Release the socket. + sender.Shutdown(SocketShutdown.Both); + sender.Close(); + } + catch (ArgumentNullException ane) { + Console.WriteLine("ArgumentNullException : {0}", ane.ToString()); + } + catch (ZeroTier.Sockets.SocketException e) { + Console.WriteLine(e); + Console.WriteLine("ServiceErrorCode={0} SocketErrorCode={1}", e.ServiceErrorCode, e.SocketErrorCode); + } + } + catch (Exception e) { + Console.WriteLine(e.ToString()); + } + } +} + +public class example { + static int Main(string[] args) + { + if (args.Length < 4 || args.Length > 5) { + Console.WriteLine("\nPlease specify either client or server mode and required arguments:"); + Console.WriteLine(" Usage: example server "); + Console.WriteLine(" Usage: example client \n"); + return 1; + } + string configFilePath = args[1]; + ulong networkId = (ulong)Int64.Parse(args[2], System.Globalization.NumberStyles.HexNumber); + + ExampleApp exampleApp = new ExampleApp(); + + if (args[0].Equals("server")) { + Console.WriteLine("Server mode..."); + ushort serverPort = (ushort)Int16.Parse(args[3]); + exampleApp.StartZeroTier(configFilePath, networkId); + IPAddress ipAddress = IPAddress.Parse("0.0.0.0"); + IPEndPoint localEndPoint = new IPEndPoint(ipAddress, serverPort); + exampleApp.SocketServer(localEndPoint); + } + + if (args[0].Equals("client")) { + Console.WriteLine("Client mode..."); + string serverIP = args[3]; + int port = Int16.Parse(args[4]); + IPAddress ipAddress = IPAddress.Parse(serverIP); + IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, port); + exampleApp.StartZeroTier(configFilePath, networkId); + exampleApp.SocketClient(remoteEndPoint); + } + exampleApp.StopZeroTier(); + return 0; + } +}