Skip to content

Commit

Permalink
Merge pull request #210 from janvanbouwel/java-fix
Browse files Browse the repository at this point in the history
4 small Java fixes
  • Loading branch information
joseph-henry committed Nov 29, 2023
2 parents ff93311 + a55152a commit 403d262
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
Expand Up @@ -22,7 +22,7 @@
*
* @author ZeroTier, Inc.
*/
public class ZeroTierDatagramSocket {
public class ZeroTierDatagramSocket implements Closeable {
ZeroTierSocket _socket;

/**
Expand Down
Expand Up @@ -22,7 +22,7 @@
*
* @author ZeroTier, Inc.
*/
public class ZeroTierServerSocket {
public class ZeroTierServerSocket implements Closeable {
private ZeroTierSocket _socket;

/**
Expand Down
13 changes: 7 additions & 6 deletions src/bindings/java/com/zerotier/sockets/ZeroTierSocket.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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
*/
Expand All @@ -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");
}
}
Expand Down
Expand Up @@ -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];

Expand Down Expand Up @@ -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)
*/
Expand Down

0 comments on commit 403d262

Please sign in to comment.