Add zts_core_query_ and world sub-APIs. Adjust event subsystem
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
@@ -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
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
119
examples/c/customroots.c
Normal file
119
examples/c/customroots.c
Normal file
@@ -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();
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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) {
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
@@ -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");
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user