Skip to content

Commit

Permalink
Add zts_core_query_ and world sub-APIs. Adjust event subsystem
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-henry committed Apr 27, 2021
1 parent ac7e01f commit c456a87
Show file tree
Hide file tree
Showing 22 changed files with 1,117 additions and 790 deletions.
13 changes: 10 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.0)
project(zt)
find_package(Threads)

set (CMAKE_BUILD_PARALLEL_LEVEL 8)

# ------------------------------------------------------------------------------
# | PLATFORM DETECTION |
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -101,7 +103,7 @@ if (ZTS_ENABLE_PINVOKE)
set(ALLOW_INSTALL_TARGET FALSE)
set(BUILD_HOST_SELFTEST FALSE)
# Sources and libraries
set(LANG_WRAPPER_FILE ${LIBZT_SRC_DIR}/bindings/csharp/*.cpp)
set(LANG_WRAPPER_FILE ${LIBZT_SRC_DIR}/bindings/csharp/*.cxx)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DZTS_ENABLE_PINVOKE=1")
endif()

Expand All @@ -119,7 +121,7 @@ if (ZTS_ENABLE_PYTHON)
# Sources and libraries
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
set(LANG_WRAPPER_FILE ${LIBZT_SRC_DIR}/bindings/python/*.cpp)
set(LANG_WRAPPER_FILE ${LIBZT_SRC_DIR}/bindings/python/*.cxx)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DZTS_ENABLE_PYTHON=1")
endif()

Expand All @@ -131,7 +133,7 @@ if (ZTS_ENABLE_JAVA)
set(ALLOW_INSTALL_TARGET FALSE)
set(BUILD_HOST_SELFTEST FALSE)
set(ZTS_ENABLE_STATS FALSE)
set(LANG_WRAPPER_FILE ${LIBZT_SRC_DIR}/bindings/java/*.cpp)
set(LANG_WRAPPER_FILE ${LIBZT_SRC_DIR}/bindings/java/*.cxx)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DZTS_ENABLE_JAVA=1")
endif()

Expand Down Expand Up @@ -286,6 +288,10 @@ if(BUILD_HOST_EXAMPLES)
${PROJ_DIR}/examples/c/nostorage.c)
target_link_libraries(nostorage ${STATIC_LIB_NAME})

add_executable(customroots
${PROJ_DIR}/examples/c/customroots.c)
target_link_libraries(customroots ${STATIC_LIB_NAME})

add_executable(client
${PROJ_DIR}/examples/c/client.c)
target_link_libraries(client ${STATIC_LIB_NAME})
Expand Down Expand Up @@ -316,6 +322,7 @@ set(SILENCE "-Wno-missing-field-initializers \
-Wno-tautological-constant-out-of-range-compare \
-Wno-parentheses-equality")

#set(ZT_FLAGS "${ZT_FLAGS} -DNO_GETADDRINFO=1")
set(ZT_FLAGS "${ZT_FLAGS} -DZT_USE_MINIUPNPC=1")
set(ZT_FLAGS "${ZT_FLAGS} -D_USING_LWIP_DEFINITIONS_=0")

Expand Down
7 changes: 3 additions & 4 deletions examples/c/adhoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int main(int argc, char** argv)

uint16_t adhocStartPort = atoi(argv[1]); // Start of port range your application will use
uint16_t adhocEndPort = atoi(argv[2]); // End of port range your application will use
uint64_t net_id = zts_net_compute_adhoc_id(adhocStartPort, adhocEndPort);
long long int net_id = zts_net_compute_adhoc_id(adhocStartPort, adhocEndPort); // At least 64 bits

// Start node and get identity

Expand All @@ -71,15 +71,14 @@ int main(int argc, char** argv)
exit(1);
}
printf("Waiting for join to complete\n");
while (zts_net_count() < 1) {
while (! zts_net_transport_is_ready(net_id)) {
zts_util_delay(50);
}

// Get address

char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
if ((err = zts_addr_compute_rfc4193_str(net_id, node_id, ipstr, ZTS_IP_MAX_STR_LEN))
!= ZTS_ERR_OK) {
if ((err = zts_addr_compute_rfc4193_str(net_id, node_id, ipstr, ZTS_IP_MAX_STR_LEN)) != ZTS_ERR_OK) {
printf("Unable to compute address (error = %d). Exiting.\n", err);
exit(1);
}
Expand Down
13 changes: 4 additions & 9 deletions examples/c/callbackapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ void on_zts_event(void* msgPtr)
}
// Virtual network events
if (msg->event_code == ZTS_EVENT_NETWORK_NOT_FOUND) {
printf(
"ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
msg->network->net_id);
printf("ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n", msg->network->net_id);
}
if (msg->event_code == ZTS_EVENT_NETWORK_ACCESS_DENIED) {
printf(
Expand All @@ -44,10 +42,7 @@ void on_zts_event(void* msgPtr)
char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 };
struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN);
printf(
"ZTS_EVENT_ADDR_NEW_IP6 --- Join %llx and ping me at %s\n",
msg->addr->net_id,
ipstr);
printf("ZTS_EVENT_ADDR_NEW_IP6 --- Join %llx and ping me at %s\n", msg->addr->net_id, ipstr);
}

// To see more exhaustive examples look at test/selftest.c
Expand All @@ -60,7 +55,7 @@ int main(int argc, char** argv)
printf("pingable-node <net_id>\n");
exit(0);
}
uint64_t net_id = strtoull(argv[1], NULL, 16);
long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits

zts_init_set_event_handler(&on_zts_event);

Expand All @@ -87,7 +82,7 @@ int main(int argc, char** argv)
}

printf("Waiting for join to complete\n");
while (zts_net_count() < 1) {
while (! zts_net_transport_is_ready(net_id)) {
zts_util_delay(50);
}

Expand Down
6 changes: 3 additions & 3 deletions examples/c/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char** argv)
exit(0);
}
char* storage_path = argv[1];
uint64_t net_id = strtoull(argv[2], NULL, 16);
long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits
char* remote_addr = argv[3];
int remote_port = atoi(argv[4]);
int err = ZTS_ERR_OK;
Expand All @@ -40,7 +40,7 @@ int main(int argc, char** argv)
while (! zts_node_is_online()) {
zts_util_delay(50);
}
printf("Public identity (node ID) is %llx\n", zts_node_get_id());
printf("Public identity (node ID) is %llx\n", (long long int)zts_node_get_id());

// Join network

Expand All @@ -51,7 +51,7 @@ int main(int argc, char** argv)
}
printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
printf("Waiting for join to complete\n");
while (zts_net_count() < 1) {
while (! zts_net_transport_is_ready(net_id)) {
zts_util_delay(50);
}

Expand Down
119 changes: 119 additions & 0 deletions examples/c/customroots.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* libzt C API example
*
* An example demonstrating how to define your own planet. In this example
* we limit the roots to US-only.
*/

#include "ZeroTierSockets.h"

#include <stdio.h>
#include <stdlib.h>

void print_peer_details(const char* msg, zts_peer_info_t* d)
{
printf(" %s\n", msg);
printf("\t- peer : %llx\n", d->address);
printf("\t- role : %d\n", d->role);
printf("\t- latency : %d\n", d->latency);
printf("\t- version : %d.%d.%d\n", d->ver_major, d->ver_minor, d->ver_rev);
printf("\t- path_count : %d\n", d->path_count);
printf("\t- paths:\n");

// Print all known paths for each peer
for (unsigned int j = 0; j < d->path_count; j++) {
char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 };
int port = 0;
struct zts_sockaddr* sa = (struct zts_sockaddr*)&(d->paths[j].address);
if (sa->sa_family == ZTS_AF_INET) {
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)sa;
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
port = ntohs(in4->sin_port);
}
if (sa->sa_family == ZTS_AF_INET6) {
struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)sa;
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN);
}
printf("\t - %15s : %6d\n", ipstr, port);
}
printf("\n\n");
}

void on_zts_event(void* msgPtr)
{
zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr;
printf("event_code = %d\n", msg->event_code);

if (msg->peer) {
if (msg->peer->role != ZTS_PEER_ROLE_PLANET) {
return; // Don't print controllers and ordinary nodes.
}
}
if (msg->event_code == ZTS_EVENT_PEER_DIRECT) {
print_peer_details("ZTS_EVENT_PEER_DIRECT", msg->peer);
}
if (msg->event_code == ZTS_EVENT_PEER_RELAY) {
print_peer_details("ZTS_EVENT_PEER_RELAY", msg->peer);
}
}

int main()
{
// World generation

// Buffers that will be filled after generating the world
char world_data_out[4096] = { 0 }; // (binary) Your new world definition
unsigned int world_len = 0;
unsigned int prev_key_len = 0;
unsigned int curr_key_len = 0;
char prev_key[4096] = { 0 }; // (binary) (optional) For updating a world
char curr_key[4096] = { 0 }; // (binary) You should save this

// Arbitrary World ID
uint64_t id = 149604618;

// Timestamp indicating when this world was generated
uint64_t ts = 1567191349589ULL;

// struct containing public keys and stable IP endpoints for roots
zts_world_t world = { 0 };

world.public_id_str[0] =
"992fcf1db7:0:"
"206ed59350b31916f749a1f85dffb3a8787dcbf83b8c6e9448d4e3ea0e3369301be716c3609344a9d1533850fb4460c5"
"0af43322bcfc8e13d3301a1f1003ceb6";
world.endpoint_ip_str[0][0] = "195.181.173.159/9993";
world.endpoint_ip_str[0][1] = "2a02:6ea0:c024::/9993";

// Generate world

zts_util_world_new(&world_data_out, &world_len, &prev_key, &prev_key_len, &curr_key, &curr_key_len, id, ts, &world);

printf("world_data_out= ");
for (int i = 0; i < world_len; i++) {
if (i > 0) {
printf(",");
}
printf("0x%.2x", (unsigned char)world_data_out[i]);
}
printf("\n");
printf("world_len = %d\n", world_len);
printf("prev_key_len = %d\n", prev_key_len);
printf("curr_key_len = %d\n", curr_key_len);

// Now, initialize node and use newly-generated world definition

zts_init_set_world(&world_data_out, world_len);
zts_init_set_event_handler(&on_zts_event);
zts_init_from_storage(".");

// Start node

zts_node_start();

while (1) {
zts_util_delay(500);
}

return zts_node_stop();
}
6 changes: 3 additions & 3 deletions examples/c/nonblockingclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char** argv)
exit(0);
}
char* storage_path = argv[1];
uint64_t net_id = strtoull(argv[2], NULL, 16);
long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits
char* remote_addr = argv[3];
int remote_port = atoi(argv[4]);
int err = ZTS_ERR_OK;
Expand All @@ -42,7 +42,7 @@ int main(int argc, char** argv)
zts_util_delay(50);
}

