Format according to new ZeroTier standard
This commit is contained in:
@@ -35,62 +35,62 @@ be taken to avoid exposing vulnerable services or sharing unwanted files or othe
|
||||
*/
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 3) {
|
||||
printf("\nUsage:\n");
|
||||
printf("adhoc <adhocStartPort> <adhocEndPort>\n");
|
||||
exit(0);
|
||||
}
|
||||
int err = ZTS_ERR_OK;
|
||||
if (argc != 3) {
|
||||
printf("\nUsage:\n");
|
||||
printf("adhoc <adhocStartPort> <adhocEndPort>\n");
|
||||
exit(0);
|
||||
}
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
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
|
||||
long long int net_id = zts_net_compute_adhoc_id(adhocStartPort, adhocEndPort); // At least 64 bits
|
||||
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
|
||||
long long int net_id = zts_net_compute_adhoc_id(adhocStartPort, adhocEndPort); // At least 64 bits
|
||||
|
||||
// Start node and get identity
|
||||
// Start node and get identity
|
||||
|
||||
printf("Starting node...\n");
|
||||
zts_node_start();
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
uint64_t node_id = zts_node_get_id();
|
||||
printf("My public identity (node ID) is %llx\n", node_id);
|
||||
char keypair[ZTS_ID_STR_BUF_LEN] = { 0 };
|
||||
uint16_t len = ZTS_ID_STR_BUF_LEN;
|
||||
if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) {
|
||||
printf("Error getting identity keypair. Exiting.\n");
|
||||
}
|
||||
printf("Identity [public/secret pair] = %s\n", keypair);
|
||||
printf("Starting node...\n");
|
||||
zts_node_start();
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
uint64_t node_id = zts_node_get_id();
|
||||
printf("My public identity (node ID) is %llx\n", node_id);
|
||||
char keypair[ZTS_ID_STR_BUF_LEN] = { 0 };
|
||||
uint16_t len = ZTS_ID_STR_BUF_LEN;
|
||||
if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) {
|
||||
printf("Error getting identity keypair. Exiting.\n");
|
||||
}
|
||||
printf("Identity [public/secret pair] = %s\n", keypair);
|
||||
|
||||
// Join the adhoc network
|
||||
// Join the adhoc network
|
||||
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for join to complete\n");
|
||||
while (! zts_net_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for join to complete\n");
|
||||
while (! zts_net_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
// Get address
|
||||
// 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) {
|
||||
printf("Unable to compute address (error = %d). Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Join %llx from another machine and ping6 me at %s\n", net_id, ipstr);
|
||||
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) {
|
||||
printf("Unable to compute address (error = %d). Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Join %llx from another machine and ping6 me at %s\n", net_id, ipstr);
|
||||
|
||||
// Do network stuff!
|
||||
// zts_socket, zts_connect, etc
|
||||
// Do network stuff!
|
||||
// zts_socket, zts_connect, etc
|
||||
|
||||
while (1) {
|
||||
zts_util_delay(500); // Idle indefinitely
|
||||
}
|
||||
while (1) {
|
||||
zts_util_delay(500); // Idle indefinitely
|
||||
}
|
||||
|
||||
printf("Stopping node\n");
|
||||
return zts_node_stop();
|
||||
printf("Stopping node\n");
|
||||
return zts_node_stop();
|
||||
}
|
||||
|
||||
@@ -12,97 +12,97 @@
|
||||
|
||||
void on_zts_event(void* msgPtr)
|
||||
{
|
||||
zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr;
|
||||
// Node events
|
||||
if (msg->event_code == ZTS_EVENT_NODE_ONLINE) {
|
||||
printf("ZTS_EVENT_NODE_ONLINE --- This node's ID is %llx\n", msg->node->node_id);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_NODE_OFFLINE) {
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, "
|
||||
"firewall, etc. What ports are you blocking?\n");
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_NETWORK_ACCESS_DENIED) {
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. "
|
||||
"Did you authorize the node yet?\n",
|
||||
msg->network->net_id);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_NETWORK_READY_IP6) {
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP6 --- Network config received. IPv6 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->net_id);
|
||||
}
|
||||
// Address events
|
||||
if (msg->event_code == ZTS_EVENT_ADDR_ADDED_IP6) {
|
||||
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);
|
||||
}
|
||||
zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr;
|
||||
// Node events
|
||||
if (msg->event_code == ZTS_EVENT_NODE_ONLINE) {
|
||||
printf("ZTS_EVENT_NODE_ONLINE --- This node's ID is %llx\n", msg->node->node_id);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_NODE_OFFLINE) {
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, "
|
||||
"firewall, etc. What ports are you blocking?\n");
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_NETWORK_ACCESS_DENIED) {
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. "
|
||||
"Did you authorize the node yet?\n",
|
||||
msg->network->net_id);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_NETWORK_READY_IP6) {
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP6 --- Network config received. IPv6 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->net_id);
|
||||
}
|
||||
// Address events
|
||||
if (msg->event_code == ZTS_EVENT_ADDR_ADDED_IP6) {
|
||||
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);
|
||||
}
|
||||
|
||||
// To see more exhaustive examples look at test/selftest.c
|
||||
// To see more exhaustive examples look at test/selftest.c
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
printf("\nUsage:\n");
|
||||
printf("pingable-node <net_id>\n");
|
||||
exit(0);
|
||||
}
|
||||
long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits
|
||||
if (argc != 2) {
|
||||
printf("\nUsage:\n");
|
||||
printf("pingable-node <net_id>\n");
|
||||
exit(0);
|
||||
}
|
||||
long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits
|
||||
|
||||
zts_init_set_event_handler(&on_zts_event);
|
||||
zts_init_set_event_handler(&on_zts_event);
|
||||
|
||||
printf("Starting node...\n");
|
||||
zts_node_start();
|
||||
printf("Starting node...\n");
|
||||
zts_node_start();
|
||||
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
printf("My public identity (node ID) is %llx\n", zts_node_get_id());
|
||||
char keypair[ZTS_ID_STR_BUF_LEN] = { 0 };
|
||||
uint16_t len = ZTS_ID_STR_BUF_LEN;
|
||||
if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) {
|
||||
printf("Error getting identity keypair. Exiting.\n");
|
||||
}
|
||||
printf("Identity [public/secret pair] = %s\n", keypair);
|
||||
printf("My public identity (node ID) is %llx\n", zts_node_get_id());
|
||||
char keypair[ZTS_ID_STR_BUF_LEN] = { 0 };
|
||||
uint16_t len = ZTS_ID_STR_BUF_LEN;
|
||||
if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) {
|
||||
printf("Error getting identity keypair. Exiting.\n");
|
||||
}
|
||||
printf("Identity [public/secret pair] = %s\n", keypair);
|
||||
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("Waiting for join to complete\n");
|
||||
while (! zts_net_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Waiting for join to complete\n");
|
||||
while (! zts_net_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
printf("Waiting for address assignment from network\n");
|
||||
int err = 0;
|
||||
while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) {
|
||||
zts_util_delay(500);
|
||||
}
|
||||
printf("Waiting for address assignment from network\n");
|
||||
int err = 0;
|
||||
while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) {
|
||||
zts_util_delay(500);
|
||||
}
|
||||
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr);
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr);
|
||||
|
||||
// Do network stuff!
|
||||
// zts_socket, zts_connect, etc
|
||||
// Do network stuff!
|
||||
// zts_socket, zts_connect, etc
|
||||
|
||||
while (1) {
|
||||
zts_util_delay(500); // Idle indefinitely
|
||||
}
|
||||
while (1) {
|
||||
zts_util_delay(500); // Idle indefinitely
|
||||
}
|
||||
|
||||
printf("Stopping node\n");
|
||||
return zts_node_stop();
|
||||
printf("Stopping node\n");
|
||||
return zts_node_stop();
|
||||
}
|
||||
|
||||
@@ -12,94 +12,94 @@
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example client\n");
|
||||
printf("client <id_storage_path> <net_id> <remote_addr> <remote_port>\n");
|
||||
exit(0);
|
||||
}
|
||||
char* storage_path = argv[1];
|
||||
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;
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example client\n");
|
||||
printf("client <id_storage_path> <net_id> <remote_addr> <remote_port>\n");
|
||||
exit(0);
|
||||
}
|
||||
char* storage_path = argv[1];
|
||||
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;
|
||||
|
||||
// Initialize node
|
||||
// Initialize node
|
||||
|
||||
if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Start node
|
||||
// Start node
|
||||
|
||||
if ((err = zts_node_start()) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Public identity (node ID) is %llx\n", (long long int)zts_node_get_id());
|
||||
if ((err = zts_node_start()) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Public identity (node ID) is %llx\n", (long long int)zts_node_get_id());
|
||||
|
||||
// Join network
|
||||
// Join network
|
||||
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
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_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
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_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
// Get assigned address (of the family type we care about)
|
||||
// Get assigned address (of the family type we care about)
|
||||
|
||||
int family = zts_util_get_ip_family(remote_addr);
|
||||
int family = zts_util_get_ip_family(remote_addr);
|
||||
|
||||
printf("Waiting for address assignment from network\n");
|
||||
while (! (err = zts_addr_is_assigned(net_id, family))) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, family, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("IP address on network %llx is %s\n", net_id, ipstr);
|
||||
printf("Waiting for address assignment from network\n");
|
||||
while (! (err = zts_addr_is_assigned(net_id, family))) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, family, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("IP address on network %llx is %s\n", net_id, ipstr);
|
||||
|
||||
// BEGIN Socket Stuff
|
||||
// BEGIN Socket Stuff
|
||||
|
||||
char* msgStr = (char*)"Welcome to the machine";
|
||||
int bytes = 0, fd;
|
||||
char recvBuf[128] = { 0 };
|
||||
memset(recvBuf, 0, sizeof(recvBuf));
|
||||
char* msgStr = (char*)"Welcome to the machine";
|
||||
int bytes = 0, fd;
|
||||
char recvBuf[128] = { 0 };
|
||||
memset(recvBuf, 0, sizeof(recvBuf));
|
||||
|
||||
// Connect to remote host
|
||||
// Connect to remote host
|
||||
|
||||
// Can also use traditional: zts_socket(), zts_connect(), etc
|
||||
// Can also use traditional: zts_socket(), zts_connect(), etc
|
||||
|
||||
printf("Attempting to connect...\n");
|
||||
while ((fd = zts_simple_tcp_client(remote_addr, remote_port)) < 0) {
|
||||
printf("Re-attempting to connect...\n");
|
||||
}
|
||||
printf("Attempting to connect...\n");
|
||||
while ((fd = zts_simple_tcp_client(remote_addr, remote_port)) < 0) {
|
||||
printf("Re-attempting to connect...\n");
|
||||
}
|
||||
|
||||
// Data I/O
|
||||
// Data I/O
|
||||
|
||||
printf("Sending message string to server...\n");
|
||||
if ((bytes = zts_write(fd, msgStr, strlen(msgStr))) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Sent %d bytes: %s\n", bytes, msgStr);
|
||||
printf("Reading message string from server...\n");
|
||||
if ((bytes = zts_read(fd, recvBuf, sizeof(recvBuf))) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Read %d bytes: %s\n", bytes, recvBuf);
|
||||
printf("Sending message string to server...\n");
|
||||
if ((bytes = zts_write(fd, msgStr, strlen(msgStr))) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Sent %d bytes: %s\n", bytes, msgStr);
|
||||
printf("Reading message string from server...\n");
|
||||
if ((bytes = zts_read(fd, recvBuf, sizeof(recvBuf))) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Read %d bytes: %s\n", bytes, recvBuf);
|
||||
|
||||
// Close
|
||||
// Close
|
||||
|
||||
zts_close(fd);
|
||||
return zts_node_stop();
|
||||
zts_close(fd);
|
||||
return zts_node_stop();
|
||||
}
|
||||
|
||||
@@ -12,108 +12,108 @@
|
||||
|
||||
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");
|
||||
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");
|
||||
// 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);
|
||||
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);
|
||||
}
|
||||
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
|
||||
// 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
|
||||
// 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;
|
||||
// Arbitrary World ID
|
||||
uint64_t id = 149604618;
|
||||
|
||||
// Timestamp indicating when this world was generated
|
||||
uint64_t ts = 1567191349589ULL;
|
||||
// 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 };
|
||||
// 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";
|
||||
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
|
||||
// Generate world
|
||||
|
||||
zts_util_world_new(&world_data_out, &world_len, &prev_key, &prev_key_len, &curr_key, &curr_key_len, id, ts, &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);
|
||||
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
|
||||
// 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(".");
|
||||
zts_init_set_world(&world_data_out, world_len);
|
||||
zts_init_set_event_handler(&on_zts_event);
|
||||
zts_init_from_storage(".");
|
||||
|
||||
// Start node
|
||||
// Start node
|
||||
|
||||
zts_node_start();
|
||||
zts_node_start();
|
||||
|
||||
while (1) {
|
||||
zts_util_delay(500);
|
||||
}
|
||||
while (1) {
|
||||
zts_util_delay(500);
|
||||
}
|
||||
|
||||
return zts_node_stop();
|
||||
return zts_node_stop();
|
||||
}
|
||||
|
||||
@@ -12,85 +12,85 @@
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example client\n");
|
||||
printf("client <id_storage_path> <net_id> <remote_addr> <remote_port>\n");
|
||||
exit(0);
|
||||
}
|
||||
char* storage_path = argv[1];
|
||||
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;
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example client\n");
|
||||
printf("client <id_storage_path> <net_id> <remote_addr> <remote_port>\n");
|
||||
exit(0);
|
||||
}
|
||||
char* storage_path = argv[1];
|
||||
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;
|
||||
|
||||
// Initialize node
|
||||
// Initialize node
|
||||
|
||||
if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Start node
|
||||
// Start node
|
||||
|
||||
if ((err = zts_node_start()) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
if ((err = zts_node_start()) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
printf("Public identity (node ID) is %llx\n", (long long int)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) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
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_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
// Sockets
|
||||
// Sockets
|
||||
|
||||
char* msgStr = (char*)"Welcome to the machine";
|
||||
int bytes = 0, fd;
|
||||
char recvBuf[128] = { 0 };
|
||||
memset(recvBuf, 0, sizeof(recvBuf));
|
||||
char* msgStr = (char*)"Welcome to the machine";
|
||||
int bytes = 0, fd;
|
||||
char recvBuf[128] = { 0 };
|
||||
memset(recvBuf, 0, sizeof(recvBuf));
|
||||
|
||||
// Create socket
|
||||
// Create socket
|
||||
|
||||
if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
|
||||
printf("Error (fd=%d, zts_errno=%d). Exiting.\n", fd, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
|
||||
printf("Error (fd=%d, zts_errno=%d). Exiting.\n", fd, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Connect
|
||||
// Connect
|
||||
|
||||
// Can also use:
|
||||
// zts_connect(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen);
|
||||
while (zts_simple_connect(fd, remote_addr, remote_port, 0) != ZTS_ERR_OK) {
|
||||
printf("Attempting to connect...\n");
|
||||
}
|
||||
// Can also use:
|
||||
// zts_connect(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen);
|
||||
while (zts_simple_connect(fd, remote_addr, remote_port, 0) != ZTS_ERR_OK) {
|
||||
printf("Attempting to connect...\n");
|
||||
}
|
||||
|
||||
// Data I/O
|
||||
// Data I/O
|
||||
|
||||
// Wait random intervals to send a message to the server
|
||||
// The non-blocking aspect of this example is server-side
|
||||
while (1) {
|
||||
if ((bytes = zts_send(fd, msgStr, strlen(msgStr), 0)) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("zts_send()=%d\n", bytes);
|
||||
zts_util_delay((rand() % 100) * 50);
|
||||
}
|
||||
// Wait random intervals to send a message to the server
|
||||
// The non-blocking aspect of this example is server-side
|
||||
while (1) {
|
||||
if ((bytes = zts_send(fd, msgStr, strlen(msgStr), 0)) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("zts_send()=%d\n", bytes);
|
||||
zts_util_delay((rand() % 100) * 50);
|
||||
}
|
||||
|
||||
zts_close(fd);
|
||||
return zts_node_stop();
|
||||
zts_close(fd);
|
||||
return zts_node_stop();
|
||||
}
|
||||
|
||||
@@ -12,158 +12,158 @@
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server <id_storage_path> <net_id> <local_addr> <local_port>\n");
|
||||
exit(0);
|
||||
}
|
||||
char* storage_path = argv[1];
|
||||
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;
|
||||
int err = ZTS_ERR_OK;
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server <id_storage_path> <net_id> <local_addr> <local_port>\n");
|
||||
exit(0);
|
||||
}
|
||||
char* storage_path = argv[1];
|
||||
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;
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
// Initialize node
|
||||
// Initialize node
|
||||
|
||||
if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Start node
|
||||
// Start node
|
||||
|
||||
if ((err = zts_node_start()) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
if ((err = zts_node_start()) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Waiting for node to come online\n");
|
||||
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", zts_node_get_id());
|
||||
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
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_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
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_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
printf("Waiting for address assignment from network\n");
|
||||
while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) {
|
||||
zts_util_delay(500);
|
||||
}
|
||||
printf("Waiting for address assignment from network\n");
|
||||
while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) {
|
||||
zts_util_delay(500);
|
||||
}
|
||||
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("Assigned IP address: %s\n", ipstr);
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("Assigned IP address: %s\n", ipstr);
|
||||
|
||||
// Sockets
|
||||
// Sockets
|
||||
|
||||
printf("Creating socket...\n");
|
||||
if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Binding...\n");
|
||||
// Can also use:
|
||||
// zts_bind(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen)
|
||||
if ((err = zts_simple_bind(fd, local_addr, local_port) < 0)) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Listening...\n");
|
||||
int backlog = 100;
|
||||
if ((err = zts_listen(fd, backlog)) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Creating socket...\n");
|
||||
if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Binding...\n");
|
||||
// Can also use:
|
||||
// zts_bind(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen)
|
||||
if ((err = zts_simple_bind(fd, local_addr, local_port) < 0)) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Listening...\n");
|
||||
int backlog = 100;
|
||||
if ((err = zts_listen(fd, backlog)) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int bytes = 0;
|
||||
char recvBuf[128] = { 0 };
|
||||
int bytes = 0;
|
||||
char recvBuf[128] = { 0 };
|
||||
|
||||
while (1) {
|
||||
// Accept
|
||||
// Can also use
|
||||
// zts_accept(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen)
|
||||
while (1) {
|
||||
// Accept
|
||||
// Can also use
|
||||
// zts_accept(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen)
|
||||
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 };
|
||||
int port = 0;
|
||||
printf("Accepting on listening socket...\n");
|
||||
if ((accfd = zts_simple_accept(fd, ipstr, ZTS_INET6_ADDRSTRLEN, &port)) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
}
|
||||
printf("Accepted connection from %s:%d\n", ipstr, port);
|
||||
}
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 };
|
||||
int port = 0;
|
||||
printf("Accepting on listening socket...\n");
|
||||
if ((accfd = zts_simple_accept(fd, ipstr, ZTS_INET6_ADDRSTRLEN, &port)) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
}
|
||||
printf("Accepted connection from %s:%d\n", ipstr, port);
|
||||
}
|
||||
|
||||
// Data I/O
|
||||
// Data I/O
|
||||
|
||||
// Technique 1: ZTS_O_NONBLOCK
|
||||
if (0) {
|
||||
zts_fcntl(fd, ZTS_F_SETFL, ZTS_O_NONBLOCK);
|
||||
zts_fcntl(accfd, ZTS_F_SETFL, ZTS_O_NONBLOCK);
|
||||
while (1) {
|
||||
bytes = zts_recv(accfd, recvBuf, sizeof(recvBuf), 0);
|
||||
printf("zts_recv(%d, ...)=%d\n", accfd, bytes);
|
||||
zts_util_delay(100);
|
||||
}
|
||||
}
|
||||
// Technique 1: ZTS_O_NONBLOCK
|
||||
if (0) {
|
||||
zts_fcntl(fd, ZTS_F_SETFL, ZTS_O_NONBLOCK);
|
||||
zts_fcntl(accfd, ZTS_F_SETFL, ZTS_O_NONBLOCK);
|
||||
while (1) {
|
||||
bytes = zts_recv(accfd, recvBuf, sizeof(recvBuf), 0);
|
||||
printf("zts_recv(%d, ...)=%d\n", accfd, bytes);
|
||||
zts_util_delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
// Technique 2: zts_select
|
||||
if (0) {
|
||||
struct zts_timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 50000;
|
||||
int result = 0;
|
||||
zts_fd_set active_fd_set, read_fd_set;
|
||||
ZTS_FD_ZERO(&active_fd_set);
|
||||
ZTS_FD_SET(accfd, &active_fd_set);
|
||||
while (1) {
|
||||
read_fd_set = active_fd_set;
|
||||
if ((result = zts_select(ZTS_FD_SETSIZE, &read_fd_set, NULL, NULL, &tv) < 0)) {
|
||||
// perror ("select");
|
||||
exit(1);
|
||||
}
|
||||
for (int i = 0; i < ZTS_FD_SETSIZE; i++) {
|
||||
if (ZTS_FD_ISSET(i, &read_fd_set)) {
|
||||
bytes = zts_recv(accfd, recvBuf, sizeof(recvBuf), 0);
|
||||
printf("zts_recv(%d, ...)=%d\n", i, bytes);
|
||||
}
|
||||
// ZTS_FD_CLR(i, &active_fd_set);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Technique 2: zts_select
|
||||
if (0) {
|
||||
struct zts_timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 50000;
|
||||
int result = 0;
|
||||
zts_fd_set active_fd_set, read_fd_set;
|
||||
ZTS_FD_ZERO(&active_fd_set);
|
||||
ZTS_FD_SET(accfd, &active_fd_set);
|
||||
while (1) {
|
||||
read_fd_set = active_fd_set;
|
||||
if ((result = zts_select(ZTS_FD_SETSIZE, &read_fd_set, NULL, NULL, &tv) < 0)) {
|
||||
// perror ("select");
|
||||
exit(1);
|
||||
}
|
||||
for (int i = 0; i < ZTS_FD_SETSIZE; i++) {
|
||||
if (ZTS_FD_ISSET(i, &read_fd_set)) {
|
||||
bytes = zts_recv(accfd, recvBuf, sizeof(recvBuf), 0);
|
||||
printf("zts_recv(%d, ...)=%d\n", i, bytes);
|
||||
}
|
||||
// ZTS_FD_CLR(i, &active_fd_set);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Technique 3: zts_poll
|
||||
if (1) {
|
||||
int numfds = 0;
|
||||
struct zts_pollfd poll_set[16];
|
||||
memset(poll_set, '\0', sizeof(poll_set));
|
||||
poll_set[0].fd = accfd;
|
||||
poll_set[0].events = ZTS_POLLIN;
|
||||
numfds++;
|
||||
int result = 0;
|
||||
int timeout_ms = 50;
|
||||
while (1) {
|
||||
result = zts_poll(poll_set, numfds, timeout_ms);
|
||||
printf("zts_poll()=%d\n", result);
|
||||
for (int i = 0; i < numfds; i++) {
|
||||
if (poll_set[i].revents & ZTS_POLLIN) {
|
||||
bytes = zts_recv(poll_set[i].fd, recvBuf, sizeof(recvBuf), 0);
|
||||
printf("zts_recv(%d, ...)=%d\n", i, bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Technique 3: zts_poll
|
||||
if (1) {
|
||||
int numfds = 0;
|
||||
struct zts_pollfd poll_set[16];
|
||||
memset(poll_set, '\0', sizeof(poll_set));
|
||||
poll_set[0].fd = accfd;
|
||||
poll_set[0].events = ZTS_POLLIN;
|
||||
numfds++;
|
||||
int result = 0;
|
||||
int timeout_ms = 50;
|
||||
while (1) {
|
||||
result = zts_poll(poll_set, numfds, timeout_ms);
|
||||
printf("zts_poll()=%d\n", result);
|
||||
for (int i = 0; i < numfds; i++) {
|
||||
if (poll_set[i].revents & ZTS_POLLIN) {
|
||||
bytes = zts_recv(poll_set[i].fd, recvBuf, sizeof(recvBuf), 0);
|
||||
printf("zts_recv(%d, ...)=%d\n", i, bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = zts_close(fd);
|
||||
return zts_node_stop();
|
||||
err = zts_close(fd);
|
||||
return zts_node_stop();
|
||||
}
|
||||
|
||||
@@ -18,92 +18,92 @@ char cache_data[ZTS_STORE_DATA_LEN];
|
||||
|
||||
void on_zts_event(void* msgPtr)
|
||||
{
|
||||
zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr;
|
||||
int len = msg->len; // Length of message (or structure)
|
||||
zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr;
|
||||
int len = msg->len; // Length of message (or structure)
|
||||
|
||||
if (msg->event_code == ZTS_EVENT_NODE_ONLINE) {
|
||||
printf("ZTS_EVENT_NODE_ONLINE\n");
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_NODE_ONLINE) {
|
||||
printf("ZTS_EVENT_NODE_ONLINE\n");
|
||||
}
|
||||
|
||||
// Copy data to a buffer that you have allocated or write it to storage.
|
||||
// The data pointed to by msg->cache will be invalid after this function
|
||||
// returns.
|
||||
// Copy data to a buffer that you have allocated or write it to storage.
|
||||
// The data pointed to by msg->cache will be invalid after this function
|
||||
// returns.
|
||||
|
||||
memset(cache_data, 0, ZTS_STORE_DATA_LEN);
|
||||
memset(cache_data, 0, ZTS_STORE_DATA_LEN);
|
||||
|
||||
if (msg->event_code == ZTS_EVENT_STORE_IDENTITY_PUBLIC) {
|
||||
printf("ZTS_EVENT_STORE_IDENTITY_PUBLIC (len=%d)\n", msg->len);
|
||||
printf("identity.public = [ %.*s ]\n", len, msg->cache);
|
||||
memcpy(cache_data, msg->cache, len);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_STORE_IDENTITY_SECRET) {
|
||||
printf("ZTS_EVENT_STORE_IDENTITY_SECRET (len=%d)\n", msg->len);
|
||||
printf("identity.secret = [ %.*s ]\n", len, msg->cache);
|
||||
memcpy(cache_data, msg->cache, len);
|
||||
// Same data can be retrieved via: zts_node_get_id_pair()
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_STORE_PLANET) {
|
||||
printf("ZTS_EVENT_STORE_PLANET (len=%d)\n", msg->len);
|
||||
// Binary data
|
||||
memcpy(cache_data, msg->cache, len);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_STORE_PEER) {
|
||||
printf("ZTS_EVENT_STORE_PEER (len=%d)\n", msg->len);
|
||||
// Binary data
|
||||
memcpy(cache_data, msg->cache, len);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_STORE_NETWORK) {
|
||||
printf("ZTS_EVENT_STORE_NETWORK (len=%d)\n", msg->len);
|
||||
// Binary data
|
||||
memcpy(cache_data, msg->cache, len);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_STORE_IDENTITY_PUBLIC) {
|
||||
printf("ZTS_EVENT_STORE_IDENTITY_PUBLIC (len=%d)\n", msg->len);
|
||||
printf("identity.public = [ %.*s ]\n", len, msg->cache);
|
||||
memcpy(cache_data, msg->cache, len);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_STORE_IDENTITY_SECRET) {
|
||||
printf("ZTS_EVENT_STORE_IDENTITY_SECRET (len=%d)\n", msg->len);
|
||||
printf("identity.secret = [ %.*s ]\n", len, msg->cache);
|
||||
memcpy(cache_data, msg->cache, len);
|
||||
// Same data can be retrieved via: zts_node_get_id_pair()
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_STORE_PLANET) {
|
||||
printf("ZTS_EVENT_STORE_PLANET (len=%d)\n", msg->len);
|
||||
// Binary data
|
||||
memcpy(cache_data, msg->cache, len);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_STORE_PEER) {
|
||||
printf("ZTS_EVENT_STORE_PEER (len=%d)\n", msg->len);
|
||||
// Binary data
|
||||
memcpy(cache_data, msg->cache, len);
|
||||
}
|
||||
if (msg->event_code == ZTS_EVENT_STORE_NETWORK) {
|
||||
printf("ZTS_EVENT_STORE_NETWORK (len=%d)\n", msg->len);
|
||||
// Binary data
|
||||
memcpy(cache_data, msg->cache, len);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int err = ZTS_ERR_OK;
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
// Initialize node
|
||||
// Initialize node
|
||||
|
||||
zts_init_set_event_handler(&on_zts_event);
|
||||
zts_init_set_event_handler(&on_zts_event);
|
||||
|
||||
// Start node
|
||||
// Start node
|
||||
|
||||
printf("Starting node...\n");
|
||||
int generate_new_id = 1;
|
||||
if (generate_new_id) {
|
||||
// OPTION A
|
||||
// Generate new automatically ID if no prior init called
|
||||
zts_node_start();
|
||||
}
|
||||
else {
|
||||
// OPTION B
|
||||
// Copy your key here
|
||||
char identity[ZTS_ID_STR_BUF_LEN] = { 0 };
|
||||
int len = ZTS_ID_STR_BUF_LEN;
|
||||
printf("Starting node...\n");
|
||||
int generate_new_id = 1;
|
||||
if (generate_new_id) {
|
||||
// OPTION A
|
||||
// Generate new automatically ID if no prior init called
|
||||
zts_node_start();
|
||||
}
|
||||
else {
|
||||
// OPTION B
|
||||
// Copy your key here
|
||||
char identity[ZTS_ID_STR_BUF_LEN] = { 0 };
|
||||
int len = ZTS_ID_STR_BUF_LEN;
|
||||
|
||||
// Generate key (optional):
|
||||
// int key_len;
|
||||
// zts_id_new(identity, &key_len);
|
||||
// Generate key (optional):
|
||||
// int key_len;
|
||||
// zts_id_new(identity, &key_len);
|
||||
|
||||
// Load pre-existing identity from buffer
|
||||
zts_init_from_memory(identity, len);
|
||||
zts_node_start();
|
||||
}
|
||||
// Load pre-existing identity from buffer
|
||||
zts_init_from_memory(identity, len);
|
||||
zts_node_start();
|
||||
}
|
||||
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
// Do network stuff!
|
||||
// zts_socket, zts_connect, etc
|
||||
// Do network stuff!
|
||||
// zts_socket, zts_connect, etc
|
||||
|
||||
printf("Node %llx is now online. Idling.\n", zts_node_get_id());
|
||||
while (1) {
|
||||
zts_util_delay(500); // Idle indefinitely
|
||||
}
|
||||
printf("Node %llx is now online. Idling.\n", zts_node_get_id());
|
||||
while (1) {
|
||||
zts_util_delay(500); // Idle indefinitely
|
||||
}
|
||||
|
||||
printf("Stopping node\n");
|
||||
return zts_node_stop();
|
||||
printf("Stopping node\n");
|
||||
return zts_node_stop();
|
||||
}
|
||||
|
||||
@@ -11,57 +11,57 @@
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
printf("\nUsage:\n");
|
||||
printf("pingable-node <net_id>\n");
|
||||
exit(0);
|
||||
}
|
||||
long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits
|
||||
if (argc != 2) {
|
||||
printf("\nUsage:\n");
|
||||
printf("pingable-node <net_id>\n");
|
||||
exit(0);
|
||||
}
|
||||
long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits
|
||||
|
||||
printf("Starting node...\n");
|
||||
zts_node_start();
|
||||
printf("Starting node...\n");
|
||||
zts_node_start();
|
||||
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
printf("My public identity (node ID) is %llx\n", (long long int)zts_node_get_id());
|
||||
char keypair[ZTS_ID_STR_BUF_LEN] = { 0 };
|
||||
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");
|
||||
}
|
||||
printf("Identity [public/secret pair] = %s\n", keypair);
|
||||
printf("My public identity (node ID) is %llx\n", (long long int)zts_node_get_id());
|
||||
char keypair[ZTS_ID_STR_BUF_LEN] = { 0 };
|
||||
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");
|
||||
}
|
||||
printf("Identity [public/secret pair] = %s\n", keypair);
|
||||
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("Waiting for join to complete\n");
|
||||
while (! zts_net_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Waiting for join to complete\n");
|
||||
while (! zts_net_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
printf("Waiting for address assignment from network\n");
|
||||
int err = 0;
|
||||
while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) {
|
||||
zts_util_delay(500);
|
||||
}
|
||||
printf("Waiting for address assignment from network\n");
|
||||
int err = 0;
|
||||
while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) {
|
||||
zts_util_delay(500);
|
||||
}
|
||||
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr);
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr);
|
||||
|
||||
// Do network stuff!
|
||||
// zts_socket, zts_connect, etc
|
||||
// Do network stuff!
|
||||
// zts_socket, zts_connect, etc
|
||||
|
||||
while (1) {
|
||||
zts_util_delay(500); // Idle indefinitely
|
||||
}
|
||||
while (1) {
|
||||
zts_util_delay(500); // Idle indefinitely
|
||||
}
|
||||
|
||||
printf("Stopping node\n");
|
||||
return zts_node_stop();
|
||||
printf("Stopping node\n");
|
||||
return zts_node_stop();
|
||||
}
|
||||
|
||||
@@ -12,99 +12,99 @@
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server <id_storage_path> <net_id> <local_addr> <local_port>\n");
|
||||
exit(0);
|
||||
}
|
||||
char* storage_path = argv[1];
|
||||
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;
|
||||
int err = ZTS_ERR_OK;
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server <id_storage_path> <net_id> <local_addr> <local_port>\n");
|
||||
exit(0);
|
||||
}
|
||||
char* storage_path = argv[1];
|
||||
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;
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
// Initialize node
|
||||
// Initialize node
|
||||
|
||||
if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// Start node
|
||||
// Start node
|
||||
|
||||
if ((err = zts_node_start()) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Public identity (node ID) is %llx\n", zts_node_get_id());
|
||||
if ((err = zts_node_start()) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Public identity (node ID) is %llx\n", zts_node_get_id());
|
||||
|
||||
// Join network
|
||||
// Join network
|
||||
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
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_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
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_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
// Get assigned address (of the family type we care about)
|
||||
// Get assigned address (of the family type we care about)
|
||||
|
||||
int family = zts_util_get_ip_family(local_addr);
|
||||
int family = zts_util_get_ip_family(local_addr);
|
||||
|
||||
printf("Waiting for address assignment from network\n");
|
||||
while (! (err = zts_addr_is_assigned(net_id, family))) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, family, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("IP address on network %llx is %s\n", net_id, ipstr);
|
||||
printf("Waiting for address assignment from network\n");
|
||||
while (! (err = zts_addr_is_assigned(net_id, family))) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, family, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("IP address on network %llx is %s\n", net_id, ipstr);
|
||||
|
||||
// BEGIN Socket Stuff
|
||||
// BEGIN Socket Stuff
|
||||
|
||||
// Accept incoming connection
|
||||
// Accept incoming connection
|
||||
|
||||
// Can also use traditional: zts_socket(), zts_bind(), zts_listen(), zts_accept(), etc.
|
||||
// Can also use traditional: zts_socket(), zts_bind(), zts_listen(), zts_accept(), etc.
|
||||
|
||||
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) {
|
||||
printf("Error (fd=%d, zts_errno=%d). Exiting.\n", accfd, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Accepted connection from %s:%d\n", remote_addr, remote_port);
|
||||
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) {
|
||||
printf("Error (fd=%d, zts_errno=%d). Exiting.\n", accfd, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Accepted connection from %s:%d\n", remote_addr, remote_port);
|
||||
|
||||
// Data I/O
|
||||
// Data I/O
|
||||
|
||||
int bytes = 0;
|
||||
char recvBuf[128] = { 0 };
|
||||
int bytes = 0;
|
||||
char recvBuf[128] = { 0 };
|
||||
|
||||
printf("Reading message string from client...\n");
|
||||
if ((bytes = zts_read(accfd, recvBuf, sizeof(recvBuf))) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Read %d bytes: %s\n", bytes, recvBuf);
|
||||
printf("Sending message string to client...\n");
|
||||
if ((bytes = zts_write(accfd, recvBuf, bytes)) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Sent %d bytes: %s\n", bytes, recvBuf);
|
||||
printf("Reading message string from client...\n");
|
||||
if ((bytes = zts_read(accfd, recvBuf, sizeof(recvBuf))) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Read %d bytes: %s\n", bytes, recvBuf);
|
||||
printf("Sending message string to client...\n");
|
||||
if ((bytes = zts_write(accfd, recvBuf, bytes)) < 0) {
|
||||
printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Sent %d bytes: %s\n", bytes, recvBuf);
|
||||
|
||||
// Close
|
||||
// Close
|
||||
|
||||
printf("Closing connection socket\n");
|
||||
err = zts_close(accfd);
|
||||
err = zts_close(fd);
|
||||
return zts_node_stop();
|
||||
printf("Closing connection socket\n");
|
||||
err = zts_close(accfd);
|
||||
err = zts_close(fd);
|
||||
return zts_node_stop();
|
||||
}
|
||||
|
||||
@@ -12,119 +12,119 @@
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
printf("\nUsage:\n");
|
||||
printf("pingable-node <net_id>\n");
|
||||
exit(0);
|
||||
}
|
||||
long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits
|
||||
if (argc != 2) {
|
||||
printf("\nUsage:\n");
|
||||
printf("pingable-node <net_id>\n");
|
||||
exit(0);
|
||||
}
|
||||
long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits
|
||||
|
||||
printf("Starting node...\n");
|
||||
zts_node_start();
|
||||
printf("Starting node...\n");
|
||||
zts_node_start();
|
||||
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Waiting for node to come online\n");
|
||||
while (! zts_node_is_online()) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
printf("My public identity (node ID) is %llx\n", zts_node_get_id());
|
||||
char keypair[ZTS_ID_STR_BUF_LEN] = { 0 };
|
||||
uint16_t len = ZTS_ID_STR_BUF_LEN;
|
||||
if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) {
|
||||
printf("Error getting identity keypair. Exiting.\n");
|
||||
}
|
||||
printf("Identity [public/secret pair] = %s\n", keypair);
|
||||
printf("My public identity (node ID) is %llx\n", zts_node_get_id());
|
||||
char keypair[ZTS_ID_STR_BUF_LEN] = { 0 };
|
||||
uint16_t len = ZTS_ID_STR_BUF_LEN;
|
||||
if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) {
|
||||
printf("Error getting identity keypair. Exiting.\n");
|
||||
}
|
||||
printf("Identity [public/secret pair] = %s\n", keypair);
|
||||
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", net_id);
|
||||
if (zts_net_join(net_id) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network. Exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
printf("Waiting for join to complete\n");
|
||||
while (! zts_net_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
printf("Waiting for join to complete\n");
|
||||
while (! zts_net_transport_is_ready(net_id)) {
|
||||
zts_util_delay(50);
|
||||
}
|
||||
|
||||
printf("Waiting for address assignment from network\n");
|
||||
int err = 0;
|
||||
while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) {
|
||||
zts_util_delay(500);
|
||||
}
|
||||
printf("Waiting for address assignment from network\n");
|
||||
int err = 0;
|
||||
while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) {
|
||||
zts_util_delay(500);
|
||||
}
|
||||
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr);
|
||||
char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 };
|
||||
zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN);
|
||||
printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr);
|
||||
|
||||
// Do network stuff!
|
||||
// zts_socket, zts_connect, etc
|
||||
// Do network stuff!
|
||||
// zts_socket, zts_connect, etc
|
||||
|
||||
// Show protocol statistics
|
||||
// Show protocol statistics
|
||||
|
||||
zts_stats_counter_t s = { 0 };
|
||||
zts_stats_counter_t s = { 0 };
|
||||
|
||||
while (1) {
|
||||
zts_util_delay(1000);
|
||||
if ((err = zts_stats_get_all(&s)) == ZTS_ERR_NO_RESULT) {
|
||||
printf("no results\n");
|
||||
continue;
|
||||
}
|
||||
printf("\n\n");
|
||||
while (1) {
|
||||
zts_util_delay(1000);
|
||||
if ((err = zts_stats_get_all(&s)) == ZTS_ERR_NO_RESULT) {
|
||||
printf("no results\n");
|
||||
continue;
|
||||
}
|
||||
printf("\n\n");
|
||||
|
||||
printf(
|
||||
" link_tx=%9d, link_rx=%9d, link_drop=%9d, link_err=%9d\n",
|
||||
s.link_tx,
|
||||
s.link_rx,
|
||||
s.link_drop,
|
||||
s.link_err);
|
||||
printf(
|
||||
"etharp_tx=%9d, etharp_rx=%9d, etharp_drop=%9d, etharp_err=%9d\n",
|
||||
s.etharp_tx,
|
||||
s.etharp_rx,
|
||||
s.etharp_drop,
|
||||
s.etharp_err);
|
||||
printf(
|
||||
" ip4_tx=%9d, ip4_rx=%9d, ip4_drop=%9d, ip4_err=%9d\n",
|
||||
s.ip4_tx,
|
||||
s.ip4_rx,
|
||||
s.ip4_drop,
|
||||
s.ip4_err);
|
||||
printf(
|
||||
" ip6_tx=%9d, ip6_rx=%9d, ip6_drop=%9d, ip6_err=%9d\n",
|
||||
s.ip6_tx,
|
||||
s.ip6_rx,
|
||||
s.ip6_drop,
|
||||
s.ip6_err);
|
||||
printf(
|
||||
" icmp4_tx=%9d, icmp4_rx=%9d, icmp4_drop=%9d, icmp4_err=%9d\n",
|
||||
s.icmp4_tx,
|
||||
s.icmp4_rx,
|
||||
s.icmp4_drop,
|
||||
s.icmp4_err);
|
||||
printf(
|
||||
" icmp6_tx=%9d, icmp6_rx=%9d, icmp6_drop=%9d, icmp6_err=%9d\n",
|
||||
s.icmp6_tx,
|
||||
s.icmp6_rx,
|
||||
s.icmp6_drop,
|
||||
s.icmp6_err);
|
||||
printf(
|
||||
" udp_tx=%9d, udp_rx=%9d, udp_drop=%9d, udp_err=%9d\n",
|
||||
s.udp_tx,
|
||||
s.udp_rx,
|
||||
s.udp_drop,
|
||||
s.udp_err);
|
||||
printf(
|
||||
" tcp_tx=%9d, tcp_rx=%9d, tcp_drop=%9d, tcp_err=%9d\n",
|
||||
s.tcp_tx,
|
||||
s.tcp_rx,
|
||||
s.tcp_drop,
|
||||
s.tcp_err);
|
||||
printf(
|
||||
" nd6_tx=%9d, nd6_rx=%9d, nd6_drop=%9d, nd6_err=%9d\n",
|
||||
s.nd6_tx,
|
||||
s.nd6_rx,
|
||||
s.nd6_drop,
|
||||
s.nd6_err);
|
||||
}
|
||||
return zts_node_stop();
|
||||
printf(
|
||||
" link_tx=%9d, link_rx=%9d, link_drop=%9d, link_err=%9d\n",
|
||||
s.link_tx,
|
||||
s.link_rx,
|
||||
s.link_drop,
|
||||
s.link_err);
|
||||
printf(
|
||||
"etharp_tx=%9d, etharp_rx=%9d, etharp_drop=%9d, etharp_err=%9d\n",
|
||||
s.etharp_tx,
|
||||
s.etharp_rx,
|
||||
s.etharp_drop,
|
||||
s.etharp_err);
|
||||
printf(
|
||||
" ip4_tx=%9d, ip4_rx=%9d, ip4_drop=%9d, ip4_err=%9d\n",
|
||||
s.ip4_tx,
|
||||
s.ip4_rx,
|
||||
s.ip4_drop,
|
||||
s.ip4_err);
|
||||
printf(
|
||||
" ip6_tx=%9d, ip6_rx=%9d, ip6_drop=%9d, ip6_err=%9d\n",
|
||||
s.ip6_tx,
|
||||
s.ip6_rx,
|
||||
s.ip6_drop,
|
||||
s.ip6_err);
|
||||
printf(
|
||||
" icmp4_tx=%9d, icmp4_rx=%9d, icmp4_drop=%9d, icmp4_err=%9d\n",
|
||||
s.icmp4_tx,
|
||||
s.icmp4_rx,
|
||||
s.icmp4_drop,
|
||||
s.icmp4_err);
|
||||
printf(
|
||||
" icmp6_tx=%9d, icmp6_rx=%9d, icmp6_drop=%9d, icmp6_err=%9d\n",
|
||||
s.icmp6_tx,
|
||||
s.icmp6_rx,
|
||||
s.icmp6_drop,
|
||||
s.icmp6_err);
|
||||
printf(
|
||||
" udp_tx=%9d, udp_rx=%9d, udp_drop=%9d, udp_err=%9d\n",
|
||||
s.udp_tx,
|
||||
s.udp_rx,
|
||||
s.udp_drop,
|
||||
s.udp_err);
|
||||
printf(
|
||||
" tcp_tx=%9d, tcp_rx=%9d, tcp_drop=%9d, tcp_err=%9d\n",
|
||||
s.tcp_tx,
|
||||
s.tcp_rx,
|
||||
s.tcp_drop,
|
||||
s.tcp_err);
|
||||
printf(
|
||||
" nd6_tx=%9d, nd6_rx=%9d, nd6_drop=%9d, nd6_err=%9d\n",
|
||||
s.nd6_tx,
|
||||
s.nd6_rx,
|
||||
s.nd6_drop,
|
||||
s.nd6_err);
|
||||
}
|
||||
return zts_node_stop();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user