Skip to content
Merged
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
33 changes: 29 additions & 4 deletions base/cvd/allocd/alloc_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ Result<std::string> SearchForIptables() {
return CF_EXPECT(Search({"/usr/sbin", "/sbin"}, "iptables"));
}

Result<std::string> SearchForDnsmasq() {
Result<std::string> p = Search(cuttlefish::Path(), "dnsmasq");
if (p.has_value()) {
return p;
}

return CF_EXPECT(Search({"/usr/sbin", "/sbin"}, "dnsmasq"));
}

} // namespace

bool CreateEthernetIface(std::string_view name, std::string_view bridge_name) {
Expand Down Expand Up @@ -284,11 +293,19 @@ void CleanupBridgeGateway(std::string_view name, std::string_view ipaddr,

bool StartDnsmasq(std::string_view bridge_name, std::string_view gateway,
std::string_view dhcp_range) {
Result<std::string> dnsmasq_path = DnsmasqPath();
if (!dnsmasq_path.has_value()) {
LOG(ERROR) << "Could not find dnsmasq binary: "
<< dnsmasq_path.error().Message();
return false;
}

auto dns_servers = "8.8.8.8,8.8.4.4";
auto dns6_servers = "2001:4860:4860::8888,2001:4860:4860::8844";

return Execute(
{"dnsmasq", "--port=0", "--strict-order", "--except-interface=lo",
{*dnsmasq_path, "--port=0", "--strict-order",
"--except-interface=lo",
absl::StrCat("--interface=", bridge_name),
absl::StrCat("--listen-address=", gateway), "--bind-interfaces",
absl::StrCat("--dhcp-range=", dhcp_range),
Expand All @@ -305,12 +322,12 @@ bool StartDnsmasq(std::string_view bridge_name, std::string_view gateway,
bool StopDnsmasq(std::string_view name) {
std::ifstream file;
std::string filename =
absl::StrFormat("/var/run/cuttlefish-dnsmasq-%s.pid", name);
absl::StrCat(CvdDir(), "/cuttlefish-dnsmasq-", name, ".pid");
LOG(INFO) << "stopping dnsmasq for interface: " << name;
file.open(filename);
if (file.is_open()) {
if (!file.is_open()) {
LOG(INFO) << "dnsmasq file:" << filename
<< " could not be opened, assume dnsmaq has already stopped";
<< " could not be opened, assume dnsmasq has already stopped";
return true;
}

Expand Down Expand Up @@ -366,4 +383,12 @@ Result<std::string> IptablesPath() {
return *iptables_path;
}

Result<std::string> DnsmasqPath() {
static const absl::NoDestructor<std::string> dnsmasq_path(
SearchForDnsmasq().value_or(""));

CF_EXPECT(!dnsmasq_path->empty(), "could not find dnsmasq");
return *dnsmasq_path;
}

} // namespace cuttlefish
1 change: 1 addition & 0 deletions base/cvd/allocd/alloc_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct GatewayConfig {

int RunExternalCommand(const std::string& command);
Result<std::string> IptablesPath();
Result<std::string> DnsmasqPath();
std::optional<std::string> GetUserName(uid_t uid);

bool CreateTap(std::string_view name);
Expand Down
Loading