Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/nsusbloader/com/net/NETCommunications.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public NETCommunications(List<File> 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();
Expand Down
34 changes: 26 additions & 8 deletions src/main/java/nsusbloader/com/net/NetworkSetupValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class NetworkSetupValidator {
boolean doNotServe,
String hostIP,
String hostPortNum,
String remoteIP,
ILogPrinter logPrinter) {
this.files = new HashMap<>();
this.logPrinter = logPrinter;
Expand All @@ -51,7 +52,7 @@ public class NetworkSetupValidator {
try {
validateFiles(filesList);
encodeAndAddFilesToMap(filesList);
resolveIp(hostIP);
resolveIp(hostIP, remoteIP);
resolvePort(hostPortNum);
}
catch (Exception e){
Expand Down Expand Up @@ -117,16 +118,16 @@ private void encodeAndAddFilesToMap(List<File> 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;

Expand Down Expand Up @@ -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<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
Expand All @@ -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;
}
}
}
}
Expand All @@ -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");
Expand Down