summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorMaciej <sppmacd@pm.me>2022-05-07 14:59:34 +0200
committerLinus Groh <mail@linusgroh.de>2022-05-26 21:47:27 +0100
commite14d4482a18f250787cbf64bae596de1f7278d8d (patch)
treebefeec29406a42cd0d75bb04d50ab8a900740cce /Userland
parent01c7158ffe4719dc1cf74c25675e6c7d73e9e2ec (diff)
downloadserenity-e14d4482a18f250787cbf64bae596de1f7278d8d.zip
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.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Services/DHCPClient/DHCPv4Client.cpp13
-rw-r--r--Userland/Services/DHCPClient/DHCPv4Client.h3
-rw-r--r--Userland/Services/DHCPClient/main.cpp11
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 <AK/Array.h>
#include <AK/Debug.h>
+#include <AK/IPv4Address.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/JsonParser.h>
@@ -116,7 +117,8 @@ static void set_params(InterfaceDescriptor const& iface, IPv4Address const& ipv4
}
}
-DHCPv4Client::DHCPv4Client()
+DHCPv4Client::DHCPv4Client(Vector<String> 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<Interfaces> get_discoverable_interfaces();
private:
- explicit DHCPv4Client();
+ explicit DHCPv4Client(Vector<String> interfaces_with_dhcp_enabled);
void try_discover_ifs();
+ Vector<String> m_interfaces_with_dhcp_enabled;
HashMap<u32, OwnPtr<DHCPv4Transaction>> m_ongoing_transactions;
RefPtr<Core::UDPServer> m_server;
RefPtr<Core::Timer> 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 <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
-ErrorOr<int> serenity_main(Main::Arguments)
+ErrorOr<int> serenity_main(Main::Arguments args)
{
+ Vector<String> 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();