summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJoseph Henry <[email protected]>2021-04-30 21:54:34 -0700
committerJoseph Henry <[email protected]>2021-04-30 21:54:34 -0700
commit6f42338f6e56a8d9410eb5ac95097828dedb9a1a (patch)
tree2828150dfeb8cec75534482f63436d65d20a1d0b /examples
parent3e304cb25ab90d3009d8ede54fa6de4c1c8b33e2 (diff)
Update C# bindings to 1.4.0 API
Diffstat (limited to 'examples')
-rw-r--r--examples/csharp/example.cs512
1 files changed, 256 insertions, 256 deletions
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
-
-/**
- *
- * 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 System.Net.Sockets;
+using System.Text;
+
using ZeroTier;
public class ExampleApp {
-
- 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()
-
- /* 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); }
-
- /* 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);
-
- /* 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); }
- }
-
- /**
- * Stop ZeroTier
- */
- public void StopZeroTier()
- {
- node.Stop();
- }
-
- /**
- * 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);
-
- if (e.EventCode == ZeroTier.Constants.EVENT_NODE_ONLINE) {
- Console.WriteLine("Node is online");
- Console.WriteLine(" - Address (NodeId): " + node.NodeId.ToString("x16"));
- }
-
- if (e.EventCode == ZeroTier.Constants.EVENT_NETWORK_OK) {
- Console.WriteLine(" - Network ID: " + e.networkDetails.networkId.ToString("x16"));
- }
- }
-
- /**
- * Example server
- */
- public void YourServer(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);
-
- // Start listening for connections.
- while (true) {
- Console.WriteLine("Waiting for a connection...");
- // Program is suspended while waiting for an incoming connection.
- bool nonblocking = true;
-
- ZeroTier.Sockets.Socket handler;
-
- 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());
-
- // handler.ReceiveTimeout = 1000;
-
- // 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...");
- }
- }
-
- handler.Shutdown(SocketShutdown.Both);
- handler.Close();
- }
-
- } catch (ZeroTier.Sockets.SocketException e) {
- Console.WriteLine(e);
- Console.WriteLine("ServiceErrorCode={0} SocketErrorCode={1}",
- e.ServiceErrorCode, e.SocketErrorCode);
- }
-
- Console.WriteLine("\nPress ENTER to continue...");
- Console.Read();
- }
-
- /**
- * Example client
- */
- public void YourClient(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 );
-
- // Connect the socket to the remote endpoint. Catch any errors.
- try {
- Console.WriteLine("Socket connecting to {0}...",
- remoteServerEndPoint.ToString());
-
- sender.Connect(remoteServerEndPoint);
-
- Console.WriteLine("Socket 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());
- }
- }
+ // 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, 0x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 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 < 5 || args.Length > 6)
- {
- Console.WriteLine("\nPlease specify either client or server mode and required arguments:");
- Console.WriteLine(" Usage: example server <config_path> <ztServicePort> <nwid> <serverPort>");
- Console.WriteLine(" Usage: example client <config_path> <ztServicePort> <nwid> <remoteServerIp> <remoteServerPort>\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);
-
- 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("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;
- }
+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 <config_path> <nwid> <localPort>");
+ Console.WriteLine(" Usage: example client <config_path> <nwid> <remoteAddress> <remotePort>\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;
+ }
}
-