From a247552df198f6ba33eba67763916aeab81c8b5b Mon Sep 17 00:00:00 2001 From: Joseph Henry Date: Mon, 26 Apr 2021 22:07:55 -0700 Subject: [PATCH] Format according to new ZeroTier standard --- examples/c/adhoc.c | 96 +- examples/c/callbackapi.c | 154 +- examples/c/client.c | 144 +- examples/c/customroots.c | 160 +- examples/c/nonblockingclient.c | 128 +- examples/c/nonblockingserver.c | 268 ++-- examples/c/nostorage.c | 140 +- examples/c/pingable-node.c | 86 +- examples/c/server.c | 154 +- examples/c/statistics.c | 206 +-- include/ZeroTierSockets.h | 1388 ++++++++--------- src/Central.cpp | 364 ++--- src/Controls.cpp | 684 ++++----- src/Debug.hpp | 20 +- src/Events.cpp | 300 ++-- src/Events.hpp | 146 +- src/NodeService.cpp | 2608 ++++++++++++++++---------------- src/NodeService.hpp | 690 ++++----- src/Signals.cpp | 66 +- src/Sockets.cpp | 996 ++++++------ src/Utilities.cpp | 200 +-- src/VirtualTap.cpp | 746 ++++----- src/VirtualTap.hpp | 238 +-- 23 files changed, 4991 insertions(+), 4991 deletions(-) diff --git a/examples/c/adhoc.c b/examples/c/adhoc.c index b36177a..87aecab 100644 --- a/examples/c/adhoc.c +++ b/examples/c/adhoc.c @@ -35,62 +35,62 @@ be taken to avoid exposing vulnerable services or sharing unwanted files or othe */ int main(int argc, char** argv) { - if (argc != 3) { - printf("\nUsage:\n"); - printf("adhoc \n"); - exit(0); - } - int err = ZTS_ERR_OK; + if (argc != 3) { + printf("\nUsage:\n"); + printf("adhoc \n"); + exit(0); + } + int err = ZTS_ERR_OK; - uint16_t adhocStartPort = atoi(argv[1]); // Start of port range your application will use - uint16_t adhocEndPort = atoi(argv[2]); // End of port range your application will use - long long int net_id = zts_net_compute_adhoc_id(adhocStartPort, adhocEndPort); // At least 64 bits + uint16_t adhocStartPort = atoi(argv[1]); // Start of port range your application will use + uint16_t adhocEndPort = atoi(argv[2]); // End of port range your application will use + long long int net_id = zts_net_compute_adhoc_id(adhocStartPort, adhocEndPort); // At least 64 bits - // Start node and get identity + // Start node and get identity - printf("Starting node...\n"); - zts_node_start(); - printf("Waiting for node to come online\n"); - while (! zts_node_is_online()) { - zts_util_delay(50); - } - uint64_t node_id = zts_node_get_id(); - printf("My public identity (node ID) is %llx\n", node_id); - char keypair[ZTS_ID_STR_BUF_LEN] = { 0 }; - uint16_t len = ZTS_ID_STR_BUF_LEN; - if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) { - printf("Error getting identity keypair. Exiting.\n"); - } - printf("Identity [public/secret pair] = %s\n", keypair); + printf("Starting node...\n"); + zts_node_start(); + printf("Waiting for node to come online\n"); + while (! zts_node_is_online()) { + zts_util_delay(50); + } + uint64_t node_id = zts_node_get_id(); + printf("My public identity (node ID) is %llx\n", node_id); + char keypair[ZTS_ID_STR_BUF_LEN] = { 0 }; + uint16_t len = ZTS_ID_STR_BUF_LEN; + if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) { + printf("Error getting identity keypair. Exiting.\n"); + } + printf("Identity [public/secret pair] = %s\n", keypair); - // Join the adhoc network + // Join the adhoc network - printf("Joining network %llx\n", net_id); - if (zts_net_join(net_id) != ZTS_ERR_OK) { - printf("Unable to join network. Exiting.\n"); - exit(1); - } - printf("Waiting for join to complete\n"); - while (! zts_net_transport_is_ready(net_id)) { - zts_util_delay(50); - } + printf("Joining network %llx\n", net_id); + if (zts_net_join(net_id) != ZTS_ERR_OK) { + printf("Unable to join network. Exiting.\n"); + exit(1); + } + printf("Waiting for join to complete\n"); + while (! zts_net_transport_is_ready(net_id)) { + zts_util_delay(50); + } - // Get address + // Get address - char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; - if ((err = zts_addr_compute_rfc4193_str(net_id, node_id, ipstr, ZTS_IP_MAX_STR_LEN)) != ZTS_ERR_OK) { - printf("Unable to compute address (error = %d). Exiting.\n", err); - exit(1); - } - printf("Join %llx from another machine and ping6 me at %s\n", net_id, ipstr); + char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; + if ((err = zts_addr_compute_rfc4193_str(net_id, node_id, ipstr, ZTS_IP_MAX_STR_LEN)) != ZTS_ERR_OK) { + printf("Unable to compute address (error = %d). Exiting.\n", err); + exit(1); + } + printf("Join %llx from another machine and ping6 me at %s\n", net_id, ipstr); - // Do network stuff! - // zts_socket, zts_connect, etc + // Do network stuff! + // zts_socket, zts_connect, etc - while (1) { - zts_util_delay(500); // Idle indefinitely - } + while (1) { + zts_util_delay(500); // Idle indefinitely + } - printf("Stopping node\n"); - return zts_node_stop(); + printf("Stopping node\n"); + return zts_node_stop(); } diff --git a/examples/c/callbackapi.c b/examples/c/callbackapi.c index 5c1441b..7e6ce63 100644 --- a/examples/c/callbackapi.c +++ b/examples/c/callbackapi.c @@ -12,97 +12,97 @@ void on_zts_event(void* msgPtr) { - zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr; - // Node events - if (msg->event_code == ZTS_EVENT_NODE_ONLINE) { - printf("ZTS_EVENT_NODE_ONLINE --- This node's ID is %llx\n", msg->node->node_id); - } - if (msg->event_code == ZTS_EVENT_NODE_OFFLINE) { - printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, " - "firewall, etc. What ports are you blocking?\n"); - } - // Virtual network events - if (msg->event_code == ZTS_EVENT_NETWORK_NOT_FOUND) { - printf("ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n", msg->network->net_id); - } - if (msg->event_code == ZTS_EVENT_NETWORK_ACCESS_DENIED) { - printf( - "ZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. " - "Did you authorize the node yet?\n", - msg->network->net_id); - } - if (msg->event_code == ZTS_EVENT_NETWORK_READY_IP6) { - printf( - "ZTS_EVENT_NETWORK_READY_IP6 --- Network config received. IPv6 traffic can now be sent " - "over network %llx\n", - msg->network->net_id); - } - // Address events - if (msg->event_code == ZTS_EVENT_ADDR_ADDED_IP6) { - char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 }; - struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr); - zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN); - printf("ZTS_EVENT_ADDR_NEW_IP6 --- Join %llx and ping me at %s\n", msg->addr->net_id, ipstr); - } + zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr; + // Node events + if (msg->event_code == ZTS_EVENT_NODE_ONLINE) { + printf("ZTS_EVENT_NODE_ONLINE --- This node's ID is %llx\n", msg->node->node_id); + } + if (msg->event_code == ZTS_EVENT_NODE_OFFLINE) { + printf("ZTS_EVENT_NODE_OFFLINE --- Check your physical Internet connection, router, " + "firewall, etc. What ports are you blocking?\n"); + } + // Virtual network events + if (msg->event_code == ZTS_EVENT_NETWORK_NOT_FOUND) { + printf("ZTS_EVENT_NETWORK_NOT_FOUND --- Are you sure %llx is a valid network?\n", msg->network->net_id); + } + if (msg->event_code == ZTS_EVENT_NETWORK_ACCESS_DENIED) { + printf( + "ZTS_EVENT_NETWORK_ACCESS_DENIED --- Access to virtual network %llx has been denied. " + "Did you authorize the node yet?\n", + msg->network->net_id); + } + if (msg->event_code == ZTS_EVENT_NETWORK_READY_IP6) { + printf( + "ZTS_EVENT_NETWORK_READY_IP6 --- Network config received. IPv6 traffic can now be sent " + "over network %llx\n", + msg->network->net_id); + } + // Address events + if (msg->event_code == ZTS_EVENT_ADDR_ADDED_IP6) { + char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 }; + struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&(msg->addr->addr); + zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN); + printf("ZTS_EVENT_ADDR_NEW_IP6 --- Join %llx and ping me at %s\n", msg->addr->net_id, ipstr); + } - // To see more exhaustive examples look at test/selftest.c + // To see more exhaustive examples look at test/selftest.c } int main(int argc, char** argv) { - if (argc != 2) { - printf("\nUsage:\n"); - printf("pingable-node \n"); - exit(0); - } - long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits + if (argc != 2) { + printf("\nUsage:\n"); + printf("pingable-node \n"); + exit(0); + } + long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits - zts_init_set_event_handler(&on_zts_event); + zts_init_set_event_handler(&on_zts_event); - printf("Starting node...\n"); - zts_node_start(); + printf("Starting node...\n"); + zts_node_start(); - printf("Waiting for node to come online\n"); - while (! zts_node_is_online()) { - zts_util_delay(50); - } + printf("Waiting for node to come online\n"); + while (! zts_node_is_online()) { + zts_util_delay(50); + } - printf("My public identity (node ID) is %llx\n", zts_node_get_id()); - char keypair[ZTS_ID_STR_BUF_LEN] = { 0 }; - uint16_t len = ZTS_ID_STR_BUF_LEN; - if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) { - printf("Error getting identity keypair. Exiting.\n"); - } - printf("Identity [public/secret pair] = %s\n", keypair); + printf("My public identity (node ID) is %llx\n", zts_node_get_id()); + char keypair[ZTS_ID_STR_BUF_LEN] = { 0 }; + uint16_t len = ZTS_ID_STR_BUF_LEN; + if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) { + printf("Error getting identity keypair. Exiting.\n"); + } + printf("Identity [public/secret pair] = %s\n", keypair); - printf("Joining network %llx\n", net_id); - if (zts_net_join(net_id) != ZTS_ERR_OK) { - printf("Unable to join network. Exiting.\n"); - exit(1); - } + printf("Joining network %llx\n", net_id); + if (zts_net_join(net_id) != ZTS_ERR_OK) { + printf("Unable to join network. Exiting.\n"); + exit(1); + } - printf("Waiting for join to complete\n"); - while (! zts_net_transport_is_ready(net_id)) { - zts_util_delay(50); - } + printf("Waiting for join to complete\n"); + while (! zts_net_transport_is_ready(net_id)) { + zts_util_delay(50); + } - printf("Waiting for address assignment from network\n"); - int err = 0; - while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) { - zts_util_delay(500); - } + printf("Waiting for address assignment from network\n"); + int err = 0; + while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) { + zts_util_delay(500); + } - char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; - zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN); - printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr); + char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; + zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN); + printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr); - // Do network stuff! - // zts_socket, zts_connect, etc + // Do network stuff! + // zts_socket, zts_connect, etc - while (1) { - zts_util_delay(500); // Idle indefinitely - } + while (1) { + zts_util_delay(500); // Idle indefinitely + } - printf("Stopping node\n"); - return zts_node_stop(); + printf("Stopping node\n"); + return zts_node_stop(); } diff --git a/examples/c/client.c b/examples/c/client.c index 3b63381..5a8e43b 100644 --- a/examples/c/client.c +++ b/examples/c/client.c @@ -12,94 +12,94 @@ int main(int argc, char** argv) { - if (argc != 5) { - printf("\nlibzt example client\n"); - printf("client \n"); - exit(0); - } - char* storage_path = argv[1]; - long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits - char* remote_addr = argv[3]; - int remote_port = atoi(argv[4]); - int err = ZTS_ERR_OK; + if (argc != 5) { + printf("\nlibzt example client\n"); + printf("client \n"); + exit(0); + } + char* storage_path = argv[1]; + long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits + char* remote_addr = argv[3]; + int remote_port = atoi(argv[4]); + int err = ZTS_ERR_OK; - // Initialize node + // Initialize node - if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) { - printf("Unable to start service, error = %d. Exiting.\n", err); - exit(1); - } + if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) { + printf("Unable to start service, error = %d. Exiting.\n", err); + exit(1); + } - // Start node + // Start node - if ((err = zts_node_start()) != ZTS_ERR_OK) { - printf("Unable to start service, error = %d. Exiting.\n", err); - exit(1); - } - printf("Waiting for node to come online\n"); - while (! zts_node_is_online()) { - zts_util_delay(50); - } - printf("Public identity (node ID) is %llx\n", (long long int)zts_node_get_id()); + if ((err = zts_node_start()) != ZTS_ERR_OK) { + printf("Unable to start service, error = %d. Exiting.\n", err); + exit(1); + } + printf("Waiting for node to come online\n"); + while (! zts_node_is_online()) { + zts_util_delay(50); + } + printf("Public identity (node ID) is %llx\n", (long long int)zts_node_get_id()); - // Join network + // Join network - printf("Joining network %llx\n", net_id); - if (zts_net_join(net_id) != ZTS_ERR_OK) { - printf("Unable to join network. Exiting.\n"); - exit(1); - } - printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n"); - printf("Waiting for join to complete\n"); - while (! zts_net_transport_is_ready(net_id)) { - zts_util_delay(50); - } + printf("Joining network %llx\n", net_id); + if (zts_net_join(net_id) != ZTS_ERR_OK) { + printf("Unable to join network. Exiting.\n"); + exit(1); + } + printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n"); + printf("Waiting for join to complete\n"); + while (! zts_net_transport_is_ready(net_id)) { + zts_util_delay(50); + } - // Get assigned address (of the family type we care about) + // Get assigned address (of the family type we care about) - int family = zts_util_get_ip_family(remote_addr); + int family = zts_util_get_ip_family(remote_addr); - printf("Waiting for address assignment from network\n"); - while (! (err = zts_addr_is_assigned(net_id, family))) { - zts_util_delay(50); - } - char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; - zts_addr_get_str(net_id, family, ipstr, ZTS_IP_MAX_STR_LEN); - printf("IP address on network %llx is %s\n", net_id, ipstr); + printf("Waiting for address assignment from network\n"); + while (! (err = zts_addr_is_assigned(net_id, family))) { + zts_util_delay(50); + } + char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; + zts_addr_get_str(net_id, family, ipstr, ZTS_IP_MAX_STR_LEN); + printf("IP address on network %llx is %s\n", net_id, ipstr); - // BEGIN Socket Stuff + // BEGIN Socket Stuff - char* msgStr = (char*)"Welcome to the machine"; - int bytes = 0, fd; - char recvBuf[128] = { 0 }; - memset(recvBuf, 0, sizeof(recvBuf)); + char* msgStr = (char*)"Welcome to the machine"; + int bytes = 0, fd; + char recvBuf[128] = { 0 }; + memset(recvBuf, 0, sizeof(recvBuf)); - // Connect to remote host + // Connect to remote host - // Can also use traditional: zts_socket(), zts_connect(), etc + // Can also use traditional: zts_socket(), zts_connect(), etc - printf("Attempting to connect...\n"); - while ((fd = zts_simple_tcp_client(remote_addr, remote_port)) < 0) { - printf("Re-attempting to connect...\n"); - } + printf("Attempting to connect...\n"); + while ((fd = zts_simple_tcp_client(remote_addr, remote_port)) < 0) { + printf("Re-attempting to connect...\n"); + } - // Data I/O + // Data I/O - printf("Sending message string to server...\n"); - if ((bytes = zts_write(fd, msgStr, strlen(msgStr))) < 0) { - printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno); - exit(1); - } - printf("Sent %d bytes: %s\n", bytes, msgStr); - printf("Reading message string from server...\n"); - if ((bytes = zts_read(fd, recvBuf, sizeof(recvBuf))) < 0) { - printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno); - exit(1); - } - printf("Read %d bytes: %s\n", bytes, recvBuf); + printf("Sending message string to server...\n"); + if ((bytes = zts_write(fd, msgStr, strlen(msgStr))) < 0) { + printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno); + exit(1); + } + printf("Sent %d bytes: %s\n", bytes, msgStr); + printf("Reading message string from server...\n"); + if ((bytes = zts_read(fd, recvBuf, sizeof(recvBuf))) < 0) { + printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno); + exit(1); + } + printf("Read %d bytes: %s\n", bytes, recvBuf); - // Close + // Close - zts_close(fd); - return zts_node_stop(); + zts_close(fd); + return zts_node_stop(); } diff --git a/examples/c/customroots.c b/examples/c/customroots.c index 8f05e7a..f7bced5 100644 --- a/examples/c/customroots.c +++ b/examples/c/customroots.c @@ -12,108 +12,108 @@ void print_peer_details(const char* msg, zts_peer_info_t* d) { - printf(" %s\n", msg); - printf("\t- peer : %llx\n", d->address); - printf("\t- role : %d\n", d->role); - printf("\t- latency : %d\n", d->latency); - printf("\t- version : %d.%d.%d\n", d->ver_major, d->ver_minor, d->ver_rev); - printf("\t- path_count : %d\n", d->path_count); - printf("\t- paths:\n"); + printf(" %s\n", msg); + printf("\t- peer : %llx\n", d->address); + printf("\t- role : %d\n", d->role); + printf("\t- latency : %d\n", d->latency); + printf("\t- version : %d.%d.%d\n", d->ver_major, d->ver_minor, d->ver_rev); + printf("\t- path_count : %d\n", d->path_count); + printf("\t- paths:\n"); - // Print all known paths for each peer - for (unsigned int j = 0; j < d->path_count; j++) { - char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 }; - int port = 0; - struct zts_sockaddr* sa = (struct zts_sockaddr*)&(d->paths[j].address); - if (sa->sa_family == ZTS_AF_INET) { - struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)sa; - zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN); - port = ntohs(in4->sin_port); - } - if (sa->sa_family == ZTS_AF_INET6) { - struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)sa; - zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN); - } - printf("\t - %15s : %6d\n", ipstr, port); - } - printf("\n\n"); + // Print all known paths for each peer + for (unsigned int j = 0; j < d->path_count; j++) { + char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 }; + int port = 0; + struct zts_sockaddr* sa = (struct zts_sockaddr*)&(d->paths[j].address); + if (sa->sa_family == ZTS_AF_INET) { + struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)sa; + zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), ipstr, ZTS_INET_ADDRSTRLEN); + port = ntohs(in4->sin_port); + } + if (sa->sa_family == ZTS_AF_INET6) { + struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)sa; + zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), ipstr, ZTS_INET6_ADDRSTRLEN); + } + printf("\t - %15s : %6d\n", ipstr, port); + } + printf("\n\n"); } void on_zts_event(void* msgPtr) { - zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr; - printf("event_code = %d\n", msg->event_code); + zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr; + printf("event_code = %d\n", msg->event_code); - if (msg->peer) { - if (msg->peer->role != ZTS_PEER_ROLE_PLANET) { - return; // Don't print controllers and ordinary nodes. - } - } - if (msg->event_code == ZTS_EVENT_PEER_DIRECT) { - print_peer_details("ZTS_EVENT_PEER_DIRECT", msg->peer); - } - if (msg->event_code == ZTS_EVENT_PEER_RELAY) { - print_peer_details("ZTS_EVENT_PEER_RELAY", msg->peer); - } + if (msg->peer) { + if (msg->peer->role != ZTS_PEER_ROLE_PLANET) { + return; // Don't print controllers and ordinary nodes. + } + } + if (msg->event_code == ZTS_EVENT_PEER_DIRECT) { + print_peer_details("ZTS_EVENT_PEER_DIRECT", msg->peer); + } + if (msg->event_code == ZTS_EVENT_PEER_RELAY) { + print_peer_details("ZTS_EVENT_PEER_RELAY", msg->peer); + } } int main() { - // World generation + // World generation - // Buffers that will be filled after generating the world - char world_data_out[4096] = { 0 }; // (binary) Your new world definition - unsigned int world_len = 0; - unsigned int prev_key_len = 0; - unsigned int curr_key_len = 0; - char prev_key[4096] = { 0 }; // (binary) (optional) For updating a world - char curr_key[4096] = { 0 }; // (binary) You should save this + // Buffers that will be filled after generating the world + char world_data_out[4096] = { 0 }; // (binary) Your new world definition + unsigned int world_len = 0; + unsigned int prev_key_len = 0; + unsigned int curr_key_len = 0; + char prev_key[4096] = { 0 }; // (binary) (optional) For updating a world + char curr_key[4096] = { 0 }; // (binary) You should save this - // Arbitrary World ID - uint64_t id = 149604618; + // Arbitrary World ID + uint64_t id = 149604618; - // Timestamp indicating when this world was generated - uint64_t ts = 1567191349589ULL; + // Timestamp indicating when this world was generated + uint64_t ts = 1567191349589ULL; - // struct containing public keys and stable IP endpoints for roots - zts_world_t world = { 0 }; + // struct containing public keys and stable IP endpoints for roots + zts_world_t world = { 0 }; - world.public_id_str[0] = - "992fcf1db7:0:" - "206ed59350b31916f749a1f85dffb3a8787dcbf83b8c6e9448d4e3ea0e3369301be716c3609344a9d1533850fb4460c5" - "0af43322bcfc8e13d3301a1f1003ceb6"; - world.endpoint_ip_str[0][0] = "195.181.173.159/9993"; - world.endpoint_ip_str[0][1] = "2a02:6ea0:c024::/9993"; + world.public_id_str[0] = + "992fcf1db7:0:" + "206ed59350b31916f749a1f85dffb3a8787dcbf83b8c6e9448d4e3ea0e3369301be716c3609344a9d1533850fb4460c5" + "0af43322bcfc8e13d3301a1f1003ceb6"; + world.endpoint_ip_str[0][0] = "195.181.173.159/9993"; + world.endpoint_ip_str[0][1] = "2a02:6ea0:c024::/9993"; - // Generate world + // Generate world - zts_util_world_new(&world_data_out, &world_len, &prev_key, &prev_key_len, &curr_key, &curr_key_len, id, ts, &world); + zts_util_world_new(&world_data_out, &world_len, &prev_key, &prev_key_len, &curr_key, &curr_key_len, id, ts, &world); - printf("world_data_out= "); - for (int i = 0; i < world_len; i++) { - if (i > 0) { - printf(","); - } - printf("0x%.2x", (unsigned char)world_data_out[i]); - } - printf("\n"); - printf("world_len = %d\n", world_len); - printf("prev_key_len = %d\n", prev_key_len); - printf("curr_key_len = %d\n", curr_key_len); + printf("world_data_out= "); + for (int i = 0; i < world_len; i++) { + if (i > 0) { + printf(","); + } + printf("0x%.2x", (unsigned char)world_data_out[i]); + } + printf("\n"); + printf("world_len = %d\n", world_len); + printf("prev_key_len = %d\n", prev_key_len); + printf("curr_key_len = %d\n", curr_key_len); - // Now, initialize node and use newly-generated world definition + // Now, initialize node and use newly-generated world definition - zts_init_set_world(&world_data_out, world_len); - zts_init_set_event_handler(&on_zts_event); - zts_init_from_storage("."); + zts_init_set_world(&world_data_out, world_len); + zts_init_set_event_handler(&on_zts_event); + zts_init_from_storage("."); - // Start node + // Start node - zts_node_start(); + zts_node_start(); - while (1) { - zts_util_delay(500); - } + while (1) { + zts_util_delay(500); + } - return zts_node_stop(); + return zts_node_stop(); } diff --git a/examples/c/nonblockingclient.c b/examples/c/nonblockingclient.c index 7fdaed2..76177f2 100644 --- a/examples/c/nonblockingclient.c +++ b/examples/c/nonblockingclient.c @@ -12,85 +12,85 @@ int main(int argc, char** argv) { - if (argc != 5) { - printf("\nlibzt example client\n"); - printf("client \n"); - exit(0); - } - char* storage_path = argv[1]; - long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits - char* remote_addr = argv[3]; - int remote_port = atoi(argv[4]); - int err = ZTS_ERR_OK; + if (argc != 5) { + printf("\nlibzt example client\n"); + printf("client \n"); + exit(0); + } + char* storage_path = argv[1]; + long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits + char* remote_addr = argv[3]; + int remote_port = atoi(argv[4]); + int err = ZTS_ERR_OK; - // Initialize node + // Initialize node - if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) { - printf("Unable to start service, error = %d. Exiting.\n", err); - exit(1); - } + if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) { + printf("Unable to start service, error = %d. Exiting.\n", err); + exit(1); + } - // Start node + // Start node - if ((err = zts_node_start()) != ZTS_ERR_OK) { - printf("Unable to start service, error = %d. Exiting.\n", err); - exit(1); - } + if ((err = zts_node_start()) != ZTS_ERR_OK) { + printf("Unable to start service, error = %d. Exiting.\n", err); + exit(1); + } - printf("Waiting for node to come online\n"); - while (! zts_node_is_online()) { - zts_util_delay(50); - } + printf("Waiting for node to come online\n"); + while (! zts_node_is_online()) { + zts_util_delay(50); + } - printf("Public identity (node ID) is %llx\n", (long long int)zts_node_get_id()); + printf("Public identity (node ID) is %llx\n", (long long int)zts_node_get_id()); - printf("Joining network %llx\n", net_id); - if (zts_net_join(net_id) != ZTS_ERR_OK) { - printf("Unable to join network. Exiting.\n"); - exit(1); - } + printf("Joining network %llx\n", net_id); + if (zts_net_join(net_id) != ZTS_ERR_OK) { + printf("Unable to join network. Exiting.\n"); + exit(1); + } - printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n"); - printf("Waiting for join to complete\n"); - while (! zts_net_transport_is_ready(net_id)) { - zts_util_delay(50); - } + printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n"); + printf("Waiting for join to complete\n"); + while (! zts_net_transport_is_ready(net_id)) { + zts_util_delay(50); + } - // Sockets + // Sockets - char* msgStr = (char*)"Welcome to the machine"; - int bytes = 0, fd; - char recvBuf[128] = { 0 }; - memset(recvBuf, 0, sizeof(recvBuf)); + char* msgStr = (char*)"Welcome to the machine"; + int bytes = 0, fd; + char recvBuf[128] = { 0 }; + memset(recvBuf, 0, sizeof(recvBuf)); - // Create socket + // Create socket - if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) { - printf("Error (fd=%d, zts_errno=%d). Exiting.\n", fd, zts_errno); - exit(1); - } + if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) { + printf("Error (fd=%d, zts_errno=%d). Exiting.\n", fd, zts_errno); + exit(1); + } - // Connect + // Connect - // Can also use: - // zts_connect(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen); - while (zts_simple_connect(fd, remote_addr, remote_port, 0) != ZTS_ERR_OK) { - printf("Attempting to connect...\n"); - } + // Can also use: + // zts_connect(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen); + while (zts_simple_connect(fd, remote_addr, remote_port, 0) != ZTS_ERR_OK) { + printf("Attempting to connect...\n"); + } - // Data I/O + // Data I/O - // Wait random intervals to send a message to the server - // The non-blocking aspect of this example is server-side - while (1) { - if ((bytes = zts_send(fd, msgStr, strlen(msgStr), 0)) < 0) { - printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno); - exit(1); - } - printf("zts_send()=%d\n", bytes); - zts_util_delay((rand() % 100) * 50); - } + // Wait random intervals to send a message to the server + // The non-blocking aspect of this example is server-side + while (1) { + if ((bytes = zts_send(fd, msgStr, strlen(msgStr), 0)) < 0) { + printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno); + exit(1); + } + printf("zts_send()=%d\n", bytes); + zts_util_delay((rand() % 100) * 50); + } - zts_close(fd); - return zts_node_stop(); + zts_close(fd); + return zts_node_stop(); } diff --git a/examples/c/nonblockingserver.c b/examples/c/nonblockingserver.c index cf2d6b4..86d1892 100644 --- a/examples/c/nonblockingserver.c +++ b/examples/c/nonblockingserver.c @@ -12,158 +12,158 @@ int main(int argc, char** argv) { - if (argc != 5) { - printf("\nlibzt example server\n"); - printf("server \n"); - exit(0); - } - char* storage_path = argv[1]; - long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits - char* local_addr = argv[3]; - int local_port = atoi(argv[4]); - int fd, accfd; - int err = ZTS_ERR_OK; + if (argc != 5) { + printf("\nlibzt example server\n"); + printf("server \n"); + exit(0); + } + char* storage_path = argv[1]; + long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits + char* local_addr = argv[3]; + int local_port = atoi(argv[4]); + int fd, accfd; + int err = ZTS_ERR_OK; - // Initialize node + // Initialize node - if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) { - printf("Unable to start service, error = %d. Exiting.\n", err); - exit(1); - } + if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) { + printf("Unable to start service, error = %d. Exiting.\n", err); + exit(1); + } - // Start node + // Start node - if ((err = zts_node_start()) != ZTS_ERR_OK) { - printf("Unable to start service, error = %d. Exiting.\n", err); - exit(1); - } + if ((err = zts_node_start()) != ZTS_ERR_OK) { + printf("Unable to start service, error = %d. Exiting.\n", err); + exit(1); + } - printf("Waiting for node to come online\n"); - while (! zts_node_is_online()) { - zts_util_delay(50); - } + printf("Waiting for node to come online\n"); + while (! zts_node_is_online()) { + zts_util_delay(50); + } - printf("Public identity (node ID) is %llx\n", zts_node_get_id()); + printf("Public identity (node ID) is %llx\n", zts_node_get_id()); - printf("Joining network %llx\n", net_id); - if (zts_net_join(net_id) != ZTS_ERR_OK) { - printf("Unable to join network. Exiting.\n"); - exit(1); - } + printf("Joining network %llx\n", net_id); + if (zts_net_join(net_id) != ZTS_ERR_OK) { + printf("Unable to join network. Exiting.\n"); + exit(1); + } - printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n"); - printf("Waiting for join to complete\n"); - while (! zts_net_transport_is_ready(net_id)) { - zts_util_delay(50); - } + printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n"); + printf("Waiting for join to complete\n"); + while (! zts_net_transport_is_ready(net_id)) { + zts_util_delay(50); + } - printf("Waiting for address assignment from network\n"); - while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) { - zts_util_delay(500); - } + printf("Waiting for address assignment from network\n"); + while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) { + zts_util_delay(500); + } - char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; - zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN); - printf("Assigned IP address: %s\n", ipstr); + char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; + zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN); + printf("Assigned IP address: %s\n", ipstr); - // Sockets + // Sockets - printf("Creating socket...\n"); - if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) { - printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno); - exit(1); - } - printf("Binding...\n"); - // Can also use: - // zts_bind(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen) - if ((err = zts_simple_bind(fd, local_addr, local_port) < 0)) { - printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno); - exit(1); - } - printf("Listening...\n"); - int backlog = 100; - if ((err = zts_listen(fd, backlog)) < 0) { - printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno); - exit(1); - } + printf("Creating socket...\n"); + if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) { + printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno); + exit(1); + } + printf("Binding...\n"); + // Can also use: + // zts_bind(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen) + if ((err = zts_simple_bind(fd, local_addr, local_port) < 0)) { + printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno); + exit(1); + } + printf("Listening...\n"); + int backlog = 100; + if ((err = zts_listen(fd, backlog)) < 0) { + printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno); + exit(1); + } - int bytes = 0; - char recvBuf[128] = { 0 }; + int bytes = 0; + char recvBuf[128] = { 0 }; - while (1) { - // Accept - // Can also use - // zts_accept(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen) + while (1) { + // Accept + // Can also use + // zts_accept(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen) - char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 }; - int port = 0; - printf("Accepting on listening socket...\n"); - if ((accfd = zts_simple_accept(fd, ipstr, ZTS_INET6_ADDRSTRLEN, &port)) < 0) { - printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno); - } - printf("Accepted connection from %s:%d\n", ipstr, port); - } + char ipstr[ZTS_INET6_ADDRSTRLEN] = { 0 }; + int port = 0; + printf("Accepting on listening socket...\n"); + if ((accfd = zts_simple_accept(fd, ipstr, ZTS_INET6_ADDRSTRLEN, &port)) < 0) { + printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, err, zts_errno); + } + printf("Accepted connection from %s:%d\n", ipstr, port); + } - // Data I/O + // Data I/O - // Technique 1: ZTS_O_NONBLOCK - if (0) { - zts_fcntl(fd, ZTS_F_SETFL, ZTS_O_NONBLOCK); - zts_fcntl(accfd, ZTS_F_SETFL, ZTS_O_NONBLOCK); - while (1) { - bytes = zts_recv(accfd, recvBuf, sizeof(recvBuf), 0); - printf("zts_recv(%d, ...)=%d\n", accfd, bytes); - zts_util_delay(100); - } - } + // Technique 1: ZTS_O_NONBLOCK + if (0) { + zts_fcntl(fd, ZTS_F_SETFL, ZTS_O_NONBLOCK); + zts_fcntl(accfd, ZTS_F_SETFL, ZTS_O_NONBLOCK); + while (1) { + bytes = zts_recv(accfd, recvBuf, sizeof(recvBuf), 0); + printf("zts_recv(%d, ...)=%d\n", accfd, bytes); + zts_util_delay(100); + } + } - // Technique 2: zts_select - if (0) { - struct zts_timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 50000; - int result = 0; - zts_fd_set active_fd_set, read_fd_set; - ZTS_FD_ZERO(&active_fd_set); - ZTS_FD_SET(accfd, &active_fd_set); - while (1) { - read_fd_set = active_fd_set; - if ((result = zts_select(ZTS_FD_SETSIZE, &read_fd_set, NULL, NULL, &tv) < 0)) { - // perror ("select"); - exit(1); - } - for (int i = 0; i < ZTS_FD_SETSIZE; i++) { - if (ZTS_FD_ISSET(i, &read_fd_set)) { - bytes = zts_recv(accfd, recvBuf, sizeof(recvBuf), 0); - printf("zts_recv(%d, ...)=%d\n", i, bytes); - } - // ZTS_FD_CLR(i, &active_fd_set); - } - } - } + // Technique 2: zts_select + if (0) { + struct zts_timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 50000; + int result = 0; + zts_fd_set active_fd_set, read_fd_set; + ZTS_FD_ZERO(&active_fd_set); + ZTS_FD_SET(accfd, &active_fd_set); + while (1) { + read_fd_set = active_fd_set; + if ((result = zts_select(ZTS_FD_SETSIZE, &read_fd_set, NULL, NULL, &tv) < 0)) { + // perror ("select"); + exit(1); + } + for (int i = 0; i < ZTS_FD_SETSIZE; i++) { + if (ZTS_FD_ISSET(i, &read_fd_set)) { + bytes = zts_recv(accfd, recvBuf, sizeof(recvBuf), 0); + printf("zts_recv(%d, ...)=%d\n", i, bytes); + } + // ZTS_FD_CLR(i, &active_fd_set); + } + } + } - // Technique 3: zts_poll - if (1) { - int numfds = 0; - struct zts_pollfd poll_set[16]; - memset(poll_set, '\0', sizeof(poll_set)); - poll_set[0].fd = accfd; - poll_set[0].events = ZTS_POLLIN; - numfds++; - int result = 0; - int timeout_ms = 50; - while (1) { - result = zts_poll(poll_set, numfds, timeout_ms); - printf("zts_poll()=%d\n", result); - for (int i = 0; i < numfds; i++) { - if (poll_set[i].revents & ZTS_POLLIN) { - bytes = zts_recv(poll_set[i].fd, recvBuf, sizeof(recvBuf), 0); - printf("zts_recv(%d, ...)=%d\n", i, bytes); - } - } - } - } + // Technique 3: zts_poll + if (1) { + int numfds = 0; + struct zts_pollfd poll_set[16]; + memset(poll_set, '\0', sizeof(poll_set)); + poll_set[0].fd = accfd; + poll_set[0].events = ZTS_POLLIN; + numfds++; + int result = 0; + int timeout_ms = 50; + while (1) { + result = zts_poll(poll_set, numfds, timeout_ms); + printf("zts_poll()=%d\n", result); + for (int i = 0; i < numfds; i++) { + if (poll_set[i].revents & ZTS_POLLIN) { + bytes = zts_recv(poll_set[i].fd, recvBuf, sizeof(recvBuf), 0); + printf("zts_recv(%d, ...)=%d\n", i, bytes); + } + } + } + } - err = zts_close(fd); - return zts_node_stop(); + err = zts_close(fd); + return zts_node_stop(); } diff --git a/examples/c/nostorage.c b/examples/c/nostorage.c index b530f81..1b26afa 100644 --- a/examples/c/nostorage.c +++ b/examples/c/nostorage.c @@ -18,92 +18,92 @@ char cache_data[ZTS_STORE_DATA_LEN]; void on_zts_event(void* msgPtr) { - zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr; - int len = msg->len; // Length of message (or structure) + zts_event_msg_t* msg = (zts_event_msg_t*)msgPtr; + int len = msg->len; // Length of message (or structure) - if (msg->event_code == ZTS_EVENT_NODE_ONLINE) { - printf("ZTS_EVENT_NODE_ONLINE\n"); - } + if (msg->event_code == ZTS_EVENT_NODE_ONLINE) { + printf("ZTS_EVENT_NODE_ONLINE\n"); + } - // Copy data to a buffer that you have allocated or write it to storage. - // The data pointed to by msg->cache will be invalid after this function - // returns. + // Copy data to a buffer that you have allocated or write it to storage. + // The data pointed to by msg->cache will be invalid after this function + // returns. - memset(cache_data, 0, ZTS_STORE_DATA_LEN); + memset(cache_data, 0, ZTS_STORE_DATA_LEN); - if (msg->event_code == ZTS_EVENT_STORE_IDENTITY_PUBLIC) { - printf("ZTS_EVENT_STORE_IDENTITY_PUBLIC (len=%d)\n", msg->len); - printf("identity.public = [ %.*s ]\n", len, msg->cache); - memcpy(cache_data, msg->cache, len); - } - if (msg->event_code == ZTS_EVENT_STORE_IDENTITY_SECRET) { - printf("ZTS_EVENT_STORE_IDENTITY_SECRET (len=%d)\n", msg->len); - printf("identity.secret = [ %.*s ]\n", len, msg->cache); - memcpy(cache_data, msg->cache, len); - // Same data can be retrieved via: zts_node_get_id_pair() - } - if (msg->event_code == ZTS_EVENT_STORE_PLANET) { - printf("ZTS_EVENT_STORE_PLANET (len=%d)\n", msg->len); - // Binary data - memcpy(cache_data, msg->cache, len); - } - if (msg->event_code == ZTS_EVENT_STORE_PEER) { - printf("ZTS_EVENT_STORE_PEER (len=%d)\n", msg->len); - // Binary data - memcpy(cache_data, msg->cache, len); - } - if (msg->event_code == ZTS_EVENT_STORE_NETWORK) { - printf("ZTS_EVENT_STORE_NETWORK (len=%d)\n", msg->len); - // Binary data - memcpy(cache_data, msg->cache, len); - } + if (msg->event_code == ZTS_EVENT_STORE_IDENTITY_PUBLIC) { + printf("ZTS_EVENT_STORE_IDENTITY_PUBLIC (len=%d)\n", msg->len); + printf("identity.public = [ %.*s ]\n", len, msg->cache); + memcpy(cache_data, msg->cache, len); + } + if (msg->event_code == ZTS_EVENT_STORE_IDENTITY_SECRET) { + printf("ZTS_EVENT_STORE_IDENTITY_SECRET (len=%d)\n", msg->len); + printf("identity.secret = [ %.*s ]\n", len, msg->cache); + memcpy(cache_data, msg->cache, len); + // Same data can be retrieved via: zts_node_get_id_pair() + } + if (msg->event_code == ZTS_EVENT_STORE_PLANET) { + printf("ZTS_EVENT_STORE_PLANET (len=%d)\n", msg->len); + // Binary data + memcpy(cache_data, msg->cache, len); + } + if (msg->event_code == ZTS_EVENT_STORE_PEER) { + printf("ZTS_EVENT_STORE_PEER (len=%d)\n", msg->len); + // Binary data + memcpy(cache_data, msg->cache, len); + } + if (msg->event_code == ZTS_EVENT_STORE_NETWORK) { + printf("ZTS_EVENT_STORE_NETWORK (len=%d)\n", msg->len); + // Binary data + memcpy(cache_data, msg->cache, len); + } } int main(int argc, char** argv) { - int err = ZTS_ERR_OK; + int err = ZTS_ERR_OK; - // Initialize node + // Initialize node - zts_init_set_event_handler(&on_zts_event); + zts_init_set_event_handler(&on_zts_event); - // Start node + // Start node - printf("Starting node...\n"); - int generate_new_id = 1; - if (generate_new_id) { - // OPTION A - // Generate new automatically ID if no prior init called - zts_node_start(); - } - else { - // OPTION B - // Copy your key here - char identity[ZTS_ID_STR_BUF_LEN] = { 0 }; - int len = ZTS_ID_STR_BUF_LEN; + printf("Starting node...\n"); + int generate_new_id = 1; + if (generate_new_id) { + // OPTION A + // Generate new automatically ID if no prior init called + zts_node_start(); + } + else { + // OPTION B + // Copy your key here + char identity[ZTS_ID_STR_BUF_LEN] = { 0 }; + int len = ZTS_ID_STR_BUF_LEN; - // Generate key (optional): - // int key_len; - // zts_id_new(identity, &key_len); + // Generate key (optional): + // int key_len; + // zts_id_new(identity, &key_len); - // Load pre-existing identity from buffer - zts_init_from_memory(identity, len); - zts_node_start(); - } + // Load pre-existing identity from buffer + zts_init_from_memory(identity, len); + zts_node_start(); + } - printf("Waiting for node to come online\n"); - while (! zts_node_is_online()) { - zts_util_delay(50); - } + printf("Waiting for node to come online\n"); + while (! zts_node_is_online()) { + zts_util_delay(50); + } - // Do network stuff! - // zts_socket, zts_connect, etc + // Do network stuff! + // zts_socket, zts_connect, etc - printf("Node %llx is now online. Idling.\n", zts_node_get_id()); - while (1) { - zts_util_delay(500); // Idle indefinitely - } + printf("Node %llx is now online. Idling.\n", zts_node_get_id()); + while (1) { + zts_util_delay(500); // Idle indefinitely + } - printf("Stopping node\n"); - return zts_node_stop(); + printf("Stopping node\n"); + return zts_node_stop(); } diff --git a/examples/c/pingable-node.c b/examples/c/pingable-node.c index 0767de8..d479257 100644 --- a/examples/c/pingable-node.c +++ b/examples/c/pingable-node.c @@ -11,57 +11,57 @@ int main(int argc, char** argv) { - if (argc != 2) { - printf("\nUsage:\n"); - printf("pingable-node \n"); - exit(0); - } - long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits + if (argc != 2) { + printf("\nUsage:\n"); + printf("pingable-node \n"); + exit(0); + } + long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits - printf("Starting node...\n"); - zts_node_start(); + printf("Starting node...\n"); + zts_node_start(); - printf("Waiting for node to come online\n"); - while (! zts_node_is_online()) { - zts_util_delay(50); - } + printf("Waiting for node to come online\n"); + while (! zts_node_is_online()) { + zts_util_delay(50); + } - printf("My public identity (node ID) is %llx\n", (long long int)zts_node_get_id()); - char keypair[ZTS_ID_STR_BUF_LEN] = { 0 }; - unsigned int len = ZTS_ID_STR_BUF_LEN; - if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) { - printf("Error getting identity keypair. Exiting.\n"); - } - printf("Identity [public/secret pair] = %s\n", keypair); + printf("My public identity (node ID) is %llx\n", (long long int)zts_node_get_id()); + char keypair[ZTS_ID_STR_BUF_LEN] = { 0 }; + unsigned int len = ZTS_ID_STR_BUF_LEN; + if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) { + printf("Error getting identity keypair. Exiting.\n"); + } + printf("Identity [public/secret pair] = %s\n", keypair); - printf("Joining network %llx\n", net_id); - if (zts_net_join(net_id) != ZTS_ERR_OK) { - printf("Unable to join network. Exiting.\n"); - exit(1); - } + printf("Joining network %llx\n", net_id); + if (zts_net_join(net_id) != ZTS_ERR_OK) { + printf("Unable to join network. Exiting.\n"); + exit(1); + } - printf("Waiting for join to complete\n"); - while (! zts_net_transport_is_ready(net_id)) { - zts_util_delay(50); - } + printf("Waiting for join to complete\n"); + while (! zts_net_transport_is_ready(net_id)) { + zts_util_delay(50); + } - printf("Waiting for address assignment from network\n"); - int err = 0; - while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) { - zts_util_delay(500); - } + printf("Waiting for address assignment from network\n"); + int err = 0; + while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) { + zts_util_delay(500); + } - char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; - zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN); - printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr); + char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; + zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN); + printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr); - // Do network stuff! - // zts_socket, zts_connect, etc + // Do network stuff! + // zts_socket, zts_connect, etc - while (1) { - zts_util_delay(500); // Idle indefinitely - } + while (1) { + zts_util_delay(500); // Idle indefinitely + } - printf("Stopping node\n"); - return zts_node_stop(); + printf("Stopping node\n"); + return zts_node_stop(); } diff --git a/examples/c/server.c b/examples/c/server.c index 0d23b6f..bab4253 100644 --- a/examples/c/server.c +++ b/examples/c/server.c @@ -12,99 +12,99 @@ int main(int argc, char** argv) { - if (argc != 5) { - printf("\nlibzt example server\n"); - printf("server \n"); - exit(0); - } - char* storage_path = argv[1]; - long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits - char* local_addr = argv[3]; - int local_port = atoi(argv[4]); - int fd, accfd; - int err = ZTS_ERR_OK; + if (argc != 5) { + printf("\nlibzt example server\n"); + printf("server \n"); + exit(0); + } + char* storage_path = argv[1]; + long long int net_id = strtoull(argv[2], NULL, 16); // At least 64 bits + char* local_addr = argv[3]; + int local_port = atoi(argv[4]); + int fd, accfd; + int err = ZTS_ERR_OK; - // Initialize node + // Initialize node - if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) { - printf("Unable to start service, error = %d. Exiting.\n", err); - exit(1); - } + if ((err = zts_init_from_storage(storage_path)) != ZTS_ERR_OK) { + printf("Unable to start service, error = %d. Exiting.\n", err); + exit(1); + } - // Start node + // Start node - if ((err = zts_node_start()) != ZTS_ERR_OK) { - printf("Unable to start service, error = %d. Exiting.\n", err); - exit(1); - } - printf("Waiting for node to come online\n"); - while (! zts_node_is_online()) { - zts_util_delay(50); - } - printf("Public identity (node ID) is %llx\n", zts_node_get_id()); + if ((err = zts_node_start()) != ZTS_ERR_OK) { + printf("Unable to start service, error = %d. Exiting.\n", err); + exit(1); + } + printf("Waiting for node to come online\n"); + while (! zts_node_is_online()) { + zts_util_delay(50); + } + printf("Public identity (node ID) is %llx\n", zts_node_get_id()); - // Join network + // Join network - printf("Joining network %llx\n", net_id); - if (zts_net_join(net_id) != ZTS_ERR_OK) { - printf("Unable to join network. Exiting.\n"); - exit(1); - } - printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n"); - printf("Waiting for join to complete\n"); - while (! zts_net_transport_is_ready(net_id)) { - zts_util_delay(50); - } + printf("Joining network %llx\n", net_id); + if (zts_net_join(net_id) != ZTS_ERR_OK) { + printf("Unable to join network. Exiting.\n"); + exit(1); + } + printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n"); + printf("Waiting for join to complete\n"); + while (! zts_net_transport_is_ready(net_id)) { + zts_util_delay(50); + } - // Get assigned address (of the family type we care about) + // Get assigned address (of the family type we care about) - int family = zts_util_get_ip_family(local_addr); + int family = zts_util_get_ip_family(local_addr); - printf("Waiting for address assignment from network\n"); - while (! (err = zts_addr_is_assigned(net_id, family))) { - zts_util_delay(50); - } - char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; - zts_addr_get_str(net_id, family, ipstr, ZTS_IP_MAX_STR_LEN); - printf("IP address on network %llx is %s\n", net_id, ipstr); + printf("Waiting for address assignment from network\n"); + while (! (err = zts_addr_is_assigned(net_id, family))) { + zts_util_delay(50); + } + char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; + zts_addr_get_str(net_id, family, ipstr, ZTS_IP_MAX_STR_LEN); + printf("IP address on network %llx is %s\n", net_id, ipstr); - // BEGIN Socket Stuff + // BEGIN Socket Stuff - // Accept incoming connection + // Accept incoming connection - // Can also use traditional: zts_socket(), zts_bind(), zts_listen(), zts_accept(), etc. + // Can also use traditional: zts_socket(), zts_bind(), zts_listen(), zts_accept(), etc. - char remote_addr[ZTS_INET6_ADDRSTRLEN] = { 0 }; - int remote_port = 0; - int len = ZTS_INET6_ADDRSTRLEN; - if ((accfd = zts_simple_tcp_server(local_addr, local_port, remote_addr, len, &remote_port)) < 0) { - printf("Error (fd=%d, zts_errno=%d). Exiting.\n", accfd, zts_errno); - exit(1); - } - printf("Accepted connection from %s:%d\n", remote_addr, remote_port); + char remote_addr[ZTS_INET6_ADDRSTRLEN] = { 0 }; + int remote_port = 0; + int len = ZTS_INET6_ADDRSTRLEN; + if ((accfd = zts_simple_tcp_server(local_addr, local_port, remote_addr, len, &remote_port)) < 0) { + printf("Error (fd=%d, zts_errno=%d). Exiting.\n", accfd, zts_errno); + exit(1); + } + printf("Accepted connection from %s:%d\n", remote_addr, remote_port); - // Data I/O + // Data I/O - int bytes = 0; - char recvBuf[128] = { 0 }; + int bytes = 0; + char recvBuf[128] = { 0 }; - printf("Reading message string from client...\n"); - if ((bytes = zts_read(accfd, recvBuf, sizeof(recvBuf))) < 0) { - printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno); - exit(1); - } - printf("Read %d bytes: %s\n", bytes, recvBuf); - printf("Sending message string to client...\n"); - if ((bytes = zts_write(accfd, recvBuf, bytes)) < 0) { - printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno); - exit(1); - } - printf("Sent %d bytes: %s\n", bytes, recvBuf); + printf("Reading message string from client...\n"); + if ((bytes = zts_read(accfd, recvBuf, sizeof(recvBuf))) < 0) { + printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno); + exit(1); + } + printf("Read %d bytes: %s\n", bytes, recvBuf); + printf("Sending message string to client...\n"); + if ((bytes = zts_write(accfd, recvBuf, bytes)) < 0) { + printf("Error (fd=%d, ret=%d, zts_errno=%d). Exiting.\n", fd, bytes, zts_errno); + exit(1); + } + printf("Sent %d bytes: %s\n", bytes, recvBuf); - // Close + // Close - printf("Closing connection socket\n"); - err = zts_close(accfd); - err = zts_close(fd); - return zts_node_stop(); + printf("Closing connection socket\n"); + err = zts_close(accfd); + err = zts_close(fd); + return zts_node_stop(); } diff --git a/examples/c/statistics.c b/examples/c/statistics.c index 87e2740..1b27f85 100644 --- a/examples/c/statistics.c +++ b/examples/c/statistics.c @@ -12,119 +12,119 @@ int main(int argc, char** argv) { - if (argc != 2) { - printf("\nUsage:\n"); - printf("pingable-node \n"); - exit(0); - } - long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits + if (argc != 2) { + printf("\nUsage:\n"); + printf("pingable-node \n"); + exit(0); + } + long long int net_id = strtoull(argv[1], NULL, 16); // At least 64 bits - printf("Starting node...\n"); - zts_node_start(); + printf("Starting node...\n"); + zts_node_start(); - printf("Waiting for node to come online\n"); - while (! zts_node_is_online()) { - zts_util_delay(50); - } + printf("Waiting for node to come online\n"); + while (! zts_node_is_online()) { + zts_util_delay(50); + } - printf("My public identity (node ID) is %llx\n", zts_node_get_id()); - char keypair[ZTS_ID_STR_BUF_LEN] = { 0 }; - uint16_t len = ZTS_ID_STR_BUF_LEN; - if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) { - printf("Error getting identity keypair. Exiting.\n"); - } - printf("Identity [public/secret pair] = %s\n", keypair); + printf("My public identity (node ID) is %llx\n", zts_node_get_id()); + char keypair[ZTS_ID_STR_BUF_LEN] = { 0 }; + uint16_t len = ZTS_ID_STR_BUF_LEN; + if (zts_node_get_id_pair(keypair, &len) != ZTS_ERR_OK) { + printf("Error getting identity keypair. Exiting.\n"); + } + printf("Identity [public/secret pair] = %s\n", keypair); - printf("Joining network %llx\n", net_id); - if (zts_net_join(net_id) != ZTS_ERR_OK) { - printf("Unable to join network. Exiting.\n"); - exit(1); - } + printf("Joining network %llx\n", net_id); + if (zts_net_join(net_id) != ZTS_ERR_OK) { + printf("Unable to join network. Exiting.\n"); + exit(1); + } - printf("Waiting for join to complete\n"); - while (! zts_net_transport_is_ready(net_id)) { - zts_util_delay(50); - } + printf("Waiting for join to complete\n"); + while (! zts_net_transport_is_ready(net_id)) { + zts_util_delay(50); + } - printf("Waiting for address assignment from network\n"); - int err = 0; - while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) { - zts_util_delay(500); - } + printf("Waiting for address assignment from network\n"); + int err = 0; + while (! (err = zts_addr_is_assigned(net_id, ZTS_AF_INET))) { + zts_util_delay(500); + } - char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; - zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN); - printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr); + char ipstr[ZTS_IP_MAX_STR_LEN] = { 0 }; + zts_addr_get_str(net_id, ZTS_AF_INET, ipstr, ZTS_IP_MAX_STR_LEN); + printf("Join %llx from another machine and ping me at %s\n", net_id, ipstr); - // Do network stuff! - // zts_socket, zts_connect, etc + // Do network stuff! + // zts_socket, zts_connect, etc - // Show protocol statistics + // Show protocol statistics - zts_stats_counter_t s = { 0 }; + zts_stats_counter_t s = { 0 }; - while (1) { - zts_util_delay(1000); - if ((err = zts_stats_get_all(&s)) == ZTS_ERR_NO_RESULT) { - printf("no results\n"); - continue; - } - printf("\n\n"); + while (1) { + zts_util_delay(1000); + if ((err = zts_stats_get_all(&s)) == ZTS_ERR_NO_RESULT) { + printf("no results\n"); + continue; + } + printf("\n\n"); - printf( - " link_tx=%9d, link_rx=%9d, link_drop=%9d, link_err=%9d\n", - s.link_tx, - s.link_rx, - s.link_drop, - s.link_err); - printf( - "etharp_tx=%9d, etharp_rx=%9d, etharp_drop=%9d, etharp_err=%9d\n", - s.etharp_tx, - s.etharp_rx, - s.etharp_drop, - s.etharp_err); - printf( - " ip4_tx=%9d, ip4_rx=%9d, ip4_drop=%9d, ip4_err=%9d\n", - s.ip4_tx, - s.ip4_rx, - s.ip4_drop, - s.ip4_err); - printf( - " ip6_tx=%9d, ip6_rx=%9d, ip6_drop=%9d, ip6_err=%9d\n", - s.ip6_tx, - s.ip6_rx, - s.ip6_drop, - s.ip6_err); - printf( - " icmp4_tx=%9d, icmp4_rx=%9d, icmp4_drop=%9d, icmp4_err=%9d\n", - s.icmp4_tx, - s.icmp4_rx, - s.icmp4_drop, - s.icmp4_err); - printf( - " icmp6_tx=%9d, icmp6_rx=%9d, icmp6_drop=%9d, icmp6_err=%9d\n", - s.icmp6_tx, - s.icmp6_rx, - s.icmp6_drop, - s.icmp6_err); - printf( - " udp_tx=%9d, udp_rx=%9d, udp_drop=%9d, udp_err=%9d\n", - s.udp_tx, - s.udp_rx, - s.udp_drop, - s.udp_err); - printf( - " tcp_tx=%9d, tcp_rx=%9d, tcp_drop=%9d, tcp_err=%9d\n", - s.tcp_tx, - s.tcp_rx, - s.tcp_drop, - s.tcp_err); - printf( - " nd6_tx=%9d, nd6_rx=%9d, nd6_drop=%9d, nd6_err=%9d\n", - s.nd6_tx, - s.nd6_rx, - s.nd6_drop, - s.nd6_err); - } - return zts_node_stop(); + printf( + " link_tx=%9d, link_rx=%9d, link_drop=%9d, link_err=%9d\n", + s.link_tx, + s.link_rx, + s.link_drop, + s.link_err); + printf( + "etharp_tx=%9d, etharp_rx=%9d, etharp_drop=%9d, etharp_err=%9d\n", + s.etharp_tx, + s.etharp_rx, + s.etharp_drop, + s.etharp_err); + printf( + " ip4_tx=%9d, ip4_rx=%9d, ip4_drop=%9d, ip4_err=%9d\n", + s.ip4_tx, + s.ip4_rx, + s.ip4_drop, + s.ip4_err); + printf( + " ip6_tx=%9d, ip6_rx=%9d, ip6_drop=%9d, ip6_err=%9d\n", + s.ip6_tx, + s.ip6_rx, + s.ip6_drop, + s.ip6_err); + printf( + " icmp4_tx=%9d, icmp4_rx=%9d, icmp4_drop=%9d, icmp4_err=%9d\n", + s.icmp4_tx, + s.icmp4_rx, + s.icmp4_drop, + s.icmp4_err); + printf( + " icmp6_tx=%9d, icmp6_rx=%9d, icmp6_drop=%9d, icmp6_err=%9d\n", + s.icmp6_tx, + s.icmp6_rx, + s.icmp6_drop, + s.icmp6_err); + printf( + " udp_tx=%9d, udp_rx=%9d, udp_drop=%9d, udp_err=%9d\n", + s.udp_tx, + s.udp_rx, + s.udp_drop, + s.udp_err); + printf( + " tcp_tx=%9d, tcp_rx=%9d, tcp_drop=%9d, tcp_err=%9d\n", + s.tcp_tx, + s.tcp_rx, + s.tcp_drop, + s.tcp_err); + printf( + " nd6_tx=%9d, nd6_rx=%9d, nd6_drop=%9d, nd6_err=%9d\n", + s.nd6_tx, + s.nd6_rx, + s.nd6_drop, + s.nd6_err); + } + return zts_node_stop(); } diff --git a/include/ZeroTierSockets.h b/include/ZeroTierSockets.h index bbae5d5..2e4579f 100644 --- a/include/ZeroTierSockets.h +++ b/include/ZeroTierSockets.h @@ -30,18 +30,18 @@ extern "C" { /** Common error return values */ typedef enum { - /** No error */ - ZTS_ERR_OK = 0, - /** Socket error, see `zts_errno` */ - ZTS_ERR_SOCKET = -1, - /** This operation is not allowed at this time. Or possibly the node hasn't been started */ - ZTS_ERR_SERVICE = -2, - /** Invalid argument */ - ZTS_ERR_ARG = -3, - /** No result (not necessarily an error) */ - ZTS_ERR_NO_RESULT = -4, - /** Consider filing a bug report */ - ZTS_ERR_GENERAL = -5 + /** No error */ + ZTS_ERR_OK = 0, + /** Socket error, see `zts_errno` */ + ZTS_ERR_SOCKET = -1, + /** This operation is not allowed at this time. Or possibly the node hasn't been started */ + ZTS_ERR_SERVICE = -2, + /** Invalid argument */ + ZTS_ERR_ARG = -3, + /** No result (not necessarily an error) */ + ZTS_ERR_NO_RESULT = -4, + /** Consider filing a bug report */ + ZTS_ERR_GENERAL = -5 } zts_error_t; //----------------------------------------------------------------------------// @@ -50,144 +50,144 @@ typedef enum { /** Event codes used by the (optional) callback API */ typedef enum { - /** - * Node has been initialized - * - * This is the first event generated, and is always sent. It may occur - * before node's constructor returns. - * - */ - ZTS_EVENT_NODE_UP = 200, + /** + * Node has been initialized + * + * This is the first event generated, and is always sent. It may occur + * before node's constructor returns. + * + */ + ZTS_EVENT_NODE_UP = 200, - /** - * Node is online -- at least one upstream node appears reachable - * - */ - ZTS_EVENT_NODE_ONLINE = 201, + /** + * Node is online -- at least one upstream node appears reachable + * + */ + ZTS_EVENT_NODE_ONLINE = 201, - /** - * Node is offline -- network does not seem to be reachable by any available - * strategy - * - */ - ZTS_EVENT_NODE_OFFLINE = 202, + /** + * Node is offline -- network does not seem to be reachable by any available + * strategy + * + */ + ZTS_EVENT_NODE_OFFLINE = 202, - /** - * Node is shutting down - * - * This is generated within Node's destructor when it is being shut down. - * It's done for convenience, since cleaning up other state in the event - * handler may appear more idiomatic. - * - */ - ZTS_EVENT_NODE_DOWN = 203, + /** + * Node is shutting down + * + * This is generated within Node's destructor when it is being shut down. + * It's done for convenience, since cleaning up other state in the event + * handler may appear more idiomatic. + * + */ + ZTS_EVENT_NODE_DOWN = 203, - /** - * A fatal error has occurred. One possible reason is: - * - * Your identity has collided with another node's ZeroTier address - * - * This happens if two different public keys both hash (via the algorithm - * in Identity::generate()) to the same 40-bit ZeroTier address. - * - * This is something you should "never" see, where "never" is defined as - * once per 2^39 new node initializations / identity creations. If you do - * see it, you're going to see it very soon after a node is first - * initialized. - * - * This is reported as an event rather than a return code since it's - * detected asynchronously via error messages from authoritative nodes. - * - * If this occurs, you must shut down and delete the node, delete the - * identity.secret record/file from the data store, and restart to generate - * a new identity. If you don't do this, you will not be able to communicate - * with other nodes. - * - * We'd automate this process, but we don't think silently deleting - * private keys or changing our address without telling the calling code - * is good form. It violates the principle of least surprise. - * - * You can technically get away with not handling this, but we recommend - * doing so in a mature reliable application. Besides, handling this - * condition is a good way to make sure it never arises. It's like how - * umbrellas prevent rain and smoke detectors prevent fires. They do, right? - * - * Meta-data: none - */ - ZTS_EVENT_NODE_FATAL_ERROR = 204, + /** + * A fatal error has occurred. One possible reason is: + * + * Your identity has collided with another node's ZeroTier address + * + * This happens if two different public keys both hash (via the algorithm + * in Identity::generate()) to the same 40-bit ZeroTier address. + * + * This is something you should "never" see, where "never" is defined as + * once per 2^39 new node initializations / identity creations. If you do + * see it, you're going to see it very soon after a node is first + * initialized. + * + * This is reported as an event rather than a return code since it's + * detected asynchronously via error messages from authoritative nodes. + * + * If this occurs, you must shut down and delete the node, delete the + * identity.secret record/file from the data store, and restart to generate + * a new identity. If you don't do this, you will not be able to communicate + * with other nodes. + * + * We'd automate this process, but we don't think silently deleting + * private keys or changing our address without telling the calling code + * is good form. It violates the principle of least surprise. + * + * You can technically get away with not handling this, but we recommend + * doing so in a mature reliable application. Besides, handling this + * condition is a good way to make sure it never arises. It's like how + * umbrellas prevent rain and smoke detectors prevent fires. They do, right? + * + * Meta-data: none + */ + ZTS_EVENT_NODE_FATAL_ERROR = 204, - /** Network ID does not correspond to a known network */ - ZTS_EVENT_NETWORK_NOT_FOUND = 210, - /** The version of ZeroTier inside libzt is too old */ - ZTS_EVENT_NETWORK_CLIENT_TOO_OLD = 211, - /** The configuration for a network has been requested (no action needed) */ - ZTS_EVENT_NETWORK_REQ_CONFIG = 212, - /** The node joined the network successfully (no action needed) */ - ZTS_EVENT_NETWORK_OK = 213, - /** The node is not allowed to join the network (you must authorize node) */ - ZTS_EVENT_NETWORK_ACCESS_DENIED = 214, - /** The node has received an IPv4 address from the network controller */ - ZTS_EVENT_NETWORK_READY_IP4 = 215, - /** The node has received an IPv6 address from the network controller */ - ZTS_EVENT_NETWORK_READY_IP6 = 216, - /** Deprecated */ - ZTS_EVENT_NETWORK_READY_IP4_IP6 = 217, - /** Network controller is unreachable */ - ZTS_EVENT_NETWORK_DOWN = 218, - /** Network change received from controller */ - ZTS_EVENT_NETWORK_UPDATE = 219, + /** Network ID does not correspond to a known network */ + ZTS_EVENT_NETWORK_NOT_FOUND = 210, + /** The version of ZeroTier inside libzt is too old */ + ZTS_EVENT_NETWORK_CLIENT_TOO_OLD = 211, + /** The configuration for a network has been requested (no action needed) */ + ZTS_EVENT_NETWORK_REQ_CONFIG = 212, + /** The node joined the network successfully (no action needed) */ + ZTS_EVENT_NETWORK_OK = 213, + /** The node is not allowed to join the network (you must authorize node) */ + ZTS_EVENT_NETWORK_ACCESS_DENIED = 214, + /** The node has received an IPv4 address from the network controller */ + ZTS_EVENT_NETWORK_READY_IP4 = 215, + /** The node has received an IPv6 address from the network controller */ + ZTS_EVENT_NETWORK_READY_IP6 = 216, + /** Deprecated */ + ZTS_EVENT_NETWORK_READY_IP4_IP6 = 217, + /** Network controller is unreachable */ + ZTS_EVENT_NETWORK_DOWN = 218, + /** Network change received from controller */ + ZTS_EVENT_NETWORK_UPDATE = 219, - /** TCP/IP stack (lwIP) is up (for debug purposes) */ - ZTS_EVENT_STACK_UP = 220, - /** TCP/IP stack (lwIP) id down (for debug purposes) */ - ZTS_EVENT_STACK_DOWN = 221, + /** TCP/IP stack (lwIP) is up (for debug purposes) */ + ZTS_EVENT_STACK_UP = 220, + /** TCP/IP stack (lwIP) id down (for debug purposes) */ + ZTS_EVENT_STACK_DOWN = 221, - /** lwIP netif up (for debug purposes) */ - ZTS_EVENT_NETIF_UP = 230, - /** lwIP netif down (for debug purposes) */ - ZTS_EVENT_NETIF_DOWN = 231, - /** lwIP netif removed (for debug purposes) */ - ZTS_EVENT_NETIF_REMOVED = 232, - /** lwIP netif link up (for debug purposes) */ - ZTS_EVENT_NETIF_LINK_UP = 233, - /** lwIP netif link down (for debug purposes) */ - ZTS_EVENT_NETIF_LINK_DOWN = 234, + /** lwIP netif up (for debug purposes) */ + ZTS_EVENT_NETIF_UP = 230, + /** lwIP netif down (for debug purposes) */ + ZTS_EVENT_NETIF_DOWN = 231, + /** lwIP netif removed (for debug purposes) */ + ZTS_EVENT_NETIF_REMOVED = 232, + /** lwIP netif link up (for debug purposes) */ + ZTS_EVENT_NETIF_LINK_UP = 233, + /** lwIP netif link down (for debug purposes) */ + ZTS_EVENT_NETIF_LINK_DOWN = 234, - /** A direct P2P path to peer is known */ - ZTS_EVENT_PEER_DIRECT = 240, - /** A direct P2P path to peer is NOT known. Traffic is now relayed */ - ZTS_EVENT_PEER_RELAY = 241, - /** A peer is unreachable. Check NAT/Firewall settings */ - ZTS_EVENT_PEER_UNREACHABLE = 242, - /** A new path to a peer was discovered */ - ZTS_EVENT_PEER_PATH_DISCOVERED = 243, - /** A known path to a peer is now considered dead */ - ZTS_EVENT_PEER_PATH_DEAD = 244, + /** A direct P2P path to peer is known */ + ZTS_EVENT_PEER_DIRECT = 240, + /** A direct P2P path to peer is NOT known. Traffic is now relayed */ + ZTS_EVENT_PEER_RELAY = 241, + /** A peer is unreachable. Check NAT/Firewall settings */ + ZTS_EVENT_PEER_UNREACHABLE = 242, + /** A new path to a peer was discovered */ + ZTS_EVENT_PEER_PATH_DISCOVERED = 243, + /** A known path to a peer is now considered dead */ + ZTS_EVENT_PEER_PATH_DEAD = 244, - /** A new managed network route was added */ - ZTS_EVENT_ROUTE_ADDED = 250, - /** A managed network route was removed */ - ZTS_EVENT_ROUTE_REMOVED = 251, + /** A new managed network route was added */ + ZTS_EVENT_ROUTE_ADDED = 250, + /** A managed network route was removed */ + ZTS_EVENT_ROUTE_REMOVED = 251, - /** A new managed IPv4 address was assigned to this peer */ - ZTS_EVENT_ADDR_ADDED_IP4 = 260, - /** A managed IPv4 address assignment was removed from this peer */ - ZTS_EVENT_ADDR_REMOVED_IP4 = 261, - /** A new managed IPv4 address was assigned to this peer */ - ZTS_EVENT_ADDR_ADDED_IP6 = 262, - /** A managed IPv6 address assignment was removed from this peer */ - ZTS_EVENT_ADDR_REMOVED_IP6 = 263, + /** A new managed IPv4 address was assigned to this peer */ + ZTS_EVENT_ADDR_ADDED_IP4 = 260, + /** A managed IPv4 address assignment was removed from this peer */ + ZTS_EVENT_ADDR_REMOVED_IP4 = 261, + /** A new managed IPv4 address was assigned to this peer */ + ZTS_EVENT_ADDR_ADDED_IP6 = 262, + /** A managed IPv6 address assignment was removed from this peer */ + ZTS_EVENT_ADDR_REMOVED_IP6 = 263, - /** The node's secret key (identity) */ - ZTS_EVENT_STORE_IDENTITY_SECRET = 270, - /** The node's public key (identity) */ - ZTS_EVENT_STORE_IDENTITY_PUBLIC = 271, - /** The node has received an updated planet config */ - ZTS_EVENT_STORE_PLANET = 272, - /** New reachability hints and peer configuration */ - ZTS_EVENT_STORE_PEER = 273, - /** New network config */ - ZTS_EVENT_STORE_NETWORK = 274 + /** The node's secret key (identity) */ + ZTS_EVENT_STORE_IDENTITY_SECRET = 270, + /** The node's public key (identity) */ + ZTS_EVENT_STORE_IDENTITY_PUBLIC = 271, + /** The node has received an updated planet config */ + ZTS_EVENT_STORE_PLANET = 272, + /** New reachability hints and peer configuration */ + ZTS_EVENT_STORE_PEER = 273, + /** New network config */ + ZTS_EVENT_STORE_NETWORK = 274 } zts_event_t; //----------------------------------------------------------------------------// @@ -200,90 +200,90 @@ typedef enum { extern int zts_errno; typedef enum { - /** Operation not permitted (`zts_errno` value) */ - ZTS_EPERM = 1, - /** No such file or directory */ - ZTS_ENOENT = 2, - /** No such process */ - ZTS_ESRCH = 3, - /** Interrupted system call */ - ZTS_EINTR = 4, - /** I/O error */ - ZTS_EIO = 5, - /** No such device or address */ - ZTS_ENXIO = 6, - /** Bad file number */ - ZTS_EBADF = 9, - /** Try again */ - ZTS_EAGAIN = 11, - /** Operation would block */ - ZTS_EWOULDBLOCK = ZTS_EAGAIN, - /** Out of memory */ - ZTS_ENOMEM = 12, - /** Permission denied */ - ZTS_EACCES = 13, - /** Bad address */ - ZTS_EFAULT = 14, - /** Device or resource busy */ - ZTS_EBUSY = 16, - /** File exists */ - ZTS_EEXIST = 17, - /** No such device */ - ZTS_ENODEV = 19, - /** Invalid argument */ - ZTS_EINVAL = 22, - /** File table overflow */ - ZTS_ENFILE = 23, - /** Too many open files */ - ZTS_EMFILE = 24, - /** Function not implemented */ - ZTS_ENOSYS = 38, - /** Socket operation on non-socket */ - ZTS_ENOTSOCK = 88, - /** Destination address required */ - ZTS_EDESTADDRREQ = 89, - /** Message too long */ - ZTS_EMSGSIZE = 90, - /** Protocol wrong type for socket */ - ZTS_EPROTOTYPE = 91, - /** Protocol not available */ - ZTS_ENOPROTOOPT = 92, - /** Protocol not supported */ - ZTS_EPROTONOSUPPORT = 93, - /** Socket type not supported */ - ZTS_ESOCKTNOSUPPORT = 94, - /** Operation not supported on transport endpoint */ - ZTS_EOPNOTSUPP = 95, - /** Protocol family not supported */ - ZTS_EPFNOSUPPORT = 96, - /** Address family not supported by protocol */ - ZTS_EAFNOSUPPORT = 97, - /** Address already in use */ - ZTS_EADDRINUSE = 98, - /** Cannot assign requested address */ - ZTS_EADDRNOTAVAIL = 99, - /** Network is down */ - ZTS_ENETDOWN = 100, - /** Network is unreachable */ - ZTS_ENETUNREACH = 101, - /** Software caused connection abort */ - ZTS_ECONNABORTED = 103, - /** Connection reset by peer */ - ZTS_ECONNRESET = 104, - /** No buffer space available */ - ZTS_ENOBUFS = 105, - /** Transport endpoint is already connected */ - ZTS_EISCONN = 106, - /** Transport endpoint is not connected */ - ZTS_ENOTCONN = 107, - /** Connection timed out */ - ZTS_ETIMEDOUT = 110, - /** No route to host */ - ZTS_EHOSTUNREACH = 113, - /** Operation already in progress */ - ZTS_EALREADY = 114, - /** Operation now in progress */ - ZTS_EINPROGRESS = 115 + /** Operation not permitted (`zts_errno` value) */ + ZTS_EPERM = 1, + /** No such file or directory */ + ZTS_ENOENT = 2, + /** No such process */ + ZTS_ESRCH = 3, + /** Interrupted system call */ + ZTS_EINTR = 4, + /** I/O error */ + ZTS_EIO = 5, + /** No such device or address */ + ZTS_ENXIO = 6, + /** Bad file number */ + ZTS_EBADF = 9, + /** Try again */ + ZTS_EAGAIN = 11, + /** Operation would block */ + ZTS_EWOULDBLOCK = ZTS_EAGAIN, + /** Out of memory */ + ZTS_ENOMEM = 12, + /** Permission denied */ + ZTS_EACCES = 13, + /** Bad address */ + ZTS_EFAULT = 14, + /** Device or resource busy */ + ZTS_EBUSY = 16, + /** File exists */ + ZTS_EEXIST = 17, + /** No such device */ + ZTS_ENODEV = 19, + /** Invalid argument */ + ZTS_EINVAL = 22, + /** File table overflow */ + ZTS_ENFILE = 23, + /** Too many open files */ + ZTS_EMFILE = 24, + /** Function not implemented */ + ZTS_ENOSYS = 38, + /** Socket operation on non-socket */ + ZTS_ENOTSOCK = 88, + /** Destination address required */ + ZTS_EDESTADDRREQ = 89, + /** Message too long */ + ZTS_EMSGSIZE = 90, + /** Protocol wrong type for socket */ + ZTS_EPROTOTYPE = 91, + /** Protocol not available */ + ZTS_ENOPROTOOPT = 92, + /** Protocol not supported */ + ZTS_EPROTONOSUPPORT = 93, + /** Socket type not supported */ + ZTS_ESOCKTNOSUPPORT = 94, + /** Operation not supported on transport endpoint */ + ZTS_EOPNOTSUPP = 95, + /** Protocol family not supported */ + ZTS_EPFNOSUPPORT = 96, + /** Address family not supported by protocol */ + ZTS_EAFNOSUPPORT = 97, + /** Address already in use */ + ZTS_EADDRINUSE = 98, + /** Cannot assign requested address */ + ZTS_EADDRNOTAVAIL = 99, + /** Network is down */ + ZTS_ENETDOWN = 100, + /** Network is unreachable */ + ZTS_ENETUNREACH = 101, + /** Software caused connection abort */ + ZTS_ECONNABORTED = 103, + /** Connection reset by peer */ + ZTS_ECONNRESET = 104, + /** No buffer space available */ + ZTS_ENOBUFS = 105, + /** Transport endpoint is already connected */ + ZTS_EISCONN = 106, + /** Transport endpoint is not connected */ + ZTS_ENOTCONN = 107, + /** Connection timed out */ + ZTS_ETIMEDOUT = 110, + /** No route to host */ + ZTS_EHOSTUNREACH = 113, + /** Operation already in progress */ + ZTS_EALREADY = 114, + /** Operation now in progress */ + ZTS_EINPROGRESS = 115 } zts_errno_t; //----------------------------------------------------------------------------// @@ -484,63 +484,63 @@ typedef uint8_t zts_sa_family_t; struct zts_in_addr { #if defined(_WIN32) - zts_in_addr_t S_addr; + zts_in_addr_t S_addr; #else - // A definition in winsock may conflict with s_addr - zts_in_addr_t s_addr; + // A definition in winsock may conflict with s_addr + zts_in_addr_t s_addr; #endif }; struct zts_in6_addr { - union un { - uint32_t u32_addr[4]; - uint8_t u8_addr[16]; - } un; - //#define s6_addr un.u8_addr + union un { + uint32_t u32_addr[4]; + uint8_t u8_addr[16]; + } un; + //#define s6_addr un.u8_addr }; /** * Address structure to specify an IPv4 endpoint */ struct zts_sockaddr_in { - uint8_t sin_len; - zts_sa_family_t sin_family; - zts_in_port_t sin_port; - struct zts_in_addr sin_addr; + uint8_t sin_len; + zts_sa_family_t sin_family; + zts_in_port_t sin_port; + struct zts_in_addr sin_addr; #define SIN_ZERO_LEN 8 - char sin_zero[SIN_ZERO_LEN]; + char sin_zero[SIN_ZERO_LEN]; }; /** * Address structure to specify an IPv6 endpoint */ struct zts_sockaddr_in6 { - uint8_t sin6_len; // length of this structure - zts_sa_family_t sin6_family; // ZTS_AF_INET6 - zts_in_port_t sin6_port; // Transport layer port # - uint32_t sin6_flowinfo; // IPv6 flow information - struct zts_in6_addr sin6_addr; // IPv6 address - uint32_t sin6_scope_id; // Set of interfaces for scope + uint8_t sin6_len; // length of this structure + zts_sa_family_t sin6_family; // ZTS_AF_INET6 + zts_in_port_t sin6_port; // Transport layer port # + uint32_t sin6_flowinfo; // IPv6 flow information + struct zts_in6_addr sin6_addr; // IPv6 address + uint32_t sin6_scope_id; // Set of interfaces for scope }; /** * Pointers to socket address structures are often cast to this type */ struct zts_sockaddr { - uint8_t sa_len; - zts_sa_family_t sa_family; - char sa_data[14]; + uint8_t sa_len; + zts_sa_family_t sa_family; + char sa_data[14]; }; /** * Address structure large enough to hold IPv4 and IPv6 addresses */ struct zts_sockaddr_storage { - uint8_t s2_len; - zts_sa_family_t ss_family; - char s2_data1[2]; - uint32_t s2_data2[3]; - uint32_t s2_data3[3]; + uint8_t s2_len; + zts_sa_family_t ss_family; + char s2_data1[2]; + uint32_t s2_data2[3]; + uint32_t s2_data3[3]; }; //----------------------------------------------------------------------------// @@ -551,373 +551,373 @@ struct zts_sockaddr_storage { * Runtime details about the current node */ typedef struct { - /** - * Node ID - */ - uint64_t node_id; + /** + * Node ID + */ + uint64_t node_id; - /** - * Port used by ZeroTier to send and receive traffic - */ - uint16_t port_primary; + /** + * Port used by ZeroTier to send and receive traffic + */ + uint16_t port_primary; - /** - * Port used by ZeroTier to send and receive traffic - */ - uint16_t port_secondary; + /** + * Port used by ZeroTier to send and receive traffic + */ + uint16_t port_secondary; - /** - * Port used by ZeroTier to send and receive traffic - */ - uint16_t port_tertiary; + /** + * Port used by ZeroTier to send and receive traffic + */ + uint16_t port_tertiary; - /** - * ZT Major version - */ - uint8_t ver_major; + /** + * ZT Major version + */ + uint8_t ver_major; - /** - * ZT Minor version - */ - uint8_t ver_minor; + /** + * ZT Minor version + */ + uint8_t ver_minor; - /** - * ZT Patch revision - */ - uint8_t ver_rev; + /** + * ZT Patch revision + */ + uint8_t ver_rev; } zts_node_info_t; /** * Details about an assigned address that was added or removed */ typedef struct { - uint64_t net_id; - struct zts_sockaddr_storage addr; + uint64_t net_id; + struct zts_sockaddr_storage addr; } zts_addr_info_t; /** * Virtual network status codes */ typedef enum { - /** - * Waiting for network configuration (also means revision == 0) - */ - ZTS_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0, + /** + * Waiting for network configuration (also means revision == 0) + */ + ZTS_NETWORK_STATUS_REQUESTING_CONFIGURATION = 0, - /** - * Configuration received and we are authorized - */ - ZTS_NETWORK_STATUS_OK = 1, + /** + * Configuration received and we are authorized + */ + ZTS_NETWORK_STATUS_OK = 1, - /** - * Netconf master told us 'nope' - */ - ZTS_NETWORK_STATUS_ACCESS_DENIED = 2, + /** + * Netconf master told us 'nope' + */ + ZTS_NETWORK_STATUS_ACCESS_DENIED = 2, - /** - * Netconf master exists, but this virtual network does not - */ - ZTS_NETWORK_STATUS_NOT_FOUND = 3, + /** + * Netconf master exists, but this virtual network does not + */ + ZTS_NETWORK_STATUS_NOT_FOUND = 3, - /** - * Initialization of network failed or other internal error - */ - ZTS_NETWORK_STATUS_PORT_ERROR = 4, + /** + * Initialization of network failed or other internal error + */ + ZTS_NETWORK_STATUS_PORT_ERROR = 4, - /** - * ZeroTier core version too old - */ - ZTS_NETWORK_STATUS_CLIENT_TOO_OLD = 5 + /** + * ZeroTier core version too old + */ + ZTS_NETWORK_STATUS_CLIENT_TOO_OLD = 5 } zts_network_status_t; /** * Virtual network type codes */ typedef enum { - /** - * Private networks are authorized via certificates of membership - */ - ZTS_NETWORK_TYPE_PRIVATE = 0, + /** + * Private networks are authorized via certificates of membership + */ + ZTS_NETWORK_TYPE_PRIVATE = 0, - /** - * Public networks have no access control -- they'll always be AUTHORIZED - */ - ZTS_NETWORK_TYPE_PUBLIC = 1 + /** + * Public networks have no access control -- they'll always be AUTHORIZED + */ + ZTS_NETWORK_TYPE_PUBLIC = 1 } zts_net_info_type_t; /** * A route to be pushed on a virtual network */ typedef struct { - /** - * Target network / netmask bits (in port field) or NULL or 0.0.0.0/0 - * for default - */ - struct zts_sockaddr_storage target; + /** + * Target network / netmask bits (in port field) or NULL or 0.0.0.0/0 + * for default + */ + struct zts_sockaddr_storage target; - /** - * Gateway IP address (port ignored) or NULL (family == 0) for LAN-local - * (no gateway) - */ - struct zts_sockaddr_storage via; + /** + * Gateway IP address (port ignored) or NULL (family == 0) for LAN-local + * (no gateway) + */ + struct zts_sockaddr_storage via; - /** - * Route flags - */ - uint16_t flags; + /** + * Route flags + */ + uint16_t flags; - /** - * Route metric (not currently used) - */ - uint16_t metric; + /** + * Route metric (not currently used) + */ + uint16_t metric; } zts_route_info_t; /** * An Ethernet multicast group */ typedef struct { - /** - * MAC address (least significant 48 bits) - */ - uint64_t mac; + /** + * MAC address (least significant 48 bits) + */ + uint64_t mac; - /** - * Additional distinguishing information (usually zero) - */ - unsigned long adi; + /** + * Additional distinguishing information (usually zero) + */ + unsigned long adi; } zts_multicast_group_t; /** * The peer's trust hierarchy role */ typedef enum { - /** - * Ordinary node - */ - ZTS_PEER_ROLE_LEAF = 0, - /** - * Moon root - */ - ZTS_PEER_ROLE_MOON = 1, - /** - * Planetary root - */ - ZTS_PEER_ROLE_PLANET = 2 + /** + * Ordinary node + */ + ZTS_PEER_ROLE_LEAF = 0, + /** + * Moon root + */ + ZTS_PEER_ROLE_MOON = 1, + /** + * Planetary root + */ + ZTS_PEER_ROLE_PLANET = 2 } zts_peer_role_t; /** * Virtual network configuration */ typedef struct { - /** - * 64-bit ZeroTier network ID - */ - uint64_t net_id; + /** + * 64-bit ZeroTier network ID + */ + uint64_t net_id; - /** - * Ethernet MAC (48 bits) that should be assigned to port - */ - uint64_t mac; + /** + * Ethernet MAC (48 bits) that should be assigned to port + */ + uint64_t mac; - /** - * Network name (from network configuration master) - */ - char name[ZTS_MAX_NETWORK_SHORT_NAME_LENGTH + 1]; + /** + * Network name (from network configuration master) + */ + char name[ZTS_MAX_NETWORK_SHORT_NAME_LENGTH + 1]; - /** - * Network configuration request status - */ - zts_network_status_t status; + /** + * Network configuration request status + */ + zts_network_status_t status; - /** - * Network type - */ - zts_net_info_type_t type; + /** + * Network type + */ + zts_net_info_type_t type; - /** - * Maximum interface MTU - */ - unsigned int mtu; + /** + * Maximum interface MTU + */ + unsigned int mtu; - /** - * If nonzero, the network this port belongs to indicates DHCP availability - * - * This is a suggestion. The underlying implementation is free to ignore it - * for security or other reasons. This is simply a netconf parameter that - * means 'DHCP is available on this network.' - */ - int dhcp; + /** + * If nonzero, the network this port belongs to indicates DHCP availability + * + * This is a suggestion. The underlying implementation is free to ignore it + * for security or other reasons. This is simply a netconf parameter that + * means 'DHCP is available on this network.' + */ + int dhcp; - /** - * If nonzero, this port is allowed to bridge to other networks - * - * This is informational. If this is false (0), bridged packets will simply - * be dropped and bridging won't work. - */ - int bridge; + /** + * If nonzero, this port is allowed to bridge to other networks + * + * This is informational. If this is false (0), bridged packets will simply + * be dropped and bridging won't work. + */ + int bridge; - /** - * If nonzero, this network supports and allows broadcast - * (ff:ff:ff:ff:ff:ff) traffic - */ - int broadcast_enabled; + /** + * If nonzero, this network supports and allows broadcast + * (ff:ff:ff:ff:ff:ff) traffic + */ + int broadcast_enabled; - /** - * If the network is in PORT_ERROR state, this is the (negative) error code - * most recently reported - */ - int port_error; + /** + * If the network is in PORT_ERROR state, this is the (negative) error code + * most recently reported + */ + int port_error; - /** - * Revision number as reported by controller or 0 if still waiting for - * config - */ - unsigned long netconf_rev; + /** + * Revision number as reported by controller or 0 if still waiting for + * config + */ + unsigned long netconf_rev; - /** - * Number of assigned addresses - */ - unsigned int assigned_addr_count; + /** + * Number of assigned addresses + */ + unsigned int assigned_addr_count; - /** - * ZeroTier-assigned addresses (in sockaddr_storage structures) - * - * For IP, the port number of the sockaddr_XX structure contains the number - * of bits in the address netmask. Only the IP address and port are used. - * Other fields like interface number can be ignored. - * - * This is only used for ZeroTier-managed address assignments sent by the - * virtual network's configuration master. - */ - struct zts_sockaddr_storage assigned_addrs[ZTS_MAX_ASSIGNED_ADDRESSES]; + /** + * ZeroTier-assigned addresses (in sockaddr_storage structures) + * + * For IP, the port number of the sockaddr_XX structure contains the number + * of bits in the address netmask. Only the IP address and port are used. + * Other fields like interface number can be ignored. + * + * This is only used for ZeroTier-managed address assignments sent by the + * virtual network's configuration master. + */ + struct zts_sockaddr_storage assigned_addrs[ZTS_MAX_ASSIGNED_ADDRESSES]; - /** - * Number of ZT-pushed routes - */ - unsigned int route_count; + /** + * Number of ZT-pushed routes + */ + unsigned int route_count; - /** - * Routes (excluding those implied by assigned addresses and their masks) - */ - zts_route_info_t routes[ZTS_MAX_NETWORK_ROUTES]; + /** + * Routes (excluding those implied by assigned addresses and their masks) + */ + zts_route_info_t routes[ZTS_MAX_NETWORK_ROUTES]; - /** - * Number of multicast groups subscribed - */ - unsigned int multicast_sub_count; + /** + * Number of multicast groups subscribed + */ + unsigned int multicast_sub_count; - /** - * Multicast groups to which this network's device is subscribed - */ - struct { - uint64_t mac; /* MAC in lower 48 bits */ - uint32_t adi; /* Additional distinguishing information, usually zero - except for IPv4 ARP groups */ - } multicast_subs[ZTS_MAX_MULTICAST_SUBSCRIPTIONS]; + /** + * Multicast groups to which this network's device is subscribed + */ + struct { + uint64_t mac; /* MAC in lower 48 bits */ + uint32_t adi; /* Additional distinguishing information, usually zero + except for IPv4 ARP groups */ + } multicast_subs[ZTS_MAX_MULTICAST_SUBSCRIPTIONS]; } zts_net_info_t; /** * Physical network path to a peer */ typedef struct { - /** - * Address of endpoint - */ - struct zts_sockaddr_storage address; + /** + * Address of endpoint + */ + struct zts_sockaddr_storage address; - /** - * Time of last send in milliseconds or 0 for never - */ - uint64_t last_tx; + /** + * Time of last send in milliseconds or 0 for never + */ + uint64_t last_tx; - /** - * Time of last receive in milliseconds or 0 for never - */ - uint64_t last_rx; + /** + * Time of last receive in milliseconds or 0 for never + */ + uint64_t last_rx; - /** - * Is this a trusted path? If so this will be its nonzero ID. - */ - uint64_t trusted_path_id; + /** + * Is this a trusted path? If so this will be its nonzero ID. + */ + uint64_t trusted_path_id; - /** - * One-way latency - */ - float latency; + /** + * One-way latency + */ + float latency; - float unused_0; - float unused_1; - float unused_2; - float unused_3; - float unused_4; - uint64_t unused_5; - uint64_t unused_6; - float unused_7; + float unused_0; + float unused_1; + float unused_2; + float unused_3; + float unused_4; + uint64_t unused_5; + uint64_t unused_6; + float unused_7; - /** - * Name of physical interface (for monitoring) - */ - char* ifname; + /** + * Name of physical interface (for monitoring) + */ + char* ifname; - /** - * Is path expired? - */ - int expired; + /** + * Is path expired? + */ + int expired; - /** - * Is path preferred? - */ - int preferred; + /** + * Is path preferred? + */ + int preferred; } zts_path_t; /** * Peer status result buffer */ typedef struct { - /** - * ZeroTier address (40 bits) - */ - uint64_t address; + /** + * ZeroTier address (40 bits) + */ + uint64_t address; - /** - * Remote major version or -1 if not known - */ - int ver_major; + /** + * Remote major version or -1 if not known + */ + int ver_major; - /** - * Remote minor version or -1 if not known - */ - int ver_minor; + /** + * Remote minor version or -1 if not known + */ + int ver_minor; - /** - * Remote revision or -1 if not known - */ - int ver_rev; + /** + * Remote revision or -1 if not known + */ + int ver_rev; - /** - * Last measured latency in milliseconds or -1 if unknown - */ - int latency; + /** + * Last measured latency in milliseconds or -1 if unknown + */ + int latency; - /** - * What trust hierarchy role does this device have? - */ - zts_peer_role_t role; + /** + * What trust hierarchy role does this device have? + */ + zts_peer_role_t role; - /** - * Number of paths (size of paths[]) - */ - unsigned int path_count; + /** + * Number of paths (size of paths[]) + */ + unsigned int path_count; - /** - * Whether this peer was ever reachable via an aggregate link - */ - int unused_0; + /** + * Whether this peer was ever reachable via an aggregate link + */ + int unused_0; - /** - * Known network paths to peer - */ - zts_path_t paths[ZTS_MAX_PEER_NETWORK_PATHS]; + /** + * Known network paths to peer + */ + zts_path_t paths[ZTS_MAX_PEER_NETWORK_PATHS]; } zts_peer_info_t; #define ZTS_MAX_NUM_ROOTS 16 @@ -927,8 +927,8 @@ typedef struct { * Structure used to specify a root topology (aka a world) */ typedef struct { - char* public_id_str[ZTS_MAX_NUM_ROOTS]; - char* endpoint_ip_str[ZTS_MAX_NUM_ROOTS][ZTS_MAX_ENDPOINTS_PER_ROOT]; + char* public_id_str[ZTS_MAX_NUM_ROOTS]; + char* endpoint_ip_str[ZTS_MAX_NUM_ROOTS][ZTS_MAX_ENDPOINTS_PER_ROOT]; } zts_world_t; /** @@ -936,62 +936,62 @@ typedef struct { * interface (netif) to a user application. */ typedef struct { - /** - * The virtual network that this interface was created for - */ - uint64_t net_id; + /** + * The virtual network that this interface was created for + */ + uint64_t net_id; - /** - * The hardware address assigned to this interface - */ - uint64_t mac; + /** + * The hardware address assigned to this interface + */ + uint64_t mac; - /** - * The MTU for this interface - */ - int mtu; + /** + * The MTU for this interface + */ + int mtu; } zts_netif_info_t; /** * Callback message */ typedef struct { - /** - * Event identifier - */ - int16_t event_code; - /** - * Node status - */ - zts_node_info_t* node; - /** - * Network information - */ - zts_net_info_t* network; - /** - * Netif status - */ - zts_netif_info_t* netif; - /** - * Managed routes - */ - zts_route_info_t* route; - /** - * Peer info - */ - zts_peer_info_t* peer; - /** - * Assigned address - */ - zts_addr_info_t* addr; - /** - * Binary data (identities, planets, network configs, peer hints, etc) - */ - void* cache; - /** - * Length of data message or structure - */ - int len; + /** + * Event identifier + */ + int16_t event_code; + /** + * Node status + */ + zts_node_info_t* node; + /** + * Network information + */ + zts_net_info_t* network; + /** + * Netif status + */ + zts_netif_info_t* netif; + /** + * Managed routes + */ + zts_route_info_t* route; + /** + * Peer info + */ + zts_peer_info_t* peer; + /** + * Assigned address + */ + zts_addr_info_t* addr; + /** + * Binary data (identities, planets, network configs, peer hints, etc) + */ + void* cache; + /** + * Length of data message or structure + */ + int len; } zts_event_msg_t; //----------------------------------------------------------------------------// @@ -1007,11 +1007,11 @@ typedef struct { */ class PythonDirectorCallbackClass { public: - /** - * Called by native code on event. Implemented in Python - */ - virtual void on_zerotier_event(zts_event_msg_t* msg); - virtual ~PythonDirectorCallbackClass() {}; + /** + * Called by native code on event. Implemented in Python + */ + virtual void on_zerotier_event(zts_event_msg_t* msg); + virtual ~PythonDirectorCallbackClass() {}; }; extern PythonDirectorCallbackClass* _userEventCallback; @@ -1716,86 +1716,86 @@ ZTS_API int ZTCALL zts_moon_deorbit(uint64_t moon_world_id); * Structure containing counters for various protocol statistics */ typedef struct { - /** Number of link packets transmitted */ - uint32_t link_tx; - /** Number of link packets received */ - uint32_t link_rx; - /** Number of link packets dropped */ - uint32_t link_drop; - /** Aggregate number of link-level errors */ - uint32_t link_err; + /** Number of link packets transmitted */ + uint32_t link_tx; + /** Number of link packets received */ + uint32_t link_rx; + /** Number of link packets dropped */ + uint32_t link_drop; + /** Aggregate number of link-level errors */ + uint32_t link_err; - /** Number of etharp packets transmitted */ - uint32_t etharp_tx; - /** Number of etharp packets received */ - uint32_t etharp_rx; - /** Number of etharp packets dropped */ - uint32_t etharp_drop; - /** Aggregate number of etharp errors */ - uint32_t etharp_err; + /** Number of etharp packets transmitted */ + uint32_t etharp_tx; + /** Number of etharp packets received */ + uint32_t etharp_rx; + /** Number of etharp packets dropped */ + uint32_t etharp_drop; + /** Aggregate number of etharp errors */ + uint32_t etharp_err; - /** Number of IPv4 packets transmitted */ - uint32_t ip4_tx; - /** Number of IPv4 packets received */ - uint32_t ip4_rx; - /** Number of IPv4 packets dropped */ - uint32_t ip4_drop; - /** Aggregate number of IPv4 errors */ - uint32_t ip4_err; + /** Number of IPv4 packets transmitted */ + uint32_t ip4_tx; + /** Number of IPv4 packets received */ + uint32_t ip4_rx; + /** Number of IPv4 packets dropped */ + uint32_t ip4_drop; + /** Aggregate number of IPv4 errors */ + uint32_t ip4_err; - /** Number of IPv6 packets transmitted */ - uint32_t ip6_tx; - /** Number of IPv6 packets received */ - uint32_t ip6_rx; - /** Number of IPv6 packets dropped */ - uint32_t ip6_drop; - /** Aggregate number of IPv6 errors */ - uint32_t ip6_err; + /** Number of IPv6 packets transmitted */ + uint32_t ip6_tx; + /** Number of IPv6 packets received */ + uint32_t ip6_rx; + /** Number of IPv6 packets dropped */ + uint32_t ip6_drop; + /** Aggregate number of IPv6 errors */ + uint32_t ip6_err; - /** Number of ICMPv4 packets transmitted */ - uint32_t icmp4_tx; - /** Number of ICMPv4 packets received */ - uint32_t icmp4_rx; - /** Number of ICMPv4 packets dropped */ - uint32_t icmp4_drop; - /** Aggregate number of ICMPv4 errors */ - uint32_t icmp4_err; + /** Number of ICMPv4 packets transmitted */ + uint32_t icmp4_tx; + /** Number of ICMPv4 packets received */ + uint32_t icmp4_rx; + /** Number of ICMPv4 packets dropped */ + uint32_t icmp4_drop; + /** Aggregate number of ICMPv4 errors */ + uint32_t icmp4_err; - /** Number of ICMPv6 packets transmitted */ - uint32_t icmp6_tx; - /** Number of ICMPv6 packets received */ - uint32_t icmp6_rx; - /** Number of ICMPv6 packets dropped */ - uint32_t icmp6_drop; - /** Aggregate number of ICMPv6 errors */ - uint32_t icmp6_err; + /** Number of ICMPv6 packets transmitted */ + uint32_t icmp6_tx; + /** Number of ICMPv6 packets received */ + uint32_t icmp6_rx; + /** Number of ICMPv6 packets dropped */ + uint32_t icmp6_drop; + /** Aggregate number of ICMPv6 errors */ + uint32_t icmp6_err; - /** Number of UDP packets transmitted */ - uint32_t udp_tx; - /** Number of UDP packets received */ - uint32_t udp_rx; - /** Number of UDP packets dropped */ - uint32_t udp_drop; - /** Aggregate number of UDP errors */ - uint32_t udp_err; + /** Number of UDP packets transmitted */ + uint32_t udp_tx; + /** Number of UDP packets received */ + uint32_t udp_rx; + /** Number of UDP packets dropped */ + uint32_t udp_drop; + /** Aggregate number of UDP errors */ + uint32_t udp_err; - /** Number of TCP packets transmitted */ - uint32_t tcp_tx; - /** Number of TCP packets received */ - uint32_t tcp_rx; - /** Number of TCP packets dropped */ - uint32_t tcp_drop; - /** Aggregate number of TCP errors */ - uint32_t tcp_err; + /** Number of TCP packets transmitted */ + uint32_t tcp_tx; + /** Number of TCP packets received */ + uint32_t tcp_rx; + /** Number of TCP packets dropped */ + uint32_t tcp_drop; + /** Aggregate number of TCP errors */ + uint32_t tcp_err; - /** Number of ND6 packets transmitted */ - uint32_t nd6_tx; - /** Number of ND6 packets received */ - uint32_t nd6_rx; - /** Number of ND6 packets dropped */ - uint32_t nd6_drop; - /** Aggregate number of ND6 errors */ - uint32_t nd6_err; + /** Number of ND6 packets transmitted */ + uint32_t nd6_tx; + /** Number of ND6 packets received */ + uint32_t nd6_rx; + /** Number of ND6 packets dropped */ + uint32_t nd6_drop; + /** Aggregate number of ND6 errors */ + uint32_t nd6_err; } zts_stats_counter_t; /** @@ -1986,8 +1986,8 @@ ZTS_API int ZTCALL zts_simple_udp_client(const char* remote_ipstr); * Structure used for manipulating linger option. */ struct zts_linger { - int l_onoff; // option on/off - int l_linger; // linger time in seconds + int l_onoff; // option on/off + int l_linger; // linger time in seconds }; #define ZTS_SO_DONTLINGER ((int)(~ZTS_SO_LINGER)) @@ -2016,11 +2016,11 @@ struct zts_linger { #define ZTS_TCP_KEEPCNT 0x0005 // IPPROTO_IPV6 options #define ZTS_IPV6_CHECKSUM \ - 0x0007 /* RFC3542: calculate and insert the ICMPv6 checksum for raw \ - sockets. */ + 0x0007 /* RFC3542: calculate and insert the ICMPv6 checksum for raw \ + sockets. */ #define ZTS_IPV6_V6ONLY \ - 0x001b /* RFC3493: boolean control to restrict ZTS_AF_INET6 sockets to \ - IPv6 communications only. */ + 0x001b /* RFC3493: boolean control to restrict ZTS_AF_INET6 sockets to \ + IPv6 communications only. */ // UDPLITE options #define ZTS_UDPLITE_SEND_CSCOV 0x01 /* sender checksum coverage */ #define ZTS_UDPLITE_RECV_CSCOV 0x02 /* minimal receiver checksum coverage */ @@ -2034,13 +2034,13 @@ struct zts_linger { #define ZTS_IP_DROP_MEMBERSHIP 4 typedef struct zts_ip_mreq { - struct zts_in_addr imr_multiaddr; /* IP multicast address of group */ - struct zts_in_addr imr_interface; /* local IP address of interface */ + struct zts_in_addr imr_multiaddr; /* IP multicast address of group */ + struct zts_in_addr imr_interface; /* local IP address of interface */ } zts_ip_mreq; struct zts_in_pktinfo { - unsigned int ipi_ifindex; /* Interface index */ - struct zts_in_addr ipi_addr; /* Destination (from header) address */ + unsigned int ipi_ifindex; /* Interface index */ + struct zts_in_addr ipi_addr; /* Destination (from header) address */ }; #define ZTS_IPV6_JOIN_GROUP 12 @@ -2049,8 +2049,8 @@ struct zts_in_pktinfo { #define ZTS_IPV6_DROP_MEMBERSHIP ZTS_IPV6_LEAVE_GROUP typedef struct zts_ipv6_mreq { - struct zts_in6_addr ipv6mr_multiaddr; /* IPv6 multicast addr */ - unsigned int ipv6mr_interface; /* interface index, or 0 */ + struct zts_in6_addr ipv6mr_multiaddr; /* IPv6 multicast addr */ + unsigned int ipv6mr_interface; /* interface index, or 0 */ } zts_ipv6_mreq; /* @@ -2164,19 +2164,19 @@ ZTS_API int ZTCALL zts_close(int fd); // Make FD_SETSIZE match NUM_SOCKETS in socket.c #define ZTS_FD_SETSIZE MEMP_NUM_NETCONN #define ZTS_FDSETSAFESET(n, code) \ - do { \ - if (((n)-LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n)-LWIP_SOCKET_OFFSET) >= 0)) { \ - code; \ - } \ - } while (0) + do { \ + if (((n)-LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n)-LWIP_SOCKET_OFFSET) >= 0)) { \ + code; \ + } \ + } while (0) #define ZTS_FDSETSAFEGET(n, code) \ - (((n)-LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n)-LWIP_SOCKET_OFFSET) >= 0) ? (code) : 0) + (((n)-LWIP_SOCKET_OFFSET < MEMP_NUM_NETCONN) && (((int)(n)-LWIP_SOCKET_OFFSET) >= 0) ? (code) : 0) #define ZTS_FD_SET(n, p) \ - ZTS_FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] |= (1 << (((n)-LWIP_SOCKET_OFFSET) & 7))) + ZTS_FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] |= (1 << (((n)-LWIP_SOCKET_OFFSET) & 7))) #define ZTS_FD_CLR(n, p) \ - ZTS_FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] &= ~(1 << (((n)-LWIP_SOCKET_OFFSET) & 7))) + ZTS_FDSETSAFESET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] &= ~(1 << (((n)-LWIP_SOCKET_OFFSET) & 7))) #define ZTS_FD_ISSET(n, p) \ - ZTS_FDSETSAFEGET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] & (1 << (((n)-LWIP_SOCKET_OFFSET) & 7))) + ZTS_FDSETSAFEGET(n, (p)->fd_bits[((n)-LWIP_SOCKET_OFFSET) / 8] & (1 << (((n)-LWIP_SOCKET_OFFSET) & 7))) #define ZTS_FD_ZERO(p) memset((void*)(p), 0, sizeof(*(p))) #elif LWIP_SOCKET_OFFSET @@ -2186,12 +2186,12 @@ ZTS_API int ZTCALL zts_close(int fd); #endif // FD_SET typedef struct zts_fd_set { - unsigned char fd_bits[(ZTS_FD_SETSIZE + 7) / 8]; + unsigned char fd_bits[(ZTS_FD_SETSIZE + 7) / 8]; } zts_fd_set; typedef struct zts_timeval { - long tv_sec; /* seconds */ - long tv_usec; /* and microseconds */ + long tv_sec; /* seconds */ + long tv_usec; /* and microseconds */ } zts_timeval; /** @@ -2246,9 +2246,9 @@ ZTS_API int ZTCALL zts_fcntl(int fd, int cmd, int flags); typedef unsigned int zts_nfds_t; struct zts_pollfd { - int fd; - short events; - short revents; + int fd; + short events; + short revents; }; /** @@ -2302,19 +2302,19 @@ ZTS_API ssize_t ZTCALL zts_sendto(int fd, const void* buf, size_t len, int flags, const struct zts_sockaddr* addr, zts_socklen_t addrlen); struct zts_iovec { - void* iov_base; - size_t iov_len; + void* iov_base; + size_t iov_len; }; /* */ struct zts_msghdr { - void* msg_name; - zts_socklen_t msg_namelen; - struct zts_iovec* msg_iov; - int msg_iovlen; - void* msg_control; - zts_socklen_t msg_controllen; - int msg_flags; + void* msg_name; + zts_socklen_t msg_namelen; + struct zts_iovec* msg_iov; + int msg_iovlen; + void* msg_control; + zts_socklen_t msg_controllen; + int msg_flags; }; /* struct msghdr->msg_flags bit field values */ @@ -2642,14 +2642,14 @@ ZTS_API int ZTCALL zts_simple_get_keepalive(int fd); //----------------------------------------------------------------------------// struct zts_hostent { - char* h_name; /* Official name of the host. */ - char** h_aliases; /* A pointer to an array of pointers to alternative host - names, terminated by a null pointer. */ - int h_addrtype; /* Address type. */ - int h_length; /* The length, in bytes, of the address. */ - char** h_addr_list; /* A pointer to an array of pointers to network - addresses (in network byte order) for the host, - terminated by a null pointer. */ + char* h_name; /* Official name of the host. */ + char** h_aliases; /* A pointer to an array of pointers to alternative host + names, terminated by a null pointer. */ + int h_addrtype; /* Address type. */ + int h_length; /* The length, in bytes, of the address. */ + char** h_addr_list; /* A pointer to an array of pointers to network + addresses (in network byte order) for the host, + terminated by a null pointer. */ #define h_addr h_addr_list[0] /* for backward compatibility */ }; @@ -2662,15 +2662,15 @@ struct zts_hostent { struct zts_hostent* zts_gethostbyname(const char* name); struct zts_ip4_addr { - uint32_t addr; + uint32_t addr; }; /** This is the aligned version of ip6_addr_t, used as local variable, on the stack, etc. */ struct zts_ip6_addr { - uint32_t addr[4]; + uint32_t addr[4]; #if LWIP_IPV6_SCOPES - uint8_t zone; + uint8_t zone; #endif /* LWIP_IPV6_SCOPES */ }; @@ -2679,11 +2679,11 @@ struct zts_ip6_addr { * ATTENTION: watch out for its size when adding IPv6 address scope! */ typedef struct zts_ip_addr { - union { - struct zts_ip6_addr ip6; - struct zts_ip4_addr ip4; - } u_addr; - uint8_t type; // ZTS_IPADDR_TYPE_V4, ZTS_IPADDR_TYPE_V6 + union { + struct zts_ip6_addr ip6; + struct zts_ip4_addr ip4; + } u_addr; + uint8_t type; // ZTS_IPADDR_TYPE_V4, ZTS_IPADDR_TYPE_V6 } zts_ip_addr; /** diff --git a/src/Central.cpp b/src/Central.cpp index c0f17e6..1644681 100644 --- a/src/Central.cpp +++ b/src/Central.cpp @@ -47,80 +47,80 @@ extern "C" { size_t on_data(void* buffer, size_t size, size_t nmemb, void* userp) { - DEBUG_INFO("buf=%p,size=%zu,nmemb=%zu,userp=%p", buffer, size, nmemb, userp); - int byte_count = (size * nmemb); - if (_resp_buf_offset + byte_count >= _resp_buf_len) { - DEBUG_INFO("Out of buffer space. Cannot store response from server"); - return 0; // Signal to libcurl that our buffer is full (triggers a - // write error.) - } - memcpy(_resp_buf + _resp_buf_offset, buffer, byte_count); - _resp_buf_offset += byte_count; - return byte_count; + DEBUG_INFO("buf=%p,size=%zu,nmemb=%zu,userp=%p", buffer, size, nmemb, userp); + int byte_count = (size * nmemb); + if (_resp_buf_offset + byte_count >= _resp_buf_len) { + DEBUG_INFO("Out of buffer space. Cannot store response from server"); + return 0; // Signal to libcurl that our buffer is full (triggers a + // write error.) + } + memcpy(_resp_buf + _resp_buf_offset, buffer, byte_count); + _resp_buf_offset += byte_count; + return byte_count; } int zts_central_set_access_mode(int8_t modes) { - if (! (modes & ZTS_CENTRAL_READ) && ! (modes & ZTS_CENTRAL_WRITE)) { - return ZTS_ERR_ARG; - } - _access_modes = modes; - return ZTS_ERR_OK; + if (! (modes & ZTS_CENTRAL_READ) && ! (modes & ZTS_CENTRAL_WRITE)) { + return ZTS_ERR_ARG; + } + _access_modes = modes; + return ZTS_ERR_OK; } int zts_central_set_verbose(int8_t is_verbose) { - if (is_verbose != 1 && is_verbose != 0) { - return ZTS_ERR_ARG; - } - _bIsVerbose = is_verbose; - return ZTS_ERR_OK; + if (is_verbose != 1 && is_verbose != 0) { + return ZTS_ERR_ARG; + } + _bIsVerbose = is_verbose; + return ZTS_ERR_OK; } void zts_central_clear_resp_buf() { - Mutex::Lock _l(_responseBuffer_m); - memset(_resp_buf, 0, _resp_buf_len); - _resp_buf_offset = 0; + Mutex::Lock _l(_responseBuffer_m); + memset(_resp_buf, 0, _resp_buf_len); + _resp_buf_offset = 0; } int zts_central_init(const char* url_str, const char* token_str, char* resp_buf, uint32_t resp_buf_len) { - _access_modes = ZTS_CENTRAL_READ; // Default read-only - _bIsVerbose = 0; // Default disable libcurl verbose output - Mutex::Lock _l(_responseBuffer_m); - if (resp_buf_len == 0) { - return ZTS_ERR_ARG; - } - _resp_buf = resp_buf; - _resp_buf_len = resp_buf_len; - _resp_buf_offset = 0; - // Initialize all curl internal submodules - curl_global_init(CURL_GLOBAL_ALL); + _access_modes = ZTS_CENTRAL_READ; // Default read-only + _bIsVerbose = 0; // Default disable libcurl verbose output + Mutex::Lock _l(_responseBuffer_m); + if (resp_buf_len == 0) { + return ZTS_ERR_ARG; + } + _resp_buf = resp_buf; + _resp_buf_len = resp_buf_len; + _resp_buf_offset = 0; + // Initialize all curl internal submodules + curl_global_init(CURL_GLOBAL_ALL); - int url_len = strlen(url_str); - if (url_len < 3 || url_len > ZTS_CENRTAL_MAX_URL_LEN) { - return ZTS_ERR_ARG; - } - else { - memset(api_url, 0, ZTS_CENRTAL_MAX_URL_LEN); - strncpy(api_url, url_str, url_len); - } - int token_len = strlen(token_str); - if (token_len != ZTS_CENTRAL_TOKEN_LEN) { - return ZTS_ERR_ARG; - } - else { - memset(api_token, 0, ZTS_CENTRAL_TOKEN_LEN); - strncpy(api_token, token_str, token_len); - } - _bInit = true; - return ZTS_ERR_OK; + int url_len = strlen(url_str); + if (url_len < 3 || url_len > ZTS_CENRTAL_MAX_URL_LEN) { + return ZTS_ERR_ARG; + } + else { + memset(api_url, 0, ZTS_CENRTAL_MAX_URL_LEN); + strncpy(api_url, url_str, url_len); + } + int token_len = strlen(token_str); + if (token_len != ZTS_CENTRAL_TOKEN_LEN) { + return ZTS_ERR_ARG; + } + else { + memset(api_token, 0, ZTS_CENTRAL_TOKEN_LEN); + strncpy(api_token, token_str, token_len); + } + _bInit = true; + return ZTS_ERR_OK; } void zts_central_cleanup() { - curl_global_cleanup(); + curl_global_cleanup(); } int central_req( @@ -131,183 +131,183 @@ int central_req( int* response_code, char* post_data) { - int err = ZTS_ERR_OK; - if (! _bInit) { - DEBUG_INFO("Error: Central API must be initialized first. Call " - "zts_central_init()"); - return ZTS_ERR_SERVICE; - } - if (request_type == ZTS_HTTP_GET && ! (_access_modes & ZTS_CENTRAL_READ)) { - DEBUG_INFO("Error: Incorrect access mode. Need (ZTS_CENTRAL_READ) permission"); - return ZTS_ERR_SERVICE; - } - if (request_type == ZTS_HTTP_POST && ! (_access_modes & ZTS_CENTRAL_WRITE)) { - DEBUG_INFO("Error: Incorrect access mode. Need (ZTS_CENTRAL_WRITE) " - "permission"); - return ZTS_ERR_SERVICE; - } - zts_central_clear_resp_buf(); - int central_strlen = strlen(central_str); - int api_route_strlen = strlen(api_route_str); - int token_strlen = strlen(token_str); - int url_len = central_strlen + api_route_strlen; - if (token_strlen > ZTS_CENTRAL_TOKEN_LEN) { - return ZTS_ERR_ARG; - } - if (url_len > ZTS_CENRTAL_MAX_URL_LEN) { - return ZTS_ERR_ARG; - } - char req_url[ZTS_CENRTAL_MAX_URL_LEN] = { 0 }; - strncpy(req_url, central_str, ZTS_CENRTAL_MAX_URL_LEN); - strncat(req_url, api_route_str, ZTS_CENRTAL_MAX_URL_LEN); + int err = ZTS_ERR_OK; + if (! _bInit) { + DEBUG_INFO("Error: Central API must be initialized first. Call " + "zts_central_init()"); + return ZTS_ERR_SERVICE; + } + if (request_type == ZTS_HTTP_GET && ! (_access_modes & ZTS_CENTRAL_READ)) { + DEBUG_INFO("Error: Incorrect access mode. Need (ZTS_CENTRAL_READ) permission"); + return ZTS_ERR_SERVICE; + } + if (request_type == ZTS_HTTP_POST && ! (_access_modes & ZTS_CENTRAL_WRITE)) { + DEBUG_INFO("Error: Incorrect access mode. Need (ZTS_CENTRAL_WRITE) " + "permission"); + return ZTS_ERR_SERVICE; + } + zts_central_clear_resp_buf(); + int central_strlen = strlen(central_str); + int api_route_strlen = strlen(api_route_str); + int token_strlen = strlen(token_str); + int url_len = central_strlen + api_route_strlen; + if (token_strlen > ZTS_CENTRAL_TOKEN_LEN) { + return ZTS_ERR_ARG; + } + if (url_len > ZTS_CENRTAL_MAX_URL_LEN) { + return ZTS_ERR_ARG; + } + char req_url[ZTS_CENRTAL_MAX_URL_LEN] = { 0 }; + strncpy(req_url, central_str, ZTS_CENRTAL_MAX_URL_LEN); + strncat(req_url, api_route_str, ZTS_CENRTAL_MAX_URL_LEN); - CURL* curl; - CURLcode res; - curl = curl_easy_init(); - if (! curl) { - return ZTS_ERR_GENERAL; - } + CURL* curl; + CURLcode res; + curl = curl_easy_init(); + if (! curl) { + return ZTS_ERR_GENERAL; + } - struct curl_slist* hs = NULL; - char auth_str[ZTS_CENTRAL_TOKEN_LEN + 32] = { 0 }; // + Authorization: Bearer - if (token_strlen == ZTS_CENTRAL_TOKEN_LEN) { - OSUtils::ztsnprintf(auth_str, ZTS_CENTRAL_TOKEN_LEN + 32, "Authorization: Bearer %s", token_str); - } + struct curl_slist* hs = NULL; + char auth_str[ZTS_CENTRAL_TOKEN_LEN + 32] = { 0 }; // + Authorization: Bearer + if (token_strlen == ZTS_CENTRAL_TOKEN_LEN) { + OSUtils::ztsnprintf(auth_str, ZTS_CENTRAL_TOKEN_LEN + 32, "Authorization: Bearer %s", token_str); + } - hs = curl_slist_append(hs, auth_str); - hs = curl_slist_append(hs, "Content-Type: application/json"); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs); - curl_easy_setopt(curl, CURLOPT_URL, req_url); - // example.com is redirected, so we tell libcurl to follow redirection - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); - if (_bIsVerbose) { - curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); - } - // Tell curl to use our write function - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, on_data); + hs = curl_slist_append(hs, auth_str); + hs = curl_slist_append(hs, "Content-Type: application/json"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs); + curl_easy_setopt(curl, CURLOPT_URL, req_url); + // example.com is redirected, so we tell libcurl to follow redirection + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + if (_bIsVerbose) { + curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); + } + // Tell curl to use our write function + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, on_data); - if (request_type == ZTS_HTTP_GET) { - // Nothing - DEBUG_INFO("Request (GET) = %s", api_route_str); - } - if (request_type == ZTS_HTTP_POST) { - DEBUG_INFO("Request (POST) = %s", api_route_str); - if (post_data) { - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); - } - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); - } - if (request_type == ZTS_HTTP_DELETE) { - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); - } - // curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); // Consider 400-500 - // series code as failures - // Perform request - res = curl_easy_perform(curl); - if (res == CURLE_OK) { - // char* url; - double elapsed_time = 0.0; - long hrc = 0; - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &hrc); - curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed_time); - DEBUG_INFO("Req. took %f second(s). HTTP code (%ld)", elapsed_time, hrc); - *response_code = hrc; - // curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url); - } - else { - DEBUG_INFO("%s", curl_easy_strerror(res)); - err = ZTS_ERR_SERVICE; - } - curl_easy_cleanup(curl); - return err; + if (request_type == ZTS_HTTP_GET) { + // Nothing + DEBUG_INFO("Request (GET) = %s", api_route_str); + } + if (request_type == ZTS_HTTP_POST) { + DEBUG_INFO("Request (POST) = %s", api_route_str); + if (post_data) { + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); + } + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); + } + if (request_type == ZTS_HTTP_DELETE) { + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); + } + // curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); // Consider 400-500 + // series code as failures + // Perform request + res = curl_easy_perform(curl); + if (res == CURLE_OK) { + // char* url; + double elapsed_time = 0.0; + long hrc = 0; + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &hrc); + curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed_time); + DEBUG_INFO("Req. took %f second(s). HTTP code (%ld)", elapsed_time, hrc); + *response_code = hrc; + // curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url); + } + else { + DEBUG_INFO("%s", curl_easy_strerror(res)); + err = ZTS_ERR_SERVICE; + } + curl_easy_cleanup(curl); + return err; } int zts_central_get_last_resp_buf(char* dest_buffer, int dest_buf_len) { - if (dest_buf_len <= _resp_buf_offset) { - return ZTS_ERR_ARG; - } - int sz = dest_buf_len < _resp_buf_len ? dest_buf_len : _resp_buf_len; - memcpy(dest_buffer, _resp_buf, sz); - return ZTS_ERR_OK; + if (dest_buf_len <= _resp_buf_offset) { + return ZTS_ERR_ARG; + } + int sz = dest_buf_len < _resp_buf_len ? dest_buf_len : _resp_buf_len; + memcpy(dest_buffer, _resp_buf, sz); + return ZTS_ERR_OK; } int zts_central_status_get(int* resp_code) { - return central_req(ZTS_HTTP_GET, api_url, (char*)"/api/status", api_token, resp_code, NULL); + return central_req(ZTS_HTTP_GET, api_url, (char*)"/api/status", api_token, resp_code, NULL); } int zts_central_self_get(int* resp_code) { - return central_req(ZTS_HTTP_GET, api_url, (char*)"/api/self", api_token, resp_code, NULL); + return central_req(ZTS_HTTP_GET, api_url, (char*)"/api/self", api_token, resp_code, NULL); } int zts_central_net_get(int* resp_code, uint64_t net_id) { - char req[REQ_LEN] = { 0 }; - OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx", net_id); - return central_req(ZTS_HTTP_GET, api_url, req, api_token, resp_code, NULL); + char req[REQ_LEN] = { 0 }; + OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx", net_id); + return central_req(ZTS_HTTP_GET, api_url, req, api_token, resp_code, NULL); } int zts_central_net_update(int* resp_code, uint64_t net_id) { - char req[REQ_LEN] = { 0 }; - OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx", net_id); - return central_req(ZTS_HTTP_POST, api_url, req, api_token, resp_code, NULL); + char req[REQ_LEN] = { 0 }; + OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx", net_id); + return central_req(ZTS_HTTP_POST, api_url, req, api_token, resp_code, NULL); } int zts_central_net_delete(int* resp_code, uint64_t net_id) { - char req[REQ_LEN] = { 0 }; - OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx", net_id); - return central_req(ZTS_HTTP_DELETE, api_url, req, api_token, resp_code, NULL); + char req[REQ_LEN] = { 0 }; + OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx", net_id); + return central_req(ZTS_HTTP_DELETE, api_url, req, api_token, resp_code, NULL); } int zts_central_net_get_all(int* resp_code) { - return central_req(ZTS_HTTP_GET, api_url, (char*)"/api/network", api_token, resp_code, NULL); + return central_req(ZTS_HTTP_GET, api_url, (char*)"/api/network", api_token, resp_code, NULL); } int zts_central_member_get(int* resp_code, uint64_t net_id, uint64_t node_id) { - if (net_id == 0 || node_id == 0) { - return ZTS_ERR_ARG; - } - char req[REQ_LEN] = { 0 }; - OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx/member/%llx", net_id, node_id); - return central_req(ZTS_HTTP_GET, api_url, req, api_token, resp_code, NULL); + if (net_id == 0 || node_id == 0) { + return ZTS_ERR_ARG; + } + char req[REQ_LEN] = { 0 }; + OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx/member/%llx", net_id, node_id); + return central_req(ZTS_HTTP_GET, api_url, req, api_token, resp_code, NULL); } int zts_central_member_update(int* resp_code, uint64_t net_id, uint64_t node_id, char* post_data) { - if (net_id == 0 || node_id == 0 || post_data == NULL) { - return ZTS_ERR_ARG; - } - char req[REQ_LEN] = { 0 }; - OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx/member/%llx", net_id, node_id); - return central_req(ZTS_HTTP_POST, api_url, req, api_token, resp_code, post_data); + if (net_id == 0 || node_id == 0 || post_data == NULL) { + return ZTS_ERR_ARG; + } + char req[REQ_LEN] = { 0 }; + OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx/member/%llx", net_id, node_id); + return central_req(ZTS_HTTP_POST, api_url, req, api_token, resp_code, post_data); } int zts_central_node_auth(int* resp_code, uint64_t net_id, uint64_t node_id, uint8_t is_authed) { - if (is_authed != 0 && is_authed != 1) { - return ZTS_ERR_ARG; - } - char config_data[REQ_LEN] = { 0 }; - if (is_authed == ZTS_CENTRAL_NODE_AUTH_TRUE) { - OSUtils::ztsnprintf(config_data, REQ_LEN, "{\"config\": {\"authorized\": true} }"); - } - if (is_authed == ZTS_CENTRAL_NODE_AUTH_FALSE) { - OSUtils::ztsnprintf(config_data, REQ_LEN, "{\"config\": {\"authorized\": false} }"); - } - return zts_central_member_update(resp_code, net_id, node_id, config_data); + if (is_authed != 0 && is_authed != 1) { + return ZTS_ERR_ARG; + } + char config_data[REQ_LEN] = { 0 }; + if (is_authed == ZTS_CENTRAL_NODE_AUTH_TRUE) { + OSUtils::ztsnprintf(config_data, REQ_LEN, "{\"config\": {\"authorized\": true} }"); + } + if (is_authed == ZTS_CENTRAL_NODE_AUTH_FALSE) { + OSUtils::ztsnprintf(config_data, REQ_LEN, "{\"config\": {\"authorized\": false} }"); + } + return zts_central_member_update(resp_code, net_id, node_id, config_data); } int zts_central_net_get_members(int* resp_code, uint64_t net_id) { - char req[REQ_LEN] = { 0 }; - OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx/member", net_id); - return central_req(ZTS_HTTP_GET, api_url, req, api_token, resp_code, NULL); + char req[REQ_LEN] = { 0 }; + OSUtils::ztsnprintf(req, REQ_LEN, "/api/network/%llx/member", net_id); + return central_req(ZTS_HTTP_GET, api_url, req, api_token, resp_code, NULL); } #ifdef __cplusplus diff --git a/src/Controls.cpp b/src/Controls.cpp index f43f7a1..98afebe 100644 --- a/src/Controls.cpp +++ b/src/Controls.cpp @@ -69,20 +69,20 @@ Mutex service_m; */ int init_subsystems() { - if (! zts_events) { - zts_events = new Events(); - } - if (zts_events->getState(ZTS_STATE_FREE_CALLED)) { - return ZTS_ERR_SERVICE; - } + if (! zts_events) { + zts_events = new Events(); + } + if (zts_events->getState(ZTS_STATE_FREE_CALLED)) { + return ZTS_ERR_SERVICE; + } #ifdef ZTS_ENABLE_CUSTOM_SIGNAL_HANDLERS - _install_signal_handlers(); + _install_signal_handlers(); #endif // ZTS_ENABLE_CUSTOM_SIGNAL_HANDLERS - if (! zts_service) { - zts_service = new NodeService(); - zts_service->setUserEventSystem(zts_events); - } - return ZTS_ERR_OK; + if (! zts_service) { + zts_service = new NodeService(); + zts_service->setUserEventSystem(zts_events); + } + return ZTS_ERR_OK; } #ifdef __cplusplus @@ -91,15 +91,15 @@ extern "C" { int zts_init_from_storage(const char* path) { - ACQUIRE_SERVICE_OFFLINE(); - zts_service->setHomePath(path); - return ZTS_ERR_OK; + ACQUIRE_SERVICE_OFFLINE(); + zts_service->setHomePath(path); + return ZTS_ERR_OK; } int zts_init_from_memory(const char* keypair, unsigned int len) { - ACQUIRE_SERVICE_OFFLINE(); - return zts_service->setIdentity(keypair, len); + ACQUIRE_SERVICE_OFFLINE(); + return zts_service->setIdentity(keypair, len); } #ifdef ZTS_ENABLE_PYTHON @@ -112,154 +112,154 @@ int zts_init_set_event_handler(PythonDirectorCallbackClass* callback) int zts_init_set_event_handler(void (*callback)(void*)) #endif { - ACQUIRE_SERVICE_OFFLINE(); - if (! callback) { - return ZTS_ERR_ARG; - } - _userEventCallback = callback; - zts_service->enableEvents(); - return ZTS_ERR_OK; + ACQUIRE_SERVICE_OFFLINE(); + if (! callback) { + return ZTS_ERR_ARG; + } + _userEventCallback = callback; + zts_service->enableEvents(); + return ZTS_ERR_OK; } int zts_init_blacklist_if(const char* prefix, unsigned int len) { - ACQUIRE_SERVICE_OFFLINE(); - return zts_service->addInterfacePrefixToBlacklist(prefix, len); + ACQUIRE_SERVICE_OFFLINE(); + return zts_service->addInterfacePrefixToBlacklist(prefix, len); } int zts_init_set_world(const void* world_data, unsigned int len) { - ACQUIRE_SERVICE_OFFLINE(); - return zts_service->setWorld(world_data, len); + ACQUIRE_SERVICE_OFFLINE(); + return zts_service->setWorld(world_data, len); } int zts_init_set_port(unsigned short port) { - ACQUIRE_SERVICE_OFFLINE(); - zts_service->setPrimaryPort(port); - return ZTS_ERR_OK; + ACQUIRE_SERVICE_OFFLINE(); + zts_service->setPrimaryPort(port); + return ZTS_ERR_OK; } int zts_init_allow_peer_cache(unsigned int allowed) { - ACQUIRE_SERVICE_OFFLINE(); - return zts_service->allowPeerCaching(allowed); + ACQUIRE_SERVICE_OFFLINE(); + return zts_service->allowPeerCaching(allowed); } int zts_init_allow_net_cache(unsigned int allowed) { - ACQUIRE_SERVICE_OFFLINE(); - return zts_service->allowNetworkCaching(allowed); + ACQUIRE_SERVICE_OFFLINE(); + return zts_service->allowNetworkCaching(allowed); } int zts_init_allow_world_cache(unsigned int allowed) { - ACQUIRE_SERVICE_OFFLINE(); - return zts_service->allowWorldCaching(allowed); + ACQUIRE_SERVICE_OFFLINE(); + return zts_service->allowWorldCaching(allowed); } int zts_init_allow_id_cache(unsigned int allowed) { - ACQUIRE_SERVICE_OFFLINE(); - return zts_service->allowIdentityCaching(allowed); + ACQUIRE_SERVICE_OFFLINE(); + return zts_service->allowIdentityCaching(allowed); } int zts_addr_compute_6plane(const uint64_t net_id, const uint64_t node_id, struct zts_sockaddr_storage* addr) { - if (! addr || ! net_id || ! node_id) { - return ZTS_ERR_ARG; - } - InetAddress _6planeAddr = InetAddress::makeIpv66plane(net_id, node_id); - struct sockaddr_in6* in6 = (struct sockaddr_in6*)addr; - memcpy(in6->sin6_addr.s6_addr, _6planeAddr.rawIpData(), sizeof(struct in6_addr)); - return ZTS_ERR_OK; + if (! addr || ! net_id || ! node_id) { + return ZTS_ERR_ARG; + } + InetAddress _6planeAddr = InetAddress::makeIpv66plane(net_id, node_id); + struct sockaddr_in6* in6 = (struct sockaddr_in6*)addr; + memcpy(in6->sin6_addr.s6_addr, _6planeAddr.rawIpData(), sizeof(struct in6_addr)); + return ZTS_ERR_OK; } int zts_addr_compute_rfc4193(const uint64_t net_id, const uint64_t node_id, struct zts_sockaddr_storage* addr) { - if (! addr || ! net_id || ! node_id) { - return ZTS_ERR_ARG; - } - InetAddress _rfc4193Addr = InetAddress::makeIpv6rfc4193(net_id, node_id); - struct sockaddr_in6* in6 = (struct sockaddr_in6*)addr; - memcpy(in6->sin6_addr.s6_addr, _rfc4193Addr.rawIpData(), sizeof(struct in6_addr)); - return ZTS_ERR_OK; + if (! addr || ! net_id || ! node_id) { + return ZTS_ERR_ARG; + } + InetAddress _rfc4193Addr = InetAddress::makeIpv6rfc4193(net_id, node_id); + struct sockaddr_in6* in6 = (struct sockaddr_in6*)addr; + memcpy(in6->sin6_addr.s6_addr, _rfc4193Addr.rawIpData(), sizeof(struct in6_addr)); + return ZTS_ERR_OK; } int zts_addr_compute_rfc4193_str(uint64_t net_id, uint64_t node_id, char* dst, unsigned int len) { - if (! net_id || ! node_id || ! dst || len != ZTS_IP_MAX_STR_LEN) { - return ZTS_ERR_ARG; - } - struct zts_sockaddr_storage ss; - int err = ZTS_ERR_OK; - if ((err = zts_addr_compute_rfc4193(net_id, node_id, &ss)) != ZTS_ERR_OK) { - return err; - } - struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&ss; - zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), dst, ZTS_IP_MAX_STR_LEN); - return ZTS_ERR_OK; + if (! net_id || ! node_id || ! dst || len != ZTS_IP_MAX_STR_LEN) { + return ZTS_ERR_ARG; + } + struct zts_sockaddr_storage ss; + int err = ZTS_ERR_OK; + if ((err = zts_addr_compute_rfc4193(net_id, node_id, &ss)) != ZTS_ERR_OK) { + return err; + } + struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&ss; + zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), dst, ZTS_IP_MAX_STR_LEN); + return ZTS_ERR_OK; } int zts_addr_compute_6plane_str(uint64_t net_id, uint64_t node_id, char* dst, unsigned int len) { - if (! net_id || ! node_id || ! dst || len != ZTS_IP_MAX_STR_LEN) { - return ZTS_ERR_ARG; - } - struct zts_sockaddr_storage ss; - int err = ZTS_ERR_OK; - if ((err = zts_addr_compute_6plane(net_id, node_id, &ss)) != ZTS_ERR_OK) { - return err; - } - struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&ss; - zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), dst, ZTS_IP_MAX_STR_LEN); - return ZTS_ERR_OK; + if (! net_id || ! node_id || ! dst || len != ZTS_IP_MAX_STR_LEN) { + return ZTS_ERR_ARG; + } + struct zts_sockaddr_storage ss; + int err = ZTS_ERR_OK; + if ((err = zts_addr_compute_6plane(net_id, node_id, &ss)) != ZTS_ERR_OK) { + return err; + } + struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&ss; + zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), dst, ZTS_IP_MAX_STR_LEN); + return ZTS_ERR_OK; } uint64_t zts_net_compute_adhoc_id(uint16_t start_port, uint16_t end_port) { - char net_id_str[ZTS_INET6_ADDRSTRLEN] = { 0 }; - OSUtils::ztsnprintf(net_id_str, ZTS_INET6_ADDRSTRLEN, "ff%04x%04x000000", start_port, end_port); - return strtoull(net_id_str, NULL, 16); + char net_id_str[ZTS_INET6_ADDRSTRLEN] = { 0 }; + OSUtils::ztsnprintf(net_id_str, ZTS_INET6_ADDRSTRLEN, "ff%04x%04x000000", start_port, end_port); + return strtoull(net_id_str, NULL, 16); } int zts_id_new(char* key, unsigned int* dst_len) { - if (key == NULL || *dst_len != ZT_IDENTITY_STRING_BUFFER_LENGTH) { - return ZTS_ERR_ARG; - } - Identity id; - id.generate(); - char idtmp[1024] = { 0 }; - std::string idser = id.toString(true, idtmp); - unsigned int key_pair_len = idser.length(); - if (key_pair_len > *dst_len) { - return ZTS_ERR_ARG; - } - memcpy(key, idser.c_str(), key_pair_len); - *dst_len = key_pair_len; - return ZTS_ERR_OK; + if (key == NULL || *dst_len != ZT_IDENTITY_STRING_BUFFER_LENGTH) { + return ZTS_ERR_ARG; + } + Identity id; + id.generate(); + char idtmp[1024] = { 0 }; + std::string idser = id.toString(true, idtmp); + unsigned int key_pair_len = idser.length(); + if (key_pair_len > *dst_len) { + return ZTS_ERR_ARG; + } + memcpy(key, idser.c_str(), key_pair_len); + *dst_len = key_pair_len; + return ZTS_ERR_OK; } int zts_id_pair_is_valid(const char* key, unsigned int len) { - if (key == NULL || len != ZT_IDENTITY_STRING_BUFFER_LENGTH) { - return false; - } - Identity id; - if ((strlen(key) > 32) && (key[10] == ':')) { - if (id.fromString(key)) { - return id.locallyValidate(); - } - } - return false; + if (key == NULL || len != ZT_IDENTITY_STRING_BUFFER_LENGTH) { + return false; + } + Identity id; + if ((strlen(key) > 32) && (key[10] == ':')) { + if (id.fromString(key)) { + return id.locallyValidate(); + } + } + return false; } int zts_node_get_id_pair(char* key, unsigned int* dst_len) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - zts_service->getIdentity(key, dst_len); - return *dst_len > 0 ? ZTS_ERR_OK : ZTS_ERR_GENERAL; + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + zts_service->getIdentity(key, dst_len); + return *dst_len > 0 ? ZTS_ERR_OK : ZTS_ERR_GENERAL; } #if defined(__WINDOWS__) @@ -269,95 +269,95 @@ void* cbRun(void* arg) #endif { #if defined(__APPLE__) - pthread_setname_np(ZTS_EVENT_CALLBACK_THREAD_NAME); + pthread_setname_np(ZTS_EVENT_CALLBACK_THREAD_NAME); #endif - zts_events->run(); + zts_events->run(); #if ZTS_ENABLE_JAVA - _java_detach_from_thread(); - // pthread_exit(0); + _java_detach_from_thread(); + // pthread_exit(0); #endif - return NULL; + return NULL; } int zts_addr_is_assigned(uint64_t net_id, unsigned int family) { - ACQUIRE_SERVICE(0); - return zts_service->addrIsAssigned(net_id, family); + ACQUIRE_SERVICE(0); + return zts_service->addrIsAssigned(net_id, family); } int zts_addr_get(uint64_t net_id, unsigned int family, struct zts_sockaddr_storage* addr) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getFirstAssignedAddr(net_id, family, addr); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getFirstAssignedAddr(net_id, family, addr); } int zts_addr_get_str(uint64_t net_id, unsigned int family, char* dst, unsigned int len) { - // No service lock required since zts_addr_get will lock it - if (net_id == 0) { - return ZTS_ERR_ARG; - } - if (len < ZTS_INET6_ADDRSTRLEN) { - return ZTS_ERR_ARG; - } + // No service lock required since zts_addr_get will lock it + if (net_id == 0) { + return ZTS_ERR_ARG; + } + if (len < ZTS_INET6_ADDRSTRLEN) { + return ZTS_ERR_ARG; + } - if (family == ZTS_AF_INET) { - struct zts_sockaddr_storage ss; - int err = ZTS_ERR_OK; - if ((err = zts_addr_get(net_id, family, &ss)) != ZTS_ERR_OK) { - return err; - } - struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&ss; - zts_inet_ntop(family, &(in4->sin_addr), dst, ZTS_INET6_ADDRSTRLEN); - } - if (family == ZTS_AF_INET6) { - struct zts_sockaddr_storage ss; - int err = ZTS_ERR_OK; - if ((err = zts_addr_get(net_id, family, &ss)) != ZTS_ERR_OK) { - return err; - } - struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&ss; - zts_inet_ntop(family, &(in6->sin6_addr), dst, ZTS_INET6_ADDRSTRLEN); - } - return ZTS_ERR_OK; + if (family == ZTS_AF_INET) { + struct zts_sockaddr_storage ss; + int err = ZTS_ERR_OK; + if ((err = zts_addr_get(net_id, family, &ss)) != ZTS_ERR_OK) { + return err; + } + struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)&ss; + zts_inet_ntop(family, &(in4->sin_addr), dst, ZTS_INET6_ADDRSTRLEN); + } + if (family == ZTS_AF_INET6) { + struct zts_sockaddr_storage ss; + int err = ZTS_ERR_OK; + if ((err = zts_addr_get(net_id, family, &ss)) != ZTS_ERR_OK) { + return err; + } + struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)&ss; + zts_inet_ntop(family, &(in6->sin6_addr), dst, ZTS_INET6_ADDRSTRLEN); + } + return ZTS_ERR_OK; } int zts_addr_get_all(uint64_t net_id, struct zts_sockaddr_storage* addr, unsigned int* count) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getAllAssignedAddr(net_id, addr, count); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getAllAssignedAddr(net_id, addr, count); } int zts_core_lock_obtain() { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - zts_service->obtainLock(); - return ZTS_ERR_OK; + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + zts_service->obtainLock(); + return ZTS_ERR_OK; } int zts_core_lock_release() { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - zts_service->releaseLock(); - return ZTS_ERR_OK; + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + zts_service->releaseLock(); + return ZTS_ERR_OK; } int zts_core_query_addr_count(uint64_t net_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->addressCount(net_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->addressCount(net_id); } int zts_core_query_addr(uint64_t net_id, unsigned int idx, char* addr, unsigned int len) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getAddrAtIdx(net_id, idx, addr, len); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getAddrAtIdx(net_id, idx, addr, len); } int zts_core_query_route_count(uint64_t net_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->routeCount(net_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->routeCount(net_id); } int zts_core_query_route( @@ -369,110 +369,110 @@ int zts_core_query_route( uint16_t* flags, uint16_t* metric) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getRouteAtIdx(net_id, idx, target, via, len, flags, metric); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getRouteAtIdx(net_id, idx, target, via, len, flags, metric); } int zts_core_query_path_count(uint64_t peer_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->pathCount(peer_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->pathCount(peer_id); } int zts_core_query_path(uint64_t peer_id, unsigned int idx, char* path, unsigned int len) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getPathAtIdx(peer_id, idx, path, len); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getPathAtIdx(peer_id, idx, path, len); } int zts_core_query_mc_count(uint64_t net_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->multicastSubCount(net_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->multicastSubCount(net_id); } int zts_core_query_mc(uint64_t net_id, unsigned int idx, uint64_t* mac, uint32_t* adi) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getMulticastSubAtIdx(net_id, idx, mac, adi); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getMulticastSubAtIdx(net_id, idx, mac, adi); } int zts_net_join(const uint64_t net_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->join(net_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->join(net_id); } int zts_net_leave(const uint64_t net_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->leave(net_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->leave(net_id); } int zts_net_transport_is_ready(const uint64_t net_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->networkIsReady(net_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->networkIsReady(net_id); } uint64_t zts_net_get_mac(uint64_t net_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getMACAddress(net_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getMACAddress(net_id); } ZTS_API int ZTCALL zts_net_get_mac_str(uint64_t net_id, char* dst, unsigned int len) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - if (! dst || len < ZTS_MAC_ADDRSTRLEN) { - return ZTS_ERR_ARG; - } - uint64_t mac = zts_service->getMACAddress(net_id); - OSUtils::ztsnprintf( - dst, - ZTS_MAC_ADDRSTRLEN, - "%x:%x:%x:%x:%x:%x", - (mac >> 48) & 0xFF, - (mac >> 32) & 0xFF, - (mac >> 24) & 0xFF, - (mac >> 16) & 0xFF, - (mac >> 8) & 0xFF, - (mac >> 0) & 0xFF); - return ZTS_ERR_OK; + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + if (! dst || len < ZTS_MAC_ADDRSTRLEN) { + return ZTS_ERR_ARG; + } + uint64_t mac = zts_service->getMACAddress(net_id); + OSUtils::ztsnprintf( + dst, + ZTS_MAC_ADDRSTRLEN, + "%x:%x:%x:%x:%x:%x", + (mac >> 48) & 0xFF, + (mac >> 32) & 0xFF, + (mac >> 24) & 0xFF, + (mac >> 16) & 0xFF, + (mac >> 8) & 0xFF, + (mac >> 0) & 0xFF); + return ZTS_ERR_OK; } int zts_net_get_broadcast(uint64_t net_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getNetworkBroadcast(net_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getNetworkBroadcast(net_id); } int zts_net_get_mtu(uint64_t net_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getNetworkMTU(net_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getNetworkMTU(net_id); } int zts_net_get_name(uint64_t net_id, char* dst, unsigned int len) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getNetworkName(net_id, dst, len); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getNetworkName(net_id, dst, len); } int zts_net_get_status(uint64_t net_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getNetworkStatus(net_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getNetworkStatus(net_id); } int zts_net_get_type(uint64_t net_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getNetworkType(net_id); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getNetworkType(net_id); } int zts_route_is_assigned(uint64_t net_id, unsigned int family) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->networkHasRoute(net_id, family); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->networkHasRoute(net_id, family); } // Start a ZeroTier NodeService background thread @@ -483,211 +483,211 @@ void* _runNodeService(void* arg) #endif { #if defined(__APPLE__) - pthread_setname_np(ZTS_SERVICE_THREAD_NAME); + pthread_setname_np(ZTS_SERVICE_THREAD_NAME); #endif - try { - zts_service->run(); - // Begin shutdown - service_m.lock(); - zts_events->clrState(ZTS_STATE_NODE_RUNNING); - delete zts_service; - zts_service = (NodeService*)0; - service_m.unlock(); - events_m.lock(); - zts_util_delay(ZTS_CALLBACK_PROCESSING_INTERVAL * 2); - if (zts_events) { - zts_events->disable(); - delete zts_events; - zts_events = (Events*)0; - } - events_m.unlock(); - } - catch (...) { - } + try { + zts_service->run(); + // Begin shutdown + service_m.lock(); + zts_events->clrState(ZTS_STATE_NODE_RUNNING); + delete zts_service; + zts_service = (NodeService*)0; + service_m.unlock(); + events_m.lock(); + zts_util_delay(ZTS_CALLBACK_PROCESSING_INTERVAL * 2); + if (zts_events) { + zts_events->disable(); + delete zts_events; + zts_events = (Events*)0; + } + events_m.unlock(); + } + catch (...) { + } #ifndef __WINDOWS__ - pthread_exit(0); + pthread_exit(0); #endif - return NULL; + return NULL; } int zts_node_start() { - ACQUIRE_SERVICE_OFFLINE(); - // Start TCP/IP stack - _lwip_driver_init(); - // Start callback thread - int res = ZTS_ERR_OK; - if (zts_events->hasCallback()) { + ACQUIRE_SERVICE_OFFLINE(); + // Start TCP/IP stack + _lwip_driver_init(); + // Start callback thread + int res = ZTS_ERR_OK; + if (zts_events->hasCallback()) { #if defined(__WINDOWS__) - HANDLE callbackThread = CreateThread(NULL, 0, cbRun, NULL, 0, NULL); - // TODO: Check success + HANDLE callbackThread = CreateThread(NULL, 0, cbRun, NULL, 0, NULL); + // TODO: Check success #else - pthread_t cbThread; - if ((res = pthread_create(&cbThread, NULL, cbRun, NULL)) != 0) {} + pthread_t cbThread; + if ((res = pthread_create(&cbThread, NULL, cbRun, NULL)) != 0) {} #endif #if defined(__linux__) - pthread_setname_np(cbThread, ZTS_EVENT_CALLBACK_THREAD_NAME); + pthread_setname_np(cbThread, ZTS_EVENT_CALLBACK_THREAD_NAME); #endif - if (res != ZTS_ERR_OK) { - zts_events->clrState(ZTS_STATE_CALLBACKS_RUNNING); - zts_events->clrCallback(); - } - zts_events->setState(ZTS_STATE_CALLBACKS_RUNNING); - } - // Start ZeroTier service + if (res != ZTS_ERR_OK) { + zts_events->clrState(ZTS_STATE_CALLBACKS_RUNNING); + zts_events->clrCallback(); + } + zts_events->setState(ZTS_STATE_CALLBACKS_RUNNING); + } + // Start ZeroTier service #if defined(__WINDOWS__) - WSAStartup(MAKEWORD(2, 2), &wsaData); - HANDLE serviceThread = CreateThread(NULL, 0, _runNodeService, (void*)NULL, 0, NULL); - // TODO: Check success + WSAStartup(MAKEWORD(2, 2), &wsaData); + HANDLE serviceThread = CreateThread(NULL, 0, _runNodeService, (void*)NULL, 0, NULL); + // TODO: Check success #else - pthread_t service_thread; - if ((res = pthread_create(&service_thread, NULL, _runNodeService, (void*)NULL)) != 0) {} + pthread_t service_thread; + if ((res = pthread_create(&service_thread, NULL, _runNodeService, (void*)NULL)) != 0) {} #endif #if defined(__linux__) - pthread_setname_np(service_thread, ZTS_SERVICE_THREAD_NAME); + pthread_setname_np(service_thread, ZTS_SERVICE_THREAD_NAME); #endif - if (res != ZTS_ERR_OK) { - zts_events->clrState(ZTS_STATE_NODE_RUNNING); - } - zts_events->setState(ZTS_STATE_NODE_RUNNING); - return ZTS_ERR_OK; + if (res != ZTS_ERR_OK) { + zts_events->clrState(ZTS_STATE_NODE_RUNNING); + } + zts_events->setState(ZTS_STATE_NODE_RUNNING); + return ZTS_ERR_OK; } int zts_node_is_online() { - ACQUIRE_SERVICE(0); - return zts_service->nodeIsOnline(); + ACQUIRE_SERVICE(0); + return zts_service->nodeIsOnline(); } uint64_t zts_node_get_id() { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getNodeId(); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getNodeId(); } int zts_node_get_port() { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - return zts_service->getPrimaryPort(); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + return zts_service->getPrimaryPort(); } int zts_node_stop() { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - zts_events->clrState(ZTS_STATE_NODE_RUNNING); - zts_service->terminate(); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + zts_events->clrState(ZTS_STATE_NODE_RUNNING); + zts_service->terminate(); #if defined(__WINDOWS__) - WSACleanup(); + WSACleanup(); #endif - return ZTS_ERR_OK; + return ZTS_ERR_OK; } int zts_node_free() { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - zts_events->setState(ZTS_STATE_FREE_CALLED); - zts_events->clrState(ZTS_STATE_NODE_RUNNING); - zts_service->terminate(); + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + zts_events->setState(ZTS_STATE_FREE_CALLED); + zts_events->clrState(ZTS_STATE_NODE_RUNNING); + zts_service->terminate(); #if defined(__WINDOWS__) - WSACleanup(); + WSACleanup(); #endif - _lwip_driver_shutdown(); - return ZTS_ERR_OK; + _lwip_driver_shutdown(); + return ZTS_ERR_OK; } int zts_moon_orbit(uint64_t moon_world_id, uint64_t moon_seed) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - zts_service->orbit(NULL, moon_world_id, moon_seed); - return ZTS_ERR_OK; + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + zts_service->orbit(NULL, moon_world_id, moon_seed); + return ZTS_ERR_OK; } int zts_moon_deorbit(uint64_t moon_world_id) { - ACQUIRE_SERVICE(ZTS_ERR_SERVICE); - zts_service->deorbit(NULL, moon_world_id); - return ZTS_ERR_OK; + ACQUIRE_SERVICE(ZTS_ERR_SERVICE); + zts_service->deorbit(NULL, moon_world_id); + return ZTS_ERR_OK; } int zts_stats_get_all(zts_stats_counter_t* dst) { - if (! dst) { - return ZTS_ERR_ARG; - } - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } + if (! dst) { + return ZTS_ERR_ARG; + } + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } #if LWIP_STATS - extern struct stats_ lwip_stats; + extern struct stats_ lwip_stats; #define lws lwip_stats - /* Here we summarize lwIP's statistics for simplicity at the expense of specificity */ + /* Here we summarize lwIP's statistics for simplicity at the expense of specificity */ - // link - dst->link_tx = lws.link.xmit; - dst->link_rx = lws.link.recv; - dst->link_drop = lws.link.drop; - dst->link_err = lws.link.chkerr + lws.link.lenerr + lws.link.memerr + lws.link.rterr + lws.link.proterr - + lws.link.opterr + lws.link.err; - // etharp - dst->etharp_tx = lws.etharp.xmit; - dst->etharp_rx = lws.etharp.recv; - dst->etharp_drop = lws.etharp.drop; - dst->etharp_err = lws.etharp.chkerr + lws.etharp.lenerr + lws.etharp.memerr + lws.etharp.rterr + lws.etharp.proterr - + lws.etharp.opterr + lws.etharp.err; - // ip4 - dst->ip4_tx = lws.ip.xmit; - dst->ip4_rx = lws.ip.recv; - dst->ip4_drop = lws.ip.drop; - dst->ip4_err = lws.ip.chkerr + lws.ip.lenerr + lws.ip.memerr + lws.ip.rterr + lws.ip.proterr + lws.ip.opterr - + lws.ip.err + lws.ip_frag.chkerr + lws.ip_frag.lenerr + lws.ip_frag.memerr + lws.ip_frag.rterr - + lws.ip_frag.proterr + lws.ip_frag.opterr + lws.ip_frag.err; - // ip6 - dst->ip6_tx = lws.ip6.xmit; - dst->ip6_rx = lws.ip6.recv; - dst->ip6_drop = lws.ip6.drop; - dst->ip6_err = lws.ip6.chkerr + lws.ip6.lenerr + lws.ip6.memerr + lws.ip6.rterr + lws.ip6.proterr + lws.ip6.opterr - + lws.ip6.err + lws.ip6_frag.chkerr + lws.ip6_frag.lenerr + lws.ip6_frag.memerr + lws.ip6_frag.rterr - + lws.ip6_frag.proterr + lws.ip6_frag.opterr + lws.ip6_frag.err; + // link + dst->link_tx = lws.link.xmit; + dst->link_rx = lws.link.recv; + dst->link_drop = lws.link.drop; + dst->link_err = lws.link.chkerr + lws.link.lenerr + lws.link.memerr + lws.link.rterr + lws.link.proterr + + lws.link.opterr + lws.link.err; + // etharp + dst->etharp_tx = lws.etharp.xmit; + dst->etharp_rx = lws.etharp.recv; + dst->etharp_drop = lws.etharp.drop; + dst->etharp_err = lws.etharp.chkerr + lws.etharp.lenerr + lws.etharp.memerr + lws.etharp.rterr + lws.etharp.proterr + + lws.etharp.opterr + lws.etharp.err; + // ip4 + dst->ip4_tx = lws.ip.xmit; + dst->ip4_rx = lws.ip.recv; + dst->ip4_drop = lws.ip.drop; + dst->ip4_err = lws.ip.chkerr + lws.ip.lenerr + lws.ip.memerr + lws.ip.rterr + lws.ip.proterr + lws.ip.opterr + + lws.ip.err + lws.ip_frag.chkerr + lws.ip_frag.lenerr + lws.ip_frag.memerr + lws.ip_frag.rterr + + lws.ip_frag.proterr + lws.ip_frag.opterr + lws.ip_frag.err; + // ip6 + dst->ip6_tx = lws.ip6.xmit; + dst->ip6_rx = lws.ip6.recv; + dst->ip6_drop = lws.ip6.drop; + dst->ip6_err = lws.ip6.chkerr + lws.ip6.lenerr + lws.ip6.memerr + lws.ip6.rterr + lws.ip6.proterr + lws.ip6.opterr + + lws.ip6.err + lws.ip6_frag.chkerr + lws.ip6_frag.lenerr + lws.ip6_frag.memerr + lws.ip6_frag.rterr + + lws.ip6_frag.proterr + lws.ip6_frag.opterr + lws.ip6_frag.err; - // icmp4 - dst->icmp4_tx = lws.icmp.xmit; - dst->icmp4_rx = lws.icmp.recv; - dst->icmp4_drop = lws.icmp.drop; - dst->icmp4_err = lws.icmp.chkerr + lws.icmp.lenerr + lws.icmp.memerr + lws.icmp.rterr + lws.icmp.proterr - + lws.icmp.opterr + lws.icmp.err; - // icmp6 - dst->icmp6_tx = lws.icmp6.xmit; - dst->icmp6_rx = lws.icmp6.recv; - dst->icmp6_drop = lws.icmp6.drop; - dst->icmp6_err = lws.icmp6.chkerr + lws.icmp6.lenerr + lws.icmp6.memerr + lws.icmp6.rterr + lws.icmp6.proterr - + lws.icmp6.opterr + lws.icmp6.err; - // udp - dst->udp_tx = lws.udp.xmit; - dst->udp_rx = lws.udp.recv; - dst->udp_drop = lws.udp.drop; - dst->udp_err = lws.udp.chkerr + lws.udp.lenerr + lws.udp.memerr + lws.udp.rterr + lws.udp.proterr + lws.udp.opterr - + lws.udp.err; - // tcp - dst->tcp_tx = lws.tcp.xmit; - dst->tcp_rx = lws.tcp.recv; - dst->tcp_drop = lws.tcp.drop; - dst->tcp_err = lws.tcp.chkerr + lws.tcp.lenerr + lws.tcp.memerr + lws.tcp.rterr + lws.tcp.proterr + lws.tcp.opterr - + lws.tcp.err; - // nd6 - dst->nd6_tx = lws.nd6.xmit; - dst->nd6_rx = lws.nd6.recv; - dst->nd6_drop = lws.nd6.drop; - dst->nd6_err = lws.nd6.chkerr + lws.nd6.lenerr + lws.nd6.memerr + lws.nd6.rterr + lws.nd6.proterr + lws.nd6.opterr - + lws.nd6.err; + // icmp4 + dst->icmp4_tx = lws.icmp.xmit; + dst->icmp4_rx = lws.icmp.recv; + dst->icmp4_drop = lws.icmp.drop; + dst->icmp4_err = lws.icmp.chkerr + lws.icmp.lenerr + lws.icmp.memerr + lws.icmp.rterr + lws.icmp.proterr + + lws.icmp.opterr + lws.icmp.err; + // icmp6 + dst->icmp6_tx = lws.icmp6.xmit; + dst->icmp6_rx = lws.icmp6.recv; + dst->icmp6_drop = lws.icmp6.drop; + dst->icmp6_err = lws.icmp6.chkerr + lws.icmp6.lenerr + lws.icmp6.memerr + lws.icmp6.rterr + lws.icmp6.proterr + + lws.icmp6.opterr + lws.icmp6.err; + // udp + dst->udp_tx = lws.udp.xmit; + dst->udp_rx = lws.udp.recv; + dst->udp_drop = lws.udp.drop; + dst->udp_err = lws.udp.chkerr + lws.udp.lenerr + lws.udp.memerr + lws.udp.rterr + lws.udp.proterr + lws.udp.opterr + + lws.udp.err; + // tcp + dst->tcp_tx = lws.tcp.xmit; + dst->tcp_rx = lws.tcp.recv; + dst->tcp_drop = lws.tcp.drop; + dst->tcp_err = lws.tcp.chkerr + lws.tcp.lenerr + lws.tcp.memerr + lws.tcp.rterr + lws.tcp.proterr + lws.tcp.opterr + + lws.tcp.err; + // nd6 + dst->nd6_tx = lws.nd6.xmit; + dst->nd6_rx = lws.nd6.recv; + dst->nd6_drop = lws.nd6.drop; + dst->nd6_err = lws.nd6.chkerr + lws.nd6.lenerr + lws.nd6.memerr + lws.nd6.rterr + lws.nd6.proterr + lws.nd6.opterr + + lws.nd6.err; - // TODO: Add mem and sys stats + // TODO: Add mem and sys stats - return ZTS_ERR_OK; + return ZTS_ERR_OK; #else - return ZTS_ERR_NO_RESULT; + return ZTS_ERR_NO_RESULT; #endif #undef lws } diff --git a/src/Debug.hpp b/src/Debug.hpp index be38235..a835640 100644 --- a/src/Debug.hpp +++ b/src/Debug.hpp @@ -62,20 +62,20 @@ #if defined(LIBZT_DEBUG) #if defined(__ANDROID__) #define DEBUG_INFO(fmt, args...) \ - ((void)__android_log_print( \ - ANDROID_LOG_VERBOSE, \ - ZT_LOG_TAG, \ - "%17s:%5d:%20s: " fmt "\n", \ - ZT_FILENAME, \ - __LINE__, \ - __FUNCTION__, \ - ##args)) + ((void)__android_log_print( \ + ANDROID_LOG_VERBOSE, \ + ZT_LOG_TAG, \ + "%17s:%5d:%20s: " fmt "\n", \ + ZT_FILENAME, \ + __LINE__, \ + __FUNCTION__, \ + ##args)) #elif defined(_WIN32) #define DEBUG_INFO(fmt, ...) \ - fprintf(stderr, ZT_WHT "%17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__) + fprintf(stderr, ZT_WHT "%17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, __VA_ARGS__) #else #define DEBUG_INFO(fmt, args...) \ - fprintf(stderr, ZT_WHT "%17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) + fprintf(stderr, ZT_WHT "%17s:%5d:%25s: " fmt "\n" ZT_RESET, ZT_FILENAME, __LINE__, __FUNCTION__, ##args) #endif #else // !LIBZT_DEBUG #if defined(_WIN32) diff --git a/src/Events.cpp b/src/Events.cpp index 53ced3a..e362ed5 100644 --- a/src/Events.cpp +++ b/src/Events.cpp @@ -75,218 +75,218 @@ moodycamel::ConcurrentQueue _callbackMsgQueue; void Events::run() { - while (getState(ZTS_STATE_CALLBACKS_RUNNING) || _callbackMsgQueue.size_approx() > 0) { - zts_event_msg_t* msg; - size_t sz = _callbackMsgQueue.size_approx(); - for (size_t j = 0; j < sz; j++) { - if (_callbackMsgQueue.try_dequeue(msg)) { - events_m.lock(); - sendToUser(msg); - events_m.unlock(); - } - } - zts_util_delay(ZTS_CALLBACK_PROCESSING_INTERVAL); - } + while (getState(ZTS_STATE_CALLBACKS_RUNNING) || _callbackMsgQueue.size_approx() > 0) { + zts_event_msg_t* msg; + size_t sz = _callbackMsgQueue.size_approx(); + for (size_t j = 0; j < sz; j++) { + if (_callbackMsgQueue.try_dequeue(msg)) { + events_m.lock(); + sendToUser(msg); + events_m.unlock(); + } + } + zts_util_delay(ZTS_CALLBACK_PROCESSING_INTERVAL); + } } void Events::enqueue(unsigned int event_code, const void* arg, int len) { - if (! _enabled) { - return; - } - zts_event_msg_t* msg = new zts_event_msg_t(); - msg->event_code = event_code; + if (! _enabled) { + return; + } + zts_event_msg_t* msg = new zts_event_msg_t(); + msg->event_code = event_code; - if (ZTS_NODE_EVENT(event_code)) { - msg->node = (zts_node_info_t*)arg; - msg->len = sizeof(zts_node_info_t); - } - if (ZTS_NETWORK_EVENT(event_code)) { - msg->network = (zts_net_info_t*)arg; - msg->len = sizeof(zts_net_info_t); - } - if (ZTS_STACK_EVENT(event_code)) { - /* nothing to convey to user */ - } - if (ZTS_NETIF_EVENT(event_code)) { - msg->netif = (zts_netif_info_t*)arg; - msg->len = sizeof(zts_netif_info_t); - } - if (ZTS_ROUTE_EVENT(event_code)) { - msg->route = (zts_route_info_t*)arg; - msg->len = sizeof(zts_route_info_t); - } - if (ZTS_PEER_EVENT(event_code)) { - msg->peer = (zts_peer_info_t*)arg; - msg->len = sizeof(zts_peer_info_t); - } - if (ZTS_ADDR_EVENT(event_code)) { - msg->addr = (zts_addr_info_t*)arg; - msg->len = sizeof(zts_addr_info_t); - } - if (ZTS_STORE_EVENT(event_code)) { - msg->cache = (void*)arg; - msg->len = len; - } - if (msg && _callbackMsgQueue.size_approx() > 1024) { - /* Rate-limit number of events. This value should only grow if the - user application isn't returning from the event handler in a timely manner. - For most applications it should hover around 1 to 2 */ - destroy(msg); - } - else { - _callbackMsgQueue.enqueue(msg); - } + if (ZTS_NODE_EVENT(event_code)) { + msg->node = (zts_node_info_t*)arg; + msg->len = sizeof(zts_node_info_t); + } + if (ZTS_NETWORK_EVENT(event_code)) { + msg->network = (zts_net_info_t*)arg; + msg->len = sizeof(zts_net_info_t); + } + if (ZTS_STACK_EVENT(event_code)) { + /* nothing to convey to user */ + } + if (ZTS_NETIF_EVENT(event_code)) { + msg->netif = (zts_netif_info_t*)arg; + msg->len = sizeof(zts_netif_info_t); + } + if (ZTS_ROUTE_EVENT(event_code)) { + msg->route = (zts_route_info_t*)arg; + msg->len = sizeof(zts_route_info_t); + } + if (ZTS_PEER_EVENT(event_code)) { + msg->peer = (zts_peer_info_t*)arg; + msg->len = sizeof(zts_peer_info_t); + } + if (ZTS_ADDR_EVENT(event_code)) { + msg->addr = (zts_addr_info_t*)arg; + msg->len = sizeof(zts_addr_info_t); + } + if (ZTS_STORE_EVENT(event_code)) { + msg->cache = (void*)arg; + msg->len = len; + } + if (msg && _callbackMsgQueue.size_approx() > 1024) { + /* Rate-limit number of events. This value should only grow if the + user application isn't returning from the event handler in a timely manner. + For most applications it should hover around 1 to 2 */ + destroy(msg); + } + else { + _callbackMsgQueue.enqueue(msg); + } } void Events::destroy(zts_event_msg_t* msg) { - if (! msg) { - return; - } - if (msg->node) { - delete msg->node; - } - if (msg->network) { - delete msg->network; - } - if (msg->netif) { - delete msg->netif; - } - if (msg->route) { - delete msg->route; - } - if (msg->peer) { - delete msg->peer; - } - if (msg->addr) { - delete msg->addr; - } - delete msg; - msg = NULL; + if (! msg) { + return; + } + if (msg->node) { + delete msg->node; + } + if (msg->network) { + delete msg->network; + } + if (msg->netif) { + delete msg->netif; + } + if (msg->route) { + delete msg->route; + } + if (msg->peer) { + delete msg->peer; + } + if (msg->addr) { + delete msg->addr; + } + delete msg; + msg = NULL; } void Events::sendToUser(zts_event_msg_t* msg) { - bool bShouldStopCallbackThread = (msg->event_code == ZTS_EVENT_STACK_DOWN); + bool bShouldStopCallbackThread = (msg->event_code == ZTS_EVENT_STACK_DOWN); #ifdef ZTS_ENABLE_PYTHON - PyGILState_STATE state = PyGILState_Ensure(); - _userEventCallback->on_zerotier_event(msg); - PyGILState_Release(state); + PyGILState_STATE state = PyGILState_Ensure(); + _userEventCallback->on_zerotier_event(msg); + PyGILState_Release(state); #endif #ifdef ZTS_ENABLE_JAVA - if (_userCallbackMethodRef) { - JNIEnv* env; + if (_userCallbackMethodRef) { + JNIEnv* env; #if defined(__ANDROID__) - jint rs = jvm->AttachCurrentThread(&env, NULL); + jint rs = jvm->AttachCurrentThread(&env, NULL); #else - jint rs = jvm->AttachCurrentThread((void**)&env, NULL); + jint rs = jvm->AttachCurrentThread((void**)&env, NULL); #endif - uint64_t arg = 0; - uint64_t id = 0; - if (ZTS_NODE_EVENT(msg->event_code)) { - id = msg->node ? msg->node->address : 0; - } - if (ZTS_NETWORK_EVENT(msg->event_code)) { - id = msg->network ? msg->network->nwid : 0; - } - if (ZTS_PEER_EVENT(msg->event_code)) { - id = msg->peer ? msg->peer->address : 0; - } - env->CallVoidMethod(objRef, _userCallbackMethodRef, id, msg->event_code); - } + uint64_t arg = 0; + uint64_t id = 0; + if (ZTS_NODE_EVENT(msg->event_code)) { + id = msg->node ? msg->node->address : 0; + } + if (ZTS_NETWORK_EVENT(msg->event_code)) { + id = msg->network ? msg->network->nwid : 0; + } + if (ZTS_PEER_EVENT(msg->event_code)) { + id = msg->peer ? msg->peer->address : 0; + } + env->CallVoidMethod(objRef, _userCallbackMethodRef, id, msg->event_code); + } #endif // ZTS_ENABLE_JAVA #ifdef ZTS_ENABLE_PINVOKE - if (_userEventCallback) { - _userEventCallback(msg); - } + if (_userEventCallback) { + _userEventCallback(msg); + } #endif #ifdef ZTS_C_API_ONLY - if (_userEventCallback) { - _userEventCallback(msg); - } + if (_userEventCallback) { + _userEventCallback(msg); + } #endif - destroy(msg); - if (bShouldStopCallbackThread) { - /* Ensure last possible callback ZTS_EVENT_STACK_DOWN is - delivered before callback thread is finally stopped. */ - clrState(ZTS_STATE_CALLBACKS_RUNNING); - } + destroy(msg); + if (bShouldStopCallbackThread) { + /* Ensure last possible callback ZTS_EVENT_STACK_DOWN is + delivered before callback thread is finally stopped. */ + clrState(ZTS_STATE_CALLBACKS_RUNNING); + } } bool Events::hasCallback() { - events_m.lock(); - bool retval = false; + events_m.lock(); + bool retval = false; #ifdef ZTS_ENABLE_JAVA - retval = (jvm && objRef && _userCallbackMethodRef); + retval = (jvm && objRef && _userCallbackMethodRef); #else - retval = _userEventCallback; + retval = _userEventCallback; #endif - events_m.unlock(); - return retval; + events_m.unlock(); + return retval; } void Events::clrCallback() { - events_m.lock(); + events_m.lock(); #ifdef ZTS_ENABLE_JAVA - objRef = NULL; - _userCallbackMethodRef = NULL; + objRef = NULL; + _userCallbackMethodRef = NULL; #else - _userEventCallback = NULL; + _userEventCallback = NULL; #endif - events_m.unlock(); + events_m.unlock(); } int Events::canPerformServiceOperation() { - return zts_service && zts_service->isRunning() && ! getState(ZTS_STATE_FREE_CALLED); + return zts_service && zts_service->isRunning() && ! getState(ZTS_STATE_FREE_CALLED); } void Events::setState(uint8_t newFlags) { - if ((newFlags ^ service_state) & ZTS_STATE_NET_SERVICE_RUNNING) { - return; // No effect. Not allowed to set this flag manually - } - SET_FLAGS(newFlags); - if (GET_FLAGS(ZTS_STATE_NODE_RUNNING) && GET_FLAGS(ZTS_STATE_STACK_RUNNING) - && ! (GET_FLAGS(ZTS_STATE_FREE_CALLED))) { - SET_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING); - } - else { - CLR_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING); - } + if ((newFlags ^ service_state) & ZTS_STATE_NET_SERVICE_RUNNING) { + return; // No effect. Not allowed to set this flag manually + } + SET_FLAGS(newFlags); + if (GET_FLAGS(ZTS_STATE_NODE_RUNNING) && GET_FLAGS(ZTS_STATE_STACK_RUNNING) + && ! (GET_FLAGS(ZTS_STATE_FREE_CALLED))) { + SET_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING); + } + else { + CLR_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING); + } } void Events::clrState(uint8_t newFlags) { - if (newFlags & ZTS_STATE_NET_SERVICE_RUNNING) { - return; // No effect. Not allowed to set this flag manually - } - CLR_FLAGS(newFlags); - if (GET_FLAGS(ZTS_STATE_NODE_RUNNING) && GET_FLAGS(ZTS_STATE_STACK_RUNNING) - && ! (GET_FLAGS(ZTS_STATE_FREE_CALLED))) { - SET_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING); - } - else { - CLR_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING); - } + if (newFlags & ZTS_STATE_NET_SERVICE_RUNNING) { + return; // No effect. Not allowed to set this flag manually + } + CLR_FLAGS(newFlags); + if (GET_FLAGS(ZTS_STATE_NODE_RUNNING) && GET_FLAGS(ZTS_STATE_STACK_RUNNING) + && ! (GET_FLAGS(ZTS_STATE_FREE_CALLED))) { + SET_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING); + } + else { + CLR_FLAGS(ZTS_STATE_NET_SERVICE_RUNNING); + } } bool Events::getState(uint8_t testFlags) { - return testFlags & service_state; + return testFlags & service_state; } void Events::enable() { - _enabled = true; + _enabled = true; } void Events::disable() { - _enabled = false; + _enabled = false; } } // namespace ZeroTier diff --git a/src/Events.hpp b/src/Events.hpp index 0ed21f9..c22d2bd 100644 --- a/src/Events.hpp +++ b/src/Events.hpp @@ -34,33 +34,33 @@ // Lock service and check that it is running #define ACQUIRE_SERVICE(x) \ - Mutex::Lock _ls(service_m); \ - if (! zts_service || ! zts_service->isRunning()) { \ - return x; \ - } + Mutex::Lock _ls(service_m); \ + if (! zts_service || ! zts_service->isRunning()) { \ + return x; \ + } // Lock service and check that it is not currently running #define ACQUIRE_SERVICE_OFFLINE() \ - Mutex::Lock _ls(service_m); \ - if (zts_service && zts_service->isRunning()) { \ - return ZTS_ERR_SERVICE; \ - } \ - if (! zts_service) { \ - init_subsystems(); \ - } + Mutex::Lock _ls(service_m); \ + if (zts_service && zts_service->isRunning()) { \ + return ZTS_ERR_SERVICE; \ + } \ + if (! zts_service) { \ + init_subsystems(); \ + } // Unlock service #define RELEASE_SERVICE() service_m.unlock(); // Lock service, ensure node is online #define ACQUIRE_ONLINE_NODE() \ - ACQUIRE_SERVICE() if (! zts_service->nodeIsOnline()) \ - { \ - return ZTS_ERR_SERVICE; \ - } + ACQUIRE_SERVICE() if (! zts_service->nodeIsOnline()) \ + { \ + return ZTS_ERR_SERVICE; \ + } // Lock event callback #define ACQUIRE_EVENTS() \ - Mutex::Lock _lc(events_m); \ - if (! zts_events) { \ - return ZTS_ERR_SERVICE; \ - } + Mutex::Lock _lc(events_m); \ + if (! zts_events) { \ + return ZTS_ERR_SERVICE; \ + } namespace ZeroTier { @@ -83,8 +83,8 @@ extern int last_state_check; inline int transport_ok() { - last_state_check = service_state & ZTS_STATE_NET_SERVICE_RUNNING; - return last_state_check; + last_state_check = service_state & ZTS_STATE_NET_SERVICE_RUNNING; + return last_state_check; } /** @@ -93,72 +93,72 @@ inline int transport_ok() #define ZTS_CALLBACK_PROCESSING_INTERVAL 25 class Events { - bool _enabled; + bool _enabled; public: - Events() : _enabled(false) - { - } + Events() : _enabled(false) + { + } - /** - * Perform one iteration of callback processing - */ - void run(); + /** + * Perform one iteration of callback processing + */ + void run(); - /** - * Enable callback event processing - */ - void enable(); + /** + * Enable callback event processing + */ + void enable(); - /** - * Disable callback event processing - */ - void disable(); + /** + * Disable callback event processing + */ + void disable(); - /** - * Enqueue an event to be sent to the user application - */ - void enqueue(unsigned int event_code, const void* arg, int len = 0); + /** + * Enqueue an event to be sent to the user application + */ + void enqueue(unsigned int event_code, const void* arg, int len = 0); - /** - * Send callback message to user application - */ - void sendToUser(zts_event_msg_t* msg); + /** + * Send callback message to user application + */ + void sendToUser(zts_event_msg_t* msg); - /** - * Free memory occupied by callback structures - */ - void destroy(zts_event_msg_t* msg); + /** + * Free memory occupied by callback structures + */ + void destroy(zts_event_msg_t* msg); - /** - * Return whether a callback method has been set - */ - bool hasCallback(); + /** + * Return whether a callback method has been set + */ + bool hasCallback(); - /** - * Clear pointer reference to user-provided callback function - */ - void clrCallback(); + /** + * Clear pointer reference to user-provided callback function + */ + void clrCallback(); - /** - * Return whether service operation can be performed at this time - */ - int canPerformServiceOperation(); + /** + * Return whether service operation can be performed at this time + */ + int canPerformServiceOperation(); - /** - * Set internal state flags - */ - void setState(uint8_t newFlags); + /** + * Set internal state flags + */ + void setState(uint8_t newFlags); - /** - * Clear internal state flags - */ - void clrState(uint8_t newFlags); + /** + * Clear internal state flags + */ + void clrState(uint8_t newFlags); - /** - * Get internal state flags - */ - bool getState(uint8_t testFlags); + /** + * Get internal state flags + */ + bool getState(uint8_t testFlags); }; } // namespace ZeroTier diff --git a/src/NodeService.cpp b/src/NodeService.cpp index 6f2cac7..dc4156a 100644 --- a/src/NodeService.cpp +++ b/src/NodeService.cpp @@ -73,388 +73,388 @@ NodeService::NodeService() NodeService::~NodeService() { - _binder.closeAll(_phy); + _binder.closeAll(_phy); #ifdef ZT_USE_MINIUPNPC - delete _portMapper; + delete _portMapper; #endif } NodeService::ReasonForTermination NodeService::run() { - _run = true; - try { - // Create home path (if necessary) - // By default, _homePath is empty and nothing is written to storage - if (_homePath.length() > 0) { - std::vector hpsp(OSUtils::split(_homePath.c_str(), ZT_PATH_SEPARATOR_S, "", "")); - std::string ptmp; - if (_homePath[0] == ZT_PATH_SEPARATOR) { - ptmp.push_back(ZT_PATH_SEPARATOR); - } - for (std::vector::iterator pi(hpsp.begin()); pi != hpsp.end(); ++pi) { - if (ptmp.length() > 0) { - ptmp.push_back(ZT_PATH_SEPARATOR); - } - ptmp.append(*pi); - if ((*pi != ".") && (*pi != "..")) { - if (OSUtils::mkdir(ptmp) == false) { - Mutex::Lock _l(_termReason_m); - _termReason = ONE_UNRECOVERABLE_ERROR; - _fatalErrorMessage = "home path could not be created"; - return _termReason; - } - } - } - } + _run = true; + try { + // Create home path (if necessary) + // By default, _homePath is empty and nothing is written to storage + if (_homePath.length() > 0) { + std::vector hpsp(OSUtils::split(_homePath.c_str(), ZT_PATH_SEPARATOR_S, "", "")); + std::string ptmp; + if (_homePath[0] == ZT_PATH_SEPARATOR) { + ptmp.push_back(ZT_PATH_SEPARATOR); + } + for (std::vector::iterator pi(hpsp.begin()); pi != hpsp.end(); ++pi) { + if (ptmp.length() > 0) { + ptmp.push_back(ZT_PATH_SEPARATOR); + } + ptmp.append(*pi); + if ((*pi != ".") && (*pi != "..")) { + if (OSUtils::mkdir(ptmp) == false) { + Mutex::Lock _l(_termReason_m); + _termReason = ONE_UNRECOVERABLE_ERROR; + _fatalErrorMessage = "home path could not be created"; + return _termReason; + } + } + } + } - // Set callbacks for ZT Node - { - struct ZT_Node_Callbacks cb; - cb.version = 0; - cb.stateGetFunction = SnodeStateGetFunction; - cb.statePutFunction = SnodeStatePutFunction; - cb.wirePacketSendFunction = SnodeWirePacketSendFunction; - cb.virtualNetworkFrameFunction = SnodeVirtualNetworkFrameFunction; - cb.virtualNetworkConfigFunction = SnodeVirtualNetworkConfigFunction; - cb.eventCallback = SnodeEventCallback; - cb.pathCheckFunction = SnodePathCheckFunction; - cb.pathLookupFunction = SnodePathLookupFunction; - _node = new Node(this, (void*)0, &cb, OSUtils::now()); - } + // Set callbacks for ZT Node + { + struct ZT_Node_Callbacks cb; + cb.version = 0; + cb.stateGetFunction = SnodeStateGetFunction; + cb.statePutFunction = SnodeStatePutFunction; + cb.wirePacketSendFunction = SnodeWirePacketSendFunction; + cb.virtualNetworkFrameFunction = SnodeVirtualNetworkFrameFunction; + cb.virtualNetworkConfigFunction = SnodeVirtualNetworkConfigFunction; + cb.eventCallback = SnodeEventCallback; + cb.pathCheckFunction = SnodePathCheckFunction; + cb.pathLookupFunction = SnodePathLookupFunction; + _node = new Node(this, (void*)0, &cb, OSUtils::now()); + } - // Make sure we can use the primary port, and hunt for one if - // configured to do so - const int portTrials = (_primaryPort == 0) ? 256 : 1; // if port is 0, pick random - for (int k = 0; k < portTrials; ++k) { - if (_primaryPort == 0) { - unsigned int randp = 0; - Utils::getSecureRandom(&randp, sizeof(randp)); - _primaryPort = 20000 + (randp % 45500); - } - if (_trialBind(_primaryPort)) { - _ports[0] = _primaryPort; - } - else { - _primaryPort = 0; - } - } - if (_ports[0] == 0) { - Mutex::Lock _l(_termReason_m); - _termReason = ONE_UNRECOVERABLE_ERROR; - _fatalErrorMessage = "cannot bind to local control interface port"; - return _termReason; - } + // Make sure we can use the primary port, and hunt for one if + // configured to do so + const int portTrials = (_primaryPort == 0) ? 256 : 1; // if port is 0, pick random + for (int k = 0; k < portTrials; ++k) { + if (_primaryPort == 0) { + unsigned int randp = 0; + Utils::getSecureRandom(&randp, sizeof(randp)); + _primaryPort = 20000 + (randp % 45500); + } + if (_trialBind(_primaryPort)) { + _ports[0] = _primaryPort; + } + else { + _primaryPort = 0; + } + } + if (_ports[0] == 0) { + Mutex::Lock _l(_termReason_m); + _termReason = ONE_UNRECOVERABLE_ERROR; + _fatalErrorMessage = "cannot bind to local control interface port"; + return _termReason; + } - // Attempt to bind to a secondary port chosen from our ZeroTier - // address. This exists because there are buggy NATs out there that - // fail if more than one device behind the same NAT tries to use the - // same internal private address port number. Buggy NATs are a - // running theme. - _ports[1] = (_secondaryPort == 0) ? 20000 + ((unsigned int)_node->address() % 45500) : _secondaryPort; - for (int i = 0;; ++i) { - if (i > 1000) { - _ports[1] = 0; - break; - } - else if (++_ports[1] >= 65536) { - _ports[1] = 20000; - } - if (_trialBind(_ports[1])) { - break; - } - } + // Attempt to bind to a secondary port chosen from our ZeroTier + // address. This exists because there are buggy NATs out there that + // fail if more than one device behind the same NAT tries to use the + // same internal private address port number. Buggy NATs are a + // running theme. + _ports[1] = (_secondaryPort == 0) ? 20000 + ((unsigned int)_node->address() % 45500) : _secondaryPort; + for (int i = 0;; ++i) { + if (i > 1000) { + _ports[1] = 0; + break; + } + else if (++_ports[1] >= 65536) { + _ports[1] = 20000; + } + if (_trialBind(_ports[1])) { + break; + } + } #ifdef ZT_USE_MINIUPNPC - if (_portMappingEnabled) { - // If we're running uPnP/NAT-PMP, bind a *third* port for that. - // We can't use the other two ports for that because some NATs - // do really funky stuff with ports that are explicitly mapped - // that breaks things. - if (_ports[1]) { - _ports[2] = (_tertiaryPort == 0) ? _ports[1] : _tertiaryPort; - for (int i = 0;; ++i) { - if (i > 1000) { - _ports[2] = 0; - break; - } - else if (++_ports[2] >= 65536) { - _ports[2] = 20000; - } - if (_trialBind(_ports[2])) { - break; - } - } - if (_ports[2]) { - char uniqueName[64] = { 0 }; - OSUtils::ztsnprintf( - uniqueName, - sizeof(uniqueName), - "ZeroTier/%.10llx@%u", - _node->address(), - _ports[2]); - _portMapper = new PortMapper(_ports[2], uniqueName); - } - } - } + if (_portMappingEnabled) { + // If we're running uPnP/NAT-PMP, bind a *third* port for that. + // We can't use the other two ports for that because some NATs + // do really funky stuff with ports that are explicitly mapped + // that breaks things. + if (_ports[1]) { + _ports[2] = (_tertiaryPort == 0) ? _ports[1] : _tertiaryPort; + for (int i = 0;; ++i) { + if (i > 1000) { + _ports[2] = 0; + break; + } + else if (++_ports[2] >= 65536) { + _ports[2] = 20000; + } + if (_trialBind(_ports[2])) { + break; + } + } + if (_ports[2]) { + char uniqueName[64] = { 0 }; + OSUtils::ztsnprintf( + uniqueName, + sizeof(uniqueName), + "ZeroTier/%.10llx@%u", + _node->address(), + _ports[2]); + _portMapper = new PortMapper(_ports[2], uniqueName); + } + } + } #endif - // Join existing networks in networks.d - if (_allowNetworkCaching) { - std::vector networksDotD( - OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "networks.d").c_str())); - for (std::vector::iterator f(networksDotD.begin()); f != networksDotD.end(); ++f) { - std::size_t dot = f->find_last_of('.'); - if ((dot == 16) && (f->substr(16) == ".conf")) { - _node->join(Utils::hexStrToU64(f->substr(0, dot).c_str()), (void*)0, (void*)0); - } - } - } - // Main I/O loop - _nextBackgroundTaskDeadline = 0; - int64_t clockShouldBe = OSUtils::now(); - _lastRestart = clockShouldBe; - int64_t lastTapMulticastGroupCheck = 0; - int64_t lastBindRefresh = 0; - int64_t lastCleanedPeersDb = 0; - int64_t lastLocalInterfaceAddressCheck = - (clockShouldBe - ZT_LOCAL_INTERFACE_CHECK_INTERVAL) + 15000; // do this in 15s to give portmapper time to - // configure and other things time to settle - for (;;) { - _run_m.lock(); - if (! _run) { - _run_m.unlock(); - _termReason_m.lock(); - _termReason = ONE_NORMAL_TERMINATION; - _termReason_m.unlock(); - break; - } - else { - _run_m.unlock(); - } + // Join existing networks in networks.d + if (_allowNetworkCaching) { + std::vector networksDotD( + OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "networks.d").c_str())); + for (std::vector::iterator f(networksDotD.begin()); f != networksDotD.end(); ++f) { + std::size_t dot = f->find_last_of('.'); + if ((dot == 16) && (f->substr(16) == ".conf")) { + _node->join(Utils::hexStrToU64(f->substr(0, dot).c_str()), (void*)0, (void*)0); + } + } + } + // Main I/O loop + _nextBackgroundTaskDeadline = 0; + int64_t clockShouldBe = OSUtils::now(); + _lastRestart = clockShouldBe; + int64_t lastTapMulticastGroupCheck = 0; + int64_t lastBindRefresh = 0; + int64_t lastCleanedPeersDb = 0; + int64_t lastLocalInterfaceAddressCheck = + (clockShouldBe - ZT_LOCAL_INTERFACE_CHECK_INTERVAL) + 15000; // do this in 15s to give portmapper time to + // configure and other things time to settle + for (;;) { + _run_m.lock(); + if (! _run) { + _run_m.unlock(); + _termReason_m.lock(); + _termReason = ONE_NORMAL_TERMINATION; + _termReason_m.unlock(); + break; + } + else { + _run_m.unlock(); + } - const int64_t now = OSUtils::now(); + const int64_t now = OSUtils::now(); - // Attempt to detect sleep/wake events by detecting delay - // overruns - bool restarted = false; - if ((now > clockShouldBe) && ((now - clockShouldBe) > 10000)) { - _lastRestart = now; - restarted = true; - } + // Attempt to detect sleep/wake events by detecting delay + // overruns + bool restarted = false; + if ((now > clockShouldBe) && ((now - clockShouldBe) > 10000)) { + _lastRestart = now; + restarted = true; + } - // Refresh bindings in case device's interfaces have changed, - // and also sync routes to update any shadow routes (e.g. shadow - // default) - if (((now - lastBindRefresh) >= ZT_BINDER_REFRESH_PERIOD) || (restarted)) { - lastBindRefresh = now; - unsigned int p[3] = { 0 }; - unsigned int pc = 0; - for (int i = 0; i < 3; ++i) { - if (_ports[i]) { - p[pc++] = _ports[i]; - } - } - _binder.refresh(_phy, p, pc, explicitBind, *this); - } + // Refresh bindings in case device's interfaces have changed, + // and also sync routes to update any shadow routes (e.g. shadow + // default) + if (((now - lastBindRefresh) >= ZT_BINDER_REFRESH_PERIOD) || (restarted)) { + lastBindRefresh = now; + unsigned int p[3] = { 0 }; + unsigned int pc = 0; + for (int i = 0; i < 3; ++i) { + if (_ports[i]) { + p[pc++] = _ports[i]; + } + } + _binder.refresh(_phy, p, pc, explicitBind, *this); + } - // Generate callback messages for user application - generateEventMsgs(); + // Generate callback messages for user application + generateEventMsgs(); - // Run background task processor in core if it's time to do so - int64_t dl = _nextBackgroundTaskDeadline; - if (dl <= now) { - _node->processBackgroundTasks((void*)0, now, &_nextBackgroundTaskDeadline); - dl = _nextBackgroundTaskDeadline; - } + // Run background task processor in core if it's time to do so + int64_t dl = _nextBackgroundTaskDeadline; + if (dl <= now) { + _node->processBackgroundTasks((void*)0, now, &_nextBackgroundTaskDeadline); + dl = _nextBackgroundTaskDeadline; + } - // Sync multicast group memberships - if ((now - lastTapMulticastGroupCheck) >= ZT_TAP_CHECK_MULTICAST_INTERVAL) { - lastTapMulticastGroupCheck = now; - std::vector, std::vector > > > - mgChanges; - { - Mutex::Lock _l(_nets_m); - mgChanges.reserve(_nets.size() + 1); - for (std::map::const_iterator n(_nets.begin()); n != _nets.end(); ++n) { - if (n->second.tap) { - mgChanges.push_back(std::pair< - uint64_t, - std::pair, std::vector > >( - n->first, - std::pair, std::vector >())); - n->second.tap->scanMulticastGroups( - mgChanges.back().second.first, - mgChanges.back().second.second); - } - } - } - for (std::vector< - std::pair, std::vector > > >:: - iterator c(mgChanges.begin()); - c != mgChanges.end(); - ++c) { - auto mgpair = c->second; - for (std::vector::iterator m(mgpair.first.begin()); m != mgpair.first.end(); ++m) - _node->multicastSubscribe((void*)0, c->first, m->mac().toInt(), m->adi()); - for (std::vector::iterator m(mgpair.second.begin()); m != mgpair.second.end(); ++m) - _node->multicastUnsubscribe(c->first, m->mac().toInt(), m->adi()); - } - } + // Sync multicast group memberships + if ((now - lastTapMulticastGroupCheck) >= ZT_TAP_CHECK_MULTICAST_INTERVAL) { + lastTapMulticastGroupCheck = now; + std::vector, std::vector > > > + mgChanges; + { + Mutex::Lock _l(_nets_m); + mgChanges.reserve(_nets.size() + 1); + for (std::map::const_iterator n(_nets.begin()); n != _nets.end(); ++n) { + if (n->second.tap) { + mgChanges.push_back(std::pair< + uint64_t, + std::pair, std::vector > >( + n->first, + std::pair, std::vector >())); + n->second.tap->scanMulticastGroups( + mgChanges.back().second.first, + mgChanges.back().second.second); + } + } + } + for (std::vector< + std::pair, std::vector > > >:: + iterator c(mgChanges.begin()); + c != mgChanges.end(); + ++c) { + auto mgpair = c->second; + for (std::vector::iterator m(mgpair.first.begin()); m != mgpair.first.end(); ++m) + _node->multicastSubscribe((void*)0, c->first, m->mac().toInt(), m->adi()); + for (std::vector::iterator m(mgpair.second.begin()); m != mgpair.second.end(); ++m) + _node->multicastUnsubscribe(c->first, m->mac().toInt(), m->adi()); + } + } - // Sync information about physical network interfaces - if ((now - lastLocalInterfaceAddressCheck) >= ZT_LOCAL_INTERFACE_CHECK_INTERVAL) { - lastLocalInterfaceAddressCheck = now; + // Sync information about physical network interfaces + if ((now - lastLocalInterfaceAddressCheck) >= ZT_LOCAL_INTERFACE_CHECK_INTERVAL) { + lastLocalInterfaceAddressCheck = now; - _node->clearLocalInterfaceAddresses(); + _node->clearLocalInterfaceAddresses(); #ifdef ZT_USE_MINIUPNPC - if (_portMapper) { - std::vector mappedAddresses(_portMapper->get()); - for (std::vector::const_iterator ext(mappedAddresses.begin()); - ext != mappedAddresses.end(); - ++ext) - _node->addLocalInterfaceAddress(reinterpret_cast(&(*ext))); - } + if (_portMapper) { + std::vector mappedAddresses(_portMapper->get()); + for (std::vector::const_iterator ext(mappedAddresses.begin()); + ext != mappedAddresses.end(); + ++ext) + _node->addLocalInterfaceAddress(reinterpret_cast(&(*ext))); + } #endif - std::vector boundAddrs(_binder.allBoundLocalInterfaceAddresses()); - for (std::vector::const_iterator i(boundAddrs.begin()); i != boundAddrs.end(); ++i) - _node->addLocalInterfaceAddress(reinterpret_cast(&(*i))); - } + std::vector boundAddrs(_binder.allBoundLocalInterfaceAddresses()); + for (std::vector::const_iterator i(boundAddrs.begin()); i != boundAddrs.end(); ++i) + _node->addLocalInterfaceAddress(reinterpret_cast(&(*i))); + } - // Clean peers.d periodically - if ((now - lastCleanedPeersDb) >= 3600000) { - lastCleanedPeersDb = now; - OSUtils::cleanDirectory( - (_homePath + ZT_PATH_SEPARATOR_S "peers.d").c_str(), - now - 2592000000LL); // delete older than 30 days - } + // Clean peers.d periodically + if ((now - lastCleanedPeersDb) >= 3600000) { + lastCleanedPeersDb = now; + OSUtils::cleanDirectory( + (_homePath + ZT_PATH_SEPARATOR_S "peers.d").c_str(), + now - 2592000000LL); // delete older than 30 days + } - const unsigned long delay = (dl > now) ? (unsigned long)(dl - now) : 100; - clockShouldBe = now + (uint64_t)delay; - _phy.poll(delay); - } - } - catch (std::exception& e) { - Mutex::Lock _l(_termReason_m); - _termReason = ONE_UNRECOVERABLE_ERROR; - _fatalErrorMessage = std::string("unexpected exception in main thread: ") + e.what(); - } - catch (...) { - Mutex::Lock _l(_termReason_m); - _termReason = ONE_UNRECOVERABLE_ERROR; - _fatalErrorMessage = "unexpected exception in main thread: unknown exception"; - } + const unsigned long delay = (dl > now) ? (unsigned long)(dl - now) : 100; + clockShouldBe = now + (uint64_t)delay; + _phy.poll(delay); + } + } + catch (std::exception& e) { + Mutex::Lock _l(_termReason_m); + _termReason = ONE_UNRECOVERABLE_ERROR; + _fatalErrorMessage = std::string("unexpected exception in main thread: ") + e.what(); + } + catch (...) { + Mutex::Lock _l(_termReason_m); + _termReason = ONE_UNRECOVERABLE_ERROR; + _fatalErrorMessage = "unexpected exception in main thread: unknown exception"; + } - { - Mutex::Lock _l(_nets_m); - for (std::map::iterator n(_nets.begin()); n != _nets.end(); ++n) - delete n->second.tap; - _nets.clear(); - } + { + Mutex::Lock _l(_nets_m); + for (std::map::iterator n(_nets.begin()); n != _nets.end(); ++n) + delete n->second.tap; + _nets.clear(); + } - switch (_termReason) { - case ONE_NORMAL_TERMINATION: - nodeEventCallback(ZT_EVENT_DOWN, NULL); - break; - case ONE_UNRECOVERABLE_ERROR: - nodeEventCallback(ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION, NULL); - break; - default: - break; - } - delete _node; - _node = (Node*)0; - return _termReason; + switch (_termReason) { + case ONE_NORMAL_TERMINATION: + nodeEventCallback(ZT_EVENT_DOWN, NULL); + break; + case ONE_UNRECOVERABLE_ERROR: + nodeEventCallback(ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION, NULL); + break; + default: + break; + } + delete _node; + _node = (Node*)0; + return _termReason; } NodeService::ReasonForTermination NodeService::reasonForTermination() const { - Mutex::Lock _l(_termReason_m); - return _termReason; + Mutex::Lock _l(_termReason_m); + return _termReason; } std::string NodeService::fatalErrorMessage() const { - Mutex::Lock _l(_termReason_m); - return _fatalErrorMessage; + Mutex::Lock _l(_termReason_m); + return _fatalErrorMessage; } void NodeService::terminate() { - _run_m.lock(); - _run = false; - _run_m.unlock(); - _primaryPort = 0; - _homePath.clear(); - _allowNetworkCaching = true; - _allowPeerCaching = true; - _allowIdentityCaching = true; - _allowWorldCaching = true; - memset(_publicIdStr, 0, ZT_IDENTITY_STRING_BUFFER_LENGTH); - memset(_secretIdStr, 0, ZT_IDENTITY_STRING_BUFFER_LENGTH); - _interfacePrefixBlacklist.clear(); - _events->disable(); - _phy.whack(); + _run_m.lock(); + _run = false; + _run_m.unlock(); + _primaryPort = 0; + _homePath.clear(); + _allowNetworkCaching = true; + _allowPeerCaching = true; + _allowIdentityCaching = true; + _allowWorldCaching = true; + memset(_publicIdStr, 0, ZT_IDENTITY_STRING_BUFFER_LENGTH); + memset(_secretIdStr, 0, ZT_IDENTITY_STRING_BUFFER_LENGTH); + _interfacePrefixBlacklist.clear(); + _events->disable(); + _phy.whack(); } void NodeService::syncManagedStuff(NetworkState& n) { - char ipbuf[64] = { 0 }; - // assumes _nets_m is locked - std::vector newManagedIps; - newManagedIps.reserve(n.config.assignedAddressCount); - for (unsigned int i = 0; i < n.config.assignedAddressCount; ++i) { - const InetAddress* ii = reinterpret_cast(&(n.config.assignedAddresses[i])); - newManagedIps.push_back(*ii); - } - std::sort(newManagedIps.begin(), newManagedIps.end()); - newManagedIps.erase(std::unique(newManagedIps.begin(), newManagedIps.end()), newManagedIps.end()); - for (std::vector::iterator ip(n.managedIps.begin()); ip != n.managedIps.end(); ++ip) { - if (std::find(newManagedIps.begin(), newManagedIps.end(), *ip) == newManagedIps.end()) { - if (! n.tap->removeIp(*ip)) { - fprintf(stderr, "ERROR: unable to remove ip address %s" ZT_EOL_S, ip->toString(ipbuf)); - } - else { - zts_addr_info_t* ad = new zts_addr_info_t(); - ad->net_id = n.tap->_net_id; - if ((*ip).isV4()) { - struct sockaddr_in* in4 = (struct sockaddr_in*)&(ad->addr); - memcpy(&(in4->sin_addr.s_addr), (*ip).rawIpData(), 4); - in4->sin_family = ZTS_AF_INET; - sendEventToUser(ZTS_EVENT_ADDR_REMOVED_IP4, (void*)ad); - } - if ((*ip).isV6()) { - struct sockaddr_in6* in6 = (struct sockaddr_in6*)&(ad->addr); - memcpy(&(in6->sin6_addr.s6_addr), (*ip).rawIpData(), 16); - in6->sin6_family = ZTS_AF_INET6; - sendEventToUser(ZTS_EVENT_ADDR_REMOVED_IP6, (void*)ad); - } - } - } - } - for (std::vector::iterator ip(newManagedIps.begin()); ip != newManagedIps.end(); ++ip) { - if (std::find(n.managedIps.begin(), n.managedIps.end(), *ip) == n.managedIps.end()) { - if (! n.tap->addIp(*ip)) { - fprintf(stderr, "ERROR: unable to add ip address %s" ZT_EOL_S, ip->toString(ipbuf)); - } - else { - zts_addr_info_t* ad = new zts_addr_info_t(); - ad->net_id = n.tap->_net_id; - if ((*ip).isV4()) { - struct sockaddr_in* in4 = (struct sockaddr_in*)&(ad->addr); - memcpy(&(in4->sin_addr.s_addr), (*ip).rawIpData(), 4); - in4->sin_family = ZTS_AF_INET; - sendEventToUser(ZTS_EVENT_ADDR_ADDED_IP4, (void*)ad); - } - if ((*ip).isV6()) { - struct sockaddr_in6* in6 = (struct sockaddr_in6*)&(ad->addr); - memcpy(&(in6->sin6_addr.s6_addr), (*ip).rawIpData(), 16); - in6->sin6_family = ZTS_AF_INET6; - sendEventToUser(ZTS_EVENT_ADDR_ADDED_IP6, (void*)ad); - } - } - } - } - n.managedIps.swap(newManagedIps); + char ipbuf[64] = { 0 }; + // assumes _nets_m is locked + std::vector newManagedIps; + newManagedIps.reserve(n.config.assignedAddressCount); + for (unsigned int i = 0; i < n.config.assignedAddressCount; ++i) { + const InetAddress* ii = reinterpret_cast(&(n.config.assignedAddresses[i])); + newManagedIps.push_back(*ii); + } + std::sort(newManagedIps.begin(), newManagedIps.end()); + newManagedIps.erase(std::unique(newManagedIps.begin(), newManagedIps.end()), newManagedIps.end()); + for (std::vector::iterator ip(n.managedIps.begin()); ip != n.managedIps.end(); ++ip) { + if (std::find(newManagedIps.begin(), newManagedIps.end(), *ip) == newManagedIps.end()) { + if (! n.tap->removeIp(*ip)) { + fprintf(stderr, "ERROR: unable to remove ip address %s" ZT_EOL_S, ip->toString(ipbuf)); + } + else { + zts_addr_info_t* ad = new zts_addr_info_t(); + ad->net_id = n.tap->_net_id; + if ((*ip).isV4()) { + struct sockaddr_in* in4 = (struct sockaddr_in*)&(ad->addr); + memcpy(&(in4->sin_addr.s_addr), (*ip).rawIpData(), 4); + in4->sin_family = ZTS_AF_INET; + sendEventToUser(ZTS_EVENT_ADDR_REMOVED_IP4, (void*)ad); + } + if ((*ip).isV6()) { + struct sockaddr_in6* in6 = (struct sockaddr_in6*)&(ad->addr); + memcpy(&(in6->sin6_addr.s6_addr), (*ip).rawIpData(), 16); + in6->sin6_family = ZTS_AF_INET6; + sendEventToUser(ZTS_EVENT_ADDR_REMOVED_IP6, (void*)ad); + } + } + } + } + for (std::vector::iterator ip(newManagedIps.begin()); ip != newManagedIps.end(); ++ip) { + if (std::find(n.managedIps.begin(), n.managedIps.end(), *ip) == n.managedIps.end()) { + if (! n.tap->addIp(*ip)) { + fprintf(stderr, "ERROR: unable to add ip address %s" ZT_EOL_S, ip->toString(ipbuf)); + } + else { + zts_addr_info_t* ad = new zts_addr_info_t(); + ad->net_id = n.tap->_net_id; + if ((*ip).isV4()) { + struct sockaddr_in* in4 = (struct sockaddr_in*)&(ad->addr); + memcpy(&(in4->sin_addr.s_addr), (*ip).rawIpData(), 4); + in4->sin_family = ZTS_AF_INET; + sendEventToUser(ZTS_EVENT_ADDR_ADDED_IP4, (void*)ad); + } + if ((*ip).isV6()) { + struct sockaddr_in6* in6 = (struct sockaddr_in6*)&(ad->addr); + memcpy(&(in6->sin6_addr.s6_addr), (*ip).rawIpData(), 16); + in6->sin6_family = ZTS_AF_INET6; + sendEventToUser(ZTS_EVENT_ADDR_ADDED_IP6, (void*)ad); + } + } + } + } + n.managedIps.swap(newManagedIps); } void NodeService::phyOnDatagram( @@ -465,25 +465,25 @@ void NodeService::phyOnDatagram( void* data, unsigned long len) { - if ((len >= 16) && (reinterpret_cast(from)->ipScope() == InetAddress::IP_SCOPE_GLOBAL)) - _lastDirectReceiveFromGlobal = OSUtils::now(); - const ZT_ResultCode rc = _node->processWirePacket( - (void*)0, - OSUtils::now(), - reinterpret_cast(sock), - reinterpret_cast(from), // Phy<> uses sockaddr_storage, so - // it'll always be that big - data, - len, - &_nextBackgroundTaskDeadline); - if (ZT_ResultCode_isFatal(rc)) { - char tmp[256] = { 0 }; - OSUtils::ztsnprintf(tmp, sizeof(tmp), "fatal error code from processWirePacket: %d", (int)rc); - Mutex::Lock _l(_termReason_m); - _termReason = ONE_UNRECOVERABLE_ERROR; - _fatalErrorMessage = tmp; - this->terminate(); - } + if ((len >= 16) && (reinterpret_cast(from)->ipScope() == InetAddress::IP_SCOPE_GLOBAL)) + _lastDirectReceiveFromGlobal = OSUtils::now(); + const ZT_ResultCode rc = _node->processWirePacket( + (void*)0, + OSUtils::now(), + reinterpret_cast(sock), + reinterpret_cast(from), // Phy<> uses sockaddr_storage, so + // it'll always be that big + data, + len, + &_nextBackgroundTaskDeadline); + if (ZT_ResultCode_isFatal(rc)) { + char tmp[256] = { 0 }; + OSUtils::ztsnprintf(tmp, sizeof(tmp), "fatal error code from processWirePacket: %d", (int)rc); + Mutex::Lock _l(_termReason_m); + _termReason = ONE_UNRECOVERABLE_ERROR; + _fatalErrorMessage = tmp; + this->terminate(); + } } int NodeService::nodeVirtualNetworkConfigFunction( @@ -492,333 +492,333 @@ int NodeService::nodeVirtualNetworkConfigFunction( enum ZT_VirtualNetworkConfigOperation op, const ZT_VirtualNetworkConfig* nwc) { - Mutex::Lock _l(_nets_m); - NetworkState& n = _nets[net_id]; + Mutex::Lock _l(_nets_m); + NetworkState& n = _nets[net_id]; - switch (op) { - case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP: - if (! n.tap) { - n.tap = new VirtualTap( - _homePath.c_str(), - MAC(nwc->mac), - nwc->mtu, - (unsigned int)ZT_IF_METRIC, - net_id, - StapFrameHandler, - (void*)this); - *nuptr = (void*)&n; - n.tap->setUserEventSystem(_events); - } - // After setting up tap, fall through to CONFIG_UPDATE since we - // also want to do this... - case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE: - memcpy(&(n.config), nwc, sizeof(ZT_VirtualNetworkConfig)); - if (n.tap) { // sanity check - syncManagedStuff(n); - n.tap->setMtu(nwc->mtu); - } - else { - _nets.erase(net_id); - return -999; // tap init failed - } - if (op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE) { - sendEventToUser(ZTS_EVENT_NETWORK_UPDATE, (void*)prepare_network_details_msg(n)); - } - break; - case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN: - case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY: - sendEventToUser(ZTS_EVENT_NETWORK_DOWN, (void*)prepare_network_details_msg(n)); - if (n.tap) { // sanity check - *nuptr = (void*)0; - delete n.tap; - _nets.erase(net_id); - if (_allowNetworkCaching) { - if (op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY) { - char nlcpath[256] = { 0 }; - OSUtils::ztsnprintf( - nlcpath, - sizeof(nlcpath), - "%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf", - _homePath.c_str(), - net_id); - OSUtils::rm(nlcpath); - } - } - } - else { - _nets.erase(net_id); - } - break; - } - return 0; + switch (op) { + case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_UP: + if (! n.tap) { + n.tap = new VirtualTap( + _homePath.c_str(), + MAC(nwc->mac), + nwc->mtu, + (unsigned int)ZT_IF_METRIC, + net_id, + StapFrameHandler, + (void*)this); + *nuptr = (void*)&n; + n.tap->setUserEventSystem(_events); + } + // After setting up tap, fall through to CONFIG_UPDATE since we + // also want to do this... + case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE: + memcpy(&(n.config), nwc, sizeof(ZT_VirtualNetworkConfig)); + if (n.tap) { // sanity check + syncManagedStuff(n); + n.tap->setMtu(nwc->mtu); + } + else { + _nets.erase(net_id); + return -999; // tap init failed + } + if (op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE) { + sendEventToUser(ZTS_EVENT_NETWORK_UPDATE, (void*)prepare_network_details_msg(n)); + } + break; + case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN: + case ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY: + sendEventToUser(ZTS_EVENT_NETWORK_DOWN, (void*)prepare_network_details_msg(n)); + if (n.tap) { // sanity check + *nuptr = (void*)0; + delete n.tap; + _nets.erase(net_id); + if (_allowNetworkCaching) { + if (op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY) { + char nlcpath[256] = { 0 }; + OSUtils::ztsnprintf( + nlcpath, + sizeof(nlcpath), + "%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf", + _homePath.c_str(), + net_id); + OSUtils::rm(nlcpath); + } + } + } + else { + _nets.erase(net_id); + } + break; + } + return 0; } void NodeService::nodeEventCallback(enum ZT_Event event, const void* metaData) { - zts_node_info_t* nd = new zts_node_info_t; - nd->node_id = _node ? _node->address() : 0x0; - nd->ver_major = ZEROTIER_ONE_VERSION_MAJOR; - nd->ver_minor = ZEROTIER_ONE_VERSION_MINOR; - nd->ver_rev = ZEROTIER_ONE_VERSION_REVISION; - nd->port_primary = _primaryPort; - nd->port_secondary = _secondaryPort; - nd->port_tertiary = _tertiaryPort; + zts_node_info_t* nd = new zts_node_info_t; + nd->node_id = _node ? _node->address() : 0x0; + nd->ver_major = ZEROTIER_ONE_VERSION_MAJOR; + nd->ver_minor = ZEROTIER_ONE_VERSION_MINOR; + nd->ver_rev = ZEROTIER_ONE_VERSION_REVISION; + nd->port_primary = _primaryPort; + nd->port_secondary = _secondaryPort; + nd->port_tertiary = _tertiaryPort; - int event_code = 0; - _nodeIsOnline = (event == ZT_EVENT_ONLINE) ? true : false; + int event_code = 0; + _nodeIsOnline = (event == ZT_EVENT_ONLINE) ? true : false; - switch (event) { - case ZT_EVENT_UP: - event_code = ZTS_EVENT_NODE_UP; - break; - case ZT_EVENT_ONLINE: - event_code = ZTS_EVENT_NODE_ONLINE; - break; - case ZT_EVENT_OFFLINE: - event_code = ZTS_EVENT_NODE_OFFLINE; - break; - case ZT_EVENT_DOWN: - event_code = ZTS_EVENT_NODE_DOWN; - break; - case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: { - Mutex::Lock _l(_termReason_m); - _termReason = ONE_IDENTITY_COLLISION; - event_code = ZTS_EVENT_NODE_FATAL_ERROR; - this->terminate(); - } break; - default: - break; - } - if (event_code) { - sendEventToUser(event_code, (void*)nd); - } + switch (event) { + case ZT_EVENT_UP: + event_code = ZTS_EVENT_NODE_UP; + break; + case ZT_EVENT_ONLINE: + event_code = ZTS_EVENT_NODE_ONLINE; + break; + case ZT_EVENT_OFFLINE: + event_code = ZTS_EVENT_NODE_OFFLINE; + break; + case ZT_EVENT_DOWN: + event_code = ZTS_EVENT_NODE_DOWN; + break; + case ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION: { + Mutex::Lock _l(_termReason_m); + _termReason = ONE_IDENTITY_COLLISION; + event_code = ZTS_EVENT_NODE_FATAL_ERROR; + this->terminate(); + } break; + default: + break; + } + if (event_code) { + sendEventToUser(event_code, (void*)nd); + } } zts_net_info_t* NodeService::prepare_network_details_msg(const NetworkState& n) { - zts_net_info_t* nd = new zts_net_info_t(); - nd->net_id = n.config.nwid; - nd->mac = n.config.mac; - strncpy(nd->name, n.config.name, sizeof(n.config.name)); - nd->status = (zts_network_status_t)n.config.status; - nd->type = (zts_net_info_type_t)n.config.type; - nd->mtu = n.config.mtu; - nd->dhcp = n.config.dhcp; - nd->bridge = n.config.bridge; - nd->broadcast_enabled = n.config.broadcastEnabled; - nd->port_error = n.config.portError; - nd->netconf_rev = n.config.netconfRevision; - // Copy and convert address structures - nd->assigned_addr_count = n.config.assignedAddressCount; - for (unsigned int i = 0; i < n.config.assignedAddressCount; i++) { - native_ss_to_zts_ss(&(nd->assigned_addrs[i]), &(n.config.assignedAddresses[i])); - } - nd->route_count = n.config.routeCount; - for (unsigned int i = 0; i < n.config.routeCount; i++) { - native_ss_to_zts_ss(&(nd->routes[i].target), &(n.config.routes[i].target)); - native_ss_to_zts_ss(&(nd->routes[i].via), &(n.config.routes[i].via)); - nd->routes[i].flags = n.config.routes[i].flags; - nd->routes[i].metric = n.config.routes[i].metric; - } - nd->multicast_sub_count = n.config.multicastSubscriptionCount; - memcpy(nd->multicast_subs, &(n.config.multicastSubscriptions), sizeof(n.config.multicastSubscriptions)); - return nd; + zts_net_info_t* nd = new zts_net_info_t(); + nd->net_id = n.config.nwid; + nd->mac = n.config.mac; + strncpy(nd->name, n.config.name, sizeof(n.config.name)); + nd->status = (zts_network_status_t)n.config.status; + nd->type = (zts_net_info_type_t)n.config.type; + nd->mtu = n.config.mtu; + nd->dhcp = n.config.dhcp; + nd->bridge = n.config.bridge; + nd->broadcast_enabled = n.config.broadcastEnabled; + nd->port_error = n.config.portError; + nd->netconf_rev = n.config.netconfRevision; + // Copy and convert address structures + nd->assigned_addr_count = n.config.assignedAddressCount; + for (unsigned int i = 0; i < n.config.assignedAddressCount; i++) { + native_ss_to_zts_ss(&(nd->assigned_addrs[i]), &(n.config.assignedAddresses[i])); + } + nd->route_count = n.config.routeCount; + for (unsigned int i = 0; i < n.config.routeCount; i++) { + native_ss_to_zts_ss(&(nd->routes[i].target), &(n.config.routes[i].target)); + native_ss_to_zts_ss(&(nd->routes[i].via), &(n.config.routes[i].via)); + nd->routes[i].flags = n.config.routes[i].flags; + nd->routes[i].metric = n.config.routes[i].metric; + } + nd->multicast_sub_count = n.config.multicastSubscriptionCount; + memcpy(nd->multicast_subs, &(n.config.multicastSubscriptions), sizeof(n.config.multicastSubscriptions)); + return nd; } void NodeService::generateEventMsgs() { - // Force the ordering of callback messages, these messages are - // only useful if the node and stack are both up and running - if (! _node->online() || ! _lwip_is_up()) { - return; - } - // Generate messages to be dequeued by the callback message thread - Mutex::Lock _l(_nets_m); - for (std::map::iterator n(_nets.begin()); n != _nets.end(); ++n) { - auto netState = n->second; - int mostRecentStatus = netState.config.status; - VirtualTap* tap = netState.tap; - // uint64_t net_id = n->first; - if (netState.tap->_networkStatus == mostRecentStatus) { - continue; // No state change - } - switch (mostRecentStatus) { - case ZT_NETWORK_STATUS_NOT_FOUND: - sendEventToUser(ZTS_EVENT_NETWORK_NOT_FOUND, (void*)prepare_network_details_msg(netState)); - break; - case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: - sendEventToUser(ZTS_EVENT_NETWORK_CLIENT_TOO_OLD, (void*)prepare_network_details_msg(netState)); - break; - case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: - sendEventToUser(ZTS_EVENT_NETWORK_REQ_CONFIG, (void*)prepare_network_details_msg(netState)); - break; - case ZT_NETWORK_STATUS_OK: - if (tap->hasIpv4Addr() && _lwip_is_netif_up(tap->netif4)) { - sendEventToUser(ZTS_EVENT_NETWORK_READY_IP4, (void*)prepare_network_details_msg(netState)); - } - if (tap->hasIpv6Addr() && _lwip_is_netif_up(tap->netif6)) { - sendEventToUser(ZTS_EVENT_NETWORK_READY_IP6, (void*)prepare_network_details_msg(netState)); - } - // In addition to the READY messages, send one OK message - sendEventToUser(ZTS_EVENT_NETWORK_OK, (void*)prepare_network_details_msg(netState)); - break; - case ZT_NETWORK_STATUS_ACCESS_DENIED: - sendEventToUser(ZTS_EVENT_NETWORK_ACCESS_DENIED, (void*)prepare_network_details_msg(netState)); - break; - default: - break; - } - netState.tap->_networkStatus = mostRecentStatus; - } - bool bShouldCopyPeerInfo = false; - int event_code = 0; - ZT_PeerList* pl = _node->peers(); - zts_peer_info_t* pd; - if (pl) { - for (unsigned long i = 0; i < pl->peerCount; ++i) { - if (! peerCache.count(pl->peers[i].address)) { - // New peer, add status - if (pl->peers[i].pathCount > 0) { - bShouldCopyPeerInfo = true; - event_code = ZTS_EVENT_PEER_DIRECT; - } - if (pl->peers[i].pathCount == 0) { - bShouldCopyPeerInfo = true; - event_code = ZTS_EVENT_PEER_RELAY; - } - } - // Previously known peer, update status - else { - if (peerCache[pl->peers[i].address] < pl->peers[i].pathCount) { - bShouldCopyPeerInfo = true; - event_code = ZTS_EVENT_PEER_PATH_DISCOVERED; - } - if (peerCache[pl->peers[i].address] > pl->peers[i].pathCount) { - bShouldCopyPeerInfo = true; - event_code = ZTS_EVENT_PEER_PATH_DEAD; - } - if (peerCache[pl->peers[i].address] == 0 && pl->peers[i].pathCount > 0) { - bShouldCopyPeerInfo = true; - event_code = ZTS_EVENT_PEER_DIRECT; - } - if (peerCache[pl->peers[i].address] > 0 && pl->peers[i].pathCount == 0) { - bShouldCopyPeerInfo = true; - event_code = ZTS_EVENT_PEER_RELAY; - } - } - if (bShouldCopyPeerInfo) { - pd = new zts_peer_info_t(); - memcpy(pd, &(pl->peers[i]), sizeof(zts_peer_info_t)); - for (unsigned int j = 0; j < pl->peers[i].pathCount; j++) { - native_ss_to_zts_ss(&(pd->paths[j].address), &(pl->peers[i].paths[j].address)); - } - sendEventToUser(event_code, (void*)pd); - bShouldCopyPeerInfo = false; - } - // Update our cache with most recently observed path count - peerCache[pl->peers[i].address] = pl->peers[i].pathCount; - } - } - _node->freeQueryResult((void*)pl); + // Force the ordering of callback messages, these messages are + // only useful if the node and stack are both up and running + if (! _node->online() || ! _lwip_is_up()) { + return; + } + // Generate messages to be dequeued by the callback message thread + Mutex::Lock _l(_nets_m); + for (std::map::iterator n(_nets.begin()); n != _nets.end(); ++n) { + auto netState = n->second; + int mostRecentStatus = netState.config.status; + VirtualTap* tap = netState.tap; + // uint64_t net_id = n->first; + if (netState.tap->_networkStatus == mostRecentStatus) { + continue; // No state change + } + switch (mostRecentStatus) { + case ZT_NETWORK_STATUS_NOT_FOUND: + sendEventToUser(ZTS_EVENT_NETWORK_NOT_FOUND, (void*)prepare_network_details_msg(netState)); + break; + case ZT_NETWORK_STATUS_CLIENT_TOO_OLD: + sendEventToUser(ZTS_EVENT_NETWORK_CLIENT_TOO_OLD, (void*)prepare_network_details_msg(netState)); + break; + case ZT_NETWORK_STATUS_REQUESTING_CONFIGURATION: + sendEventToUser(ZTS_EVENT_NETWORK_REQ_CONFIG, (void*)prepare_network_details_msg(netState)); + break; + case ZT_NETWORK_STATUS_OK: + if (tap->hasIpv4Addr() && _lwip_is_netif_up(tap->netif4)) { + sendEventToUser(ZTS_EVENT_NETWORK_READY_IP4, (void*)prepare_network_details_msg(netState)); + } + if (tap->hasIpv6Addr() && _lwip_is_netif_up(tap->netif6)) { + sendEventToUser(ZTS_EVENT_NETWORK_READY_IP6, (void*)prepare_network_details_msg(netState)); + } + // In addition to the READY messages, send one OK message + sendEventToUser(ZTS_EVENT_NETWORK_OK, (void*)prepare_network_details_msg(netState)); + break; + case ZT_NETWORK_STATUS_ACCESS_DENIED: + sendEventToUser(ZTS_EVENT_NETWORK_ACCESS_DENIED, (void*)prepare_network_details_msg(netState)); + break; + default: + break; + } + netState.tap->_networkStatus = mostRecentStatus; + } + bool bShouldCopyPeerInfo = false; + int event_code = 0; + ZT_PeerList* pl = _node->peers(); + zts_peer_info_t* pd; + if (pl) { + for (unsigned long i = 0; i < pl->peerCount; ++i) { + if (! peerCache.count(pl->peers[i].address)) { + // New peer, add status + if (pl->peers[i].pathCount > 0) { + bShouldCopyPeerInfo = true; + event_code = ZTS_EVENT_PEER_DIRECT; + } + if (pl->peers[i].pathCount == 0) { + bShouldCopyPeerInfo = true; + event_code = ZTS_EVENT_PEER_RELAY; + } + } + // Previously known peer, update status + else { + if (peerCache[pl->peers[i].address] < pl->peers[i].pathCount) { + bShouldCopyPeerInfo = true; + event_code = ZTS_EVENT_PEER_PATH_DISCOVERED; + } + if (peerCache[pl->peers[i].address] > pl->peers[i].pathCount) { + bShouldCopyPeerInfo = true; + event_code = ZTS_EVENT_PEER_PATH_DEAD; + } + if (peerCache[pl->peers[i].address] == 0 && pl->peers[i].pathCount > 0) { + bShouldCopyPeerInfo = true; + event_code = ZTS_EVENT_PEER_DIRECT; + } + if (peerCache[pl->peers[i].address] > 0 && pl->peers[i].pathCount == 0) { + bShouldCopyPeerInfo = true; + event_code = ZTS_EVENT_PEER_RELAY; + } + } + if (bShouldCopyPeerInfo) { + pd = new zts_peer_info_t(); + memcpy(pd, &(pl->peers[i]), sizeof(zts_peer_info_t)); + for (unsigned int j = 0; j < pl->peers[i].pathCount; j++) { + native_ss_to_zts_ss(&(pd->paths[j].address), &(pl->peers[i].paths[j].address)); + } + sendEventToUser(event_code, (void*)pd); + bShouldCopyPeerInfo = false; + } + // Update our cache with most recently observed path count + peerCache[pl->peers[i].address] = pl->peers[i].pathCount; + } + } + _node->freeQueryResult((void*)pl); } int NodeService::join(uint64_t net_id) { - if (! net_id) { - return ZTS_ERR_ARG; - } - _node->join(net_id, NULL, NULL); - return ZTS_ERR_OK; + if (! net_id) { + return ZTS_ERR_ARG; + } + _node->join(net_id, NULL, NULL); + return ZTS_ERR_OK; } int NodeService::leave(uint64_t net_id) { - if (! net_id) { - return ZTS_ERR_ARG; - } - _node->leave(net_id, NULL, NULL); - return ZTS_ERR_OK; + if (! net_id) { + return ZTS_ERR_ARG; + } + _node->leave(net_id, NULL, NULL); + return ZTS_ERR_OK; } void NodeService::obtainLock() const { - _nets_m.lock(); + _nets_m.lock(); } void NodeService::releaseLock() const { - _nets_m.unlock(); + _nets_m.unlock(); } bool NodeService::networkIsReady(uint64_t net_id) const { - if (! net_id) { - return ZTS_ERR_ARG; - } - Mutex::Lock _l(_nets_m); - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return false; - } - auto netState = n->second; - if (netState.config.assignedAddressCount == 0) { - return ZTS_ERR_NO_RESULT; - } + if (! net_id) { + return ZTS_ERR_ARG; + } + Mutex::Lock _l(_nets_m); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return false; + } + auto netState = n->second; + if (netState.config.assignedAddressCount == 0) { + return ZTS_ERR_NO_RESULT; + } } int NodeService::addressCount(uint64_t net_id) const { - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - return n->second.config.assignedAddressCount; + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + return n->second.config.assignedAddressCount; } int NodeService::routeCount(uint64_t net_id) const { - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - return n->second.config.routeCount; + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + return n->second.config.routeCount; } int NodeService::multicastSubCount(uint64_t net_id) const { - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - return n->second.config.multicastSubscriptionCount; + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + return n->second.config.multicastSubscriptionCount; } int NodeService::pathCount(uint64_t peer_id) const { - return ZTS_ERR_NO_RESULT; // TODO + return ZTS_ERR_NO_RESULT; // TODO } int NodeService::getAddrAtIdx(uint64_t net_id, unsigned int idx, char* dst, unsigned int len) { - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return 0; - } - auto netState = n->second; - if (idx >= netState.config.assignedAddressCount) { - return ZTS_ERR_ARG; - } - struct sockaddr* sa = (struct sockaddr*)&(netState.config.assignedAddresses[idx]); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return 0; + } + auto netState = n->second; + if (idx >= netState.config.assignedAddressCount) { + return ZTS_ERR_ARG; + } + struct sockaddr* sa = (struct sockaddr*)&(netState.config.assignedAddresses[idx]); - if (sa->sa_family == AF_INET) { - struct sockaddr_in* in4 = (struct sockaddr_in*)sa; - inet_ntop(AF_INET, &(in4->sin_addr), dst, ZTS_INET6_ADDRSTRLEN); - } - if (sa->sa_family == AF_INET6) { - struct sockaddr_in6* in6 = (struct sockaddr_in6*)sa; - inet_ntop(AF_INET6, &(in6->sin6_addr), dst, ZTS_INET6_ADDRSTRLEN); - } - return ZTS_ERR_OK; + if (sa->sa_family == AF_INET) { + struct sockaddr_in* in4 = (struct sockaddr_in*)sa; + inet_ntop(AF_INET, &(in4->sin_addr), dst, ZTS_INET6_ADDRSTRLEN); + } + if (sa->sa_family == AF_INET6) { + struct sockaddr_in6* in6 = (struct sockaddr_in6*)sa; + inet_ntop(AF_INET6, &(in6->sin6_addr), dst, ZTS_INET6_ADDRSTRLEN); + } + return ZTS_ERR_OK; } int NodeService::getRouteAtIdx( @@ -830,218 +830,218 @@ int NodeService::getRouteAtIdx( uint16_t* flags, uint16_t* metric) { - // We want to use strlen later so let's ensure there's no junk first. - memset(target, 0, len); - memset(via, 0, len); - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return 0; - } - auto netState = n->second; - if (idx >= netState.config.routeCount) { - return ZTS_ERR_ARG; - } - // target - const char* err = NULL; - struct sockaddr* sa = (struct sockaddr*)&(netState.config.routes[idx].target); - if (sa->sa_family == AF_INET) { - struct sockaddr_in* in4 = (struct sockaddr_in*)sa; - err = inet_ntop(AF_INET, &(in4->sin_addr), target, ZTS_INET6_ADDRSTRLEN); - } - if (sa->sa_family == AF_INET6) { - struct sockaddr_in6* in6 = (struct sockaddr_in6*)sa; - err = inet_ntop(AF_INET6, &(in6->sin6_addr), target, ZTS_INET6_ADDRSTRLEN); - } - // via - struct sockaddr* sa_via = (struct sockaddr*)&(netState.config.routes[idx].via); - if (sa_via->sa_family == AF_INET) { - struct sockaddr_in* in4 = (struct sockaddr_in*)sa_via; - err = inet_ntop(AF_INET, &(in4->sin_addr), via, ZTS_INET6_ADDRSTRLEN); - } - if (sa_via->sa_family == AF_INET6) { - struct sockaddr_in6* in6 = (struct sockaddr_in6*)sa_via; - err = inet_ntop(AF_INET6, &(in6->sin6_addr), via, ZTS_INET6_ADDRSTRLEN); - } - if (strlen(via) == 0) { - strncpy(via, "0.0.0.0", 7); - // TODO: Double check - } - *flags = netState.config.routes[idx].flags; - *metric = netState.config.routes[idx].metric; - return ZTS_ERR_OK; + // We want to use strlen later so let's ensure there's no junk first. + memset(target, 0, len); + memset(via, 0, len); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return 0; + } + auto netState = n->second; + if (idx >= netState.config.routeCount) { + return ZTS_ERR_ARG; + } + // target + const char* err = NULL; + struct sockaddr* sa = (struct sockaddr*)&(netState.config.routes[idx].target); + if (sa->sa_family == AF_INET) { + struct sockaddr_in* in4 = (struct sockaddr_in*)sa; + err = inet_ntop(AF_INET, &(in4->sin_addr), target, ZTS_INET6_ADDRSTRLEN); + } + if (sa->sa_family == AF_INET6) { + struct sockaddr_in6* in6 = (struct sockaddr_in6*)sa; + err = inet_ntop(AF_INET6, &(in6->sin6_addr), target, ZTS_INET6_ADDRSTRLEN); + } + // via + struct sockaddr* sa_via = (struct sockaddr*)&(netState.config.routes[idx].via); + if (sa_via->sa_family == AF_INET) { + struct sockaddr_in* in4 = (struct sockaddr_in*)sa_via; + err = inet_ntop(AF_INET, &(in4->sin_addr), via, ZTS_INET6_ADDRSTRLEN); + } + if (sa_via->sa_family == AF_INET6) { + struct sockaddr_in6* in6 = (struct sockaddr_in6*)sa_via; + err = inet_ntop(AF_INET6, &(in6->sin6_addr), via, ZTS_INET6_ADDRSTRLEN); + } + if (strlen(via) == 0) { + strncpy(via, "0.0.0.0", 7); + // TODO: Double check + } + *flags = netState.config.routes[idx].flags; + *metric = netState.config.routes[idx].metric; + return ZTS_ERR_OK; } int NodeService::getMulticastSubAtIdx(uint64_t net_id, unsigned int idx, uint64_t* mac, uint32_t* adi) { - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return 0; - } - auto netState = n->second; - if (idx >= netState.config.multicastSubscriptionCount) { - return ZTS_ERR_ARG; - } - *mac = netState.config.multicastSubscriptions[idx].mac; - *adi = netState.config.multicastSubscriptions[idx].adi; - return ZTS_ERR_OK; + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return 0; + } + auto netState = n->second; + if (idx >= netState.config.multicastSubscriptionCount) { + return ZTS_ERR_ARG; + } + *mac = netState.config.multicastSubscriptions[idx].mac; + *adi = netState.config.multicastSubscriptions[idx].adi; + return ZTS_ERR_OK; } int NodeService::getPathAtIdx(uint64_t peer_id, unsigned int idx, char* path, unsigned int len) { - return ZTS_ERR_NO_RESULT; // TODO + return ZTS_ERR_NO_RESULT; // TODO } int NodeService::getFirstAssignedAddr(uint64_t net_id, unsigned int family, struct zts_sockaddr_storage* addr) { - if (net_id == 0 || ((family != ZTS_AF_INET) && (family != ZTS_AF_INET6)) || ! addr) { - return ZTS_ERR_ARG; - } - Mutex::Lock _l(_nets_m); - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - auto netState = n->second; - if (netState.config.assignedAddressCount == 0) { - return ZTS_ERR_NO_RESULT; - } - for (unsigned int i = 0; i < netState.config.assignedAddressCount; i++) { - struct sockaddr* sa = (struct sockaddr*)&(netState.config.assignedAddresses[i]); - // Family values may vary across platforms, thus the following - if (sa->sa_family == AF_INET && family == ZTS_AF_INET) { - native_ss_to_zts_ss(addr, &(netState.config.assignedAddresses[i])); - return ZTS_ERR_OK; - } - if (sa->sa_family == AF_INET6 && family == ZTS_AF_INET6) { - native_ss_to_zts_ss(addr, &(netState.config.assignedAddresses[i])); - return ZTS_ERR_OK; - } - } - return ZTS_ERR_NO_RESULT; + if (net_id == 0 || ((family != ZTS_AF_INET) && (family != ZTS_AF_INET6)) || ! addr) { + return ZTS_ERR_ARG; + } + Mutex::Lock _l(_nets_m); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + auto netState = n->second; + if (netState.config.assignedAddressCount == 0) { + return ZTS_ERR_NO_RESULT; + } + for (unsigned int i = 0; i < netState.config.assignedAddressCount; i++) { + struct sockaddr* sa = (struct sockaddr*)&(netState.config.assignedAddresses[i]); + // Family values may vary across platforms, thus the following + if (sa->sa_family == AF_INET && family == ZTS_AF_INET) { + native_ss_to_zts_ss(addr, &(netState.config.assignedAddresses[i])); + return ZTS_ERR_OK; + } + if (sa->sa_family == AF_INET6 && family == ZTS_AF_INET6) { + native_ss_to_zts_ss(addr, &(netState.config.assignedAddresses[i])); + return ZTS_ERR_OK; + } + } + return ZTS_ERR_NO_RESULT; } int NodeService::getAllAssignedAddr(uint64_t net_id, struct zts_sockaddr_storage* addr, unsigned int* count) { - if (net_id == 0 || ! addr || ! count || *count != ZTS_MAX_ASSIGNED_ADDRESSES) { - return ZTS_ERR_ARG; - } - Mutex::Lock _l(_nets_m); - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - memset(addr, 0, sizeof(struct zts_sockaddr_storage) * ZTS_MAX_ASSIGNED_ADDRESSES); - auto netState = n->second; - if (netState.config.assignedAddressCount == 0) { - return ZTS_ERR_NO_RESULT; - } - for (unsigned int i = 0; i < netState.config.assignedAddressCount; i++) { - native_ss_to_zts_ss(&addr[i], &(netState.config.assignedAddresses[i])); - } - *count = netState.config.assignedAddressCount; - return ZTS_ERR_OK; + if (net_id == 0 || ! addr || ! count || *count != ZTS_MAX_ASSIGNED_ADDRESSES) { + return ZTS_ERR_ARG; + } + Mutex::Lock _l(_nets_m); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + memset(addr, 0, sizeof(struct zts_sockaddr_storage) * ZTS_MAX_ASSIGNED_ADDRESSES); + auto netState = n->second; + if (netState.config.assignedAddressCount == 0) { + return ZTS_ERR_NO_RESULT; + } + for (unsigned int i = 0; i < netState.config.assignedAddressCount; i++) { + native_ss_to_zts_ss(&addr[i], &(netState.config.assignedAddresses[i])); + } + *count = netState.config.assignedAddressCount; + return ZTS_ERR_OK; } int NodeService::addrIsAssigned(uint64_t net_id, unsigned int family) { - if (net_id == 0) { - return ZTS_ERR_ARG; - } - struct zts_sockaddr_storage addr; // unused - return getFirstAssignedAddr(net_id, family, &addr) != ZTS_ERR_NO_RESULT; + if (net_id == 0) { + return ZTS_ERR_ARG; + } + struct zts_sockaddr_storage addr; // unused + return getFirstAssignedAddr(net_id, family, &addr) != ZTS_ERR_NO_RESULT; } int NodeService::networkHasRoute(uint64_t net_id, unsigned int family) { - Mutex::Lock _l(_nets_m); - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - auto netState = n->second; - for (unsigned int i = 0; i < netState.config.routeCount; i++) { - struct sockaddr* sa = (struct sockaddr*)&(netState.config.routes[i].target); - if (sa->sa_family == AF_INET && family == ZTS_AF_INET) { - return true; - } - if (sa->sa_family == AF_INET6 && family == ZTS_AF_INET6) { - return true; - } - } - return false; + Mutex::Lock _l(_nets_m); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + auto netState = n->second; + for (unsigned int i = 0; i < netState.config.routeCount; i++) { + struct sockaddr* sa = (struct sockaddr*)&(netState.config.routes[i].target); + if (sa->sa_family == AF_INET && family == ZTS_AF_INET) { + return true; + } + if (sa->sa_family == AF_INET6 && family == ZTS_AF_INET6) { + return true; + } + } + return false; } int NodeService::orbit(void* tptr, uint64_t moon_world_id, uint64_t moon_seed) { - if (! moon_world_id || ! moon_seed) { - return ZTS_ERR_ARG; - } - Mutex::Lock _lr(_run_m); - if (! _run) { - return ZTS_ERR_SERVICE; - } - return _node->orbit(NULL, moon_world_id, moon_seed); + if (! moon_world_id || ! moon_seed) { + return ZTS_ERR_ARG; + } + Mutex::Lock _lr(_run_m); + if (! _run) { + return ZTS_ERR_SERVICE; + } + return _node->orbit(NULL, moon_world_id, moon_seed); } int NodeService::deorbit(void* tptr, uint64_t moon_world_id) { - if (! moon_world_id) { - return ZTS_ERR_ARG; - } - Mutex::Lock _lr(_run_m); - if (! _run) { - return ZTS_ERR_SERVICE; - } - return _node->deorbit(NULL, moon_world_id); + if (! moon_world_id) { + return ZTS_ERR_ARG; + } + Mutex::Lock _lr(_run_m); + if (! _run) { + return ZTS_ERR_SERVICE; + } + return _node->deorbit(NULL, moon_world_id); } uint64_t NodeService::getNodeId() { - Mutex::Lock _lr(_run_m); - if (! _run) { - return 0x0; - } - return _node ? _node->address() : 0x0; + Mutex::Lock _lr(_run_m); + if (! _run) { + return 0x0; + } + return _node ? _node->address() : 0x0; } int NodeService::setIdentity(const char* keypair, unsigned int len) { - if (keypair == NULL || len < ZT_IDENTITY_STRING_BUFFER_LENGTH) { - return ZTS_ERR_ARG; - } - // Double check user-provided keypair - Identity id; - if ((strlen(keypair) > 32) && (keypair[10] == ':')) { - if (! id.fromString(keypair)) { - return id.locallyValidate(); - } - } - Mutex::Lock _lr(_run_m); - if (_run) { - return ZTS_ERR_SERVICE; - } - Mutex::Lock _ls(_store_m); - memcpy(_secretIdStr, keypair, len); - return ZTS_ERR_OK; + if (keypair == NULL || len < ZT_IDENTITY_STRING_BUFFER_LENGTH) { + return ZTS_ERR_ARG; + } + // Double check user-provided keypair + Identity id; + if ((strlen(keypair) > 32) && (keypair[10] == ':')) { + if (! id.fromString(keypair)) { + return id.locallyValidate(); + } + } + Mutex::Lock _lr(_run_m); + if (_run) { + return ZTS_ERR_SERVICE; + } + Mutex::Lock _ls(_store_m); + memcpy(_secretIdStr, keypair, len); + return ZTS_ERR_OK; } int NodeService::getIdentity(char* keypair, unsigned int* len) { - if (keypair == NULL || *len < ZT_IDENTITY_STRING_BUFFER_LENGTH) { - return ZTS_ERR_ARG; - } - if (_node) { - _node->identity().toString(true, keypair); - } - return ZTS_ERR_OK; + if (keypair == NULL || *len < ZT_IDENTITY_STRING_BUFFER_LENGTH) { + return ZTS_ERR_ARG; + } + if (_node) { + _node->identity().toString(true, keypair); + } + return ZTS_ERR_OK; } void NodeService::sendEventToUser(unsigned int event_code, const void* arg, unsigned int len) { - if (! _events) { - return; - } - _events->enqueue(event_code, arg, len); + if (! _events) { + return; + } + _events->enqueue(event_code, arg, len); } void NodeService::nodeStatePutFunction( @@ -1050,121 +1050,121 @@ void NodeService::nodeStatePutFunction( const void* data, unsigned int len) { - char p[1024] = { 0 }; - FILE* f; - bool secure = false; - char dirname[1024] = { 0 }; - dirname[0] = 0; + char p[1024] = { 0 }; + FILE* f; + bool secure = false; + char dirname[1024] = { 0 }; + dirname[0] = 0; - Mutex::Lock _ls(_store_m); + Mutex::Lock _ls(_store_m); - switch (type) { - case ZT_STATE_OBJECT_IDENTITY_PUBLIC: - sendEventToUser(ZTS_EVENT_STORE_IDENTITY_PUBLIC, data, len); - memcpy(_publicIdStr, data, len); - if (_homePath.length() > 0 && _allowIdentityCaching) { - OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "identity.public", _homePath.c_str()); - } - else { - return; - } - break; - case ZT_STATE_OBJECT_IDENTITY_SECRET: - sendEventToUser(ZTS_EVENT_STORE_IDENTITY_SECRET, data, len); - memcpy(_secretIdStr, data, len); - if (_homePath.length() > 0 && _allowIdentityCaching) { - OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "identity.secret", _homePath.c_str()); - secure = true; - } - else { - return; - } - break; - case ZT_STATE_OBJECT_PLANET: - sendEventToUser(ZTS_EVENT_STORE_PLANET, data, len); - memcpy(_worldData, data, len); - if (_homePath.length() > 0 && _allowWorldCaching) { - OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "world", _homePath.c_str()); - } - else { - return; - } - break; - case ZT_STATE_OBJECT_NETWORK_CONFIG: - if (_homePath.length() > 0 && _allowNetworkCaching) { - OSUtils::ztsnprintf(dirname, sizeof(dirname), "%s" ZT_PATH_SEPARATOR_S "networks.d", _homePath.c_str()); - OSUtils::ztsnprintf( - p, - sizeof(p), - "%s" ZT_PATH_SEPARATOR_S "%.16llx.conf", - dirname, - (unsigned long long)id[0]); - secure = true; - } - else { - return; - } - break; - case ZT_STATE_OBJECT_PEER: - if (_homePath.length() > 0 && _allowPeerCaching) { - OSUtils::ztsnprintf(dirname, sizeof(dirname), "%s" ZT_PATH_SEPARATOR_S "peers.d", _homePath.c_str()); - OSUtils::ztsnprintf( - p, - sizeof(p), - "%s" ZT_PATH_SEPARATOR_S "%.10llx.peer", - dirname, - (unsigned long long)id[0]); - } - else { - return; - } - break; - default: - return; - } + switch (type) { + case ZT_STATE_OBJECT_IDENTITY_PUBLIC: + sendEventToUser(ZTS_EVENT_STORE_IDENTITY_PUBLIC, data, len); + memcpy(_publicIdStr, data, len); + if (_homePath.length() > 0 && _allowIdentityCaching) { + OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "identity.public", _homePath.c_str()); + } + else { + return; + } + break; + case ZT_STATE_OBJECT_IDENTITY_SECRET: + sendEventToUser(ZTS_EVENT_STORE_IDENTITY_SECRET, data, len); + memcpy(_secretIdStr, data, len); + if (_homePath.length() > 0 && _allowIdentityCaching) { + OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "identity.secret", _homePath.c_str()); + secure = true; + } + else { + return; + } + break; + case ZT_STATE_OBJECT_PLANET: + sendEventToUser(ZTS_EVENT_STORE_PLANET, data, len); + memcpy(_worldData, data, len); + if (_homePath.length() > 0 && _allowWorldCaching) { + OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "world", _homePath.c_str()); + } + else { + return; + } + break; + case ZT_STATE_OBJECT_NETWORK_CONFIG: + if (_homePath.length() > 0 && _allowNetworkCaching) { + OSUtils::ztsnprintf(dirname, sizeof(dirname), "%s" ZT_PATH_SEPARATOR_S "networks.d", _homePath.c_str()); + OSUtils::ztsnprintf( + p, + sizeof(p), + "%s" ZT_PATH_SEPARATOR_S "%.16llx.conf", + dirname, + (unsigned long long)id[0]); + secure = true; + } + else { + return; + } + break; + case ZT_STATE_OBJECT_PEER: + if (_homePath.length() > 0 && _allowPeerCaching) { + OSUtils::ztsnprintf(dirname, sizeof(dirname), "%s" ZT_PATH_SEPARATOR_S "peers.d", _homePath.c_str()); + OSUtils::ztsnprintf( + p, + sizeof(p), + "%s" ZT_PATH_SEPARATOR_S "%.10llx.peer", + dirname, + (unsigned long long)id[0]); + } + else { + return; + } + break; + default: + return; + } - if (len >= 0) { - // Check to see if we've already written this first. This reduces - // redundant writes and I/O overhead on most platforms and has - // little effect on others. - f = fopen(p, "rb"); - if (f) { - char buf[65535] = { 0 }; - long l = (long)fread(buf, 1, sizeof(buf), f); - fclose(f); - if ((l == (long)len) && (memcmp(data, buf, l) == 0)) { - return; - } - } + if (len >= 0) { + // Check to see if we've already written this first. This reduces + // redundant writes and I/O overhead on most platforms and has + // little effect on others. + f = fopen(p, "rb"); + if (f) { + char buf[65535] = { 0 }; + long l = (long)fread(buf, 1, sizeof(buf), f); + fclose(f); + if ((l == (long)len) && (memcmp(data, buf, l) == 0)) { + return; + } + } - f = fopen(p, "wb"); - if ((! f) && (dirname[0])) { // create subdirectory if it does not exist - OSUtils::mkdir(dirname); - f = fopen(p, "wb"); - } - if (f) { - if (fwrite(data, len, 1, f) != 1) - fprintf( - stderr, - "WARNING: unable to write to file: %s (I/O " - "error)" ZT_EOL_S, - p); - fclose(f); - if (secure) { - OSUtils::lockDownFile(p, false); - } - } - else { - fprintf( - stderr, - "WARNING: unable to write to file: %s (unable to " - "open)" ZT_EOL_S, - p); - } - } - else { - OSUtils::rm(p); - } + f = fopen(p, "wb"); + if ((! f) && (dirname[0])) { // create subdirectory if it does not exist + OSUtils::mkdir(dirname); + f = fopen(p, "wb"); + } + if (f) { + if (fwrite(data, len, 1, f) != 1) + fprintf( + stderr, + "WARNING: unable to write to file: %s (I/O " + "error)" ZT_EOL_S, + p); + fclose(f); + if (secure) { + OSUtils::lockDownFile(p, false); + } + } + else { + fprintf( + stderr, + "WARNING: unable to write to file: %s (unable to " + "open)" ZT_EOL_S, + p); + } + } + else { + OSUtils::rm(p); + } } int NodeService::nodeStateGetFunction( @@ -1173,64 +1173,64 @@ int NodeService::nodeStateGetFunction( void* data, unsigned int maxlen) { - char p[4096] = { 0 }; - unsigned int keylen = 0; - switch (type) { - case ZT_STATE_OBJECT_IDENTITY_PUBLIC: - keylen = strlen(_publicIdStr); - if (keylen > 0 && keylen <= maxlen) { - memcpy(data, _publicIdStr, keylen); - return keylen; - } - if (_homePath.length() > 0) { - OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "identity.public", _homePath.c_str()); - } - break; - case ZT_STATE_OBJECT_IDENTITY_SECRET: - keylen = strlen(_secretIdStr); - if (keylen > 0 && keylen <= maxlen) { - memcpy(data, _secretIdStr, keylen); - return keylen; - } - if (_homePath.length() > 0) { - OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "identity.secret", _homePath.c_str()); - } - break; - case ZT_STATE_OBJECT_PLANET: - if (_userDefinedWorld) { - memcpy(data, _worldData, _worldDataLen); - return _worldDataLen; - } - OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "world", _homePath.c_str()); - break; - case ZT_STATE_OBJECT_NETWORK_CONFIG: - OSUtils::ztsnprintf( - p, - sizeof(p), - "%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.conf", - _homePath.c_str(), - (unsigned long long)id[0]); - break; - case ZT_STATE_OBJECT_PEER: - OSUtils::ztsnprintf( - p, - sizeof(p), - "%s" ZT_PATH_SEPARATOR_S "peers.d" ZT_PATH_SEPARATOR_S "%.10llx.peer", - _homePath.c_str(), - (unsigned long long)id[0]); - break; - default: - return -1; - } - FILE* f = fopen(p, "rb"); - if (f) { - int n = (int)fread(data, 1, maxlen, f); - fclose(f); - if (n >= 0) { - return n; - } - } - return -1; + char p[4096] = { 0 }; + unsigned int keylen = 0; + switch (type) { + case ZT_STATE_OBJECT_IDENTITY_PUBLIC: + keylen = strlen(_publicIdStr); + if (keylen > 0 && keylen <= maxlen) { + memcpy(data, _publicIdStr, keylen); + return keylen; + } + if (_homePath.length() > 0) { + OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "identity.public", _homePath.c_str()); + } + break; + case ZT_STATE_OBJECT_IDENTITY_SECRET: + keylen = strlen(_secretIdStr); + if (keylen > 0 && keylen <= maxlen) { + memcpy(data, _secretIdStr, keylen); + return keylen; + } + if (_homePath.length() > 0) { + OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "identity.secret", _homePath.c_str()); + } + break; + case ZT_STATE_OBJECT_PLANET: + if (_userDefinedWorld) { + memcpy(data, _worldData, _worldDataLen); + return _worldDataLen; + } + OSUtils::ztsnprintf(p, sizeof(p), "%s" ZT_PATH_SEPARATOR_S "world", _homePath.c_str()); + break; + case ZT_STATE_OBJECT_NETWORK_CONFIG: + OSUtils::ztsnprintf( + p, + sizeof(p), + "%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.conf", + _homePath.c_str(), + (unsigned long long)id[0]); + break; + case ZT_STATE_OBJECT_PEER: + OSUtils::ztsnprintf( + p, + sizeof(p), + "%s" ZT_PATH_SEPARATOR_S "peers.d" ZT_PATH_SEPARATOR_S "%.10llx.peer", + _homePath.c_str(), + (unsigned long long)id[0]); + break; + default: + return -1; + } + FILE* f = fopen(p, "rb"); + if (f) { + int n = (int)fread(data, 1, maxlen, f); + fclose(f); + if (n >= 0) { + return n; + } + } + return -1; } int NodeService::nodeWirePacketSendFunction( @@ -1240,21 +1240,21 @@ int NodeService::nodeWirePacketSendFunction( unsigned int len, unsigned int ttl) { - // Even when relaying we still send via UDP. This way if UDP starts - // working we can instantly "fail forward" to it and stop using TCP - // proxy fallback, which is slow. + // Even when relaying we still send via UDP. This way if UDP starts + // working we can instantly "fail forward" to it and stop using TCP + // proxy fallback, which is slow. - if ((localSocket != -1) && (localSocket != 0) && (_binder.isUdpSocketValid((PhySocket*)((uintptr_t)localSocket)))) { - if ((ttl) && (addr->ss_family == AF_INET)) - _phy.setIp4UdpTtl((PhySocket*)((uintptr_t)localSocket), ttl); - const bool r = _phy.udpSend((PhySocket*)((uintptr_t)localSocket), (const struct sockaddr*)addr, data, len); - if ((ttl) && (addr->ss_family == AF_INET)) - _phy.setIp4UdpTtl((PhySocket*)((uintptr_t)localSocket), 255); - return ((r) ? 0 : -1); - } - else { - return ((_binder.udpSendAll(_phy, addr, data, len, ttl)) ? 0 : -1); - } + if ((localSocket != -1) && (localSocket != 0) && (_binder.isUdpSocketValid((PhySocket*)((uintptr_t)localSocket)))) { + if ((ttl) && (addr->ss_family == AF_INET)) + _phy.setIp4UdpTtl((PhySocket*)((uintptr_t)localSocket), ttl); + const bool r = _phy.udpSend((PhySocket*)((uintptr_t)localSocket), (const struct sockaddr*)addr, data, len); + if ((ttl) && (addr->ss_family == AF_INET)) + _phy.setIp4UdpTtl((PhySocket*)((uintptr_t)localSocket), 255); + return ((r) ? 0 : -1); + } + else { + return ((_binder.udpSendAll(_phy, addr, data, len, ttl)) ? 0 : -1); + } } void NodeService::nodeVirtualNetworkFrameFunction( @@ -1267,11 +1267,11 @@ void NodeService::nodeVirtualNetworkFrameFunction( const void* data, unsigned int len) { - NetworkState* n = reinterpret_cast(*nuptr); - if ((! n) || (! n->tap)) { - return; - } - n->tap->put(MAC(sourceMac), MAC(destMac), etherType, data, len); + NetworkState* n = reinterpret_cast(*nuptr); + if ((! n) || (! n->tap)) { + return; + } + n->tap->put(MAC(sourceMac), MAC(destMac), etherType, data, len); } int NodeService::nodePathCheckFunction( @@ -1279,83 +1279,83 @@ int NodeService::nodePathCheckFunction( const int64_t localSocket, const struct sockaddr_storage* remoteAddr) { - // Make sure we're not trying to do ZeroTier-over-ZeroTier - { - Mutex::Lock _l(_nets_m); - for (std::map::const_iterator n(_nets.begin()); n != _nets.end(); ++n) { - if (n->second.tap) { - std::vector ips(n->second.tap->ips()); - for (std::vector::const_iterator i(ips.begin()); i != ips.end(); ++i) { - if (i->containsAddress(*(reinterpret_cast(remoteAddr)))) { - return 0; - } - } - } - } - } + // Make sure we're not trying to do ZeroTier-over-ZeroTier + { + Mutex::Lock _l(_nets_m); + for (std::map::const_iterator n(_nets.begin()); n != _nets.end(); ++n) { + if (n->second.tap) { + std::vector ips(n->second.tap->ips()); + for (std::vector::const_iterator i(ips.begin()); i != ips.end(); ++i) { + if (i->containsAddress(*(reinterpret_cast(remoteAddr)))) { + return 0; + } + } + } + } + } - /* Note: I do not think we need to scan for overlap with managed routes - * because of the "route forking" and interface binding that we do. This - * ensures (we hope) that ZeroTier traffic will still take the physical - * path even if its managed routes this for other traffic. Will - * revisit if we see recursion problems. */ + /* Note: I do not think we need to scan for overlap with managed routes + * because of the "route forking" and interface binding that we do. This + * ensures (we hope) that ZeroTier traffic will still take the physical + * path even if its managed routes this for other traffic. Will + * revisit if we see recursion problems. */ - // Check blacklists - const Hashtable >* blh = - (const Hashtable >*)0; - const std::vector* gbl = (const std::vector*)0; - if (remoteAddr->ss_family == AF_INET) { - blh = &_v4Blacklists; - gbl = &_globalV4Blacklist; - } - else if (remoteAddr->ss_family == AF_INET6) { - blh = &_v6Blacklists; - gbl = &_globalV6Blacklist; - } - if (blh) { - Mutex::Lock _l(_localConfig_m); - const std::vector* l = blh->get(ztaddr); - if (l) { - for (std::vector::const_iterator a(l->begin()); a != l->end(); ++a) { - if (a->containsAddress(*reinterpret_cast(remoteAddr))) { - return 0; - } - } - } - } - if (gbl) { - for (std::vector::const_iterator a(gbl->begin()); a != gbl->end(); ++a) { - if (a->containsAddress(*reinterpret_cast(remoteAddr))) { - return 0; - } - } - } - return 1; + // Check blacklists + const Hashtable >* blh = + (const Hashtable >*)0; + const std::vector* gbl = (const std::vector*)0; + if (remoteAddr->ss_family == AF_INET) { + blh = &_v4Blacklists; + gbl = &_globalV4Blacklist; + } + else if (remoteAddr->ss_family == AF_INET6) { + blh = &_v6Blacklists; + gbl = &_globalV6Blacklist; + } + if (blh) { + Mutex::Lock _l(_localConfig_m); + const std::vector* l = blh->get(ztaddr); + if (l) { + for (std::vector::const_iterator a(l->begin()); a != l->end(); ++a) { + if (a->containsAddress(*reinterpret_cast(remoteAddr))) { + return 0; + } + } + } + } + if (gbl) { + for (std::vector::const_iterator a(gbl->begin()); a != gbl->end(); ++a) { + if (a->containsAddress(*reinterpret_cast(remoteAddr))) { + return 0; + } + } + } + return 1; } int NodeService::nodePathLookupFunction(uint64_t ztaddr, unsigned int family, struct sockaddr_storage* result) { - const Hashtable >* lh = (const Hashtable >*)0; - if (family < 0) { - lh = (_node->prng() & 1) ? &_v4Hints : &_v6Hints; - } - else if (family == AF_INET) { - lh = &_v4Hints; - } - else if (family == AF_INET6) { - lh = &_v6Hints; - } - else { - return 0; - } - const std::vector* l = lh->get(ztaddr); - if ((l) && (l->size() > 0)) { - memcpy(result, &((*l)[(unsigned long)_node->prng() % l->size()]), sizeof(struct sockaddr_storage)); - return 1; - } - else { - return 0; - } + const Hashtable >* lh = (const Hashtable >*)0; + if (family < 0) { + lh = (_node->prng() & 1) ? &_v4Hints : &_v6Hints; + } + else if (family == AF_INET) { + lh = &_v4Hints; + } + else if (family == AF_INET6) { + lh = &_v6Hints; + } + else { + return 0; + } + const std::vector* l = lh->get(ztaddr); + if ((l) && (l->size() > 0)) { + memcpy(result, &((*l)[(unsigned long)_node->prng() % l->size()]), sizeof(struct sockaddr_storage)); + return 1; + } + else { + return 0; + } } void NodeService::tapFrameHandler( @@ -1367,349 +1367,349 @@ void NodeService::tapFrameHandler( const void* data, unsigned int len) { - _node->processVirtualNetworkFrame( - (void*)0, - OSUtils::now(), - net_id, - from.toInt(), - to.toInt(), - etherType, - vlanId, - data, - len, - &_nextBackgroundTaskDeadline); + _node->processVirtualNetworkFrame( + (void*)0, + OSUtils::now(), + net_id, + from.toInt(), + to.toInt(), + etherType, + vlanId, + data, + len, + &_nextBackgroundTaskDeadline); } int NodeService::shouldBindInterface(const char* ifname, const InetAddress& ifaddr) { #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux) - if ((ifname[0] == 'l') && (ifname[1] == 'o')) { - return false; // loopback - } - if ((ifname[0] == 'z') && (ifname[1] == 't')) { - return false; // sanity check: zt# - } - if ((ifname[0] == 't') && (ifname[1] == 'u') && (ifname[2] == 'n')) { - return false; // tun# is probably an OpenVPN tunnel or similar - } - if ((ifname[0] == 't') && (ifname[1] == 'a') && (ifname[2] == 'p')) { - return false; // tap# is probably an OpenVPN tunnel or similar - } + if ((ifname[0] == 'l') && (ifname[1] == 'o')) { + return false; // loopback + } + if ((ifname[0] == 'z') && (ifname[1] == 't')) { + return false; // sanity check: zt# + } + if ((ifname[0] == 't') && (ifname[1] == 'u') && (ifname[2] == 'n')) { + return false; // tun# is probably an OpenVPN tunnel or similar + } + if ((ifname[0] == 't') && (ifname[1] == 'a') && (ifname[2] == 'p')) { + return false; // tap# is probably an OpenVPN tunnel or similar + } #endif #ifdef __APPLE__ - if ((ifname[0] == 'f') && (ifname[1] == 'e') && (ifname[2] == 't') && (ifname[3] == 'h')) { - return false; // ... as is feth# - } - if ((ifname[0] == 'l') && (ifname[1] == 'o')) { - return false; // loopback - } - if ((ifname[0] == 'z') && (ifname[1] == 't')) { - return false; // sanity check: zt# - } - if ((ifname[0] == 't') && (ifname[1] == 'u') && (ifname[2] == 'n')) { - return false; // tun# is probably an OpenVPN tunnel or similar - } - if ((ifname[0] == 't') && (ifname[1] == 'a') && (ifname[2] == 'p')) { - return false; // tap# is probably an OpenVPN tunnel or similar - } - if ((ifname[0] == 'u') && (ifname[1] == 't') && (ifname[2] == 'u') && (ifname[3] == 'n')) { - return false; // ... as is utun# - } + if ((ifname[0] == 'f') && (ifname[1] == 'e') && (ifname[2] == 't') && (ifname[3] == 'h')) { + return false; // ... as is feth# + } + if ((ifname[0] == 'l') && (ifname[1] == 'o')) { + return false; // loopback + } + if ((ifname[0] == 'z') && (ifname[1] == 't')) { + return false; // sanity check: zt# + } + if ((ifname[0] == 't') && (ifname[1] == 'u') && (ifname[2] == 'n')) { + return false; // tun# is probably an OpenVPN tunnel or similar + } + if ((ifname[0] == 't') && (ifname[1] == 'a') && (ifname[2] == 'p')) { + return false; // tap# is probably an OpenVPN tunnel or similar + } + if ((ifname[0] == 'u') && (ifname[1] == 't') && (ifname[2] == 'u') && (ifname[3] == 'n')) { + return false; // ... as is utun# + } #endif - { - Mutex::Lock _l(_localConfig_m); - for (std::vector::const_iterator p(_interfacePrefixBlacklist.begin()); - p != _interfacePrefixBlacklist.end(); - ++p) { - if (! strncmp(p->c_str(), ifname, p->length())) { - return false; - } - } - } - { - // Check global blacklists - const std::vector* gbl = (const std::vector*)0; - if (ifaddr.ss_family == AF_INET) { - gbl = &_globalV4Blacklist; - } - else if (ifaddr.ss_family == AF_INET6) { - gbl = &_globalV6Blacklist; - } - if (gbl) { - Mutex::Lock _l(_localConfig_m); - for (std::vector::const_iterator a(gbl->begin()); a != gbl->end(); ++a) { - if (a->containsAddress(ifaddr)) { - return false; - } - } - } - } - { - Mutex::Lock _l(_nets_m); - for (std::map::const_iterator n(_nets.begin()); n != _nets.end(); ++n) { - if (n->second.tap) { - std::vector ips(n->second.tap->ips()); - for (std::vector::const_iterator i(ips.begin()); i != ips.end(); ++i) { - if (i->ipsEqual(ifaddr)) { - return false; - } - } - } - } - } - return true; + { + Mutex::Lock _l(_localConfig_m); + for (std::vector::const_iterator p(_interfacePrefixBlacklist.begin()); + p != _interfacePrefixBlacklist.end(); + ++p) { + if (! strncmp(p->c_str(), ifname, p->length())) { + return false; + } + } + } + { + // Check global blacklists + const std::vector* gbl = (const std::vector*)0; + if (ifaddr.ss_family == AF_INET) { + gbl = &_globalV4Blacklist; + } + else if (ifaddr.ss_family == AF_INET6) { + gbl = &_globalV6Blacklist; + } + if (gbl) { + Mutex::Lock _l(_localConfig_m); + for (std::vector::const_iterator a(gbl->begin()); a != gbl->end(); ++a) { + if (a->containsAddress(ifaddr)) { + return false; + } + } + } + } + { + Mutex::Lock _l(_nets_m); + for (std::map::const_iterator n(_nets.begin()); n != _nets.end(); ++n) { + if (n->second.tap) { + std::vector ips(n->second.tap->ips()); + for (std::vector::const_iterator i(ips.begin()); i != ips.end(); ++i) { + if (i->ipsEqual(ifaddr)) { + return false; + } + } + } + } + } + return true; } int NodeService::_trialBind(unsigned int port) { - struct sockaddr_in in4; - struct sockaddr_in6 in6; - PhySocket* tb; + struct sockaddr_in in4; + struct sockaddr_in6 in6; + PhySocket* tb; - memset(&in4, 0, sizeof(in4)); - in4.sin_family = AF_INET; - in4.sin_port = Utils::hton((uint16_t)port); - tb = _phy.udpBind(reinterpret_cast(&in4), (void*)0, 0); - if (tb) { - _phy.close(tb, false); - return true; - } + memset(&in4, 0, sizeof(in4)); + in4.sin_family = AF_INET; + in4.sin_port = Utils::hton((uint16_t)port); + tb = _phy.udpBind(reinterpret_cast(&in4), (void*)0, 0); + if (tb) { + _phy.close(tb, false); + return true; + } - memset(&in6, 0, sizeof(in6)); - in6.sin6_family = AF_INET6; - in6.sin6_port = Utils::hton((uint16_t)port); - tb = _phy.udpBind(reinterpret_cast(&in6), (void*)0, 0); - if (tb) { - _phy.close(tb, false); - return true; - } - return false; + memset(&in6, 0, sizeof(in6)); + in6.sin6_family = AF_INET6; + in6.sin6_port = Utils::hton((uint16_t)port); + tb = _phy.udpBind(reinterpret_cast(&in6), (void*)0, 0); + if (tb) { + _phy.close(tb, false); + return true; + } + return false; } int NodeService::isRunning() const { - return _run; + return _run; } int NodeService::nodeIsOnline() const { - return _nodeIsOnline; + return _nodeIsOnline; } int NodeService::setHomePath(const char* homePath) { - if (! homePath) { - return ZTS_ERR_ARG; - } - Mutex::Lock _lr(_run_m); - if (_run) { - return ZTS_ERR_SERVICE; - } - _homePath = std::string(homePath); - return ZTS_ERR_OK; + if (! homePath) { + return ZTS_ERR_ARG; + } + Mutex::Lock _lr(_run_m); + if (_run) { + return ZTS_ERR_SERVICE; + } + _homePath = std::string(homePath); + return ZTS_ERR_OK; } int NodeService::setPrimaryPort(unsigned short primaryPort) { - Mutex::Lock _lr(_run_m); - if (_run) { - return ZTS_ERR_SERVICE; - } - _primaryPort = primaryPort; - return ZTS_ERR_OK; + Mutex::Lock _lr(_run_m); + if (_run) { + return ZTS_ERR_SERVICE; + } + _primaryPort = primaryPort; + return ZTS_ERR_OK; } unsigned short NodeService::getPrimaryPort() const { - return _primaryPort; + return _primaryPort; } int NodeService::setUserEventSystem(Events* events) { - Mutex::Lock _lr(_run_m); - if (_run) { - return ZTS_ERR_SERVICE; - } - _events = events; - return ZTS_ERR_OK; + Mutex::Lock _lr(_run_m); + if (_run) { + return ZTS_ERR_SERVICE; + } + _events = events; + return ZTS_ERR_OK; } void NodeService::enableEvents() { - Mutex::Lock _lr(_run_m); - if (! _events) { - return; - } - _events->enable(); + Mutex::Lock _lr(_run_m); + if (! _events) { + return; + } + _events->enable(); } int NodeService::setWorld(const void* worldData, unsigned int len) { - if (! worldData || len <= 0 || len > ZTS_STORE_DATA_LEN) { - return ZTS_ERR_ARG; - } - Mutex::Lock _lr(_run_m); - if (_run) { - return ZTS_ERR_SERVICE; - } - Mutex::Lock _ls(_store_m); - memcpy(_worldData, worldData, len); - _worldDataLen = len; - _userDefinedWorld = true; - return ZTS_ERR_OK; + if (! worldData || len <= 0 || len > ZTS_STORE_DATA_LEN) { + return ZTS_ERR_ARG; + } + Mutex::Lock _lr(_run_m); + if (_run) { + return ZTS_ERR_SERVICE; + } + Mutex::Lock _ls(_store_m); + memcpy(_worldData, worldData, len); + _worldDataLen = len; + _userDefinedWorld = true; + return ZTS_ERR_OK; } int NodeService::addInterfacePrefixToBlacklist(const char* prefix, unsigned int len) { - if (! prefix || len == 0 || len > 15) { - return ZTS_ERR_ARG; - } - Mutex::Lock _lr(_run_m); - if (_run) { - return ZTS_ERR_SERVICE; - } - Mutex::Lock _l(_localConfig_m); - _interfacePrefixBlacklist.push_back(std::string(prefix)); - return ZTS_ERR_OK; + if (! prefix || len == 0 || len > 15) { + return ZTS_ERR_ARG; + } + Mutex::Lock _lr(_run_m); + if (_run) { + return ZTS_ERR_SERVICE; + } + Mutex::Lock _l(_localConfig_m); + _interfacePrefixBlacklist.push_back(std::string(prefix)); + return ZTS_ERR_OK; } uint64_t NodeService::getMACAddress(uint64_t net_id) const { - if (net_id == 0) { - return ZTS_ERR_ARG; - } - Mutex::Lock _lr(_run_m); - if (! _run) { - return ZTS_ERR_SERVICE; - } - Mutex::Lock _ln(_nets_m); - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - return n->second.config.mac; + if (net_id == 0) { + return ZTS_ERR_ARG; + } + Mutex::Lock _lr(_run_m); + if (! _run) { + return ZTS_ERR_SERVICE; + } + Mutex::Lock _ln(_nets_m); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + return n->second.config.mac; } int NodeService::getNetworkName(uint64_t net_id, char* dst, unsigned int len) const { - if (net_id == 0 || ! dst || len != ZTS_MAX_NETWORK_SHORT_NAME_LENGTH) { - return ZTS_ERR_ARG; - } - Mutex::Lock _lr(_run_m); - if (! _run) { - return ZTS_ERR_SERVICE; - } - if (! _nodeIsOnline) { - return ZTS_ERR_SERVICE; - } - Mutex::Lock _ln(_nets_m); - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - auto netState = n->second; - memcpy(dst, netState.config.name, sizeof(netState.config.name)); - return ZTS_ERR_OK; + if (net_id == 0 || ! dst || len != ZTS_MAX_NETWORK_SHORT_NAME_LENGTH) { + return ZTS_ERR_ARG; + } + Mutex::Lock _lr(_run_m); + if (! _run) { + return ZTS_ERR_SERVICE; + } + if (! _nodeIsOnline) { + return ZTS_ERR_SERVICE; + } + Mutex::Lock _ln(_nets_m); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + auto netState = n->second; + memcpy(dst, netState.config.name, sizeof(netState.config.name)); + return ZTS_ERR_OK; } int NodeService::allowPeerCaching(unsigned int allowed) { - Mutex::Lock _lr(_run_m); - if (_run) { - return ZTS_ERR_SERVICE; - } - _allowPeerCaching = allowed; - return ZTS_ERR_OK; + Mutex::Lock _lr(_run_m); + if (_run) { + return ZTS_ERR_SERVICE; + } + _allowPeerCaching = allowed; + return ZTS_ERR_OK; } int NodeService::allowNetworkCaching(unsigned int allowed) { - Mutex::Lock _lr(_run_m); - if (_run) { - return ZTS_ERR_SERVICE; - } - _allowNetworkCaching = allowed; - return ZTS_ERR_OK; + Mutex::Lock _lr(_run_m); + if (_run) { + return ZTS_ERR_SERVICE; + } + _allowNetworkCaching = allowed; + return ZTS_ERR_OK; } int NodeService::allowIdentityCaching(unsigned int allowed) { - Mutex::Lock _lr(_run_m); - if (_run) { - return ZTS_ERR_SERVICE; - } - _allowIdentityCaching = allowed; - return ZTS_ERR_OK; + Mutex::Lock _lr(_run_m); + if (_run) { + return ZTS_ERR_SERVICE; + } + _allowIdentityCaching = allowed; + return ZTS_ERR_OK; } int NodeService::allowWorldCaching(unsigned int allowed) { - Mutex::Lock _lr(_run_m); - if (_run) { - return ZTS_ERR_SERVICE; - } - _allowWorldCaching = allowed; - return ZTS_ERR_OK; + Mutex::Lock _lr(_run_m); + if (_run) { + return ZTS_ERR_SERVICE; + } + _allowWorldCaching = allowed; + return ZTS_ERR_OK; } int NodeService::getNetworkBroadcast(uint64_t net_id) { - if (net_id == 0) { - return ZTS_ERR_ARG; - } - Mutex::Lock _lr(_run_m); - if (! _run) { - return ZTS_ERR_SERVICE; - } - Mutex::Lock _ln(_nets_m); - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - return n->second.config.broadcastEnabled; + if (net_id == 0) { + return ZTS_ERR_ARG; + } + Mutex::Lock _lr(_run_m); + if (! _run) { + return ZTS_ERR_SERVICE; + } + Mutex::Lock _ln(_nets_m); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + return n->second.config.broadcastEnabled; } int NodeService::getNetworkMTU(uint64_t net_id) { - Mutex::Lock _lr(_run_m); - if (! _run) { - return ZTS_ERR_SERVICE; - } - Mutex::Lock _ln(_nets_m); - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - return n->second.config.mtu; + Mutex::Lock _lr(_run_m); + if (! _run) { + return ZTS_ERR_SERVICE; + } + Mutex::Lock _ln(_nets_m); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + return n->second.config.mtu; } int NodeService::getNetworkType(uint64_t net_id) { - Mutex::Lock _lr(_run_m); - if (! _run) { - return ZTS_ERR_SERVICE; - } - Mutex::Lock _ln(_nets_m); - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - return n->second.config.type; + Mutex::Lock _lr(_run_m); + if (! _run) { + return ZTS_ERR_SERVICE; + } + Mutex::Lock _ln(_nets_m); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + return n->second.config.type; } int NodeService::getNetworkStatus(uint64_t net_id) { - Mutex::Lock _lr(_run_m); - if (! _run) { - return ZTS_ERR_SERVICE; - } - Mutex::Lock _ln(_nets_m); - std::map::const_iterator n(_nets.find(net_id)); - if (n == _nets.end()) { - return ZTS_ERR_NO_RESULT; - } - return n->second.config.status; + Mutex::Lock _lr(_run_m); + if (! _run) { + return ZTS_ERR_SERVICE; + } + Mutex::Lock _ln(_nets_m); + std::map::const_iterator n(_nets.find(net_id)); + if (n == _nets.end()) { + return ZTS_ERR_NO_RESULT; + } + return n->second.config.status; } } // namespace ZeroTier diff --git a/src/NodeService.hpp b/src/NodeService.hpp index 1cea16f..b0fcdf9 100644 --- a/src/NodeService.hpp +++ b/src/NodeService.hpp @@ -55,412 +55,412 @@ namespace ZeroTier { */ class NodeService { public: - /** - * Returned by node main if/when it terminates - */ - enum ReasonForTermination { - /** - * Instance is still running - */ - ONE_STILL_RUNNING = 0, + /** + * Returned by node main if/when it terminates + */ + enum ReasonForTermination { + /** + * Instance is still running + */ + ONE_STILL_RUNNING = 0, - /** - * Normal shutdown - */ - ONE_NORMAL_TERMINATION = 1, + /** + * Normal shutdown + */ + ONE_NORMAL_TERMINATION = 1, - /** - * A serious unrecoverable error has occurred - */ - ONE_UNRECOVERABLE_ERROR = 2, + /** + * A serious unrecoverable error has occurred + */ + ONE_UNRECOVERABLE_ERROR = 2, - /** - * Your identity has collided with another - */ - ONE_IDENTITY_COLLISION = 3 - }; + /** + * Your identity has collided with another + */ + ONE_IDENTITY_COLLISION = 3 + }; - /** - * Local settings for each network - */ - struct NetworkSettings { - /** - * Allow this network to configure IP addresses and routes? - */ - bool allowManaged; + /** + * Local settings for each network + */ + struct NetworkSettings { + /** + * Allow this network to configure IP addresses and routes? + */ + bool allowManaged; - /** - * Whitelist of addresses that can be configured by this network. - * If empty and allowManaged is true, allow all - * private/pseudoprivate addresses. - */ - std::vector allowManagedWhitelist; + /** + * Whitelist of addresses that can be configured by this network. + * If empty and allowManaged is true, allow all + * private/pseudoprivate addresses. + */ + std::vector allowManagedWhitelist; - /** - * Allow configuration of IPs and routes within global (Internet) IP - * space? - */ - bool allowGlobal; + /** + * Allow configuration of IPs and routes within global (Internet) IP + * space? + */ + bool allowGlobal; - /** - * Allow overriding of system default routes for "full tunnel" - * operation? - */ - bool allowDefault; - }; + /** + * Allow overriding of system default routes for "full tunnel" + * operation? + */ + bool allowDefault; + }; - Phy _phy; - Node* _node; - unsigned int _primaryPort = 0; - unsigned int _secondaryPort = 0; - unsigned int _tertiaryPort = 0; - volatile unsigned int _udpPortPickerCounter; + Phy _phy; + Node* _node; + unsigned int _primaryPort = 0; + unsigned int _secondaryPort = 0; + unsigned int _tertiaryPort = 0; + volatile unsigned int _udpPortPickerCounter; - std::map peerCache; + std::map peerCache; - // Local configuration and memo-ized information from it - Hashtable > _v4Hints; - Hashtable > _v6Hints; - Hashtable > _v4Blacklists; - Hashtable > _v6Blacklists; - std::vector _globalV4Blacklist; - std::vector _globalV6Blacklist; - std::vector _allowManagementFrom; - std::vector _interfacePrefixBlacklist; - Mutex _localConfig_m; + // Local configuration and memo-ized information from it + Hashtable > _v4Hints; + Hashtable > _v6Hints; + Hashtable > _v4Blacklists; + Hashtable > _v6Blacklists; + std::vector _globalV4Blacklist; + std::vector _globalV6Blacklist; + std::vector _allowManagementFrom; + std::vector _interfacePrefixBlacklist; + Mutex _localConfig_m; - std::vector explicitBind; + std::vector explicitBind; - /* - * To attempt to handle NAT/gateway craziness we use three local UDP - * ports: - * - * [0] is the normal/default port, usually 9993 - * [1] is a port derived from our ZeroTier address - * [2] is a port computed from the normal/default for use with - * uPnP/NAT-PMP mappings - * - * [2] exists because on some gateways trying to do regular NAT-t - * interferes destructively with uPnP port mapping behavior in very - * weird buggy ways. It's only used if uPnP/NAT-PMP is enabled in this - * build. - */ - unsigned int _ports[3] = { 0 }; - Binder _binder; + /* + * To attempt to handle NAT/gateway craziness we use three local UDP + * ports: + * + * [0] is the normal/default port, usually 9993 + * [1] is a port derived from our ZeroTier address + * [2] is a port computed from the normal/default for use with + * uPnP/NAT-PMP mappings + * + * [2] exists because on some gateways trying to do regular NAT-t + * interferes destructively with uPnP port mapping behavior in very + * weird buggy ways. It's only used if uPnP/NAT-PMP is enabled in this + * build. + */ + unsigned int _ports[3] = { 0 }; + Binder _binder; - // Time we last received a packet from a global address - uint64_t _lastDirectReceiveFromGlobal; + // Time we last received a packet from a global address + uint64_t _lastDirectReceiveFromGlobal; - // Last potential sleep/wake event - uint64_t _lastRestart; + // Last potential sleep/wake event + uint64_t _lastRestart; - // Deadline for the next background task service function - volatile int64_t _nextBackgroundTaskDeadline; + // Deadline for the next background task service function + volatile int64_t _nextBackgroundTaskDeadline; - // Configured networks - struct NetworkState { - NetworkState() : tap((VirtualTap*)0) - { - // Real defaults are in network 'up' code in network event - // handler - settings.allowManaged = true; - settings.allowGlobal = false; - settings.allowDefault = false; - } + // Configured networks + struct NetworkState { + NetworkState() : tap((VirtualTap*)0) + { + // Real defaults are in network 'up' code in network event + // handler + settings.allowManaged = true; + settings.allowGlobal = false; + settings.allowDefault = false; + } - VirtualTap* tap; - ZT_VirtualNetworkConfig config; // memcpy() of raw config from core - std::vector managedIps; - NetworkSettings settings; - }; - std::map _nets; + VirtualTap* tap; + ZT_VirtualNetworkConfig config; // memcpy() of raw config from core + std::vector managedIps; + NetworkSettings settings; + }; + std::map _nets; - /** Lock to control access to network configuration data */ - Mutex _nets_m; - /** Lock to control access to storage data */ - Mutex _store_m; - /** Lock to control access to service run state */ - Mutex _run_m; - // Set to false to force service to stop - volatile bool _run; - /** Lock to control access to termination reason */ - Mutex _termReason_m; - // Termination status information - ReasonForTermination _termReason; + /** Lock to control access to network configuration data */ + Mutex _nets_m; + /** Lock to control access to storage data */ + Mutex _store_m; + /** Lock to control access to service run state */ + Mutex _run_m; + // Set to false to force service to stop + volatile bool _run; + /** Lock to control access to termination reason */ + Mutex _termReason_m; + // Termination status information + ReasonForTermination _termReason; - std::string _fatalErrorMessage; + std::string _fatalErrorMessage; - // uPnP/NAT-PMP port mapper if enabled - bool _portMappingEnabled; // local.conf settings + // uPnP/NAT-PMP port mapper if enabled + bool _portMappingEnabled; // local.conf settings #ifdef ZT_USE_MINIUPNPC - PortMapper* _portMapper; + PortMapper* _portMapper; #endif - uint8_t _allowNetworkCaching; - uint8_t _allowPeerCaching; - uint8_t _allowIdentityCaching; - uint8_t _allowWorldCaching; + uint8_t _allowNetworkCaching; + uint8_t _allowPeerCaching; + uint8_t _allowIdentityCaching; + uint8_t _allowWorldCaching; - char _publicIdStr[ZT_IDENTITY_STRING_BUFFER_LENGTH] = { 0 }; - char _secretIdStr[ZT_IDENTITY_STRING_BUFFER_LENGTH] = { 0 }; + char _publicIdStr[ZT_IDENTITY_STRING_BUFFER_LENGTH] = { 0 }; + char _secretIdStr[ZT_IDENTITY_STRING_BUFFER_LENGTH] = { 0 }; - bool _userDefinedWorld; - char _worldData[ZTS_STORE_DATA_LEN] = { 0 }; - int _worldDataLen = 0; + bool _userDefinedWorld; + char _worldData[ZTS_STORE_DATA_LEN] = { 0 }; + int _worldDataLen = 0; - /** Whether the node has successfully come online */ - bool _nodeIsOnline; + /** Whether the node has successfully come online */ + bool _nodeIsOnline; - /** Whether we allow the NodeService to generate events for the user */ - bool _eventsEnabled; + /** Whether we allow the NodeService to generate events for the user */ + bool _eventsEnabled; - /** Storage path defined by the user */ - std::string _homePath; + /** Storage path defined by the user */ + std::string _homePath; - /** System to ingest events from this class and emit them to the user */ - Events* _events; + /** System to ingest events from this class and emit them to the user */ + Events* _events; - NodeService(); - ~NodeService(); + NodeService(); + ~NodeService(); - /** Main service loop */ - ReasonForTermination run(); + /** Main service loop */ + ReasonForTermination run(); - ReasonForTermination reasonForTermination() const; + ReasonForTermination reasonForTermination() const; - std::string fatalErrorMessage() const; + std::string fatalErrorMessage() const; - /** Stop the node and service */ - void terminate(); + /** Stop the node and service */ + void terminate(); - /** Apply or update managed IPs for a configured network */ - void syncManagedStuff(NetworkState& n); + /** Apply or update managed IPs for a configured network */ + void syncManagedStuff(NetworkState& n); - void phyOnDatagram( - PhySocket* sock, - void** uptr, - const struct sockaddr* localAddr, - const struct sockaddr* from, - void* data, - unsigned long len); + void phyOnDatagram( + PhySocket* sock, + void** uptr, + const struct sockaddr* localAddr, + const struct sockaddr* from, + void* data, + unsigned long len); - int nodeVirtualNetworkConfigFunction( - uint64_t net_id, - void** nuptr, - enum ZT_VirtualNetworkConfigOperation op, - const ZT_VirtualNetworkConfig* nwc); + int nodeVirtualNetworkConfigFunction( + uint64_t net_id, + void** nuptr, + enum ZT_VirtualNetworkConfigOperation op, + const ZT_VirtualNetworkConfig* nwc); - void nodeEventCallback(enum ZT_Event event, const void* metaData); + void nodeEventCallback(enum ZT_Event event, const void* metaData); - zts_net_info_t* prepare_network_details_msg(const NetworkState& n); + zts_net_info_t* prepare_network_details_msg(const NetworkState& n); - void generateEventMsgs(); + void generateEventMsgs(); - void sendEventToUser(unsigned int event_code, const void* arg, unsigned int len = 0); + void sendEventToUser(unsigned int event_code, const void* arg, unsigned int len = 0); - /** Join a network */ - int join(uint64_t net_id); + /** Join a network */ + int join(uint64_t net_id); - /** Leave a network */ - int leave(uint64_t net_id); + /** Leave a network */ + int leave(uint64_t net_id); - /** Return whether the network is ready for transport services */ - bool networkIsReady(uint64_t net_id) const; + /** Return whether the network is ready for transport services */ + bool networkIsReady(uint64_t net_id) const; - /** Lock the service so we can perform queries */ - void obtainLock() const; + /** Lock the service so we can perform queries */ + void obtainLock() const; - /** Unlock the service */ - void releaseLock() const; + /** Unlock the service */ + void releaseLock() const; - /** Return number of assigned addresses on the network. Service must be locked. */ - int addressCount(uint64_t net_id) const; + /** Return number of assigned addresses on the network. Service must be locked. */ + int addressCount(uint64_t net_id) const; - /** Return number of managed routes on the network. Service must be locked. */ - int routeCount(uint64_t net_id) const; + /** Return number of managed routes on the network. Service must be locked. */ + int routeCount(uint64_t net_id) const; - /** Return number of multicast subscriptions on the network. Service must be locked. */ - int multicastSubCount(uint64_t net_id) const; + /** Return number of multicast subscriptions on the network. Service must be locked. */ + int multicastSubCount(uint64_t net_id) const; - /** Return number of known physical paths to the peer. Service must be locked. */ - int pathCount(uint64_t peer_id) const; + /** Return number of known physical paths to the peer. Service must be locked. */ + int pathCount(uint64_t peer_id) const; - int getAddrAtIdx(uint64_t net_id, unsigned int idx, char* dst, unsigned int len); + int getAddrAtIdx(uint64_t net_id, unsigned int idx, char* dst, unsigned int len); - int getRouteAtIdx( - uint64_t net_id, - unsigned int idx, - char* target, - char* via, - unsigned int len, - uint16_t* flags, - uint16_t* metric); + int getRouteAtIdx( + uint64_t net_id, + unsigned int idx, + char* target, + char* via, + unsigned int len, + uint16_t* flags, + uint16_t* metric); - int getMulticastSubAtIdx(uint64_t net_id, unsigned int idx, uint64_t* mac, uint32_t* adi); + int getMulticastSubAtIdx(uint64_t net_id, unsigned int idx, uint64_t* mac, uint32_t* adi); - int getPathAtIdx(uint64_t peer_id, unsigned int idx, char* path, unsigned int len); + int getPathAtIdx(uint64_t peer_id, unsigned int idx, char* path, unsigned int len); - /** Orbit a moon */ - int orbit(void* tptr, uint64_t moonWorldId, uint64_t moonSeed); + /** Orbit a moon */ + int orbit(void* tptr, uint64_t moonWorldId, uint64_t moonSeed); - /** De-orbit a moon */ - int deorbit(void* tptr, uint64_t moonWorldId); + /** De-orbit a moon */ + int deorbit(void* tptr, uint64_t moonWorldId); - /** Return the integer-form of the node's identity */ - uint64_t getNodeId(); + /** Return the integer-form of the node's identity */ + uint64_t getNodeId(); - /** Gets the node's identity */ - int getIdentity(char* keypair, unsigned int* len); + /** Gets the node's identity */ + int getIdentity(char* keypair, unsigned int* len); - /** Set the node's identity */ - int setIdentity(const char* keypair, unsigned int len); + /** Set the node's identity */ + int setIdentity(const char* keypair, unsigned int len); - void nodeStatePutFunction(enum ZT_StateObjectType type, const uint64_t id[2], const void* data, unsigned int len); + void nodeStatePutFunction(enum ZT_StateObjectType type, const uint64_t id[2], const void* data, unsigned int len); - int nodeStateGetFunction(enum ZT_StateObjectType type, const uint64_t id[2], void* data, unsigned int maxlen); + int nodeStateGetFunction(enum ZT_StateObjectType type, const uint64_t id[2], void* data, unsigned int maxlen); - int nodeWirePacketSendFunction( - const int64_t localSocket, - const struct sockaddr_storage* addr, - const void* data, - unsigned int len, - unsigned int ttl); + int nodeWirePacketSendFunction( + const int64_t localSocket, + const struct sockaddr_storage* addr, + const void* data, + unsigned int len, + unsigned int ttl); - void nodeVirtualNetworkFrameFunction( - uint64_t net_id, - void** nuptr, - uint64_t sourceMac, - uint64_t destMac, - unsigned int etherType, - unsigned int vlanId, - const void* data, - unsigned int len); + void nodeVirtualNetworkFrameFunction( + uint64_t net_id, + void** nuptr, + uint64_t sourceMac, + uint64_t destMac, + unsigned int etherType, + unsigned int vlanId, + const void* data, + unsigned int len); - int nodePathCheckFunction(uint64_t ztaddr, const int64_t localSocket, const struct sockaddr_storage* remoteAddr); + int nodePathCheckFunction(uint64_t ztaddr, const int64_t localSocket, const struct sockaddr_storage* remoteAddr); - int nodePathLookupFunction(uint64_t ztaddr, unsigned int family, struct sockaddr_storage* result); + int nodePathLookupFunction(uint64_t ztaddr, unsigned int family, struct sockaddr_storage* result); - void tapFrameHandler( - uint64_t net_id, - const MAC& from, - const MAC& to, - unsigned int etherType, - unsigned int vlanId, - const void* data, - unsigned int len); + void tapFrameHandler( + uint64_t net_id, + const MAC& from, + const MAC& to, + unsigned int etherType, + unsigned int vlanId, + const void* data, + unsigned int len); - int shouldBindInterface(const char* ifname, const InetAddress& ifaddr); + int shouldBindInterface(const char* ifname, const InetAddress& ifaddr); - int _trialBind(unsigned int port); + int _trialBind(unsigned int port); - /** Return whether the NodeService is running */ - int isRunning() const; + /** Return whether the NodeService is running */ + int isRunning() const; - /** Return whether the node is online */ - int nodeIsOnline() const; + /** Return whether the node is online */ + int nodeIsOnline() const; - /** Instruct the NodeService on where to look for identity files and caches */ - int setHomePath(const char* homePath); - - /** Set the NodeService's primary port */ - int setPrimaryPort(unsigned short primaryPort); - - /** Get the NodeService's primary port */ - unsigned short getPrimaryPort() const; - - /** Set the event system instance used to convey messages to the user */ - int setUserEventSystem(Events* events); - - void enableEvents(); - - /** Set the world definition */ - int setWorld(const void* data, unsigned int len); - - /** Add Interface prefix to blacklist (prevents ZeroTier from using that interface) */ - int addInterfacePrefixToBlacklist(const char* prefix, unsigned int len); - - /** Return the MAC Address of the node in the given network */ - uint64_t getMACAddress(uint64_t net_id) const; - - /** Get the string format name of a network */ - int getNetworkName(uint64_t net_id, char* dst, unsigned int len) const; - - /** Allow ZeroTier to cache peer hints to storage */ - int allowPeerCaching(unsigned int allowed); - - /** Allow ZeroTier to cache network info to storage */ - int allowNetworkCaching(unsigned int allowed); - - /** Allow ZeroTier to write identities to storage */ - int allowIdentityCaching(unsigned int allowed); - - /** Allow ZeroTier to cache world definitions to storage */ - int allowWorldCaching(unsigned int allowed); - - /** Return whether broadcast is enabled on the given network */ - int getNetworkBroadcast(uint64_t net_id); - - /** Return the MTU of the given network */ - int getNetworkMTU(uint64_t net_id); - - /** Return whether the network is public or private */ - int getNetworkType(uint64_t net_id); - - /** Return the status of the network join */ - int getNetworkStatus(uint64_t net_id); - - /** Get the first address assigned by the network */ - int getFirstAssignedAddr(uint64_t net_id, unsigned int family, struct zts_sockaddr_storage* addr); - - /** Get an array of assigned addresses for the given network */ - int getAllAssignedAddr(uint64_t net_id, struct zts_sockaddr_storage* addr, unsigned int* count); - - /** Return whether a managed route of the given family has been assigned by the network */ - int networkHasRoute(uint64_t net_id, unsigned int family); - - /** Return whether an address of the given family has been assigned by the network */ - int addrIsAssigned(uint64_t net_id, unsigned int family); - - void phyOnTcpConnect(PhySocket* sock, void** uptr, bool success) - { - // Intentionally left empty - } - void phyOnTcpAccept(PhySocket* sockL, PhySocket* sockN, void** uptrL, void** uptrN, const struct sockaddr* from) - { - // Intentionally left empty - } - void phyOnTcpClose(PhySocket* sock, void** uptr) - { - // Intentionally left empty - } - void phyOnTcpData(PhySocket* sock, void** uptr, void* data, unsigned long len) - { - // Intentionally left empty - } - void phyOnTcpWritable(PhySocket* sock, void** uptr) - { - // Intentionally left empty - } - void phyOnFileDescriptorActivity(PhySocket* sock, void** uptr, bool readable, bool writable) - { - // Intentionally left empty - } - void phyOnUnixAccept(PhySocket* sockL, PhySocket* sockN, void** uptrL, void** uptrN) - { - // Intentionally left empty - } - void phyOnUnixClose(PhySocket* sock, void** uptr) - { - // Intentionally left empty - } - void phyOnUnixData(PhySocket* sock, void** uptr, void* data, unsigned long len) - { - // Intentionally left empty - } - void phyOnUnixWritable(PhySocket* sock, void** uptr) - { - // Intentionally left empty - } + /** Instruct the NodeService on where to look for identity files and caches */ + int setHomePath(const char* homePath); + + /** Set the NodeService's primary port */ + int setPrimaryPort(unsigned short primaryPort); + + /** Get the NodeService's primary port */ + unsigned short getPrimaryPort() const; + + /** Set the event system instance used to convey messages to the user */ + int setUserEventSystem(Events* events); + + void enableEvents(); + + /** Set the world definition */ + int setWorld(const void* data, unsigned int len); + + /** Add Interface prefix to blacklist (prevents ZeroTier from using that interface) */ + int addInterfacePrefixToBlacklist(const char* prefix, unsigned int len); + + /** Return the MAC Address of the node in the given network */ + uint64_t getMACAddress(uint64_t net_id) const; + + /** Get the string format name of a network */ + int getNetworkName(uint64_t net_id, char* dst, unsigned int len) const; + + /** Allow ZeroTier to cache peer hints to storage */ + int allowPeerCaching(unsigned int allowed); + + /** Allow ZeroTier to cache network info to storage */ + int allowNetworkCaching(unsigned int allowed); + + /** Allow ZeroTier to write identities to storage */ + int allowIdentityCaching(unsigned int allowed); + + /** Allow ZeroTier to cache world definitions to storage */ + int allowWorldCaching(unsigned int allowed); + + /** Return whether broadcast is enabled on the given network */ + int getNetworkBroadcast(uint64_t net_id); + + /** Return the MTU of the given network */ + int getNetworkMTU(uint64_t net_id); + + /** Return whether the network is public or private */ + int getNetworkType(uint64_t net_id); + + /** Return the status of the network join */ + int getNetworkStatus(uint64_t net_id); + + /** Get the first address assigned by the network */ + int getFirstAssignedAddr(uint64_t net_id, unsigned int family, struct zts_sockaddr_storage* addr); + + /** Get an array of assigned addresses for the given network */ + int getAllAssignedAddr(uint64_t net_id, struct zts_sockaddr_storage* addr, unsigned int* count); + + /** Return whether a managed route of the given family has been assigned by the network */ + int networkHasRoute(uint64_t net_id, unsigned int family); + + /** Return whether an address of the given family has been assigned by the network */ + int addrIsAssigned(uint64_t net_id, unsigned int family); + + void phyOnTcpConnect(PhySocket* sock, void** uptr, bool success) + { + // Intentionally left empty + } + void phyOnTcpAccept(PhySocket* sockL, PhySocket* sockN, void** uptrL, void** uptrN, const struct sockaddr* from) + { + // Intentionally left empty + } + void phyOnTcpClose(PhySocket* sock, void** uptr) + { + // Intentionally left empty + } + void phyOnTcpData(PhySocket* sock, void** uptr, void* data, unsigned long len) + { + // Intentionally left empty + } + void phyOnTcpWritable(PhySocket* sock, void** uptr) + { + // Intentionally left empty + } + void phyOnFileDescriptorActivity(PhySocket* sock, void** uptr, bool readable, bool writable) + { + // Intentionally left empty + } + void phyOnUnixAccept(PhySocket* sockL, PhySocket* sockN, void** uptrL, void** uptrN) + { + // Intentionally left empty + } + void phyOnUnixClose(PhySocket* sock, void** uptr) + { + // Intentionally left empty + } + void phyOnUnixData(PhySocket* sock, void** uptr, void* data, unsigned long len) + { + // Intentionally left empty + } + void phyOnUnixWritable(PhySocket* sock, void** uptr) + { + // Intentionally left empty + } }; static int SnodeVirtualNetworkConfigFunction( @@ -472,12 +472,12 @@ static int SnodeVirtualNetworkConfigFunction( enum ZT_VirtualNetworkConfigOperation op, const ZT_VirtualNetworkConfig* nwconf) { - return reinterpret_cast(uptr)->nodeVirtualNetworkConfigFunction(net_id, nuptr, op, nwconf); + return reinterpret_cast(uptr)->nodeVirtualNetworkConfigFunction(net_id, nuptr, op, nwconf); } static void SnodeEventCallback(ZT_Node* node, void* uptr, void* tptr, enum ZT_Event event, const void* metaData) { - reinterpret_cast(uptr)->nodeEventCallback(event, metaData); + reinterpret_cast(uptr)->nodeEventCallback(event, metaData); } static void SnodeStatePutFunction( @@ -489,7 +489,7 @@ static void SnodeStatePutFunction( const void* data, int len) { - reinterpret_cast(uptr)->nodeStatePutFunction(type, id, data, len); + reinterpret_cast(uptr)->nodeStatePutFunction(type, id, data, len); } static int SnodeStateGetFunction( @@ -501,7 +501,7 @@ static int SnodeStateGetFunction( void* data, unsigned int maxlen) { - return reinterpret_cast(uptr)->nodeStateGetFunction(type, id, data, maxlen); + return reinterpret_cast(uptr)->nodeStateGetFunction(type, id, data, maxlen); } static int SnodeWirePacketSendFunction( @@ -514,7 +514,7 @@ static int SnodeWirePacketSendFunction( unsigned int len, unsigned int ttl) { - return reinterpret_cast(uptr)->nodeWirePacketSendFunction(localSocket, addr, data, len, ttl); + return reinterpret_cast(uptr)->nodeWirePacketSendFunction(localSocket, addr, data, len, ttl); } static void SnodeVirtualNetworkFrameFunction( @@ -530,8 +530,8 @@ static void SnodeVirtualNetworkFrameFunction( const void* data, unsigned int len) { - reinterpret_cast(uptr) - ->nodeVirtualNetworkFrameFunction(net_id, nuptr, sourceMac, destMac, etherType, vlanId, data, len); + reinterpret_cast(uptr) + ->nodeVirtualNetworkFrameFunction(net_id, nuptr, sourceMac, destMac, etherType, vlanId, data, len); } static int SnodePathCheckFunction( @@ -542,7 +542,7 @@ static int SnodePathCheckFunction( int64_t localSocket, const struct sockaddr_storage* remoteAddr) { - return reinterpret_cast(uptr)->nodePathCheckFunction(ztaddr, localSocket, remoteAddr); + return reinterpret_cast(uptr)->nodePathCheckFunction(ztaddr, localSocket, remoteAddr); } static int SnodePathLookupFunction( @@ -553,7 +553,7 @@ static int SnodePathLookupFunction( int family, struct sockaddr_storage* result) { - return reinterpret_cast(uptr)->nodePathLookupFunction(ztaddr, family, result); + return reinterpret_cast(uptr)->nodePathLookupFunction(ztaddr, family, result); } static void StapFrameHandler( @@ -567,7 +567,7 @@ static void StapFrameHandler( const void* data, unsigned int len) { - reinterpret_cast(uptr)->tapFrameHandler(net_id, from, to, etherType, vlanId, data, len); + reinterpret_cast(uptr)->tapFrameHandler(net_id, from, to, etherType, vlanId, data, len); } } // namespace ZeroTier diff --git a/src/Signals.cpp b/src/Signals.cpp index 7d6652d..7769f6c 100644 --- a/src/Signals.cpp +++ b/src/Signals.cpp @@ -38,43 +38,43 @@ void _signal_handler(int signal) { - /* - switch(signal) - { - case SIGINT: - fprintf(stderr, "SIGINT\n"); - break; - case SIGABRT: - fprintf(stderr, "SIGABRT\n"); - break; - case SIGILL: - fprintf(stderr, "SIGILL\n"); - break; - case SIGSEGV: - fprintf(stderr, "SIGSEGV\n"); - break; - case SIGFPE: - fprintf(stderr, "SIGFPE\n"); - break; - case SIGTERM: - default: - fprintf(stderr, "SIGTERM\n"); - break; - } - */ - exit(signal); + /* + switch(signal) + { + case SIGINT: + fprintf(stderr, "SIGINT\n"); + break; + case SIGABRT: + fprintf(stderr, "SIGABRT\n"); + break; + case SIGILL: + fprintf(stderr, "SIGILL\n"); + break; + case SIGSEGV: + fprintf(stderr, "SIGSEGV\n"); + break; + case SIGFPE: + fprintf(stderr, "SIGFPE\n"); + break; + case SIGTERM: + default: + fprintf(stderr, "SIGTERM\n"); + break; + } + */ + exit(signal); } void _install_signal_handlers() { - signal(SIGINT, &_signal_handler); - /* - signal(SIGABRT, &_signal_handler); - signal(SIGFPE, &_signal_handler); - signal(SIGILL, &_signal_handler); - signal(SIGSEGV, &_signal_handler); - signal(SIGTERM, &_signal_handler); - */ + signal(SIGINT, &_signal_handler); + /* + signal(SIGABRT, &_signal_handler); + signal(SIGFPE, &_signal_handler); + signal(SIGILL, &_signal_handler); + signal(SIGSEGV, &_signal_handler); + signal(SIGTERM, &_signal_handler); + */ } #endif diff --git a/src/Sockets.cpp b/src/Sockets.cpp index 0864bc1..78c7ce8 100644 --- a/src/Sockets.cpp +++ b/src/Sockets.cpp @@ -40,451 +40,451 @@ extern "C" { int zts_socket(const int socket_family, const int socket_type, const int protocol) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_socket(socket_family, socket_type, protocol); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_socket(socket_family, socket_type, protocol); } int zts_connect(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! addr) { - return ZTS_ERR_ARG; - } - if (addrlen > (zts_socklen_t)sizeof(struct zts_sockaddr_storage) - || addrlen < (zts_socklen_t)sizeof(struct zts_sockaddr_in)) { - return ZTS_ERR_ARG; - } - return lwip_connect(fd, (sockaddr*)addr, addrlen); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! addr) { + return ZTS_ERR_ARG; + } + if (addrlen > (zts_socklen_t)sizeof(struct zts_sockaddr_storage) + || addrlen < (zts_socklen_t)sizeof(struct zts_sockaddr_in)) { + return ZTS_ERR_ARG; + } + return lwip_connect(fd, (sockaddr*)addr, addrlen); } int zts_simple_connect(int fd, const char* ipstr, int port, int timeout_ms) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (timeout_ms < 0) { - return ZTS_ERR_ARG; - } - if (timeout_ms == 0) { - timeout_ms = 30000; // Default - } - int div = 4; // Must be > 0, Four connection attempts per second - int n_tries = (timeout_ms / 1000) * div; - int connect_delay = 1000 / div; - int err = ZTS_ERR_SOCKET; + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (timeout_ms < 0) { + return ZTS_ERR_ARG; + } + if (timeout_ms == 0) { + timeout_ms = 30000; // Default + } + int div = 4; // Must be > 0, Four connection attempts per second + int n_tries = (timeout_ms / 1000) * div; + int connect_delay = 1000 / div; + int err = ZTS_ERR_SOCKET; - zts_socklen_t addrlen = 0; - struct zts_sockaddr_storage ss; - struct zts_sockaddr* sa = NULL; + zts_socklen_t addrlen = 0; + struct zts_sockaddr_storage ss; + struct zts_sockaddr* sa = NULL; - // Convert to standard address structure + // Convert to standard address structure - addrlen = sizeof(ss); - zts_util_ipstr_to_saddr(ipstr, port, (struct zts_sockaddr*)&ss, &addrlen); - sa = (struct zts_sockaddr*)&ss; + addrlen = sizeof(ss); + zts_util_ipstr_to_saddr(ipstr, port, (struct zts_sockaddr*)&ss, &addrlen); + sa = (struct zts_sockaddr*)&ss; - if (addrlen > 0 && sa != NULL) { - if (zts_simple_get_blocking(fd)) { - do { - err = zts_connect(fd, sa, addrlen); - zts_util_delay(connect_delay); - n_tries--; - } while ((err < 0) && (zts_errno != 0) && (n_tries > 0)); - } - return err; - } - return ZTS_ERR_ARG; + if (addrlen > 0 && sa != NULL) { + if (zts_simple_get_blocking(fd)) { + do { + err = zts_connect(fd, sa, addrlen); + zts_util_delay(connect_delay); + n_tries--; + } while ((err < 0) && (zts_errno != 0) && (n_tries > 0)); + } + return err; + } + return ZTS_ERR_ARG; } int zts_bind(int fd, const struct zts_sockaddr* addr, zts_socklen_t addrlen) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! addr) { - return ZTS_ERR_ARG; - } - if (addrlen > (int)sizeof(struct zts_sockaddr_storage) || addrlen < (int)sizeof(struct zts_sockaddr_in)) { - return ZTS_ERR_ARG; - } - return lwip_bind(fd, (sockaddr*)addr, addrlen); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! addr) { + return ZTS_ERR_ARG; + } + if (addrlen > (int)sizeof(struct zts_sockaddr_storage) || addrlen < (int)sizeof(struct zts_sockaddr_in)) { + return ZTS_ERR_ARG; + } + return lwip_bind(fd, (sockaddr*)addr, addrlen); } int zts_simple_bind(int fd, const char* ipstr, int port) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } - zts_socklen_t addrlen = 0; - struct zts_sockaddr_storage ss; - struct zts_sockaddr* sa = NULL; + zts_socklen_t addrlen = 0; + struct zts_sockaddr_storage ss; + struct zts_sockaddr* sa = NULL; - addrlen = sizeof(ss); - zts_util_ipstr_to_saddr(ipstr, port, (struct zts_sockaddr*)&ss, &addrlen); - sa = (struct zts_sockaddr*)&ss; + addrlen = sizeof(ss); + zts_util_ipstr_to_saddr(ipstr, port, (struct zts_sockaddr*)&ss, &addrlen); + sa = (struct zts_sockaddr*)&ss; - return zts_bind(fd, sa, addrlen); + return zts_bind(fd, sa, addrlen); } int zts_listen(int fd, int backlog) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_listen(fd, backlog); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_listen(fd, backlog); } int zts_accept(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_accept(fd, (sockaddr*)addr, (socklen_t*)addrlen); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_accept(fd, (sockaddr*)addr, (socklen_t*)addrlen); } int zts_simple_accept(int fd, char* remote_addr, int len, int* port) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (len != ZTS_INET6_ADDRSTRLEN) { - return ZTS_ERR_ARG; - } - zts_sockaddr_storage ss; - zts_socklen_t addrlen = sizeof(ss); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (len != ZTS_INET6_ADDRSTRLEN) { + return ZTS_ERR_ARG; + } + zts_sockaddr_storage ss; + zts_socklen_t addrlen = sizeof(ss); - int acc_fd = zts_accept(fd, (zts_sockaddr*)&ss, (zts_socklen_t*)&addrlen); - struct zts_sockaddr* sa = (struct zts_sockaddr*)&ss; - if (sa->sa_family == ZTS_AF_INET) { - struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)sa; - zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), remote_addr, ZTS_INET_ADDRSTRLEN); - *port = ntohs(in4->sin_port); - } - if (sa->sa_family == ZTS_AF_INET6) { - struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)sa; - zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), remote_addr, ZTS_INET6_ADDRSTRLEN); - *port = ntohs(in6->sin6_port); - } - return acc_fd; + int acc_fd = zts_accept(fd, (zts_sockaddr*)&ss, (zts_socklen_t*)&addrlen); + struct zts_sockaddr* sa = (struct zts_sockaddr*)&ss; + if (sa->sa_family == ZTS_AF_INET) { + struct zts_sockaddr_in* in4 = (struct zts_sockaddr_in*)sa; + zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr), remote_addr, ZTS_INET_ADDRSTRLEN); + *port = ntohs(in4->sin_port); + } + if (sa->sa_family == ZTS_AF_INET6) { + struct zts_sockaddr_in6* in6 = (struct zts_sockaddr_in6*)sa; + zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr), remote_addr, ZTS_INET6_ADDRSTRLEN); + *port = ntohs(in6->sin6_port); + } + return acc_fd; } int zts_simple_tcp_client(const char* remote_ipstr, int remote_port) { - int fd, family = zts_util_get_ip_family(remote_ipstr); - if ((fd = zts_socket(family, ZTS_SOCK_STREAM, 0)) < 0) { - return fd; // Failed to create socket - } - int timeout = 0; - if ((fd = zts_simple_connect(fd, remote_ipstr, remote_port, timeout)) < 0) { - zts_close(fd); - return fd; // Failed to connect - } - return fd; + int fd, family = zts_util_get_ip_family(remote_ipstr); + if ((fd = zts_socket(family, ZTS_SOCK_STREAM, 0)) < 0) { + return fd; // Failed to create socket + } + int timeout = 0; + if ((fd = zts_simple_connect(fd, remote_ipstr, remote_port, timeout)) < 0) { + zts_close(fd); + return fd; // Failed to connect + } + return fd; } int zts_simple_tcp_server(const char* local_ipstr, int local_port, char* remote_ipstr, int len, int* remote_port) { - int listen_fd, family = zts_util_get_ip_family(local_ipstr); - if ((listen_fd = zts_socket(family, ZTS_SOCK_STREAM, 0)) < 0) { - return listen_fd; // Failed to create socket - } - if ((listen_fd = zts_simple_bind(listen_fd, local_ipstr, local_port)) < 0) { - return listen_fd; // Failed to bind - } - int backlog = 0; - if ((listen_fd = zts_listen(listen_fd, backlog)) < 0) { - return listen_fd; // Failed to listen - } - int acc_fd = 0; - if ((acc_fd = zts_simple_accept(listen_fd, remote_ipstr, len, remote_port)) < 0) { - return acc_fd; // Failed to accept - } - zts_close(listen_fd); - return acc_fd; + int listen_fd, family = zts_util_get_ip_family(local_ipstr); + if ((listen_fd = zts_socket(family, ZTS_SOCK_STREAM, 0)) < 0) { + return listen_fd; // Failed to create socket + } + if ((listen_fd = zts_simple_bind(listen_fd, local_ipstr, local_port)) < 0) { + return listen_fd; // Failed to bind + } + int backlog = 0; + if ((listen_fd = zts_listen(listen_fd, backlog)) < 0) { + return listen_fd; // Failed to listen + } + int acc_fd = 0; + if ((acc_fd = zts_simple_accept(listen_fd, remote_ipstr, len, remote_port)) < 0) { + return acc_fd; // Failed to accept + } + zts_close(listen_fd); + return acc_fd; } int zts_simple_udp_server(const char* local_ipstr, int local_port) { - int fd, family = zts_util_get_ip_family(local_ipstr); - if ((fd = zts_socket(family, ZTS_SOCK_DGRAM, 0)) < 0) { - return fd; // Failed to create socket - } - if ((fd = zts_simple_bind(fd, local_ipstr, local_port)) < 0) { - zts_close(fd); - return fd; // Failed to connect - } - return fd; + int fd, family = zts_util_get_ip_family(local_ipstr); + if ((fd = zts_socket(family, ZTS_SOCK_DGRAM, 0)) < 0) { + return fd; // Failed to create socket + } + if ((fd = zts_simple_bind(fd, local_ipstr, local_port)) < 0) { + zts_close(fd); + return fd; // Failed to connect + } + return fd; } int zts_simple_udp_client(const char* remote_ipstr) { - int fd, family = zts_util_get_ip_family(remote_ipstr); - if ((fd = zts_socket(family, ZTS_SOCK_DGRAM, 0)) < 0) { - return fd; // Failed to create socket - } - return fd; + int fd, family = zts_util_get_ip_family(remote_ipstr); + if ((fd = zts_socket(family, ZTS_SOCK_DGRAM, 0)) < 0) { + return fd; // Failed to create socket + } + return fd; } int zts_setsockopt(int fd, int level, int optname, const void* optval, zts_socklen_t optlen) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_setsockopt(fd, level, optname, optval, optlen); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_setsockopt(fd, level, optname, optval, optlen); } int zts_getsockopt(int fd, int level, int optname, void* optval, zts_socklen_t* optlen) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_getsockopt(fd, level, optname, optval, (socklen_t*)optlen); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_getsockopt(fd, level, optname, optval, (socklen_t*)optlen); } int zts_getsockname(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! addr) { - return ZTS_ERR_ARG; - } - if (*addrlen > (int)sizeof(struct zts_sockaddr_storage) || *addrlen < (int)sizeof(struct zts_sockaddr_in)) { - return ZTS_ERR_ARG; - } - return lwip_getsockname(fd, (sockaddr*)addr, (socklen_t*)addrlen); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! addr) { + return ZTS_ERR_ARG; + } + if (*addrlen > (int)sizeof(struct zts_sockaddr_storage) || *addrlen < (int)sizeof(struct zts_sockaddr_in)) { + return ZTS_ERR_ARG; + } + return lwip_getsockname(fd, (sockaddr*)addr, (socklen_t*)addrlen); } int zts_getpeername(int fd, struct zts_sockaddr* addr, zts_socklen_t* addrlen) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! addr) { - return ZTS_ERR_ARG; - } - if (*addrlen > (int)sizeof(struct zts_sockaddr_storage) || *addrlen < (int)sizeof(struct zts_sockaddr_in)) { - return ZTS_ERR_ARG; - } - return lwip_getpeername(fd, (sockaddr*)addr, (socklen_t*)addrlen); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! addr) { + return ZTS_ERR_ARG; + } + if (*addrlen > (int)sizeof(struct zts_sockaddr_storage) || *addrlen < (int)sizeof(struct zts_sockaddr_in)) { + return ZTS_ERR_ARG; + } + return lwip_getpeername(fd, (sockaddr*)addr, (socklen_t*)addrlen); } int zts_close(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_close(fd); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_close(fd); } int zts_select(int nfds, zts_fd_set* readfds, zts_fd_set* writefds, zts_fd_set* exceptfds, struct zts_timeval* timeout) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_select(nfds, (fd_set*)readfds, (fd_set*)writefds, (fd_set*)exceptfds, (timeval*)timeout); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_select(nfds, (fd_set*)readfds, (fd_set*)writefds, (fd_set*)exceptfds, (timeval*)timeout); } int zts_fcntl(int fd, int cmd, int flags) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_fcntl(fd, cmd, flags); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_fcntl(fd, cmd, flags); } int zts_poll(struct zts_pollfd* fds, nfds_t nfds, int timeout) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_poll((pollfd*)fds, nfds, timeout); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_poll((pollfd*)fds, nfds, timeout); } int zts_ioctl(int fd, unsigned long request, void* argp) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! argp) { - return ZTS_ERR_ARG; - } - return lwip_ioctl(fd, request, argp); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! argp) { + return ZTS_ERR_ARG; + } + return lwip_ioctl(fd, request, argp); } ssize_t zts_send(int fd, const void* buf, size_t len, int flags) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! buf) { - return ZTS_ERR_ARG; - } - return lwip_send(fd, buf, len, flags); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! buf) { + return ZTS_ERR_ARG; + } + return lwip_send(fd, buf, len, flags); } ssize_t zts_sendto(int fd, const void* buf, size_t len, int flags, const struct zts_sockaddr* addr, zts_socklen_t addrlen) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! addr || ! buf) { - return ZTS_ERR_ARG; - } - if (addrlen > (int)sizeof(struct zts_sockaddr_storage) || addrlen < (int)sizeof(struct zts_sockaddr_in)) { - return ZTS_ERR_ARG; - } - return lwip_sendto(fd, buf, len, flags, (sockaddr*)addr, addrlen); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! addr || ! buf) { + return ZTS_ERR_ARG; + } + if (addrlen > (int)sizeof(struct zts_sockaddr_storage) || addrlen < (int)sizeof(struct zts_sockaddr_in)) { + return ZTS_ERR_ARG; + } + return lwip_sendto(fd, buf, len, flags, (sockaddr*)addr, addrlen); } ssize_t zts_sendmsg(int fd, const struct zts_msghdr* msg, int flags) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_sendmsg(fd, (const struct msghdr*)msg, flags); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_sendmsg(fd, (const struct msghdr*)msg, flags); } ssize_t zts_recv(int fd, void* buf, size_t len, int flags) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! buf) { - return ZTS_ERR_ARG; - } - return lwip_recv(fd, buf, len, flags); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! buf) { + return ZTS_ERR_ARG; + } + return lwip_recv(fd, buf, len, flags); } ssize_t zts_recvfrom(int fd, void* buf, size_t len, int flags, struct zts_sockaddr* addr, zts_socklen_t* addrlen) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! buf) { - return ZTS_ERR_ARG; - } - return lwip_recvfrom(fd, buf, len, flags, (sockaddr*)addr, (socklen_t*)addrlen); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! buf) { + return ZTS_ERR_ARG; + } + return lwip_recvfrom(fd, buf, len, flags, (sockaddr*)addr, (socklen_t*)addrlen); } ssize_t zts_recvmsg(int fd, struct zts_msghdr* msg, int flags) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! msg) { - return ZTS_ERR_ARG; - } - return lwip_recvmsg(fd, (struct msghdr*)msg, flags); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! msg) { + return ZTS_ERR_ARG; + } + return lwip_recvmsg(fd, (struct msghdr*)msg, flags); } ssize_t zts_read(int fd, void* buf, size_t len) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! buf) { - return ZTS_ERR_ARG; - } - return lwip_read(fd, buf, len); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! buf) { + return ZTS_ERR_ARG; + } + return lwip_read(fd, buf, len); } ssize_t zts_readv(int fd, const struct zts_iovec* iov, int iovcnt) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_readv(fd, (iovec*)iov, iovcnt); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_readv(fd, (iovec*)iov, iovcnt); } ssize_t zts_write(int fd, const void* buf, size_t len) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (! buf) { - return ZTS_ERR_ARG; - } - return lwip_write(fd, buf, len); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (! buf) { + return ZTS_ERR_ARG; + } + return lwip_write(fd, buf, len); } ssize_t zts_writev(int fd, const struct zts_iovec* iov, int iovcnt) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_writev(fd, (iovec*)iov, iovcnt); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_writev(fd, (iovec*)iov, iovcnt); } int zts_shutdown(int fd, int how) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - return lwip_shutdown(fd, how); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + return lwip_shutdown(fd, how); } struct zts_hostent* zts_gethostbyname(const char* name) { - if (! transport_ok()) { - return NULL; - } - if (! name) { - return NULL; - } - return (struct zts_hostent*)lwip_gethostbyname(name); + if (! transport_ok()) { + return NULL; + } + if (! name) { + return NULL; + } + return (struct zts_hostent*)lwip_gethostbyname(name); } int zts_dns_set_server(uint8_t index, const zts_ip_addr* addr) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (index >= DNS_MAX_SERVERS) { - return ZTS_ERR_ARG; - } - if (! addr) { - return ZTS_ERR_ARG; - } - dns_setserver(index, (const ip_addr_t*)addr); - return ZTS_ERR_OK; + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (index >= DNS_MAX_SERVERS) { + return ZTS_ERR_ARG; + } + if (! addr) { + return ZTS_ERR_ARG; + } + dns_setserver(index, (const ip_addr_t*)addr); + return ZTS_ERR_OK; } const zts_ip_addr* zts_dns_get_server(uint8_t index) { - if (! transport_ok()) { - return NULL; - } - if (index >= DNS_MAX_SERVERS) { - return NULL; - } - return (const zts_ip_addr*)dns_getserver(index); + if (! transport_ok()) { + return NULL; + } + if (index >= DNS_MAX_SERVERS) { + return NULL; + } + return (const zts_ip_addr*)dns_getserver(index); } char* zts_ipaddr_ntoa(const zts_ip_addr* addr) { - return ipaddr_ntoa((ip_addr_t*)addr); + return ipaddr_ntoa((ip_addr_t*)addr); } int zts_ipaddr_aton(const char* cp, zts_ip_addr* addr) { - return ipaddr_aton(cp, (ip_addr_t*)addr); + return ipaddr_aton(cp, (ip_addr_t*)addr); } const char* zts_inet_ntop(int family, const void* src, char* dst, zts_socklen_t size) { - return lwip_inet_ntop(family, src, dst, size); + return lwip_inet_ntop(family, src, dst, size); } int zts_inet_pton(int family, const char* src, void* dst) { - return lwip_inet_pton(family, src, dst); + return lwip_inet_pton(family, src, dst); } int zts_util_ipstr_to_saddr( @@ -493,35 +493,35 @@ int zts_util_ipstr_to_saddr( struct zts_sockaddr* dest_addr, zts_socklen_t* addrlen) { - int family = zts_util_get_ip_family(src_ipstr); + int family = zts_util_get_ip_family(src_ipstr); - if (family == ZTS_AF_INET) { - struct zts_sockaddr_in in4; - in4.sin_port = htons(port); - in4.sin_family = family; + if (family == ZTS_AF_INET) { + struct zts_sockaddr_in in4; + in4.sin_port = htons(port); + in4.sin_family = family; #if defined(_WIN32) - zts_inet_pton(family, src_ipstr, &(in4.sin_addr.S_addr)); + zts_inet_pton(family, src_ipstr, &(in4.sin_addr.S_addr)); #else - zts_inet_pton(family, src_ipstr, &(in4.sin_addr.s_addr)); + zts_inet_pton(family, src_ipstr, &(in4.sin_addr.s_addr)); #endif - dest_addr = (struct zts_sockaddr*)&in4; - *addrlen = sizeof(in4); - return ZTS_ERR_OK; - } - if (family == ZTS_AF_INET6) { - struct zts_sockaddr_in6 in6; - in6.sin6_port = htons(port); - in6.sin6_family = family; + dest_addr = (struct zts_sockaddr*)&in4; + *addrlen = sizeof(in4); + return ZTS_ERR_OK; + } + if (family == ZTS_AF_INET6) { + struct zts_sockaddr_in6 in6; + in6.sin6_port = htons(port); + in6.sin6_family = family; #if defined(_WIN32) - zts_inet_pton(family, src_ipstr, &(in6.sin6_addr)); + zts_inet_pton(family, src_ipstr, &(in6.sin6_addr)); #else - zts_inet_pton(family, src_ipstr, &(in6.sin6_addr)); + zts_inet_pton(family, src_ipstr, &(in6.sin6_addr)); #endif - dest_addr = (struct zts_sockaddr*)&in6; - *addrlen = sizeof(in6); - return ZTS_ERR_OK; - } - return ZTS_ERR_ARG; + dest_addr = (struct zts_sockaddr*)&in6; + *addrlen = sizeof(in6); + return ZTS_ERR_OK; + } + return ZTS_ERR_ARG; } //----------------------------------------------------------------------------// @@ -538,278 +538,278 @@ int zts_util_ipstr_to_saddr( int zts_simple_set_no_delay(int fd, int enabled) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (enabled != 0 && enabled != 1) { - return ZTS_ERR_ARG; - } - return zts_setsockopt(fd, ZTS_IPPROTO_TCP, ZTS_TCP_NODELAY, (void*)&enabled, sizeof(int)); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (enabled != 0 && enabled != 1) { + return ZTS_ERR_ARG; + } + return zts_setsockopt(fd, ZTS_IPPROTO_TCP, ZTS_TCP_NODELAY, (void*)&enabled, sizeof(int)); } int zts_simple_get_no_delay(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - int err, optval = 0; - zts_socklen_t len = sizeof(optval); - if ((err = zts_getsockopt(fd, ZTS_IPPROTO_TCP, ZTS_TCP_NODELAY, (void*)&optval, &len)) < 0) { - return err; - } - return optval != 0; + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + int err, optval = 0; + zts_socklen_t len = sizeof(optval); + if ((err = zts_getsockopt(fd, ZTS_IPPROTO_TCP, ZTS_TCP_NODELAY, (void*)&optval, &len)) < 0) { + return err; + } + return optval != 0; } int zts_simple_set_linger(int fd, int enabled, int value) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (enabled != 0 && enabled != 1) { - return ZTS_ERR_ARG; - } - if (value < 0) { - return ZTS_ERR_ARG; - } - struct zts_linger linger; - linger.l_onoff = enabled; - linger.l_linger = value; - return zts_setsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_LINGER, (void*)&linger, sizeof(linger)); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (enabled != 0 && enabled != 1) { + return ZTS_ERR_ARG; + } + if (value < 0) { + return ZTS_ERR_ARG; + } + struct zts_linger linger; + linger.l_onoff = enabled; + linger.l_linger = value; + return zts_setsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_LINGER, (void*)&linger, sizeof(linger)); } int zts_simple_get_linger_enabled(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - struct zts_linger linger; - zts_socklen_t len = sizeof(linger); - int err; - if ((err = zts_getsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_LINGER, (void*)&linger, &len)) < 0) { - return err; - } - return linger.l_onoff; + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + struct zts_linger linger; + zts_socklen_t len = sizeof(linger); + int err; + if ((err = zts_getsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_LINGER, (void*)&linger, &len)) < 0) { + return err; + } + return linger.l_onoff; } int zts_simple_get_linger_value(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - struct zts_linger linger; - zts_socklen_t len = sizeof(linger); - int err; - if ((err = zts_getsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_LINGER, (void*)&linger, &len)) < 0) { - return err; - } - return linger.l_linger; + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + struct zts_linger linger; + zts_socklen_t len = sizeof(linger); + int err; + if ((err = zts_getsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_LINGER, (void*)&linger, &len)) < 0) { + return err; + } + return linger.l_linger; } int zts_simple_set_reuse_addr(int fd, int enabled) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (enabled != 0 && enabled != 1) { - return ZTS_ERR_ARG; - } - return zts_setsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_REUSEADDR, (void*)&enabled, sizeof(enabled)); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (enabled != 0 && enabled != 1) { + return ZTS_ERR_ARG; + } + return zts_setsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_REUSEADDR, (void*)&enabled, sizeof(enabled)); } int zts_simple_get_reuse_addr(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - int err, optval = 0; - zts_socklen_t optlen = sizeof(optval); - if ((err = zts_getsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_REUSEADDR, (void*)&optval, &optlen)) < 0) { - return err; - } - return optval != 0; + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + int err, optval = 0; + zts_socklen_t optlen = sizeof(optval); + if ((err = zts_getsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_REUSEADDR, (void*)&optval, &optlen)) < 0) { + return err; + } + return optval != 0; } int zts_simple_set_recv_timeout(int fd, int seconds, int microseconds) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (seconds < 0 || microseconds < 0) { - return ZTS_ERR_ARG; - } - struct timeval tv; - tv.tv_sec = seconds; - tv.tv_usec = microseconds; - return zts_setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void*)&tv, sizeof(tv)); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (seconds < 0 || microseconds < 0) { + return ZTS_ERR_ARG; + } + struct timeval tv; + tv.tv_sec = seconds; + tv.tv_usec = microseconds; + return zts_setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void*)&tv, sizeof(tv)); } int zts_simple_get_recv_timeout(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - struct timeval tv; - zts_socklen_t optlen = sizeof(tv); - int err; - if ((err = zts_getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void*)&tv, &optlen)) < 0) { - return err; - } - return tv.tv_sec; // TODO microseconds + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + struct timeval tv; + zts_socklen_t optlen = sizeof(tv); + int err; + if ((err = zts_getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (void*)&tv, &optlen)) < 0) { + return err; + } + return tv.tv_sec; // TODO microseconds } int zts_simple_set_send_timeout(int fd, int seconds, int microseconds) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (seconds < 0 || microseconds < 0) { - return ZTS_ERR_ARG; - } - struct timeval tv; - tv.tv_sec = seconds; - tv.tv_usec = microseconds; - return zts_setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (void*)&tv, sizeof(tv)); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (seconds < 0 || microseconds < 0) { + return ZTS_ERR_ARG; + } + struct timeval tv; + tv.tv_sec = seconds; + tv.tv_usec = microseconds; + return zts_setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (void*)&tv, sizeof(tv)); } int zts_simple_get_send_timeout(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - struct zts_timeval tv; - zts_socklen_t optlen = sizeof(tv); - int err; - if ((err = zts_getsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (void*)&tv, &optlen)) < 0) { - return err; - } - return tv.tv_sec; // TODO microseconds + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + struct zts_timeval tv; + zts_socklen_t optlen = sizeof(tv); + int err; + if ((err = zts_getsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (void*)&tv, &optlen)) < 0) { + return err; + } + return tv.tv_sec; // TODO microseconds } int zts_simple_set_send_buf_size(int fd, int size) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (size < 0) { - return ZTS_ERR_ARG; - } - return zts_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void*)&size, sizeof(int)); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (size < 0) { + return ZTS_ERR_ARG; + } + return zts_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void*)&size, sizeof(int)); } int zts_simple_get_send_buf_size(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - int err, optval = 0; - zts_socklen_t optlen = sizeof(optval); - if ((err = zts_getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&optval, &optlen)) < 0) { - return err; - } - return optval; + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + int err, optval = 0; + zts_socklen_t optlen = sizeof(optval); + if ((err = zts_getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char*)&optval, &optlen)) < 0) { + return err; + } + return optval; } int zts_simple_set_recv_buf_size(int fd, int size) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (size < 0) { - return ZTS_ERR_ARG; - } - return zts_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void*)&size, sizeof(int)); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (size < 0) { + return ZTS_ERR_ARG; + } + return zts_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void*)&size, sizeof(int)); } int zts_simple_get_recv_buf_size(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - int err, optval = 0; - zts_socklen_t optlen = sizeof(optval); - if ((err = zts_getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&optval, &optlen)) < 0) { - return err; - } - return optval; + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + int err, optval = 0; + zts_socklen_t optlen = sizeof(optval); + if ((err = zts_getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char*)&optval, &optlen)) < 0) { + return err; + } + return optval; } int zts_simple_set_ttl(int fd, int ttl) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (ttl < 0 || ttl > 255) { - return ZTS_ERR_ARG; - } - return zts_setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (ttl < 0 || ttl > 255) { + return ZTS_ERR_ARG; + } + return zts_setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); } int zts_simple_get_ttl(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - int err, ttl = 0; - zts_socklen_t optlen = sizeof(ttl); - if ((err = zts_getsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, &optlen)) < 0) { - return err; - } - return ttl; + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + int err, ttl = 0; + zts_socklen_t optlen = sizeof(ttl); + if ((err = zts_getsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, &optlen)) < 0) { + return err; + } + return ttl; } int zts_simple_set_blocking(int fd, int enabled) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (enabled != 0 && enabled != 1) { - return ZTS_ERR_ARG; - } - int flags = zts_fcntl(fd, ZTS_F_GETFL, 0); - if (! enabled) { - return zts_fcntl(fd, ZTS_F_SETFL, flags | ZTS_O_NONBLOCK); - } - else { - // Default - return zts_fcntl(fd, ZTS_F_SETFL, flags & (~ZTS_O_NONBLOCK)); - } + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (enabled != 0 && enabled != 1) { + return ZTS_ERR_ARG; + } + int flags = zts_fcntl(fd, ZTS_F_GETFL, 0); + if (! enabled) { + return zts_fcntl(fd, ZTS_F_SETFL, flags | ZTS_O_NONBLOCK); + } + else { + // Default + return zts_fcntl(fd, ZTS_F_SETFL, flags & (~ZTS_O_NONBLOCK)); + } } int zts_simple_get_blocking(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - int flags = zts_fcntl(fd, ZTS_F_GETFL, 0); - if (flags < 0) { - return flags; - } - return ! (flags & ZTS_O_NONBLOCK); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + int flags = zts_fcntl(fd, ZTS_F_GETFL, 0); + if (flags < 0) { + return flags; + } + return ! (flags & ZTS_O_NONBLOCK); } int zts_simple_set_keepalive(int fd, int enabled) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - if (enabled != 0 && enabled != 1) { - return ZTS_ERR_ARG; - } - int keepalive = enabled; - return zts_setsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_KEEPALIVE, &keepalive, sizeof(keepalive)); + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + if (enabled != 0 && enabled != 1) { + return ZTS_ERR_ARG; + } + int keepalive = enabled; + return zts_setsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_KEEPALIVE, &keepalive, sizeof(keepalive)); } int zts_simple_get_keepalive(int fd) { - if (! transport_ok()) { - return ZTS_ERR_SERVICE; - } - int err, optval = 0; - zts_socklen_t optlen = sizeof(optval); - if ((err = zts_getsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_KEEPALIVE, (void*)&optval, &optlen)) < 0) { - return err; - } - return optval != 0; + if (! transport_ok()) { + return ZTS_ERR_SERVICE; + } + int err, optval = 0; + zts_socklen_t optlen = sizeof(optval); + if ((err = zts_getsockopt(fd, ZTS_SOL_SOCKET, ZTS_SO_KEEPALIVE, (void*)&optval, &optlen)) < 0) { + return err; + } + return optval != 0; } #ifdef __cplusplus diff --git a/src/Utilities.cpp b/src/Utilities.cpp index 32ca4eb..f82fd6d 100644 --- a/src/Utilities.cpp +++ b/src/Utilities.cpp @@ -46,32 +46,32 @@ extern "C" { int zts_util_get_ip_family(const char* ipstr) { - if (! ipstr) { - return ZTS_ERR_ARG; - } - int family = -1; - struct zts_sockaddr_in sa4; - if (zts_inet_pton(ZTS_AF_INET, ipstr, &(sa4.sin_addr)) == 1) { - family = ZTS_AF_INET; - } - struct zts_sockaddr_in6 sa6; - if (zts_inet_pton(ZTS_AF_INET6, ipstr, &(sa6.sin6_addr)) == 1) { - family = ZTS_AF_INET6; - } - return family; + if (! ipstr) { + return ZTS_ERR_ARG; + } + int family = -1; + struct zts_sockaddr_in sa4; + if (zts_inet_pton(ZTS_AF_INET, ipstr, &(sa4.sin_addr)) == 1) { + family = ZTS_AF_INET; + } + struct zts_sockaddr_in6 sa6; + if (zts_inet_pton(ZTS_AF_INET6, ipstr, &(sa6.sin6_addr)) == 1) { + family = ZTS_AF_INET6; + } + return family; } void zts_util_delay(unsigned long milliseconds) { #ifdef __WINDOWS__ - Sleep(milliseconds); + Sleep(milliseconds); #elif _POSIX_C_SOURCE >= 199309L - struct timespec ts; - ts.tv_sec = milliseconds / 1000; - ts.tv_nsec = (milliseconds % 1000) * 1000000; - nanosleep(&ts, NULL); + struct timespec ts; + ts.tv_sec = milliseconds / 1000; + ts.tv_nsec = (milliseconds % 1000) * 1000000; + nanosleep(&ts, NULL); #else - usleep(milliseconds * 1000); + usleep(milliseconds * 1000); #endif } @@ -86,97 +86,97 @@ int zts_util_world_new( uint64_t ts, zts_world_t* world_spec) { - if (! world_spec || ! prev_key || ! curr_key || ! prev_key_len || ! curr_key_len) { - return ZTS_ERR_ARG; - } - // Generate signing keys - std::string previous, current; - if ((! OSUtils::readFile("previous.c25519", previous)) || (! OSUtils::readFile("current.c25519", current))) { - C25519::Pair np(C25519::generate()); - previous = std::string(); - previous.append((const char*)np.pub.data, ZT_C25519_PUBLIC_KEY_LEN); - previous.append((const char*)np.priv.data, ZT_C25519_PRIVATE_KEY_LEN); - current = previous; - } - if ((previous.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN)) - || (current.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN))) { - // Previous.c25519 or current.c25519 empty or invalid - return ZTS_ERR_ARG; - } - C25519::Pair previousKP; - memcpy(previousKP.pub.data, previous.data(), ZT_C25519_PUBLIC_KEY_LEN); - memcpy(previousKP.priv.data, previous.data() + ZT_C25519_PUBLIC_KEY_LEN, ZT_C25519_PRIVATE_KEY_LEN); - C25519::Pair currentKP; - memcpy(currentKP.pub.data, current.data(), ZT_C25519_PUBLIC_KEY_LEN); - memcpy(currentKP.priv.data, current.data() + ZT_C25519_PUBLIC_KEY_LEN, ZT_C25519_PRIVATE_KEY_LEN); + if (! world_spec || ! prev_key || ! curr_key || ! prev_key_len || ! curr_key_len) { + return ZTS_ERR_ARG; + } + // Generate signing keys + std::string previous, current; + if ((! OSUtils::readFile("previous.c25519", previous)) || (! OSUtils::readFile("current.c25519", current))) { + C25519::Pair np(C25519::generate()); + previous = std::string(); + previous.append((const char*)np.pub.data, ZT_C25519_PUBLIC_KEY_LEN); + previous.append((const char*)np.priv.data, ZT_C25519_PRIVATE_KEY_LEN); + current = previous; + } + if ((previous.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN)) + || (current.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN))) { + // Previous.c25519 or current.c25519 empty or invalid + return ZTS_ERR_ARG; + } + C25519::Pair previousKP; + memcpy(previousKP.pub.data, previous.data(), ZT_C25519_PUBLIC_KEY_LEN); + memcpy(previousKP.priv.data, previous.data() + ZT_C25519_PUBLIC_KEY_LEN, ZT_C25519_PRIVATE_KEY_LEN); + C25519::Pair currentKP; + memcpy(currentKP.pub.data, current.data(), ZT_C25519_PUBLIC_KEY_LEN); + memcpy(currentKP.priv.data, current.data() + ZT_C25519_PUBLIC_KEY_LEN, ZT_C25519_PRIVATE_KEY_LEN); - // Set up world definition - std::vector roots; - for (int i = 0; i < ZTS_MAX_NUM_ROOTS; i++) { - if (! world_spec->public_id_str[i]) { - break; - } - if (strlen(world_spec->public_id_str[i])) { - // printf("id = %s\n", world_spec->public_id_str[i]); - roots.push_back(World::Root()); - roots.back().identity = Identity(world_spec->public_id_str[i]); - for (int j = 0; j < ZTS_MAX_ENDPOINTS_PER_ROOT; j++) { - if (! world_spec->endpoint_ip_str[i][j]) { - break; - } - if (strlen(world_spec->endpoint_ip_str[i][j])) { - roots.back().stableEndpoints.push_back(InetAddress(world_spec->endpoint_ip_str[i][j])); - // printf(" ep = %s\n", world_spec->endpoint_ip_str[i][j]); - } - } - } - } + // Set up world definition + std::vector roots; + for (int i = 0; i < ZTS_MAX_NUM_ROOTS; i++) { + if (! world_spec->public_id_str[i]) { + break; + } + if (strlen(world_spec->public_id_str[i])) { + // printf("id = %s\n", world_spec->public_id_str[i]); + roots.push_back(World::Root()); + roots.back().identity = Identity(world_spec->public_id_str[i]); + for (int j = 0; j < ZTS_MAX_ENDPOINTS_PER_ROOT; j++) { + if (! world_spec->endpoint_ip_str[i][j]) { + break; + } + if (strlen(world_spec->endpoint_ip_str[i][j])) { + roots.back().stableEndpoints.push_back(InetAddress(world_spec->endpoint_ip_str[i][j])); + // printf(" ep = %s\n", world_spec->endpoint_ip_str[i][j]); + } + } + } + } - // Generate - World nw = World::make(World::TYPE_PLANET, id, ts, currentKP.pub, roots, previousKP); - // Test - Buffer outtmp; - nw.serialize(outtmp, false); - World testw; - testw.deserialize(outtmp, 0); - if (testw != nw) { - // Serialization test failed - return ZTS_ERR_GENERAL; - } - // Write output - memcpy(world_out, (char*)outtmp.data(), outtmp.size()); - *world_len = outtmp.size(); - memcpy(prev_key, previous.data(), previous.length()); - *prev_key_len = ZT_C25519_PRIVATE_KEY_LEN + ZT_C25519_PUBLIC_KEY_LEN; - memcpy(curr_key, current.data(), current.length()); - *curr_key_len = ZT_C25519_PRIVATE_KEY_LEN + ZT_C25519_PUBLIC_KEY_LEN; - return ZTS_ERR_OK; + // Generate + World nw = World::make(World::TYPE_PLANET, id, ts, currentKP.pub, roots, previousKP); + // Test + Buffer outtmp; + nw.serialize(outtmp, false); + World testw; + testw.deserialize(outtmp, 0); + if (testw != nw) { + // Serialization test failed + return ZTS_ERR_GENERAL; + } + // Write output + memcpy(world_out, (char*)outtmp.data(), outtmp.size()); + *world_len = outtmp.size(); + memcpy(prev_key, previous.data(), previous.length()); + *prev_key_len = ZT_C25519_PRIVATE_KEY_LEN + ZT_C25519_PUBLIC_KEY_LEN; + memcpy(curr_key, current.data(), current.length()); + *curr_key_len = ZT_C25519_PRIVATE_KEY_LEN + ZT_C25519_PUBLIC_KEY_LEN; + return ZTS_ERR_OK; } void native_ss_to_zts_ss(struct zts_sockaddr_storage* ss_out, const struct sockaddr_storage* ss_in) { - if (ss_in->ss_family == AF_INET) { - struct sockaddr_in* s_in4 = (struct sockaddr_in*)ss_in; - struct zts_sockaddr_in* d_in4 = (struct zts_sockaddr_in*)ss_out; + if (ss_in->ss_family == AF_INET) { + struct sockaddr_in* s_in4 = (struct sockaddr_in*)ss_in; + struct zts_sockaddr_in* d_in4 = (struct zts_sockaddr_in*)ss_out; #ifndef __WINDOWS__ - d_in4->sin_len = 0; // s_in4->sin_len; + d_in4->sin_len = 0; // s_in4->sin_len; #endif - d_in4->sin_family = ZTS_AF_INET; - d_in4->sin_port = s_in4->sin_port; - memcpy(&(d_in4->sin_addr), &(s_in4->sin_addr), sizeof(s_in4->sin_addr)); - } - if (ss_in->ss_family == AF_INET6) { - struct sockaddr_in6* s_in6 = (struct sockaddr_in6*)ss_in; - struct zts_sockaddr_in6* d_in6 = (struct zts_sockaddr_in6*)ss_out; + d_in4->sin_family = ZTS_AF_INET; + d_in4->sin_port = s_in4->sin_port; + memcpy(&(d_in4->sin_addr), &(s_in4->sin_addr), sizeof(s_in4->sin_addr)); + } + if (ss_in->ss_family == AF_INET6) { + struct sockaddr_in6* s_in6 = (struct sockaddr_in6*)ss_in; + struct zts_sockaddr_in6* d_in6 = (struct zts_sockaddr_in6*)ss_out; #ifndef __WINDOWS__ - d_in6->sin6_len = 0; // s_in6->sin6_len; + d_in6->sin6_len = 0; // s_in6->sin6_len; #endif - d_in6->sin6_family = ZTS_AF_INET6; - d_in6->sin6_port = s_in6->sin6_port; - d_in6->sin6_flowinfo = s_in6->sin6_flowinfo; - memcpy(&(d_in6->sin6_addr), &(s_in6->sin6_addr), sizeof(s_in6->sin6_addr)); - d_in6->sin6_scope_id = s_in6->sin6_scope_id; - } + d_in6->sin6_family = ZTS_AF_INET6; + d_in6->sin6_port = s_in6->sin6_port; + d_in6->sin6_flowinfo = s_in6->sin6_flowinfo; + memcpy(&(d_in6->sin6_addr), &(s_in6->sin6_addr), sizeof(s_in6->sin6_addr)); + d_in6->sin6_scope_id = s_in6->sin6_scope_id; + } } #ifdef __cplusplus diff --git a/src/VirtualTap.cpp b/src/VirtualTap.cpp index b5d3dc1..a07458b 100644 --- a/src/VirtualTap.cpp +++ b/src/VirtualTap.cpp @@ -84,191 +84,191 @@ VirtualTap::VirtualTap( , _unixListenSocket((PhySocket*)0) , _phy(this, false, true) { - OSUtils::ztsnprintf(vtap_full_name, VTAP_NAME_LEN, "libzt-vtap-%llx", _net_id); + OSUtils::ztsnprintf(vtap_full_name, VTAP_NAME_LEN, "libzt-vtap-%llx", _net_id); #ifndef __WINDOWS__ - ::pipe(_shutdownSignalPipe); + ::pipe(_shutdownSignalPipe); #endif - // Start virtual tap thread and stack I/O loops - _thread = Thread::start(this); + // Start virtual tap thread and stack I/O loops + _thread = Thread::start(this); } VirtualTap::~VirtualTap() { - _run = false; + _run = false; #ifndef __WINDOWS__ - ::write(_shutdownSignalPipe[1], "\0", 1); + ::write(_shutdownSignalPipe[1], "\0", 1); #endif - _phy.whack(); - _lwip_remove_netif(netif4); - netif4 = NULL; - _lwip_remove_netif(netif6); - netif6 = NULL; - Thread::join(_thread); + _phy.whack(); + _lwip_remove_netif(netif4); + netif4 = NULL; + _lwip_remove_netif(netif6); + netif6 = NULL; + Thread::join(_thread); #ifndef __WINDOWS__ - ::close(_shutdownSignalPipe[0]); - ::close(_shutdownSignalPipe[1]); + ::close(_shutdownSignalPipe[0]); + ::close(_shutdownSignalPipe[1]); #endif } void VirtualTap::lastConfigUpdate(uint64_t lastConfigUpdateTime) { - _lastConfigUpdateTime = lastConfigUpdateTime; + _lastConfigUpdateTime = lastConfigUpdateTime; } void VirtualTap::setEnabled(bool en) { - _enabled = en; + _enabled = en; } bool VirtualTap::enabled() const { - return _enabled; + return _enabled; } void VirtualTap::setUserEventSystem(Events* events) { - _events = events; + _events = events; } bool VirtualTap::hasIpv4Addr() { - Mutex::Lock _l(_ips_m); - std::vector::iterator it(_ips.begin()); - while (it != _ips.end()) { - if ((*it).isV4()) { - return true; - } - ++it; - } - return false; + Mutex::Lock _l(_ips_m); + std::vector::iterator it(_ips.begin()); + while (it != _ips.end()) { + if ((*it).isV4()) { + return true; + } + ++it; + } + return false; } bool VirtualTap::hasIpv6Addr() { - Mutex::Lock _l(_ips_m); - std::vector::iterator it(_ips.begin()); - while (it != _ips.end()) { - if ((*it).isV6()) { - return true; - } - ++it; - } - return false; + Mutex::Lock _l(_ips_m); + std::vector::iterator it(_ips.begin()); + while (it != _ips.end()) { + if ((*it).isV6()) { + return true; + } + ++it; + } + return false; } bool VirtualTap::addIp(const InetAddress& ip) { - // TODO: Rewrite to allow for more addresses - char ipbuf[128] = { 0 }; - /* Limit address assignments to one per type. - This limitation can be removed if some changes - are made in the netif driver. */ - if (ip.isV4() && hasIpv4Addr()) { - ip.toString(ipbuf); - // DEBUG_INFO("failed to add IP (%s), only one per type per netif - // allowed\n", ipbuf); - return false; - } - if (ip.isV6() && hasIpv6Addr()) { - ip.toString(ipbuf); - // DEBUG_INFO("failed to add IP (%s), only one per type per netif - // allowed\n", ipbuf); - return false; - } + // TODO: Rewrite to allow for more addresses + char ipbuf[128] = { 0 }; + /* Limit address assignments to one per type. + This limitation can be removed if some changes + are made in the netif driver. */ + if (ip.isV4() && hasIpv4Addr()) { + ip.toString(ipbuf); + // DEBUG_INFO("failed to add IP (%s), only one per type per netif + // allowed\n", ipbuf); + return false; + } + if (ip.isV6() && hasIpv6Addr()) { + ip.toString(ipbuf); + // DEBUG_INFO("failed to add IP (%s), only one per type per netif + // allowed\n", ipbuf); + return false; + } - Mutex::Lock _l(_ips_m); - if (_ips.size() >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) { - return false; - } - if (std::find(_ips.begin(), _ips.end(), ip) == _ips.end()) { - _lwip_init_interface((void*)this, ip); - _ips.push_back(ip); - std::sort(_ips.begin(), _ips.end()); - } - return true; + Mutex::Lock _l(_ips_m); + if (_ips.size() >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) { + return false; + } + if (std::find(_ips.begin(), _ips.end(), ip) == _ips.end()) { + _lwip_init_interface((void*)this, ip); + _ips.push_back(ip); + std::sort(_ips.begin(), _ips.end()); + } + return true; } bool VirtualTap::removeIp(const InetAddress& ip) { - Mutex::Lock _l(_ips_m); - if (std::find(_ips.begin(), _ips.end(), ip) != _ips.end()) { - std::vector::iterator i(std::find(_ips.begin(), _ips.end(), ip)); - _lwip_remove_address_from_netif((void*)this, ip); - _ips.erase(i); - } - return true; + Mutex::Lock _l(_ips_m); + if (std::find(_ips.begin(), _ips.end(), ip) != _ips.end()) { + std::vector::iterator i(std::find(_ips.begin(), _ips.end(), ip)); + _lwip_remove_address_from_netif((void*)this, ip); + _ips.erase(i); + } + return true; } std::vector VirtualTap::ips() const { - Mutex::Lock _l(_ips_m); - return _ips; + Mutex::Lock _l(_ips_m); + return _ips; } void VirtualTap::put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len) { - if (len && _enabled) { - _lwip_eth_rx(this, from, to, etherType, data, len); - } + if (len && _enabled) { + _lwip_eth_rx(this, from, to, etherType, data, len); + } } void VirtualTap::scanMulticastGroups(std::vector& added, std::vector& removed) { - std::vector newGroups; - Mutex::Lock _l(_multicastGroups_m); - // TODO: get multicast subscriptions - std::vector allIps(ips()); - for (std::vector::iterator ip(allIps.begin()); ip != allIps.end(); ++ip) - newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip)); + std::vector newGroups; + Mutex::Lock _l(_multicastGroups_m); + // TODO: get multicast subscriptions + std::vector allIps(ips()); + for (std::vector::iterator ip(allIps.begin()); ip != allIps.end(); ++ip) + newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip)); - std::sort(newGroups.begin(), newGroups.end()); + std::sort(newGroups.begin(), newGroups.end()); - for (std::vector::iterator m(newGroups.begin()); m != newGroups.end(); ++m) { - if (! std::binary_search(_multicastGroups.begin(), _multicastGroups.end(), *m)) - added.push_back(*m); - } - for (std::vector::iterator m(_multicastGroups.begin()); m != _multicastGroups.end(); ++m) { - if (! std::binary_search(newGroups.begin(), newGroups.end(), *m)) - removed.push_back(*m); - } - _multicastGroups.swap(newGroups); + for (std::vector::iterator m(newGroups.begin()); m != newGroups.end(); ++m) { + if (! std::binary_search(_multicastGroups.begin(), _multicastGroups.end(), *m)) + added.push_back(*m); + } + for (std::vector::iterator m(_multicastGroups.begin()); m != _multicastGroups.end(); ++m) { + if (! std::binary_search(newGroups.begin(), newGroups.end(), *m)) + removed.push_back(*m); + } + _multicastGroups.swap(newGroups); } void VirtualTap::setMtu(unsigned int mtu) { - _mtu = mtu; + _mtu = mtu; } void VirtualTap::threadMain() throw() { - fd_set readfds, nullfds; - struct timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 0; - FD_ZERO(&readfds); - FD_ZERO(&nullfds); - int nfds = (int)std::max(_shutdownSignalPipe[0], 0) + 1; + fd_set readfds, nullfds; + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 0; + FD_ZERO(&readfds); + FD_ZERO(&nullfds); + int nfds = (int)std::max(_shutdownSignalPipe[0], 0) + 1; #if defined(__linux__) - pthread_setname_np(pthread_self(), vtap_full_name); + pthread_setname_np(pthread_self(), vtap_full_name); #endif #if defined(__APPLE__) - pthread_setname_np(vtap_full_name); + pthread_setname_np(vtap_full_name); #endif - while (true) { - FD_SET(_shutdownSignalPipe[0], &readfds); - select(nfds, &readfds, &nullfds, &nullfds, &tv); - // writes to shutdown pipe terminate thread - if (FD_ISSET(_shutdownSignalPipe[0], &readfds)) { - break; - } + while (true) { + FD_SET(_shutdownSignalPipe[0], &readfds); + select(nfds, &readfds, &nullfds, &nullfds, &tv); + // writes to shutdown pipe terminate thread + if (FD_ISSET(_shutdownSignalPipe[0], &readfds)) { + break; + } #if defined(__WINDOWS__) - Sleep(ZTS_TAP_THREAD_POLLING_INTERVAL); + Sleep(ZTS_TAP_THREAD_POLLING_INTERVAL); #else - struct timespec sleepValue = { 0 }; - sleepValue.tv_nsec = ZTS_TAP_THREAD_POLLING_INTERVAL * 500000; - nanosleep(&sleepValue, NULL); + struct timespec sleepValue = { 0 }; + sleepValue.tv_nsec = ZTS_TAP_THREAD_POLLING_INTERVAL * 500000; + nanosleep(&sleepValue, NULL); #endif - } + } } void VirtualTap::phyOnDatagram( @@ -279,7 +279,7 @@ void VirtualTap::phyOnDatagram( void* data, unsigned long len) { - // Intentionally empty + // Intentionally empty } void VirtualTap::phyOnTcpConnect(PhySocket* sock, void** uptr, bool success) @@ -327,118 +327,118 @@ Mutex stackLock; // Callback for when the TCPIP thread has been successfully started static void _tcpip_init_done(void* arg) { - sys_sem_t* sem; - sem = (sys_sem_t*)arg; - zts_events->setState(ZTS_STATE_STACK_RUNNING); - _has_started = true; - zts_events->enqueue(ZTS_EVENT_STACK_UP, NULL); - sys_sem_signal(sem); + sys_sem_t* sem; + sem = (sys_sem_t*)arg; + zts_events->setState(ZTS_STATE_STACK_RUNNING); + _has_started = true; + zts_events->enqueue(ZTS_EVENT_STACK_UP, NULL); + sys_sem_signal(sem); } static void _main_lwip_driver_loop(void* arg) { #if defined(__linux__) - pthread_setname_np(pthread_self(), ZTS_LWIP_THREAD_NAME); + pthread_setname_np(pthread_self(), ZTS_LWIP_THREAD_NAME); #endif #if defined(__APPLE__) - pthread_setname_np(ZTS_LWIP_THREAD_NAME); + pthread_setname_np(ZTS_LWIP_THREAD_NAME); #endif - sys_sem_t sem; - LWIP_UNUSED_ARG(arg); - if (sys_sem_new(&sem, 0) != ERR_OK) { - // DEBUG_ERROR("failed to create semaphore"); - } - tcpip_init(_tcpip_init_done, &sem); - sys_sem_wait(&sem); - // Main loop - while (zts_events->getState(ZTS_STATE_STACK_RUNNING)) { - zts_util_delay(LWIP_DRIVER_LOOP_INTERVAL); - } - _has_exited = true; - zts_events->enqueue(ZTS_EVENT_STACK_DOWN, NULL); + sys_sem_t sem; + LWIP_UNUSED_ARG(arg); + if (sys_sem_new(&sem, 0) != ERR_OK) { + // DEBUG_ERROR("failed to create semaphore"); + } + tcpip_init(_tcpip_init_done, &sem); + sys_sem_wait(&sem); + // Main loop + while (zts_events->getState(ZTS_STATE_STACK_RUNNING)) { + zts_util_delay(LWIP_DRIVER_LOOP_INTERVAL); + } + _has_exited = true; + zts_events->enqueue(ZTS_EVENT_STACK_DOWN, NULL); } bool _lwip_is_up() { - Mutex::Lock _l(stackLock); - return zts_events->getState(ZTS_STATE_STACK_RUNNING); + Mutex::Lock _l(stackLock); + return zts_events->getState(ZTS_STATE_STACK_RUNNING); } void _lwip_driver_init() { - if (_lwip_is_up()) { - return; - } - if (_has_exited) { - return; - } - Mutex::Lock _l(stackLock); + if (_lwip_is_up()) { + return; + } + if (_has_exited) { + return; + } + Mutex::Lock _l(stackLock); #if defined(__WINDOWS__) - sys_init(); // Required for win32 init of critical sections + sys_init(); // Required for win32 init of critical sections #endif - sys_thread_new(ZTS_LWIP_THREAD_NAME, _main_lwip_driver_loop, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); + sys_thread_new(ZTS_LWIP_THREAD_NAME, _main_lwip_driver_loop, NULL, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); } void _lwip_driver_shutdown() { - if (_has_exited) { - return; - } - Mutex::Lock _l(stackLock); - // Set flag to stop sending frames into the core - zts_events->clrState(ZTS_STATE_STACK_RUNNING); - // Wait until the main lwIP thread has exited - if (_has_started) { - while (! _has_exited) { - zts_util_delay(LWIP_DRIVER_LOOP_INTERVAL); - } - } + if (_has_exited) { + return; + } + Mutex::Lock _l(stackLock); + // Set flag to stop sending frames into the core + zts_events->clrState(ZTS_STATE_STACK_RUNNING); + // Wait until the main lwIP thread has exited + if (_has_started) { + while (! _has_exited) { + zts_util_delay(LWIP_DRIVER_LOOP_INTERVAL); + } + } } void _lwip_remove_netif(void* netif) { - if (! netif) { - return; - } - struct netif* n = (struct netif*)netif; - LOCK_TCPIP_CORE(); - netif_remove(n); - netif_set_down(n); - netif_set_link_down(n); - UNLOCK_TCPIP_CORE(); + if (! netif) { + return; + } + struct netif* n = (struct netif*)netif; + LOCK_TCPIP_CORE(); + netif_remove(n); + netif_set_down(n); + netif_set_link_down(n); + UNLOCK_TCPIP_CORE(); } err_t _lwip_eth_tx(struct netif* n, struct pbuf* p) { - if (! n) { - return ERR_IF; - } - struct pbuf* q; - char buf[ZT_MAX_MTU + 32] = { 0 }; - char* bufptr; - int totalLength = 0; + if (! n) { + return ERR_IF; + } + struct pbuf* q; + char buf[ZT_MAX_MTU + 32] = { 0 }; + char* bufptr; + int totalLength = 0; - VirtualTap* tap = (VirtualTap*)n->state; - bufptr = buf; - for (q = p; q != NULL; q = q->next) { - memcpy(bufptr, q->payload, q->len); - bufptr += q->len; - totalLength += q->len; - } - struct eth_hdr* ethhdr; - ethhdr = (struct eth_hdr*)buf; + VirtualTap* tap = (VirtualTap*)n->state; + bufptr = buf; + for (q = p; q != NULL; q = q->next) { + memcpy(bufptr, q->payload, q->len); + bufptr += q->len; + totalLength += q->len; + } + struct eth_hdr* ethhdr; + ethhdr = (struct eth_hdr*)buf; - MAC src_mac; - MAC dest_mac; - src_mac.setTo(ethhdr->src.addr, 6); - dest_mac.setTo(ethhdr->dest.addr, 6); + MAC src_mac; + MAC dest_mac; + src_mac.setTo(ethhdr->src.addr, 6); + dest_mac.setTo(ethhdr->dest.addr, 6); - char* data = buf + sizeof(struct eth_hdr); - int len = totalLength - sizeof(struct eth_hdr); - int proto = Utils::ntoh((uint16_t)ethhdr->type); - tap->_handler(tap->_arg, NULL, tap->_net_id, src_mac, dest_mac, proto, 0, data, len); + char* data = buf + sizeof(struct eth_hdr); + int len = totalLength - sizeof(struct eth_hdr); + int proto = Utils::ntoh((uint16_t)ethhdr->type); + tap->_handler(tap->_arg, NULL, tap->_net_id, src_mac, dest_mac, proto, 0, data, len); - return ERR_OK; + return ERR_OK; } void _lwip_eth_rx( @@ -450,213 +450,213 @@ void _lwip_eth_rx( unsigned int len) { #ifdef LWIP_STATS - stats_display(); + stats_display(); #endif - if (! zts_events->getState(ZTS_STATE_STACK_RUNNING)) { - return; - } - struct pbuf *p, *q; - struct eth_hdr ethhdr; - from.copyTo(ethhdr.src.addr, 6); - to.copyTo(ethhdr.dest.addr, 6); - ethhdr.type = Utils::hton((uint16_t)etherType); + if (! zts_events->getState(ZTS_STATE_STACK_RUNNING)) { + return; + } + struct pbuf *p, *q; + struct eth_hdr ethhdr; + from.copyTo(ethhdr.src.addr, 6); + to.copyTo(ethhdr.dest.addr, 6); + ethhdr.type = Utils::hton((uint16_t)etherType); - p = pbuf_alloc(PBUF_RAW, (uint16_t)len + sizeof(struct eth_hdr), PBUF_RAM); - if (! p) { - // DEBUG_ERROR("dropped packet: unable to allocate memory for - // pbuf"); - return; - } - // First pbuf gets Ethernet header at start - q = p; - if (q->len < sizeof(ethhdr)) { - pbuf_free(p); - p = NULL; - // DEBUG_ERROR("dropped packet: first pbuf smaller than Ethernet - // header"); - return; - } - // Copy frame data into pbuf - const char* dataptr = reinterpret_cast(data); - memcpy(q->payload, ðhdr, sizeof(ethhdr)); - int remainingPayloadSpace = q->len - sizeof(ethhdr); - memcpy((char*)q->payload + sizeof(ethhdr), dataptr, remainingPayloadSpace); - dataptr += remainingPayloadSpace; - // Remaining pbufs (if any) get rest of data - while ((q = q->next)) { - memcpy(q->payload, dataptr, q->len); - dataptr += q->len; - } - // Feed packet into stack - int err; + p = pbuf_alloc(PBUF_RAW, (uint16_t)len + sizeof(struct eth_hdr), PBUF_RAM); + if (! p) { + // DEBUG_ERROR("dropped packet: unable to allocate memory for + // pbuf"); + return; + } + // First pbuf gets Ethernet header at start + q = p; + if (q->len < sizeof(ethhdr)) { + pbuf_free(p); + p = NULL; + // DEBUG_ERROR("dropped packet: first pbuf smaller than Ethernet + // header"); + return; + } + // Copy frame data into pbuf + const char* dataptr = reinterpret_cast(data); + memcpy(q->payload, ðhdr, sizeof(ethhdr)); + int remainingPayloadSpace = q->len - sizeof(ethhdr); + memcpy((char*)q->payload + sizeof(ethhdr), dataptr, remainingPayloadSpace); + dataptr += remainingPayloadSpace; + // Remaining pbufs (if any) get rest of data + while ((q = q->next)) { + memcpy(q->payload, dataptr, q->len); + dataptr += q->len; + } + // Feed packet into stack + int err; - if (Utils::ntoh(ethhdr.type) == 0x800 || Utils::ntoh(ethhdr.type) == 0x806) { - if (tap->netif4) { - if ((err = ((struct netif*)tap->netif4)->input(p, (struct netif*)tap->netif4)) != ERR_OK) { - // DEBUG_ERROR("packet input error (%d)", err); - pbuf_free(p); - } - } - } - if (Utils::ntoh(ethhdr.type) == 0x86DD) { - if (tap->netif6) { - if ((err = ((struct netif*)tap->netif6)->input(p, (struct netif*)tap->netif6)) != ERR_OK) { - // DEBUG_ERROR("packet input error (%d)", err); - pbuf_free(p); - } - } - } + if (Utils::ntoh(ethhdr.type) == 0x800 || Utils::ntoh(ethhdr.type) == 0x806) { + if (tap->netif4) { + if ((err = ((struct netif*)tap->netif4)->input(p, (struct netif*)tap->netif4)) != ERR_OK) { + // DEBUG_ERROR("packet input error (%d)", err); + pbuf_free(p); + } + } + } + if (Utils::ntoh(ethhdr.type) == 0x86DD) { + if (tap->netif6) { + if ((err = ((struct netif*)tap->netif6)->input(p, (struct netif*)tap->netif6)) != ERR_OK) { + // DEBUG_ERROR("packet input error (%d)", err); + pbuf_free(p); + } + } + } } bool _lwip_is_netif_up(void* n) { - if (! n) { - return false; - } - LOCK_TCPIP_CORE(); - bool result = netif_is_up((struct netif*)n); - UNLOCK_TCPIP_CORE(); - return result; + if (! n) { + return false; + } + LOCK_TCPIP_CORE(); + bool result = netif_is_up((struct netif*)n); + UNLOCK_TCPIP_CORE(); + return result; } static err_t _netif_init4(struct netif* n) { - if (! n || ! n->state) { - return ERR_IF; - } - // Called from core, no need to lock - VirtualTap* tap = (VirtualTap*)(n->state); - n->hwaddr_len = 6; - n->name[0] = '4'; - n->name[1] = 'a' + netifCount; - n->linkoutput = _lwip_eth_tx; - n->output = etharp_output; - n->mtu = std::min(LWIP_MTU, (int)tap->_mtu); - n->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6 - | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP; - n->hwaddr_len = sizeof(n->hwaddr); - tap->_mac.copyTo(n->hwaddr, n->hwaddr_len); - return ERR_OK; + if (! n || ! n->state) { + return ERR_IF; + } + // Called from core, no need to lock + VirtualTap* tap = (VirtualTap*)(n->state); + n->hwaddr_len = 6; + n->name[0] = '4'; + n->name[1] = 'a' + netifCount; + n->linkoutput = _lwip_eth_tx; + n->output = etharp_output; + n->mtu = std::min(LWIP_MTU, (int)tap->_mtu); + n->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6 + | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP; + n->hwaddr_len = sizeof(n->hwaddr); + tap->_mac.copyTo(n->hwaddr, n->hwaddr_len); + return ERR_OK; } static err_t _netif_init6(struct netif* n) { - if (! n || ! n->state) { - return ERR_IF; - } - n->hwaddr_len = sizeof(n->hwaddr); - VirtualTap* tap = (VirtualTap*)(n->state); - tap->_mac.copyTo(n->hwaddr, n->hwaddr_len); - // Called from core, no need to lock - n->hwaddr_len = 6; - n->name[0] = '6'; - n->name[1] = 'a' + netifCount; - n->linkoutput = _lwip_eth_tx; - n->output_ip6 = ethip6_output; - n->mtu = std::min(LWIP_MTU, (int)tap->_mtu); - n->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6 - | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP; - return ERR_OK; + if (! n || ! n->state) { + return ERR_IF; + } + n->hwaddr_len = sizeof(n->hwaddr); + VirtualTap* tap = (VirtualTap*)(n->state); + tap->_mac.copyTo(n->hwaddr, n->hwaddr_len); + // Called from core, no need to lock + n->hwaddr_len = 6; + n->name[0] = '6'; + n->name[1] = 'a' + netifCount; + n->linkoutput = _lwip_eth_tx; + n->output_ip6 = ethip6_output; + n->mtu = std::min(LWIP_MTU, (int)tap->_mtu); + n->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6 + | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP; + return ERR_OK; } void _lwip_init_interface(void* tapref, const InetAddress& ip) { - char macbuf[ZTS_MAC_ADDRSTRLEN] = { 0 }; + char macbuf[ZTS_MAC_ADDRSTRLEN] = { 0 }; - VirtualTap* vtap = (VirtualTap*)tapref; - struct netif* n = NULL; - bool isNewNetif = false; + VirtualTap* vtap = (VirtualTap*)tapref; + struct netif* n = NULL; + bool isNewNetif = false; - if (ip.isV4()) { - if (vtap->netif4) { - n = (struct netif*)vtap->netif4; - } - else { - n = new struct netif; - isNewNetif = true; - netifCount++; - } + if (ip.isV4()) { + if (vtap->netif4) { + n = (struct netif*)vtap->netif4; + } + else { + n = new struct netif; + isNewNetif = true; + netifCount++; + } - static ip4_addr_t ip4, netmask, gw; - IP4_ADDR(&gw, 127, 0, 0, 1); - ip4.addr = *((u32_t*)ip.rawIpData()); - netmask.addr = *((u32_t*)ip.netmask().rawIpData()); - LOCK_TCPIP_CORE(); - netif_add(n, &ip4, &netmask, &gw, (void*)vtap, _netif_init4, tcpip_input); - vtap->netif4 = (void*)n; - UNLOCK_TCPIP_CORE(); - snprintf( - macbuf, - ZTS_MAC_ADDRSTRLEN, - "%02x:%02x:%02x:%02x:%02x:%02x", - n->hwaddr[0], - n->hwaddr[1], - n->hwaddr[2], - n->hwaddr[3], - n->hwaddr[4], - n->hwaddr[5]); - } - if (ip.isV6()) { - if (vtap->netif6) { - n = (struct netif*)vtap->netif6; - } - else { - n = new struct netif; - isNewNetif = true; - netifCount++; - } - static ip6_addr_t ip6; - memcpy(&(ip6.addr), ip.rawIpData(), sizeof(ip6.addr)); - LOCK_TCPIP_CORE(); - if (isNewNetif) { - vtap->netif6 = (void*)n; - netif_add(n, NULL, NULL, NULL, (void*)vtap, _netif_init6, ethernet_input); - n->ip6_autoconfig_enabled = 1; - vtap->_mac.copyTo(n->hwaddr, n->hwaddr_len); - netif_create_ip6_linklocal_address(n, 1); - netif_set_link_up(n); - netif_set_up(n); - netif_set_default(n); - } - netif_add_ip6_address(n, &ip6, NULL); - n->output_ip6 = ethip6_output; - UNLOCK_TCPIP_CORE(); - snprintf( - macbuf, - ZTS_MAC_ADDRSTRLEN, - "%02x:%02x:%02x:%02x:%02x:%02x", - n->hwaddr[0], - n->hwaddr[1], - n->hwaddr[2], - n->hwaddr[3], - n->hwaddr[4], - n->hwaddr[5]); - } + static ip4_addr_t ip4, netmask, gw; + IP4_ADDR(&gw, 127, 0, 0, 1); + ip4.addr = *((u32_t*)ip.rawIpData()); + netmask.addr = *((u32_t*)ip.netmask().rawIpData()); + LOCK_TCPIP_CORE(); + netif_add(n, &ip4, &netmask, &gw, (void*)vtap, _netif_init4, tcpip_input); + vtap->netif4 = (void*)n; + UNLOCK_TCPIP_CORE(); + snprintf( + macbuf, + ZTS_MAC_ADDRSTRLEN, + "%02x:%02x:%02x:%02x:%02x:%02x", + n->hwaddr[0], + n->hwaddr[1], + n->hwaddr[2], + n->hwaddr[3], + n->hwaddr[4], + n->hwaddr[5]); + } + if (ip.isV6()) { + if (vtap->netif6) { + n = (struct netif*)vtap->netif6; + } + else { + n = new struct netif; + isNewNetif = true; + netifCount++; + } + static ip6_addr_t ip6; + memcpy(&(ip6.addr), ip.rawIpData(), sizeof(ip6.addr)); + LOCK_TCPIP_CORE(); + if (isNewNetif) { + vtap->netif6 = (void*)n; + netif_add(n, NULL, NULL, NULL, (void*)vtap, _netif_init6, ethernet_input); + n->ip6_autoconfig_enabled = 1; + vtap->_mac.copyTo(n->hwaddr, n->hwaddr_len); + netif_create_ip6_linklocal_address(n, 1); + netif_set_link_up(n); + netif_set_up(n); + netif_set_default(n); + } + netif_add_ip6_address(n, &ip6, NULL); + n->output_ip6 = ethip6_output; + UNLOCK_TCPIP_CORE(); + snprintf( + macbuf, + ZTS_MAC_ADDRSTRLEN, + "%02x:%02x:%02x:%02x:%02x:%02x", + n->hwaddr[0], + n->hwaddr[1], + n->hwaddr[2], + n->hwaddr[3], + n->hwaddr[4], + n->hwaddr[5]); + } } void _lwip_remove_address_from_netif(void* tapref, const InetAddress& ip) { - if (! tapref) { - return; - } - VirtualTap* vtap = (VirtualTap*)tapref; - struct netif* n = NULL; - /* When true multi-homing is implemented this will need to - be a bit more sophisticated */ - if (ip.isV4()) { - if (vtap->netif4) { - n = (struct netif*)vtap->netif4; - } - } - if (ip.isV6()) { - if (vtap->netif6) { - n = (struct netif*)vtap->netif6; - } - } - if (! n) { - return; - } - _lwip_remove_netif(n); + if (! tapref) { + return; + } + VirtualTap* vtap = (VirtualTap*)tapref; + struct netif* n = NULL; + /* When true multi-homing is implemented this will need to + be a bit more sophisticated */ + if (ip.isV4()) { + if (vtap->netif4) { + n = (struct netif*)vtap->netif4; + } + } + if (ip.isV6()) { + if (vtap->netif6) { + n = (struct netif*)vtap->netif6; + } + } + if (! n) { + return; + } + _lwip_remove_netif(n); } } // namespace ZeroTier diff --git a/src/VirtualTap.hpp b/src/VirtualTap.hpp index 757f457..0159a05 100644 --- a/src/VirtualTap.hpp +++ b/src/VirtualTap.hpp @@ -42,153 +42,153 @@ struct InetAddress; * then be destroyed upon leaving the network. */ class VirtualTap { - friend class Phy; + friend class Phy; public: - VirtualTap( - const char* homePath, - const MAC& mac, - unsigned int mtu, - unsigned int metric, - uint64_t net_id, - void (*handler)( - void*, - void*, - uint64_t, - const MAC&, - const MAC&, - unsigned int, - unsigned int, - const void*, - unsigned int), - void* arg); + VirtualTap( + const char* homePath, + const MAC& mac, + unsigned int mtu, + unsigned int metric, + uint64_t net_id, + void (*handler)( + void*, + void*, + uint64_t, + const MAC&, + const MAC&, + unsigned int, + unsigned int, + const void*, + unsigned int), + void* arg); - ~VirtualTap(); + ~VirtualTap(); - void setEnabled(bool en); - bool enabled() const; + void setEnabled(bool en); + bool enabled() const; - /** - * System to ingest events from this class and emit them to the user - */ - Events* _events; + /** + * System to ingest events from this class and emit them to the user + */ + Events* _events; - /** - * Mutex for protecting IP address container for this tap. - */ - Mutex _ips_m; // Public because we want it accessible by the driver - // layer + /** + * Mutex for protecting IP address container for this tap. + */ + Mutex _ips_m; // Public because we want it accessible by the driver + // layer - void setUserEventSystem(Events* events); + void setUserEventSystem(Events* events); - /** - * Return whether this tap has been assigned an IPv4 address. - */ - bool hasIpv4Addr(); + /** + * Return whether this tap has been assigned an IPv4 address. + */ + bool hasIpv4Addr(); - /** - * Return whether this tap has been assigned an IPv6 address. - */ - bool hasIpv6Addr(); + /** + * Return whether this tap has been assigned an IPv6 address. + */ + bool hasIpv6Addr(); - /** - * Adds an address to the user-space stack interface associated with - * this VirtualTap - * - Starts VirtualTap main thread ONLY if successful - */ - bool addIp(const InetAddress& ip); + /** + * Adds an address to the user-space stack interface associated with + * this VirtualTap + * - Starts VirtualTap main thread ONLY if successful + */ + bool addIp(const InetAddress& ip); - /** - * Removes an address from the user-space stack interface associated - * with this VirtualTap - */ - bool removeIp(const InetAddress& ip); + /** + * Removes an address from the user-space stack interface associated + * with this VirtualTap + */ + bool removeIp(const InetAddress& ip); - /** - * Presents data to the user-space stack - */ - void put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len); + /** + * Presents data to the user-space stack + */ + void put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len); - /** - * Scan multicast groups - */ - void scanMulticastGroups(std::vector& added, std::vector& removed); + /** + * Scan multicast groups + */ + void scanMulticastGroups(std::vector& added, std::vector& removed); - /** - * Set MTU - */ - void setMtu(unsigned int mtu); + /** + * Set MTU + */ + void setMtu(unsigned int mtu); - /** - * Calls main network stack loops - */ - void threadMain() throw(); + /** + * Calls main network stack loops + */ + void threadMain() throw(); - /** - * For moving data onto the ZeroTier virtual wire - */ - void (*_handler)( - void*, - void*, - uint64_t, - const MAC&, - const MAC&, - unsigned int, - unsigned int, - const void*, - unsigned int); + /** + * For moving data onto the ZeroTier virtual wire + */ + void (*_handler)( + void*, + void*, + uint64_t, + const MAC&, + const MAC&, + unsigned int, + unsigned int, + const void*, + unsigned int); - void* netif4 = NULL; - void* netif6 = NULL; + void* netif4 = NULL; + void* netif6 = NULL; - // The last time that this virtual tap received a network config update - // from the core - uint64_t _lastConfigUpdateTime = 0; + // The last time that this virtual tap received a network config update + // from the core + uint64_t _lastConfigUpdateTime = 0; - void lastConfigUpdate(uint64_t lastConfigUpdateTime); + void lastConfigUpdate(uint64_t lastConfigUpdateTime); - int _networkStatus = 0; + int _networkStatus = 0; - char vtap_full_name[VTAP_NAME_LEN] = { 0 }; + char vtap_full_name[VTAP_NAME_LEN] = { 0 }; - std::vector ips() const; - std::vector _ips; + std::vector ips() const; + std::vector _ips; - std::string _homePath; - void* _arg; - volatile bool _initialized; - volatile bool _enabled; - volatile bool _run; - MAC _mac; - unsigned int _mtu; - uint64_t _net_id; - PhySocket* _unixListenSocket; - Phy _phy; + std::string _homePath; + void* _arg; + volatile bool _initialized; + volatile bool _enabled; + volatile bool _run; + MAC _mac; + unsigned int _mtu; + uint64_t _net_id; + PhySocket* _unixListenSocket; + Phy _phy; - Thread _thread; + Thread _thread; - int _shutdownSignalPipe[2] = { 0 }; + int _shutdownSignalPipe[2] = { 0 }; - std::vector _multicastGroups; - Mutex _multicastGroups_m; + std::vector _multicastGroups; + Mutex _multicastGroups_m; - //----------------------------------------------------------------------------// - // Not used in this implementation // - //----------------------------------------------------------------------------// + //----------------------------------------------------------------------------// + // Not used in this implementation // + //----------------------------------------------------------------------------// - void phyOnDatagram( - PhySocket* sock, - void** uptr, - const struct sockaddr* local_address, - const struct sockaddr* from, - void* data, - unsigned long len); - void phyOnTcpConnect(PhySocket* sock, void** uptr, bool success); - void phyOnTcpAccept(PhySocket* sockL, PhySocket* sockN, void** uptrL, void** uptrN, const struct sockaddr* from); - void phyOnTcpClose(PhySocket* sock, void** uptr); - void phyOnTcpData(PhySocket* sock, void** uptr, void* data, unsigned long len); - void phyOnTcpWritable(PhySocket* sock, void** uptr); - void phyOnUnixClose(PhySocket* sock, void** uptr); + void phyOnDatagram( + PhySocket* sock, + void** uptr, + const struct sockaddr* local_address, + const struct sockaddr* from, + void* data, + unsigned long len); + void phyOnTcpConnect(PhySocket* sock, void** uptr, bool success); + void phyOnTcpAccept(PhySocket* sockL, PhySocket* sockN, void** uptrL, void** uptrN, const struct sockaddr* from); + void phyOnTcpClose(PhySocket* sock, void** uptr); + void phyOnTcpData(PhySocket* sock, void** uptr, void* data, unsigned long len); + void phyOnTcpWritable(PhySocket* sock, void** uptr); + void phyOnUnixClose(PhySocket* sock, void** uptr); }; /**