diff --git a/make-linux.mk b/make-linux.mk index 28c20a1..80091eb 100644 --- a/make-linux.mk +++ b/make-linux.mk @@ -76,15 +76,15 @@ endif all: shared_lib check remove_only_intermediates: - -find . -type f -name '*.o' -delete + -find . -type f \( -name '*.o' -o -name '*.so' \) -delete linux_shared_lib: remove_only_intermediates $(OBJS) + mkdir -p build/linux_shared_lib $(CXX) $(CXXFLAGS) $(LDFLAGS) $(DEFS) -DZT_SDK -DZT_ONE_NO_ROOT_CHECK -Iext/lwip/src/include -Iext/lwip/src/include/ipv4 -Iext/lwip/src/include/ipv6 -Izerotierone/osdep -Izerotierone/node -Isrc -o build/zerotier-sdk-service $(OBJS) zerotierone/service/OneService.cpp src/SDK_EthernetTap.cpp src/SDK_Proxy.cpp zerotierone/one.cpp -x c src/SDK_RPC.c $(LDLIBS) -ldl # Build liblwip.so which must be placed in ZT home for zerotier-netcon-service to work make -f make-liblwip.mk # Use gcc not clang to build standalone intercept library since gcc is typically used for libc and we want to ensure maximal ABI compatibility cd src ; gcc $(DEFS) -g -O2 -Wall -std=c99 -fPIC -DVERBOSE -D_GNU_SOURCE -DSDK_INTERCEPT -I. -I../zerotierone/node -nostdlib -shared -o libztintercept.so SDK_Sockets.c SDK_Intercept.c SDK_Debug.c SDK_RPC.c -ldl - mkdir -p build/linux_shared_lib cp src/libztintercept.so build/linux_shared_lib/libztintercept.so ln -sf zerotier-sdk-service zerotier-cli ln -sf zerotier-sdk-service zerotier-idtool diff --git a/make-mac.mk b/make-mac.mk index 4293878..f609198 100644 --- a/make-mac.mk +++ b/make-mac.mk @@ -78,15 +78,17 @@ android_jni_lib: cd build; for res_f in *; do mv "$res_f" "android_jni_lib_$res_f"; done #cp docs/android_zt_sdk.md build/README.md -osx_shared_lib: $(OBJS) - -find . -type f -name '*.o' -delete +remove_only_intermediates: + -find . -type f \( -name '*.o' -o -name '*.so' \) -delete + +osx_shared_lib: remove_only_intermediates $(OBJS) + mkdir -p build/osx_shared_lib # Need to selectively rebuild one.cpp and OneService.cpp with ZT_SERVICE_NETCON and ZT_ONE_NO_ROOT_CHECK defined, and also NetconEthernetTap $(CXX) $(CXXFLAGS) $(LDFLAGS) -DZT_SDK -DZT_ONE_NO_ROOT_CHECK -Iext/lwip/src/include -Iext/lwip/src/include/ipv4 -Iext/lwip/src/include/ipv6 -Izerotierone/osdep -Izerotierone/node -Isrc -o build/zerotier-sdk-service $(OBJS) zerotierone/service/OneService.cpp src/SDK_EthernetTap.cpp src/SDK_Proxy.cpp zerotierone/one.cpp -x c src/SDK_RPC.c $(LDLIBS) -ldl # Build liblwip.so which must be placed in ZT home for zerotier-sdk-service to work make -f make-liblwip.mk # Use gcc not clang to build standalone intercept library since gcc is typically used for libc and we want to ensure maximal ABI compatibility cd src ; gcc $(DEFS) -O2 -Wall -std=c99 -fPIC -fno-common -dynamiclib -flat_namespace -DVERBOSE -D_GNU_SOURCE -DNETCON_INTERCEPT -I. -I../zerotierone/node -nostdlib -shared -o libztintercept.so SDK_Sockets.c SDK_Intercept.c SDK_Debug.c SDK_RPC.c -ldl - mkdir -p build/osx_shared_lib cp src/libztintercept.so build/osx_shared_lib/libztintercept.so ln -sf zerotier-sdk-service zerotier-cli ln -sf zerotier-sdk-service zerotier-idtool diff --git a/src/SDK.h b/src/SDK.h index 4e04eda..1078cd8 100644 --- a/src/SDK.h +++ b/src/SDK.h @@ -39,8 +39,8 @@ extern "C" { #define INTERCEPT_ENABLED 111 #define INTERCEPT_DISABLED 222 -void zt_init_rpc(char *nwid); -const char *get_netpath(); +void zt_init_rpc(char *path, char *nwid); +extern char *api_netpath; #if defined(__linux__) static int (*realaccept4)(ACCEPT4_SIG) = 0; diff --git a/src/SDK_Intercept.c b/src/SDK_Intercept.c index 328fbd9..54c988d 100644 --- a/src/SDK_Intercept.c +++ b/src/SDK_Intercept.c @@ -59,12 +59,9 @@ #include "SDK_Debug.h" #include "SDK_RPC.h" -int set_netpath(char * path); -char *netpath = (char *)0; void dwr(int level, const char *fmt, ... ); -const char *get_netpath(); - pthread_key_t thr_id_key; +char *api_netpath; // ------------------------------------------------------------------------------ // --------------------- Get Original socket API pointers ----------------------- @@ -108,6 +105,7 @@ pthread_key_t thr_id_key; load_symbols(); } #if defined(SDK_BUNDLED) + /* The reasoning for this check is that if you've built the SDK with SDK_BUNDLE=1, then you've included a full ZeroTier service in the same binary as your intercept, and we don't want to run ZeroTier network API calls through the intercept, so we must specify @@ -116,7 +114,7 @@ pthread_key_t thr_id_key; int thr_id = spec != NULL ? *((int*)spec) : -1; return thr_id == INTERCEPT_ENABLED; #else - return 1 + return 1; #endif } @@ -130,9 +128,8 @@ pthread_key_t thr_id_key; int connected_to_service(int sockfd) { - socklen_t len; struct sockaddr_storage addr; - len = sizeof addr; + socklen_t len = sizeof addr; struct sockaddr_un * addr_un; getpeername(sockfd, (struct sockaddr*)&addr, &len); if (addr.ss_family == AF_LOCAL || addr.ss_family == AF_LOCAL) { diff --git a/src/SDK_RPC.c b/src/SDK_RPC.c index 3a06277..a089726 100644 --- a/src/SDK_RPC.c +++ b/src/SDK_RPC.c @@ -275,66 +275,44 @@ ssize_t sock_fd_write(int sock, int fd) * Read a file descriptor */ ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd) -{ - FILE *file = fopen("/Users/Joseph/code/__log","a"); - - +{ ssize_t size; if (fd) { - - fprintf(file, "A"); - struct msghdr msg; struct iovec iov; union { struct cmsghdr cmsghdr; char control[CMSG_SPACE(sizeof (int))]; } cmsgu; - - fprintf(file, "B"); - + struct cmsghdr *cmsg; iov.iov_base = buf; iov.iov_len = bufsize; msg.msg_name = NULL; msg.msg_namelen = 0; - fprintf(file, "C"); - - msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_control = cmsgu.control; msg.msg_controllen = sizeof(cmsgu.control); size = recvmsg (sock, &msg, 0); - fprintf(file, "D"); - if (size < 0) return -1; cmsg = CMSG_FIRSTHDR(&msg); if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { - fprintf(file, "E"); - if (cmsg->cmsg_level != SOL_SOCKET) { - fprintf(file, "F"); - fprintf (stderr, "invalid cmsg_level %d\n",cmsg->cmsg_level); return -1; } if (cmsg->cmsg_type != SCM_RIGHTS) { - fprintf(file, "G"); - fprintf (stderr, "invalid cmsg_type %d\n",cmsg->cmsg_type); return -1; } *fd = *((int *) CMSG_DATA(cmsg)); } else { - fprintf(file, "H"); *fd = -1;} } else { - fprintf(file, "I"); - size = read (sock, buf, bufsize); if (size < 0) { fprintf(stderr, "sock_fd_read(): read: Error\n"); diff --git a/src/SDK_ServiceSetup.cpp b/src/SDK_ServiceSetup.cpp index 2d06ae5..ad9dfa0 100644 --- a/src/SDK_ServiceSetup.cpp +++ b/src/SDK_ServiceSetup.cpp @@ -55,9 +55,9 @@ pthread_t intercept_thread; int * intercept_thread_id; pthread_key_t thr_id_key; static ZeroTier::OneService *volatile zt1Service; -static std::string homeDir; +std::string homeDir; std::string netDir; - +char *api_netpath; #ifdef __cplusplus extern "C" { @@ -88,6 +88,7 @@ extern "C" { LOGV("unable to write network conf file: %s\n", nwid); } zt1Service->join(nwid); + zt_init_rpc(homeDir.c_str(), nwid); // This provides the shim API with the RPC information } void leave_network(const char *nwid) { zt1Service->leave(nwid); } diff --git a/src/SDK_ServiceSetup.hpp b/src/SDK_ServiceSetup.hpp index 49b134e..8c0a299 100644 --- a/src/SDK_ServiceSetup.hpp +++ b/src/SDK_ServiceSetup.hpp @@ -38,6 +38,8 @@ extern "C" { #define INTERCEPT_ENABLED 111 #define INTERCEPT_DISABLED 222 + +extern std::string homeDir; #if defined(__ANDROID__) // JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME diff --git a/src/SDK_Sockets.c b/src/SDK_Sockets.c index f529c3b..ef1adce 100644 --- a/src/SDK_Sockets.c +++ b/src/SDK_Sockets.c @@ -75,73 +75,35 @@ void print_addr(struct sockaddr *addr); void dwr(int level, const char *fmt, ... ); -static char *api_netpath = (char *)0; - -// TODO: Remove before production -void set_netpath(char * path) { - dwr(MSG_DEBUG,"set_netpath(%s)", path); - api_netpath = path; - rpc_mutex_init(); // TODO: double-check this -} - -const char *get_netpath() { - return api_netpath; -} - +char *api_netpath = (char *)0; + // ------------------------------------------------------------------------------ // ---------------------------------- zt_init_rpc ------------------------------- // ------------------------------------------------------------------------------ - void zt_init_rpc(char *nwid) + void zt_init_rpc(char *path, char *nwid) { dwr(MSG_DEBUG, "zt_init_rpc\n"); - #if defined(__UNITY_3D__) - //char *nw = "565799d8f6e1c11a"; - //char *path = "/Users/Joseph/utest2/nc_"; - //char *fullpath = malloc(strlen(path)+strlen(nw)+1); - //if(fullpath) { - // strcpy(fullpath, path); - // strcat(fullpath, nw); - // api_netpath = fullpath; - //} - //api_netpath = nwid; - api_netpath = "/Users/Joseph/utest2/nc_565799d8f6e1c11a"; - - #endif - - #if defined(__IOS__) - /* - api_netpath = "ZeroTier/One/nc_" + nwid; - - void *spec = pthread_getspecific(thr_id_key); - int thr_id = spec != NULL ? *((int*)spec) : -1; - // dwr(MSG_DEBUG_EXTRA, "set_up_intercept(thr_id=%d)\n", thr_id); - if(thr_id == INTERCEPT_ENABLED) { - if (!api_netpath) { - api_netpath = "ZeroTier/One/nc_e5cd7a9e1c3511dd"; // Path allowed on iOS devices - - } - return 1; + if(!api_netpath) { + #if defined(SDK_BUNDLED) + // Get the path/nwid from the user application + char *fullpath = malloc(strlen(path)+strlen(nwid)+1); + if(fullpath) { + strcpy(fullpath, path); + strcat(fullpath, nwid); + api_netpath = fullpath; + } + #else + // Get path/nwid from environment variables + if (!api_netpath) { + api_netpath = getenv("ZT_NC_NETWORK"); + dwr(MSG_DEBUG, "$ZT_NC_NETWORK = %s\n", api_netpath); + } + #endif } - return 0; - */ - api_netpath = "ZeroTier/One/nc_e5cd7a9e1c3511dd"; - - #elif defined(__ANDROID__) - api_netpath = "ZeroTier/One/nc_" + nwid; - return 1; - #else - if (!api_netpath) { - api_netpath = getenv("ZT_NC_NETWORK"); - set_netpath(api_netpath); - - dwr(MSG_DEBUG, "netpath = %s\n", api_netpath); - if(!api_netpath) { - // return 0; - } - } - #endif } + + void get_api_netpath() { zt_init_rpc("",""); } // ------------------------------------------------------------------------------ // ------------------------------------ sendto() -------------------------------- @@ -308,9 +270,7 @@ const char *get_netpath() { // ------------------------------------------------------------------------------ #if defined(__UNITY_3D__) - // Just expose some basic calls for configuring and RX/TXing through ZT sockets - ssize_t zt_send(int fd, void *buf, int len) { return write(fd, buf, len); } @@ -359,7 +319,7 @@ const char *get_netpath() { // int socket_family, int socket_type, int protocol int zt_socket(SOCKET_SIG) { - zt_init_rpc(""); + get_api_netpath(); dwr(MSG_DEBUG, "zt_socket()\n"); /* Check that type makes sense */ #if defined(__linux__) @@ -408,6 +368,7 @@ const char *get_netpath() { int zt_connect(CONNECT_SIG) { + get_api_netpath(); dwr(MSG_DEBUG,"zt_connect(%d)\n", __fd); struct connect_st rpc_st; #if defined(__linux__) @@ -431,6 +392,7 @@ const char *get_netpath() { #if !defined(__ANDROID__) int zt_bind(BIND_SIG) { + get_api_netpath(); dwr(MSG_DEBUG,"zt_bind(%d)\n", sockfd); struct bind_st rpc_st; rpc_st.sockfd = sockfd; @@ -455,6 +417,7 @@ const char *get_netpath() { #if defined(__linux__) int zt_accept4(ACCEPT4_SIG) { + get_api_netpath(); dwr(MSG_DEBUG,"zt_accept4(%d):\n", sockfd); #if !defined(__ANDROID__) if ((flags & SOCK_CLOEXEC)) @@ -473,12 +436,13 @@ const char *get_netpath() { int zt_accept(ACCEPT_SIG) { + get_api_netpath(); dwr(MSG_DEBUG,"zt_accept(%d):\n", sockfd); -// FIXME: Find a better solution for this before production -#if !defined(__UNITY_3D__) + // FIXME: Find a better solution for this before production + #if !defined(__UNITY_3D__) if(addr) addr->sa_family = AF_INET; -#endif + #endif int new_fd = get_new_fd(sockfd); dwr(MSG_DEBUG,"newfd = %d\n", new_fd); @@ -497,6 +461,7 @@ const char *get_netpath() { int zt_listen(LISTEN_SIG) { + get_api_netpath(); dwr(MSG_DEBUG,"zt_listen(%d):\n", sockfd); struct listen_st rpc_st; rpc_st.sockfd = sockfd; @@ -528,6 +493,7 @@ const char *get_netpath() { int zt_getsockname(GETSOCKNAME_SIG) { + get_api_netpath(); dwr(MSG_DEBUG,"zt_getsockname(%d):\n", sockfd); /* TODO: This is kind of a hack as it stands -- assumes sockaddr is sockaddr_in * and is an IPv4 address. */