Prevent mDNS from overwriting external IP addresses

This commit is contained in:
Cameron Gutman 2017-09-09 15:21:31 -07:00
parent b2bd7257e1
commit 5b05220008
2 changed files with 19 additions and 12 deletions

View File

@ -210,8 +210,8 @@ public class ComputerManagerService extends Service {
} }
} }
public boolean addComputerBlocking(String addr) { public boolean addComputerBlocking(String addr, boolean manuallyAdded) {
return ComputerManagerService.this.addComputerBlocking(addr); return ComputerManagerService.this.addComputerBlocking(addr, manuallyAdded);
} }
public void removeComputer(String name) { public void removeComputer(String name) {
@ -289,7 +289,7 @@ public class ComputerManagerService extends Service {
@Override @Override
public void notifyComputerAdded(MdnsComputer computer) { public void notifyComputerAdded(MdnsComputer computer) {
// Kick off a serverinfo poll on this machine // Kick off a serverinfo poll on this machine
addComputerBlocking(computer.getAddress().getHostAddress()); addComputerBlocking(computer.getAddress().getHostAddress(), false);
} }
@Override @Override
@ -305,15 +305,22 @@ public class ComputerManagerService extends Service {
}; };
} }
private void addTuple(ComputerDetails details) { private void addTuple(ComputerDetails details, boolean manuallyAdded) {
synchronized (pollingTuples) { synchronized (pollingTuples) {
for (PollingTuple tuple : pollingTuples) { for (PollingTuple tuple : pollingTuples) {
// Check if this is the same computer // Check if this is the same computer
if (tuple.computer.uuid.equals(details.uuid)) { if (tuple.computer.uuid.equals(details.uuid)) {
// Update details anyway in case this machine has been re-added by IP if (manuallyAdded) {
// after not being reachable by our existing information // Update details anyway in case this machine has been re-added by IP
tuple.computer.localAddress = details.localAddress; // after not being reachable by our existing information
tuple.computer.remoteAddress = details.remoteAddress; tuple.computer.localAddress = details.localAddress;
tuple.computer.remoteAddress = details.remoteAddress;
}
else {
// This indicates that mDNS discovered this address, so we
// should only apply the local address.
tuple.computer.localAddress = details.localAddress;
}
// Start a polling thread if polling is active // Start a polling thread if polling is active
if (pollingActive && tuple.thread == null) { if (pollingActive && tuple.thread == null) {
@ -338,7 +345,7 @@ public class ComputerManagerService extends Service {
} }
} }
public boolean addComputerBlocking(String addr) { public boolean addComputerBlocking(String addr, boolean manuallyAdded) {
// Setup a placeholder // Setup a placeholder
ComputerDetails fakeDetails = new ComputerDetails(); ComputerDetails fakeDetails = new ComputerDetails();
fakeDetails.localAddress = addr; fakeDetails.localAddress = addr;
@ -356,7 +363,7 @@ public class ComputerManagerService extends Service {
LimeLog.info("New PC ("+fakeDetails.name+") is UUID "+fakeDetails.uuid); LimeLog.info("New PC ("+fakeDetails.name+") is UUID "+fakeDetails.uuid);
// Start a polling thread for this machine // Start a polling thread for this machine
addTuple(fakeDetails); addTuple(fakeDetails, manuallyAdded);
return true; return true;
} }
else { else {
@ -616,7 +623,7 @@ public class ComputerManagerService extends Service {
for (ComputerDetails computer : dbManager.getAllComputers()) { for (ComputerDetails computer : dbManager.getAllComputers()) {
// Add tuples for each computer // Add tuples for each computer
addTuple(computer); addTuple(computer, true);
} }
releaseLocalDatabaseReference(); releaseLocalDatabaseReference();

View File

@ -46,7 +46,7 @@ public class AddComputerManually extends Activity {
SpinnerDialog dialog = SpinnerDialog.displayDialog(this, getResources().getString(R.string.title_add_pc), SpinnerDialog dialog = SpinnerDialog.displayDialog(this, getResources().getString(R.string.title_add_pc),
getResources().getString(R.string.msg_add_pc), false); getResources().getString(R.string.msg_add_pc), false);
if (!managerBinder.addComputerBlocking(host)){ if (!managerBinder.addComputerBlocking(host, true)){
msg = getResources().getString(R.string.addpc_fail); msg = getResources().getString(R.string.addpc_fail);
} }
else { else {