Commit 55076700 authored by matterchen's avatar matterchen Committed by Commit Bot

Crostini Port Forwarding: Remove and deactivate port fwd

Port forwarding in Cros will allow users to add, activate, remove and
deactivate ports.

This CL includes the implementation of the remove and deactivate port
utilities and unit tests.

The tracking of the port forwarding preferences in the user's profile
preferences has not been implemented yet.

Bug: chromium:848127
Test: crostini_port_forwarder_unittest.cc and everything builds
Change-Id: Ie7b5358813ca9cf454e0e216467c6d1ec8344c08
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2015868Reviewed-by: default avatarFergus Dall <sidereal@google.com>
Reviewed-by: default avatarRyo Hashimoto <hashimoto@chromium.org>
Commit-Queue: Matthew Chen <matterchen@google.com>
Cr-Commit-Position: refs/heads/master@{#736249}
parent 4cbeee91
...@@ -90,11 +90,42 @@ void CrostiniPortForwarder::OnAddPortCompleted(ResultCallback result_callback, ...@@ -90,11 +90,42 @@ void CrostiniPortForwarder::OnAddPortCompleted(ResultCallback result_callback,
std::move(result_callback).Run(success); std::move(result_callback).Run(success);
} }
void CrostiniPortForwarder::OnDeactivatePortCompleted(
ResultCallback result_callback,
PortRuleKey key,
bool success) {
if (!success) {
LOG(ERROR) << "Failed to deactivate port, port is still being forwarded: "
<< key.port_number;
std::move(result_callback).Run(success);
return;
}
// TODO(matterchen): Set existing port forward preference active state ==
// False.
forwarded_ports_.erase(key);
std::move(result_callback).Run(success);
}
void CrostiniPortForwarder::OnRemovePortCompleted(
ResultCallback result_callback,
PortRuleKey key,
bool success) {
if (!success) {
LOG(ERROR) << "Failed to remove port, port is still being forwarded: "
<< key.port_number;
std::move(result_callback).Run(success);
return;
}
// TODO(matterchen): Remove existing port forward preference.
forwarded_ports_.erase(key);
std::move(result_callback).Run(success);
}
void CrostiniPortForwarder::TryActivatePort( void CrostiniPortForwarder::TryActivatePort(
uint16_t port_number, uint16_t port_number,
const Protocol& protocol_type, const Protocol& protocol_type,
const std::string& ipv4_addr, const std::string& ipv4_addr,
chromeos::PermissionBrokerClient::ResultCallback result_callback) { base::OnceCallback<void(bool)> result_callback) {
chromeos::PermissionBrokerClient* client = chromeos::PermissionBrokerClient* client =
chromeos::PermissionBrokerClient::Get(); chromeos::PermissionBrokerClient::Get();
if (!client) { if (!client) {
...@@ -121,14 +152,45 @@ void CrostiniPortForwarder::TryActivatePort( ...@@ -121,14 +152,45 @@ void CrostiniPortForwarder::TryActivatePort(
forwarded_ports_[port_key] = std::move(lifeline_local); forwarded_ports_[port_key] = std::move(lifeline_local);
if (Protocol::TCP == protocol_type) { switch (protocol_type) {
client->RequestTcpPortForward( case Protocol::TCP:
port_number, kDefaultInterfaceToForward, ipv4_addr, port_number, client->RequestTcpPortForward(
std::move(lifeline_remote.get()), std::move(result_callback)); port_number, kDefaultInterfaceToForward, ipv4_addr, port_number,
} else if (Protocol::UDP == protocol_type) { std::move(lifeline_remote.get()), std::move(result_callback));
client->RequestUdpPortForward( break;
port_number, kDefaultInterfaceToForward, ipv4_addr, port_number, case Protocol::UDP:
std::move(lifeline_remote.get()), std::move(result_callback)); client->RequestUdpPortForward(
port_number, kDefaultInterfaceToForward, ipv4_addr, port_number,
std::move(lifeline_remote.get()), std::move(result_callback));
break;
}
}
void CrostiniPortForwarder::TryDeactivatePort(
const PortRuleKey& key,
base::OnceCallback<void(bool)> result_callback) {
if (forwarded_ports_.find(key) == forwarded_ports_.end()) {
LOG(ERROR) << "Trying to deactivate a non-active port.";
std::move(result_callback).Run(false);
return;
}
chromeos::PermissionBrokerClient* client =
chromeos::PermissionBrokerClient::Get();
if (!client) {
LOG(ERROR) << "Could not get permission broker client.";
std::move(result_callback).Run(false);
return;
}
switch (key.protocol_type) {
case Protocol::TCP:
client->ReleaseTcpPortForward(key.port_number, kDefaultInterfaceToForward,
std::move(result_callback));
break;
case Protocol::UDP:
client->ReleaseUdpPortForward(key.port_number, kDefaultInterfaceToForward,
std::move(result_callback));
} }
} }
...@@ -159,23 +221,72 @@ void CrostiniPortForwarder::AddPort(uint16_t port_number, ...@@ -159,23 +221,72 @@ void CrostiniPortForwarder::AddPort(uint16_t port_number,
} }
void CrostiniPortForwarder::ActivatePort(uint16_t port_number, void CrostiniPortForwarder::ActivatePort(uint16_t port_number,
const Protocol& protocol_type,
ResultCallback result_callback) { ResultCallback result_callback) {
// TODO(matterchen): Find the current port setting from profile preferences.
PortRuleKey existing_port_key = { PortRuleKey existing_port_key = {
.port_number = port_number, .port_number = port_number,
.protocol_type = Protocol::TCP, .protocol_type = protocol_type,
.input_ifname = kDefaultInterfaceToForward, .input_ifname = kDefaultInterfaceToForward,
}; };
if (forwarded_ports_.find(existing_port_key) != forwarded_ports_.end()) {
LOG(ERROR) << "Trying to activate an already active port.";
std::move(result_callback).Run(false);
return;
}
base::OnceCallback<void(bool)> on_activate_port_completed = base::OnceCallback<void(bool)> on_activate_port_completed =
base::BindOnce(&CrostiniPortForwarder::OnActivatePortCompleted, base::BindOnce(&CrostiniPortForwarder::OnActivatePortCompleted,
weak_ptr_factory_.GetWeakPtr(), std::move(result_callback), weak_ptr_factory_.GetWeakPtr(), std::move(result_callback),
existing_port_key); existing_port_key);
// TODO(matterchen): Extract container IPv4 address. // TODO(matterchen): Extract container IPv4 address.
CrostiniPortForwarder::TryActivatePort(port_number, Protocol::TCP, CrostiniPortForwarder::TryActivatePort(port_number, protocol_type,
"PLACEHOLDER_IP_ADDRESS", "PLACEHOLDER_IP_ADDRESS",
std::move(on_activate_port_completed)); std::move(on_activate_port_completed));
} }
void CrostiniPortForwarder::DeactivatePort(uint16_t port_number,
const Protocol& protocol_type,
ResultCallback result_callback) {
PortRuleKey existing_port_key = {
.port_number = port_number,
.protocol_type = protocol_type,
.input_ifname = kDefaultInterfaceToForward,
};
base::OnceCallback<void(bool)> on_deactivate_port_completed =
base::BindOnce(&CrostiniPortForwarder::OnDeactivatePortCompleted,
weak_ptr_factory_.GetWeakPtr(), std::move(result_callback),
existing_port_key);
CrostiniPortForwarder::TryDeactivatePort(
existing_port_key, std::move(on_deactivate_port_completed));
}
void CrostiniPortForwarder::RemovePort(uint16_t port_number,
const Protocol& protocol_type,
ResultCallback result_callback) {
// TODO(matterchen): Check if port is active in preferences, if active,
// deactivate port using on_remove_port_completed callback. Otherwise, just
// remove from preferences by calling OnRemovePortCompleted directly.
PortRuleKey existing_port_key = {
.port_number = port_number,
.protocol_type = protocol_type,
.input_ifname = kDefaultInterfaceToForward,
};
base::OnceCallback<void(bool)> on_remove_port_completed =
base::BindOnce(&CrostiniPortForwarder::OnRemovePortCompleted,
weak_ptr_factory_.GetWeakPtr(), std::move(result_callback),
existing_port_key);
CrostiniPortForwarder::TryDeactivatePort(existing_port_key,
std::move(on_remove_port_completed));
}
size_t CrostiniPortForwarder::GetNumberOfForwardedPortsForTesting() {
return forwarded_ports_.size();
}
} // namespace crostini } // namespace crostini
...@@ -46,11 +46,21 @@ class CrostiniPortForwarder : public KeyedService { ...@@ -46,11 +46,21 @@ class CrostiniPortForwarder : public KeyedService {
}; };
using ResultCallback = base::OnceCallback<void(bool)>; using ResultCallback = base::OnceCallback<void(bool)>;
void ActivatePort(uint16_t port_number, ResultCallback result_callback); void ActivatePort(uint16_t port_number,
const Protocol& protocol_type,
ResultCallback result_callback);
void AddPort(uint16_t port_number, void AddPort(uint16_t port_number,
const Protocol& protocol_type, const Protocol& protocol_type,
const std::string& label, const std::string& label,
ResultCallback result_callback); ResultCallback result_callback);
void DeactivatePort(uint16_t port_number,
const Protocol& protocol_type,
ResultCallback result_callback);
void RemovePort(uint16_t port_number,
const Protocol& protocol_type,
ResultCallback result_callback);
size_t GetNumberOfForwardedPortsForTesting();
static CrostiniPortForwarder* GetForProfile(Profile* profile); static CrostiniPortForwarder* GetForProfile(Profile* profile);
...@@ -58,9 +68,6 @@ class CrostiniPortForwarder : public KeyedService { ...@@ -58,9 +68,6 @@ class CrostiniPortForwarder : public KeyedService {
~CrostiniPortForwarder() override; ~CrostiniPortForwarder() override;
private: private:
FRIEND_TEST_ALL_PREFIXES(CrostiniPortForwarderTest, AddPortDuplicateFail);
FRIEND_TEST_ALL_PREFIXES(CrostiniPortForwarderTest, AddPortMultipleSuccess);
FRIEND_TEST_ALL_PREFIXES(CrostiniPortForwarderTest, AddPortUdpAndTcpSuccess);
FRIEND_TEST_ALL_PREFIXES(CrostiniPortForwarderTest, FRIEND_TEST_ALL_PREFIXES(CrostiniPortForwarderTest,
TryActivatePortPermissionBrokerClientFail); TryActivatePortPermissionBrokerClientFail);
void OnActivatePortCompleted(ResultCallback result_callback, void OnActivatePortCompleted(ResultCallback result_callback,
...@@ -70,6 +77,14 @@ class CrostiniPortForwarder : public KeyedService { ...@@ -70,6 +77,14 @@ class CrostiniPortForwarder : public KeyedService {
std::string label, std::string label,
PortRuleKey key, PortRuleKey key,
bool success); bool success);
void OnDeactivatePortCompleted(ResultCallback result_callback,
PortRuleKey key,
bool success);
void OnRemovePortCompleted(ResultCallback result_callback,
PortRuleKey key,
bool success);
void TryDeactivatePort(const PortRuleKey& key,
base::OnceCallback<void(bool)> result_callback);
void TryActivatePort(uint16_t port_number, void TryActivatePort(uint16_t port_number,
const Protocol& protocol_type, const Protocol& protocol_type,
const std::string& ipv4_addr, const std::string& ipv4_addr,
......
...@@ -178,14 +178,18 @@ void FakePermissionBrokerClient::ReleaseTcpPortForward( ...@@ -178,14 +178,18 @@ void FakePermissionBrokerClient::ReleaseTcpPortForward(
uint16_t in_port, uint16_t in_port,
const std::string& in_interface, const std::string& in_interface,
ResultCallback callback) { ResultCallback callback) {
std::move(callback).Run(false); auto rule = std::make_pair(in_port, in_interface);
tcp_forwarding_set_.erase(rule);
std::move(callback).Run(true);
} }
void FakePermissionBrokerClient::ReleaseUdpPortForward( void FakePermissionBrokerClient::ReleaseUdpPortForward(
uint16_t in_port, uint16_t in_port,
const std::string& in_interface, const std::string& in_interface,
ResultCallback callback) { ResultCallback callback) {
std::move(callback).Run(false); auto rule = std::make_pair(in_port, in_interface);
udp_forwarding_set_.erase(rule);
std::move(callback).Run(true);
} }
bool FakePermissionBrokerClient::RequestPortImpl(uint16_t port, bool FakePermissionBrokerClient::RequestPortImpl(uint16_t port,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment