Applied styling rules from .clang-format
This commit is contained in:
@@ -86,14 +86,15 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
struct Node
|
||||
{
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0) {}
|
||||
struct Node {
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0)
|
||||
{
|
||||
}
|
||||
bool online;
|
||||
bool joinedAtLeastOneNetwork;
|
||||
uint64_t id;
|
||||
@@ -104,9 +105,9 @@ struct Node
|
||||
to ensure timely receipt of future events. You should not call libzt API functions from
|
||||
this function unless it's something trivial like zts_inet_ntop() or similar that has
|
||||
no state-change implications. */
|
||||
void on_zts_event(void *msgPtr)
|
||||
void on_zts_event(void* msgPtr)
|
||||
{
|
||||
struct zts_callback_msg *msg = (struct zts_callback_msg *)msgPtr;
|
||||
struct zts_callback_msg* msg = (struct zts_callback_msg*)msgPtr;
|
||||
|
||||
// Node events
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_ONLINE) {
|
||||
@@ -115,25 +116,34 @@ void on_zts_event(void *msgPtr)
|
||||
myNode.online = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_OFFLINE) {
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, firewall, etc. What ports are you blocking?\n");
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, "
|
||||
"firewall, etc. What ports are you blocking?\n");
|
||||
myNode.online = false;
|
||||
}
|
||||
|
||||
// Virtual network events
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_NOT_FOUND) {
|
||||
printf("ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_REQ_CONFIG) {
|
||||
printf("ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a few seconds...\n", msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a "
|
||||
"few seconds...\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. "
|
||||
"Did you authorize the node yet?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP6 --- Network config received. IPv6 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->nwid);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_DOWN) {
|
||||
@@ -142,24 +152,25 @@ void on_zts_event(void *msgPtr)
|
||||
|
||||
// Network stack events
|
||||
if (msg->eventCode == ZTS_EVENT_NETIF_UP) {
|
||||
printf("ZTS_EVENT_NETIF_UP --- network=%llx, mac=%llx, mtu=%d\n",
|
||||
msg->netif->nwid,
|
||||
msg->netif->mac,
|
||||
msg->netif->mtu);
|
||||
printf(
|
||||
"ZTS_EVENT_NETIF_UP --- network=%llx, mac=%llx, mtu=%d\n",
|
||||
msg->netif->nwid,
|
||||
msg->netif->mac,
|
||||
msg->netif->mtu);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETIF_DOWN) {
|
||||
printf("ZTS_EVENT_NETIF_DOWN --- network=%llx, mac=%llx\n",
|
||||
msg->netif->nwid,
|
||||
msg->netif->mac);
|
||||
printf(
|
||||
"ZTS_EVENT_NETIF_DOWN --- network=%llx, mac=%llx\n",
|
||||
msg->netif->nwid,
|
||||
msg->netif->mac);
|
||||
}
|
||||
|
||||
// Address events
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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->nwid, ipstr);
|
||||
printf("ZTS_EVENT_ADDR_NEW_IP6 --- Join %llx and ping me at %s\n", msg->addr->nwid, ipstr);
|
||||
}
|
||||
|
||||
// Peer events
|
||||
@@ -170,19 +181,23 @@ void on_zts_event(void *msgPtr)
|
||||
return;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_DIRECT) {
|
||||
printf("ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_RELAY) {
|
||||
printf("ZTS_EVENT_PEER_RELAY --- No direct path to node=%llx\n", msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DISCOVERED) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for "
|
||||
"node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DEAD) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -212,46 +227,55 @@ be taken to avoid exposing vulnerable services or sharing unwanted files or othe
|
||||
|
||||
*/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example\n");
|
||||
printf("adhoc <config_file_path> <adhocStartPort> <adhocEndPort> <ztServicePort>\n");
|
||||
exit(0);
|
||||
}
|
||||
int adhocStartPort = atoi(argv[2]); // Start of port range your application will use
|
||||
int adhocEndPort = atoi(argv[3]); // End of port range your application will use
|
||||
int ztServicePort = atoi(argv[4]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
int adhocStartPort = atoi(argv[2]); // Start of port range your application will use
|
||||
int adhocEndPort = atoi(argv[3]); // End of port range your application will use
|
||||
int ztServicePort = atoi(
|
||||
argv[4]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
|
||||
uint64_t adhoc_nwid = zts_generate_adhoc_nwid_from_range(adhocStartPort, adhocEndPort);
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take slightly longer to start the node
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take
|
||||
// slightly longer to start the node
|
||||
zts_allow_network_caching(1);
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take slightly longer to contact a remote peer
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take
|
||||
// slightly longer to contact a remote peer
|
||||
zts_allow_peer_caching(1);
|
||||
// If disabled: Settings will NOT be read from local.conf
|
||||
zts_allow_local_conf(1);
|
||||
|
||||
if((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online...\n");
|
||||
while (!myNode.online) { zts_delay_ms(50); }
|
||||
while (! myNode.online) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
printf("This node's identity is stored in %s\n", argv[1]);
|
||||
|
||||
if((err = zts_join(adhoc_nwid)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_join(adhoc_nwid)) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", adhoc_nwid);
|
||||
while (!myNode.joinedAtLeastOneNetwork) { zts_delay_ms(50); }
|
||||
while (! myNode.joinedAtLeastOneNetwork) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
|
||||
// Idle and just show callback events, stack statistics, etc
|
||||
|
||||
printf("Node will now idle...\n");
|
||||
while (true) { zts_delay_ms(1000); }
|
||||
while (true) {
|
||||
zts_delay_ms(1000);
|
||||
}
|
||||
|
||||
// Shut down service and stack threads
|
||||
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <string>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
// For optional JSON parsing
|
||||
#include "../ext/ZeroTierOne/ext/json/json.hpp"
|
||||
|
||||
void process_response(char *response, int http_response_code)
|
||||
void process_response(char* response, int http_response_code)
|
||||
{
|
||||
if (http_response_code == 0) {
|
||||
// Request failed at library level, do nothing. There would be no HTTP code at this point.
|
||||
@@ -23,22 +22,22 @@ void process_response(char *response, int http_response_code)
|
||||
return;
|
||||
}
|
||||
nlohmann::json res = nlohmann::json::parse(response);
|
||||
if (!res.is_object()) {
|
||||
if (! res.is_object()) {
|
||||
fprintf(stderr, "Unable to parse (root element is not a JSON object)");
|
||||
}
|
||||
// Pretty print JSON blob
|
||||
std::cout << std::setw(4) << res << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 3) {
|
||||
printf("\nlibzt example central API client\n");
|
||||
printf("centralapi <central_url> <api_token>\n");
|
||||
exit(0);
|
||||
}
|
||||
std::string central_url = argv[1]; // API endpoint
|
||||
std::string api_token = argv[2]; // User token (generate at my.zerotier.com)
|
||||
std::string central_url = argv[1]; // API endpoint
|
||||
std::string api_token = argv[2]; // User token (generate at my.zerotier.com)
|
||||
|
||||
/**
|
||||
* This example demonstrates how to use the ZeroTier Central API to:
|
||||
@@ -67,12 +66,17 @@ int main(int argc, char **argv)
|
||||
|
||||
// Provide URL to Central API server and user API token generated at https://my.zerotier.com
|
||||
printf("Initializing Central API client...\n");
|
||||
if ((err = zts_central_init(central_url.c_str(), api_token.c_str(), rbuf, ZTS_CENTRAL_RESP_BUF_DEFAULT_SZ)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_central_init(
|
||||
central_url.c_str(),
|
||||
api_token.c_str(),
|
||||
rbuf,
|
||||
ZTS_CENTRAL_RESP_BUF_DEFAULT_SZ))
|
||||
!= ZTS_ERR_OK) {
|
||||
fprintf(stderr, "Error while initializing client's Central API parameters\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
zts_central_set_verbose(false); // (optiona) Turn on reporting from libcurl
|
||||
zts_central_set_verbose(false); // (optiona) Turn on reporting from libcurl
|
||||
zts_central_set_access_mode(ZTS_CENTRAL_READ | ZTS_CENTRAL_WRITE);
|
||||
|
||||
int http_res_code = 0;
|
||||
@@ -81,7 +85,8 @@ int main(int argc, char **argv)
|
||||
printf("Requesting Central API server status (/api/status):\n");
|
||||
if ((err = zts_central_get_status(&http_res_code)) != ZTS_ERR_OK) {
|
||||
fprintf(stderr, "Error (%d) making the request.\n", err);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
process_response(rbuf, http_res_code);
|
||||
}
|
||||
// Get network config
|
||||
@@ -89,15 +94,18 @@ int main(int argc, char **argv)
|
||||
printf("Requesting network config: /api/network/%llx\n", nwid);
|
||||
if ((err = zts_central_get_network(&http_res_code, nwid)) != ZTS_ERR_OK) {
|
||||
fprintf(stderr, "Error (%d) making the request.\n", err);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
process_response(rbuf, http_res_code);
|
||||
}
|
||||
// Authorize a node on a network
|
||||
int64_t nodeid = 0x9934343434;
|
||||
printf("Authorizing: /api/network/%llx/member/%llx\n", nwid, nodeid);
|
||||
if ((err = zts_central_set_node_auth(&http_res_code, nwid, nodeid, ZTS_CENTRAL_NODE_AUTH_TRUE)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_central_set_node_auth(&http_res_code, nwid, nodeid, ZTS_CENTRAL_NODE_AUTH_TRUE))
|
||||
!= ZTS_ERR_OK) {
|
||||
fprintf(stderr, "Error (%d) making the request.\n", err);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
process_response(rbuf, http_res_code);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,16 +2,17 @@
|
||||
* libzt API example
|
||||
*/
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
struct Node
|
||||
{
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0) {}
|
||||
struct Node {
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0)
|
||||
{
|
||||
}
|
||||
bool online;
|
||||
bool joinedAtLeastOneNetwork;
|
||||
uint64_t id;
|
||||
@@ -22,9 +23,9 @@ struct Node
|
||||
to ensure timely receipt of future events. You should not call libzt API functions from
|
||||
this function unless it's something trivial like zts_inet_ntop() or similar that has
|
||||
no state-change implications. */
|
||||
void on_zts_event(void *msgPtr)
|
||||
void on_zts_event(void* msgPtr)
|
||||
{
|
||||
struct zts_callback_msg *msg = (struct zts_callback_msg *)msgPtr;
|
||||
struct zts_callback_msg* msg = (struct zts_callback_msg*)msgPtr;
|
||||
|
||||
// Node events
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_ONLINE) {
|
||||
@@ -33,7 +34,8 @@ void on_zts_event(void *msgPtr)
|
||||
myNode.online = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_OFFLINE) {
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, firewall, etc. What ports are you blocking?\n");
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, "
|
||||
"firewall, etc. What ports are you blocking?\n");
|
||||
myNode.online = false;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_NORMAL_TERMINATION) {
|
||||
@@ -42,24 +44,34 @@ void on_zts_event(void *msgPtr)
|
||||
|
||||
// Virtual network events
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_NOT_FOUND) {
|
||||
printf("ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_REQ_CONFIG) {
|
||||
printf("ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a few seconds...\n", msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a "
|
||||
"few seconds...\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. "
|
||||
"Did you authorize the node yet?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_READY_IP4) {
|
||||
printf("ZTS_EVENT_NETWORK_READY_IP4 --- Network config received. IPv4 traffic can now be sent over network %llx\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP4 --- Network config received. IPv4 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->nwid);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP6 --- Network config received. IPv6 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->nwid);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_DOWN) {
|
||||
@@ -69,31 +81,41 @@ void on_zts_event(void *msgPtr)
|
||||
// Address events
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP4) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("ZTS_EVENT_ADDR_NEW_IP4 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid, ipstr);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_NEW_IP4 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid,
|
||||
ipstr);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid, ipstr);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_NEW_IP6 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid,
|
||||
ipstr);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP4) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("ZTS_EVENT_ADDR_REMOVED_IP4 --- The virtual address %s for this node on network %llx has been removed.\n",
|
||||
ipstr, msg->addr->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_REMOVED_IP4 --- The virtual address %s for this node on network %llx "
|
||||
"has been removed.\n",
|
||||
ipstr,
|
||||
msg->addr->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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_REMOVED_IP6 --- The virtual address %s for this node on network %llx has been removed.\n",
|
||||
ipstr, msg->addr->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_REMOVED_IP6 --- The virtual address %s for this node on network %llx "
|
||||
"has been removed.\n",
|
||||
ipstr,
|
||||
msg->addr->nwid);
|
||||
}
|
||||
// Peer events
|
||||
if (msg->peer) {
|
||||
@@ -103,19 +125,23 @@ void on_zts_event(void *msgPtr)
|
||||
return;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_DIRECT) {
|
||||
printf("ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_RELAY) {
|
||||
printf("ZTS_EVENT_PEER_RELAY --- No direct path to node=%llx\n", msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DISCOVERED) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for "
|
||||
"node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DEAD) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -202,17 +228,18 @@ void on_zts_event(void *msgPtr)
|
||||
*
|
||||
*/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 6) {
|
||||
printf("\nlibzt example client\n");
|
||||
printf("client <config_file_path> <nwid> <remoteAddr> <remotePort> <ztServicePort>\n");
|
||||
exit(0);
|
||||
}
|
||||
uint64_t nwid = strtoull(argv[2],NULL,16); // Network ID to join
|
||||
std::string remoteAddr = argv[3]; // Remote application's virtual ZT address
|
||||
int remotePort = atoi(argv[4]); // Port the application will try to connect to the server on
|
||||
int ztServicePort = atoi(argv[5]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
uint64_t nwid = strtoull(argv[2], NULL, 16); // Network ID to join
|
||||
std::string remoteAddr = argv[3]; // Remote application's virtual ZT address
|
||||
int remotePort = atoi(argv[4]); // Port the application will try to connect to the server on
|
||||
int ztServicePort = atoi(
|
||||
argv[5]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
|
||||
struct zts_sockaddr_in in4;
|
||||
in4.sin_port = htons(remotePort);
|
||||
@@ -227,33 +254,39 @@ int main(int argc, char **argv)
|
||||
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take slightly longer to start the node
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take
|
||||
// slightly longer to start the node
|
||||
zts_allow_network_caching(1);
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take slightly longer to contact a remote peer
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take
|
||||
// slightly longer to contact a remote peer
|
||||
zts_allow_peer_caching(1);
|
||||
// If disabled: Settings will NOT be read from local.conf
|
||||
zts_allow_local_conf(1);
|
||||
|
||||
if((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online...\n");
|
||||
while (!myNode.online) { zts_delay_ms(50); }
|
||||
while (! myNode.online) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
printf("This node's identity is stored in %s\n", argv[1]);
|
||||
|
||||
if((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", nwid);
|
||||
printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
|
||||
while (!myNode.joinedAtLeastOneNetwork) { zts_delay_ms(50); }
|
||||
while (! myNode.joinedAtLeastOneNetwork) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
|
||||
// Socket-like API example
|
||||
|
||||
char *msgStr = (char*)"Welcome to the machine";
|
||||
int bytes=0, fd;
|
||||
char* msgStr = (char*)"Welcome to the machine";
|
||||
int bytes = 0, fd;
|
||||
char recvBuf[128];
|
||||
memset(recvBuf, 0, sizeof(recvBuf));
|
||||
|
||||
@@ -264,13 +297,19 @@ int main(int argc, char **argv)
|
||||
// Retries are often required since ZT uses transport-triggered links (explained above)
|
||||
for (;;) {
|
||||
printf("Connecting to remote host...\n");
|
||||
if ((err = zts_connect(fd, (const struct zts_sockaddr *)&in4, sizeof(in4))) < 0) {
|
||||
printf("Error connecting to remote host (fd=%d, ret=%d, zts_errno=%d). Trying again.\n",
|
||||
fd, err, zts_errno);
|
||||
if ((err = zts_connect(fd, (const struct zts_sockaddr*)&in4, sizeof(in4))) < 0) {
|
||||
printf(
|
||||
"Error connecting to remote host (fd=%d, ret=%d, zts_errno=%d). Trying again.\n",
|
||||
fd,
|
||||
err,
|
||||
zts_errno);
|
||||
zts_close(fd);
|
||||
printf("Creating socket...\n");
|
||||
if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
|
||||
printf("Error creating ZeroTier socket (fd=%d, zts_errno=%d). Exiting.\n", fd, zts_errno);
|
||||
printf(
|
||||
"Error creating ZeroTier socket (fd=%d, zts_errno=%d). Exiting.\n",
|
||||
fd,
|
||||
zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
zts_delay_ms(250);
|
||||
@@ -281,14 +320,22 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
printf("Sending message string to server...\n");
|
||||
if((bytes = zts_write(fd, msgStr, strlen(msgStr))) < 0) {
|
||||
printf("Error writing to socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
if ((bytes = zts_write(fd, msgStr, strlen(msgStr))) < 0) {
|
||||
printf(
|
||||
"Error writing to socket (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 writing to socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
if ((bytes = zts_read(fd, recvBuf, sizeof(recvBuf))) < 0) {
|
||||
printf(
|
||||
"Error writing to socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n",
|
||||
fd,
|
||||
bytes,
|
||||
zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Read %d bytes: %s\n", bytes, recvBuf);
|
||||
|
||||
@@ -86,55 +86,64 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
#include "winsock.h"
|
||||
#include "winsock.h"
|
||||
#endif
|
||||
struct Node
|
||||
{
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0) {}
|
||||
struct Node {
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0)
|
||||
{
|
||||
}
|
||||
bool online;
|
||||
bool joinedAtLeastOneNetwork;
|
||||
uint64_t id;
|
||||
// etc
|
||||
} myNode;
|
||||
|
||||
void printNodeDetails(const char *msgStr, struct zts_node_details *d)
|
||||
void printNodeDetails(const char* msgStr, struct zts_node_details* d)
|
||||
{
|
||||
printf("\n%s\n", msgStr);
|
||||
printf("\t- id : %llx\n", d->address);
|
||||
printf("\t- version : %d.%d.%d\n", d->versionMajor, d->versionMinor, d->versionRev);
|
||||
printf(
|
||||
"\t- version : %d.%d.%d\n",
|
||||
d->versionMajor,
|
||||
d->versionMinor,
|
||||
d->versionRev);
|
||||
printf("\t- primaryPort : %d\n", d->primaryPort);
|
||||
printf("\t- secondaryPort : %d\n", d->secondaryPort);
|
||||
}
|
||||
|
||||
void printPeerDetails(const char *msgStr, struct zts_peer_details *d)
|
||||
void printPeerDetails(const char* msgStr, struct zts_peer_details* d)
|
||||
{
|
||||
printf("\n%s\n", msgStr);
|
||||
printf("\t- peer : %llx\n", d->address);
|
||||
printf("\t- role : %llx\n", d->role);
|
||||
printf("\t- latency : %d\n", d->latency);
|
||||
printf("\t- version : %d.%d.%d\n", d->versionMajor, d->versionMinor, d->versionRev);
|
||||
printf(
|
||||
"\t- version : %d.%d.%d\n",
|
||||
d->versionMajor,
|
||||
d->versionMinor,
|
||||
d->versionRev);
|
||||
printf("\t- pathCount : %d\n", d->pathCount);
|
||||
printf("\t- paths:\n");
|
||||
|
||||
// Print all known paths for each peer
|
||||
for (unsigned int j=0; j<d->pathCount; j++) {
|
||||
for (unsigned int j = 0; j < d->pathCount; j++) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
int port = 0;
|
||||
struct zts_sockaddr *sa = (struct zts_sockaddr *)&(d->paths[j].address);
|
||||
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;
|
||||
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;
|
||||
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);
|
||||
@@ -142,7 +151,7 @@ void printPeerDetails(const char *msgStr, struct zts_peer_details *d)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void printNetworkDetails(const char *msgStr, struct zts_network_details *d)
|
||||
void printNetworkDetails(const char* msgStr, struct zts_network_details* d)
|
||||
{
|
||||
printf("\n%s\n", msgStr);
|
||||
printf("\t- nwid : %llx\n", d->nwid);
|
||||
@@ -159,54 +168,57 @@ void printNetworkDetails(const char *msgStr, struct zts_network_details *d)
|
||||
printf("\t- routeCount : %d\n", d->routeCount);
|
||||
printf("\t- multicastSubscriptionCount : %d\n", d->multicastSubscriptionCount);
|
||||
|
||||
for (int i=0; i<d->multicastSubscriptionCount; i++) {
|
||||
printf("\t - mac=%llx, adi=%x\n", d->multicastSubscriptions[i].mac, d->multicastSubscriptions[i].adi);
|
||||
for (int i = 0; i < d->multicastSubscriptionCount; i++) {
|
||||
printf(
|
||||
"\t - mac=%llx, adi=%x\n",
|
||||
d->multicastSubscriptions[i].mac,
|
||||
d->multicastSubscriptions[i].adi);
|
||||
}
|
||||
|
||||
printf("\t- addresses:\n");
|
||||
|
||||
for (int i=0; i<d->assignedAddressCount; i++) {
|
||||
for (int i = 0; i < d->assignedAddressCount; i++) {
|
||||
if (d->assignedAddresses[i].ss_family == ZTS_AF_INET) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(d->assignedAddresses[i]);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(d->assignedAddresses[i]);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("\t - %s\n",ipstr);
|
||||
printf("\t - %s\n", ipstr);
|
||||
}
|
||||
if (d->assignedAddresses[i].ss_family == ZTS_AF_INET6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(d->assignedAddresses[i]);
|
||||
struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&(d->assignedAddresses[i]);
|
||||
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN);
|
||||
printf("\t - %s\n",ipstr);
|
||||
printf("\t - %s\n", ipstr);
|
||||
}
|
||||
}
|
||||
|
||||
printf("\t- routes:\n");
|
||||
|
||||
for (int i=0; i<d->routeCount; i++) {
|
||||
for (int i = 0; i < d->routeCount; i++) {
|
||||
if (d->routes[i].target.ss_family == ZTS_AF_INET) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(d->routes[i].target);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(d->routes[i].target);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("\t - target : %s\n",ipstr);
|
||||
printf("\t - target : %s\n", ipstr);
|
||||
in4 = (struct zts_sockaddr_in*)&(d->routes[i].via);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("\t - via : %s\n",ipstr);
|
||||
printf("\t - via : %s\n", ipstr);
|
||||
}
|
||||
if (d->routes[i].target.ss_family == ZTS_AF_INET6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(d->routes[i].target);
|
||||
struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&(d->routes[i].target);
|
||||
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN);
|
||||
printf("\t - target : %s\n",ipstr);
|
||||
printf("\t - target : %s\n", ipstr);
|
||||
in6 = (struct zts_sockaddr_in6*)&(d->routes[i].via);
|
||||
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN);
|
||||
printf("\t - via : %s\n",ipstr);
|
||||
printf("\t - via : %s\n", ipstr);
|
||||
}
|
||||
printf("\t - flags : %d\n", d->routes[i].flags);
|
||||
printf("\t - metric : %d\n", d->routes[i].metric);
|
||||
}
|
||||
}
|
||||
|
||||
void printNetifDetails(const char *msgStr, struct zts_netif_details *d)
|
||||
void printNetifDetails(const char* msgStr, struct zts_netif_details* d)
|
||||
{
|
||||
printf("\n%s\n", msgStr);
|
||||
printf("\t- nwid : %llx\n", d->nwid);
|
||||
@@ -218,9 +230,9 @@ void printNetifDetails(const char *msgStr, struct zts_netif_details *d)
|
||||
to ensure timely receipt of future events. You should not call libzt API functions from
|
||||
this function unless it's something trivial like zts_inet_ntop() or similar that has
|
||||
no state-change implications. */
|
||||
void on_zts_event(void *msgPtr)
|
||||
void on_zts_event(void* msgPtr)
|
||||
{
|
||||
struct zts_callback_msg *msg = (struct zts_callback_msg *)msgPtr;
|
||||
struct zts_callback_msg* msg = (struct zts_callback_msg*)msgPtr;
|
||||
printf("eventCode=%d\n", msg->eventCode);
|
||||
|
||||
// Node events
|
||||
@@ -230,33 +242,44 @@ void on_zts_event(void *msgPtr)
|
||||
myNode.online = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_OFFLINE) {
|
||||
printf("\nZTS_EVENT_NODE_OFFLINE --- Check your Internet connection, router, firewall, etc. What ports are you blocking?\n");
|
||||
printf("\nZTS_EVENT_NODE_OFFLINE --- Check your Internet connection, router, firewall, "
|
||||
"etc. What ports are you blocking?\n");
|
||||
myNode.online = false;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_NORMAL_TERMINATION) {
|
||||
printf("\nZTS_EVENT_NODE_NORMAL_TERMINATION -- A call to zts_start() will restart ZeroTier.\n");
|
||||
printf("\nZTS_EVENT_NODE_NORMAL_TERMINATION -- A call to zts_start() will restart "
|
||||
"ZeroTier.\n");
|
||||
myNode.online = false;
|
||||
}
|
||||
|
||||
// Virtual network events
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_NOT_FOUND) {
|
||||
printf("\nZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"\nZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_REQ_CONFIG) {
|
||||
printf("\nZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a few seconds...\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"\nZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a "
|
||||
"few seconds...\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_ACCESS_DENIED) {
|
||||
printf("\nZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. Did you authorize the node yet?\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"\nZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. "
|
||||
"Did you authorize the node yet?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_READY_IP4) {
|
||||
printNetworkDetails("ZTS_EVENT_NETWORK_READY_IP4 --- Network config received.", msg->network);
|
||||
printNetworkDetails(
|
||||
"ZTS_EVENT_NETWORK_READY_IP4 --- Network config received.",
|
||||
msg->network);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_READY_IP6) {
|
||||
printNetworkDetails("ZTS_EVENT_NETWORK_READY_IP6 --- Network config received.", msg->network);
|
||||
printNetworkDetails(
|
||||
"ZTS_EVENT_NETWORK_READY_IP6 --- Network config received.",
|
||||
msg->network);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_DOWN) {
|
||||
@@ -269,31 +292,41 @@ void on_zts_event(void *msgPtr)
|
||||
// Address events
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP4) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("\nZTS_EVENT_ADDR_ADDED_IP4 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid, ipstr);
|
||||
printf(
|
||||
"\nZTS_EVENT_ADDR_ADDED_IP4 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid,
|
||||
ipstr);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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("\nZTS_EVENT_ADDR_ADDED_IP6 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid, ipstr);
|
||||
printf(
|
||||
"\nZTS_EVENT_ADDR_ADDED_IP6 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid,
|
||||
ipstr);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP4) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("\nZTS_EVENT_ADDR_REMOVED_IP4 --- The virtual address %s for this node on network %llx has been removed.\n",
|
||||
ipstr, msg->addr->nwid);
|
||||
printf(
|
||||
"\nZTS_EVENT_ADDR_REMOVED_IP4 --- The virtual address %s for this node on network %llx "
|
||||
"has been removed.\n",
|
||||
ipstr,
|
||||
msg->addr->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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("\nZTS_EVENT_ADDR_REMOVED_IP6 --- The virtual address %s for this node on network %llx has been removed.\n",
|
||||
ipstr, msg->addr->nwid);
|
||||
printf(
|
||||
"\nZTS_EVENT_ADDR_REMOVED_IP6 --- The virtual address %s for this node on network %llx "
|
||||
"has been removed.\n",
|
||||
ipstr,
|
||||
msg->addr->nwid);
|
||||
}
|
||||
|
||||
// Peer events
|
||||
@@ -310,7 +343,9 @@ void on_zts_event(void *msgPtr)
|
||||
printPeerDetails("ZTS_EVENT_PEER_RELAY --- No direct path known.", msg->peer);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DISCOVERED) {
|
||||
printPeerDetails("ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered.", msg->peer);
|
||||
printPeerDetails(
|
||||
"ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered.",
|
||||
msg->peer);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DEAD) {
|
||||
printPeerDetails("ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died.", msg->peer);
|
||||
@@ -338,16 +373,17 @@ void on_zts_event(void *msgPtr)
|
||||
printf("\nZTS_EVENT_STACK_UP --- No action required.\n");
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_STACK_DOWN) {
|
||||
printf("\nZTS_EVENT_STACK_DOWN --- No action required. An app restart is needed to use ZeroTier again.\n");
|
||||
printf("\nZTS_EVENT_STACK_DOWN --- No action required. An app restart is needed to use "
|
||||
"ZeroTier again.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void get6PLANEAddressOfPeer(uint64_t peerId, uint64_t nwId)
|
||||
{
|
||||
char peerAddrStr[ZTS_INET6_ADDRSTRLEN] = {0};
|
||||
char peerAddrStr[ZTS_INET6_ADDRSTRLEN] = { 0 };
|
||||
struct zts_sockaddr_storage sixplane_addr;
|
||||
zts_get_6plane_addr(&sixplane_addr, nwId, peerId);
|
||||
struct zts_sockaddr_in6 *p6 = (struct zts_sockaddr_in6*)&sixplane_addr;
|
||||
struct zts_sockaddr_in6* p6 = (struct zts_sockaddr_in6*)&sixplane_addr;
|
||||
zts_inet_ntop(ZTS_AF_INET6, &(p6->sin6_addr), peerAddrStr, ZTS_INET6_ADDRSTRLEN);
|
||||
printf("6PLANE address of peer is: %s\n", peerAddrStr);
|
||||
}
|
||||
@@ -358,7 +394,8 @@ void display_stack_stats()
|
||||
{
|
||||
int err = 0;
|
||||
// Count received pings
|
||||
if ((err = zts_get_protocol_stats(ZTS_STATS_PROTOCOL_ICMP, &protoSpecificStats)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_get_protocol_stats(ZTS_STATS_PROTOCOL_ICMP, &protoSpecificStats))
|
||||
!= ZTS_ERR_OK) {
|
||||
printf("zts_get_proto_stats()=%d", err);
|
||||
return;
|
||||
}
|
||||
@@ -371,7 +408,7 @@ void display_stack_stats()
|
||||
printf("tcp.drop=%d\n", protoSpecificStats.drop);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 4) {
|
||||
printf("\nlibzt example server\n");
|
||||
@@ -379,52 +416,59 @@ int main(int argc, char **argv)
|
||||
exit(0);
|
||||
}
|
||||
std::string configPath = std::string(argv[1]);
|
||||
uint64_t nwid = strtoull(argv[2],NULL,16); // Network ID to join
|
||||
int ztServicePort = atoi(argv[3]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
uint64_t nwid = strtoull(argv[2], NULL, 16); // Network ID to join
|
||||
int ztServicePort = atoi(
|
||||
argv[3]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
|
||||
// Bring up ZeroTier service and join network
|
||||
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take slightly longer to start the node
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take
|
||||
// slightly longer to start the node
|
||||
zts_allow_network_caching(1);
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take slightly longer to contact a remote peer
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take
|
||||
// slightly longer to contact a remote peer
|
||||
zts_allow_peer_caching(1);
|
||||
// If disabled: Settings will NOT be read from local.conf
|
||||
zts_allow_local_conf(1);
|
||||
|
||||
if((err = zts_start(configPath.c_str(), &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_start(configPath.c_str(), &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online...\n");
|
||||
while (!myNode.online) { zts_delay_ms(50); }
|
||||
while (! myNode.online) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
printf("This node's identity is stored in %s\n", argv[1]);
|
||||
|
||||
if((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", nwid);
|
||||
printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
|
||||
while (!myNode.joinedAtLeastOneNetwork) { zts_delay_ms(50); }
|
||||
while (! myNode.joinedAtLeastOneNetwork) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
|
||||
// Idle and just show callback events, stack statistics, etc
|
||||
// Alternatively, this is where you could start making calls to the socket API
|
||||
|
||||
/*
|
||||
while(true) {
|
||||
display_stack_stats();
|
||||
zts_delay_ms(1000);
|
||||
display_stack_stats();
|
||||
zts_delay_ms(1000);
|
||||
}
|
||||
*/
|
||||
|
||||
int delay = 500000;
|
||||
printf("This program will delay for %d seconds and then shut down.\n", (delay / 1000));
|
||||
zts_delay_ms(delay);
|
||||
//printf("Leaving network %llx\n", nwid);
|
||||
//zts_leave(nwid);
|
||||
//zts_delay_ms(3000); /* added for demo purposes so that events show up */
|
||||
// printf("Leaving network %llx\n", nwid);
|
||||
// zts_leave(nwid);
|
||||
// zts_delay_ms(3000); /* added for demo purposes so that events show up */
|
||||
printf("Stopping ZeroTier\n");
|
||||
zts_stop();
|
||||
zts_delay_ms(delay); /* added for demo purposes so that events show up */
|
||||
|
||||
@@ -86,14 +86,15 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
struct Node
|
||||
{
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0) {}
|
||||
struct Node {
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0)
|
||||
{
|
||||
}
|
||||
bool online;
|
||||
bool joinedAtLeastOneNetwork;
|
||||
uint64_t id;
|
||||
@@ -104,9 +105,9 @@ struct Node
|
||||
to ensure timely receipt of future events. You should not call libzt API functions from
|
||||
this function unless it's something trivial like zts_inet_ntop() or similar that has
|
||||
no state-change implications. */
|
||||
void on_zts_event(void *msgPtr)
|
||||
void on_zts_event(void* msgPtr)
|
||||
{
|
||||
struct zts_callback_msg *msg = (struct zts_callback_msg *)msgPtr;
|
||||
struct zts_callback_msg* msg = (struct zts_callback_msg*)msgPtr;
|
||||
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_ONLINE) {
|
||||
printf("ZTS_EVENT_NODE_ONLINE --- This node's ID is %llx\n", msg->node->address);
|
||||
@@ -114,19 +115,27 @@ void on_zts_event(void *msgPtr)
|
||||
myNode.online = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_OFFLINE) {
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, firewall, etc. What ports are you blocking?\n");
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, "
|
||||
"firewall, etc. What ports are you blocking?\n");
|
||||
myNode.online = false;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_REQ_CONFIG) {
|
||||
printf("ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a few seconds...\n", msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a "
|
||||
"few seconds...\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. "
|
||||
"Did you authorize the node yet?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP6 --- Network config received. IPv6 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->nwid);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_DOWN) {
|
||||
@@ -134,17 +143,15 @@ void on_zts_event(void *msgPtr)
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP4) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("ZTS_EVENT_ADDR_NEW_IP4 --- Join %llx and ping me at %s\n",
|
||||
msg->addr->nwid, ipstr);
|
||||
printf("ZTS_EVENT_ADDR_NEW_IP4 --- Join %llx and ping me at %s\n", msg->addr->nwid, ipstr);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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->nwid, ipstr);
|
||||
printf("ZTS_EVENT_ADDR_NEW_IP6 --- Join %llx and ping me at %s\n", msg->addr->nwid, ipstr);
|
||||
}
|
||||
// Peer events
|
||||
if (msg->peer) {
|
||||
@@ -154,62 +161,75 @@ void on_zts_event(void *msgPtr)
|
||||
return;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_DIRECT) {
|
||||
printf("ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_RELAY) {
|
||||
printf("ZTS_EVENT_PEER_RELAY --- No direct path to node=%llx\n", msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DISCOVERED) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for "
|
||||
"node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DEAD) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 3) {
|
||||
printf("\nlibzt example\n");
|
||||
printf("earthtest <config_file_path> <ztServicePort>\n");
|
||||
exit(0);
|
||||
}
|
||||
int ztServicePort = atoi(argv[2]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
int ztServicePort = atoi(
|
||||
argv[2]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take slightly longer to start the node
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take
|
||||
// slightly longer to start the node
|
||||
zts_allow_network_caching(1);
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take slightly longer to contact a remote peer
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take
|
||||
// slightly longer to contact a remote peer
|
||||
zts_allow_peer_caching(1);
|
||||
// If disabled: Settings will NOT be read from local.conf
|
||||
zts_allow_local_conf(1);
|
||||
|
||||
if((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online...\n");
|
||||
while (!myNode.online) { zts_delay_ms(50); }
|
||||
while (! myNode.online) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
printf("This node's identity is stored in %s\n", argv[1]);
|
||||
|
||||
uint64_t nwid = 0x8056c2e21c000001;
|
||||
|
||||
if((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", nwid);
|
||||
while (!myNode.joinedAtLeastOneNetwork) { zts_delay_ms(50); }
|
||||
while (! myNode.joinedAtLeastOneNetwork) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
|
||||
// Idle and just show callback events, stack statistics, etc
|
||||
|
||||
printf("Node will now idle...\n");
|
||||
while (true) { zts_delay_ms(1000); }
|
||||
while (true) {
|
||||
zts_delay_ms(1000);
|
||||
}
|
||||
|
||||
// Shut down service and stack threads
|
||||
|
||||
|
||||
@@ -5,15 +5,16 @@
|
||||
* local storage. In this mode you are responsible for saving keys.
|
||||
*/
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
struct Node
|
||||
{
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0) {}
|
||||
struct Node {
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0)
|
||||
{
|
||||
}
|
||||
bool online;
|
||||
bool joinedAtLeastOneNetwork;
|
||||
uint64_t id;
|
||||
@@ -24,9 +25,9 @@ struct Node
|
||||
to ensure timely receipt of future events. You should not call libzt API functions from
|
||||
this function unless it's something trivial like zts_inet_ntop() or similar that has
|
||||
no state-change implications. */
|
||||
void on_zts_event(void *msgPtr)
|
||||
void on_zts_event(void* msgPtr)
|
||||
{
|
||||
struct zts_callback_msg *msg = (struct zts_callback_msg *)msgPtr;
|
||||
struct zts_callback_msg* msg = (struct zts_callback_msg*)msgPtr;
|
||||
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_ONLINE) {
|
||||
printf("ZTS_EVENT_NODE_ONLINE --- This node's ID is %llx\n", msg->node->address);
|
||||
@@ -34,36 +35,39 @@ void on_zts_event(void *msgPtr)
|
||||
myNode.online = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_OFFLINE) {
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, firewall, etc. What ports are you blocking?\n");
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, "
|
||||
"firewall, etc. What ports are you blocking?\n");
|
||||
myNode.online = false;
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP6 --- Network config received. IPv6 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->nwid);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
}
|
||||
|
||||
#define KEY_BUF_LEN 2048
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 3) {
|
||||
printf("\nlibzt example\n");
|
||||
printf("earthtest <config_file_path> <ztServicePort>\n");
|
||||
exit(0);
|
||||
}
|
||||
int ztServicePort = atoi(argv[2]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
int ztServicePort = atoi(
|
||||
argv[2]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
|
||||
// BEGIN key handling
|
||||
|
||||
|
||||
// Do not allow ZT to write anything to disk
|
||||
zts_disable_local_storage(1);
|
||||
|
||||
// Buffer used to store identity keypair (if someone can read this, they can impersonate your node!)
|
||||
// Buffer used to store identity keypair (if someone can read this, they can impersonate your
|
||||
// node!)
|
||||
char keypair[KEY_BUF_LEN];
|
||||
memset(keypair, 0, KEY_BUF_LEN);
|
||||
|
||||
@@ -77,15 +81,18 @@ int main(int argc, char **argv)
|
||||
printf("\n\nVerifying ident...\n");
|
||||
if (zts_verify_identity(keypair)) {
|
||||
printf("\tIdentity is valid\n");
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
printf("\tIdentity is invalid\n");
|
||||
}
|
||||
|
||||
printf("\n\nStarting node with generated identity...\n");
|
||||
zts_start_with_identity(keypair, keypair_len, &on_zts_event, ztServicePort);
|
||||
|
||||
|
||||
printf("\n\nWaiting for node to come online...\n");
|
||||
while (!myNode.online) { zts_delay_ms(50); }
|
||||
while (! myNode.online) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
|
||||
printf("\n\nAs a test, copy node's identity keypair back into buffer...\n");
|
||||
memset(keypair, 0, KEY_BUF_LEN);
|
||||
@@ -93,23 +100,25 @@ int main(int argc, char **argv)
|
||||
zts_get_node_identity(keypair, &keypair_len);
|
||||
printf("keypair(len=%d) = [%s]\n", keypair_len, keypair);
|
||||
|
||||
|
||||
// END key handling
|
||||
|
||||
|
||||
uint64_t nwid = 0x8056c2e21c000001;
|
||||
|
||||
if((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", nwid);
|
||||
while (!myNode.joinedAtLeastOneNetwork) { zts_delay_ms(50); }
|
||||
while (! myNode.joinedAtLeastOneNetwork) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
|
||||
// Idle and just show callback events, stack statistics, etc
|
||||
|
||||
printf("Node will now idle...\n");
|
||||
while (true) { zts_delay_ms(1000); }
|
||||
while (true) {
|
||||
zts_delay_ms(1000);
|
||||
}
|
||||
|
||||
// Shut down service and stack threads
|
||||
|
||||
|
||||
@@ -84,16 +84,17 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
struct Node
|
||||
{
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0) {}
|
||||
struct Node {
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0)
|
||||
{
|
||||
}
|
||||
bool online;
|
||||
bool joinedAtLeastOneNetwork;
|
||||
uint64_t id;
|
||||
@@ -104,9 +105,9 @@ struct Node
|
||||
to ensure timely receipt of future events. You should not call libzt API functions from
|
||||
this function unless it's something trivial like zts_inet_ntop() or similar that has
|
||||
no state-change implications. */
|
||||
void on_zts_event(void *msgPtr)
|
||||
void on_zts_event(void* msgPtr)
|
||||
{
|
||||
struct zts_callback_msg *msg = (struct zts_callback_msg *)msgPtr;
|
||||
struct zts_callback_msg* msg = (struct zts_callback_msg*)msgPtr;
|
||||
|
||||
// Node events
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_ONLINE) {
|
||||
@@ -115,7 +116,8 @@ void on_zts_event(void *msgPtr)
|
||||
myNode.online = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_OFFLINE) {
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, firewall, etc. What ports are you blocking?\n");
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, "
|
||||
"firewall, etc. What ports are you blocking?\n");
|
||||
myNode.online = false;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_NORMAL_TERMINATION) {
|
||||
@@ -125,25 +127,34 @@ void on_zts_event(void *msgPtr)
|
||||
|
||||
// Virtual network events
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_NOT_FOUND) {
|
||||
printf("ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_REQ_CONFIG) {
|
||||
printf("ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a few seconds...\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a "
|
||||
"few seconds...\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. "
|
||||
"Did you authorize the node yet?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_READY_IP4) {
|
||||
printf("ZTS_EVENT_NETWORK_READY_IP4 --- Network config received. IPv4 traffic can now be sent over network %llx\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP4 --- Network config received. IPv4 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->nwid);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP6 --- Network config received. IPv6 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->nwid);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_DOWN) {
|
||||
@@ -153,31 +164,41 @@ void on_zts_event(void *msgPtr)
|
||||
// Address events
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP4) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("ZTS_EVENT_ADDR_NEW_IP4 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid, ipstr);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_NEW_IP4 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid,
|
||||
ipstr);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid, ipstr);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_NEW_IP6 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid,
|
||||
ipstr);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP4) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("ZTS_EVENT_ADDR_REMOVED_IP4 --- The virtual address %s for this node on network %llx has been removed.\n",
|
||||
ipstr, msg->addr->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_REMOVED_IP4 --- The virtual address %s for this node on network %llx "
|
||||
"has been removed.\n",
|
||||
ipstr,
|
||||
msg->addr->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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_REMOVED_IP6 --- The virtual address %s for this node on network %llx has been removed.\n",
|
||||
ipstr, msg->addr->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_REMOVED_IP6 --- The virtual address %s for this node on network %llx "
|
||||
"has been removed.\n",
|
||||
ipstr,
|
||||
msg->addr->nwid);
|
||||
}
|
||||
|
||||
// Peer events
|
||||
@@ -188,34 +209,40 @@ void on_zts_event(void *msgPtr)
|
||||
return;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_DIRECT) {
|
||||
printf("ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_RELAY) {
|
||||
printf("ZTS_EVENT_PEER_RELAY --- No direct path to node=%llx\n", msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DISCOVERED) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for "
|
||||
"node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DEAD) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 6) {
|
||||
printf("\nlibzt example non-blocking client\n");
|
||||
printf("nonblockingclient <config_file_path> <nwid> <remoteAddr> <remotePort> <ztServicePort>\n");
|
||||
printf("nonblockingclient <config_file_path> <nwid> <remoteAddr> <remotePort> "
|
||||
"<ztServicePort>\n");
|
||||
exit(0);
|
||||
}
|
||||
uint64_t nwid = strtoull(argv[2],NULL,16); // Network ID to join
|
||||
std::string remoteAddr = argv[3]; // Remote application's virtual ZT address
|
||||
int remotePort = atoi(argv[4]); // Port the application will try to connect to the server on
|
||||
int ztServicePort = atoi(argv[5]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
uint64_t nwid = strtoull(argv[2], NULL, 16); // Network ID to join
|
||||
std::string remoteAddr = argv[3]; // Remote application's virtual ZT address
|
||||
int remotePort = atoi(argv[4]); // Port the application will try to connect to the server on
|
||||
int ztServicePort = atoi(
|
||||
argv[5]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
|
||||
struct zts_sockaddr_in in4;
|
||||
in4.sin_port = htons(remotePort);
|
||||
@@ -230,33 +257,39 @@ int main(int argc, char **argv)
|
||||
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take slightly longer to start the node
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take
|
||||
// slightly longer to start the node
|
||||
zts_allow_network_caching(1);
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take slightly longer to contact a remote peer
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take
|
||||
// slightly longer to contact a remote peer
|
||||
zts_allow_peer_caching(1);
|
||||
// If disabled: Settings will NOT be read from local.conf
|
||||
zts_allow_local_conf(1);
|
||||
|
||||
if((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online...\n");
|
||||
while (!myNode.online) { zts_delay_ms(50); }
|
||||
while (! myNode.online) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
printf("This node's identity is stored in %s\n", argv[1]);
|
||||
|
||||
if((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", nwid);
|
||||
printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
|
||||
while (!myNode.joinedAtLeastOneNetwork) { zts_delay_ms(50); }
|
||||
while (! myNode.joinedAtLeastOneNetwork) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
|
||||
// Socket-like API example
|
||||
|
||||
char *msgStr = (char*)"Welcome to the machine";
|
||||
int bytes=0, fd;
|
||||
char* msgStr = (char*)"Welcome to the machine";
|
||||
int bytes = 0, fd;
|
||||
char recvBuf[128];
|
||||
memset(recvBuf, 0, sizeof(recvBuf));
|
||||
|
||||
@@ -267,13 +300,19 @@ int main(int argc, char **argv)
|
||||
// Retries are often required since ZT uses transport-triggered links (explained above)
|
||||
for (;;) {
|
||||
printf("Connecting to remote host...\n");
|
||||
if ((err = zts_connect(fd, (const struct zts_sockaddr *)&in4, sizeof(in4))) < 0) {
|
||||
printf("Error connecting to remote host (fd=%d, ret=%d, zts_errno=%d). Trying again.\n",
|
||||
fd, err, zts_errno);
|
||||
if ((err = zts_connect(fd, (const struct zts_sockaddr*)&in4, sizeof(in4))) < 0) {
|
||||
printf(
|
||||
"Error connecting to remote host (fd=%d, ret=%d, zts_errno=%d). Trying again.\n",
|
||||
fd,
|
||||
err,
|
||||
zts_errno);
|
||||
zts_close(fd);
|
||||
printf("Creating socket...\n");
|
||||
if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
|
||||
printf("Error creating ZeroTier socket (fd=%d, zts_errno=%d). Exiting.\n", fd, zts_errno);
|
||||
printf(
|
||||
"Error creating ZeroTier socket (fd=%d, zts_errno=%d). Exiting.\n",
|
||||
fd,
|
||||
zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
zts_delay_ms(250);
|
||||
@@ -286,9 +325,13 @@ int main(int argc, char **argv)
|
||||
|
||||
// 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 writing to socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
while (1) {
|
||||
if ((bytes = zts_send(fd, msgStr, strlen(msgStr), 0)) < 0) {
|
||||
printf(
|
||||
"Error writing to socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n",
|
||||
fd,
|
||||
bytes,
|
||||
zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("zts_send()=%d\n", bytes);
|
||||
|
||||
@@ -84,15 +84,16 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
struct Node
|
||||
{
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0) {}
|
||||
struct Node {
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0)
|
||||
{
|
||||
}
|
||||
bool online;
|
||||
bool joinedAtLeastOneNetwork;
|
||||
uint64_t id;
|
||||
@@ -103,9 +104,9 @@ struct Node
|
||||
to ensure timely receipt of future events. You should not call libzt API functions from
|
||||
this function unless it's something trivial like zts_inet_ntop() or similar that has
|
||||
no state-change implications. */
|
||||
void on_zts_event(void *msgPtr)
|
||||
void on_zts_event(void* msgPtr)
|
||||
{
|
||||
struct zts_callback_msg *msg = (struct zts_callback_msg *)msgPtr;
|
||||
struct zts_callback_msg* msg = (struct zts_callback_msg*)msgPtr;
|
||||
|
||||
// Node events
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_ONLINE) {
|
||||
@@ -114,7 +115,8 @@ void on_zts_event(void *msgPtr)
|
||||
myNode.online = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_OFFLINE) {
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, firewall, etc. What ports are you blocking?\n");
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, "
|
||||
"firewall, etc. What ports are you blocking?\n");
|
||||
myNode.online = false;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_NORMAL_TERMINATION) {
|
||||
@@ -124,25 +126,34 @@ void on_zts_event(void *msgPtr)
|
||||
|
||||
// Virtual network events
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_NOT_FOUND) {
|
||||
printf("ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_REQ_CONFIG) {
|
||||
printf("ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a few seconds...\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a "
|
||||
"few seconds...\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. "
|
||||
"Did you authorize the node yet?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_READY_IP4) {
|
||||
printf("ZTS_EVENT_NETWORK_READY_IP4 --- Network config received. IPv4 traffic can now be sent over network %llx\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP4 --- Network config received. IPv4 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->nwid);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP6 --- Network config received. IPv6 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->nwid);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_DOWN) {
|
||||
@@ -152,31 +163,41 @@ void on_zts_event(void *msgPtr)
|
||||
// Address events
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP4) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("ZTS_EVENT_ADDR_NEW_IP4 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid, ipstr);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_NEW_IP4 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid,
|
||||
ipstr);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid, ipstr);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_NEW_IP6 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid,
|
||||
ipstr);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP4) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("ZTS_EVENT_ADDR_REMOVED_IP4 --- The virtual address %s for this node on network %llx has been removed.\n",
|
||||
ipstr, msg->addr->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_REMOVED_IP4 --- The virtual address %s for this node on network %llx "
|
||||
"has been removed.\n",
|
||||
ipstr,
|
||||
msg->addr->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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_REMOVED_IP6 --- The virtual address %s for this node on network %llx has been removed.\n",
|
||||
ipstr, msg->addr->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_REMOVED_IP6 --- The virtual address %s for this node on network %llx "
|
||||
"has been removed.\n",
|
||||
ipstr,
|
||||
msg->addr->nwid);
|
||||
}
|
||||
|
||||
// Peer events
|
||||
@@ -187,33 +208,38 @@ void on_zts_event(void *msgPtr)
|
||||
return;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_DIRECT) {
|
||||
printf("ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_RELAY) {
|
||||
printf("ZTS_EVENT_PEER_RELAY --- No direct path to node=%llx\n", msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DISCOVERED) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for "
|
||||
"node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DEAD) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example non-blocking server\n");
|
||||
printf("nonblockingserver <config_file_path> <nwid> <serverBindPort> <ztServicePort>\n");
|
||||
exit(0);
|
||||
}
|
||||
uint64_t nwid = strtoull(argv[2],NULL,16); // Network ID to join
|
||||
int serverBindPort = atoi(argv[3]); // Port the application should bind to
|
||||
int ztServicePort = atoi(argv[4]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
uint64_t nwid = strtoull(argv[2], NULL, 16); // Network ID to join
|
||||
int serverBindPort = atoi(argv[3]); // Port the application should bind to
|
||||
int ztServicePort = atoi(
|
||||
argv[4]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
|
||||
struct zts_sockaddr_in in4, acc_in4;
|
||||
in4.sin_port = htons(serverBindPort);
|
||||
@@ -229,51 +255,73 @@ int main(int argc, char **argv)
|
||||
int fd, accfd;
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take slightly longer to start the node
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take
|
||||
// slightly longer to start the node
|
||||
zts_allow_network_caching(1);
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take slightly longer to contact a remote peer
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take
|
||||
// slightly longer to contact a remote peer
|
||||
zts_allow_peer_caching(1);
|
||||
// If disabled: Settings will NOT be read from local.conf
|
||||
zts_allow_local_conf(1);
|
||||
|
||||
if((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online...\n");
|
||||
while (!myNode.online) { zts_delay_ms(50); }
|
||||
while (! myNode.online) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
printf("This node ID is %llx\n", myNode.id);
|
||||
printf("This node's identity is stored in %s\n", argv[1]);
|
||||
|
||||
if((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", nwid);
|
||||
printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
|
||||
while (!myNode.joinedAtLeastOneNetwork) { zts_delay_ms(50); }
|
||||
while (! myNode.joinedAtLeastOneNetwork) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
|
||||
// Socket-like API example
|
||||
|
||||
printf("Creating socket...\n");
|
||||
if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
|
||||
printf("Error creating ZeroTier socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n",fd, err, zts_errno);
|
||||
printf(
|
||||
"Error creating ZeroTier socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n",
|
||||
fd,
|
||||
err,
|
||||
zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Binding...\n");
|
||||
if ((err = zts_bind(fd, (struct zts_sockaddr *)&in4, sizeof(struct zts_sockaddr_in)) < 0)) {
|
||||
printf("Error binding to interface (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
if ((err = zts_bind(fd, (struct zts_sockaddr*)&in4, sizeof(struct zts_sockaddr_in)) < 0)) {
|
||||
printf(
|
||||
"Error binding to interface (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 placing socket in LISTENING state (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
printf(
|
||||
"Error placing socket in LISTENING state (fd=%d, ret=%d, zts_errno=%d). Exiting.\n",
|
||||
fd,
|
||||
err,
|
||||
zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
zts_socklen_t client_addrlen = sizeof(zts_sockaddr_in);
|
||||
if ((accfd = zts_accept(fd, (struct zts_sockaddr *)&acc_in4, &client_addrlen)) < 0) {
|
||||
printf("Error accepting connection (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
if ((accfd = zts_accept(fd, (struct zts_sockaddr*)&acc_in4, &client_addrlen)) < 0) {
|
||||
printf(
|
||||
"Error accepting connection (fd=%d, ret=%d, zts_errno=%d). Exiting.\n",
|
||||
fd,
|
||||
err,
|
||||
zts_errno);
|
||||
}
|
||||
|
||||
zts_socklen_t peer_addrlen = sizeof(struct zts_sockaddr_storage);
|
||||
@@ -283,7 +331,7 @@ int main(int argc, char **argv)
|
||||
zts_inet_ntop(ZTS_AF_INET, &(acc_in4.sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("Accepted connection from %s:%d\n", ipstr, ntohs(acc_in4.sin_port));
|
||||
|
||||
int bytes=0;
|
||||
int bytes = 0;
|
||||
char recvBuf[128];
|
||||
memset(recvBuf, 0, sizeof(recvBuf));
|
||||
|
||||
@@ -293,7 +341,7 @@ int main(int argc, char **argv)
|
||||
if (false) {
|
||||
zts_fcntl(fd, ZTS_F_SETFL, ZTS_O_NONBLOCK);
|
||||
zts_fcntl(accfd, ZTS_F_SETFL, ZTS_O_NONBLOCK);
|
||||
while(1) {
|
||||
while (1) {
|
||||
bytes = zts_recv(accfd, recvBuf, sizeof(recvBuf), 0);
|
||||
printf("zts_recv(%d, ...)=%d\n", accfd, bytes);
|
||||
zts_delay_ms(100);
|
||||
@@ -311,21 +359,18 @@ int main(int argc, char **argv)
|
||||
zts_fd_set active_fd_set, read_fd_set;
|
||||
ZTS_FD_ZERO(&active_fd_set);
|
||||
ZTS_FD_SET(accfd, &active_fd_set);
|
||||
while (1)
|
||||
{
|
||||
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);
|
||||
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))
|
||||
{
|
||||
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);
|
||||
// ZTS_FD_CLR(i, &active_fd_set);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -342,12 +387,11 @@ int main(int argc, char **argv)
|
||||
numfds++;
|
||||
int result = 0;
|
||||
int timeout_ms = 50;
|
||||
while(1) {
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -2,16 +2,17 @@
|
||||
* libzt API example
|
||||
*/
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string>
|
||||
|
||||
#include "ZeroTierSockets.h"
|
||||
|
||||
struct Node
|
||||
{
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0) {}
|
||||
struct Node {
|
||||
Node() : online(false), joinedAtLeastOneNetwork(false), id(0)
|
||||
{
|
||||
}
|
||||
bool online;
|
||||
bool joinedAtLeastOneNetwork;
|
||||
uint64_t id;
|
||||
@@ -22,9 +23,9 @@ struct Node
|
||||
to ensure timely receipt of future events. You should not call libzt API functions from
|
||||
this function unless it's something trivial like zts_inet_ntop() or similar that has
|
||||
no state-change implications. */
|
||||
void on_zts_event(void *msgPtr)
|
||||
void on_zts_event(void* msgPtr)
|
||||
{
|
||||
struct zts_callback_msg *msg = (struct zts_callback_msg *)msgPtr;
|
||||
struct zts_callback_msg* msg = (struct zts_callback_msg*)msgPtr;
|
||||
|
||||
// Node events
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_ONLINE) {
|
||||
@@ -33,7 +34,8 @@ void on_zts_event(void *msgPtr)
|
||||
myNode.online = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_OFFLINE) {
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, firewall, etc. What ports are you blocking?\n");
|
||||
printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, "
|
||||
"firewall, etc. What ports are you blocking?\n");
|
||||
myNode.online = false;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NODE_NORMAL_TERMINATION) {
|
||||
@@ -42,24 +44,34 @@ void on_zts_event(void *msgPtr)
|
||||
|
||||
// Virtual network events
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_NOT_FOUND) {
|
||||
printf("ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_REQ_CONFIG) {
|
||||
printf("ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a few seconds...\n", msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_REQ_CONFIG --- Requesting config for network %llx, please wait a "
|
||||
"few seconds...\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. "
|
||||
"Did you authorize the node yet?\n",
|
||||
msg->network->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_READY_IP4) {
|
||||
printf("ZTS_EVENT_NETWORK_READY_IP4 --- Network config received. IPv4 traffic can now be sent over network %llx\n",
|
||||
msg->network->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP4 --- Network config received. IPv4 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->nwid);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == 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->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_NETWORK_READY_IP6 --- Network config received. IPv6 traffic can now be sent "
|
||||
"over network %llx\n",
|
||||
msg->network->nwid);
|
||||
myNode.joinedAtLeastOneNetwork = true;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_NETWORK_DOWN) {
|
||||
@@ -69,31 +81,41 @@ void on_zts_event(void *msgPtr)
|
||||
// Address events
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP4) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("ZTS_EVENT_ADDR_NEW_IP4 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid, ipstr);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_NEW_IP4 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid,
|
||||
ipstr);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid, ipstr);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_NEW_IP6 --- This node's virtual address on network %llx is %s\n",
|
||||
msg->addr->nwid,
|
||||
ipstr);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP4) {
|
||||
char ipstr[ZTS_INET_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&(msg->addr->addr);
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN);
|
||||
printf("ZTS_EVENT_ADDR_REMOVED_IP4 --- The virtual address %s for this node on network %llx has been removed.\n",
|
||||
ipstr, msg->addr->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_REMOVED_IP4 --- The virtual address %s for this node on network %llx "
|
||||
"has been removed.\n",
|
||||
ipstr,
|
||||
msg->addr->nwid);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP6) {
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr);
|
||||
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_REMOVED_IP6 --- The virtual address %s for this node on network %llx has been removed.\n",
|
||||
ipstr, msg->addr->nwid);
|
||||
printf(
|
||||
"ZTS_EVENT_ADDR_REMOVED_IP6 --- The virtual address %s for this node on network %llx "
|
||||
"has been removed.\n",
|
||||
ipstr,
|
||||
msg->addr->nwid);
|
||||
}
|
||||
// Peer events
|
||||
if (msg->peer) {
|
||||
@@ -103,19 +125,23 @@ void on_zts_event(void *msgPtr)
|
||||
return;
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_DIRECT) {
|
||||
printf("ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_DIRECT --- A direct path is known for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_RELAY) {
|
||||
printf("ZTS_EVENT_PEER_RELAY --- No direct path to node=%llx\n", msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DISCOVERED) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DISCOVERED --- A new direct path was discovered for "
|
||||
"node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
if (msg->eventCode == ZTS_EVENT_PEER_PATH_DEAD) {
|
||||
printf("ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
printf(
|
||||
"ZTS_EVENT_PEER_PATH_DEAD --- A direct path has died for node=%llx\n",
|
||||
msg->peer->address);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -202,16 +228,17 @@ void on_zts_event(void *msgPtr)
|
||||
*
|
||||
*/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server <config_file_path> <nwid> <serverBindPort> <ztServicePort>\n");
|
||||
exit(0);
|
||||
}
|
||||
uint64_t nwid = strtoull(argv[2],NULL,16); // Network ID to join
|
||||
int serverBindPort = atoi(argv[3]); // Port the application should bind to
|
||||
int ztServicePort = atoi(argv[4]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
uint64_t nwid = strtoull(argv[2], NULL, 16); // Network ID to join
|
||||
int serverBindPort = atoi(argv[3]); // Port the application should bind to
|
||||
int ztServicePort = atoi(
|
||||
argv[4]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
|
||||
struct zts_sockaddr_in in4, acc_in4;
|
||||
in4.sin_port = htons(serverBindPort);
|
||||
@@ -227,56 +254,78 @@ int main(int argc, char **argv)
|
||||
int fd, accfd;
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take slightly longer to start the node
|
||||
// If disabled: (network) details will NOT be written to or read from (networks.d/). It may take
|
||||
// slightly longer to start the node
|
||||
zts_allow_network_caching(1);
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take slightly longer to contact a remote peer
|
||||
// If disabled: (peer) details will NOT be written to or read from (peers.d/). It may take
|
||||
// slightly longer to contact a remote peer
|
||||
zts_allow_peer_caching(1);
|
||||
// If disabled: Settings will NOT be read from local.conf
|
||||
zts_allow_local_conf(1);
|
||||
|
||||
if((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_start(argv[1], &on_zts_event, ztServicePort)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online...\n");
|
||||
while (!myNode.online) { zts_delay_ms(50); }
|
||||
while (! myNode.online) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
printf("This node's identity is stored in %s\n", argv[1]);
|
||||
|
||||
if((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
if ((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
printf("Unable to join network, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", nwid);
|
||||
printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
|
||||
while (!myNode.joinedAtLeastOneNetwork) { zts_delay_ms(50); }
|
||||
while (! myNode.joinedAtLeastOneNetwork) {
|
||||
zts_delay_ms(50);
|
||||
}
|
||||
|
||||
// Socket-like API example
|
||||
|
||||
printf("Creating socket...\n");
|
||||
if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
|
||||
printf("Error creating ZeroTier socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n",fd, err, zts_errno);
|
||||
printf(
|
||||
"Error creating ZeroTier socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n",
|
||||
fd,
|
||||
err,
|
||||
zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Binding...\n");
|
||||
if ((err = zts_bind(fd, (struct zts_sockaddr *)&in4, sizeof(struct zts_sockaddr_in)) < 0)) {
|
||||
printf("Error binding to interface (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
if ((err = zts_bind(fd, (struct zts_sockaddr*)&in4, sizeof(struct zts_sockaddr_in)) < 0)) {
|
||||
printf(
|
||||
"Error binding to interface (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 placing socket in LISTENING state (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
printf(
|
||||
"Error placing socket in LISTENING state (fd=%d, ret=%d, zts_errno=%d). Exiting.\n",
|
||||
fd,
|
||||
err,
|
||||
zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int bytes=0;
|
||||
int bytes = 0;
|
||||
char recvBuf[128];
|
||||
memset(recvBuf, 0, sizeof(recvBuf));
|
||||
|
||||
while (true) {
|
||||
zts_socklen_t client_addrlen = sizeof(zts_sockaddr_in);
|
||||
if ((accfd = zts_accept(fd, (struct zts_sockaddr *)&acc_in4, &client_addrlen)) < 0) {
|
||||
printf("Error accepting connection (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno);
|
||||
if ((accfd = zts_accept(fd, (struct zts_sockaddr*)&acc_in4, &client_addrlen)) < 0) {
|
||||
printf(
|
||||
"Error accepting connection (fd=%d, ret=%d, zts_errno=%d). Exiting.\n",
|
||||
fd,
|
||||
err,
|
||||
zts_errno);
|
||||
}
|
||||
zts_socklen_t peer_addrlen = sizeof(struct zts_sockaddr_storage);
|
||||
zts_getpeername(accfd, (struct zts_sockaddr*)&acc_in4, &peer_addrlen);
|
||||
@@ -287,14 +336,22 @@ int main(int argc, char **argv)
|
||||
printf("Accepted connection from %s:%d\n", ipstr, ntohs(acc_in4.sin_port));
|
||||
|
||||
printf("Reading message string from client...\n");
|
||||
if((bytes = zts_read(accfd, recvBuf, sizeof(recvBuf))) < 0) {
|
||||
printf("Error writing to socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
if ((bytes = zts_read(accfd, recvBuf, sizeof(recvBuf))) < 0) {
|
||||
printf(
|
||||
"Error writing to socket (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 writing to socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno);
|
||||
if ((bytes = zts_write(accfd, recvBuf, bytes)) < 0) {
|
||||
printf(
|
||||
"Error writing to socket (fd=%d, ret=%d, zts_errno=%d). Exiting.\n",
|
||||
fd,
|
||||
bytes,
|
||||
zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
printf("Sent %d bytes: %s\n", bytes, recvBuf);
|
||||
|
||||
Reference in New Issue
Block a user