1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:07:34 +00:00

NetworkServer: Support setting default gateway

This commit adds an IPv4Gateway to Network.ini. If that option is set to
value other than 0.0.0.0, the NetworkServer adds a default route (e.g.
with address 0.0.0.0/0) with the specified destination.
This commit is contained in:
Maciej 2022-05-28 18:46:55 +02:00 committed by Linus Groh
parent d90131bce1
commit 1ffba0b8b4
2 changed files with 8 additions and 1 deletions

View file

@ -20,14 +20,16 @@ The interface that is not listed in this config file is not set up and disabled
* `DHCP` (default: `false`) - Whether the DHCP client should be run on this interface.
* `IPv4Address` (default: `0.0.0.0`) - The static IPv4 address for the interface. Used only when `DHCP` is `false`.
* `IPv4Netmask` (default: `0.0.0.0`) - The static IPv4 netmask for the interface. Used only when `DHCP` is `false`.
* `IPv4Gateway` (default: `0.0.0.0`) - The static IPv4 default gateway for the interface. Used only when `DHCP` is `false`.
## Example
```ini
# Set static IP address to 10.0.0.5 and mask to 255.0.0.0
# Set static IP address to 10.0.0.5/8 and default gateway to 10.0.0.1
[ep1s0]
IPv4Address=10.0.0.5
IPv4Netmask=255.0.0.0
IPv4Gateway=10.0.0.1
# Try to run DHCP discovery on ep0s8.
[ep0s8]

View file

@ -22,6 +22,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
TRY(Core::System::unveil("/bin/DHCPClient", "x"));
TRY(Core::System::unveil("/etc/Network.ini", "r"));
TRY(Core::System::unveil("/bin/ifconfig", "x"));
TRY(Core::System::unveil("/bin/route", "x"));
TRY(Core::System::unveil(nullptr, nullptr));
auto config_file = TRY(Core::ConfigFile::open_for_system("Network"));
@ -40,6 +41,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
bool dhcp_enabled = false;
String ipv4_address = "0.0.0.0";
String ipv4_netmask = "0.0.0.0";
String ipv4_gateway = "0.0.0.0";
};
Vector<String> interfaces_with_dhcp_enabled;
@ -59,6 +61,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
if (!config.dhcp_enabled) {
config.ipv4_address = config_file->read_entry(ifname, "IPv4Address", "0.0.0.0");
config.ipv4_netmask = config_file->read_entry(ifname, "IPv4Netmask", "0.0.0.0");
config.ipv4_gateway = config_file->read_entry(ifname, "IPv4Gateway", "0.0.0.0");
}
}
if (config.enabled) {
@ -69,6 +72,8 @@ ErrorOr<int> serenity_main(Main::Arguments)
// FIXME: Do this asynchronously
dbgln("Setting up interface {} statically ({}/{})", ifname, config.ipv4_address, config.ipv4_netmask);
MUST(Core::command("ifconfig", { "-a", ifname.characters(), "-i", config.ipv4_address.characters(), "-m", config.ipv4_netmask.characters() }, {}));
if (config.ipv4_gateway != "0.0.0.0")
MUST(Core::command("route", { "add", "-n", "0.0.0.0", "-m", "0.0.0.0", "-g", config.ipv4_gateway, "-i", ifname }, {}));
}
}
});