path trickery update, direct-call API bugfix

This commit is contained in:
System Administrator
2016-07-17 20:56:42 -07:00
parent 87ffdcfd3d
commit 2393cd6367
11 changed files with 158 additions and 72 deletions

View File

@@ -1,5 +1,5 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
// Implementations located in src/SDK_XcodeWrapper.cpp
//
#ifndef Example_OSX_Bridging_Header_h
@@ -7,6 +7,7 @@
int start_intercept();
void start_service(const char * path);
void start_service_and_rpc(const char * path, const char * nwid);
void join_network(const char * nwid);
void disable_intercept();
void enable_intercept();
@@ -30,7 +31,6 @@ int zts_getsockopt(GETSOCKOPT_SIG);
int zts_close(CLOSE_SIG);
int zts_getsockname(GETSOCKNAME_SIG);
#endif /* Example_OSX_Bridging_Header_h */

View File

@@ -139,19 +139,19 @@ int rpc_join(char * sockname)
strncpy(addr.sun_path, sockname, sizeof(addr.sun_path)-1);
int sock;
#if defined(__ANDROID__)
if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
#else
#if defined(SDK_INTERCEPT)
if((sock = realsocket(AF_UNIX, SOCK_STREAM, 0)) < 0){
#else
if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
#endif
LOGV(stderr, "Error while creating RPC socket\n");
return -1;
}
while((conn_err != 0) && (attempts < SERVICE_CONNECT_ATTEMPTS)){
#if defined(__ANDROID__)
if((conn_err = connect(sock, (struct sockaddr*)&addr, sizeof(addr))) != 0) {
#else
#if defined(SDK_INTERCEPT)
if((conn_err = realconnect(sock, (struct sockaddr*)&addr, sizeof(addr))) != 0) {
#else
if((conn_err = connect(sock, (struct sockaddr*)&addr, sizeof(addr))) != 0) {
#endif
LOGV("Error while connecting to RPC socket. Re-attempting...\n");
sleep(1);

View File

@@ -42,12 +42,11 @@
#include <sys/types.h>
#include <pthread.h>
#include "service/OneService.hpp"
#include "OneService.hpp"
#include "Utils.hpp"
#include "OSUtils.hpp"
#include "SDK.h"
#include "SDK_Debug.h"
#include "SDK_ServiceSetup.hpp"
@@ -56,7 +55,10 @@ pthread_t intercept_thread;
int * intercept_thread_id;
pthread_key_t thr_id_key;
static ZeroTier::OneService *volatile zt1Service;
std::string homeDir;
std::string localHomeDir; // Local shortened path
std::string givenHomeDir; // What the user/application provides as a suggestion
std::string homeDir; // The resultant platform-specific dir we *must* use internally
std::string netDir;
#ifdef __cplusplus
@@ -145,20 +147,24 @@ void zt_init_rpc(const char * path, const char * nwid);
/*
* Starts a service thread and performs basic setup tasks
*/
void init_service(int key, const char * path)
{
homeDir = path;
void init_service(int key, const char * path) {
givenHomeDir = path;
pthread_key_create(&thr_id_key, NULL);
intercept_thread_id = (int*)malloc(sizeof(int));
*intercept_thread_id = key;
pthread_create(&intercept_thread, NULL, startOneService, (void *)(intercept_thread_id));
}
void init_service_and_rpc(int key, const char * path, const char * nwid)
{
init_service(key, path);
zt_init_rpc(path, nwid);
}
/*
* Enables or disables intercept for current thread using key in thread-local storage
*/
void set_intercept_status(int mode)
{
void set_intercept_status(int mode) {
fprintf(stderr, "set_intercept_status(mode=%d): tid = %d\n", mode, pthread_mach_thread_np(pthread_self()));
pthread_key_create(&thr_id_key, NULL);
intercept_thread_id = (int*)malloc(sizeof(int));
@@ -192,28 +198,30 @@ void zt_init_rpc(const char * path, const char * nwid);
// homeDir according to platform and build type
if(!homeDir.length())
{
#if defined(__UNITY_3D__)
int MAX_DIR_SZ = 256;
char current_dir[MAX_DIR_SZ];
getcwd(current_dir, MAX_DIR_SZ);
chdir(service_path.c_str());
#endif
#if defined(__UNITY_3D__) && !defined(__ANDROID__) && !defined(__IOS__)
// Unity3D on a non-mobile platform
homeDir = current_dir; // homeDir shall be current dir
#endif
#if defined(__APPLE__)
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR
// homeDir = "dont/run/this/in/the/simulator";
#elif TARGET_OS_IPHONE
homeDir = "ZeroTier/One";
#endif
#endif
}
#if defined(__UNITY_3D__)
int MAX_DIR_SZ = 256;
char current_dir[MAX_DIR_SZ];
getcwd(current_dir, MAX_DIR_SZ);
chdir(service_path.c_str());
#endif
#if defined(__APPLE__)
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR
// homeDir = "dont/run/this/in/the/simulator/it/wont/work";
#elif TARGET_OS_IPHONE
localHomeDir = "ZeroTier/One";
std::string del = givenHomeDir.length() && givenHomeDir[givenHomeDir.length()-1]!='/' ? "/" : "";
homeDir = givenHomeDir + del + localHomeDir;
#endif
#endif
#if defined(__ANDROID__)
/* NOTE: Since on Android devices the sdcard is formatted as fat32, we can't use this
location to set up the RPC unix domain socket. Rather we must use the application's
@@ -223,13 +231,16 @@ void zt_init_rpc(const char * path, const char * nwid);
//join_network("565799d8f65063e5");
#endif
LOGV("homeDir = %s", homeDir.c_str());
#if defined(__APPLE__) && !defined(__IOS__)
homeDir = givenHomeDir;
#endif
LOGV("homeDir = %s", givenHomeDir.c_str());
// Where network .conf files will be stored
netDir = homeDir + "/networks.d";
zt1Service = (ZeroTier::OneService *)0;
LOGV("Starting ZT service...\n");
// Construct path for network config and supporting service files
if (!homeDir.length()) {
#if defined(__ANDROID__)
return;
@@ -254,6 +265,18 @@ void zt_init_rpc(const char * path, const char * nwid);
}
}
#if defined(__IOS__)
// Go to the app's data directory so we can shorten the sun_path we bind to
int MAX_DIR_SZ = 256;
char current_dir[MAX_DIR_SZ];
getcwd(current_dir, MAX_DIR_SZ);
std::string targetDir = homeDir + "/../../";
chdir(targetDir.c_str());
homeDir = localHomeDir;
#endif
//chdir(current_dir); // Return to previous current working directory (at the request of Unity3D)
//Debug(homeDir.c_str());

View File

@@ -57,6 +57,7 @@ extern std::string homeDir;
#else
void *startOneService(void *thread_id);
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);
#endif
void set_intercept_status(int mode);

View File

@@ -92,24 +92,28 @@ int (*realclose)(CLOSE_SIG);
void zt_init_rpc(const char *path, const char *nwid)
{
dwr(MSG_DEBUG, "zt_init_rpc\n");
// Just double check we have
if(!realconnect) {
load_symbols_rpc();
}
#if !defined(__IOS__)
// Since we don't use function interposition in iOS
if(!realconnect) {
load_symbols_rpc();
}
#endif
// If no path, construct one or get it fron system env vars
if(!api_netpath) {
#if defined(SDK_BUNDLED)
// Get the path/nwid from the user application
// netpath = [path + "/nc_" + nwid]
char *fullpath = malloc(strlen(path)+strlen(nwid)+1);
char *fullpath = malloc(strlen(path)+strlen(nwid)+1+4);
if(fullpath) {
strcpy(fullpath, path);
strcat(fullpath, "/nc_");
strcat(fullpath, nwid);
//api_netpath = fullpath;
api_netpath = "/data/data/com.example.joseph.example_app/files/zerotier/nc_565799d8f65063e5";
api_netpath = fullpath;
//api_netpath = "/data/data/com.example.joseph.example_app/files/zerotier/nc_565799d8f65063e5";
}
#else
// Get path/nwid from environment variables
// This is used when you're dynamically-linking our library into your application at runtime
if (!api_netpath) {
api_netpath = getenv("ZT_NC_NETWORK");
dwr(MSG_DEBUG, "$ZT_NC_NETWORK = %s\n", api_netpath);
@@ -370,7 +374,7 @@ int (*realclose)(CLOSE_SIG);
printf("path = %s\n", api_netpath);
LOGV("path = %s\n", api_netpath);
int err = rpc_send_command(api_netpath, RPC_SOCKET, -1, &rpc_st, sizeof(struct socket_st));
LOGV("socket() = %s\n", err);
//LOGV("socket() = %d\n", err);
dwr(MSG_DEBUG," socket() = %d\n", err);
return err;
}

View File

@@ -17,10 +17,17 @@
#include "SDK_ServiceSetup.hpp"
// Starts a ZeroTier service at the specified path
// This will only support SOCKS5 Proxy
extern "C" void start_service(const char * path) {
init_service(INTERCEPT_DISABLED, path);
}
// Starts a ZeroTier service at the specified path and initializes the RPC mechanism
// This will allow direct API calls
extern "C" void start_service_and_rpc(const char * path, const char * nwid) {
init_service_and_rpc(INTERCEPT_DISABLED, path, nwid);
}
// Joins a ZeroTier virtual network
extern "C" void zt_join_network(const char * nwid){
join_network(nwid);
@@ -32,9 +39,13 @@ extern "C" void zt_leave_network(const char * nwid){
}
// Explicit ZT API wrappers
extern "C" void zts_init_rpc(const char *path, const char *nwid) {
zt_init_rpc(path, nwid);
}
#if !defined(__IOS__)
// This isn't available for iOS since function interposition isn't as reliable
extern "C" void zts_init_rpc(const char *path, const char *nwid) {
zt_init_rpc(path, nwid);
}
#endif
extern "C" int zts_socket(SOCKET_SIG) {
return zt_socket(socket_family, socket_type, protocol);
}