[cdc_rsync] Use any available server port (#94)

Instead of calling netstat on the remote device to detect available
ports, simply call bind with port 0 to bind to any available port.
Since the port is not yet known when cdc_rsync_server.exe is called,
port forwarding needs to be started AFTER the server reports its port.
This commit is contained in:
Lutz Justen
2023-03-06 14:16:21 +01:00
committed by GitHub
parent 5fd86e4625
commit a8059e8572
10 changed files with 137 additions and 131 deletions

View File

@@ -168,7 +168,7 @@ absl::StatusOr<int> PortManager::ReservePort(int remote_timeout_sec,
local_ports,
FindAvailableLocalPorts(first_port_, last_port_,
ArchType::kWindows_x86_64, process_factory_),
"Failed to find available ports on workstation");
"Failed to find available local ports");
// Find available port on remote instance.
std::unordered_set<int> remote_ports = local_ports;
@@ -178,7 +178,7 @@ absl::StatusOr<int> PortManager::ReservePort(int remote_timeout_sec,
FindAvailableRemotePorts(first_port_, last_port_, remote_arch_type,
process_factory_, remote_util_,
remote_timeout_sec, steady_clock_),
"Failed to find available ports on instance");
"Failed to find available remote ports");
}
// Fetch shared memory.
@@ -209,9 +209,9 @@ absl::StatusOr<int> PortManager::ReservePort(int remote_timeout_sec,
LOG_DEBUG("Trying to reserve port %i", port);
// We have reserved this port. Double-check that it's actually not in use
// on both the workstation and the server.
// on both the local and the remote device
if (local_ports.find(port) == local_ports.end()) {
LOG_DEBUG("Port %i not available on workstation", port);
LOG_DEBUG("Local port %i not available", port);
InterlockedCompareExchange64(ts_ptr, now, port_timestamp);
continue;
}
@@ -221,7 +221,7 @@ absl::StatusOr<int> PortManager::ReservePort(int remote_timeout_sec,
continue;
}
LOG_DEBUG("Port %i is available on workstation and instance", port);
LOG_DEBUG("Port %i is available", port);
reserved_ports_.insert(port);
return port;
}
@@ -269,7 +269,7 @@ absl::StatusOr<std::unordered_set<int>> PortManager::FindAvailableLocalPorts(
return WrapStatus(status, "Failed to run netstat:\n%s", errors);
}
LOG_DEBUG("netstat (workstation) output:\n%s", output);
LOG_DEBUG("netstat (local) output:\n%s", output);
return FindAvailablePorts(first_port, last_port, output, arch_type);
}
@@ -316,7 +316,7 @@ absl::StatusOr<std::unordered_set<int>> PortManager::FindAvailableRemotePorts(
errors);
}
LOG_DEBUG("netstat (instance) output:\n%s", output);
LOG_DEBUG("netstat (remote) output:\n%s", output);
return FindAvailablePorts(first_port, last_port, output, arch_type);
}