diff --git a/app/src/main/java/net/christianbeier/droidvnc_ng/MainActivity.java b/app/src/main/java/net/christianbeier/droidvnc_ng/MainActivity.java index 3a793dd1..fa5c1724 100644 --- a/app/src/main/java/net/christianbeier/droidvnc_ng/MainActivity.java +++ b/app/src/main/java/net/christianbeier/droidvnc_ng/MainActivity.java @@ -311,7 +311,7 @@ protected void onCreate(Bundle savedInstanceState) { }); - ListenIfAdapter lsif = new ListenIfAdapter(MainService.getAvailableNICs(), this); + ListenIfAdapter lsif = new ListenIfAdapter(Utils.getAvailableNICs(), this); final Spinner listenInterfaceSpin = findViewById(R.id.settings_listening_interface); listenInterfaceSpin.setAdapter(lsif); listenInterfaceSpin.setOnItemSelectedListener(new OnItemSelectedListener() { diff --git a/app/src/main/java/net/christianbeier/droidvnc_ng/MainService.java b/app/src/main/java/net/christianbeier/droidvnc_ng/MainService.java index e28faa16..844cc684 100644 --- a/app/src/main/java/net/christianbeier/droidvnc_ng/MainService.java +++ b/app/src/main/java/net/christianbeier/droidvnc_ng/MainService.java @@ -766,60 +766,6 @@ static boolean isServerActive() { } - - /** - * Get all the available network interfaces, in the up status, which have at least one IPv4 address - * @return An ArrayList<NetworkInterfaces> containing the above-mentioned interfaces - */ - static ArrayList getAvailableNICs() { - ArrayList nics = new ArrayList<>(); - - try { - // Thanks go to https://stackoverflow.com/a/20103869/361413 - Enumeration nis = NetworkInterface.getNetworkInterfaces(); - NetworkInterface ni; - while (nis.hasMoreElements()) { - ni = nis.nextElement(); - if (ni.isUp()) { - // Check if there are actual ipv4 addresses, if so, add the NetworkInterface - // Should we consider IPv6 also? Technically yes, but the program, at the moment, does not support them. - for (InterfaceAddress ia : ni.getInterfaceAddresses()) { - if (ia.getAddress().getAddress().length == 4) { - nics.add(ni); - break; - } - } - } - } - } catch (SocketException e) { - //unused - } - - return nics; - } - - - - /** - * Gets all the ipv4s available for a single NetworkInterface - * @param ni the NetworkInterface to check - * @return an ArrayList<String> which contains all the collected ipv4 - */ - static ArrayList getIPv4ForInterface(NetworkInterface ni) { - ArrayList ipv4s = new ArrayList<>(); - - for (InterfaceAddress ia : ni.getInterfaceAddresses()) { - //filter for ipv4/ipv6 - if (ia.getAddress().getAddress().length == 4) { - //4 for ipv4, 16 for ipv6 - ipv4s.add(ia.getAddress().toString().replaceAll("/", "")); - } - } - - return ipv4s; - } - - /** * This returns A SINGLE ipv4 address for the selected interface... giving the possibility * to the server to listen to that address only. @@ -828,14 +774,9 @@ static ArrayList getIPv4ForInterface(NetworkInterface ni) { */ static String getInterfaceListeningIPv4Address(String ifName) { if (!ifName.equals("0.0.0.0")) { - try { - ArrayList ipv4s = MainService.getIPv4ForInterface(NetworkInterface.getByName(ifName)); - if (ipv4s.size() > 0) { - return ipv4s.get(0); - } - - } catch (SocketException ex) { - // unused + ArrayList ipv4s = Utils.getIPv4ForInterface(ifName); + if (ipv4s.size() > 0) { + return ipv4s.get(0); } } @@ -864,10 +805,10 @@ static ArrayList getReachableIPv4s() { try { if (listenInterface.equals("0.0.0.0")) { // Any mode: get all the available NICs and add their IPv4 - ArrayList nics = MainService.getAvailableNICs(); + ArrayList nics = Utils.getAvailableNICs(); for (NetworkInterface nic : nics) { if (!nic.isLoopback()) { - ArrayList ipv4s = MainService.getIPv4ForInterface(nic); + ArrayList ipv4s = Utils.getIPv4ForInterface(nic); for (String ipv4 : ipv4s) { hosts.add(ipv4); } @@ -875,7 +816,7 @@ static ArrayList getReachableIPv4s() { } } else { // Single interface: get all its IPv4 addresses - ArrayList ipv4s = MainService.getIPv4ForInterface(NetworkInterface.getByName(listenInterface)); + ArrayList ipv4s = Utils.getIPv4ForInterface(NetworkInterface.getByName(listenInterface)); for (String ipv4 : ipv4s) { hosts.add(ipv4); } diff --git a/app/src/main/java/net/christianbeier/droidvnc_ng/Utils.kt b/app/src/main/java/net/christianbeier/droidvnc_ng/Utils.kt index c4fe663d..9ae3d7d5 100644 --- a/app/src/main/java/net/christianbeier/droidvnc_ng/Utils.kt +++ b/app/src/main/java/net/christianbeier/droidvnc_ng/Utils.kt @@ -12,6 +12,10 @@ import java.io.IOException import java.io.InputStream import java.io.InputStreamReader +import java.net.InterfaceAddress; +import java.net.NetworkInterface; +import java.net.SocketException; + object Utils { @JvmStatic @@ -92,4 +96,65 @@ object Utils { directory.deleteRecursively() } + + @JvmStatic + fun getAvailableNICs(upIfOnly: Boolean, ipAvailOnly: Boolean): ArrayList { + val nics = ArrayList(); + + try { + // Thanks go to https://stackoverflow.com/a/20103869/361413 + val nis = NetworkInterface.getNetworkInterfaces(); + while (nis.hasMoreElements()) { + val ni = nis.nextElement(); + if (upIfOnly) { + if (ipAvailOnly) { + // Check if there are actual ipv4 addresses, if so, add the NetworkInterface + // Should we consider IPv6 also? Technically yes, but the program, at the moment, does not support them. + for (ia in ni.getInterfaceAddresses()) { + if (ia.getAddress().getAddress().count() == 4) { + nics.add(ni); + break; + } + } + } else { + nics.add(ni); + } + } else { + nics.add(ni); + } + } + } catch (e: SocketException) { + //unused + } + + return nics; + } + + + @JvmStatic + fun getAvailableNICs(): ArrayList { + return getAvailableNICs(true, true); + } + + + @JvmStatic + fun getIPv4ForInterface(ni: String): ArrayList{ + return getIPv4ForInterface(NetworkInterface.getByName(ni)); + } + + + @JvmStatic + fun getIPv4ForInterface(ni: NetworkInterface): ArrayList{ + val ipv4s = ArrayList(); + + for (ia in ni.getInterfaceAddresses()) { + //filter for ipv4/ipv6 + if (ia.getAddress().getAddress().count() == 4) { + //4 for ipv4, 16 for ipv6 + ipv4s.add(ia.getAddress().toString().replace("/", "")); + } + } + + return ipv4s; + } } \ No newline at end of file