Remove outdated examples

This commit is contained in:
Joseph Henry
2020-04-17 10:32:43 -07:00
parent 41c8f019a0
commit d7be0b7052
14 changed files with 0 additions and 5133 deletions

View File

@@ -1,39 +0,0 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#include <stdio.h>
#include "libzt.h"
int main(int argc, char **argv)
{
uint64_t nwid = 0x8056c2e21c000001;
zts_startjoin(".", nwid);
fprintf(stderr, "%llx", (unsigned long long)zts_get_node_id());
zts_leave(nwid);
zts_stop();
return 0;
}

View File

@@ -1,96 +0,0 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#include <stdio.h>
#include <string.h>
#include <string>
#include <inttypes.h>
#if defined(_WIN32)
#include <WinSock2.h>
#include <stdint.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "libzt.h"
char *msg = (char*)"welcome to the machine";
int main(int argc, char **argv)
{
if (argc != 5) {
printf("\nlibzt example client\n");
printf("client [config_file_path] [nwid] [remote_addr] [remote_port]\n");
exit(0);
}
std::string path = argv[1];
std::string nwidstr = argv[2];
std::string remote_addr = argv[3];
int remote_port = atoi(argv[4]);
int r=0, w=0, err=0, sockfd;
char rbuf[32];
memset(rbuf, 0, sizeof rbuf);
struct sockaddr_in in4;
in4.sin_port = htons(remote_port);
in4.sin_addr.s_addr = inet_addr(remote_addr.c_str());
in4.sin_family = AF_INET;
// --- BEGIN EXAMPLE CODE
printf("Waiting for libzt to come online...\n");
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
printf("nwid=%llx\n", (unsigned long long)nwid);
zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = zts_get_node_id();
printf("I am %llx\n", (unsigned long long)nodeId);
if ((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("error creating ZeroTier socket\n");
}
if ((err = zts_connect(sockfd, (const struct sockaddr *)&in4, sizeof(in4))) < 0) {
printf("error connecting to remote host (%d)\n", err);
}
printf("sending to server...\n");
w = zts_write(sockfd, msg, strlen(msg));
printf("reading from server...\n");
r = zts_read(sockfd, rbuf, strlen(msg));
printf("Sent : %s\n", msg);
printf("Received : %s\n", rbuf);
err = zts_close(sockfd);
return err;
}

View File

@@ -1,109 +0,0 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#include <stdio.h>
#include <string.h>
#include <string>
#include <inttypes.h>
#include <unistd.h>
#if defined(_WIN32)
#include <WinSock2.h>
#include <stdint.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "libzt.h"
char *msg = (char*)"welcome to the machine";
int main(int argc, char **argv)
{
if (argc != 5) {
printf("\nlibzt example client\n");
printf("client [config_file_path] [nwid] [remote_addr] [remote_port]\n");
exit(0);
}
std::string path = argv[1];
std::string nwidstr = argv[2];
std::string remote_addr = argv[3];
int remote_port = atoi(argv[4]);
int r=0, w=0, err=0, sockfd;
char rbuf[32];
memset(rbuf, 0, sizeof rbuf);
struct sockaddr_in in4;
in4.sin_port = htons(remote_port);
in4.sin_addr.s_addr = INADDR_ANY;
in4.sin_family = AF_INET;
struct sockaddr_in remote4;
remote4.sin_port = htons(remote_port);
remote4.sin_addr.s_addr = inet_addr(remote_addr.c_str());
remote4.sin_family = AF_INET;
// --- BEGIN EXAMPLE CODE
printf("Waiting for libzt to come online...\n");
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
printf("nwid=%llx\n", (unsigned long long)nwid);
zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = zts_get_node_id();
printf("I am %llx\n", (unsigned long long)nodeId);
if ((sockfd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("error creating ZeroTier socket\n");
}
if ((err = zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) {
printf("error binding to interface (%d)\n", err);
}
int flags = 0;
int len = strlen(msg);
while(true) {
sleep(1);
if ((err = zts_sendto(sockfd, msg, len, flags, (const struct sockaddr *)&remote4, sizeof(remote4))) < 0) {
printf("error sending message to remote host (%d)\n", err);
}
printf("sent=%d\n", err);
}
/*
printf("reading from server...\n");
r = zts_read(sockfd, rbuf, strlen(msg));
printf("Sent : %s\n", msg);
printf("Received : %s\n", rbuf);
*/
err = zts_close(sockfd);
return err;
}

View File

@@ -1,119 +0,0 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#include <stdio.h>
#include <string.h>
#include <string>
#include <inttypes.h>
#if defined(_WIN32)
#include <WinSock2.h>
#include <stdint.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "libzt.h"
int main(int argc, char **argv)
{
if (argc != 4) {
printf("\nlibzt example server\n");
printf("server [config_file_path] [nwid] [bind_port]\n");
exit(0);
}
std::string path = argv[1];
std::string nwidstr = argv[2];
int bind_port = atoi(argv[3]);
int w=0, r=0, err=0, sockfd, accfd;
char rbuf[1000];
memset(rbuf, 0, sizeof rbuf);
struct sockaddr_in in4, acc_in4;
in4.sin_port = htons(bind_port);
in4.sin_addr.s_addr = INADDR_ANY;
in4.sin_family = AF_INET;
// --- BEGIN EXAMPLE CODE
printf("Waiting for libzt to come online...\n");
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
printf("nwid=%llx\n", (unsigned long long)nwid);
zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = zts_get_node_id();
printf("I am %llx\n", (unsigned long long)nodeId);
if ((sockfd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("error creating ZeroTier socket\n");
}
if ((err = zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) {
printf("error binding to interface (%d)\n", err);
}
if ((err = zts_listen(sockfd, 100)) < 0) {
printf("error placing socket in LISTENING state (%d)\n", err);
}
socklen_t client_addrlen = sizeof(sockaddr_in);
if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in4, &client_addrlen)) < 0) {
printf("error accepting connection (%d)\n", err);
}
socklen_t peer_addrlen = sizeof(struct sockaddr_storage);
zts_getpeername(accfd, (struct sockaddr*)&acc_in4, &peer_addrlen);
printf("accepted connection from %s : %d\n", inet_ntoa(acc_in4.sin_addr), ntohs(acc_in4.sin_port));
int tot = 0;
printf("reading from client...\n");
while(true) {
memset(rbuf, 0, sizeof rbuf);
if (tot == 500000)
{
break;
}
r = zts_read(accfd, rbuf, sizeof rbuf);
printf("r=%d\n", r);
printf("Received : %s\n", rbuf);
tot+= r;
printf("tot=%d\n", tot);
}
printf("tot=%d\n", tot);
printf("sending to client...\n");
w = zts_write(accfd, rbuf, strlen(rbuf));
printf("w=%d\n", r);
printf("Received : %s\n", rbuf);
err = zts_close(sockfd);
err = zts_close(accfd);
return err;
}

View File

@@ -1,107 +0,0 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#include <stdio.h>
#include <string.h>
#include <string>
#include <inttypes.h>
#if defined(_WIN32)
#include <WinSock2.h>
#include <stdint.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "libzt.h"
int main(int argc, char **argv)
{
if (argc != 4) {
printf("\nlibzt example server\n");
printf("server [config_file_path] [nwid] [bind_port]\n");
exit(0);
}
std::string path = argv[1];
std::string nwidstr = argv[2];
int bind_port = atoi(argv[3]);
int w=0, r=0, err=0, sockfd = 0, accfd = 0, flags = 0;
char rbuf[32];
memset(rbuf, 0, sizeof rbuf);
struct sockaddr_in in4, acc_in4;
in4.sin_port = htons(bind_port);
in4.sin_addr.s_addr = INADDR_ANY;
in4.sin_family = AF_INET;
// --- BEGIN EXAMPLE CODE
printf("Waiting for libzt to come online...\n");
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
printf("nwid=%llx\n", (unsigned long long)nwid);
zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = zts_get_node_id();
printf("I am %llx\n", (unsigned long long)nodeId);
if ((sockfd = zts_socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("error creating ZeroTier socket\n");
}
if ((err = zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) {
printf("error binding to interface (%d)\n", err);
}
/*
socklen_t peer_addrlen = sizeof(struct sockaddr_storage);
zts_getpeername(accfd, (struct sockaddr*)&acc_in4, &peer_addrlen);
DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in4.sin_addr), ntohs(acc_in4.sin_port));
*/
printf("reading from client...\n");
socklen_t addrlen = sizeof(acc_in4);
memset(&acc_in4, 0, sizeof acc_in4);
while(true) {
memset(&rbuf, 0, sizeof rbuf);
r = zts_recvfrom(accfd, rbuf, sizeof(rbuf), flags, (struct sockaddr *)&acc_in4, &addrlen);
if (r >= 0) {
char *ip = inet_ntoa(acc_in4.sin_addr);
printf("Received : r=%d, %s -- from: %s : %d\n", r, rbuf, ip, ntohs(acc_in4.sin_port));
}
}
/*
printf("sending to client...\n");
w = zts_write(accfd, rbuf, strlen(rbuf));
*/
err = zts_close(sockfd);
return err;
}

View File

@@ -1,114 +0,0 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#include <stdio.h>
#include <string.h>
#include <string>
#include <inttypes.h>
#if defined(_WIN32)
#include <WinSock2.h>
#include <stdint.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "libzt.h"
char *msg = (char*)"welcome to the machine";
uint64_t generate_adhoc_nwid_from_port(int port)
{
std::string padding;
if(port < 10) {
padding = "000";
} else if(port < 100) {
padding = "00";
} else if(port < 1000) {
padding = "0";
}
// We will join ad-hoc network ffSSSSEEEE000000
// Where SSSS = start port
// EEEE = end port
padding = padding + std::to_string(port); // SSSS
std::string nwidstr = "ff" + padding + padding + "000000"; // ff + SSSS + EEEE + 000000
return strtoull(nwidstr.c_str(), NULL, 16);
}
int main(int argc, char **argv)
{
if (argc != 5) {
printf("\nlibzt example client\n");
printf("client [config_file_path] [nwid] [remote_addr] [remote_port]\n");
exit(0);
}
std::string path = argv[1];
std::string nwidstr = argv[2];
std::string remote_addr = argv[3];
int remote_port = atoi(argv[4]);
int r=0, w=0, err=0, sockfd;
char rbuf[32];
memset(rbuf, 0, sizeof rbuf);
struct sockaddr_in6 in6;
in6.sin6_port = htons(remote_port);
inet_pton(AF_INET6, remote_addr.c_str(), &(in6.sin6_addr));
in6.sin6_family = AF_INET6;
// --- BEGIN EXAMPLE CODE
printf("Waiting for libzt to come online...\n");
uint64_t nwid = generate_adhoc_nwid_from_port(remote_port);
printf("nwid=%llx\n", (unsigned long long)nwid);
zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = zts_get_node_id();
printf("I am %llx\n", (unsigned long long)nodeId);
if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
printf("error creating ZeroTier socket\n");
}
if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) {
printf("error connecting to remote host (%d)\n", err);
}
printf("sending to server...\n");
w = zts_write(sockfd, msg, strlen(msg));
printf("reading from server...\n");
r = zts_read(sockfd, rbuf, strlen(msg));
printf("Sent : %s\n", msg);
printf("Received : %s\n", rbuf);
err = zts_close(sockfd);
return err;
}

View File

@@ -1,127 +0,0 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#include <stdio.h>
#include <string.h>
#include <string>
#include <inttypes.h>
#if defined(_WIN32)
#include <WinSock2.h>
#include <stdint.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "libzt.h"
uint64_t generate_adhoc_nwid_from_port(int port)
{
std::string padding;
if(port < 10) {
padding = "000";
} else if(port < 100) {
padding = "00";
} else if(port < 1000) {
padding = "0";
}
// We will join ad-hoc network ffSSSSEEEE000000
// Where SSSS = start port
// EEEE = end port
padding = padding + std::to_string(port); // SSSS
std::string nwidstr = "ff" + padding + padding + "000000"; // ff + SSSS + EEEE + 000000
return strtoull(nwidstr.c_str(), NULL, 16);
}
int main(int argc, char **argv)
{
if (argc != 4) {
printf("\nlibzt example server\n");
printf("server [config_file_path] [bind_port]\n");
exit(0);
}
std::string path = argv[1];
int bind_port = atoi(argv[2]);
uint64_t nwid = generate_adhoc_nwid_from_port(bind_port);
int w=0, r=0, err=0, sockfd, accfd;
char rbuf[32];
memset(rbuf, 0, sizeof rbuf);
struct sockaddr_in6 in6, acc_in6;
in6.sin6_port = htons(bind_port);
in6.sin6_family = AF_INET6;
in6.sin6_addr = in6addr_any;
fprintf(stderr, "nwid=%llx\n", (unsigned long long)nwid);
exit(-1);
// --- BEGIN EXAMPLE CODE
printf("Waiting for libzt to come online...\n");
nwid = generate_adhoc_nwid_from_port(bind_port);
printf("nwid=%llx\n", (unsigned long long)nwid);
zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = zts_get_node_id();
printf("I am %llx\n", (unsigned long long)nodeId);
if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
printf("error creating ZeroTier socket\n");
}
if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) {
printf("error binding to interface (%d)\n", err);
}
if ((err = zts_listen(sockfd, 100)) < 0) {
printf("error placing socket in LISTENING state (%d)\n", err);
}
socklen_t client_addrlen = sizeof(sockaddr_in6);
if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) {
printf("error accepting connection (%d)\n", err);
}
socklen_t peer_addrlen = sizeof(struct sockaddr_storage);
zts_getpeername(accfd, (struct sockaddr*)&acc_in6, &peer_addrlen);
//printf("accepted connection from %s : %d", inet_ntoa(acc_in6.sin6_addr), ntohs(acc_in6.sin6_port));
printf("reading from client...\n");
r = zts_read(accfd, rbuf, sizeof rbuf);
printf("sending to client...\n");
w = zts_write(accfd, rbuf, strlen(rbuf));
printf("Received : %s\n", rbuf);
err = zts_close(sockfd);
err = zts_close(accfd);
return err;
}

