RPC improvements for bundled and non-bundled builds

This commit is contained in:
Joseph Henry
2016-06-29 01:35:58 -07:00
parent 973e13561c
commit b52551ea4a
8 changed files with 51 additions and 105 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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;

View File

@@ -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) {

View File

@@ -276,14 +276,8 @@ ssize_t sock_fd_write(int sock, int fd)
*/
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 {
@@ -291,50 +285,34 @@ ssize_t sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
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");

View File

@@ -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); }

View File

@@ -39,6 +39,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
/* If you define anything else in this file it *must* follow that convention

View File

@@ -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
#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;
}
return 1;
}
return 0;
*/
api_netpath = "ZeroTier/One/nc_e5cd7a9e1c3511dd";
#elif defined(__ANDROID__)
api_netpath = "ZeroTier/One/nc_" + nwid;
return 1;
#else
// Get path/nwid from environment variables
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;
}
dwr(MSG_DEBUG, "$ZT_NC_NETWORK = %s\n", api_netpath);
}
#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,6 +436,7 @@ 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__)
@@ -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. */