diff --git a/src/bindings/java/com/zerotier/sockets/ZeroTierDatagramSocket.java b/src/bindings/java/com/zerotier/sockets/ZeroTierDatagramSocket.java index 34d44700..8263f689 100644 --- a/src/bindings/java/com/zerotier/sockets/ZeroTierDatagramSocket.java +++ b/src/bindings/java/com/zerotier/sockets/ZeroTierDatagramSocket.java @@ -22,7 +22,7 @@ * * @author ZeroTier, Inc. */ -public class ZeroTierDatagramSocket { +public class ZeroTierDatagramSocket implements Closeable { ZeroTierSocket _socket; /** diff --git a/src/bindings/java/com/zerotier/sockets/ZeroTierServerSocket.java b/src/bindings/java/com/zerotier/sockets/ZeroTierServerSocket.java index 1a3cc576..f74ebc45 100644 --- a/src/bindings/java/com/zerotier/sockets/ZeroTierServerSocket.java +++ b/src/bindings/java/com/zerotier/sockets/ZeroTierServerSocket.java @@ -22,7 +22,7 @@ * * @author ZeroTier, Inc. */ -public class ZeroTierServerSocket { +public class ZeroTierServerSocket implements Closeable { private ZeroTierSocket _socket; /** diff --git a/src/bindings/java/com/zerotier/sockets/ZeroTierSocket.java b/src/bindings/java/com/zerotier/sockets/ZeroTierSocket.java index 7b0fea30..873be17e 100644 --- a/src/bindings/java/com/zerotier/sockets/ZeroTierSocket.java +++ b/src/bindings/java/com/zerotier/sockets/ZeroTierSocket.java @@ -22,7 +22,7 @@ * * @author ZeroTier, Inc. */ -public class ZeroTierSocket { +public class ZeroTierSocket implements Closeable { // File descriptor from lower native layer private int _zfd = -1; private int _family = -1; @@ -58,11 +58,13 @@ public int getNativeFileDescriptor() return _zfd; } - private ZeroTierSocket(int family, int type, int protocol, int zfd) + private ZeroTierSocket(int family, int type, int protocol, int zfd, ZeroTierSocketAddress remoteAddr) { _family = family; _type = type; _protocol = protocol; + _remoteAddr = remoteAddr.toInetAddress(); + _remotePort = remoteAddr.getPort(); setNativeFileDescriptor(zfd); // Since we only call this from accept() we will mark it as connected _isConnected = true; @@ -237,7 +239,7 @@ public ZeroTierSocket accept() throws IOException if ((acceptedFd = ZeroTierNative.zts_bsd_accept(_zfd, addr)) < 0) { throw new IOException("Error while accepting connection (" + acceptedFd + ")"); } - return new ZeroTierSocket(_family, _type, _protocol, acceptedFd); + return new ZeroTierSocket(_family, _type, _protocol, acceptedFd, addr); } /** @@ -632,7 +634,7 @@ public void setSoLinger(boolean enabled, int lingerTime) throws SocketException /** * Set the timeout value for SO_RCVTIMEO - * @param timeout Socket receive timeout value. + * @param timeout Socket receive timeout value in milliseconds. * * @exception SocketException when an error occurs in the native socket layer */ @@ -644,8 +646,7 @@ public void setSoTimeout(int timeout) throws SocketException if (timeout < 0) { throw new IllegalArgumentException("Error: SO_TIMEOUT < 0"); } - // TODO: This is incorrect - if (ZeroTierNative.zts_set_recv_timeout(_zfd, timeout, timeout) != ZeroTierNative.ZTS_ERR_OK) { + if (ZeroTierNative.zts_set_recv_timeout(_zfd, timeout/1000, (timeout % 1000) * 1000) != ZeroTierNative.ZTS_ERR_OK) { throw new SocketException("Error: Could not set SO_RCVTIMEO"); } } diff --git a/src/bindings/java/com/zerotier/sockets/ZeroTierSocketAddress.java b/src/bindings/java/com/zerotier/sockets/ZeroTierSocketAddress.java index 21a278e9..e84dfc87 100644 --- a/src/bindings/java/com/zerotier/sockets/ZeroTierSocketAddress.java +++ b/src/bindings/java/com/zerotier/sockets/ZeroTierSocketAddress.java @@ -21,7 +21,7 @@ * 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 { +public class ZeroTierSocketAddress { private byte[] _ip6 = new byte[16]; private byte[] _ip4 = new byte[4]; @@ -84,6 +84,30 @@ public String ipString() return ""; } + /** + * Convert to InetAddress + */ + public InetAddress toInetAddress() + { + if (_family == ZeroTierNative.ZTS_AF_INET) { + try { + return InetAddress.getByAddress(_ip4); + } + catch (Exception e) { + System.out.println(e); + } + } + if (_family == ZeroTierNative.ZTS_AF_INET6) { + try { + return InetAddress.getByAddress(_ip6); + } + catch (Exception e) { + System.out.println(e); + } + } + return null; + } + /** * Convert to string (ip and port) */