Android API updates, address-related bugfixes, file leak fix

This commit is contained in:
Joseph Henry
2016-08-14 22:20:55 -07:00
parent 93b452bef2
commit c454c5c0bc
13 changed files with 379 additions and 155 deletions

View File

@@ -46,6 +46,17 @@ public class SDK {
public static int SOCK_STREAM = 1;
public static int SOCK_DGRAM = 2;
// fcntl flags
public static int O_APPEND = 1024;
public static int O_NONBLOCK = 2048;
public static int O_ASYNC = 8192;
public static int O_DIRECT = 65536;
public static int O_NOATIME = 262144;
// fcntl cmds
public static int F_GETFL = 3;
public static int F_SETFL = 4;
// Loads JNI code
static { System.loadLibrary("ZeroTierOneJNI"); }
@@ -65,9 +76,25 @@ public class SDK {
zt_leave_network(nwid);
}
// ------------------------------------------------------------------------------
// ------------------------------- get_addresses() ------------------------------
// ------------------------------------------------------------------------------
public native ArrayList<String> zt_get_addresses(String nwid);
public ArrayList<String> get_addresses(String nwid) {
return zt_get_addresses(nwid);
int err = -1;
ArrayList<String> addresses;
while (err < 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
addresses = zt_get_addresses(nwid);
if (addresses.size() > 0) {
return addresses;
}
}
return null;
}
public native int zt_get_proxy_port(String nwid);
@@ -104,7 +131,7 @@ public class SDK {
public int connect(int sock, String addr, int port, String nwid)
{
int err = -1;
ArrayList<String> addresses = new ArrayList<String>();
ArrayList<String> addresses;
while (err < 0) {
try {
Thread.sleep(500);
@@ -131,7 +158,7 @@ public class SDK {
}
public int bind(int sock, String addr, int port, String nwid) {
int err = -1;
ArrayList<String> addresses = new ArrayList<String>();
ArrayList<String> addresses;
while (err < 0) {
try {
Thread.sleep(500);
@@ -213,6 +240,15 @@ public class SDK {
return zt_sendto(fd,buf,len,flags,addr);
}
// ------------------------------------------------------------------------------
// ----------------------------------- send() -----------------------------------
// ------------------------------------------------------------------------------
public native int zt_send(int fd, byte[] buf, int len, int flags);
public int send(int fd, byte[] buf, int len, int flags) {
return zt_send(fd, buf, len, flags);
}
// ------------------------------------------------------------------------------
// ---------------------------------- recvfrom() --------------------------------
// ------------------------------------------------------------------------------
@@ -222,6 +258,17 @@ public class SDK {
return zt_recvfrom(fd,buf,len,flags,addr);
}
// ------------------------------------------------------------------------------
// ---------------------------------- recvfrom() --------------------------------
// ------------------------------------------------------------------------------
public native int zt_fcntl(int sock, int cmd, int flag);
public int fcntl(int sock, int cmd, int flag) {
return zt_fcntl(sock, F_SETFL, O_NONBLOCK);
}
//public static native int zt_getsockopt(int fd, int type, int protocol);
//public static native int zt_setsockopt(int fd, int type, int protocol);
//public static native int zt_getsockname(int fd, int type, int protocol);

View File

@@ -4,20 +4,23 @@ import java.math.BigInteger;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.regex.Pattern;
public class ZTAddress
{
// int -> byte array
static public byte[] toIPByteArray(long addr){
return new byte[]{(byte)addr,(byte)(addr>>>8),(byte)(addr>>>16),(byte)(addr>>>24)};
}
int pack(byte[] bytes) {
int val = 0;
for (int i = 0; i < bytes.length; i++) {
val <<= 8;
val |= bytes[i] & 0xff;
// byte array -> int
long toIPInt(String _addr) {
long result = 0;
for(String part: _addr.split(Pattern.quote("."))) {
result = result << 8;
result |= Integer.parseInt(part);
}
return val;
return result;
}
public int port;
@@ -42,18 +45,20 @@ public class ZTAddress
public ZTAddress()
{
port = -1;
_rawAddr = -1;
}
public ZTAddress(String _addr, int _port)
{
_rawAddr = toIPInt(_addr);
port = _port;
_rawAddr = pack(_addr.getBytes());
}
public void ZTAddress(InetSocketAddress ins)
{
port = ins.getPort();
_rawAddr = toIPInt(ins.getAddress().getHostAddress());
}
public InetSocketAddress ToInetSocketAddress() throws IllegalArgumentException {
@@ -65,4 +70,8 @@ public class ZTAddress
}
return sock_addr;
}
public boolean isValid() {
return port != -1 && !Address().startsWith("-1.-1.-1.-1/-1");
}
}

View File

@@ -175,36 +175,42 @@ public class MainActivity extends AppCompatActivity {
int sock = zt.socket(SDK.AF_INET, SDK.SOCK_DGRAM, 0);
Log.d("TEST", "binding...");
err = zt.bind(sock, bindAddr, nwid);
if (err < 0)
if((err = zt.bind(sock, bindAddr, nwid)) < 0)
Log.d("TEST", "bind_err = " + err + "\n");
err = zt.listen(sock, 0);
if(err < 0)
if((err = zt.listen(sock, 0)) < 0)
Log.d("TEST", "listen_err = " + err);
ArrayList<String> addresses = zt.get_addresses(nwid);
if(addresses.size() < 0) {
Log.d("TEST", "unable to obtain ZT address");
return;
}
else {
Log.d("TEST", "IPV4 = " + addresses.get(0));
}
String bufStr = "";
byte[] buffer = new byte[1024];
zt.fcntl(sock, zt.F_SETFL, zt.O_NONBLOCK);
// ECHO
while(true) {
bufStr = "";
while(err >= 0) {
// RX
byte[] buffer = new byte[32];
String addr_string = "-1.-1.-1.-1";
int port_no = -1;
if((err = zt.recvfrom(sock, buffer, 32, 0, remoteServer)) > 0) {
bufStr = new String(buffer).substring(0, err);
Log.d("TEST", "read (" + err + ") bytes from " + remoteServer.Address() + " : " + remoteServer.Port() + ", msg = " + bufStr);
err = zt.recvfrom(sock, buffer, 32, 0, remoteServer);
String bufStr = new String(buffer).substring(0, err);
Log.d("TEST", "read (" + err + ") bytes from " + remoteServer.Address() + " : " + remoteServer.Port() + ", msg = " + bufStr);
// TX
if(err > 0) {
String msg = "Welcome response from android";
// TX
String msg = "Welcome response from android\n";
err = zt.sendto(sock, msg.getBytes(), msg.length(), 0, remoteServer);
if (err < 0)
Log.d("TEST", "sendto_err = " + err);
}
}
Log.d("TEST", "leaving network");
zt.leave_network(nwid);
//Log.d("TEST", "leaving network");
//zt.leave_network(nwid);
}
}
}