Added zts_set_service_port()
This commit is contained in:
@@ -75,6 +75,18 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief (optional) Sets the port for the background libzt service. If this function is called
|
||||||
|
* with a port number between 1-65535 it will attempt to bind to that port. If it is called with
|
||||||
|
* a port number of 0 it will attempt to randomly search for an available port. If this function
|
||||||
|
* is never called, the service will try to bind on LIBZT_DEFAULT_PORT which is 9994.
|
||||||
|
*
|
||||||
|
* @usage Should be called at the beginning of your application before `zts_startjoin()`
|
||||||
|
* @param portno Port number
|
||||||
|
* @return 0 if successful; or -1 if failed
|
||||||
|
*/
|
||||||
|
ZT_SOCKET_API int ZTCALL zts_set_service_port(int portno);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Starts libzt
|
* @brief Starts libzt
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -33,6 +33,10 @@
|
|||||||
#ifndef LIBZT_DEFINES_H
|
#ifndef LIBZT_DEFINES_H
|
||||||
#define LIBZT_DEFINES_H
|
#define LIBZT_DEFINES_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default port that libzt will use to support all virtual communication
|
||||||
|
*/
|
||||||
|
#define LIBZT_DEFAULT_PORT 9994
|
||||||
/**
|
/**
|
||||||
* Use ZeroTier Virtual Socket layer to abstract network stack raw API
|
* Use ZeroTier Virtual Socket layer to abstract network stack raw API
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -55,6 +55,8 @@ std::string netDir; // Where network .conf files are to be written
|
|||||||
|
|
||||||
ZeroTier::Mutex _multiplexer_lock;
|
ZeroTier::Mutex _multiplexer_lock;
|
||||||
|
|
||||||
|
int servicePort = LIBZT_DEFAULT_PORT;
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
@@ -216,12 +218,12 @@ void *zts_start_service(void *thread_id)
|
|||||||
DEBUG_ERROR("homeDir is empty, could not construct path");
|
DEBUG_ERROR("homeDir is empty, could not construct path");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
if (servicePort <= 0) {
|
||||||
// Generate random port for new service instance
|
DEBUG_INFO("no port specified, will bind to random port. use zts_set_service_port() if you want.");
|
||||||
unsigned int randp = 0;
|
}
|
||||||
ZeroTier::Utils::getSecureRandom(&randp,sizeof(randp));
|
else {
|
||||||
// TODO: Better port random range selection
|
DEBUG_INFO("binding to port=%d", servicePort);
|
||||||
int servicePort = 9000 + (randp % 1000);
|
}
|
||||||
for (;;) {
|
for (;;) {
|
||||||
zt1Service = ZeroTier::OneService::newInstance(homeDir.c_str(),servicePort);
|
zt1Service = ZeroTier::OneService::newInstance(homeDir.c_str(),servicePort);
|
||||||
switch(zt1Service->run()) {
|
switch(zt1Service->run()) {
|
||||||
@@ -229,7 +231,6 @@ void *zts_start_service(void *thread_id)
|
|||||||
case ZeroTier::OneService::ONE_NORMAL_TERMINATION:
|
case ZeroTier::OneService::ONE_NORMAL_TERMINATION:
|
||||||
break;
|
break;
|
||||||
case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR:
|
case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR:
|
||||||
DEBUG_ERROR("ZTO service port = %d", servicePort);
|
|
||||||
DEBUG_ERROR("fatal error: %s",zt1Service->fatalErrorMessage().c_str());
|
DEBUG_ERROR("fatal error: %s",zt1Service->fatalErrorMessage().c_str());
|
||||||
break;
|
break;
|
||||||
case ZeroTier::OneService::ONE_IDENTITY_COLLISION: {
|
case ZeroTier::OneService::ONE_IDENTITY_COLLISION: {
|
||||||
@@ -292,6 +293,20 @@ int zts_get_address_at_index(
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************/
|
||||||
|
/* ZeroTier Service Controls */
|
||||||
|
/****************************************************************************/
|
||||||
|
|
||||||
|
int zts_set_service_port(int portno)
|
||||||
|
{
|
||||||
|
if (portno > -1 && portno < 65535) {
|
||||||
|
// 0 is allowed, see docs
|
||||||
|
servicePort = portno;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int zts_get_address(const uint64_t nwid, struct sockaddr_storage *addr,
|
int zts_get_address(const uint64_t nwid, struct sockaddr_storage *addr,
|
||||||
const int address_family)
|
const int address_family)
|
||||||
{
|
{
|
||||||
@@ -432,13 +447,16 @@ int zts_start(const char *path, bool blocking = false)
|
|||||||
if (blocking) { // block to prevent service calls before we're ready
|
if (blocking) { // block to prevent service calls before we're ready
|
||||||
ZT_NodeStatus status;
|
ZT_NodeStatus status;
|
||||||
status.online = 0;
|
status.online = 0;
|
||||||
|
DEBUG_EXTRA("waiting for zerotier service thread to start");
|
||||||
while (zts_core_running() == false || zt1Service->getNode() == NULL) {
|
while (zts_core_running() == false || zt1Service->getNode() == NULL) {
|
||||||
api_sleep(ZTO_WRAPPER_CHECK_INTERVAL);
|
api_sleep(ZTO_WRAPPER_CHECK_INTERVAL);
|
||||||
}
|
}
|
||||||
|
DEBUG_EXTRA("waiting for node address assignment");
|
||||||
while (zt1Service->getNode()->address() <= 0) {
|
while (zt1Service->getNode()->address() <= 0) {
|
||||||
api_sleep(ZTO_WRAPPER_CHECK_INTERVAL);
|
api_sleep(ZTO_WRAPPER_CHECK_INTERVAL);
|
||||||
}
|
}
|
||||||
DEBUG_INFO("node=%llx", zts_get_node_id());
|
DEBUG_EXTRA("node=%llx", zts_get_node_id());
|
||||||
|
DEBUG_EXTRA("waiting for node to come online");
|
||||||
while (status.online <= 0) {
|
while (status.online <= 0) {
|
||||||
api_sleep(ZTO_WRAPPER_CHECK_INTERVAL);
|
api_sleep(ZTO_WRAPPER_CHECK_INTERVAL);
|
||||||
zt1Service->getNode()->status(&status);
|
zt1Service->getNode()->status(&status);
|
||||||
|
|||||||
@@ -2963,6 +2963,11 @@ int main(int argc , char *argv[])
|
|||||||
// set start time here since we need to wait for both libzt instances to be online
|
// set start time here since we need to wait for both libzt instances to be online
|
||||||
DEBUG_TEST("app-thread, waiting for libzt to come online...\n");
|
DEBUG_TEST("app-thread, waiting for libzt to come online...\n");
|
||||||
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
|
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
|
||||||
|
int portno = LIBZT_DEFAULT_PORT;
|
||||||
|
if (zts_set_service_port(portno) < 0) {
|
||||||
|
DEBUG_TEST("error, invalid zt service port number: %d", portno);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
zts_startjoin(path.c_str(), nwid);
|
zts_startjoin(path.c_str(), nwid);
|
||||||
uint64_t nodeId = zts_get_node_id();
|
uint64_t nodeId = zts_get_node_id();
|
||||||
DEBUG_TEST("I am %llx, %s", nodeId, me.c_str());
|
DEBUG_TEST("I am %llx, %s", nodeId, me.c_str());
|
||||||
|
|||||||
Reference in New Issue
Block a user