From e14d4482a18f250787cbf64bae596de1f7278d8d Mon Sep 17 00:00:00 2001 From: Maciej Date: Sat, 7 May 2022 14:59:34 +0200 Subject: [PATCH] DHCPClient: Don't discover interfaces other than given by default Now, the caller needs to give interface names in command-line arguments. The DHCPClient will perform DHCP discovery only on these adapters. The service now immediately closes when no interfaces were given. We don't check if interface has already IP address assigned; we just reset it to zero so that DHCP resolution will not fail. --- Userland/Services/DHCPClient/DHCPv4Client.cpp | 13 +++++++++++-- Userland/Services/DHCPClient/DHCPv4Client.h | 3 ++- Userland/Services/DHCPClient/main.cpp | 11 +++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Userland/Services/DHCPClient/DHCPv4Client.cpp b/Userland/Services/DHCPClient/DHCPv4Client.cpp index 24a25ab127..6ebd8236ee 100644 --- a/Userland/Services/DHCPClient/DHCPv4Client.cpp +++ b/Userland/Services/DHCPClient/DHCPv4Client.cpp @@ -7,6 +7,7 @@ #include "DHCPv4Client.h" #include #include +#include #include #include #include @@ -116,7 +117,8 @@ static void set_params(InterfaceDescriptor const& iface, IPv4Address const& ipv4 } } -DHCPv4Client::DHCPv4Client() +DHCPv4Client::DHCPv4Client(Vector interfaces_with_dhcp_enabled) + : m_interfaces_with_dhcp_enabled(move(interfaces_with_dhcp_enabled)) { m_server = Core::UDPServer::construct(this); m_server->on_ready_to_receive = [this] { @@ -149,11 +151,18 @@ void DHCPv4Client::try_discover_ifs() if (ifs_result.is_error()) return; + dbgln("Interfaces with DHCP enabled: {}", m_interfaces_with_dhcp_enabled); bool sent_discover_request = false; Interfaces& ifs = ifs_result.value(); for (auto& iface : ifs.ready) { - if (iface.current_ip_address != IPv4Address { 0, 0, 0, 0 }) + dbgln("Checking interface {} / {}", iface.ifname, iface.current_ip_address); + if (!m_interfaces_with_dhcp_enabled.contains_slow(iface.ifname)) continue; + if (iface.current_ip_address != IPv4Address { 0, 0, 0, 0 }) { + dbgln_if(DHCPV4CLIENT_DEBUG, "Resetting params for {}", iface.ifname); + set_params(iface, IPv4Address { 0, 0, 0, 0 }, IPv4Address { 0, 0, 0, 0 }, {}); + iface.current_ip_address = IPv4Address { 0, 0, 0, 0 }; + } dhcp_discover(iface); sent_discover_request = true; diff --git a/Userland/Services/DHCPClient/DHCPv4Client.h b/Userland/Services/DHCPClient/DHCPv4Client.h index 6e7146d36a..7cf6fb1392 100644 --- a/Userland/Services/DHCPClient/DHCPv4Client.h +++ b/Userland/Services/DHCPClient/DHCPv4Client.h @@ -54,10 +54,11 @@ public: static ErrorOr get_discoverable_interfaces(); private: - explicit DHCPv4Client(); + explicit DHCPv4Client(Vector interfaces_with_dhcp_enabled); void try_discover_ifs(); + Vector m_interfaces_with_dhcp_enabled; HashMap> m_ongoing_transactions; RefPtr m_server; RefPtr m_check_timer; diff --git a/Userland/Services/DHCPClient/main.cpp b/Userland/Services/DHCPClient/main.cpp index 812ab78275..36d6802db0 100644 --- a/Userland/Services/DHCPClient/main.cpp +++ b/Userland/Services/DHCPClient/main.cpp @@ -5,19 +5,26 @@ */ #include "DHCPv4Client.h" +#include #include #include #include -ErrorOr serenity_main(Main::Arguments) +ErrorOr serenity_main(Main::Arguments args) { + Vector interfaces; + + Core::ArgsParser parser; + parser.add_positional_argument(interfaces, "Interfaces to run DHCP server on", "interfaces"); + parser.parse(args); + TRY(Core::System::pledge("stdio unix inet cpath rpath")); Core::EventLoop event_loop; TRY(Core::System::unveil("/proc/net/", "r")); TRY(Core::System::unveil(nullptr, nullptr)); - auto client = TRY(DHCPv4Client::try_create()); + auto client = TRY(DHCPv4Client::try_create(interfaces)); TRY(Core::System::pledge("stdio inet cpath rpath")); return event_loop.exec();