diff --git a/src/main/java/nsusbloader/com/net/NETCommunications.java b/src/main/java/nsusbloader/com/net/NETCommunications.java index b26e8bd..e6d137f 100644 --- a/src/main/java/nsusbloader/com/net/NETCommunications.java +++ b/src/main/java/nsusbloader/com/net/NETCommunications.java @@ -73,7 +73,7 @@ public NETCommunications(List filesList, this.logPrinter = Log.getPrinter(EModule.USB_NET_TRANSFERS); NetworkSetupValidator validator = - new NetworkSetupValidator(filesList, doNotServe, hostIP, hostPortNum, logPrinter); + new NetworkSetupValidator(filesList, doNotServe, hostIP, hostPortNum, switchIP, logPrinter); this.hostIP = validator.getHostIP(); this.hostPort = validator.getHostPort(); diff --git a/src/main/java/nsusbloader/com/net/NetworkSetupValidator.java b/src/main/java/nsusbloader/com/net/NetworkSetupValidator.java index 38898b1..a32e831 100644 --- a/src/main/java/nsusbloader/com/net/NetworkSetupValidator.java +++ b/src/main/java/nsusbloader/com/net/NetworkSetupValidator.java @@ -43,6 +43,7 @@ public class NetworkSetupValidator { boolean doNotServe, String hostIP, String hostPortNum, + String remoteIP, ILogPrinter logPrinter) { this.files = new HashMap<>(); this.logPrinter = logPrinter; @@ -51,7 +52,7 @@ public class NetworkSetupValidator { try { validateFiles(filesList); encodeAndAddFilesToMap(filesList); - resolveIp(hostIP); + resolveIp(hostIP, remoteIP); resolvePort(hostPortNum); } catch (Exception e){ @@ -117,16 +118,16 @@ private void encodeAndAddFilesToMap(List filesList) throws UnsupportedEnco } } - private void resolveIp(String hostIPaddr) throws IOException, InterruptedException{ + private void resolveIp(String hostIPaddr, String remoteIP) throws IOException, InterruptedException{ if (! hostIPaddr.isEmpty()){ this.hostIP = hostIPaddr; logPrinter.print("NET: Host IP defined as: " + hostIP, EMsgType.PASS); return; } - if (findIpLocally()) + if (findIpLocally(remoteIP)) return; - + if (findIpUsingHost("google.com")) return; @@ -155,7 +156,7 @@ private boolean findIpUsingHost(String host) throws InterruptedException{ } } - private boolean findIpLocally() throws InterruptedException{ + private boolean findIpLocally(String remoteIP) throws InterruptedException{ try { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { @@ -166,9 +167,13 @@ private boolean findIpLocally() throws InterruptedException{ while (addresses.hasMoreElements()) { InetAddress addr = addresses.nextElement(); if (addr instanceof Inet4Address && !addr.isLoopbackAddress()) { - hostIP = addr.getHostAddress(); - logPrinter.print("NET: Host IP detected locally as: " + hostIP, EMsgType.PASS); - return true; + // Test if this interface can reach the remote IP + if (isAddressReachable(iface, remoteIP)) { + hostIP = addr.getHostAddress(); + logPrinter.print("NET: Host IP verified as: " + hostIP + + " (can reach " + remoteIP + ")", EMsgType.PASS); + return true; + } } } } @@ -179,6 +184,19 @@ private boolean findIpLocally() throws InterruptedException{ return false; } + private boolean isAddressReachable(NetworkInterface iface, String remoteIP) + { + try { + InetAddress remoteAddr = InetAddress.getByName(remoteIP); + // Test if remoteIP is reachable via this specific network interface + boolean reachable = remoteAddr.isReachable(iface, 64, 5000); + return reachable; + } + catch (Exception e) { + return false; + } + } + private String getAvaliableIpExamples(){ try { StringBuilder builder = new StringBuilder("Check for:\n");