mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:58:12 +00:00
DHCPClient: Retry DISCOVER for interfaces that fail
Attempts are spaced out with exponential backoff, cut at 10 minutes per attempt. Also avoid trying to acquire an IP on interfaces that aren't up. Fixes #6126. Fixes #6125.
This commit is contained in:
parent
8b0f1f816b
commit
c6607719d2
3 changed files with 129 additions and 54 deletions
|
@ -38,23 +38,6 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static u8 mac_part(const Vector<String>& parts, size_t index)
|
||||
{
|
||||
auto result = AK::StringUtils::convert_to_uint_from_hex(parts.at(index));
|
||||
VERIFY(result.has_value());
|
||||
return result.value();
|
||||
}
|
||||
|
||||
static MACAddress mac_from_string(const String& str)
|
||||
{
|
||||
auto chunks = str.split(':');
|
||||
VERIFY(chunks.size() == 6); // should we...worry about this?
|
||||
return {
|
||||
mac_part(chunks, 0), mac_part(chunks, 1), mac_part(chunks, 2),
|
||||
mac_part(chunks, 3), mac_part(chunks, 4), mac_part(chunks, 5)
|
||||
};
|
||||
}
|
||||
|
||||
int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
|
||||
{
|
||||
if (pledge("stdio unix inet cpath rpath fattr", nullptr) < 0) {
|
||||
|
@ -71,36 +54,16 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
|
|||
|
||||
unveil(nullptr, nullptr);
|
||||
|
||||
auto file = Core::File::construct("/proc/net/adapters");
|
||||
if (!file->open(Core::IODevice::ReadOnly)) {
|
||||
dbgln("Error: {}", file->error_string());
|
||||
auto ifs_result = DHCPv4Client::get_discoverable_interfaces();
|
||||
if (ifs_result.is_error()) {
|
||||
warnln("Error: {}", ifs_result.error());
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto file_contents = file->read_all();
|
||||
auto json = JsonValue::from_string(file_contents);
|
||||
auto ifs = ifs_result.release_value();
|
||||
auto client = DHCPv4Client::construct(move(ifs.ready), move(ifs.not_ready));
|
||||
|
||||
if (!json.has_value() || !json.value().is_array()) {
|
||||
dbgln("Error: No network adapters available");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Vector<InterfaceDescriptor> ifnames;
|
||||
json.value().as_array().for_each([&ifnames](auto& value) {
|
||||
auto if_object = value.as_object();
|
||||
|
||||
if (if_object.get("class_name").to_string() == "LoopbackAdapter")
|
||||
return;
|
||||
|
||||
auto name = if_object.get("name").to_string();
|
||||
auto mac = if_object.get("mac_address").to_string();
|
||||
dbgln_if(DHCPV4_DEBUG, "Found adapter '{}' with mac {}", name, mac);
|
||||
ifnames.append({ name, mac_from_string(mac) });
|
||||
});
|
||||
|
||||
auto client = DHCPv4Client::construct(move(ifnames));
|
||||
|
||||
if (pledge("stdio inet", nullptr) < 0) {
|
||||
if (pledge("stdio inet cpath rpath fattr", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue