diff --git a/integrations/Unity3D/Assets/MainScene.unity b/integrations/Unity3D/Assets/MainScene.unity index cf749dc..ec9e829 100644 Binary files a/integrations/Unity3D/Assets/MainScene.unity and b/integrations/Unity3D/Assets/MainScene.unity differ diff --git a/integrations/Unity3D/Assets/MyZeroTier.cs b/integrations/Unity3D/Assets/MyZeroTier.cs old mode 100644 new mode 100755 diff --git a/integrations/Unity3D/Assets/WorldMain.cs b/integrations/Unity3D/Assets/WorldMain.cs old mode 100644 new mode 100755 index 5f28270..f32e06f --- a/integrations/Unity3D/Assets/WorldMain.cs +++ b/integrations/Unity3D/Assets/WorldMain.cs @@ -1 +1,45 @@ - \ No newline at end of file +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +using UnityEngine; +using System.Collections; +using UnityEngine.UI; +using System; + +public class WorldMain : MonoBehaviour { + void Start() { + } + + public void API_selection() + { + GameObject go = GameObject.Find ("dropdownAPI"); + Dropdown dd = go.GetComponents () [0]; + Debug.Log("API selected: " + dd.captionText.text); + } + + +} diff --git a/integrations/Unity3D/Assets/ZT.mat b/integrations/Unity3D/Assets/ZT.mat old mode 100644 new mode 100755 diff --git a/integrations/Unity3D/Assets/ZerTierLLAPI_Demo.cs b/integrations/Unity3D/Assets/ZerTierLLAPI_Demo.cs new file mode 100755 index 0000000..70273bc --- /dev/null +++ b/integrations/Unity3D/Assets/ZerTierLLAPI_Demo.cs @@ -0,0 +1,214 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +using UnityEngine; +using System.Collections; +using System.Threading; +using System.Runtime.Serialization.Formatters.Binary; +using System.IO; +using UnityEngine.UI; +using UnityEngine.Networking; + +// Demonstrates the usage of the ZeroTier LLAPI (modeled after the Unity LLAPI) +public class ZeroTierLLAPI_Demo : MonoBehaviour +{ + public float speed = 300f; + + private ZeroTierLLAPI zt; + string nwid = ""; + + int server_connection_socket; // The "connection id" + int host_socket; + + // Demo button methods + public void Join() + { + GameObject go = GameObject.Find ("inputNetworkID"); + InputField input = go.GetComponents () [0]; + Debug.Log ("Joining: " + input.text); + zt.JoinNetwork (input.text); + } + + public void Leave() + { + GameObject go = GameObject.Find ("inputNetworkID"); + InputField input = go.GetComponents () [0]; + Debug.Log ("Leaving: " + input.text); + zt.LeaveNetwork (input.text); + } + + public void Connect() + { + GameObject addr_go = GameObject.Find ("inputServerAddress"); + GameObject port_go = GameObject.Find ("inputServerPort"); + InputField addr = addr_go.GetComponents () [0]; + InputField port = port_go.GetComponents () [0]; + Debug.Log ("Connecting to: " + addr.text + ":" + port.text); + + Thread connectThread = new Thread(() => { + byte error = 0; + server_connection_socket = zt.Connect (0, addr.text, int.Parse (port.text), out error); + Debug.Log ("server_connection_socket = " + server_connection_socket); + Debug.Log ("Connect(): " + error); + }); + connectThread.IsBackground = true; + connectThread.Start(); + } + + public void Bind() + { + GameObject addr_go = GameObject.Find ("inputServerAddress"); + GameObject port_go = GameObject.Find ("inputServerPort"); + InputField addr = addr_go.GetComponents () [0]; + InputField port = port_go.GetComponents () [0]; + Debug.Log ("Binding to: " + addr.text + ":" + port.text); + + Thread connectThread = new Thread(() => { + byte error = 0; + host_socket = zt.AddHost (int.Parse (port.text)); + Debug.Log ("host_socket = " + host_socket); + Debug.Log ("Bind(): " + error); + }); + connectThread.IsBackground = true; + connectThread.Start(); + } + + public void Disconnect() + { + GameObject addr_go = GameObject.Find ("inputServerAddress"); + GameObject port_go = GameObject.Find ("inputServerAddress"); + InputField addr = addr_go.GetComponents () [0]; + InputField port = port_go.GetComponents () [0]; + Debug.Log ("Disconnecting from: " + addr.text + ":" + port.text); + Debug.Log ("Disconnect(): " + zt.Disconnect (server_connection_socket)); + } + + public void SendMessage() + { + //zt_test_network (); + GameObject go = GameObject.Find ("inputMessage"); + InputField msg = go.GetComponents () [0]; + + Thread sendThread = new Thread(() => { + Debug.Log ("Sending Message: " + msg.text); + byte error = 0; + zt.Send (server_connection_socket, msg.text.ToCharArray (), msg.text.ToCharArray ().Length, out error); + Debug.Log ("Send(): " + error); + }); + sendThread.IsBackground = true; + sendThread.Start(); + + } + + void Start() + { + // Set defaults + InputField input; + GameObject go; + go = GameObject.Find ("inputNetworkID"); + input = go.GetComponents () [0]; + input.text = "565799d8f6e1c11a"; + go = GameObject.Find ("inputServerAddress"); + input = go.GetComponents () [0]; + input.text = "172.22.211.245"; + go = GameObject.Find ("inputServerPort"); + input = go.GetComponents () [0]; + input.text = "8887"; + go = GameObject.Find ("inputMessage"); + input = go.GetComponents () [0]; + input.text = "Welcome to the machine"; + + // Create new instance of ZeroTier in separate thread + zt = new ZeroTierLLAPI ("/Users/Joseph/utest2/nc_565799d8f6e1c11a"); + + /* This new instance will communicate via a named pipe, so any + * API calls (ZeroTier.Connect(), ZeroTier.Send(), etc) will be sent to the service + * via this pipe. + */ + } + + // Terminate the ZeroTier service when the application quits + void OnApplicationQuit() { + Debug.Log ("OnApplicationQuit()"); + zt.Terminate (); + } + + // Update is called once per frame + void Update () { + /* + if (text) { + text.text = IsRunning() ? "ZeroTier Status: Online" : "ZeroTier Status: Offline"; + } + */ + + // --- + + int recHostId; + int connectionId; + int channelId; + byte[] recBuffer = new byte[1024]; + int bufferSize = 1024; + int dataSize; + byte error; + NetworkEventType recData = zt.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error); + switch (recData) + { + case NetworkEventType.Nothing: + break; + case NetworkEventType.ConnectEvent: + Debug.Log("NetworkEventType.ConnectEvent"); + break; + case NetworkEventType.DataEvent: + Debug.Log("NetworkEventType.DataEvent"); + break; + case NetworkEventType.DisconnectEvent: + Debug.Log("NetworkEventType.DisconnectEvent"); + break; + } + + // --- + + /* + GameObject go = GameObject.Find ("_txtStatusIndicator"); + Text text = go.GetComponents () [0]; + text.text = zt.IsRunning () ? "ZeroTier Service: ONLINE" : "ZeroTier Service: OFFLINE"; + */ + + // Rotate ZTCube when ZT is running + /* + if (zt.IsRunning ()) { + + + go = GameObject.Find ("ZTCube"); + Vector3 rotvec = new Vector3 (10f, 10f, 10f); + go.transform.Rotate (rotvec, speed * Time.deltaTime); + } + GameObject go = GameObject.Find("ZTCube"); + Text text = go.GetComponents ()[0]; + */ + } +} \ No newline at end of file diff --git a/integrations/Unity3D/Assets/ZeroTierLLAPI.cs b/integrations/Unity3D/Assets/ZeroTierLLAPI.cs new file mode 100644 index 0000000..13eb213 --- /dev/null +++ b/integrations/Unity3D/Assets/ZeroTierLLAPI.cs @@ -0,0 +1,175 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +using UnityEngine; +using System.Collections; +using System.Threading; +using System.Net.Sockets; +using System.Runtime.InteropServices; +using System; +using UnityEngine.Networking; + +// Provides a familiar interface which is modeled after the Unity LLAPI +public class ZeroTierLLAPI : ZeroTierNetworkInterface { + + // Initialize the ZeroTier service with a given path + public ZeroTierLLAPI(string path) + { + rpc_path = path; + Init(); + } + + // Creates a ZeroTier Socket and binds on the port provided + // A client instance can now connect to this "host" + public int AddHost(int port) + { + int sockfd = zt_socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified); + if (sockfd < 0) { + return -1; + } + GCHandle sockaddr_ptr = ZeroTierUtils.Generate_unmananged_sockaddr("0.0.0.0" + ":" + port); + IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject (); + int addrlen = Marshal.SizeOf (pSockAddr); + // Set socket to non-blocking for RX polling in Receive() + zt_set_nonblock(sockfd); + return zt_bind (sockfd, pSockAddr, addrlen); + } + + // hostId - host socket ID for this connection + public int Connect(int hostId, string address, int port, out byte error) + { + int sockfd = zt_socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified); + Debug.Log ("sockfd = " + sockfd); + + if (sockfd < 0) { + error = (byte)sockfd; + return -1; + } + GCHandle sockaddr_ptr = ZeroTierUtils.Generate_unmananged_sockaddr(address + ":" + port); + IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject (); + int addrlen = Marshal.SizeOf (pSockAddr); + error = (byte)zt_connect (sockfd, pSockAddr, addrlen); + + // Set socket to non-blocking for RX polling in Receive() + zt_set_nonblock(sockfd); + return sockfd; + } + + // Write data to a ZeroTier socket + public int Send(int fd, char[] buf, int len, out byte error) + { + error = 0; + return Write(fd, buf, len); + } + + // Checks for data to RX + /* + public enum NetworkEventType + { + DataEvent, + ConnectEvent, + DisconnectEvent, + Nothing, + BroadcastEvent + } + */ + public NetworkEventType Receive(out int hostId, out int connectionId, out int channelId, byte[] buffer, int bufferSize, out int receivedSize, out byte error) + { + hostId = 0; + connectionId = 0; + channelId = 0; + receivedSize = 0; + error = 0; + + + // Read() ... + + /* + * If recBuffer is big enough to contain data, data will be copied in the buffer. + * If not, error will contain MessageToLong error and you will need reallocate + * buffer and call this function again. */ + + //for (int i = 0; i < connections.Count; i++) { + + //} + + /* + int res; + res = zt_recv (connectionId, buffer, bufferSize); + + // FIXME: Not quite semantically the same, but close enough for alpha release? + // FIXME: Get notifications of disconnect events? + if (res == -1) { + error = -1; + return NetworkEventType.DisconnectEvent; + } + if(res == 0) { + error = 0; + return NetworkEventType.Nothing; // No data read + } + if (res > 0) { + receivedSize = res; + Marshal.Copy(buffer, buffer, 0, res); + return NetworkEventType.DataEvent; // Data read into buffer + } + */ + + return NetworkEventType.Nothing; + } + + // Shutdown a given connection + public int Disconnect(int fd) + { + return zt_close (fd); + } + + // Test serialization methods, should be removed for production + + public static byte[] RawSerializeEx( object anything ) + { + int rawsize = Marshal.SizeOf( anything ); + byte[] rawdatas = new byte[ rawsize ]; + GCHandle handle = GCHandle.Alloc( rawdatas, GCHandleType.Pinned ); + IntPtr buffer = handle.AddrOfPinnedObject(); + Marshal.StructureToPtr( anything, buffer, false ); + handle.Free(); + return rawdatas; + } + + + public static object RawDeserializeEx( byte[] rawdatas, Type anytype ) + { + int rawsize = Marshal.SizeOf( anytype ); + if( rawsize > rawdatas.Length ) + return null; + GCHandle handle = GCHandle.Alloc( rawdatas, GCHandleType.Pinned ); + IntPtr buffer = handle.AddrOfPinnedObject(); + object retobj = Marshal.PtrToStructure( buffer, anytype ); + handle.Free(); + return retobj; + } +} diff --git a/integrations/Unity3D/Assets/ZeroTierSockets.cs b/integrations/Unity3D/Assets/ZeroTierSockets.cs new file mode 100755 index 0000000..cbc9a41 --- /dev/null +++ b/integrations/Unity3D/Assets/ZeroTierSockets.cs @@ -0,0 +1,305 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +using UnityEngine; +using UnityEngine.UI; +using UnityEngine.Networking; + +using System; +using System.Collections; +using System.Runtime.InteropServices; +using System.Threading; +using System.Net.Sockets; +using System.Net; +using System.IO; +using System.Collections.Generic; + +// Provides a bare-bones interface to ZeroTier-administered sockets +public class ZeroTierNetworkInterface { + + // Apple + #if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX + const string DLL_PATH = "ZeroTierSDK_Unity3D_OSX"; + #endif + + #if UNITY_IOS || UNITY_IPHONE + const string DLL_PATH = "ZeroTierSDK_Unity3D_iOS"; + #endif + + // Windows + #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN + const string DLL_PATH = "ZeroTierSDK_Unity3D_WIN"; + #endif + + // Linux + #if UNITY_STANDALONE_LINUX + const string DLL_PATH = "ZeroTierSDK_Unity3D_LINUX"; + #endif + + // Android + #if UNITY_ANDROID + const string DLL_PATH = "ZeroTierSDK_Unity3D_ANDROID"; + #endif + + // Interop structures + [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)] + public struct sockaddr { + /// u_short->unsigned short + public ushort sa_family; + /// char[14] + [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=14)] + public string sa_data; + } + + // ZeroTier background thread + protected Thread ztThread; + protected List connections = new List (); + protected int MaxPacketSize; + protected string rpc_path = ""; + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate void MyDelegate(string str); + + static void CallBackFunction(string str) { + Debug.Log("Native ZT Plugin: " + str); + } + +#region DLL Imports + // ZeroTier service / debug initialization + [DllImport (DLL_PATH)] + public static extern void SetDebugFunction( IntPtr fp ); + [DllImport (DLL_PATH)] + private static extern int unity_start_service(string path); + + // Connection calls + [DllImport (DLL_PATH)] + protected static extern int zt_socket(int family, int type, int protocol); + + [DllImport (DLL_PATH)] + unsafe protected static extern int zt_bind(int sockfd, System.IntPtr addr, int addrlen); + [DllImport (DLL_PATH)] + unsafe protected static extern int zt_connect(int sockfd, System.IntPtr addr, int addrlen); + + [DllImport (DLL_PATH)] + protected static extern int zt_accept(int sockfd); + [DllImport (DLL_PATH)] + protected static extern int zt_listen(int sockfd, int backlog); + [DllImport (DLL_PATH)] + protected static extern int zt_close(int sockfd); + + // RX / TX + [DllImport (DLL_PATH)] + unsafe protected static extern int zt_recv(int sockfd, IntPtr buf, int len); + [DllImport (DLL_PATH)] + unsafe protected static extern int zt_send(int sockfd, IntPtr buf, int len); + [DllImport (DLL_PATH)] + unsafe protected static extern int zt_set_nonblock(int sockfd); + + // ZT Thread controls + [DllImport (DLL_PATH)] + protected static extern bool zt_is_running(); + [DllImport (DLL_PATH)] + protected static extern void zt_terminate(); + + // ZT Network controls + [DllImport (DLL_PATH)] + protected static extern bool zt_join_network(string nwid); + [DllImport (DLL_PATH)] + protected static extern void zt_leave_network(string nwid); +#endregion + + // Thread which starts the ZeroTier service + // The ZeroTier service may spin off a SOCKS5 proxy server + // if -DUSE_SOCKS_PROXY is set when building the bundle + protected void zt_service_thread() + { + MyDelegate callback_delegate = new MyDelegate( CallBackFunction ); + // Convert callback_delegate into a function pointer that can be + // used in unmanaged code. + IntPtr intptr_delegate = + Marshal.GetFunctionPointerForDelegate(callback_delegate); + // Call the API passing along the function pointer. + SetDebugFunction( intptr_delegate ); + Debug.Log ("rpc_path = " + rpc_path); + unity_start_service (rpc_path); + } + + // Start the ZeroTier service + protected void Init() + { + // TODO: Handle exceptions from unmanaged code + ztThread = new Thread(() => { + try { + zt_service_thread(); + } catch(Exception e) { + Debug.Log(e.Message.ToString()); + } + }); + ztThread.IsBackground = true; // Allow the thread to be aborted safely + ztThread.Start(); + } + + // Initialize the ZeroTier service with a given path + public ZeroTierNetworkInterface(string path) + { + rpc_path = path; + Init(); + } + + // Initialize the ZeroTier service + public ZeroTierNetworkInterface() + { + Init(); + } + + // Initialize the ZeroTier service + // Use the GlobalConfig to set things like the max packet size + public ZeroTierNetworkInterface(GlobalConfig gConfig) + { + MaxPacketSize = gConfig.MaxPacketSize; // TODO: Do something with this! + Init(); + } + +#region Network Handling + // Joins a ZeroTier virtual network + public void JoinNetwork(string nwid) + { + zt_join_network(nwid); + } + + // Leaves a ZeroTier virtual network + public void LeaveNetwork(string nwid) + { + zt_leave_network(nwid); + } +#endregion + + // Low-level representations of ZeroTier sockets + // The ZeroTier LLAPI is built on top of these + + // Creates a new ZeroTier-administered socket + public int Socket(int family, int type, int protocol) + { + return zt_socket (family, type, protocol); + } + + // Binds to a specific address + public int Bind(int fd, string addr, int port) + { + GCHandle sockaddr_ptr = ZeroTierUtils.Generate_unmananged_sockaddr(addr + ":" + port); + IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject (); + int addrlen = Marshal.SizeOf (pSockAddr); + return zt_bind (fd, pSockAddr, addrlen); + } + + // Listens for an incoming connection request + public int Listen(int fd, int backlog) + { + return zt_listen(fd, backlog); + } + + // Accepts an incoming connection + public int Accept(int fd) + { + return zt_accept (fd); + } + + // Closes a connection + public int Close(int fd) + { + return Close (fd); + } + + // Connects to a remote host + public int Connect(int fd, string addr, int port) + { + GCHandle sockaddr_ptr = ZeroTierUtils.Generate_unmananged_sockaddr(addr + ":" + port); + IntPtr pSockAddr = sockaddr_ptr.AddrOfPinnedObject (); + int addrlen = Marshal.SizeOf (pSockAddr); + return zt_connect (fd, pSockAddr, addrlen); + } + + + /* + unsafe { + byte *ptr = (byte *)buffer.ToPointer(); + + int offset = 0; + for (int i=0; i. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +using UnityEngine; +using System.Collections; +using System.Threading; +using System.Runtime.Serialization.Formatters.Binary; +using System.IO; +using UnityEngine.UI; +using UnityEngine.Networking; + +// Demonstrates the usage of bare-bones ZeroTier-administered sockets +using System.Net.Sockets; +using System.Runtime.InteropServices; +using System; + + +public class ZeroTierSockets_Demo : MonoBehaviour +{ + public float speed = 300f; + + private ZeroTierNetworkInterface zt; + string nwid = ""; + + int connection_socket; // The "connection id" + int host_socket; + + // Demo button methods + public void Join() + { + GameObject go = GameObject.Find ("inputNetworkID"); + InputField input = go.GetComponents () [0]; + Debug.Log ("Joining: " + input.text); + zt.JoinNetwork (input.text); + } + + public void Leave() + { + GameObject go = GameObject.Find ("inputNetworkID"); + InputField input = go.GetComponents () [0]; + Debug.Log ("Leaving: " + input.text); + zt.LeaveNetwork (input.text); + } + + // 1. Create ZeroTier-socket + // 2. Connect to remote host (on ZeroTier network) via socket + public void Connect() + { + GameObject addr_go = GameObject.Find ("inputServerAddress"); + GameObject port_go = GameObject.Find ("inputServerPort"); + InputField addr = addr_go.GetComponents () [0]; + InputField port = port_go.GetComponents () [0]; + Debug.Log ("Connecting to: " + addr.text + ":" + port.text); + + Thread connectThread = new Thread(() => { + + int sockfd = zt.Socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified); + Debug.Log ("sockfd = " + sockfd); + int port_num; + int.TryParse(port.text,out port_num); + zt.Connect (sockfd, addr.text,port_num); + Debug.Log ("connection_socket = " + connection_socket); + }); + connectThread.IsBackground = true; + connectThread.Start(); + } + + public void Bind() + { + GameObject addr_go = GameObject.Find ("inputServerAddress"); + GameObject port_go = GameObject.Find ("inputServerPort"); + InputField addr = addr_go.GetComponents () [0]; + InputField port = port_go.GetComponents () [0]; + Debug.Log ("Binding to: " + addr.text + ":" + port.text); + + Thread connectThread = new Thread(() => { + + // Socket() + int sockfd = zt.Socket ((int)AddressFamily.InterNetwork, (int)SocketType.Stream, (int)ProtocolType.Unspecified); + Debug.Log ("sockfd = " + sockfd); + + // Bind() + int port_num; + int.TryParse(port.text,out port_num); + int bind_res = zt.Bind(connection_socket, "0.0.0.0", port_num); + Debug.Log ("bind_res = " + bind_res); + + // Listen() + int listen_res = zt.Listen(connection_socket, 1); + Debug.Log ("listen_res = " + listen_res); + + // Accept() loop + Debug.Log("entering accept() loop"); + int accept_res = -1; + while(accept_res < 0) + { + //yield return new WaitForSeconds(1); + accept_res = zt.Accept(connection_socket); + Debug.Log ("accept_res = " + accept_res); + + } + }); + connectThread.IsBackground = true; + connectThread.Start(); + } + + public void Disconnect() + { + GameObject addr_go = GameObject.Find ("inputServerAddress"); + GameObject port_go = GameObject.Find ("inputServerAddress"); + InputField addr = addr_go.GetComponents () [0]; + InputField port = port_go.GetComponents () [0]; + Debug.Log ("Disconnecting from: " + addr.text + ":" + port.text); + Debug.Log ("Disconnect(): " + zt.Close (connection_socket)); + } + + public void SendMessage() + { + //zt_test_network (); + /* + GameObject go = GameObject.Find ("inputMessage"); + InputField msg = go.GetComponents () [0]; + + Thread sendThread = new Thread(() => { + Debug.Log ("Sending Message: " + msg.text); + byte error = 0; + zt.Send (server_connection_socket, msg.text.ToCharArray (), msg.text.ToCharArray ().Length, out error); + Debug.Log ("Send(): " + error); + }); + sendThread.IsBackground = true; + sendThread.Start(); + */ + } + + void Start() + { + // Set defaults + InputField input; + GameObject go; + go = GameObject.Find ("inputNetworkID"); + input = go.GetComponents () [0]; + input.text = "565799d8f6e1c11a"; + go = GameObject.Find ("inputServerAddress"); + input = go.GetComponents () [0]; + input.text = "172.22.211.245"; + go = GameObject.Find ("inputServerPort"); + input = go.GetComponents () [0]; + input.text = "8887"; + go = GameObject.Find ("inputMessage"); + input = go.GetComponents () [0]; + input.text = "Welcome to the machine"; + + // Create new instance of ZeroTier in separate thread + zt = new ZeroTierLLAPI ("/Users/Joseph/utest2/nc_565799d8f6e1c11a"); + + /* This new instance will communicate via a named pipe, so any + * API calls (ZeroTier.Connect(), ZeroTier.Send(), etc) will be sent to the service + * via this pipe. + */ + } + + // Terminate the ZeroTier service when the application quits + void OnApplicationQuit() { + Debug.Log ("OnApplicationQuit()"); + zt.Terminate (); + } + + // Update is called once per frame + void Update () { + /* + if (text) { + text.text = IsRunning() ? "ZeroTier Status: Online" : "ZeroTier Status: Offline"; + } + */ + + // --- + /* + int recHostId; + int connectionId; + int channelId; + byte[] recBuffer = new byte[1024]; + int bufferSize = 1024; + int dataSize; + byte error; + NetworkEventType recData = zt.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error); + switch (recData) + { + case NetworkEventType.Nothing: + break; + case NetworkEventType.ConnectEvent: + Debug.Log("NetworkEventType.ConnectEvent"); + break; + case NetworkEventType.DataEvent: + Debug.Log("NetworkEventType.DataEvent"); + break; + case NetworkEventType.DisconnectEvent: + Debug.Log("NetworkEventType.DisconnectEvent"); + break; + } + */ + // --- + + /* + GameObject go = GameObject.Find ("_txtStatusIndicator"); + Text text = go.GetComponents () [0]; + text.text = zt.IsRunning () ? "ZeroTier Service: ONLINE" : "ZeroTier Service: OFFLINE"; + */ + + // Rotate ZTCube when ZT is running + /* + if (zt.IsRunning ()) { + + + go = GameObject.Find ("ZTCube"); + Vector3 rotvec = new Vector3 (10f, 10f, 10f); + go.transform.Rotate (rotvec, speed * Time.deltaTime); + } + GameObject go = GameObject.Find("ZTCube"); + Text text = go.GetComponents ()[0]; + */ + } +} \ No newline at end of file diff --git a/integrations/Unity3D/Assets/ZeroTierUtils.cs b/integrations/Unity3D/Assets/ZeroTierUtils.cs old mode 100644 new mode 100755 index a1da79d..033d646 --- a/integrations/Unity3D/Assets/ZeroTierUtils.cs +++ b/integrations/Unity3D/Assets/ZeroTierUtils.cs @@ -1,4 +1,31 @@ -using UnityEngine; +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2015 ZeroTier, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +using UnityEngine; using System.Collections; using System.Net; using System; diff --git a/integrations/Unity3D/Assets/smcs.rsp b/integrations/Unity3D/Assets/smcs.rsp old mode 100644 new mode 100755 diff --git a/tests/client.c b/tests/client.c index 490ff7b..e79a829 100644 --- a/tests/client.c +++ b/tests/client.c @@ -8,23 +8,24 @@ int main(int argc , char *argv[]) { - int sock; + if(argc < 3) { + printf("usage: client \n"); + return 1; + } + + int sock, port = atoi(argv[1]); struct sockaddr_in server; char message[1000] , server_reply[2000]; - //Create socket sock = socket(AF_INET , SOCK_STREAM , 0); - if (sock == -1) - { + if (sock == -1) { printf("Could not create socket"); - } - puts("Socket created"); - - server.sin_addr.s_addr = inet_addr("127.0.0.1"); + } + server.sin_addr.s_addr = inet_addr(argv[1]); server.sin_family = AF_INET; - server.sin_port = htons( 8888 ); + server.sin_port = htons( port ); - //Connect to remote server + printf("connecting...\n"); if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) { perror("connect failed. Error"); @@ -33,7 +34,6 @@ int main(int argc , char *argv[]) puts("Connected\n"); - //keep communicating with server while(1) { printf("Enter message : ");