View File

@@ -1,96 +0,0 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#include <stdio.h>
#include <string.h>
#include <string>
#include <inttypes.h>
#if defined(_WIN32)
#include <WinSock2.h>
#include <stdint.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "libzt.h"
char *msg = (char*)"welcome to the machine";
int main(int argc, char **argv)
{
if (argc != 5) {
printf("\nlibzt example client\n");
printf("client [config_file_path] [nwid] [remote_addr] [remote_port]\n");
exit(0);
}
std::string path = argv[1];
std::string nwidstr = argv[2];
std::string remote_addr = argv[3];
int remote_port = atoi(argv[4]);
int r=0, w=0, err=0, sockfd;
char rbuf[32];
memset(rbuf, 0, sizeof rbuf);
struct sockaddr_in6 in6;
in6.sin6_port = htons(remote_port);
inet_pton(AF_INET6, remote_addr.c_str(), &(in6.sin6_addr));
in6.sin6_family = AF_INET6;
// --- BEGIN EXAMPLE CODE
printf("Waiting for libzt to come online...\n");
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
printf("nwid=%llx\n", (unsigned long long)nwid);
zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = zts_get_node_id();
printf("I am %llx\n", (unsigned long long)nodeId);
if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
printf("error creating ZeroTier socket\n");
}
if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) {
printf("error connecting to remote host (%d)\n", err);
}
printf("sending to server...\n");
w = zts_write(sockfd, msg, strlen(msg));
printf("reading from server...\n");
r = zts_read(sockfd, rbuf, strlen(msg));
printf("Sent : %s\n", msg);
printf("Received : %s\n", rbuf);
err = zts_close(sockfd);
return err;
}

