Update examples for Windows compatibility (plus some extra tidbits)
This commit is contained in:
@@ -97,12 +97,31 @@
|
||||
* inet_ntop(AF_INET6, &(in6->sin6_addr), dstStr, INET6_ADDRSTRLEN);
|
||||
*/
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.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
|
||||
|
||||
void delay_ms(long ms)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
Sleep(ms);
|
||||
#else
|
||||
usleep(ms*1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
#include "ZeroTier.h"
|
||||
|
||||
@@ -209,12 +228,12 @@ int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example\n");
|
||||
printf("server <config_file_path> <adhocStartPort> <adhocEndPort> <ztServicePort>\n");
|
||||
printf("adhoc <config_file_path> <adhocStartPort> <adhocEndPort> <ztServicePort>\n");
|
||||
exit(0);
|
||||
}
|
||||
int adhocStartPort = atoi(argv[2]); // Start of port range your application will use
|
||||
int adhocEndPort = atoi(argv[3]); // End of port range your application will use
|
||||
int ztServicePort = atoi(argv[4]); // Port the library uses to send encapsulated and encrypted UDP packets to peers
|
||||
int ztServicePort = atoi(argv[4]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
|
||||
uint64_t adhoc_nwid = zts_generate_adhoc_nwid_from_range(adhocStartPort, adhocEndPort);
|
||||
int err = ZTS_ERR_OK;
|
||||
@@ -226,7 +245,7 @@ int main(int argc, char **argv)
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online...\n");
|
||||
while (!nodeReady) { usleep(50000); }
|
||||
while (!nodeReady) { delay_ms(50); }
|
||||
printf("This node's identity is stored in %s\n", argv[1]);
|
||||
|
||||
if((err = zts_join(adhoc_nwid)) != ZTS_ERR_OK) {
|
||||
@@ -234,12 +253,12 @@ int main(int argc, char **argv)
|
||||
exit(1);
|
||||
}
|
||||
printf("Joining network %llx\n", adhoc_nwid);
|
||||
while (!networkReady) { usleep(50000); }
|
||||
while (!networkReady) { delay_ms(50); }
|
||||
|
||||
// Idle and just show callback events, stack statistics, etc
|
||||
|
||||
printf("Node will now idle...\n");
|
||||
while (true) { sleep(1); }
|
||||
while (true) { delay_ms(1000); }
|
||||
|
||||
// Shut down service and stack threads
|
||||
|
||||
|
||||
@@ -2,10 +2,8 @@
|
||||
* libzt API example
|
||||
*/
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
@@ -20,6 +18,15 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
void delay_ms(long ms)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
Sleep(ms);
|
||||
#else
|
||||
usleep(ms*1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
#include "ZeroTier.h"
|
||||
|
||||
bool nodeReady = false;
|
||||
@@ -202,31 +209,35 @@ void myZeroTierEventCallback(struct zts_callback_msg *msg)
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
if (argc != 6) {
|
||||
printf("\nlibzt example client\n");
|
||||
printf("client <config_file_path> <nwid> <remoteAddr> <remotePort>\n");
|
||||
printf("client <config_file_path> <nwid> <remoteAddr> <remotePort> <ztServicePort>\n");
|
||||
exit(0);
|
||||
}
|
||||
uint64_t nwid = strtoull(argv[2],NULL,16);
|
||||
std::string remoteAddr = argv[3];
|
||||
int remotePort = atoi(argv[4]);
|
||||
int defaultServicePort = 9998;
|
||||
uint64_t nwid = strtoull(argv[2],NULL,16); // Network ID to join
|
||||
std::string remoteAddr = argv[3]; // Remote application's virtual ZT address
|
||||
int remotePort = atoi(argv[4]); // Port the application will try to connect to the server on
|
||||
int ztServicePort = atoi(argv[5]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
|
||||
struct zts_sockaddr_in in4;
|
||||
in4.sin_port = htons(remotePort);
|
||||
in4.sin_addr.s_addr = inet_addr(remoteAddr.c_str());
|
||||
#if defined(_WIN32)
|
||||
in4.sin_addr.S_addr = inet_addr(remoteAddr.c_str());;
|
||||
#else
|
||||
in4.sin_addr.s_addr = inet_addr(remoteAddr.c_str());;
|
||||
#endif
|
||||
in4.sin_family = ZTS_AF_INET;
|
||||
|
||||
// Bring up ZeroTier service and join network
|
||||
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
if((err = zts_start(argv[1], &myZeroTierEventCallback, defaultServicePort)) != ZTS_ERR_OK) {
|
||||
if((err = zts_start(argv[1], &myZeroTierEventCallback, ztServicePort)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online...\n");
|
||||
while (!nodeReady) { usleep(50000); }
|
||||
while (!nodeReady) { delay_ms(50); }
|
||||
printf("This node's identity is stored in %s\n", argv[1]);
|
||||
|
||||
if((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
@@ -235,7 +246,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
printf("Joining network %llx\n", nwid);
|
||||
printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
|
||||
while (!networkReady) { usleep(50000); }
|
||||
while (!networkReady) { delay_ms(50); }
|
||||
|
||||
// Socket-like API example
|
||||
|
||||
@@ -249,19 +260,18 @@ int main(int argc, char **argv)
|
||||
exit(1);
|
||||
}
|
||||
// Retries are often required since ZT uses transport-triggered links (explained above)
|
||||
int delay = 1; // second
|
||||
for (;;) {
|
||||
printf("Connecting to remote host...\n");
|
||||
if ((err = zts_connect(fd, (const struct sockaddr *)&in4, sizeof(in4))) < 0) {
|
||||
printf("Error connecting to remote host (fd=%d, ret=%d, zts_errno=%d). Trying again in %ds\n",
|
||||
fd, err, zts_errno, delay);
|
||||
printf("Error connecting to remote host (fd=%d, ret=%d, zts_errno=%d). Trying again.\n",
|
||||
fd, err, zts_errno);
|
||||
zts_close(fd);
|
||||
printf("Creating socket...\n");
|
||||
if ((fd = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0)) < 0) {
|
||||
printf("Error creating ZeroTier socket (fd=%d, zts_errno=%d). Exiting.\n", fd, zts_errno);
|
||||
exit(1);
|
||||
}
|
||||
sleep(delay);
|
||||
delay_ms(250);
|
||||
}
|
||||
else {
|
||||
printf("Connected.\n");
|
||||
|
||||
@@ -97,12 +97,31 @@
|
||||
* inet_ntop(AF_INET6, &(in6->sin6_addr), dstStr, INET6_ADDRSTRLEN);
|
||||
*/
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
#include <stdlib.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
|
||||
|
||||
void delay_ms(long ms)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
Sleep(ms);
|
||||
#else
|
||||
usleep(ms*1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
#include "ZeroTier.h"
|
||||
|
||||
@@ -218,7 +237,7 @@ void printPeerDetails(struct zts_peer_details *pd)
|
||||
pd->versionRev,
|
||||
pd->pathCount);
|
||||
// Print all known paths for each peer
|
||||
for (int j=0; j<pd->pathCount; j++) {
|
||||
for (unsigned int j=0; j<pd->pathCount; j++) {
|
||||
char ipstr[INET6_ADDRSTRLEN];
|
||||
int port = 0;
|
||||
struct sockaddr *sa = (struct sockaddr *)&(pd->paths[j].address);
|
||||
@@ -269,7 +288,7 @@ void getAllPeerDetails()
|
||||
expensive for large numbers of peers. Consider using
|
||||
get_peer(struct zts_peer_details *pds, uint64_t peerId)
|
||||
instead */
|
||||
int num = 128;
|
||||
unsigned int num = 128;
|
||||
int err;
|
||||
if ((err = zts_get_peers(pd, &num)) < 0) {
|
||||
printf("error (%d)\n", err);
|
||||
@@ -277,7 +296,7 @@ void getAllPeerDetails()
|
||||
}
|
||||
if (num) {
|
||||
printf("num=%d\n", num);
|
||||
for (int i=0; i<num; i++) {
|
||||
for (unsigned int i=0; i<num; i++) {
|
||||
printPeerDetails(&pd[i]);
|
||||
}
|
||||
}
|
||||
@@ -304,13 +323,13 @@ void display_stack_stats()
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 3) {
|
||||
if (argc != 4) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server <config_file_path> <nwid> <ztServicePort>\n");
|
||||
printf("comprehensive <config_file_path> <nwid> <ztServicePort>\n");
|
||||
exit(0);
|
||||
}
|
||||
uint64_t nwid = strtoull(argv[2],NULL,16);
|
||||
int ztServicePort = atoi(argv[3]);
|
||||
uint64_t nwid = strtoull(argv[2],NULL,16); // Network ID to join
|
||||
int ztServicePort = atoi(argv[3]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
|
||||
// Bring up ZeroTier service and join network
|
||||
|
||||
@@ -329,8 +348,8 @@ int main(int argc, char **argv)
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online...\n");
|
||||
while (!nodeReady) { usleep(50000); }
|
||||
printf("This node ID is %lx\n", zts_get_node_id());
|
||||
while (!nodeReady) { delay_ms(50); }
|
||||
printf("This node ID is %llx\n", zts_get_node_id());
|
||||
printf("This node's identity is stored in %s\n", argv[1]);
|
||||
|
||||
if((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
@@ -339,7 +358,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
printf("Joining network %llx\n", nwid);
|
||||
printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
|
||||
while (!networkReady) { usleep(50000); }
|
||||
while (!networkReady) { delay_ms(50); }
|
||||
|
||||
// Get multiple peer's details
|
||||
getAllPeerDetails();
|
||||
@@ -360,7 +379,7 @@ int main(int argc, char **argv)
|
||||
// Idle and just show callback events, stack statistics, etc
|
||||
|
||||
while (true) {
|
||||
usleep(50000);
|
||||
delay_ms(1000);
|
||||
status = zts_get_node_status();
|
||||
printf("zts_get_node_status()=%d\n", status);
|
||||
display_stack_stats();
|
||||
|
||||
106
examples/cpp/ip6.cpp
Normal file
106
examples/cpp/ip6.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
@@ -2,10 +2,8 @@
|
||||
* libzt API example
|
||||
*/
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <string>
|
||||
#include <inttypes.h>
|
||||
|
||||
@@ -20,6 +18,15 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
void delay_ms(long ms)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
Sleep(ms);
|
||||
#else
|
||||
usleep(ms*1000);
|
||||
#endif
|
||||
}
|
||||
|
||||
#include "ZeroTier.h"
|
||||
|
||||
bool nodeReady = false;
|
||||
@@ -204,32 +211,33 @@ int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 5) {
|
||||
printf("\nlibzt example server\n");
|
||||
printf("server <config_file_path> <nwid> <4|6> <bind port>\n");
|
||||
printf("server <config_file_path> <nwid> <serverBindPort> <ztServicePort>\n");
|
||||
exit(0);
|
||||
}
|
||||
uint64_t nwid = strtoull(argv[2],NULL,16);
|
||||
int ipVersion = atoi(argv[3]);
|
||||
int bindPort = atoi(argv[4]);
|
||||
int defaultServicePort = 9998;
|
||||
uint64_t nwid = strtoull(argv[2],NULL,16); // Network ID to join
|
||||
int serverBindPort = atoi(argv[3]); // Port the application should bind to
|
||||
int ztServicePort = atoi(argv[4]); // Port ZT uses to send encrypted UDP packets to peers (try something like 9994)
|
||||
|
||||
struct zts_sockaddr_in in4, acc_in4;
|
||||
if (ipVersion == 4) {
|
||||
in4.sin_port = htons(bindPort);
|
||||
in4.sin_addr.s_addr = INADDR_ANY;
|
||||
in4.sin_family = ZTS_AF_INET;
|
||||
}
|
||||
in4.sin_port = htons(serverBindPort);
|
||||
#if defined(_WIN32)
|
||||
in4.sin_addr.S_addr = INADDR_ANY;
|
||||
#else
|
||||
in4.sin_addr.s_addr = INADDR_ANY;
|
||||
#endif
|
||||
in4.sin_family = ZTS_AF_INET;
|
||||
|
||||
// Bring up ZeroTier service and join network
|
||||
|
||||
int fd, accfd;
|
||||
int err = ZTS_ERR_OK;
|
||||
|
||||
if((err = zts_start(argv[1], &myZeroTierEventCallback, defaultServicePort)) != ZTS_ERR_OK) {
|
||||
if((err = zts_start(argv[1], &myZeroTierEventCallback, ztServicePort)) != ZTS_ERR_OK) {
|
||||
printf("Unable to start service, error = %d. Exiting.\n", err);
|
||||
exit(1);
|
||||
}
|
||||
printf("Waiting for node to come online...\n");
|
||||
while (!nodeReady) { usleep(50000); }
|
||||
while (!nodeReady) { delay_ms(50); }
|
||||
printf("This node's identity is stored in %s\n", argv[1]);
|
||||
|
||||
if((err = zts_join(nwid)) != ZTS_ERR_OK) {
|
||||
@@ -238,7 +246,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
printf("Joining network %llx\n", nwid);
|
||||
printf("Don't forget to authorize this device in my.zerotier.com or the web API!\n");
|
||||
while (!networkReady) { usleep(50000); }
|
||||
while (!networkReady) { delay_ms(50); }
|
||||
|
||||
// Socket-like API example
|
||||
|
||||
|
||||
Reference in New Issue
Block a user