printf("Public identity (node ID) is %llx\n", zts_node_get_id());
printf("Public identity (node ID) is %llx\n", (long long int)zts_node_get_id());

printf("Joining network %llx\n", net_id);
if (zts_net_join(net_id) != ZTS_ERR_OK) {
Expand All @@ -52,7 +52,7 @@ int main(int argc, char** argv)

printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
printf("Waiting for join to complete\n");
while (zts_net_count() < 1) {
while (! zts_net_transport_is_ready(net_id)) {
zts_util_delay(50);
}

Expand Down
4 changes: 2 additions & 2 deletions examples/c/nonblockingserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char** argv)
exit(0);
}
char* storage_path = argv[1];
uint64_t net_id = strtoull(argv[2], NULL, 16);
long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits
char* local_addr = argv[3];
int local_port = atoi(argv[4]);
int fd, accfd;
Expand Down Expand Up @@ -53,7 +53,7 @@ int main(int argc, char** argv)

printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
printf("Waiting for join to complete\n");
while (zts_net_count() < 1) {
while (! zts_net_transport_is_ready(net_id)) {
zts_util_delay(50);
}

Expand Down
8 changes: 4 additions & 4 deletions examples/c/pingable-node.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ int main(int argc, char** argv)
printf("pingable-node <net_id>\n");
exit(0);
}
uint64_t net_id = strtoull(argv[1], NULL, 16);
long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits

printf("Starting node...\n");
zts_node_start();
Expand All @@ -26,9 +26,9 @@ int main(int argc, char** argv)
zts_util_delay(50);
}

printf("My public identity (node ID) is %llx\n", zts_node_get_id());
printf("My public identity (node ID) is %llx\n", (long long int)zts_node_get_id());
char keypair[ZTS_ID_STR_BUF_LEN] = { 0 };
uint16_t len = ZTS_ID_STR_BUF_LEN;
unsigned int len = ZTS_ID_STR_BUF_LEN;
if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) {
printf("Error getting identity keypair. Exiting.\n");
}
Expand All @@ -41,7 +41,7 @@ int main(int argc, char** argv)
}

printf("Waiting for join to complete\n");
while (zts_net_count() < 1) {
while (! zts_net_transport_is_ready(net_id)) {
zts_util_delay(50);
}

Expand Down
7 changes: 3 additions & 4 deletions examples/c/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char** argv)
exit(0);
}
char* storage_path = argv[1];
uint64_t net_id = strtoull(argv[2], NULL, 16);
long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits
char* local_addr = argv[3];
int local_port = atoi(argv[4]);
int fd, accfd;
Expand Down Expand Up @@ -52,7 +52,7 @@ int main(int argc, char** argv)
}
printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
printf("Waiting for join to complete\n");
while (zts_net_count() < 1) {
while (! zts_net_transport_is_ready(net_id)) {
zts_util_delay(50);
}

Expand All @@ -77,8 +77,7 @@ int main(int argc, char** argv)
char remote_addr[ZTS_INET6_ADDRSTRLEN] = { 0 };
int remote_port = 0;
int len = ZTS_INET6_ADDRSTRLEN;
if ((accfd = zts_simple_tcp_server(local_addr, local_port, remote_addr, len, &remote_port))
< 0) {
if ((accfd = zts_simple_tcp_server(local_addr, local_port, remote_addr, len, &remote_port)) < 0) {
printf("Error (fd=%d, zts_errno=%d). Exiting.\n", accfd, zts_errno);
exit(1);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/c/statistics.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int argc, char** argv)
printf("pingable-node <net_id>\n");
exit(0);
}
uint64_t net_id = strtoull(argv[1], NULL, 16);
long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits

printf("Starting node...\n");
zts_node_start();
Expand All @@ -42,7 +42,7 @@ int main(int argc, char** argv)
}

printf("Waiting for join to complete\n");
while (zts_net_count() < 1) {
while (! zts_net_transport_is_ready(net_id)) {
zts_util_delay(50);
}

Expand Down
Loading

0 comments on commit c456a87

Please sign in to comment.