From 5c41a0c75ccca39d3bada9fc4fca1ebaf992f4b7 Mon Sep 17 00:00:00 2001 From: Joseph Henry Date: Fri, 17 Mar 2017 17:31:22 -0700 Subject: [PATCH] added 6PLANE address address/ID getters for zts_* API --- src/sdk.h | 65 +++++++++++++++++++++++++------------------------ src/service.cpp | 63 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 78 insertions(+), 50 deletions(-) diff --git a/src/sdk.h b/src/sdk.h index 57368ad..0124464 100644 --- a/src/sdk.h +++ b/src/sdk.h @@ -28,6 +28,17 @@ #ifndef _ZT_SDK_H #define _ZT_SDK_H 1 +#include +#include + + // ------------------------------------------------------------------------------ + // ---------------------------- Compilation flag checks ------------------------- + // ------------------------------------------------------------------------------ + +#define INTERCEPT_ENABLED 111 +#define INTERCEPT_DISABLED 222 +#define MAX_DIR_SZ 256 // Max path length used for home dir + #if defined(SDK_SERVICE) // Sanity checks for compilation #if !defined(SDK_LWIP) && !defined(SDK_PICOTCP) @@ -44,22 +55,20 @@ #endif #endif -#include -#include + // ------------------------------------------------------------------------------ + // -------------- Socket API function signatures for convenience ---------------- + // ------------------------------------------------------------------------------ #define SETSOCKOPT_SIG int fd, int level, int optname, const void *optval, socklen_t optlen #define GETSOCKOPT_SIG int fd, int level, int optname, void *optval, socklen_t *optlen - #define SENDMSG_SIG int fd, const struct msghdr *msg, int flags #define SENDTO_SIG int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen #define RECV_SIG int fd, void *buf, size_t len, int flags #define RECVFROM_SIG int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen #define RECVMSG_SIG int fd, struct msghdr *msg,int flags - #define SEND_SIG int fd, const void *buf, size_t len, int flags #define WRITE_SIG int fd, const void *buf, size_t len #define READ_SIG int fd, void *buf, size_t len - #define SOCKET_SIG int socket_family, int socket_type, int protocol #define CONNECT_SIG int fd, const struct sockaddr *addr, socklen_t addrlen #define BIND_SIG int fd, const struct sockaddr *addr, socklen_t addrlen @@ -80,15 +89,15 @@ extern "C" { #endif -#define INTERCEPT_ENABLED 111 -#define INTERCEPT_DISABLED 222 -#define MAX_DIR_SZ 256 // Max path length used for home dir - extern void load_symbols(); extern void zts_init_rpc(const char *path, const char *nwid); extern char *api_netpath; extern char *debug_logfile; + // ------------------------------------------------------------------------------ + // ------------------------- Ancient INTERCEPT-related cruft -------------------- + // ------------------------------------------------------------------------------ + // Function pointers to original system calls // - These are used when we detect that either the intercept is not // available or that ZeroTier hasn't administered the given socket @@ -115,20 +124,16 @@ extern char *debug_logfile; extern int (*realgetsockopt)(GETSOCKOPT_SIG); extern int (*realclose)(CLOSE_SIG); extern int (*realgetsockname)(GETSOCKNAME_SIG); - -// Direct call -// - Skips intercept -// - Uses RPC -// - Depending on the target, the API will be exposed as zt_* in -// the specific way needed for that platform, but will be implemented -// in terms of zts_* + + // ------------------------------------------------------------------------------ + // ---------------------------- Direct API call section ------------------------- + // ------------------------------------------------------------------------------ // SOCKS5 Proxy Controls int zts_start_proxy_server(const char *homepath, const char * nwid, struct sockaddr_storage * addr); int zts_stop_proxy_server(const char *nwid); int zts_get_proxy_server_address(const char * nwid, struct sockaddr_storage *addr); bool zts_proxy_is_running(const char *nwid); - // ZT Service Controls void zts_start_service(const char *path); void *zts_start_core_service(void *thread_id); @@ -137,22 +142,17 @@ void zts_stop(); bool zts_service_is_running(); void zts_join_network(const char * nwid); void zts_join_network_soft(const char * filepath, const char * nwid); +void zts_leave_network_soft(const char * filepath, const char * nwid); void zts_leave_network(const char * nwid); -// void zts_get_addresses(const char * nwid, char * addrstr); void zts_get_ipv4_address(const char *nwid, char *addrstr); void zts_get_ipv6_address(const char *nwid, char *addrstr); bool zts_has_address(const char *nwid); -int zts_get_device_id(); -bool zts_is_relayed(); +int zts_get_device_id(char *devID); +int zts_get_device_id_from_file(const char *filepath, char *devID); char *zts_get_homepath(); - -// ZT Intercept/RPC Controls -// TODO: Remove any? -//void set_intercept_status(int mode); // TODO: Rethink this -//void init_service(int key, const char * path); -//void init_service_and_rpc(int key, const char * path, const char * nwid); -//void init_intercept(int key); - +void zts_get_6plane_addr(char *addr, const char *nwid, const char *devID); +void zts_get_rfc4193_addr(char *addr, const char *nwid, const char *devID); +// BSD-like socket API int zts_socket(SOCKET_SIG); int zts_connect(CONNECT_SIG); int zts_bind(BIND_SIG); @@ -167,12 +167,10 @@ int zts_getsockname(GETSOCKNAME_SIG); int zts_getpeername(GETPEERNAME_SIG); int zts_close(CLOSE_SIG); int zts_fcntl(FCNTL_SIG); - ssize_t zts_sendto(SENDTO_SIG); ssize_t zts_sendmsg(SENDMSG_SIG); ssize_t zts_recvfrom(RECVFROM_SIG); ssize_t zts_recvmsg(RECVMSG_SIG); - #if defined(__UNITY_3D__) ssize_t zts_recv(int fd, void *buf, int len); ssize_t zts_send(int fd, void *buf, int len); @@ -185,7 +183,10 @@ ssize_t zts_recvmsg(RECVMSG_SIG); void zt_leave_network(const char * nwid); #endif -// Android JNI Direct-call API + // ------------------------------------------------------------------------------ + // --------------------- Direct API call section (for Android) ------------------ + // ------------------------------------------------------------------------------ + // JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME #if defined(__ANDROID__) // ZT SERVICE CONTROLS @@ -228,7 +229,7 @@ ssize_t zts_recvmsg(RECVMSG_SIG); // Prototypes for redefinition of syscalls -// - Implemented in SDK_Intercept.c +// - Implemented in intercept.c #if defined(SDK_INTERCEPT) int socket(SOCKET_SIG); int connect(CONNECT_SIG); diff --git a/src/service.cpp b/src/service.cpp index 7e67414..a01c36d 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -45,6 +45,7 @@ #include "OneService.hpp" #include "Utils.hpp" #include "OSUtils.hpp" +#include "InetAddress.hpp" #include "tap.hpp" #include "sdk.h" @@ -152,20 +153,26 @@ void zts_join_network_soft(const char * filepath, const char * nwid) { } } } -// +// Prevent service from joining network upon startup +void zts_leave_network_soft(const char * filepath, const char * nwid) { + std::string net_dir = std::string(filepath) + "/networks.d/"; + ZeroTier::OSUtils::rm((net_dir + nwid + ".conf").c_str()); +} +// Instruct the service to leave the network void zts_leave_network(const char * nwid) { if(zt1Service) - zt1Service->leave(nwid); + zt1Service->leave(nwid); } -// +// Check whether the service is running bool zts_service_is_running() { return !zt1Service ? false : zt1Service->isRunning(); } -// +// Stop the service void zts_stop_service() { if(zt1Service) zt1Service->terminate(); } +// Stop the service, proxy server, stack, etc void zts_stop() { DEBUG_INFO("Stopping STSDK"); zts_stop_service(); @@ -177,7 +184,6 @@ void zts_stop() { // Now only returns first assigned address per network. Shouldn't normally be a problem. // Get IPV4 Address for this device on given network - bool zts_has_address(const char *nwid) { char ipv4_addr[64], ipv6_addr[64]; @@ -190,8 +196,6 @@ bool zts_has_address(const char *nwid) } return true; } - - void zts_get_ipv4_address(const char *nwid, char *addrstr) { uint64_t nwid_int = strtoull(nwid, NULL, 16); @@ -229,23 +233,46 @@ void zts_get_ipv6_address(const char *nwid, char *addrstr) memcpy(addrstr, "-1.-1.-1.-1/-1", 14); } } -// Get device ID -int zts_get_device_id() -{ - // zt->node->status - /* TODO */ return 0; +// Get device ID (from running service) +int zts_get_device_id(char *devID) { + if(zt1Service) { + char id[10]; + sprintf(id, "%lx",zt1Service->getNode()->address()); + memcpy(devID, id, 10); + return 0; + } + else + return -1; } -// -bool zts_is_relayed() { - // TODO - // zt1Service->getNode()->peers() - return false; +// Get device ID (from file) +int zts_get_device_id_from_file(const char *filepath, char *devID) { + std::string fname("identity.public"); + std::string fpath(filepath); + + if(ZeroTier::OSUtils::fileExists((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),false)) { + std::string oldid; + ZeroTier::OSUtils::readFile((fpath + ZT_PATH_SEPARATOR_S + fname).c_str(),oldid); + memcpy(devID, oldid.c_str(), 10); // first 10 bytes of file + return 0; + } + return -1; } // Return the home path for this instance of ZeroTier char *zts_get_homepath() { return (char*)givenHomeDir.c_str(); } - +// Returns a 6PLANE IPv6 address given a network ID and zerotier ID +void zts_get_6plane_addr(char *addr, const char *nwid, const char *devID) +{ + ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv66plane(ZeroTier::Utils::hexStrToU64(nwid),ZeroTier::Utils::hexStrToU64(devID)); + memcpy(addr, _6planeAddr.toIpString().c_str(), 40); +} +// Returns a RFC 4193 IPv6 address given a network ID and zerotier ID +void zts_get_rfc4193_addr(char *addr, const char *nwid, const char *devID) +{ + ZeroTier::InetAddress _6planeAddr = ZeroTier::InetAddress::makeIpv6rfc4193(ZeroTier::Utils::hexStrToU64(nwid),ZeroTier::Utils::hexStrToU64(devID)); + memcpy(addr, _6planeAddr.toIpString().c_str(), 40); +} // ------------------------------------------------------------------------------ // ----------------------------- .NET Interop functions -------------------------