Added SocketTap multiplexing logic
This commit is contained in:
@@ -25,10 +25,6 @@
|
||||
* LLC. Start here: http://www.zerotier.com/
|
||||
*/
|
||||
|
||||
#if defined(__ANDROID__) || defined(__JNI_LIB__)
|
||||
#include <jni.h>
|
||||
#endif
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include <sys/socket.h>
|
||||
#include <stdio.h>
|
||||
@@ -57,58 +53,52 @@ extern "C" {
|
||||
|
||||
static ZeroTier::OneService *zt1Service;
|
||||
|
||||
std::string service_path;
|
||||
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; // Where network .conf files are to be written
|
||||
|
||||
|
||||
/****************************************************************************/
|
||||
/* SDK Socket API */
|
||||
/* SDK Socket API - Language Bindings are written in terms of these */
|
||||
/****************************************************************************/
|
||||
|
||||
void zts_start(const char *path)
|
||||
{
|
||||
if(zt1Service)
|
||||
return;
|
||||
if(ZeroTier::picostack)
|
||||
return;
|
||||
|
||||
ZeroTier::picostack = new ZeroTier::picoTCP();
|
||||
pico_stack_init();
|
||||
|
||||
DEBUG_INFO("path=%s", path);
|
||||
if(path)
|
||||
homeDir = path;
|
||||
zts_start_core_service(NULL);
|
||||
pthread_t service_thread;
|
||||
pthread_create(&service_thread, NULL, _start_service, (void *)(path));
|
||||
}
|
||||
|
||||
// Stop the service, proxy server, stack, etc
|
||||
void zts_stop() {
|
||||
DEBUG_INFO();
|
||||
zts_stop_service();
|
||||
if(zt1Service) {
|
||||
zt1Service->terminate();
|
||||
zt1Service->removeNets();
|
||||
}
|
||||
}
|
||||
|
||||
char *zts_core_version() {
|
||||
return (char*)"1.2.2";
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// --------------------------------- Base zts_* API -----------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
// Prototypes
|
||||
void *zts_start_core_service(void *thread_id);
|
||||
void zts_init_rpc(const char * path, const char * nwid);
|
||||
|
||||
// Basic ZT service controls
|
||||
void zts_join_network(const char * nwid) {
|
||||
DEBUG_ERROR();
|
||||
std::string confFile = zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf";
|
||||
if(!ZeroTier::OSUtils::mkdir(netDir)) {
|
||||
DEBUG_ERROR("unable to create: %s", netDir.c_str());
|
||||
if(zt1Service) {
|
||||
std::string confFile = zt1Service->givenHomePath() + "/networks.d/" + nwid + ".conf";
|
||||
if(!ZeroTier::OSUtils::mkdir(netDir))
|
||||
DEBUG_ERROR("unable to create: %s", netDir.c_str());
|
||||
if(!ZeroTier::OSUtils::writeFile(confFile.c_str(), ""))
|
||||
DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
|
||||
zt1Service->join(nwid);
|
||||
// Provide the API with the RPC information
|
||||
// zts_init_rpc(homeDir.c_str(), nwid);
|
||||
}
|
||||
if(!ZeroTier::OSUtils::writeFile(confFile.c_str(), "")) {
|
||||
DEBUG_ERROR("unable to write network conf file: %s", confFile.c_str());
|
||||
}
|
||||
zt1Service->join(nwid);
|
||||
// Provide the API with the RPC information
|
||||
zts_init_rpc(homeDir.c_str(), nwid);
|
||||
}
|
||||
// Just create the dir and conf file required, don't instruct the core to do anything
|
||||
|
||||
void zts_join_network_soft(const char * filepath, const char * nwid) {
|
||||
std::string net_dir = std::string(filepath) + "/networks.d/";
|
||||
std::string confFile = net_dir + std::string(nwid) + ".conf";
|
||||
@@ -121,105 +111,142 @@ 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);
|
||||
}
|
||||
// Check whether the service is running
|
||||
int zts_service_is_running() {
|
||||
return !zt1Service ? false : zt1Service->isRunning();
|
||||
}
|
||||
// Stop the service
|
||||
void zts_stop_service() {
|
||||
if(zt1Service)
|
||||
zt1Service->terminate();
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
void zts_get_homepath(char *homePath, int len) {
|
||||
if(homeDir.length())
|
||||
memcpy(homePath, homeDir.c_str(), len < homeDir.length() ? len : homeDir.length());
|
||||
}
|
||||
|
||||
// FIXME: Re-implemented to make it play nicer with the C-linkage required for Xcode integrations
|
||||
// Now only returns first assigned address per network. Shouldn't normally be a problem.
|
||||
void zts_core_version(char *ver) {
|
||||
int major, minor, revision;
|
||||
ZT_version(&major, &minor, &revision);
|
||||
sprintf(ver, "%d.%d.%d", major, minor, revision);
|
||||
}
|
||||
|
||||
// Get IPV4 Address for this device on given network
|
||||
int zts_has_address(const char *nwid)
|
||||
{
|
||||
char ipv4_addr[64], ipv6_addr[64];
|
||||
memset(ipv4_addr, 0, 64);
|
||||
memset(ipv6_addr, 0, 64);
|
||||
zts_get_ipv4_address(nwid, ipv4_addr);
|
||||
zts_get_ipv6_address(nwid, ipv6_addr);
|
||||
if(!strcmp(ipv4_addr, "-1.-1.-1.-1/-1") && !strcmp(ipv4_addr, "-1.-1.-1.-1/-1")) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
void zts_sdk_version(char *ver) {
|
||||
sprintf(ver, "%d.%d.%d", ZT_SDK_VERSION_MAJOR, ZT_SDK_VERSION_MINOR, ZT_SDK_VERSION_REVISION);
|
||||
}
|
||||
void zts_get_ipv4_address(const char *nwid, char *addrstr)
|
||||
{
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
ZeroTier::SocketTap *tap = zt1Service->getTap(nwid_int);
|
||||
if(tap && tap->_ips.size()){
|
||||
for(int i=0; i<tap->_ips.size(); i++) {
|
||||
if(tap->_ips[i].isV4()) {
|
||||
std::string addr = tap->_ips[i].toString();
|
||||
// DEBUG_EXTRA("addr=%s, addrlen=%d", addr.c_str(), addr.length());
|
||||
memcpy(addrstr, addr.c_str(), addr.length()); // first address found that matches protocol version 4
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
memcpy(addrstr, "-1.-1.-1.-1/-1", 14);
|
||||
}
|
||||
}
|
||||
// Get IPV6 Address for this device on given network
|
||||
void zts_get_ipv6_address(const char *nwid, char *addrstr)
|
||||
{
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
ZeroTier::SocketTap *tap = zt1Service->getTap(nwid_int);
|
||||
if(tap && tap->_ips.size()){
|
||||
for(int i=0; i<tap->_ips.size(); i++) {
|
||||
if(tap->_ips[i].isV6()) {
|
||||
std::string addr = tap->_ips[i].toString();
|
||||
// DEBUG_EXTRA("addr=%s, addrlen=%d", addr.c_str(), addr.length());
|
||||
memcpy(addrstr, addr.c_str(), addr.length()); // first address found that matches protocol version 4
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
memcpy(addrstr, "-1.-1.-1.-1/-1", 14);
|
||||
}
|
||||
}
|
||||
// Get device ID (from running service)
|
||||
|
||||
int zts_get_device_id(char *devID) {
|
||||
if(zt1Service) {
|
||||
char id[10];
|
||||
char id[ZT_ID_LEN+1];
|
||||
sprintf(id, "%lx",zt1Service->getNode()->address());
|
||||
memcpy(devID, id, 10);
|
||||
memcpy(devID, id, ZT_ID_LEN+1);
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
// 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);
|
||||
else // Service isn't online, try to read ID from file
|
||||
{
|
||||
std::string fname("identity.public");
|
||||
std::string fpath(homeDir);
|
||||
|
||||
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;
|
||||
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(), ZT_ID_LEN); // first 10 bytes of file
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
// Get the IP address of a peer if a direct path is available
|
||||
|
||||
int zts_service_running() {
|
||||
return !zt1Service ? false : zt1Service->isRunning();
|
||||
}
|
||||
|
||||
int zts_has_ipv4_address(const char *nwid)
|
||||
{
|
||||
char ipv4_addr[ZT_MAX_IPADDR_LEN];
|
||||
memset(ipv4_addr, 0, ZT_MAX_IPADDR_LEN);
|
||||
zts_get_ipv4_address(nwid, ipv4_addr, ZT_MAX_IPADDR_LEN);
|
||||
return strcmp(ipv4_addr, "\0");
|
||||
}
|
||||
|
||||
int zts_has_ipv6_address(const char *nwid)
|
||||
{
|
||||
char ipv6_addr[ZT_MAX_IPADDR_LEN];
|
||||
memset(ipv6_addr, 0, ZT_MAX_IPADDR_LEN);
|
||||
zts_get_ipv6_address(nwid, ipv6_addr, ZT_MAX_IPADDR_LEN);
|
||||
return strcmp(ipv6_addr, "\0");
|
||||
}
|
||||
|
||||
int zts_has_address(const char *nwid)
|
||||
{
|
||||
return zts_has_ipv4_address(nwid) || zts_has_ipv6_address(nwid);
|
||||
}
|
||||
|
||||
void zts_get_ipv4_address(const char *nwid, char *addrstr, const int addrlen)
|
||||
{
|
||||
if(zt1Service) {
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
ZeroTier::SocketTap *tap = zt1Service->getTap(nwid_int);
|
||||
if(tap && tap->_ips.size()){
|
||||
for(int i=0; i<tap->_ips.size(); i++) {
|
||||
if(tap->_ips[i].isV4()) {
|
||||
std::string addr = tap->_ips[i].toString();
|
||||
int len = addrlen < addr.length() ? addrlen : addr.length();
|
||||
memset(addrstr, 0, len);
|
||||
memcpy(addrstr, addr.c_str(), len);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
memcpy(addrstr, "\0", 1);
|
||||
}
|
||||
|
||||
void zts_get_ipv6_address(const char *nwid, char *addrstr, const int addrlen)
|
||||
{
|
||||
if(zt1Service) {
|
||||
uint64_t nwid_int = strtoull(nwid, NULL, 16);
|
||||
ZeroTier::SocketTap *tap = zt1Service->getTap(nwid_int);
|
||||
if(tap && tap->_ips.size()){
|
||||
for(int i=0; i<tap->_ips.size(); i++) {
|
||||
if(tap->_ips[i].isV6()) {
|
||||
std::string addr = tap->_ips[i].toString();
|
||||
int len = addrlen < addr.length() ? addrlen : addr.length();
|
||||
memset(addrstr, 0, len);
|
||||
memcpy(addrstr, addr.c_str(), len);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
memcpy(addrstr, "\0", 1);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
unsigned long zts_get_peer_count() {
|
||||
if(zt1Service)
|
||||
return zt1Service->getNode()->peers()->peerCount;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int zts_get_peer_address(char *peer, const char *devID) {
|
||||
if(zt1Service) {
|
||||
ZT_PeerList *pl = zt1Service->getNode()->peers();
|
||||
@@ -233,49 +260,168 @@ int zts_get_peer_address(char *peer, const char *devID) {
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
// Return the number of peers on this network
|
||||
unsigned long zts_get_peer_count() {
|
||||
if(zt1Service)
|
||||
return zt1Service->getNode()->peers()->peerCount;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
// 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)
|
||||
|
||||
void zts_enable_http_control_plane()
|
||||
{
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// ------------------------------ EXPORTED JNI METHODS --------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
// JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME
|
||||
void zts_disable_http_control_plane()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/* SocketTap Multiplexer Functionality --- DONT CALL THESE DIRECTLY */
|
||||
/* - This section of the API is used to implement the general socket */
|
||||
/* controls. Basically this is designed to handle socket provisioning */
|
||||
/* requests when no SocketTap is yet initialized, and as a way to */
|
||||
/* determine which SocketTap is to be used for a particular connect() or */
|
||||
/* bind() call */
|
||||
/****************************************************************************/
|
||||
|
||||
namespace ZeroTier
|
||||
{
|
||||
picoTCP *picostack = NULL;
|
||||
std::map<int, Connection*> UnassignedConnections;
|
||||
}
|
||||
|
||||
ZeroTier::Mutex _multiplexer_lock;
|
||||
|
||||
int zts_multiplex_new_socket(ZT_SOCKET_SIG)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
_multiplexer_lock.lock();
|
||||
ZeroTier::Connection *conn = new ZeroTier::Connection();
|
||||
int err;
|
||||
// set up pico_socket
|
||||
struct pico_socket * psock;
|
||||
int pico_protocol, protocol_version;
|
||||
#if defined(SDK_IPV4)
|
||||
protocol_version = PICO_PROTO_IPV4;
|
||||
#elif defined(SDK_IPV6)
|
||||
protocol_version = PICO_PROTO_IPV6;
|
||||
#endif
|
||||
if(socket_type == SOCK_DGRAM) {
|
||||
pico_protocol = PICO_PROTO_UDP;
|
||||
psock = pico_socket_open(protocol_version, pico_protocol, &ZeroTier::picoTCP::pico_cb_socket_activity);
|
||||
}
|
||||
if(socket_type == SOCK_STREAM) {
|
||||
pico_protocol = PICO_PROTO_TCP;
|
||||
psock = pico_socket_open(protocol_version, pico_protocol, &ZeroTier::picoTCP::pico_cb_socket_activity);
|
||||
}
|
||||
// set up Unix Domain socket (used for data later on)
|
||||
if(psock) {
|
||||
int unix_data_sock;
|
||||
if((unix_data_sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
|
||||
DEBUG_ERROR("unable to create unix domain socket for data");
|
||||
// errno = ?
|
||||
err = -1;
|
||||
}
|
||||
else {
|
||||
conn->socket_family = socket_family;
|
||||
conn->socket_type = socket_type;
|
||||
conn->data_sock = unix_data_sock;
|
||||
conn->picosock = psock;
|
||||
memset(conn->rxbuf, 0, DEFAULT_UDP_RX_BUF_SZ);
|
||||
ZeroTier::UnassignedConnections[unix_data_sock] = conn;
|
||||
err = unix_data_sock;
|
||||
}
|
||||
}
|
||||
else {
|
||||
DEBUG_ERROR("failed to create pico_socket");
|
||||
err = -1;
|
||||
}
|
||||
_multiplexer_lock.unlock();
|
||||
return err;
|
||||
}
|
||||
|
||||
int zts_multiplex_new_connect(ZT_CONNECT_SIG)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
if(!zt1Service) {
|
||||
// errno = ?
|
||||
return -1;
|
||||
}
|
||||
|
||||
_multiplexer_lock.lock();
|
||||
int err;
|
||||
ZeroTier::Connection *conn = ZeroTier::UnassignedConnections[fd];
|
||||
|
||||
if(conn != NULL) {
|
||||
char ipstr[INET6_ADDRSTRLEN];//, nm_str[INET6_ADDRSTRLEN];
|
||||
memset(ipstr, 0, INET6_ADDRSTRLEN);
|
||||
ZeroTier::InetAddress iaddr;
|
||||
|
||||
if(conn->socket_family == AF_INET) {
|
||||
// FIXME: Fix this typecast mess
|
||||
inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)addr)->sin_addr.s_addr, ipstr, INET_ADDRSTRLEN);
|
||||
}
|
||||
if(conn->socket_family == AF_INET6) {
|
||||
// FIXME: Fix this typecast mess
|
||||
inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)addr)->sin6_addr.s6_addr, ipstr, INET6_ADDRSTRLEN);
|
||||
}
|
||||
|
||||
iaddr.fromString(ipstr);
|
||||
|
||||
DEBUG_INFO("ipstr= %s", ipstr);
|
||||
DEBUG_INFO("iaddr= %s", iaddr.toString().c_str());
|
||||
|
||||
ZeroTier::SocketTap *tap = zt1Service->getTap(iaddr);
|
||||
|
||||
if(!tap) {
|
||||
DEBUG_ERROR("no route to host");
|
||||
// errno = ?
|
||||
err = -1;
|
||||
}
|
||||
else {
|
||||
DEBUG_INFO("found appropriate SocketTap");
|
||||
err = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
DEBUG_ERROR("unable to locate connection");
|
||||
// errno = ?
|
||||
err = -1;
|
||||
}
|
||||
_multiplexer_lock.unlock();
|
||||
return err;
|
||||
}
|
||||
|
||||
int zts_multiplex_new_bind(ZT_BIND_SIG)
|
||||
{
|
||||
DEBUG_INFO();
|
||||
_multiplexer_lock.lock();
|
||||
int err;
|
||||
|
||||
// ?
|
||||
|
||||
_multiplexer_lock.unlock();
|
||||
return err;
|
||||
}
|
||||
|
||||
/****************************************************************************/
|
||||
/* SDK Socket API (Java Native Interface JNI) */
|
||||
/* JNI naming convention: Java_PACKAGENAME_CLASSNAME_METHODNAME */
|
||||
/****************************************************************************/
|
||||
|
||||
#if defined(__ANDROID__) || defined(__JNI_LIB__)
|
||||
// Returns whether the ZeroTier service is running
|
||||
JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_zt_1service_1is_1running(JNIEnv *env, jobject thisObj) {
|
||||
if(zt1Service)
|
||||
return zts_service_is_running();
|
||||
return false;
|
||||
JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_zt_1service_1is_1running(
|
||||
JNIEnv *env, jobject thisObj)
|
||||
{
|
||||
return zts_service_is_running();
|
||||
}
|
||||
// Returns path for ZT config/data files
|
||||
JNIEXPORT jstring JNICALL Java_zerotier_ZeroTier_zt_1get_1homepath(JNIEnv *env, jobject thisObj) {
|
||||
JNIEXPORT jstring JNICALL Java_zerotier_ZeroTier_zt_1get_1homepath(
|
||||
JNIEnv *env, jobject thisObj)
|
||||
{
|
||||
return (*env).NewStringUTF(zts_get_homepath());
|
||||
}
|
||||
// Join a network
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_zt_1join_1network(JNIEnv *env, jobject thisObj, jstring nwid) {
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_zt_1join_1network(
|
||||
JNIEnv *env, jobject thisObj, jstring nwid)
|
||||
{
|
||||
const char *nwidstr;
|
||||
if(nwid) {
|
||||
nwidstr = env->GetStringUTFChars(nwid, NULL);
|
||||
@@ -283,7 +429,9 @@ void zts_get_rfc4193_addr(char *addr, const char *nwid, const char *devID)
|
||||
}
|
||||
}
|
||||
// Leave a network
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_zt_1leave_1network(JNIEnv *env, jobject thisObj, jstring nwid) {
|
||||
JNIEXPORT void JNICALL Java_zerotier_ZeroTier_zt_1leave_1network(
|
||||
JNIEnv *env, jobject thisObj, jstring nwid)
|
||||
{
|
||||
const char *nwidstr;
|
||||
if(nwid) {
|
||||
nwidstr = env->GetStringUTFChars(nwid, NULL);
|
||||
@@ -292,7 +440,9 @@ void zts_get_rfc4193_addr(char *addr, const char *nwid, const char *devID)
|
||||
}
|
||||
// FIXME: Re-implemented to make it play nicer with the C-linkage required for Xcode integrations
|
||||
// Now only returns first assigned address per network. Shouldn't normally be a problem
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_zt_1get_1ipv4_1address(JNIEnv *env, jobject thisObj, jstring nwid) {
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_zt_1get_1ipv4_1address(
|
||||
JNIEnv *env, jobject thisObj, jstring nwid)
|
||||
{
|
||||
const char *nwid_str = env->GetStringUTFChars(nwid, NULL);
|
||||
char address_string[32];
|
||||
memset(address_string, 0, 32);
|
||||
@@ -304,7 +454,9 @@ void zts_get_rfc4193_addr(char *addr, const char *nwid, const char *devID)
|
||||
return addresses;
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_zt_1get_1ipv6_1address(JNIEnv *env, jobject thisObj, jstring nwid) {
|
||||
JNIEXPORT jobject JNICALL Java_zerotier_ZeroTier_zt_1get_1ipv6_1address(
|
||||
JNIEnv *env, jobject thisObj, jstring nwid)
|
||||
{
|
||||
const char *nwid_str = env->GetStringUTFChars(nwid, NULL);
|
||||
char address_string[32];
|
||||
memset(address_string, 0, 32);
|
||||
@@ -317,54 +469,25 @@ void zts_get_rfc4193_addr(char *addr, const char *nwid, const char *devID)
|
||||
}
|
||||
|
||||
// Returns the device is in integer form
|
||||
JNIEXPORT jint Java_zerotier_ZeroTier_zt_1get_1device_1id() {
|
||||
JNIEXPORT jint Java_zerotier_ZeroTier_zt_1get_1device_1id()
|
||||
{
|
||||
return zts_get_device_id(NULL); // TODO
|
||||
}
|
||||
// Returns whether the path to an endpoint is currently relayed by a root server
|
||||
JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_zt_1is_1relayed() {
|
||||
JNIEXPORT jboolean JNICALL Java_zerotier_ZeroTier_zt_1is_1relayed()
|
||||
{
|
||||
return 0;
|
||||
// TODO
|
||||
// zts_is_relayed();
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// --------------------------- zts_start_core_service ---------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
/****************************************************************************/
|
||||
/* SDK Socket API Helper functions --- DONT CALL THESE DIRECTLY */
|
||||
/****************************************************************************/
|
||||
|
||||
// Starts a ZeroTier service in the background
|
||||
void *zts_start_core_service(void *thread_id) {
|
||||
|
||||
#if defined(SDK_BUNDLED)
|
||||
if(thread_id)
|
||||
homeDir = std::string((char*)thread_id);
|
||||
#endif
|
||||
|
||||
#if defined(__IOS__)
|
||||
char current_dir[MAX_DIR_SZ];
|
||||
// Go to the app's data directory so we can shorten the sun_path we bind to
|
||||
getcwd(current_dir, MAX_DIR_SZ);
|
||||
std::string targetDir = homeDir; // + "/../../";
|
||||
chdir(targetDir.c_str());
|
||||
homeDir = localHomeDir;
|
||||
#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(__APPLE__) && !defined(__IOS__)
|
||||
localHomeDir = homeDir; // Used for RPC and *can* differ from homeDir on some platforms
|
||||
#endif
|
||||
void *_start_service(void *thread_id) {
|
||||
|
||||
DEBUG_INFO("homeDir=%s", homeDir.c_str());
|
||||
// Where network .conf files will be stored
|
||||
@@ -373,7 +496,8 @@ void *zts_start_core_service(void *thread_id) {
|
||||
|
||||
// Construct path for network config and supporting service files
|
||||
if (homeDir.length()) {
|
||||
std::vector<std::string> hpsp(ZeroTier::OSUtils::split(homeDir.c_str(),ZT_PATH_SEPARATOR_S,"",""));
|
||||
std::vector<std::string> hpsp(ZeroTier::OSUtils::split(homeDir.c_str(),
|
||||
ZT_PATH_SEPARATOR_S,"",""));
|
||||
std::string ptmp;
|
||||
if (homeDir[0] == ZT_PATH_SEPARATOR)
|
||||
ptmp.push_back(ZT_PATH_SEPARATOR);
|
||||
@@ -393,18 +517,22 @@ void *zts_start_core_service(void *thread_id) {
|
||||
DEBUG_ERROR("homeDir is empty, could not construct path");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DEBUG_INFO("starting service...");
|
||||
// rpc dir
|
||||
if(!ZeroTier::OSUtils::mkdir(homeDir + "/" + ZT_SDK_RPC_DIR_PREFIX)) {
|
||||
DEBUG_ERROR("unable to create dir: " ZT_SDK_RPC_DIR_PREFIX);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Generate random port for new service instance
|
||||
unsigned int randp = 0;
|
||||
ZeroTier::Utils::getSecureRandom(&randp,sizeof(randp));
|
||||
int servicePort = 9000 + (randp % 1000);
|
||||
int servicePort = 9000 + (randp % 10000);
|
||||
DEBUG_ERROR("servicePort = %d", servicePort);
|
||||
|
||||
for(;;) {
|
||||
zt1Service = ZeroTier::OneService::newInstance(homeDir.c_str(),servicePort);
|
||||
switch(zt1Service->run()) {
|
||||
case ZeroTier::OneService::ONE_STILL_RUNNING: // shouldn't happen, run() won't return until done
|
||||
case ZeroTier::OneService::ONE_STILL_RUNNING:
|
||||
case ZeroTier::OneService::ONE_NORMAL_TERMINATION:
|
||||
break;
|
||||
case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR:
|
||||
@@ -414,11 +542,15 @@ void *zts_start_core_service(void *thread_id) {
|
||||
delete zt1Service;
|
||||
zt1Service = (ZeroTier::OneService *)0;
|
||||
std::string oldid;
|
||||
ZeroTier::OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid);
|
||||
ZeroTier::OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret").c_str(),oldid);
|
||||
if (oldid.length()) {
|
||||
ZeroTier::OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid);
|
||||
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str());
|
||||
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str());
|
||||
ZeroTier::OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret.saved_after_collision").c_str(),oldid);
|
||||
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.secret").c_str());
|
||||
ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S
|
||||
+ "identity.public").c_str());
|
||||
}
|
||||
}
|
||||
continue; // restart!
|
||||
|
||||
Reference in New Issue
Block a user