mirror of
https://github.com/RGBCube/serenity
synced 2025-05-30 17:08:13 +00:00
route: Accept CIDR notation when specifying network
Now that the IPv4Address has the ability to generate valid IP addresses from CIDR notations, this provides a nicer interface to the user when specifying the network address to add or delete.
This commit is contained in:
parent
c7aa05cdcc
commit
6691ef5a44
1 changed files with 18 additions and 3 deletions
|
@ -157,8 +157,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
if (!value_host_address.is_empty())
|
if (!value_host_address.is_empty())
|
||||||
destination = AK::IPv4Address::from_string(value_host_address);
|
destination = AK::IPv4Address::from_string(value_host_address);
|
||||||
|
|
||||||
if (!value_network_address.is_empty())
|
StringView address;
|
||||||
destination = AK::IPv4Address::from_string(value_network_address);
|
StringView cidr;
|
||||||
|
if (!value_network_address.is_empty()) {
|
||||||
|
// Check if a CIDR notation was provided and parse accordingly
|
||||||
|
if (auto position = value_network_address.find('/'); position.has_value()) {
|
||||||
|
address = value_network_address.substring_view(0, position.value());
|
||||||
|
cidr = value_network_address.substring_view(position.value() + 1);
|
||||||
|
} else {
|
||||||
|
address = value_network_address;
|
||||||
|
}
|
||||||
|
destination = AK::IPv4Address::from_string(address);
|
||||||
|
}
|
||||||
|
|
||||||
if (!destination.has_value()) {
|
if (!destination.has_value()) {
|
||||||
warnln("Invalid destination IPv4 address");
|
warnln("Invalid destination IPv4 address");
|
||||||
|
@ -171,7 +181,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto genmask = AK::IPv4Address::from_string(value_netmask_address);
|
Optional<IPv4Address> genmask;
|
||||||
|
if (auto cidr_int = cidr.to_int(); cidr_int.has_value())
|
||||||
|
genmask = AK::IPv4Address::netmask_from_cidr(cidr_int.value());
|
||||||
|
else
|
||||||
|
genmask = AK::IPv4Address::from_string(value_netmask_address);
|
||||||
|
|
||||||
if (!genmask.has_value()) {
|
if (!genmask.has_value()) {
|
||||||
warnln("Invalid genmask IPv4 address: '{}'", value_netmask_address);
|
warnln("Invalid genmask IPv4 address: '{}'", value_netmask_address);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue