diff --git a/.github/workflows/jar.yaml b/.github/workflows/jar.yaml new file mode 100644 index 0000000..5421229 --- /dev/null +++ b/.github/workflows/jar.yaml @@ -0,0 +1,20 @@ +name: Build Java JAR + +on: [workflow_dispatch] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Checkout submodules + run: git submodule update --init + + - name: Install JDK + run: sudo apt install default-jdk + + - name: Build + run: ./build.sh host-jar "release" + diff --git a/CMakeLists.txt b/CMakeLists.txt index 195f1a9..e9edddb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,7 +82,8 @@ endif() # ------------------------------------------------------------------------------ # | INCLUDE DIRECTORIES | # ------------------------------------------------------------------------------ - +# Temporary measure to test GitHub workflow on Ubuntu +include_directories(/usr/lib/jvm/java-11-openjdk-amd64/include/linux/) # ZeroTier include_directories(${ZTO_SRC_DIR}) include_directories(${ZTO_SRC_DIR}/include) diff --git a/build.sh b/build.sh index 6152528..6f367dc 100755 --- a/build.sh +++ b/build.sh @@ -493,7 +493,7 @@ host-jar() mkdir -p $PKG_OUTPUT_DIR # Share same cache dir with CMake JAVA_JAR_DIR=$CACHE_DIR/pkg/jar - JAVA_JAR_SOURCE_TREE_DIR=$JAVA_JAR_DIR/com/zerotier/sdk/ + JAVA_JAR_SOURCE_TREE_DIR=$JAVA_JAR_DIR/com/zerotier/sockets/ mkdir -p $JAVA_JAR_SOURCE_TREE_DIR cp -f src/bindings/java/*.java $JAVA_JAR_SOURCE_TREE_DIR # Build @@ -503,8 +503,8 @@ host-jar() cp -f $CACHE_DIR/lib/libzt.* $JAVA_JAR_DIR cd $JAVA_JAR_DIR export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8 - javac -Xlint:deprecation com/zerotier/sdk/*.java - jar cf libzt-"$(git describe --abbrev=0)".jar $SHARED_LIB_NAME com/zerotier/sdk/*.class + javac -Xlint:deprecation com/zerotier/sockets/*.java + jar cf libzt-"$(git describe --abbrev=0)".jar $SHARED_LIB_NAME com/zerotier/sockets/*.class rm -rf com $SHARED_LIB_NAME cd - # Copy JAR to dist/ @@ -633,7 +633,7 @@ format-code() if [[ ! $(which $CLANG_FORMAT) = "" ]]; then # Eventually: find . -path ./ext -prune -false -o -type f \( -iname \*.c -o -iname \*.h -o -iname \*.cpp -o -iname \*.hpp \) -exec clang-format -i {} \; - $CLANG_FORMAT -i include/*.h \ + $CLANG_FORMAT -i --verbose include/*.h \ src/*.c \ src/*.cpp \ src/*.hpp \ diff --git a/examples/java/Example.java b/examples/java/Example.java index ba3d761..25e649e 100644 --- a/examples/java/Example.java +++ b/examples/java/Example.java @@ -1,12 +1,14 @@ -import com.zerotier.sdk.*; +import com.zerotier.sockets.*; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.math.BigInteger; +import java.net.InetAddress; +import java.net.DatagramPacket; public class selftest { public static void main(String[] args) { - System.err.println(args.length); if (args.length < 4 || args.length > 5) { System.err.println("Invalid arguments"); System.err.println(" Usage: "); @@ -20,7 +22,9 @@ public class selftest { int port = 0; String mode = args[0]; storagePath = args[1]; - BigInteger networkId = new BigInteger(args[2], 16); + BigInteger nwid = new BigInteger(args[2], 16); + Long networkId = nwid.longValue(); + if (args.length == 4) { port = Integer.parseInt(args[3]); } @@ -29,7 +33,7 @@ public class selftest { port = Integer.parseInt(args[4]); } System.out.println("mode = " + mode); - System.out.println("networkId = " + Long.toHexString(networkId.longValue())); + System.out.println("networkId = " + Long.toHexString(networkId)); System.out.println("storagePath = " + storagePath); System.out.println("remoteAddr = " + remoteAddr); System.out.println("port = " + port); @@ -37,9 +41,8 @@ public class selftest { // ZeroTier setup ZeroTierNode node = new ZeroTierNode(); - node.initFromStorage(storagePath); - // node.initSetEventHandler(new MyZeroTierEventListener()); + node.initSetEventHandler(new MyZeroTierEventListener()); // node.initSetPort(9994); node.start(); @@ -49,29 +52,40 @@ public class selftest { } System.out.println("Node ID: " + Long.toHexString(node.getId())); System.out.println("Joining network..."); - node.join(networkId.longValue()); + node.join(networkId); System.out.println("Waiting for network..."); - while (! node.isNetworkTransportReady(networkId.longValue())) { + while (! node.isNetworkTransportReady(networkId)) { ZeroTierNative.zts_util_delay(50); } - System.out.println("joined"); + System.out.println("Joined"); + + // IPv4 + + InetAddress addr4 = node.getIPv4Address(networkId); + System.out.println("IPv4 address = " + addr4.getHostAddress()); + + // IPv6 + + InetAddress addr6 = node.getIPv6Address(networkId); + System.out.println("IPv6 address = " + addr6.getHostAddress()); + + // MAC address + + System.out.println("MAC address = " + node.getMACAddress(networkId)); // Socket logic if (mode.equals("server")) { System.out.println("Starting server..."); try { - ZeroTierSocket socket = - new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_STREAM, 0); - socket.bind("0.0.0.0", port); - socket.listen(100); - ZeroTierSocket newConnection = socket.accept(); - ZeroTierInputStream inputStream = newConnection.getInputStream(); + ZeroTierServerSocket listener = new ZeroTierServerSocket(port); + ZeroTierSocket conn = listener.accept(); + ZeroTierInputStream inputStream = conn.getInputStream(); DataInputStream dataInputStream = new DataInputStream(inputStream); String message = dataInputStream.readUTF(); System.out.println("recv: " + message); - socket.close(); - newConnection.close(); + listener.close(); + conn.close(); } catch (Exception ex) { System.out.println(ex); @@ -81,9 +95,7 @@ public class selftest { if (mode.equals("client")) { System.out.println("Starting client..."); try { - ZeroTierSocket socket = - new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_STREAM, 0); - socket.connect(remoteAddr, port); + ZeroTierSocket socket = new ZeroTierSocket(remoteAddr, port); ZeroTierOutputStream outputStream = socket.getOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); dataOutputStream.writeUTF("Hello from java!"); @@ -99,40 +111,38 @@ public class selftest { /** * (OPTIONAL) event handler */ -/* class MyZeroTierEventListener implements ZeroTierEventListener { - public void onZeroTierEvent(long id, int event_code) + public void onZeroTierEvent(long id, int eventCode) { - if (event_code == ZeroTierNative.ZTS_EVENT_NODE_UP) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_UP) { System.out.println("EVENT_NODE_UP"); } - if (event_code == ZeroTierNative.ZTS_EVENT_NODE_ONLINE) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_ONLINE) { System.out.println("EVENT_NODE_ONLINE: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NODE_OFFLINE) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_OFFLINE) { System.out.println("EVENT_NODE_OFFLINE"); } - if (event_code == ZeroTierNative.ZTS_EVENT_NODE_DOWN) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_DOWN) { System.out.println("EVENT_NODE_DOWN"); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP4) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP4) { System.out.println("ZTS_EVENT_NETWORK_READY_IP4: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP6) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP6) { System.out.println("ZTS_EVENT_NETWORK_READY_IP6: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_DOWN) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_DOWN) { System.out.println("EVENT_NETWORK_DOWN: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_OK) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_OK) { System.out.println("EVENT_NETWORK_OK: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_ACCESS_DENIED) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_ACCESS_DENIED) { System.out.println("EVENT_NETWORK_ACCESS_DENIED: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_NOT_FOUND) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_NOT_FOUND) { System.out.println("EVENT_NETWORK_NOT_FOUND: " + Long.toHexString(id)); } } } -*/ diff --git a/src/bindings/java/JavaSockets.cxx b/src/bindings/java/JavaSockets.cxx index 651e68d..d3de89b 100644 --- a/src/bindings/java/JavaSockets.cxx +++ b/src/bindings/java/JavaSockets.cxx @@ -53,13 +53,13 @@ void fdset2ztfdset(JNIEnv* env, int nfds, zts_fd_set* src_fd_set, jobject dest_z * Called from Java, saves a static reference to the VM so it can be used * later to call a user-specified callback method from C. */ -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1init(JNIEnv* env, jobject thisObj) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1init(JNIEnv* env, jobject thisObj) { jint rs = env->GetJavaVM(&jvm); return rs != JNI_OK ? ZTS_ERR_GENERAL : ZTS_ERR_OK; } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1socket( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1socket( JNIEnv* env, jobject thisObj, jint family, @@ -70,38 +70,19 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1socket( return retval > -1 ? retval : -(zts_errno); // Encode lwIP errno into return value for JNI functions only } -/* JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1connect(JNIEnv* env, jobject thisObj, jint fd, jobject addr) -{ - struct zts_sockaddr_storage ss; - zta2ss(env, &ss, addr); - socklen_t addrlen = ss.ss_family == ZTS_AF_INET ? sizeof(struct zts_sockaddr_in) : sizeof(struct zts_sockaddr_in6); - int retval = zts_bsd_connect(fd, (struct zts_sockaddr*)&ss, addrlen); - return retval > -1 ? retval : -(zts_errno); -} - -JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1bind(JNIEnv* env, jobject thisObj, jint fd, jobject addr) -{ - struct zts_sockaddr_storage ss; - zta2ss(env, &ss, addr); - zts_socklen_t addrlen = - ss.ss_family == ZTS_AF_INET ? sizeof(struct zts_sockaddr_in) : sizeof(struct zts_sockaddr_in6); - int retval = zts_bsd_bind(fd, (struct zts_sockaddr*)&ss, addrlen); - return retval > -1 ? retval : -(zts_errno); -} -*/ - -JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1listen(JNIEnv* env, jobject thisObj, jint fd, int backlog) +Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1listen(JNIEnv* env, jobject thisObj, jint fd, int backlog) { int retval = zts_bsd_listen(fd, backlog); return retval > -1 ? retval : -(zts_errno); } -JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1accept(JNIEnv* env, jobject thisObj, jint fd, jobject addr, jint port) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1accept( + JNIEnv* env, + jobject thisObj, + jint fd, + jobject addr, + jint port) { struct zts_sockaddr_storage ss; zts_socklen_t addrlen = sizeof(struct zts_sockaddr_storage); @@ -109,95 +90,9 @@ Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1accept(JNIEnv* env, jobject thisO ss2zta(env, &ss, addr); return retval > -1 ? retval : -(zts_errno); } -/* -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1setsockopt( - JNIEnv* env, - jobject thisObj, - jint fd, - jint level, - jint optname, - jobject optval) -{ - jclass c = env->GetObjectClass(optval); - if (! c) { - return ZTS_ERR_SERVICE; - } - int optval_int = -1; - - if (optname == SO_BROADCAST || optname == SO_KEEPALIVE || optname == SO_REUSEADDR || optname == SO_REUSEPORT - || optname == TCP_NODELAY) { - jfieldID fid = env->GetFieldID(c, "booleanValue", "Z"); - optval_int = (int)(env->GetBooleanField(optval, fid)); - } - if (optname == IP_TTL || optname == SO_RCVTIMEO || optname == IP_TOS || optname == SO_LINGER || optname == SO_RCVBUF - || optname == SO_SNDBUF) { - jfieldID fid = env->GetFieldID(c, "integerValue", "I"); - optval_int = env->GetIntField(optval, fid); - } - - int retval = ZTS_ERR_OK; - - if (optname == SO_RCVTIMEO) { - struct timeval tv; - // Convert milliseconds from setSoTimeout() call to seconds and microseconds - tv.tv_usec = optval_int * 1000; - tv.tv_sec = optval_int / 1000000; - retval = zts_bsd_setsockopt(fd, level, optname, &tv, sizeof(tv)); - } - else { - retval = zts_bsd_setsockopt(fd, level, optname, &optval_int, sizeof(optval_int)); - } - return retval > -1 ? retval : -(zts_errno); -} - -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1getsockopt( - JNIEnv* env, - jobject thisObj, - jint fd, - jint level, - jint optname, - jobject optval) -{ - jclass c = env->GetObjectClass(optval); - if (! c) { - return ZTS_ERR_SERVICE; - } - int optval_int = 0; - zts_socklen_t optlen; // Intentionally not used - - int retval; - - if (optname == SO_RCVTIMEO) { - struct zts_timeval tv; - optlen = sizeof(tv); - retval = zts_bsd_getsockopt(fd, level, optname, &tv, &optlen); - // Convert seconds and microseconds back to milliseconds - optval_int = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); - } - else { - retval = zts_bsd_getsockopt(fd, level, optname, &optval_int, &optlen); - } - - if (optname == SO_BROADCAST || optname == SO_KEEPALIVE || optname == SO_REUSEADDR || optname == SO_REUSEPORT - || optname == TCP_NODELAY) { - jfieldID fid = env->GetFieldID(c, "isBoolean", "Z"); - env->SetBooleanField(optval, fid, true); - fid = env->GetFieldID(c, "booleanValue", "Z"); - env->SetBooleanField(optval, fid, (bool)optval_int); - } - if (optname == IP_TTL || optname == SO_RCVTIMEO || optname == IP_TOS || optname == SO_LINGER || optname == SO_RCVBUF - || optname == SO_SNDBUF) { - jfieldID fid = env->GetFieldID(c, "isInteger", "Z"); - env->SetBooleanField(optval, fid, true); - fid = env->GetFieldID(c, "integerValue", "I"); - env->SetIntField(optval, fid, optval_int); - } - return retval > -1 ? retval : -(zts_errno); -} -*/ JNIEXPORT jboolean JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1getsockname(JNIEnv* env, jobject thisObj, jint fd, jobject addr) +Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1getsockname(JNIEnv* env, jobject thisObj, jint fd, jobject addr) { struct zts_sockaddr_storage ss; zts_socklen_t addrlen = sizeof(struct zts_sockaddr_storage); @@ -207,7 +102,7 @@ Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1getsockname(JNIEnv* env, jobject } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1getpeername(JNIEnv* env, jobject thisObj, jint fd, jobject addr) +Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1getpeername(JNIEnv* env, jobject thisObj, jint fd, jobject addr) { struct zts_sockaddr_storage ss; int retval = @@ -216,12 +111,12 @@ Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1getpeername(JNIEnv* env, jobject return retval > -1 ? retval : -(zts_errno); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1close(JNIEnv* env, jobject thisObj, jint fd) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1close(JNIEnv* env, jobject thisObj, jint fd) { return zts_bsd_close(fd); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1select( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1select( JNIEnv* env, jobject thisObj, jint nfds, @@ -264,14 +159,18 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1select( } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1fcntl(JNIEnv* env, jobject thisObj, jint fd, jint cmd, jint flags) +Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1fcntl(JNIEnv* env, jobject thisObj, jint fd, jint cmd, jint flags) { int retval = zts_bsd_fcntl(fd, cmd, flags); return retval > -1 ? retval : -(zts_errno); } -JNIEXPORT int JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1ioctl(JNIEnv* env, jobject thisObj, jint fd, jlong request, jobject argp) +JNIEXPORT int JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1ioctl( + JNIEnv* env, + jobject thisObj, + jint fd, + jlong request, + jobject argp) { int retval = ZTS_ERR_OK; if (request == FIONREAD) { @@ -293,8 +192,12 @@ Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1ioctl(JNIEnv* env, jobject thisOb return retval > -1 ? retval : -(zts_errno); } -JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1send(JNIEnv* env, jobject thisObj, jint fd, jbyteArray buf, int flags) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1send( + JNIEnv* env, + jobject thisObj, + jint fd, + jbyteArray buf, + int flags) { void* data = env->GetPrimitiveArrayCritical(buf, NULL); int retval = zts_bsd_send(fd, data, env->GetArrayLength(buf), flags); @@ -302,7 +205,7 @@ Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1send(JNIEnv* env, jobject thisObj return retval > -1 ? retval : -(zts_errno); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1sendto( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1sendto( JNIEnv* env, jobject thisObj, jint fd, @@ -320,8 +223,12 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1sendto( return retval > -1 ? retval : -(zts_errno); } -JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1recv(JNIEnv* env, jobject thisObj, jint fd, jbyteArray buf, jint flags) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1recv( + JNIEnv* env, + jobject thisObj, + jint fd, + jbyteArray buf, + jint flags) { void* data = env->GetPrimitiveArrayCritical(buf, NULL); int retval = zts_bsd_recv(fd, data, env->GetArrayLength(buf), flags); @@ -329,7 +236,7 @@ Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1recv(JNIEnv* env, jobject thisObj return retval > -1 ? retval : -(zts_errno); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1recvfrom( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1recvfrom( JNIEnv* env, jobject thisObj, jint fd, @@ -347,7 +254,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1recvfrom( } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1read(JNIEnv* env, jobject thisObj, jint fd, jbyteArray buf) +Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1read(JNIEnv* env, jobject thisObj, jint fd, jbyteArray buf) { void* data = env->GetPrimitiveArrayCritical(buf, NULL); int retval = zts_bsd_read(fd, data, env->GetArrayLength(buf)); @@ -361,7 +268,7 @@ ssize_t zts_bsd_read_offset(int fd, void* buf, size_t offset, size_t len) return zts_bsd_read(fd, &(cbuf[offset]), len); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1read_1offset( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1read_1offset( JNIEnv* env, jobject thisObj, jint fd, @@ -375,7 +282,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1read_1offs return retval > -1 ? retval : -(zts_errno); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1read_1length( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1read_1length( JNIEnv* env, jobject thisObj, jint fd, @@ -389,7 +296,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1read_1leng } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1write__IB(JNIEnv* env, jobject thisObj, jint fd, jbyteArray buf) +Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1write__IB(JNIEnv* env, jobject thisObj, jint fd, jbyteArray buf) { void* data = env->GetPrimitiveArrayCritical(buf, NULL); int retval = zts_bsd_write(fd, data, env->GetArrayLength(buf)); @@ -397,7 +304,7 @@ Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1write__IB(JNIEnv* env, jobject th return retval > -1 ? retval : -(zts_errno); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1write_1offset( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1write_1offset( JNIEnv* env, jobject thisObj, jint fd, @@ -412,14 +319,14 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1write_1off } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1write_1byte(JNIEnv* env, jobject thisObj, jint fd, jbyte buf) +Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1write_1byte(JNIEnv* env, jobject thisObj, jint fd, jbyte buf) { int retval = zts_bsd_write(fd, &buf, 1); return retval > -1 ? retval : -(zts_errno); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1shutdown(JNIEnv* env, jobject thisObj, int fd, int how) +Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1shutdown(JNIEnv* env, jobject thisObj, int fd, int how) { return zts_bsd_shutdown(fd, how); } @@ -541,49 +448,51 @@ void zta2ss(JNIEnv* env, struct zts_sockaddr_storage* ss, jobject addr) } } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1node_1get_1port(JNIEnv* jenv, jobject thisObj) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1node_1get_1port(JNIEnv* jenv, jobject thisObj) { return zts_node_get_port(); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1node_1stop(JNIEnv* jenv, jobject thisObj) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1node_1stop(JNIEnv* jenv, jobject thisObj) { int res = zts_node_stop(); java_detach_from_thread(); return res; } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1node_1free(JNIEnv* jenv, jobject thisObj) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1node_1free(JNIEnv* jenv, jobject thisObj) { int res = zts_node_free(); java_detach_from_thread(); return res; } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1join(JNIEnv* env, jobject thisObj, jlong net_id) +JNIEXPORT jint JNICALL +Java_com_zerotier_sockets_ZeroTierNative_zts_1net_1join(JNIEnv* env, jobject thisObj, jlong net_id) { return zts_net_join((uint64_t)net_id); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1leave(JNIEnv* env, jobject thisObj, jlong net_id) +JNIEXPORT jint JNICALL +Java_com_zerotier_sockets_ZeroTierNative_zts_1net_1leave(JNIEnv* env, jobject thisObj, jlong net_id) { return zts_net_leave((uint64_t)net_id); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1id_1new(JNIEnv* jenv, jobject thisObj, char* key, int* key_buf_len) +Java_com_zerotier_sockets_ZeroTierNative_zts_1id_1new(JNIEnv* jenv, jobject thisObj, char* key, int* key_buf_len) { return ZTS_ERR_OK; } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1id_1pair_1is_1valid(JNIEnv* jenv, jobject thisObj, char* key, int len) +Java_com_zerotier_sockets_ZeroTierNative_zts_1id_1pair_1is_1valid(JNIEnv* jenv, jobject thisObj, char* key, int len) { return ZTS_ERR_OK; } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1from_1storage(JNIEnv* jenv, jobject thisObj, jstring path) +Java_com_zerotier_sockets_ZeroTierNative_zts_1init_1from_1storage(JNIEnv* jenv, jobject thisObj, jstring path) { if (! path) { return ZTS_ERR_ARG; @@ -598,7 +507,7 @@ Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1from_1storage(JNIEnv* jenv, jobj } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1set_1event_1handler(JNIEnv* env, jobject thisObj, jobject callback) +Java_com_zerotier_sockets_ZeroTierNative_zts_1init_1set_1event_1handler(JNIEnv* env, jobject thisObj, jobject callback) { jclass eventListenerClass = env->GetObjectClass(callback); if (eventListenerClass == NULL) { @@ -614,19 +523,22 @@ Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1set_1event_1handler(JNIEnv* env, } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1set_1port(JNIEnv* jenv, jobject thisObj, short port) +Java_com_zerotier_sockets_ZeroTierNative_zts_1init_1set_1port(JNIEnv* jenv, jobject thisObj, short port) { return zts_init_set_port(port); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1from_1memory(JNIEnv* jenv, jobject thisObj, char* key, int len) +Java_com_zerotier_sockets_ZeroTierNative_zts_1init_1from_1memory(JNIEnv* jenv, jobject thisObj, char* key, int len) { return ZTS_ERR_OK; } -JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1blacklist_1if(JNIEnv* jenv, jobject thisObj, jstring prefix, jint len) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1init_1blacklist_1if( + JNIEnv* jenv, + jobject thisObj, + jstring prefix, + jint len) { if (! prefix) { return ZTS_ERR_ARG; @@ -641,70 +553,75 @@ Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1blacklist_1if(JNIEnv* jenv, jobj } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1set_roots(JNIEnv* jenv, jobject thisObj, void* roots_data, jint len) +Java_com_zerotier_sockets_ZeroTierNative_zts_1init_1set_roots(JNIEnv* jenv, jobject thisObj, void* roots_data, jint len) { return ZTS_ERR_OK; } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1allow_1net_1cache(JNIEnv* jenv, jobject thisObj, jint allowed) +Java_com_zerotier_sockets_ZeroTierNative_zts_1init_1allow_1net_1cache(JNIEnv* jenv, jobject thisObj, jint allowed) { return zts_init_allow_net_cache(allowed); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1allow_1peer_1cache(JNIEnv* jenv, jobject thisObj, jint allowed) +Java_com_zerotier_sockets_ZeroTierNative_zts_1init_1allow_1peer_1cache(JNIEnv* jenv, jobject thisObj, jint allowed) { return zts_init_allow_peer_cache(allowed); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1allow_1roots_1cache(JNIEnv* jenv, jobject thisObj, jint allowed) +Java_com_zerotier_sockets_ZeroTierNative_zts_1init_1allow_1roots_1cache(JNIEnv* jenv, jobject thisObj, jint allowed) { return zts_init_allow_roots_cache(allowed); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1init_1allow_1id_1cache(JNIEnv* jenv, jobject thisObj, jint allowed) +Java_com_zerotier_sockets_ZeroTierNative_zts_1init_1allow_1id_1cache(JNIEnv* jenv, jobject thisObj, jint allowed) { return zts_init_allow_id_cache(allowed); } -JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1is_1assigned(JNIEnv* jenv, jobject thisObj, jlong net_id, jint family) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1addr_1is_1assigned( + JNIEnv* jenv, + jobject thisObj, + jlong net_id, + jint family) { return zts_addr_is_assigned(net_id, family); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1get( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1addr_1get( JNIEnv* jenv, jobject thisObj, long net_id, jint family, struct sockaddr_storage* addr) { + // Use Java_com_zerotier_sockets_ZeroTierNative_zts_1addr_1get_1str instead } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1get_1str( - JNIEnv* jenv, - jobject thisObj, - long net_id, - jint family, - char* dst, - jint len) +JNIEXPORT jstring JNICALL +Java_com_zerotier_sockets_ZeroTierNative_zts_1addr_1get_1str(JNIEnv* jenv, jobject thisObj, long net_id, jint family) { + char ip_str[ZTS_IP_MAX_STR_LEN] = { 0 }; + zts_addr_get_str(net_id, family, ip_str, ZTS_IP_MAX_STR_LEN); + jstring result = jenv->NewStringUTF(ip_str); + return result; } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1get_1all( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1addr_1get_1all( JNIEnv* jenv, jobject thisObj, long net_id, struct sockaddr_storage* addr, jint* count) { + /* This feature will be implemented once the lower-level + limitation of one addr per family per network is removed. */ } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1compute_16plane( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1addr_1compute_16plane( JNIEnv* jenv, jobject thisObj, jlong net_id, @@ -713,7 +630,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1compute_1 { } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1compute_1rfc4193( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1addr_1compute_1rfc4193( JNIEnv* jenv, jobject thisObj, jlong net_id, @@ -722,7 +639,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1compute_1 { } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1compute_1rfc4193_1str( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1addr_1compute_1rfc4193_1str( JNIEnv* jenv, jobject thisObj, jlong net_id, @@ -733,7 +650,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1compute_1 return ZTS_ERR_OK; } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1compute_16plane_1str( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1addr_1compute_16plane_1str( JNIEnv* jenv, jobject thisObj, jlong net_id, @@ -744,7 +661,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1addr_1compute_1 return ZTS_ERR_OK; } -JNIEXPORT uint64_t JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1compute_1adhoc_1id( +JNIEXPORT uint64_t JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1net_1compute_1adhoc_1id( JNIEnv* jenv, jobject thisObj, short start_port, @@ -754,40 +671,39 @@ JNIEXPORT uint64_t JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1comput } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1transport_1is_1ready(JNIEnv* jenv, jobject thisObj, jlong net_id) +Java_com_zerotier_sockets_ZeroTierNative_zts_1net_1transport_1is_1ready(JNIEnv* jenv, jobject thisObj, jlong net_id) { return zts_net_transport_is_ready(net_id); } JNIEXPORT uint64_t JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1net_get_mac(JNIEnv* jenv, jobject thisObj, jlong net_id) +Java_com_zerotier_sockets_ZeroTierNative_zts_1net_get_mac(JNIEnv* jenv, jobject thisObj, jlong net_id) { return zts_net_get_mac(net_id); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1get_1mac_1str( - JNIEnv* jenv, - jobject thisObj, - jlong net_id, - jstring dst, - jint len) +JNIEXPORT jstring JNICALL +Java_com_zerotier_sockets_ZeroTierNative_zts_1net_1get_1mac_1str(JNIEnv* jenv, jobject thisObj, jlong net_id) { - return ZTS_ERR_OK; + char mac_str[ZTS_MAC_ADDRSTRLEN] = { 0 }; + zts_net_get_mac_str(net_id, mac_str, ZTS_MAC_ADDRSTRLEN); + jstring result = jenv->NewStringUTF(mac_str); + return result; } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1get_1broadcast(JNIEnv* jenv, jobject thisObj, jlong net_id) +Java_com_zerotier_sockets_ZeroTierNative_zts_1net_1get_1broadcast(JNIEnv* jenv, jobject thisObj, jlong net_id) { return zts_net_get_broadcast(net_id); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1get_1mtu(JNIEnv* jenv, jobject thisObj, jlong net_id) +Java_com_zerotier_sockets_ZeroTierNative_zts_1net_1get_1mtu(JNIEnv* jenv, jobject thisObj, jlong net_id) { return zts_net_get_mtu(net_id); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1get_1name( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1net_1get_1name( JNIEnv* jenv, jobject thisObj, jlong net_id, @@ -798,39 +714,42 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1get_1name( } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1get_1status(JNIEnv* jenv, jobject thisObj, jlong net_id) +Java_com_zerotier_sockets_ZeroTierNative_zts_1net_1get_1status(JNIEnv* jenv, jobject thisObj, jlong net_id) { return zts_net_get_status(net_id); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1net_1get_1type(JNIEnv* jenv, jobject thisObj, jlong net_id) +Java_com_zerotier_sockets_ZeroTierNative_zts_1net_1get_1type(JNIEnv* jenv, jobject thisObj, jlong net_id) { return zts_net_get_type(net_id); } -JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1route_1is_1assigned(JNIEnv* jenv, jobject thisObj, jlong net_id, jint family) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1route_1is_1assigned( + JNIEnv* jenv, + jobject thisObj, + jlong net_id, + jint family) { return zts_route_is_assigned(net_id, family); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1node_1start(JNIEnv* jenv, jobject thisObj) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1node_1start(JNIEnv* jenv, jobject thisObj) { return zts_node_start(); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1node_1is_1online(JNIEnv* jenv, jobject thisObj) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1node_1is_1online(JNIEnv* jenv, jobject thisObj) { return zts_node_is_online(); } -JNIEXPORT uint64_t JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1node_1get_1id(JNIEnv* jenv, jobject thisObj) +JNIEXPORT uint64_t JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1node_1get_1id(JNIEnv* jenv, jobject thisObj) { return zts_node_get_id(); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1node_1get_1id_1pair( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1node_1get_1id_1pair( JNIEnv* jenv, jobject thisObj, char* key, @@ -839,7 +758,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1node_1get_1id_1 return ZTS_ERR_OK; } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1moon_1orbit( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1moon_1orbit( JNIEnv* jenv, jobject thisObj, jlong moon_roots_id, @@ -849,12 +768,12 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1moon_1orbit( } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1moon_1deorbit(JNIEnv* jenv, jobject thisObj, jlong moon_roots_id) +Java_com_zerotier_sockets_ZeroTierNative_zts_1moon_1deorbit(JNIEnv* jenv, jobject thisObj, jlong moon_roots_id) { return zts_moon_deorbit(moon_roots_id); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1connect( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1connect( JNIEnv* jenv, jobject thisObj, jint fd, @@ -875,7 +794,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1connect( } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1bind(JNIEnv* jenv, jobject thisObj, jint fd, jstring ipstr, jint port) +Java_com_zerotier_sockets_ZeroTierNative_zts_1bind(JNIEnv* jenv, jobject thisObj, jint fd, jstring ipstr, jint port) { if (! ipstr) { return ZTS_ERR_ARG; @@ -889,7 +808,7 @@ Java_com_zerotier_sdk_ZeroTierNative_zts_1bind(JNIEnv* jenv, jobject thisObj, ji return retval; } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1accept( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1accept( JNIEnv* jenv, jobject thisObj, int fd, @@ -897,81 +816,69 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1accept( jint len, jint* port) { - /* - if (! remote_addr) { - return ZTS_ERR_ARG; - } - const char* utf_string = jenv->GetStringUTFChars(remote_addr, NULL); - if (! utf_string) { - return ZTS_ERR_GENERAL; - } - int retval = zts_bind(fd, utf_string, port); - jenv->ReleaseStringUTFChars(ipstr, utf_string); - return retval; - - - jstr = (*env)->NewStringUTF(env, greeting); - - return jstr; - - */ + // Use Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1accept instead return ZTS_ERR_OK; } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1udp_1client(JNIEnv* jenv, jobject thisObj, jstring remote_ipstr) +Java_com_zerotier_sockets_ZeroTierNative_zts_1udp_1client(JNIEnv* jenv, jobject thisObj, jstring remote_ipstr) { return ZTS_ERR_OK; } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1no_1delay(JNIEnv* jenv, jobject thisObj, jint fd, jint enabled) +Java_com_zerotier_sockets_ZeroTierNative_zts_1set_1no_1delay(JNIEnv* jenv, jobject thisObj, jint fd, jint enabled) { return zts_set_no_delay(fd, enabled); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1no_1delay(JNIEnv* jenv, jobject thisObj, jint fd) +JNIEXPORT jint JNICALL +Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1no_1delay(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_no_delay(fd); } -JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1linger(JNIEnv* jenv, jobject thisObj, jint fd, jint enabled, jint value) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1set_1linger( + JNIEnv* jenv, + jobject thisObj, + jint fd, + jint enabled, + jint value) { return zts_set_linger(fd, enabled, value); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1linger_1enabled(JNIEnv* jenv, jobject thisObj, jint fd) +Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1linger_1enabled(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_linger_enabled(fd); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1linger_1value(JNIEnv* jenv, jobject thisObj, jint fd) +Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1linger_1value(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_linger_value(fd); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1pending_1data_1size(JNIEnv* jenv, jobject thisObj, jint fd) +Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1pending_1data_1size(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_pending_data_size(fd); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1reuse_1addr(JNIEnv* jenv, jobject thisObj, jint fd, jint enabled) +Java_com_zerotier_sockets_ZeroTierNative_zts_1set_1reuse_1addr(JNIEnv* jenv, jobject thisObj, jint fd, jint enabled) { return zts_set_reuse_addr(fd, enabled); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1reuse_1addr(JNIEnv* jenv, jobject thisObj, jint fd) +Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1reuse_1addr(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_reuse_addr(fd); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1recv_1timeout( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1set_1recv_1timeout( JNIEnv* jenv, jobject thisObj, jint fd, @@ -982,12 +889,12 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1recv_1time } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1recv_1timeout(JNIEnv* jenv, jobject thisObj, jint fd) +Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1recv_1timeout(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_recv_timeout(fd); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1send_1timeout( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1set_1send_1timeout( JNIEnv* jenv, jobject thisObj, jint fd, @@ -998,76 +905,81 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1send_1time } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1send_1timeout(JNIEnv* jenv, jobject thisObj, jint fd) +Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1send_1timeout(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_send_timeout(fd); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1send_1buf_1size(JNIEnv* jenv, jobject thisObj, jint fd, jint size) +Java_com_zerotier_sockets_ZeroTierNative_zts_1set_1send_1buf_1size(JNIEnv* jenv, jobject thisObj, jint fd, jint size) { return zts_set_send_buf_size(fd, size); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1send_1buf_1size(JNIEnv* jenv, jobject thisObj, jint fd) +Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1send_1buf_1size(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_send_buf_size(fd); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1recv_1buf_1size(JNIEnv* jenv, jobject thisObj, jint fd, jint size) +Java_com_zerotier_sockets_ZeroTierNative_zts_1set_1recv_1buf_1size(JNIEnv* jenv, jobject thisObj, jint fd, jint size) { return zts_set_recv_buf_size(fd, size); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1recv_1buf_1size(JNIEnv* jenv, jobject thisObj, jint fd) +Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1recv_1buf_1size(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_recv_buf_size(fd); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1ttl(JNIEnv* jenv, jobject thisObj, jint fd, jint ttl) +Java_com_zerotier_sockets_ZeroTierNative_zts_1set_1ttl(JNIEnv* jenv, jobject thisObj, jint fd, jint ttl) { return zts_set_ttl(fd, ttl); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1ttl(JNIEnv* jenv, jobject thisObj, jint fd) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1ttl(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_ttl(fd); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1blocking(JNIEnv* jenv, jobject thisObj, jint fd, jint enabled) +Java_com_zerotier_sockets_ZeroTierNative_zts_1set_1blocking(JNIEnv* jenv, jobject thisObj, jint fd, jint enabled) { return zts_set_blocking(fd, enabled); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1blocking(JNIEnv* jenv, jobject thisObj, jint fd) +JNIEXPORT jint JNICALL +Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1blocking(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_blocking(fd); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1set_1keepalive(JNIEnv* jenv, jobject thisObj, jint fd, jint enabled) +Java_com_zerotier_sockets_ZeroTierNative_zts_1set_1keepalive(JNIEnv* jenv, jobject thisObj, jint fd, jint enabled) { return zts_set_keepalive(fd, enabled); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1get_1keepalive(JNIEnv* jenv, jobject thisObj, jint fd) +JNIEXPORT jint JNICALL +Java_com_zerotier_sockets_ZeroTierNative_zts_1get_1keepalive(JNIEnv* jenv, jobject thisObj, jint fd) { return zts_get_keepalive(fd); } struct hostent* -Java_com_zerotier_sdk_ZeroTierNative_zts_1bsd_1gethostbyname(JNIEnv* jenv, jobject thisObj, jstring name) +Java_com_zerotier_sockets_ZeroTierNative_zts_1bsd_1gethostbyname(JNIEnv* jenv, jobject thisObj, jstring name) { return NULL; } -JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1dns_1set_1server(JNIEnv* jenv, jobject thisObj, uint8_t index, ip_addr* addr) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1dns_1set_1server( + JNIEnv* jenv, + jobject thisObj, + uint8_t index, + ip_addr* addr) { return ZTS_ERR_OK; } @@ -1077,23 +989,23 @@ JNIEXPORT ip_addr* JNICALL dns_1get_1server(JNIEnv* jenv, jobject thisObj, uint8 return NULL; } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1lock_1obtain(JNIEnv* jenv, jobject thisObj) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1core_1lock_1obtain(JNIEnv* jenv, jobject thisObj) { return zts_core_lock_obtain(); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1lock_1release(JNIEnv* jenv, jobject thisObj) +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1core_1lock_1release(JNIEnv* jenv, jobject thisObj) { return zts_core_lock_release(); } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1addr_1count(JNIEnv* jenv, jobject thisObj, jlong net_id) +Java_com_zerotier_sockets_ZeroTierNative_zts_1core_1query_1addr_1count(JNIEnv* jenv, jobject thisObj, jlong net_id) { return zts_core_query_addr_count(net_id); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1addr( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1core_1query_1addr( JNIEnv* jenv, jobject thisObj, jlong net_id, @@ -1105,12 +1017,12 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1ad } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1route_1count(JNIEnv* jenv, jobject thisObj, jlong net_id) +Java_com_zerotier_sockets_ZeroTierNative_zts_1core_1query_1route_1count(JNIEnv* jenv, jobject thisObj, jlong net_id) { return zts_core_query_route_count(net_id); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1route( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1core_1query_1route( JNIEnv* jenv, jobject thisObj, jlong net_id, @@ -1125,12 +1037,12 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1ro } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1path_1count(JNIEnv* jenv, jobject thisObj, jlong peer_id) +Java_com_zerotier_sockets_ZeroTierNative_zts_1core_1query_1path_1count(JNIEnv* jenv, jobject thisObj, jlong peer_id) { return zts_core_query_path_count(peer_id); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1path( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1core_1query_1path( JNIEnv* jenv, jobject thisObj, jlong peer_id, @@ -1142,12 +1054,12 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1pa } JNIEXPORT jint JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1mc_1count(JNIEnv* jenv, jobject thisObj, jlong net_id) +Java_com_zerotier_sockets_ZeroTierNative_zts_1core_1query_1mc_1count(JNIEnv* jenv, jobject thisObj, jlong net_id) { return zts_core_query_mc_count(net_id); } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1mc( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1core_1query_1mc( JNIEnv* jenv, jobject thisObj, long net_id, @@ -1158,7 +1070,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1core_1query_1mc return ZTS_ERR_OK; } -JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1util_1roots_1new( +JNIEXPORT jint JNICALL Java_com_zerotier_sockets_ZeroTierNative_zts_1util_1roots_1new( JNIEnv* jenv, jobject thisObj, char* roots_out, @@ -1175,7 +1087,7 @@ JNIEXPORT jint JNICALL Java_com_zerotier_sdk_ZeroTierNative_zts_1util_1roots_1ne } JNIEXPORT void JNICALL -Java_com_zerotier_sdk_ZeroTierNative_zts_1util_1delay(JNIEnv* jenv, jobject thisObj, jlong milliseconds) +Java_com_zerotier_sockets_ZeroTierNative_zts_1util_1delay(JNIEnv* jenv, jobject thisObj, jlong milliseconds) { zts_util_delay(milliseconds); } diff --git a/src/bindings/java/ZeroTierDatagramSocket.java b/src/bindings/java/ZeroTierDatagramSocket.java new file mode 100644 index 0000000..a37608d --- /dev/null +++ b/src/bindings/java/ZeroTierDatagramSocket.java @@ -0,0 +1,346 @@ +/* + * Copyright (c)2013-2021 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2026-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +package com.zerotier.sockets; + +import com.zerotier.sockets.*; +import java.io.*; +import java.net.*; + +/** + * Implements Socket-like behavior over ZeroTier + * + * @author ZeroTier, Inc. + */ +public class ZeroTierDatagramSocket { + ZeroTierSocket _socket; + + /** + * Create a ZeroTierDatagramSocket and bind it to any available port + */ + public ZeroTierDatagramSocket() throws IOException + { + _socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_DGRAM, 0); + _socket.bind("0.0.0.0", 0); + } + + /** + * Create a ZeroTierDatagramSocket bound to the given port + */ + public ZeroTierDatagramSocket(int localPort) throws IOException + { + _socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_DGRAM, 0); + _socket.bind("0.0.0.0", localPort); + } + + /** + * Create a ZeroTierDatagramSocket bound to the given port and local address + */ + public ZeroTierDatagramSocket(int localPort, InetAddress localAddr) throws IOException + { + _socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_DGRAM, 0); + _socket.bind(localAddr.getHostAddress(), localPort); + } + + /** + * Create a ZeroTierDatagramSocket bound to the given port and local address + */ + public ZeroTierDatagramSocket(String localAddr, int localPort) throws IOException + { + _socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_DGRAM, 0); + _socket.bind(localAddr, localPort); + } + + /** + * Create a ZeroTierDatagramSocket bound to the given endpoint + */ + public ZeroTierDatagramSocket(SocketAddress localAddr) throws IOException + { + _socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_DGRAM, 0); + int localPort = ((InetSocketAddress)localAddr).getPort(); + _socket.bind(((InetSocketAddress)localAddr).getHostString(), localPort); + } + + /** + * Connect to a remote host + * @param remoteAddr Remote address to which this socket should connect + * @param remotePort Remote port to which this socket should connect + * + * @exception IOException when an I/O error occurs + */ + public void connect(InetAddress remoteAddr, int remotePort) throws IOException + { + _socket.connect(remoteAddr, remotePort); + } + + /** + * Connect to a remote host + * @param remoteAddr Remote address to which this socket should connect + * @param remotePort Remote port to which this socket should connect + * + * @exception IOException when an I/O error occurs + */ + public void connect(String remoteAddr, int remotePort) throws IOException + { + InetAddress remoteInetAddr = InetAddress.getByName(remoteAddr); + connect(remoteInetAddr, remotePort); + } + + /** + * Connect to a remote host + * @param remoteAddr Remote address to which this socket should connect + * + * @exception IOException when an I/O error occurs + */ + public void connect(SocketAddress remoteAddr) throws IOException + { + int remotePort = ((InetSocketAddress)remoteAddr).getPort(); + connect(((InetSocketAddress)remoteAddr).getHostString(), remotePort); + } + + /** + * Bind to a local address + * @param localAddr Local address to which this socket should bind + * @param localPort Local port to which this socket should bind + * + * @exception IOException when an I/O error occurs + */ + public void bind(InetAddress localAddr, int localPort) throws IOException + { + _socket.bind(localAddr, localPort); + } + + /** + * Bind to a local address + * @param localAddr Local address to which this socket should bind + * @param localPort Local port to which this socket should bind + * + * @exception IOException when an I/O error occurs + */ + public void bind(String localAddr, int localPort) throws IOException + { + InetAddress localInetAddr = InetAddress.getByName(localAddr); + bind(localInetAddr, localPort); + } + + /** + * Send a DatagramPacket to a remote host + * @param packet The packet to send + * + * @exception IOException when an I/O error occurs + */ + public void send(DatagramPacket packet) throws IOException + { + int bytesWritten = ZeroTierNative.zts_bsd_write_offset( + _socket.getNativeFileDescriptor(), + packet.getData(), + 0, + packet.getLength()); + if (bytesWritten < 0) { + throw new IOException("send(DatagramPacket), errno=" + bytesWritten); + } + } + + /** + * Receive a DatagramPacket from a remote host + * @param packet The packet received + * + * @exception IOException when an I/O error occurs + */ + public void receive(DatagramPacket packet) throws IOException + { + int bytesRead = ZeroTierNative.zts_bsd_read_offset( + _socket.getNativeFileDescriptor(), + packet.getData(), + 0, + packet.getLength()); + if ((bytesRead <= 0) | (bytesRead == -104) /* EINTR, from SO_RCVTIMEO */) { + throw new IOException("read​(DatagramPacket), errno=" + bytesRead); + } + } + + /** + * Close the ZeroTierSocket. + * + * @exception IOException when an I/O error occurs + */ + public void close() throws IOException + { + _socket.close(); + } + + /** + * Get the local port to which this ZeroTierSocket is bound + * @return Local port + */ + public int getLocalPort() + { + return _socket.getLocalPort(); + } + + /** + * Get the local address to which this ZeroTierSocket is bound + * @return Local address + */ + public InetAddress getLocalAddress() + { + return _socket.getLocalAddress(); + } + + /** + * Get the remote port to which this ZeroTierSocket is connected + * @return Remote port + */ + public int getRemotePort() + { + return _socket.getRemotePort(); + } + + /** + * Get the remote address to which this ZeroTierSocket is connected + * @return Remote address + */ + public InetAddress getRemoteAddress() + { + return _socket.getRemoteAddress(); + } + + /** + * Get the remote address to which this ZeroTierSocket is connected. Same as getRemoteAddress() + * @return Remote address + */ + public InetAddress getInetAddress() + { + return _socket.getInetAddress(); + } + + /** + * Get the local endpoint address to which this socket is bound to + * @return Local endpoint address + */ + public SocketAddress getLocalSocketAddress() + { + return _socket.getLocalSocketAddress(); + } + + /** + * Return the size of the receive buffer for the ZeroTierSocket's ZeroTierInputStream (SO_RCVBUF) + * @return Size of the receive buffer + * @exception SocketException when an error occurs in the native socket layer + */ + public int getReceiveBufferSize() throws SocketException + { + return _socket.getReceiveBufferSize(); + } + + /** + * Return the size of the send buffer for the ZeroTierSocket's ZeroTierOutputStream (SO_SNDBUF) + * @return Size of the send buffer + * @exception SocketException when an error occurs in the native socket layer + */ + public int getSendBufferSize() throws SocketException + { + return _socket.getSendBufferSize(); + } + + /** + * Return whether address reuse is enabled on this ZeroTierSocket (SO_REUSEADDR) + * @return true or false + * @exception SocketException when an error occurs in the native socket layer + */ + public boolean getReuseAddress() throws SocketException + { + return _socket.getReuseAddress(); + } + + /** + * Get the ZeroTierSocket's timeout value (SO_RCVTIMEO) + * @return Nothing. + * @exception SocketException when an error occurs in the native socket layer + */ + public int getSoTimeout() throws SocketException + { + return _socket.getSoTimeout(); + } + + /** + * Return whether this ZeroTierSocket is bound to a local address + * @return true or false + */ + public boolean isBound​() + { + return _socket.isBound​(); + } + + /** + * Return whether this ZeroTierSocket has been closed + * @return true or false + */ + public boolean isClosed​() + { + return _socket.isClosed(); + } + + /** + * Return whether this ZeroTierSocket is connected to a remote address + * @return true or false + */ + public boolean isConnected​() + { + return _socket.isConnected(); + } + + /** + * Set the size of the receive buffer for the ZeroTierSocket's ZeroTierInputStream. + * @param bufferSize Size of receive buffer + * + * @exception SocketException when an error occurs in the native socket layer + */ + public void setReceiveBufferSize(int bufferSize) throws SocketException + { + _socket.setReceiveBufferSize(bufferSize); + } + + /** + * Enable or disable the re-use of addresses (SO_REUSEADDR) + * @param enabled Whether SO_REUSEADDR is enabled + * + * @exception SocketException when an error occurs in the native socket layer + */ + public void setReuseAddress(boolean enabled) throws SocketException + { + _socket.setReuseAddress(enabled); + } + + /** + * Set the size of the send buffer for the ZeroTierSocket's ZeroTierOutputStream (SO_SNDBUF) + * @param bufferSize Size of send buffer + * + * @exception SocketException when an error occurs in the native socket layer + */ + public void setSendBufferSize(int bufferSize) throws SocketException + { + _socket.setSendBufferSize(bufferSize); + } + + /** + * Set the timeout value for SO_RCVTIMEO + * @param timeout Socket receive timeout value. + * + * @exception SocketException when an error occurs in the native socket layer + */ + public void setSoTimeout(int timeout) throws SocketException + { + _socket.setSoTimeout(timeout); + } +} diff --git a/src/bindings/java/ZeroTierEventListener.java b/src/bindings/java/ZeroTierEventListener.java index 6e8ef10..0154b7e 100644 --- a/src/bindings/java/ZeroTierEventListener.java +++ b/src/bindings/java/ZeroTierEventListener.java @@ -11,14 +11,14 @@ */ /****/ -package com.zerotier.sdk; +package com.zerotier.sockets; /** * Class that must be implemented to receive ZeroTier event notifications. */ -interface ZeroTierEventListener { +public interface ZeroTierEventListener { /* * Called when an even occurs within ZeroTier */ - public void onZeroTierEvent(long nwid, int event_code); + public void onZeroTierEvent(long id, int eventCode); } diff --git a/src/bindings/java/ZeroTierFileDescriptorSet.java b/src/bindings/java/ZeroTierFileDescriptorSet.java index fa22cfd..24f9688 100644 --- a/src/bindings/java/ZeroTierFileDescriptorSet.java +++ b/src/bindings/java/ZeroTierFileDescriptorSet.java @@ -11,7 +11,7 @@ */ /****/ -package com.zerotier.sdk; +package com.zerotier.sockets; /** * This class provides an fdset-like behavior to ZeroTierSocket diff --git a/src/bindings/java/ZeroTierInputStream.java b/src/bindings/java/ZeroTierInputStream.java index ac6928a..8d23d88 100644 --- a/src/bindings/java/ZeroTierInputStream.java +++ b/src/bindings/java/ZeroTierInputStream.java @@ -11,9 +11,9 @@ */ /****/ -package com.zerotier.sdk; +package com.zerotier.sockets; -import com.zerotier.sdk.ZeroTierNative; +import com.zerotier.sockets.ZeroTierNative; import java.io.*; import java.util.Objects; diff --git a/src/bindings/java/ZeroTierNative.java b/src/bindings/java/ZeroTierNative.java index e3dd489..0300e1d 100644 --- a/src/bindings/java/ZeroTierNative.java +++ b/src/bindings/java/ZeroTierNative.java @@ -11,7 +11,7 @@ */ /****/ -package com.zerotier.sdk; +package com.zerotier.sockets; /** * Class that exposes the low-level C socket interface provided by libzt. This @@ -251,10 +251,6 @@ public class ZeroTierNative { public static int ZTS_TCP_KEEPINTVL = 0x00000004; public static int ZTS_TCP_KEEPCNT = 0x00000005; - //----------------------------------------------------------------------------// - // ZeroTier Service Controls // - //----------------------------------------------------------------------------// - //----------------------------------------------------------------------------// // Error codes // //----------------------------------------------------------------------------// @@ -426,11 +422,9 @@ public class ZeroTierNative { // public static native int zts_id_new(char* key, int* key_buf_len); // public static native int zts_id_pair_is_valid(/*const*/ char* key, int len); - public static native int zts_init_from_storage(String path); public static native int zts_init_set_event_handler(ZeroTierEventListener callbackClass); public static native int zts_init_set_port(short port); - // public static native int zts_init_from_memory(/*const*/ char* key, int len); public static native int zts_init_blacklist_if(/*const*/ String prefix, int len); // public static native int zts_init_set_roots(/*const*/ void* roots_data, int len); @@ -439,70 +433,37 @@ public class ZeroTierNative { public static native int zts_init_allow_roots_cache(int allowed); public static native int zts_init_allow_id_cache(int allowed); public static native int zts_addr_is_assigned(long net_id, int family); - // public static native int zts_addr_get(long net_id, int family, struct sockaddr_storage* addr); - // public static native int zts_addr_get_str(long net_id, int family, char* dst, int len); + public static native String zts_addr_get_str(long net_id, int family); // public static native int zts_addr_get_all(long net_id, struct sockaddr_storage* addr, int* count); // public static native int zts_addr_compute_6plane(/*const*/ long net_id, /*const*/ long node_id, struct // sockaddr_storage* addr); public static native int zts_addr_compute_rfc4193(/*const*/ long net_id, /*const*/ long // node_id, struct sockaddr_storage* addr); public static native int zts_addr_compute_rfc4193_str(long net_id, long node_id, String dst, int len); public static native int zts_addr_compute_6plane_str(long net_id, long node_id, String dst, int len); - public static native long zts_net_compute_adhoc_id(short start_port, short end_port); public static native int zts_net_join(long net_id); public static native int zts_net_leave(long net_id); public static native int zts_net_transport_is_ready(/*const*/ long net_id); public static native long zts_net_get_mac(long net_id); - public static native int zts_net_get_mac_str(long net_id, String dst, int len); + public static native String zts_net_get_mac_str(long net_id); public static native int zts_net_get_broadcast(long net_id); public static native int zts_net_get_mtu(long net_id); public static native int zts_net_get_name(long net_id, String dst, int len); public static native int zts_net_get_status(long net_id); public static native int zts_net_get_type(long net_id); - public static native int zts_route_is_assigned(long net_id, int family); - public static native int zts_node_start(); public static native int zts_node_is_online(); public static native long zts_node_get_id(); - // public static native int zts_node_get_id_pair(char* key, int* key_buf_len); + // public static native int zts_node_get_id_pair(char* key, int* key_buf_len); public static native int zts_node_get_port(); public static native int zts_node_stop(); public static native int zts_node_free(); - public static native int zts_moon_orbit(long moon_roots_id, long moon_seed); public static native int zts_moon_deorbit(long moon_roots_id); - - // public static native int zts_bsd_socket(int family, int type, int protocol); - // public static native int zts_bsd_connect(int fd, /*const*/ struct sockaddr* addr, socklen_t addrlen); - // public static native int zts_bsd_bind(int fd, /*const*/ struct sockaddr* addr, socklen_t addrlen); - // public static native int zts_bsd_listen(int fd, int backlog); - // public static native int zts_bsd_accept(int fd, struct sockaddr* addr, socklen_t* addrlen); - // public static native int zts_bsd_setsockopt(int fd, int level, int optname, /*const*/ void* optval, socklen_t - // optlen); public static native int zts_bsd_getsockopt(int fd, int level, int optname, void* optval, socklen_t* - // optlen); public static native int zts_bsd_getsockname(int fd, struct sockaddr* addr, socklen_t* addrlen); public - // static native int zts_bsd_getpeername(int fd, struct sockaddr* addr, socklen_t* addrlen); public static native - // int zts_bsd_close(int fd); public static native int zts_bsd_select(int nfds, fd_set* readfds, fd_set* writefds, - // fd_set* exceptfds, struct timeval* timeout); public static native int zts_bsd_fcntl(int fd, int cmd, int flags); - // public static native int zts_bsd_poll(struct pollfd* fds, nfds_t nfds, int timeout); public static native int - // zts_bsd_ioctl(int fd, long request, void* argp); public static native int send(int fd, /*const*/ void* buf, - // size_t len, int flags); public static native int sendto(int fd, - // /*const*/ void* buf, size_t len, int flags, /*const*/ struct sockaddr* addr, socklen_t addrlen); public static - // native int sendmsg(int fd, /*const*/ struct msghdr* msg, int flags); public static native int recv(int fd, - // void* buf, size_t len, int flags); public static native int recvfrom(int fd, void* buf, size_t len, int flags, - // struct sockaddr* addr, socklen_t* addrlen); public static native int recvmsg(int fd, struct msghdr* msg, int - // flags); public static native int read(int fd, void* buf, size_t len); public static native int readv(int fd, - // /*const*/ struct iovec* iov, int iovcnt); public static native int write(int fd, /*const*/ void* buf, size_t - // len); public static native int writev(int fd, /*const*/ struct iovec* iov, int iovcnt); public static native int - // shutdown(int fd, int how); - public static native int zts_connect(int fd, /*const*/ String ipstr, int port, int timeout_ms); - public static native int zts_bind(int fd, /*const*/ String ipstr, int port); + public static native int zts_connect(int fd, String ipstr, int port, int timeout_ms); + public static native int zts_bind(int fd, String ipstr, int port); // public static native int zts_accept(int fd, String remote_addr, int len, int* port); - public static native int zts_tcp_client(/*const*/ String remote_ipstr, int remote_port); - // public static native int zts_tcp_server(/*const*/ String local_ipstr, int local_port, String remote_ipstr, - // int len, int* remote_port); - public static native int zts_udp_server(/*const*/ String local_ipstr, int local_port); - public static native int zts_udp_client(/*const*/ String remote_ipstr); public static native int zts_set_no_delay(int fd, int enabled); public static native int zts_get_no_delay(int fd); public static native int zts_set_linger(int fd, int enabled, int value); @@ -527,7 +488,7 @@ public class ZeroTierNative { public static native int zts_get_keepalive(int fd); // struct hostent* gethostbyname(/*const*/ String name); // public static native int zts_dns_set_server(uint8_t index, /*const*/ ip_addr* addr); - // ZTS_API /*const*/ ip_addr* ZTCALL dns_get_server(uint8_t index); + // ip_addr* dns_get_server(uint8_t index); public static native int zts_core_lock_obtain(); public static native int zts_core_lock_release(); public static native int zts_core_query_addr_count(long net_id); @@ -560,35 +521,21 @@ public class ZeroTierNative { roots_t* roots_spec); */ public static native void zts_util_delay(long milliseconds); - - ////////////////////////////////////////////////////////////////////////////// - // Socket API // - ////////////////////////////////////////////////////////////////////////////// - public static native int zts_bsd_socket(int family, int type, int protocol); - // public static native int zts_bsd_connect(int fd, ZeroTierSocketAddress addr); - // public static native int zts_bsd_bind(int fd, ZeroTierSocketAddress addr); public static native int zts_bsd_listen(int fd, int backlog); public static native int zts_bsd_accept(int fd, ZeroTierSocketAddress addr); - - // public static native int zts_bsd_setsockopt(int fd, int level, int optname, ZeroTierSocketOptionValue optval); - // public static native int zts_bsd_getsockopt(int fd, int level, int optname, ZeroTierSocketOptionValue optval); - public static native int zts_bsd_read(int fd, byte[] buf); public static native int zts_bsd_read_offset(int fd, byte[] buf, int offset, int len); public static native int zts_bsd_read_length(int fd, byte[] buf, int len); public static native int zts_bsd_recv(int fd, byte[] buf, int flags); public static native int zts_bsd_recvfrom(int fd, byte[] buf, int flags, ZeroTierSocketAddress addr); - public static native int zts_bsd_write(int fd, byte[] buf); public static native int zts_bsd_write_byte(int fd, byte b); public static native int zts_bsd_write_offset(int fd, byte[] buf, int offset, int len); public static native int zts_bsd_sendto(int fd, byte[] buf, int flags, ZeroTierSocketAddress addr); public static native int zts_bsd_send(int fd, byte[] buf, int flags); - public static native int zts_bsd_shutdown(int fd, int how); public static native int zts_bsd_close(int fd); - public static native boolean zts_bsd_getsockname(int fd, ZeroTierSocketAddress addr); public static native int zts_bsd_getpeername(int fd, ZeroTierSocketAddress addr); public static native int zts_bsd_fcntl(int sock, int cmd, int flag); diff --git a/src/bindings/java/ZeroTierNode.java b/src/bindings/java/ZeroTierNode.java index 8fa72d9..b35ccd9 100644 --- a/src/bindings/java/ZeroTierNode.java +++ b/src/bindings/java/ZeroTierNode.java @@ -11,9 +11,11 @@ */ /****/ -package com.zerotier.sdk; +package com.zerotier.sockets; -import com.zerotier.sdk.*; +import com.zerotier.sockets.*; +import java.net.InetAddress; +import java.net.UnknownHostException; /** * Class that provides a control interface for nodes and networks by @@ -191,4 +193,50 @@ public class ZeroTierNode { { return ZeroTierNative.zts_node_get_id(); } + + /** + * Get the first-assigned IPv4 address + * + * @param networkId Network to get assigned address for + * + * @return address + */ + public InetAddress getIPv4Address(long networkId) + { + try { + return InetAddress.getByName(ZeroTierNative.zts_addr_get_str(networkId, ZeroTierNative.ZTS_AF_INET)); + } + catch (Exception e) { + return null; + } + } + + /** + * Get the first-assigned IPv6 address + * + * @param networkId Network to get assigned address for + * + * @return address + */ + public InetAddress getIPv6Address(long networkId) + { + try { + return InetAddress.getByName(ZeroTierNative.zts_addr_get_str(networkId, ZeroTierNative.ZTS_AF_INET6)); + } + catch (Exception e) { + return null; + } + } + + /** + * Get the first-assigned IPv6 address + * + * @param networkId Network to get assigned address for + * + * @return address + */ + public String getMACAddress(long networkId) + { + return ZeroTierNative.zts_net_get_mac_str(networkId); + } } diff --git a/src/bindings/java/ZeroTierOutputStream.java b/src/bindings/java/ZeroTierOutputStream.java index a1127de..9ce2203 100644 --- a/src/bindings/java/ZeroTierOutputStream.java +++ b/src/bindings/java/ZeroTierOutputStream.java @@ -11,9 +11,9 @@ */ /****/ -package com.zerotier.sdk; +package com.zerotier.sockets; -import com.zerotier.sdk.*; +import com.zerotier.sockets.*; import java.io.*; import java.util.Arrays; import java.util.Objects; @@ -23,7 +23,7 @@ import java.util.Objects; */ public class ZeroTierOutputStream extends OutputStream { /** - * File descriptor used by lower native layer + * File descriptor used by lower native layer. No touch! */ public int zfd = -1; diff --git a/src/bindings/java/ZeroTierPeerDetails.java b/src/bindings/java/ZeroTierPeerDetails.java index fd83f77..c9e6324 100644 --- a/src/bindings/java/ZeroTierPeerDetails.java +++ b/src/bindings/java/ZeroTierPeerDetails.java @@ -11,9 +11,9 @@ */ /****/ -package com.zerotier.sdk; +package com.zerotier.sockets; -import com.zerotier.sdk.*; +import com.zerotier.sockets.*; /** * This class encapsulates details about a Peer on a ZeroTier network diff --git a/src/bindings/java/ZeroTierServerSocket.java b/src/bindings/java/ZeroTierServerSocket.java new file mode 100644 index 0000000..5d96acf --- /dev/null +++ b/src/bindings/java/ZeroTierServerSocket.java @@ -0,0 +1,214 @@ +/* + * Copyright (c)2013-2021 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2026-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +package com.zerotier.sockets; + +import com.zerotier.sockets.*; +import java.io.*; +import java.net.*; + +public class ZeroTierServerSocket { + private ZeroTierSocket _socket; + + /** + * Create an unbound ZeroTierServerSocket + */ + public ZeroTierServerSocket() throws IOException + { + _socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET6, ZeroTierNative.ZTS_SOCK_STREAM, 0); + } + + /** + * Create a ZeroTierServerSocket bound to the given port + */ + public ZeroTierServerSocket(int localPort) throws IOException + { + _socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_STREAM, 0); + _socket.bind("0.0.0.0", localPort); + _socket.listen(0); + } + + /** + * Create a ZeroTierServerSocket bound to the given port with a backlog + */ + public ZeroTierServerSocket(int localPort, int backlog) throws IOException + { + _socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET6, ZeroTierNative.ZTS_SOCK_STREAM, 0); + _socket.bind("::", localPort); + _socket.listen(backlog); + } + + /** + * Create a ZeroTierServerSocket bound to the given port and local address + */ + public ZeroTierServerSocket(int localPort, int backlog, InetAddress localAddr) throws IOException + { + _socket = new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET6, ZeroTierNative.ZTS_SOCK_STREAM, 0); + _socket.bind(localAddr.getHostAddress(), localPort); + _socket.listen(backlog); + } + + /** + * Accept incoming connections on this ZeroTierSocket + * @return New ZeroTierSocket representing the accepted connection + * @exception IOException when an I/O error occurs + */ + public ZeroTierSocket accept() throws IOException + { + return _socket.accept(); + } + + /** + * Bind to a local address + * @param localAddr Local address to which this socket should bind + * @param localPort Local port to which this socket should bind + * + * @exception IOException when an I/O error occurs + */ + public void bind(SocketAddress localAddr) throws IOException + { + InetSocketAddress inetAddr = (InetSocketAddress)localAddr; + _socket.bind(inetAddr.getHostName(), inetAddr.getPort()); + } + + /** + * Bind to a local address + * @param localAddr Local address to which this socket should bind + * @param localPort Local port to which this socket should bind + * + * @exception IOException when an I/O error occurs + */ + public void bind(SocketAddress localAddr, int backlog) throws IOException + { + InetSocketAddress inetAddr = (InetSocketAddress)localAddr; + _socket.bind(inetAddr.getHostName(), inetAddr.getPort()); + } + + /** + * Close the ZeroTierSocket. + * + * @exception IOException when an I/O error occurs + */ + public void close() throws IOException + { + _socket.close(); + } + + /** + * Get the remote address to which this ZeroTierSocket is bound + * @return Remote address + */ + public InetAddress getInetAddress() + { + return _socket.getLocalAddress(); + } + + /** + * Get the local port to which this ZeroTierSocket is bound + * @return Local port + */ + public int getLocalPort() + { + return _socket.getLocalPort(); + } + + /** + * Get the local address to which this ZeroTierSocket is bound + * @return Local address + */ + public SocketAddress getLocalSocketAddress() + { + return _socket.getLocalSocketAddress(); + } + + /** + * Return the size of the receive buffer for the ZeroTierSocket's ZeroTierInputStream (SO_RCVBUF) + * @return Size of the receive buffer + * @exception SocketException when an error occurs in the native socket layer + */ + public int getReceiveBufferSize() throws IOException + { + return _socket.getReceiveBufferSize(); + } + + /** + * Return whether address reuse is enabled on this ZeroTierSocket (SO_REUSEADDR) + * @return true or false + * @exception SocketException when an error occurs in the native socket layer + */ + public boolean getReuseAddress() throws IOException + { + return _socket.getReuseAddress(); + } + + /** + * Get the ZeroTierSocket's timeout value (SO_RCVTIMEO) + * @return Nothing. + * @exception SocketException when an error occurs in the native socket layer + */ + public int getSoTimeout() throws IOException + { + return _socket.getSoTimeout(); + } + + /** + * Return whether this ZeroTierSocket is bound to a local address + * @return true or false + */ + public boolean isBound() + { + return _socket.isBound(); + } + + /** + * Return whether this ZeroTierSocket has been closed + * @return true or false + */ + public boolean isClosed() + { + return _socket.isClosed(); + } + + /** + * Set the size of the receive buffer for the ZeroTierSocket's ZeroTierInputStream. + * @param bufferSize Size of receive buffer + * + * @exception SocketException when an error occurs in the native socket layer + */ + public void setReceiveBufferSize(int bufferSize) throws IOException + { + _socket.setReceiveBufferSize(bufferSize); + } + + /** + * Enable or disable the re-use of addresses (SO_REUSEADDR) + * @param enabled Whether SO_REUSEADDR is enabled + * + * @exception SocketException when an error occurs in the native socket layer + */ + public void setReuseAddress(boolean enabled) throws IOException + { + _socket.setReuseAddress(enabled); + } + + /** + * Set the timeout value for SO_RCVTIMEO + * @param timeout Socket receive timeout value. + * + * @exception SocketException when an error occurs in the native socket layer + */ + public void setSoTimeout(int timeout) throws IOException + { + _socket.setSoTimeout(timeout); + } +} diff --git a/src/bindings/java/ZeroTierSocket.java b/src/bindings/java/ZeroTierSocket.java index d8f526b..ffedb3b 100644 --- a/src/bindings/java/ZeroTierSocket.java +++ b/src/bindings/java/ZeroTierSocket.java @@ -11,9 +11,9 @@ */ /****/ -package com.zerotier.sdk; +package com.zerotier.sockets; -import com.zerotier.sdk.*; +import com.zerotier.sockets.*; import java.io.*; import java.net.*; @@ -53,6 +53,11 @@ public class ZeroTierSocket { _outputStream.zfd = fd; } + public int getNativeFileDescriptor() + { + return _zfd; + } + private ZeroTierSocket(int family, int type, int protocol, int zfd) { _family = family; @@ -63,6 +68,28 @@ public class ZeroTierSocket { _isConnected = true; } + public ZeroTierSocket(String remoteAddr, int remotePort) throws IOException + { + _protocol = 0; + _type = ZeroTierNative.ZTS_SOCK_STREAM; + + InetAddress address = InetAddress.getByName(remoteAddr); + if (address instanceof Inet6Address) { + _family = ZeroTierNative.ZTS_AF_INET6; + } + else if (address instanceof Inet4Address) { + _family = ZeroTierNative.ZTS_AF_INET; + } + + _zfd = ZeroTierNative.zts_bsd_socket(_family, _type, _protocol); + setNativeFileDescriptor(_zfd); + int err; + if ((err = ZeroTierNative.zts_connect(_zfd, remoteAddr, remotePort, 0)) < 0) { + throw new IOException("Error while connecting to remote host (" + err + ")"); + } + _isConnected = true; + } + /** * Create a new ZeroTierSocket with the given attributes * @param family The socket family @@ -76,7 +103,7 @@ public class ZeroTierSocket { if (_zfd > -1) { throw new IOException("This socket has already been created (fd=" + _zfd + ")"); } - _zfd = ZeroTierNative.zts_bsd_socket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_STREAM, protocol); + _zfd = ZeroTierNative.zts_bsd_socket(family, type, protocol); if (_zfd < 0) { throw new IOException("Error while creating socket (" + _zfd + ")"); } @@ -265,7 +292,7 @@ public class ZeroTierSocket { } /** - * Get the remote port to which this ZeroTierSocket is bound + * Get the remote port to which this ZeroTierSocket is connected * @return Remote port */ public int getRemotePort() @@ -277,7 +304,7 @@ public class ZeroTierSocket { } /** - * Get the remote address to which this ZeroTierSocket is bound + * Get the remote address to which this ZeroTierSocket is connected * @return Remote address */ public InetAddress getRemoteAddress() @@ -288,6 +315,30 @@ public class ZeroTierSocket { return _remoteAddr; } + /** + * Get the remote address to which this ZeroTierSocket is connected. Same as getRemoteAddress() + * @return Remote address + */ + public InetAddress getInetAddress() + { + if (! _isConnected) { + return null; + } + return _remoteAddr; + } + + /** + * Get the local endpoint address to which this socket is bound to + * @return Local endpoint address + */ + public SocketAddress getLocalSocketAddress() + { + if (! _isConnected) { + return null; + } + return new InetSocketAddress(_remoteAddr, _remotePort); + } + /** * Return the size of the receive buffer for the ZeroTierSocket's ZeroTierInputStream (SO_RCVBUF) * @return Size of the receive buffer @@ -319,7 +370,7 @@ public class ZeroTierSocket { * @return true or false * @exception SocketException when an error occurs in the native socket layer */ - public boolean getReuseAddressEnabled() throws SocketException + public boolean getReuseAddress() throws SocketException { if (_isClosed) { throw new SocketException("Error: ZeroTierSocket is closed"); @@ -585,7 +636,7 @@ public class ZeroTierSocket { * * @exception SocketException when an error occurs in the native socket layer */ - public void setSoTimeoutValue(int timeout) throws SocketException + public void setSoTimeout(int timeout) throws SocketException { if (_isClosed) { throw new SocketException("Error: ZeroTierSocket is closed"); diff --git a/src/bindings/java/ZeroTierSocketAddress.java b/src/bindings/java/ZeroTierSocketAddress.java index 433f219..21a278e 100644 --- a/src/bindings/java/ZeroTierSocketAddress.java +++ b/src/bindings/java/ZeroTierSocketAddress.java @@ -11,13 +11,15 @@ */ /****/ -package com.zerotier.sdk; +package com.zerotier.sockets; -import com.zerotier.sdk.ZeroTierNative; +import com.zerotier.sockets.ZeroTierNative; import java.net.InetAddress; /** * Convenience class for holding address information. Used internally by JNI layer. + * You (as a consumer of this library) should probably not use this as it is non-standard + * and very likely to be removed at some point. */ class ZeroTierSocketAddress { private byte[] _ip6 = new byte[16]; diff --git a/test/selftest.java b/test/selftest.java index ba3d761..25e649e 100644 --- a/test/selftest.java +++ b/test/selftest.java @@ -1,12 +1,14 @@ -import com.zerotier.sdk.*; +import com.zerotier.sockets.*; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.math.BigInteger; +import java.net.InetAddress; +import java.net.DatagramPacket; public class selftest { public static void main(String[] args) { - System.err.println(args.length); if (args.length < 4 || args.length > 5) { System.err.println("Invalid arguments"); System.err.println(" Usage: "); @@ -20,7 +22,9 @@ public class selftest { int port = 0; String mode = args[0]; storagePath = args[1]; - BigInteger networkId = new BigInteger(args[2], 16); + BigInteger nwid = new BigInteger(args[2], 16); + Long networkId = nwid.longValue(); + if (args.length == 4) { port = Integer.parseInt(args[3]); } @@ -29,7 +33,7 @@ public class selftest { port = Integer.parseInt(args[4]); } System.out.println("mode = " + mode); - System.out.println("networkId = " + Long.toHexString(networkId.longValue())); + System.out.println("networkId = " + Long.toHexString(networkId)); System.out.println("storagePath = " + storagePath); System.out.println("remoteAddr = " + remoteAddr); System.out.println("port = " + port); @@ -37,9 +41,8 @@ public class selftest { // ZeroTier setup ZeroTierNode node = new ZeroTierNode(); - node.initFromStorage(storagePath); - // node.initSetEventHandler(new MyZeroTierEventListener()); + node.initSetEventHandler(new MyZeroTierEventListener()); // node.initSetPort(9994); node.start(); @@ -49,29 +52,40 @@ public class selftest { } System.out.println("Node ID: " + Long.toHexString(node.getId())); System.out.println("Joining network..."); - node.join(networkId.longValue()); + node.join(networkId); System.out.println("Waiting for network..."); - while (! node.isNetworkTransportReady(networkId.longValue())) { + while (! node.isNetworkTransportReady(networkId)) { ZeroTierNative.zts_util_delay(50); } - System.out.println("joined"); + System.out.println("Joined"); + + // IPv4 + + InetAddress addr4 = node.getIPv4Address(networkId); + System.out.println("IPv4 address = " + addr4.getHostAddress()); + + // IPv6 + + InetAddress addr6 = node.getIPv6Address(networkId); + System.out.println("IPv6 address = " + addr6.getHostAddress()); + + // MAC address + + System.out.println("MAC address = " + node.getMACAddress(networkId)); // Socket logic if (mode.equals("server")) { System.out.println("Starting server..."); try { - ZeroTierSocket socket = - new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_STREAM, 0); - socket.bind("0.0.0.0", port); - socket.listen(100); - ZeroTierSocket newConnection = socket.accept(); - ZeroTierInputStream inputStream = newConnection.getInputStream(); + ZeroTierServerSocket listener = new ZeroTierServerSocket(port); + ZeroTierSocket conn = listener.accept(); + ZeroTierInputStream inputStream = conn.getInputStream(); DataInputStream dataInputStream = new DataInputStream(inputStream); String message = dataInputStream.readUTF(); System.out.println("recv: " + message); - socket.close(); - newConnection.close(); + listener.close(); + conn.close(); } catch (Exception ex) { System.out.println(ex); @@ -81,9 +95,7 @@ public class selftest { if (mode.equals("client")) { System.out.println("Starting client..."); try { - ZeroTierSocket socket = - new ZeroTierSocket(ZeroTierNative.ZTS_AF_INET, ZeroTierNative.ZTS_SOCK_STREAM, 0); - socket.connect(remoteAddr, port); + ZeroTierSocket socket = new ZeroTierSocket(remoteAddr, port); ZeroTierOutputStream outputStream = socket.getOutputStream(); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); dataOutputStream.writeUTF("Hello from java!"); @@ -99,40 +111,38 @@ public class selftest { /** * (OPTIONAL) event handler */ -/* class MyZeroTierEventListener implements ZeroTierEventListener { - public void onZeroTierEvent(long id, int event_code) + public void onZeroTierEvent(long id, int eventCode) { - if (event_code == ZeroTierNative.ZTS_EVENT_NODE_UP) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_UP) { System.out.println("EVENT_NODE_UP"); } - if (event_code == ZeroTierNative.ZTS_EVENT_NODE_ONLINE) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_ONLINE) { System.out.println("EVENT_NODE_ONLINE: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NODE_OFFLINE) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_OFFLINE) { System.out.println("EVENT_NODE_OFFLINE"); } - if (event_code == ZeroTierNative.ZTS_EVENT_NODE_DOWN) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NODE_DOWN) { System.out.println("EVENT_NODE_DOWN"); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP4) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP4) { System.out.println("ZTS_EVENT_NETWORK_READY_IP4: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP6) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_READY_IP6) { System.out.println("ZTS_EVENT_NETWORK_READY_IP6: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_DOWN) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_DOWN) { System.out.println("EVENT_NETWORK_DOWN: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_OK) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_OK) { System.out.println("EVENT_NETWORK_OK: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_ACCESS_DENIED) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_ACCESS_DENIED) { System.out.println("EVENT_NETWORK_ACCESS_DENIED: " + Long.toHexString(id)); } - if (event_code == ZeroTierNative.ZTS_EVENT_NETWORK_NOT_FOUND) { + if (eventCode == ZeroTierNative.ZTS_EVENT_NETWORK_NOT_FOUND) { System.out.println("EVENT_NETWORK_NOT_FOUND: " + Long.toHexString(id)); } } } -*/