View File

@@ -1,106 +0,0 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#include <stdio.h>
#include <string.h>
#include <string>
#include <inttypes.h>
#if defined(_WIN32)
#include <WinSock2.h>
#include <stdint.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif
#include "libzt.h"
int main(int argc, char **argv)
{
if (argc != 4) {
printf("\nlibzt example server\n");
printf("server [config_file_path] [nwid] [bind_port]\n");
exit(0);
}
std::string path = argv[1];
std::string nwidstr = argv[2];
int bind_port = atoi(argv[3]);
int w=0, r=0, err=0, sockfd, accfd;
char rbuf[32];
memset(rbuf, 0, sizeof rbuf);
struct sockaddr_in6 in6, acc_in6;
in6.sin6_port = htons(bind_port);
in6.sin6_family = AF_INET6;
in6.sin6_addr = in6addr_any;
// --- BEGIN EXAMPLE CODE
printf("Waiting for libzt to come online...\n");
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
printf("nwid=%llx\n", (unsigned long long)nwid);
zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = zts_get_node_id();
printf("I am %llx\n", (unsigned long long)nodeId);
if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
printf("error creating ZeroTier socket\n");
}
if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) {
printf("error binding to interface (%d)\n", err);
}
if ((err = zts_listen(sockfd, 100)) < 0) {
printf("error placing socket in LISTENING state (%d)\n", err);
}
socklen_t client_addrlen = sizeof(sockaddr_in6);
if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) {
printf("error accepting connection (%d)\n", err);
}
socklen_t peer_addrlen = sizeof(struct sockaddr_storage);
zts_getpeername(accfd, (struct sockaddr*)&acc_in6, &peer_addrlen);
//DEBUG_INFO("accepted connection from %s : %d", inet_ntoa(acc_in6.sin6_addr), ntohs(acc_in6.sin6_port));
printf("reading from client...\n");
r = zts_read(accfd, rbuf, sizeof rbuf);
printf("sending to client...\n");
w = zts_write(accfd, rbuf, strlen(rbuf));
printf("Received : %s\n", rbuf);
err = zts_close(sockfd);
err = zts_close(accfd);
return err;
}

View File

