From a4a5ea5c318cd3013b56be6a4e7c08dd1f26ce10 Mon Sep 17 00:00:00 2001 From: Joseph Henry Date: Tue, 21 Nov 2017 15:24:51 -0800 Subject: [PATCH] Updated bindings --- .../client.cpp | 28 +++- .../server.cpp | 28 +++- examples/bindings/cpp/ipv6adhoc/client.cpp | 116 ++++++++++++++++ examples/bindings/cpp/ipv6adhoc/server.cpp | 124 ++++++++++++++++++ examples/bindings/cpp/ipv6simple/client.cpp | 98 ++++++++++++++ examples/bindings/cpp/ipv6simple/server.cpp | 103 +++++++++++++++ examples/bindings/java/ExampleApp.java | 96 +++++++++++++- examples/bindings/java/Makefile | 9 +- examples/bindings/java/README.md | 9 +- examples/bindings/java/zerotier/ZeroTier.java | 36 ++--- examples/bindings/processing/README.md | 6 + examples/bindings/scala/ExampleApp.scala | 2 +- examples/bindings/scala/libzt.scala | 8 +- test/selftest.cpp | 6 +- 14 files changed, 636 insertions(+), 33 deletions(-) rename examples/bindings/cpp/{simple_client_server => ipv4simple}/client.cpp (60%) rename examples/bindings/cpp/{simple_client_server => ipv4simple}/server.cpp (65%) create mode 100644 examples/bindings/cpp/ipv6adhoc/client.cpp create mode 100644 examples/bindings/cpp/ipv6adhoc/server.cpp create mode 100644 examples/bindings/cpp/ipv6simple/client.cpp create mode 100644 examples/bindings/cpp/ipv6simple/server.cpp create mode 100644 examples/bindings/processing/README.md diff --git a/examples/bindings/cpp/simple_client_server/client.cpp b/examples/bindings/cpp/ipv4simple/client.cpp similarity index 60% rename from examples/bindings/cpp/simple_client_server/client.cpp rename to examples/bindings/cpp/ipv4simple/client.cpp index 5254cc7..52161ff 100644 --- a/examples/bindings/cpp/simple_client_server/client.cpp +++ b/examples/bindings/cpp/ipv4simple/client.cpp @@ -1,3 +1,29 @@ +/* + * 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 . + * + * -- + * + * 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 #include #include @@ -40,7 +66,7 @@ int main(int argc, char **argv) in4.sin_addr.s_addr = inet_addr(remote_addr.c_str()); in4.sin_family = AF_INET; - // --- BEGIN + // --- BEGIN EXAMPLE CODE DEBUG_TEST("Waiting for libzt to come online...\n"); uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); diff --git a/examples/bindings/cpp/simple_client_server/server.cpp b/examples/bindings/cpp/ipv4simple/server.cpp similarity index 65% rename from examples/bindings/cpp/simple_client_server/server.cpp rename to examples/bindings/cpp/ipv4simple/server.cpp index 97846e7..a2ec509 100644 --- a/examples/bindings/cpp/simple_client_server/server.cpp +++ b/examples/bindings/cpp/ipv4simple/server.cpp @@ -1,3 +1,29 @@ +/* + * 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 . + * + * -- + * + * 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 #include #include @@ -36,7 +62,7 @@ int main(int argc, char **argv) in4.sin_addr.s_addr = INADDR_ANY; in4.sin_family = AF_INET; - // --- BEGIN + // --- BEGIN EXAMPLE CODE DEBUG_TEST("Waiting for libzt to come online...\n"); uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); diff --git a/examples/bindings/cpp/ipv6adhoc/client.cpp b/examples/bindings/cpp/ipv6adhoc/client.cpp new file mode 100644 index 0000000..9bfc3d7 --- /dev/null +++ b/examples/bindings/cpp/ipv6adhoc/client.cpp @@ -0,0 +1,116 @@ +/* + * 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 . + * + * -- + * + * 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 +#include +#include +#include +#include + +#if defined(__linux__) || defined(__APPLE__) +#include +#include +#include +#include +#endif + +#if defined(__MINGW32__) || defined(__MINGW64__) +#include +#include +#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 + + DEBUG_TEST("Waiting for libzt to come online...\n"); + uint64_t nwid = generate_adhoc_nwid_from_port(remote_port); + printf("nwid=%llx\n", nwid); + zts_startjoin(path.c_str(), nwid); + uint64_t nodeId = zts_get_node_id(); + DEBUG_TEST("I am %llx", nodeId); + + sleep(5); + + if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) + DEBUG_ERROR("error creating ZeroTier socket"); + + if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) + DEBUG_ERROR("error connecting to remote host (%d)", err); + + DEBUG_TEST("sending to server..."); + w = zts_write(sockfd, msg, strlen(msg)); + + DEBUG_TEST("reading from server..."); + r = zts_read(sockfd, rbuf, strlen(msg)); + + DEBUG_TEST("Sent : %s", msg); + DEBUG_TEST("Received : %s", rbuf); + + err = zts_close(sockfd); + + return err; +} \ No newline at end of file diff --git a/examples/bindings/cpp/ipv6adhoc/server.cpp b/examples/bindings/cpp/ipv6adhoc/server.cpp new file mode 100644 index 0000000..c631cce --- /dev/null +++ b/examples/bindings/cpp/ipv6adhoc/server.cpp @@ -0,0 +1,124 @@ +/* + * 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 . + * + * -- + * + * 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 +#include +#include +#include + +#if defined(__linux__) || defined(__APPLE__) +#include +#include +#include +#include +#endif + +#if defined(__MINGW32__) || defined(__MINGW64__) +#include +#include +#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", nwid); + exit(-1); + + // --- BEGIN EXAMPLE CODE + + DEBUG_TEST("Waiting for libzt to come online...\n"); + nwid = generate_adhoc_nwid_from_port(bind_port); + printf("nwid=%llx\n", nwid); + zts_startjoin(path.c_str(), nwid); + uint64_t nodeId = zts_get_node_id(); + DEBUG_TEST("I am %llx", nodeId); + + if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) + DEBUG_ERROR("error creating ZeroTier socket"); + + if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) + DEBUG_ERROR("error binding to interface (%d)", err); + + if ((err = zts_listen(sockfd, 100)) < 0) + DEBUG_ERROR("error placing socket in LISTENING state (%d)", err); + + socklen_t client_addrlen = sizeof(sockaddr_in6); + if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) + DEBUG_ERROR("error accepting connection (%d)", 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)); + + DEBUG_TEST("reading from client..."); + r = zts_read(accfd, rbuf, sizeof rbuf); + + DEBUG_TEST("sending to client..."); + w = zts_write(accfd, rbuf, strlen(rbuf)); + + DEBUG_TEST("Received : %s", rbuf); + + err = zts_close(sockfd); + err = zts_close(accfd); + + return err; +} \ No newline at end of file diff --git a/examples/bindings/cpp/ipv6simple/client.cpp b/examples/bindings/cpp/ipv6simple/client.cpp new file mode 100644 index 0000000..74e7623 --- /dev/null +++ b/examples/bindings/cpp/ipv6simple/client.cpp @@ -0,0 +1,98 @@ +/* + * 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 . + * + * -- + * + * 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 +#include +#include +#include +#include + +#if defined(__linux__) || defined(__APPLE__) +#include +#include +#include +#include +#endif + +#if defined(__MINGW32__) || defined(__MINGW64__) +#include +#include +#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 + + DEBUG_TEST("Waiting for libzt to come online...\n"); + uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); + printf("nwid=%llx\n", nwid); + zts_startjoin(path.c_str(), nwid); + uint64_t nodeId = zts_get_node_id(); + DEBUG_TEST("I am %llx", nodeId); + + sleep(5); + + if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) + DEBUG_ERROR("error creating ZeroTier socket"); + + if ((err = zts_connect(sockfd, (const struct sockaddr *)&in6, sizeof(in6))) < 0) + DEBUG_ERROR("error connecting to remote host (%d)", err); + + DEBUG_TEST("sending to server..."); + w = zts_write(sockfd, msg, strlen(msg)); + + DEBUG_TEST("reading from server..."); + r = zts_read(sockfd, rbuf, strlen(msg)); + + DEBUG_TEST("Sent : %s", msg); + DEBUG_TEST("Received : %s", rbuf); + + err = zts_close(sockfd); + + return err; +} \ No newline at end of file diff --git a/examples/bindings/cpp/ipv6simple/server.cpp b/examples/bindings/cpp/ipv6simple/server.cpp new file mode 100644 index 0000000..48a9392 --- /dev/null +++ b/examples/bindings/cpp/ipv6simple/server.cpp @@ -0,0 +1,103 @@ +/* + * 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 . + * + * -- + * + * 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 +#include +#include +#include + +#if defined(__linux__) || defined(__APPLE__) +#include +#include +#include +#include +#endif + +#if defined(__MINGW32__) || defined(__MINGW64__) +#include +#include +#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 + + DEBUG_TEST("Waiting for libzt to come online...\n"); + uint64_t nwid = strtoull(nwidstr.c_str(),NULL,16); + printf("nwid=%llx\n", nwid); + zts_startjoin(path.c_str(), nwid); + uint64_t nodeId = zts_get_node_id(); + DEBUG_TEST("I am %llx", nodeId); + + if ((sockfd = zts_socket(AF_INET6, SOCK_STREAM, 0)) < 0) + DEBUG_ERROR("error creating ZeroTier socket"); + + if ((err = zts_bind(sockfd, (struct sockaddr *)&in6, sizeof(struct sockaddr_in6)) < 0)) + DEBUG_ERROR("error binding to interface (%d)", err); + + if ((err = zts_listen(sockfd, 100)) < 0) + DEBUG_ERROR("error placing socket in LISTENING state (%d)", err); + + socklen_t client_addrlen = sizeof(sockaddr_in6); + if ((accfd = zts_accept(sockfd, (struct sockaddr *)&acc_in6, &client_addrlen)) < 0) + DEBUG_ERROR("error accepting connection (%d)", 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)); + + DEBUG_TEST("reading from client..."); + r = zts_read(accfd, rbuf, sizeof rbuf); + + DEBUG_TEST("sending to client..."); + w = zts_write(accfd, rbuf, strlen(rbuf)); + + DEBUG_TEST("Received : %s", rbuf); + + err = zts_close(sockfd); + err = zts_close(accfd); + + return err; +} \ No newline at end of file diff --git a/examples/bindings/java/ExampleApp.java b/examples/bindings/java/ExampleApp.java index 437a354..9a086a2 100644 --- a/examples/bindings/java/ExampleApp.java +++ b/examples/bindings/java/ExampleApp.java @@ -28,6 +28,7 @@ import zerotier.ZeroTier; import java.net.*; +import java.lang.Thread; public class ExampleApp { @@ -45,23 +46,108 @@ public class ExampleApp { new Thread(new Runnable() { public void run() { - System.out.println("starting libzt"); - //libzt.startjoin("config_path", "123456789abcdeff"); - libzt.startjoin("/Users/joseph/op/zt/libzt/ztjni", "17d709436c2c5367"); - System.out.println("started."); + String path = "/Users/joseph/op/zt/libzt/ztjni"; + long nwid = 0xa09acf0233ac70fdL; + + // METHOD 1 (easy) + // Blocking call that waits for the core, userspace stack and IP assignment before unblocking + if (true) + { + libzt.startjoin(path, nwid); + } + + // METHOD 2 + // Optionally-nonblocking call. You'll have to use the below process to determine when you + // are allowed to stack making socket calls. The advantage of this method is that you can + // get your nodeId before joining the network. + if (false) { + libzt.start(path, true); + // Wait for core service to start + while(!libzt.core_running()) { + try { + Thread.sleep(1000); + } + catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + System.out.println("core started"); + long nodeId = libzt.get_node_id(); + System.out.println("nodeId=" + Long.toHexString(nodeId)); + libzt.join(nwid); + // Wait for userspace stack to start, we trigger this by joining a network + while(!libzt.stack_running()) { + try { + Thread.sleep(1000); + } + catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + } + + System.out.println("core and stack started, now ready for socket API calls"); + + int num_addresses = libzt.get_num_assigned_addresses(nwid); + System.out.println("number of assigned addresses for this node on this network = " + String.valueOf(num_addresses)); + + // get IPv4 address + //InetAddress assigned = libzt.get_address(nwid, libzt.AF_INET6); + //System.out.println("assigned address = " + assigned.toString()); + + // get address at arbitrary (index < num_addresses) + //assigned = libzt.get_address_at_index(nwid, 0); + //System.out.println("assigned address = " + assigned.toString()); + + // get IPv6 address + //assigned = libzt.get_address(nwid, libzt.AF_INET6); + //System.out.println("assigned address = " + assigned.toString()); + + String homePath = libzt.get_path(); + System.out.println("homePath=" + homePath); + + while(!libzt.has_address(nwid)) { + try { + Thread.sleep(1000); + } + catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + } + + + //InetAddress assigned = libzt.get_address(nwid); + //System.out.println("assigned address = " + assigned.toString()); int fd = 0, err = 0; if ((fd = libzt.socket(libzt.AF_INET, libzt.SOCK_STREAM, 0)) < 0) { System.out.println("error creating socket"); return; } + System.out.println("Created socket"); - InetSocketAddress addr = new InetSocketAddress("0.0.0.0", 3434); + while(true) + { + try { Thread.sleep(3000); } + catch (InterruptedException e) { e.printStackTrace(); } + } + + /* + InetSocketAddress remoteAddr = new InetSocketAddress("172.27.54.9", 3434); + + if ((err = libzt.connect(fd, remoteAddr)) < 0) { + System.out.println("error connecting"); + return; + } + */ +/* + InetSocketAddress localAddr = new InetSocketAddress("0.0.0.0", 3434); if ((err = libzt.bind(fd, addr)) < 0) { System.out.println("error binding socket to virtual interface"); return; } + */ } }).start(); diff --git a/examples/bindings/java/Makefile b/examples/bindings/java/Makefile index f6af4b8..38849e1 100644 --- a/examples/bindings/java/Makefile +++ b/examples/bindings/java/Makefile @@ -11,8 +11,15 @@ endif example_java_app: javac *.java +example_java_app_1.6: + javac -source 1.6 -bootclasspath /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.41.x86_64/jre/lib/rt.jar -target 1.6 *.java + copy_dynamic_lib: cp ../../../$(BUILD)/$(SHARED_LIB) . +jar: + jar cf libzt.jar libzt.dylib zerotier/ZeroTier.class + clean: - -find . -type f \( -name '*.class' \) -delete \ No newline at end of file + -rm -rf *.jar *.dylib *.so + -find . -type f \( -name '*.class' \) -delete diff --git a/examples/bindings/java/README.md b/examples/bindings/java/README.md index 965533d..dc6773f 100644 --- a/examples/bindings/java/README.md +++ b/examples/bindings/java/README.md @@ -1,19 +1,24 @@ ## ZeroTier with Java via JNI *** -To get this example project to work, do the following: +### Example App - From libzt main directory, build shared library: `make shared_jni_lib` - Copy the resultant dynamic library (`*.so` or `*.dylib`) from `build/` to this current directory - Change to this directory and `make example_java_app` - Run: `java -cp "." ExampleApp` +### JAR file (with embedded C++ dynamic library) + +``` +make example_app +make jar +``` Notes: Upon execution, it will load the libzt dynamic library via the `loadLibrary` method and begin generating an identity. - *** More resources on JNI usage: diff --git a/examples/bindings/java/zerotier/ZeroTier.java b/examples/bindings/java/zerotier/ZeroTier.java index 2afbc30..c78b8d4 100644 --- a/examples/bindings/java/zerotier/ZeroTier.java +++ b/examples/bindings/java/zerotier/ZeroTier.java @@ -29,10 +29,9 @@ package zerotier; import java.net.*; public class ZeroTier { - // socket families - public static int AF_UNIX = 1; public static int AF_INET = 2; + public static int AF_INET6 = 30; // socket types public static int SOCK_STREAM = 1; public static int SOCK_DGRAM = 2; @@ -45,33 +44,38 @@ public class ZeroTier { // fcntl cmds public static int F_GETFL = 3; public static int F_SETFL = 4; - // basic service controls - public native void start(String homeDir, boolean blocking); - public native void startjoin(String homeDir, String nwid); + // service controls + public native void start(String homePath, boolean blocking); + public native void startjoin(String homePath, long nwid); public native void stop(); - public native boolean running(); - public native void join(String nwid); - public native void leave(String nwid); - // advanced service controls - public native void get_path(); - public native int get_id(); + public native boolean core_running(); + public native boolean stack_running(); + public native boolean ready(); + public native int join(long nwid); + public native int leave(long nwid); + public native String get_path(); + public native long get_node_id(); + public native int get_num_assigned_addresses(long nwid); + public native InetAddress get_address_at_index(long nwid, int index); + public native boolean has_address(long nwid); + public native InetAddress get_address(long nwid, int address_family); public native void get_6plane_addr(); public native void get_rfc4193_addr(); // socket API public native int socket(int family, int type, int protocol); - public native int connect(int fd, String addr, int port); + public native int connect(int fd, InetSocketAddress addr); public native int bind(int fd, InetSocketAddress addr); public native int listen(int fd, int backlog); - public native int accept(int fd, Address addr); + public native int accept(int fd, InetSocketAddress addr); public native int accept4(int fd, String addr, int port); public native int close(int fd); //public native int setsockopt(); //public native int getsockopt(); + public native int sendto(int fd, byte[] buf, int len, int flags, InetSocketAddress addr); + public native int send(int fd, byte[] buf, int len, int flags); + public native int recvfrom(int fd, byte[] buf, int len, int flags, InetSocketAddress addr); public native int read(int fd, byte[] buf, int len); public native int write(int fd, byte[] buf, int len); - public native int sendto(int fd, byte[] buf, int len, int flags, Address addr); - public native int send(int fd, byte[] buf, int len, int flags); - public native int recvfrom(int fd, byte[] buf, int len, int flags, Address addr); public native int shutdown(int fd, int how); //public native int getsockname(); //public native int getpeername(); diff --git a/examples/bindings/processing/README.md b/examples/bindings/processing/README.md new file mode 100644 index 0000000..e204b30 --- /dev/null +++ b/examples/bindings/processing/README.md @@ -0,0 +1,6 @@ +## Processing +*** + +See the [Java](../java) eample. + +*NOTE: Currently Processing requires that any JAR file contain only code compiled against JDK 1.6* \ No newline at end of file diff --git a/examples/bindings/scala/ExampleApp.scala b/examples/bindings/scala/ExampleApp.scala index 29589b6..d774169 100644 --- a/examples/bindings/scala/ExampleApp.scala +++ b/examples/bindings/scala/ExampleApp.scala @@ -30,7 +30,7 @@ object ExampleApp extends App { // load libzt.dylib or libzt.so System.loadLibrary("zt") val libzt = new ZeroTier - libzt.startjoin("/Users/joseph/op/zt/libzt/ztjni", "1212121212121212") + libzt.startjoin("/Users/joseph/op/zt/libzt/ztjni", 0xa09acf0232a930f7L) val fd = libzt.socket(2, 1, 0) println(s"libzt.socket(): $fd") } \ No newline at end of file diff --git a/examples/bindings/scala/libzt.scala b/examples/bindings/scala/libzt.scala index 9a8470d..4ee304c 100644 --- a/examples/bindings/scala/libzt.scala +++ b/examples/bindings/scala/libzt.scala @@ -32,14 +32,14 @@ class ZeroTier { // socket types // basic service controls @native def start(path: String, blocking: Boolean): Int - @native def startjoin(path: String, nwid: String): Int + @native def startjoin(path: String, nwid: Long): Int @native def stop(): Unit @native def running(): Int - @native def join(nwid: String): Unit - @native def leave(nwid: String): Unit + @native def join(nwid: Long): Unit + @native def leave(nwid: Long): Unit // advanced service controls //@native def get_path(): Unit - //@native def get_id(): Int + @native def get_node_id(): Long //@native def get_6plane_addr(): Unit //@native def get_rfc4193_addr(): Unit // socket API diff --git a/test/selftest.cpp b/test/selftest.cpp index e5c3808..da324e2 100644 --- a/test/selftest.cpp +++ b/test/selftest.cpp @@ -2697,9 +2697,11 @@ int main(int argc , char *argv[]) exit(-1); } uint64_t nwid = strtoull(argv[2],NULL,16); - zts_start(argv[3], true); - zts_join(nwid); uint64_t nodeId = zts_get_node_id(); + + zts_start(argv[3], true); + //zts_join(nwid); + //sleep(5); DEBUG_TEST("generated id: %llx", nodeId); exit(0); }