diff --git a/dist.sh b/dist.sh index e235e77..ad1e388 100755 --- a/dist.sh +++ b/dist.sh @@ -43,9 +43,9 @@ XCODE_MACOS_PROJ_DIR=$(pwd)/ports/xcode_macos # Generates wrapper source files for various target languages generate_swig_wrappers() { - SRC=../../src + SRC=../src - cd ports/swig; + cd ports/; # C# mkdir -p ${SRC}/csharp diff --git a/ports/csharp/csharp_callback.cs b/ports/csharp/csharp_callback.cs new file mode 100644 index 0000000..b4babfd --- /dev/null +++ b/ports/csharp/csharp_callback.cs @@ -0,0 +1,255 @@ +using System.Runtime.InteropServices; + +public struct CallbackMessage +{ + public int eventCode; + /* Pointers to structures that contain details about the + subject of the callback */ + public System.IntPtr node; + public System.IntPtr network; + public System.IntPtr netif; + public System.IntPtr route; + public System.IntPtr path; + public System.IntPtr peer; + public System.IntPtr addr; +} + +[StructLayout(LayoutKind.Sequential)] +public struct SockAddrStorage +{ + public byte Length; + public byte Family; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)] + public byte[] Data1; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public uint[] Data2; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] + public uint[] Data3; +} + +[StructLayout(LayoutKind.Sequential)] +public struct SockAddr +{ + public ushort Family; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 14)] + public byte[] Data; +} + +[StructLayout(LayoutKind.Sequential)] +public struct SockAddrIn +{ + public byte Length; + public byte Family; + public ushort Port; + public uint Addr; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] + public byte[] Zero; +} + +public struct NodeDetails +{ + /** + * The node ID + */ + public ulong address; + + /** + * The current clock value accord to the node + */ + public ulong clock; + + /** + * Whether or not this node is online + */ + public bool online; + + /** + * Whether port mapping is enabled + */ + public bool portMappingEnabled; + + /** + * Whether multipath support is enabled. If true, this node will + * be capable of utilizing multiple physical links simultaneosly + * to create higher quality or more robust aggregate links. + * + * See: https://www.zerotier.com/manual.shtml#2_1_5 + */ + public bool multipathEnabled; + + /** + * The port used by the service to send and receive + * all encapsulated traffic + */ + public ushort primaryPort; + + /** + * Planet ID + */ + public ulong planetWorldId; + public ulong planetWorldTimestamp; + public byte versionMajor; + public byte versionMinor; + public byte versionRev; +}; +struct AddrDetails +{ + public ulong nwid; + public SockAddrStorage addr; +}; + +struct NetifDetails +{ + /** + * The virtual network that this interface was commissioned for. + */ + public ulong nwid; + + /** + * The hardware address assigned to this interface + */ + public ulong mac; + + /** + * The MTU for this interface + */ + public int mtu; +}; + +struct RouteDetails +{ + /** + * Target network / netmask bits (in port field) or NULL or 0.0.0.0/0 for default + */ + public System.IntPtr target; + + /** + * Gateway IP address (port ignored) or NULL (family == 0) for LAN-local (no gateway) + */ + public System.IntPtr via; + + /** + * Route flags + */ + public ushort flags; + + /** + * Route metric (not currently used) + */ + public ushort metric; +}; + +struct NetworkDetails +{ + /** + * Network ID + */ + public ulong nwid; + + /** + * Maximum Transmission Unit size for this network + */ + public int mtu; + + /** + * Number of addresses (actually) assigned to the node on this network + */ + public short num_addresses; + + /** + * Array of IPv4 and IPv6 addresses assigned to the node on this network + */ + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)] + public System.IntPtr[] addr; + + /** + * Number of routes + */ + public uint num_routes; + + /** + * Array of IPv4 and IPv6 addresses assigned to the node on this network + */ + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32)] + public System.IntPtr[] routes; +}; + +struct PathDetails +{ + /** + * Address of endpoint + */ + public System.IntPtr address; + + /** + * Time of last send in milliseconds or 0 for never + */ + public ulong lastSend; + + /** + * Time of last receive in milliseconds or 0 for never + */ + public ulong lastReceive; + + /** + * Is this a trusted path? If so this will be its nonzero ID. + */ + public ulong trustedPathId; + + /** + * Is path expired? + */ + int expired; + + /** + * Is path preferred? + */ + int preferred; +}; + +struct PeerDetails +{ + /** + * ZeroTier address (40 bits) + */ + public ulong address; + + /** + * Remote major version or -1 if not known + */ + int versionMajor; + + /** + * Remote minor version or -1 if not known + */ + int versionMinor; + + /** + * Remote revision or -1 if not known + */ + int versionRev; + + /** + * Last measured latency in milliseconds or -1 if unknown + */ + int latency; + + /** + * What trust hierarchy role does this device have? + */ + public int role; + + /** + * Number of paths (size of paths[]) + */ + public uint pathCount; + + /** + * Known network paths to peer + */ + [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)] + public System.IntPtr[] paths; +}; + +[UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)] +public delegate void CSharpCallback(System.IntPtr msg); diff --git a/ports/zt.i b/ports/zt.i new file mode 100644 index 0000000..69aa836 --- /dev/null +++ b/ports/zt.i @@ -0,0 +1,30 @@ +/* libzt.i */ + +%begin +%{ +#define SWIG_PYTHON_CAST_MODE +%} + +%include + +#define PYTHON_BUILD 1 + +%module libzt +%{ +#include "../include/ZeroTier.h" +#include "../include/ZeroTierConstants.h" +%} + +%define %cs_callback(TYPE, CSTYPE) + %typemap(ctype) TYPE, TYPE& "void *" + %typemap(in) TYPE %{ $1 = ($1_type)$input; %} + %typemap(in) TYPE& %{ $1 = ($1_type)&$input; %} + %typemap(imtype, out="IntPtr") TYPE, TYPE& "CSTYPE" + %typemap(cstype, out="IntPtr") TYPE, TYPE& "CSTYPE" + %typemap(csin) TYPE, TYPE& "$csinput" +%enddef + +%cs_callback(userCallbackFunc, CSharpCallback) + +%include "../include/ZeroTier.h" +%include "../include/ZeroTierConstants.h" diff --git a/src/Controls.cpp b/src/Controls.cpp index 7265f09..c7d4a8a 100644 --- a/src/Controls.cpp +++ b/src/Controls.cpp @@ -253,12 +253,12 @@ void _clear_registered_callback() _callback_lock.unlock(); } -int __zts_node_online() +int _zts_node_online() { return service && service->getNode() && service->getNode()->online(); } -int __zts_can_perform_service_operation() +int _zts_can_perform_service_operation() { return service && service->isRunning() @@ -436,7 +436,7 @@ JNIEXPORT int JNICALL Java_com_zerotier_libzt_ZeroTier_init( int zts_join(const uint64_t nwid) { Mutex::Lock _l(_service_lock); - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } else { @@ -455,7 +455,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_join( int zts_leave(const uint64_t nwid) { Mutex::Lock _l(_service_lock); - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } else { @@ -474,7 +474,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_leave( int zts_leave_all() { Mutex::Lock _l(_service_lock); - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } else { @@ -489,7 +489,7 @@ int zts_orbit(uint64_t moonWorldId, uint64_t moonSeed) { Mutex::Lock _l(_service_lock); void *tptr = NULL; - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } else { service->getNode()->orbit(tptr, moonWorldId, moonSeed); @@ -503,7 +503,7 @@ int zts_deorbit(uint64_t moonWorldId) { Mutex::Lock _l(_service_lock); void *tptr = NULL; - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } else { service->getNode()->deorbit(tptr, moonWorldId); @@ -514,7 +514,7 @@ int zts_deorbit(uint64_t moonWorldId) #endif int zts_start( - const char *path, void (*callback)(struct zts_callback_msg*), int port) + const char *path, userCallbackFunc callback, int port) { Mutex::Lock _l(_service_lock); lwip_driver_init(); @@ -627,7 +627,7 @@ JNIEXPORT int JNICALL Java_com_zerotier_libzt_ZeroTier_start( int zts_stop() { Mutex::Lock _l(_service_lock); - if (__zts_can_perform_service_operation()) { + if (_zts_can_perform_service_operation()) { _run_service = false; service->terminate(); #if defined(_WIN32) @@ -658,7 +658,7 @@ int zts_restart() int tmpPort = _port; std::string tmpPath = _path; // Stop the service - if (__zts_can_perform_service_operation()) { + if (_zts_can_perform_service_operation()) { _run_service = false; service->terminate(); #if defined(_WIN32) @@ -713,7 +713,7 @@ JNIEXPORT void JNICALL Java_com_zerotier_libzt_ZeroTier_free( uint64_t zts_get_node_id() { Mutex::Lock _l(_service_lock); - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } return service->getNode()->address(); @@ -733,7 +733,7 @@ JNIEXPORT jlong JNICALL Java_com_zerotier_libzt_ZeroTier_get_1node_1id( int zts_get_peer_count() { Mutex::Lock _l(_service_lock); - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } return service->getNode()->peers()->peerCount; @@ -752,7 +752,7 @@ int zts_get_peers(struct zts_peer_details *pds, int *num) if (!pds || !num) { return ZTS_ERR_INVALID_ARG; } - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } ZT_PeerList *pl = service->getNode()->peers(); @@ -782,7 +782,7 @@ int zts_get_peer(struct zts_peer_details *pd, uint64_t peerId) if (!pd || !peerId) { return ZTS_ERR_INVALID_ARG; } - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } ZT_PeerList *pl = service->getNode()->peers(); @@ -814,7 +814,7 @@ int zts_get_num_joined_networks() { Mutex::Lock _l(_service_lock); int retval = ZTS_ERR_OK; - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } return service->networkCount(); @@ -831,7 +831,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_get_1num_1joined_1networ // Network Details // ////////////////////////////////////////////////////////////////////////////// -void __get_network_details_helper(uint64_t nwid, struct zts_network_details *nd) +void _get_network_details_helper(uint64_t nwid, struct zts_network_details *nd) { /* socklen_t addrlen; @@ -854,7 +854,7 @@ void _get_network_details(uint64_t nwid, struct zts_network_details *nd) { /* _vtaps_lock.lock(); - __get_network_details_helper(nwid, nd); + _get_network_details_helper(nwid, nd); _vtaps_lock.unlock(); */ } @@ -1061,7 +1061,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_libzt_ZeroTier_get_1protocol_1stats( int zts_get_node_status() { Mutex::Lock _l(_service_lock); - // Don't check __zts_can_perform_service_operation() here. + // Don't check _zts_can_perform_service_operation() here. return service && service->getNode() && service->getNode()->online() ? ZTS_EVENT_NODE_ONLINE : ZTS_EVENT_NODE_OFFLINE; @@ -1080,7 +1080,7 @@ int zts_get_network_status(uint64_t networkId) if (!networkId) { return ZTS_ERR_INVALID_ARG; } - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } /* @@ -1103,7 +1103,7 @@ int zts_get_peer_status(uint64_t peerId) { Mutex::Lock _l(_service_lock); int retval = ZTS_ERR_OK; - if (!__zts_can_perform_service_operation()) { + if (!_zts_can_perform_service_operation()) { return ZTS_ERR_SERVICE; } return service->getPeerStatus(peerId); diff --git a/src/Controls.hpp b/src/Controls.hpp index c78246b..2981546 100644 --- a/src/Controls.hpp +++ b/src/Controls.hpp @@ -94,7 +94,7 @@ void *_zts_run_service(void *thread_id); * @usage Can be called at any time * @return 1 or 0 */ -int __zts_can_perform_service_operation(); +int _zts_can_perform_service_operation(); /** * @brief [Should not be called from user application] Returns whether or not the node is @@ -102,7 +102,7 @@ int __zts_can_perform_service_operation(); * @usage Can be called at any time * @return 1 or 0 */ -int __zts_node_online(); +int _zts_node_online(); /** * @brief [Should not be called from user application] Adjusts the delay multiplier for the