@@ -1,215 +0,0 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#if !defined(_MSC_VER)
#include <stdio.h>
#include <string.h>
#include <string>
#include <inttypes.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <dlfcn.h>
#include "libzt.h"
// function pointers which will have values assigned once the dynamic library is loaded
int (*_zts_set_service_port)(int portno);
int (*_zts_start)(const char *path, bool blocking);
int (*_zts_startjoin)(const char*, uint64_t);
void (*_zts_stop)(void);
int (*_zts_core_running)(void);
int (*_zts_stack_running)(void);
int (*_zts_ready)(void);
int (*_zts_join)(const uint64_t);
int (*_zts_leave)(const uint64_t);
void (*_zts_get_path)(char *homePath, const size_t len);
uint64_t (*_zts_get_node_id)(void);
int (*_zts_has_address)(const uint64_t);
int (*_zts_get_num_assigned_addresses)(const uint64_t nwid);
int (*_zts_get_address_at_index)(const uint64_t nwid, const int index, struct sockaddr_storage *addr);
int (*_zts_get_address)(const uint64_t nwid, struct sockaddr_storage *addr, const int address_family);
void (*_zts_get_6plane_addr)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId);
void (*_zts_get_rfc4193_addr)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId);
unsigned long (*_zts_get_peer_count)(void);
int (*_zts_get_peer_address)(char *peer, const uint64_t nodeId);
int (*_zts_socket)(int socket_family, int socket_type, int protocol);
int (*_zts_connect)(int fd, const struct sockaddr *addr, socklen_t addrlen);
int (*_zts_bind)(int fd, const struct sockaddr *addr, socklen_t addrlen);
int (*_zts_listen)(int fd, int backlog);
int (*_zts_accept)(int fd, struct sockaddr *addr, socklen_t *addrlen);
#if defined(__linux__)
int (*_zts_accept4)(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags);
#endif
int (*_zts_setsockopt)(int fd, int level, int optname, const void *optval, socklen_t optlen);
int (*_zts_getsockopt)(int fd, int level, int optname, void *optval, socklen_t *optlen);
int (*_zts_getsockname)(int fd, struct sockaddr *addr, socklen_t *addrlen);
int (*_zts_getpeername)(int fd, struct sockaddr *addr, socklen_t *addrlen);
int (*_zts_gethostname)(char *name, size_t len);
int (*_zts_sethostname)(const char *name, size_t len);
struct hostent *(*_zts_gethostbyname)(const char *name);
int (*_zts_close)(int fd);
int(*_zts_fcntl)(int fd, int cmd, int flags);
int (*_zts_ioctl)(int fd, unsigned long request, void *argp);
ssize_t (*_zts_send)(int fd, const void *buf, size_t len, int flags);
ssize_t (*_zts_sendto)(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen);
ssize_t (*_zts_sendmsg)(int fd, const struct msghdr *msg, int flags);
ssize_t (*_zts_recv)(int fd, void *buf, size_t len, int flags);
ssize_t (*_zts_recvfrom)(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen);
ssize_t (*_zts_recvmsg)(int fd, struct msghdr *msg,int flags);
int (*_zts_read)(int fd, void *buf, size_t len);
int (*_zts_write)(int fd, const void *buf, size_t len);
int (*_zts_shutdown)(int fd, int how);
int (*_zts_add_dns_nameserver)(struct sockaddr *addr);
int (*_zts_del_dns_nameserver)(struct sockaddr *addr);
void load_library_symbols(char *library_path)
{
void *libHandle = dlopen(library_path, RTLD_LAZY);
if (libHandle == NULL) {
DEBUG_ERROR("unable to load dynamic libs: %s", dlerror());
exit(-1);
}
// Load symbols from library (call these directly)
_zts_set_service_port = (int(*)(int portno))dlsym(libHandle, "zts_set_service_port");
_zts_start = (int(*)(const char *path, bool blocking))dlsym(libHandle, "zts_start");
_zts_startjoin = (int(*)(const char*, uint64_t))dlsym(libHandle, "zts_startjoin");
_zts_stop = (void(*)(void))dlsym(libHandle, "zts_stop");
_zts_core_running = (int(*)(void))dlsym(libHandle, "zts_core_running");
_zts_stack_running = (int(*)(void))dlsym(libHandle, "zts_stack_running");
_zts_ready = (int(*)(void))dlsym(libHandle, "zts_ready");
_zts_join = (int(*)(const uint64_t))dlsym(libHandle, "zts_join");
_zts_leave = (int(*)(const uint64_t))dlsym(libHandle, "zts_leave");
_zts_get_path = (void(*)(char *homePath, const size_t len))dlsym(libHandle, "zts_get_path");
_zts_get_node_id = (uint64_t(*)(void))dlsym(libHandle, "zts_get_node_id");
_zts_has_address = (int(*)(const uint64_t))dlsym(libHandle, "zts_has_address");
_zts_get_num_assigned_addresses = (int(*)(const uint64_t nwid))dlsym(libHandle, "zts_get_num_assigned_addresses");
_zts_get_address_at_index = (int(*)(const uint64_t nwid, const int index, struct sockaddr_storage *addr))dlsym(libHandle, "zts_get_address_at_index");
_zts_get_address = (int(*)(const uint64_t nwid, struct sockaddr_storage *addr, const int address_family))dlsym(libHandle, "zts_get_address");
_zts_get_6plane_addr = (void(*)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId))dlsym(libHandle, "zts_get_6plane_addr");
_zts_get_rfc4193_addr = (void(*)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId))dlsym(libHandle, "zts_get_rfc4193_addr");
_zts_get_peer_count = (unsigned long(*)(void))dlsym(libHandle, "zts_get_peer_count");
_zts_get_peer_address = (int(*)(char *peer, const uint64_t nodeId))dlsym(libHandle, "zts_get_peer_address");
_zts_socket = (int(*)(int socket_family, int socket_type, int protocol))dlsym(libHandle, "zts_socket");
_zts_connect = (int(*)(int fd, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_connect");
_zts_bind = (int(*)(int fd, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_bind");
_zts_listen = (int(*)(int fd, int backlog))dlsym(libHandle, "zts_listen");
_zts_accept = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_accept");
#if defined(__linux__)
_zts_accept4 = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags))dlsym(libHandle, "zts_accept4");
#endif
_zts_setsockopt = (int(*)(int fd, int level, int optname, const void *optval, socklen_t optlen))dlsym(libHandle, "zts_setsockopt");
_zts_getsockopt = (int(*)(int fd, int level, int optname, void *optval, socklen_t *optlen))dlsym(libHandle, "zts_getsockopt");
_zts_getsockname = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_getsockname");
_zts_getpeername = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_getpeername");
_zts_gethostname = (int(*)(char *name, size_t len))dlsym(libHandle, "zts_gethostname");
_zts_sethostname = (int(*)(const char *name, size_t len))dlsym(libHandle, "zts_sethostname");
_zts_gethostbyname = (struct hostent*(*)(const char *name))dlsym(libHandle, "zts_gethostbyname");
_zts_close = (int(*)(int fd))dlsym(libHandle, "zts_close");
_zts_fcntl = (int(*)(int fd, int cmd, int flags))dlsym(libHandle, "zts_fcntl");
_zts_ioctl = (int(*)(int fd, unsigned long request, void *argp))dlsym(libHandle, "zts_ioctl");
_zts_send = (ssize_t(*)(int fd, const void *buf, size_t len, int flags))dlsym(libHandle, "zts_send");
_zts_sendto = (ssize_t(*)(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_sendto");
_zts_sendmsg = (ssize_t(*)(int fd, const struct msghdr *msg, int flags))dlsym(libHandle, "zts_sendmsg");
_zts_recv = (ssize_t(*)(int fd, void *buf, size_t len, int flags))dlsym(libHandle, "zts_recv");
_zts_recvfrom = (ssize_t(*)(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_recvfrom");
_zts_recvmsg = (ssize_t(*)(int fd, struct msghdr *msg,int flags))dlsym(libHandle, "zts_recvmsg");
_zts_read = (int(*)(int fd, void *buf, size_t len))dlsym(libHandle, "zts_read");
_zts_write = (int(*)(int fd, const void *buf, size_t len))dlsym(libHandle, "zts_write");
_zts_shutdown = (int(*)(int fd, int how))dlsym(libHandle, "zts_shutdown");
_zts_add_dns_nameserver = (int(*)(struct sockaddr *addr))dlsym(libHandle, "zts_add_dns_nameserver");
}
char *msg = (char*)"welcome to the machine";
int main(int argc, char **argv)
{
if (argc != 5) {
printf("\nlibzt example client\n");
printf("client [config_file_path] [nwid] [remote_addr] [remote_port]\n");
exit(0);
}
#ifdef __linux__
char *library_path = (char*)"./libzt.so";
#endif
#ifdef __APPLE__
char *library_path = (char*)"./libzt.dylib";
#endif
load_library_symbols(library_path);
std::string path = argv[1];
std::string nwidstr = argv[2];
std::string remote_addr = argv[3];
int remote_port = atoi(argv[4]);
int r=0, w=0, err=0, sockfd;
char rbuf[32];
memset(rbuf, 0, sizeof rbuf);
struct sockaddr_in in4;
in4.sin_port = htons(remote_port);
in4.sin_addr.s_addr = inet_addr(remote_addr.c_str());
in4.sin_family = AF_INET;
//
printf("Waiting for libzt to come online...\n");
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
printf("nwid=%llx\n", (unsigned long long)nwid);
_zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = _zts_get_node_id();
printf("I am %llx\n", (unsigned long long)nodeId);
if ((sockfd = _zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("error creating ZeroTier socket\n");
}
if ((err = _zts_connect(sockfd, (const struct sockaddr *)&in4, sizeof(in4))) < 0) {
printf("error connecting to remote host (%d)\n", err);
}
printf("sending to server...\n");
w = _zts_write(sockfd, msg, strlen(msg));
printf("reading from server...\n");
r = _zts_read(sockfd, rbuf, strlen(msg));
printf("Sent : %s\n", msg);
printf("Received : %s\n", rbuf);
err = _zts_close(sockfd);
return err;
}
#endif // _MSC_VER

View File

@@ -1,222 +0,0 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
#if !defined(_MSC_VER)
#include <stdio.h>
#include <string.h>
#include <string>
#include <inttypes.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <dlfcn.h>
#include "libzt.h"
// function pointers which will have values assigned once the dynamic library is loaded
int (*_zts_set_service_port)(int portno);
int (*_zts_start)(const char *path, bool blocking);
int (*_zts_startjoin)(const char*, uint64_t);
void (*_zts_stop)(void);
int (*_zts_core_running)(void);
int (*_zts_stack_running)(void);
int (*_zts_ready)(void);
int (*_zts_join)(const uint64_t);
int (*_zts_leave)(const uint64_t);
void (*_zts_get_path)(char *homePath, const size_t len);
uint64_t (*_zts_get_node_id)(void);
int (*_zts_has_address)(const uint64_t);
int (*_zts_get_num_assigned_addresses)(const uint64_t nwid);
int (*_zts_get_address_at_index)(const uint64_t nwid, const int index, struct sockaddr_storage *addr);
int (*_zts_get_address)(const uint64_t nwid, struct sockaddr_storage *addr, const int address_family);
void (*_zts_get_6plane_addr)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId);
void (*_zts_get_rfc4193_addr)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId);
unsigned long (*_zts_get_peer_count)(void);
int (*_zts_get_peer_address)(char *peer, const uint64_t nodeId);
int (*_zts_socket)(int socket_family, int socket_type, int protocol);
int (*_zts_connect)(int fd, const struct sockaddr *addr, socklen_t addrlen);
int (*_zts_bind)(int fd, const struct sockaddr *addr, socklen_t addrlen);
int (*_zts_listen)(int fd, int backlog);
int (*_zts_accept)(int fd, struct sockaddr *addr, socklen_t *addrlen);
#if defined(__linux__)
int (*_zts_accept4)(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags);
#endif
int (*_zts_setsockopt)(int fd, int level, int optname, const void *optval, socklen_t optlen);
int (*_zts_getsockopt)(int fd, int level, int optname, void *optval, socklen_t *optlen);
int (*_zts_getsockname)(int fd, struct sockaddr *addr, socklen_t *addrlen);
int (*_zts_getpeername)(int fd, struct sockaddr *addr, socklen_t *addrlen);
int (*_zts_gethostname)(char *name, size_t len);
int (*_zts_sethostname)(const char *name, size_t len);
struct hostent *(*_zts_gethostbyname)(const char *name);
int (*_zts_close)(int fd);
int(*_zts_fcntl)(int fd, int cmd, int flags);
int (*_zts_ioctl)(int fd, unsigned long request, void *argp);
ssize_t (*_zts_send)(int fd, const void *buf, size_t len, int flags);
ssize_t (*_zts_sendto)(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen);
ssize_t (*_zts_sendmsg)(int fd, const struct msghdr *msg, int flags);
ssize_t (*_zts_recv)(int fd, void *buf, size_t len, int flags);
ssize_t (*_zts_recvfrom)(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen);
ssize_t (*_zts_recvmsg)(int fd, struct msghdr *msg,int flags);
int (*_zts_read)(int fd, void *buf, size_t len);
int (*_zts_write)(int fd, const void *buf, size_t len);
int (*_zts_shutdown)(int fd, int how);
int (*_zts_add_dns_nameserver)(struct sockaddr *addr);
int (*_zts_del_dns_nameserver)(struct sockaddr *addr);
void load_library_symbols(char *library_path)
{
void *libHandle = dlopen(library_path, RTLD_LAZY);
if (libHandle == NULL) {
DEBUG_ERROR("unable to load dynamic libs: %s", dlerror());
exit(-1);
}
// Load symbols from library (call these directly)
_zts_set_service_port = (int(*)(int portno))dlsym(libHandle, "zts_set_service_port");
_zts_start = (int(*)(const char *path, bool blocking))dlsym(libHandle, "zts_start");
_zts_startjoin = (int(*)(const char*, uint64_t))dlsym(libHandle, "zts_startjoin");
_zts_stop = (void(*)(void))dlsym(libHandle, "zts_stop");
_zts_core_running = (int(*)(void))dlsym(libHandle, "zts_core_running");
_zts_stack_running = (int(*)(void))dlsym(libHandle, "zts_stack_running");
_zts_ready = (int(*)(void))dlsym(libHandle, "zts_ready");
_zts_join = (int(*)(const uint64_t))dlsym(libHandle, "zts_join");
_zts_leave = (int(*)(const uint64_t))dlsym(libHandle, "zts_leave");
_zts_get_path = (void(*)(char *homePath, const size_t len))dlsym(libHandle, "zts_get_path");
_zts_get_node_id = (uint64_t(*)(void))dlsym(libHandle, "zts_get_node_id");
_zts_has_address = (int(*)(const uint64_t))dlsym(libHandle, "zts_has_address");
_zts_get_num_assigned_addresses = (int(*)(const uint64_t nwid))dlsym(libHandle, "zts_get_num_assigned_addresses");
_zts_get_address_at_index = (int(*)(const uint64_t nwid, const int index, struct sockaddr_storage *addr))dlsym(libHandle, "zts_get_address_at_index");
_zts_get_address = (int(*)(const uint64_t nwid, struct sockaddr_storage *addr, const int address_family))dlsym(libHandle, "zts_get_address");
_zts_get_6plane_addr = (void(*)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId))dlsym(libHandle, "zts_get_6plane_addr");
_zts_get_rfc4193_addr = (void(*)(struct sockaddr_storage *addr, const uint64_t nwid, const uint64_t nodeId))dlsym(libHandle, "zts_get_rfc4193_addr");
_zts_get_peer_count = (unsigned long(*)(void))dlsym(libHandle, "zts_get_peer_count");
_zts_get_peer_address = (int(*)(char *peer, const uint64_t nodeId))dlsym(libHandle, "zts_get_peer_address");
_zts_socket = (int(*)(int socket_family, int socket_type, int protocol))dlsym(libHandle, "zts_socket");
_zts_connect = (int(*)(int fd, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_connect");
_zts_bind = (int(*)(int fd, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_bind");
_zts_listen = (int(*)(int fd, int backlog))dlsym(libHandle, "zts_listen");
_zts_accept = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_accept");
#if defined(__linux__)
_zts_accept4 = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags))dlsym(libHandle, "zts_accept4");
#endif
_zts_setsockopt = (int(*)(int fd, int level, int optname, const void *optval, socklen_t optlen))dlsym(libHandle, "zts_setsockopt");
_zts_getsockopt = (int(*)(int fd, int level, int optname, void *optval, socklen_t *optlen))dlsym(libHandle, "zts_getsockopt");
_zts_getsockname = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_getsockname");
_zts_getpeername = (int(*)(int fd, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_getpeername");
_zts_gethostname = (int(*)(char *name, size_t len))dlsym(libHandle, "zts_gethostname");
_zts_sethostname = (int(*)(const char *name, size_t len))dlsym(libHandle, "zts_sethostname");
_zts_gethostbyname = (struct hostent*(*)(const char *name))dlsym(libHandle, "zts_gethostbyname");
_zts_close = (int(*)(int fd))dlsym(libHandle, "zts_close");
_zts_fcntl = (int(*)(int fd, int cmd, int flags))dlsym(libHandle, "zts_fcntl");
_zts_ioctl = (int(*)(int fd, unsigned long request, void *argp))dlsym(libHandle, "zts_ioctl");
_zts_send = (ssize_t(*)(int fd, const void *buf, size_t len, int flags))dlsym(libHandle, "zts_send");
_zts_sendto = (ssize_t(*)(int fd, const void *buf, size_t len, int flags, const struct sockaddr *addr, socklen_t addrlen))dlsym(libHandle, "zts_sendto");
_zts_sendmsg = (ssize_t(*)(int fd, const struct msghdr *msg, int flags))dlsym(libHandle, "zts_sendmsg");
_zts_recv = (ssize_t(*)(int fd, void *buf, size_t len, int flags))dlsym(libHandle, "zts_recv");
_zts_recvfrom = (ssize_t(*)(int fd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen))dlsym(libHandle, "zts_recvfrom");
_zts_recvmsg = (ssize_t(*)(int fd, struct msghdr *msg,int flags))dlsym(libHandle, "zts_recvmsg");
_zts_read = (int(*)(int fd, void *buf, size_t len))dlsym(libHandle, "zts_read");
_zts_write = (int(*)(int fd, const void *buf, size_t len))dlsym(libHandle, "zts_write");
_zts_shutdown = (int(*)(int fd, int how))dlsym(libHandle, "zts_shutdown");
_zts_add_dns_nameserver = (int(*)(struct sockaddr *addr))dlsym(libHandle, "zts_add_dns_nameserver");
}
int main(int argc, char **argv)
{
if (argc != 4) {
printf("\nlibzt example server\n");
printf("server [config_file_path] [nwid] [bind_port]\n");
exit(0);
}
#ifdef __linux__
char *library_path = (char*)"./libzt.so";
#endif
#ifdef __APPLE__
char *library_path = (char*)"./libzt.dylib";
#endif
load_library_symbols(library_path);
std::string path = argv[1];
std::string nwidstr = argv[2];
int bind_port = atoi(argv[3]);
int w=0, r=0, err=0, sockfd, accfd;
char rbuf[32];
memset(rbuf, 0, sizeof rbuf);
struct sockaddr_in in4, acc_in4;
in4.sin_port = htons(bind_port);
in4.sin_addr.s_addr = INADDR_ANY;
in4.sin_family = AF_INET;
//
printf("Waiting for libzt to come online...\n");
uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16);
printf("nwid=%llx\n", (unsigned long long)nwid);
_zts_startjoin(path.c_str(), nwid);
uint64_t nodeId = _zts_get_node_id();
printf("I am %llx\n", (unsigned long long)nodeId);
if ((sockfd = _zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("error creating ZeroTier socket\n");
}
if ((err = _zts_bind(sockfd, (struct sockaddr *)&in4, sizeof(struct sockaddr_in)) < 0)) {
printf("error binding to interface (%d)\n", err);
}
if ((err = _zts_listen(sockfd, 100)) < 0) {
printf("error placing socket in LISTENING state (%d)\n", err);
}
socklen_t client_addrlen = sizeof(sockaddr_in);
if ((accfd = _zts_accept(sockfd, (struct sockaddr *)&acc_in4, &client_addrlen)) < 0) {
printf("error accepting connection (%d)\n", err);
}
socklen_t peer_addrlen = sizeof(struct sockaddr_storage);
_zts_getpeername(accfd, (struct sockaddr*)&acc_in4, &peer_addrlen);
DEBUG_INFO("accepted connection from %s : %d\n", inet_ntoa(acc_in4.sin_addr), ntohs(acc_in4.sin_port));
printf("reading from client...\n");
r = _zts_read(accfd, rbuf, sizeof rbuf);
printf("sending to client...\n");
w = _zts_write(accfd, rbuf, strlen(rbuf));
printf("Received : %s\n", rbuf);
err = _zts_close(sockfd);
err = _zts_close(accfd);
return err;
}
#endif // _MSC_VER

View File

@@ -1,280 +0,0 @@
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ZeroTier.h"
bool node_ready = false;
bool network_ready = false;
// Example callbacks
void myZeroTierEventCallback(struct zts_callback_msg *msg)
{
//
// Node events
//
if (msg->eventCode == ZTS_EVENT_NODE_ONLINE) {
printf("ZTS_EVENT_NODE_ONLINE, node=%llx\n", msg->node->address);
node_ready = true;
// ZeroTier service is running and online
}
if (msg->eventCode == ZTS_EVENT_NODE_OFFLINE) {
printf("ZTS_EVENT_NODE_OFFLINE\n");
node_ready = false;
// ZeroTier service is running and online
}
if (msg->eventCode == ZTS_EVENT_NODE_NORMAL_TERMINATION) {
printf("ZTS_EVENT_NODE_NORMAL_TERMINATION\n");
// ZeroTier service has stopped
}
//
// Virtual network events
//
if (msg->eventCode == ZTS_EVENT_NETWORK_NOT_FOUND) {
printf("ZTS_EVENT_NETWORK_NOT_FOUND --- network=%llx\n", msg->network->nwid);
// Is your nwid incorrect?
}
if (msg->eventCode == ZTS_EVENT_NETWORK_REQUESTING_CONFIG) {
printf("ZTS_EVENT_NETWORK_REQUESTING_CONFIG --- network=%llx\n", msg->network->nwid);
// Node is requesting network config details from controller, please wait
}
if (msg->eventCode == ZTS_EVENT_NETWORK_ACCESS_DENIED) {
printf("ZTS_EVENT_NETWORK_ACCESS_DENIED --- network=%llx\n", msg->network->nwid);
// This node is not authorized to join nwid
}
if (msg->eventCode == ZTS_EVENT_NETWORK_READY_IP4) {
printf("ZTS_EVENT_NETWORK_READY_IP4 --- network=%llx\n", msg->network->nwid);
network_ready = true;
// IPv4 traffic can now be processed for nwid
}
if (msg->eventCode == ZTS_EVENT_NETWORK_READY_IP6) {
printf("ZTS_EVENT_NETWORK_READY_IP6 --- network=%llx\n", msg->network->nwid);
network_ready = true;
}
if (msg->eventCode == ZTS_EVENT_NETWORK_DOWN) {
printf("ZTS_EVENT_NETWORK_DOWN --- network=%llx\n", msg->network->nwid);
// Can happen if another thread called zts_leave()
}
//
// Network stack events
//
if (msg->eventCode == ZTS_EVENT_NETIF_UP) {
printf("ZTS_EVENT_NETIF_UP --- network=%llx, mac=%llx, mtu=%d\n",
msg->netif->nwid,
msg->netif->mac,
msg->netif->mtu);
network_ready = true;
}
if (msg->eventCode == ZTS_EVENT_NETIF_DOWN) {
printf("ZTS_EVENT_NETIF_DOWN --- network=%llx, mac=%llx\n",
msg->netif->nwid,
msg->netif->mac);
network_ready = true;
}
//
// Address events
//
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP4) {
char ipstr[INET_ADDRSTRLEN];
struct sockaddr_in *in4 = (struct sockaddr_in*)&(msg->addr->addr);
inet_ntop(AF_INET, &(in4->sin_addr), ipstr, INET_ADDRSTRLEN);
printf("ZTS_EVENT_ADDR_NEW_IP4 --- addr=%s (on network=%llx)\n",
ipstr, msg->addr->nwid);
}
if (msg->eventCode == ZTS_EVENT_ADDR_ADDED_IP6) {
char ipstr[INET6_ADDRSTRLEN];
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)&(msg->addr->addr);
inet_ntop(AF_INET6, &(in6->sin6_addr), ipstr, INET6_ADDRSTRLEN);
printf("ZTS_EVENT_ADDR_NEW_IP6 --- addr=%s (on network=%llx)\n",
ipstr, msg->addr->nwid);
}
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP4) {
char ipstr[INET_ADDRSTRLEN];
struct sockaddr_in *in4 = (struct sockaddr_in*)&(msg->addr->addr);
inet_ntop(AF_INET, &(in4->sin_addr), ipstr, INET_ADDRSTRLEN);
printf("ZTS_EVENT_ADDR_REMOVED_IP4 --- addr=%s (on network=%llx)\n",
ipstr, msg->addr->nwid);
}
if (msg->eventCode == ZTS_EVENT_ADDR_REMOVED_IP6) {
char ipstr[INET6_ADDRSTRLEN];
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)&(msg->addr->addr);
inet_ntop(AF_INET6, &(in6->sin6_addr), ipstr, INET6_ADDRSTRLEN);
printf("ZTS_EVENT_ADDR_REMOVED_IP6 --- addr=%s (on network=%llx)\n",
ipstr, msg->addr->nwid);
}
//
// Peer events
//
if (msg->eventCode == ZTS_EVENT_PEER_P2P) {
printf("ZTS_EVENT_PEER_P2P --- node=%llx\n", msg->peer->address);
// A direct path is known for nodeId
}
if (msg->eventCode == ZTS_EVENT_PEER_RELAY) {
printf("ZTS_EVENT_PEER_RELAY --- node=%llx\n", msg->peer->address);
// No direct path is known for nodeId
}
}
void printPeerDetails(struct zts_peer_details *pd)
{
printf("\npeer=%llx, latency=%d, version=%d.%d.%d, pathCount=%d\n",
pd->address,
pd->latency,
pd->versionMajor,
pd->versionMinor,
pd->versionRev,
pd->pathCount);
// Print all known paths for each peer
for (int j=0; j<pd->pathCount; j++) {
char ipstr[INET6_ADDRSTRLEN];
int port;
struct sockaddr *sa = (struct sockaddr *)&(pd->paths[j].address);
if (sa->sa_family == AF_INET) {
struct sockaddr_in *in4 = (struct sockaddr_in*)sa;
inet_ntop(AF_INET, &(in4->sin_addr), ipstr, INET_ADDRSTRLEN);
port = ntohs(in4->sin_port);
}
if (sa->sa_family == AF_INET6) {
struct sockaddr_in6 *in6 = (struct sockaddr_in6*)sa;
inet_ntop(AF_INET6, &(in6->sin6_addr), ipstr, INET6_ADDRSTRLEN);
}
printf("\tpath[%d]=%s, port=%d\n", j, ipstr, port);
}
}
void getSinglePeerDetails(uint64_t peerId)
{
struct zts_peer_details pd;
int err = zts_get_peer(&pd, peerId);
if (err == ZTS_ERR_OK) {
printf("(%d) call succeeded\n", err);
} if (err == ZTS_ERR_INVALID_ARG) {
printf("(%d) invalid argument\n", err);
return;
} if (err == ZTS_ERR_SERVICE) {
printf("(%d) error: service is unavailable\n", err);
return;
} if (err == ZTS_ERR_INVALID_OP) {
printf("(%d) error: invalid API operation\n", err);
return;
} if (err == ZTS_ERR_NO_RESULT) {
printf("(%d) error: object or result not found\n", err);
return;
}
if (err == 0) { // ZTS_ERR_OK
printPeerDetails(&pd);
}
}
// Similar to "zerotier-cli listpeers"
void getAllPeerDetails()
{
struct zts_peer_details pd[128];
/* This number should be large enough to handle the
expected number of peers. This call can also get
expensive for large numbers of peers. Consider using
get_peer(struct zts_peer_details *pds, uint64_t peerId)
instead */
int num = 128;
int err;
if ((err = zts_get_peers(pd, &num)) < 0) {
printf("error (%d)\n", err);
return;
}
if (num) {
printf("num=%d\n", num);
for (int i=0; i<num; i++) {
printPeerDetails(&pd[i]);
}
}
}
struct zts_stats_proto protoSpecificStats;
void display_stack_stats()
{
int err = 0;
// Count received pings
if ((err = zts_get_protocol_stats(ZTS_STATS_PROTOCOL_ICMP, &protoSpecificStats)) != ZTS_ERR_OK) {
printf("zts_get_proto_stats()=%d", err);
return;
}
printf("icmp.recv=%d\n", protoSpecificStats.recv);
// Count dropped TCP packets
if ((err = zts_get_protocol_stats(ZTS_STATS_PROTOCOL_TCP, &protoSpecificStats)) != ZTS_ERR_OK) {
printf("zts_get_proto_stats()=%d", err);
return;
}
printf("tcp.drop=%d\n", protoSpecificStats.drop);
}
int main()
{
char *str = "welcome to the machine";
char *remoteIp = "11.7.7.223";
int remotePort = 8082;
int fd, err = 0;
struct zts_sockaddr_in addr;
addr.sin_family = ZTS_AF_INET;
addr.sin_addr.s_addr = inet_addr(remoteIp);
addr.sin_port = htons(remotePort);
// Set up ZeroTier service
int defaultServicePort = 9994;
int nwid = 0x0123456789abcdef;
zts_start("test/path", &myZeroTierEventCallback, defaultServicePort);
printf("Waiting for node to come online...\n");
// Wait for the node to come online before joining a network
while (!node_ready) { sleep(1); }
zts_join(nwid);
printf("Joined virtual network. Requesting configuration...\n");
//sleep(1);
// Get multiple peer's details
getAllPeerDetails();
// Get a single peer's details
getSinglePeerDetails(0x01b34f67c90);
int status = -1;
// Get status of the node/service
status = zts_get_node_status();
printf("zts_get_node_status()=%d\n", status);
// Get status of a network
status = zts_get_network_status(0x0123456789abcdef);
printf("zts_get_network_status()=%d\n", status);
while (true) {
sleep(1);
status = zts_get_node_status();
printf("zts_get_node_status()=%d\n", status);
display_stack_stats();
}
// Socket API example
printf("zts_errno=%d\n",zts_errno);
if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("error creating socket\n");
}
printf("fd=%d, zts_errno=%d\n", fd, zts_errno);
if ((err = zts_connect(fd, (const struct sockaddr *)&addr, sizeof(addr))) < 0) {
printf("error connecting to remote host\n");
}
if ((err = zts_write(fd, str, strlen(str))) < 0) {
printf("error writing to socket\n");
}
zts_close(fd);
zts_stop();
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,92 +0,0 @@
#include <stdio.h>
#ifdef _WIN32
#include <WinSock2.h>
#include <stdint.h>
#include <WS2tcpip.h>
#include <Windows.h>
#else
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#endif
#include <ZeroTier.h>
bool node_ready = false;
bool network_ready = false;
void myZeroTierEventCallback(struct zts_callback_msg *msg)
{
switch (msg->eventCode)
{
case ZTS_EVENT_NODE_ONLINE:
printf("ZTS_EVENT_NODE_ONLINE, node=%llx\n", msg->node->address);
node_ready = true;
break;
case ZTS_EVENT_NODE_OFFLINE:
printf("ZTS_EVENT_NODE_OFFLINE\n");
node_ready = false;
break;
case ZTS_EVENT_NETWORK_READY_IP4:
printf("ZTS_EVENT_NETWORK_READY_IP4 --- network=%llx\n", msg->network->nwid);
network_ready = true;
break;
case ZTS_EVENT_PEER_P2P:
printf("ZTS_EVENT_PEER_P2P --- node=%llx\n", msg->peer->address);
break;
case ZTS_EVENT_PEER_RELAY:
printf("ZTS_EVENT_PEER_RELAY --- node=%llx\n", msg->peer->address);
break;
// ...
default:
break;
}
}
void delay(int n)
{
#ifdef _WIN32
Sleep(n * 1000);
#else
sleep(n);
#endif
}
int main()
{
char *str = "welcome to the machine";
char *remoteIp = "11.7.7.223";
int remotePort = 8082;
int fd, err = 0;
struct sockaddr_in addr;
addr.sin_family = ZTS_AF_INET;
addr.sin_addr.s_addr = inet_addr(remoteIp);
addr.sin_port = htons(remotePort);
// Set up ZeroTier service and wai for callbacks
int port = 9994;
uint64_t nwid = 0x0123456789abcdef;
zts_start("path", &myZeroTierEventCallback, port);
printf("Waiting for node to come online...\n");
while (!node_ready) { delay(1); }
printf("joining network...\n");
zts_join(nwid);
printf("Joined virtual network. Requesting configuration...\n");
while (!network_ready) { delay(1); }
printf("I am %llx\n", zts_get_node_id());
// Socket API example
if ((fd = zts_socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("error creating socket\n");
}
if ((err = zts_connect(fd, (const struct sockaddr *)&addr, sizeof(addr))) < 0) {
printf("error connecting to remote host (%s)\n", remoteIp);
}
if ((err = zts_write(fd, str, strlen(str))) < 0) {
printf("error writing to socket\n");
}
zts_close(fd);
zts_stop();
return 0;
}