2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-13 04:42:43 +00:00
|
|
|
* Copyright (c) 2022, Alex Major
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-15 10:34:40 +00:00
|
|
|
#include <AK/Assertions.h>
|
2021-06-03 10:56:32 +00:00
|
|
|
#include <AK/IPv4Address.h>
|
2019-08-08 02:32:35 +00:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
2020-08-15 18:05:46 +00:00
|
|
|
#include <AK/NumberFormat.h>
|
2020-03-11 20:30:41 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2022-09-14 13:32:26 +00:00
|
|
|
#include <LibCore/System.h>
|
2022-01-13 04:42:43 +00:00
|
|
|
#include <LibMain/Main.h>
|
2019-09-23 17:06:53 +00:00
|
|
|
#include <net/if.h>
|
2020-01-02 02:52:47 +00:00
|
|
|
#include <netinet/in.h>
|
2019-06-16 05:06:49 +00:00
|
|
|
|
2022-01-13 04:42:43 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-06-16 05:06:49 +00:00
|
|
|
{
|
2022-07-11 20:42:03 +00:00
|
|
|
StringView value_ipv4 {};
|
|
|
|
StringView value_adapter {};
|
|
|
|
StringView value_mask {};
|
2019-09-23 17:06:53 +00:00
|
|
|
|
2020-03-11 20:30:41 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 15:22:58 +00:00
|
|
|
args_parser.set_general_help("Display or modify the configuration of each network interface.");
|
2021-06-04 16:15:33 +00:00
|
|
|
args_parser.add_option(value_ipv4, "Set the IP address of the selected network", "ipv4", 'i', "ip");
|
|
|
|
args_parser.add_option(value_adapter, "Select a specific network adapter to configure", "adapter", 'a', "adapter");
|
|
|
|
args_parser.add_option(value_mask, "Set the network mask of the selected network", "mask", 'm', "mask");
|
2022-01-13 04:42:43 +00:00
|
|
|
args_parser.parse(arguments);
|
2020-03-11 20:30:41 +00:00
|
|
|
|
2022-07-11 20:42:03 +00:00
|
|
|
if (value_ipv4.is_empty() && value_adapter.is_empty() && value_mask.is_empty()) {
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file = TRY(Core::File::open("/sys/kernel/net/adapters"sv, Core::File::OpenMode::Read));
|
2022-12-11 16:49:00 +00:00
|
|
|
auto file_contents = TRY(file->read_until_eof());
|
2022-09-14 13:32:26 +00:00
|
|
|
auto json = TRY(JsonValue::from_string(file_contents));
|
2020-03-11 20:30:41 +00:00
|
|
|
|
2021-11-15 00:46:51 +00:00
|
|
|
json.as_array().for_each([](auto& value) {
|
2021-05-31 16:59:02 +00:00
|
|
|
auto& if_object = value.as_object();
|
2020-03-11 20:30:41 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
auto name = if_object.get_byte_string("name"sv).value_or({});
|
|
|
|
auto class_name = if_object.get_byte_string("class_name"sv).value_or({});
|
|
|
|
auto mac_address = if_object.get_byte_string("mac_address"sv).value_or({});
|
|
|
|
auto ipv4_address = if_object.get_byte_string("ipv4_address"sv).value_or({});
|
|
|
|
auto netmask = if_object.get_byte_string("ipv4_netmask"sv).value_or({});
|
2022-12-22 14:27:48 +00:00
|
|
|
auto packets_in = if_object.get_u32("packets_in"sv).value_or(0);
|
|
|
|
auto bytes_in = if_object.get_u32("bytes_in"sv).value_or(0);
|
|
|
|
auto packets_out = if_object.get_u32("packets_out"sv).value_or(0);
|
|
|
|
auto bytes_out = if_object.get_u32("bytes_out"sv).value_or(0);
|
|
|
|
auto mtu = if_object.get_u32("mtu"sv).value_or(0);
|
2020-03-11 20:30:41 +00:00
|
|
|
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("{}:", name);
|
|
|
|
outln("\tmac: {}", mac_address);
|
|
|
|
outln("\tipv4: {}", ipv4_address);
|
|
|
|
outln("\tnetmask: {}", netmask);
|
|
|
|
outln("\tclass: {}", class_name);
|
|
|
|
outln("\tRX: {} packets {} bytes ({})", packets_in, bytes_in, human_readable_size(bytes_in));
|
|
|
|
outln("\tTX: {} packets {} bytes ({})", packets_out, bytes_out, human_readable_size(bytes_out));
|
|
|
|
outln("\tMTU: {}", mtu);
|
|
|
|
outln();
|
2020-03-11 20:30:41 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
|
2022-07-11 20:42:03 +00:00
|
|
|
if (value_adapter.is_empty()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("No network adapter was specified.");
|
2019-09-23 17:06:53 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString ifname = value_adapter;
|
2019-09-23 17:06:53 +00:00
|
|
|
|
2022-07-11 20:42:03 +00:00
|
|
|
if (!value_ipv4.is_empty()) {
|
2020-03-11 20:30:41 +00:00
|
|
|
auto address = IPv4Address::from_string(value_ipv4);
|
2019-09-23 17:06:53 +00:00
|
|
|
|
2020-03-11 20:30:41 +00:00
|
|
|
if (!address.has_value()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Invalid IPv4 address: '{}'", value_ipv4);
|
2020-03-11 20:30:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-14 13:32:26 +00:00
|
|
|
auto fd = TRY(Core::System::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP));
|
2020-03-11 20:30:41 +00:00
|
|
|
|
|
|
|
struct ifreq ifr;
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
|
2020-08-25 14:32:01 +00:00
|
|
|
bool fits = ifname.copy_characters_to_buffer(ifr.ifr_name, IFNAMSIZ);
|
|
|
|
if (!fits) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Interface name '{}' is too long", ifname);
|
2020-08-25 14:32:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2020-03-11 20:30:41 +00:00
|
|
|
ifr.ifr_addr.sa_family = AF_INET;
|
|
|
|
((sockaddr_in&)ifr.ifr_addr).sin_addr.s_addr = address.value().to_in_addr_t();
|
|
|
|
|
2022-09-14 13:32:26 +00:00
|
|
|
TRY(Core::System::ioctl(fd, SIOCSIFADDR, &ifr));
|
2019-09-23 17:06:53 +00:00
|
|
|
}
|
2019-06-16 05:06:49 +00:00
|
|
|
|
2022-07-11 20:42:03 +00:00
|
|
|
if (!value_mask.is_empty()) {
|
2020-03-11 20:30:41 +00:00
|
|
|
auto address = IPv4Address::from_string(value_mask);
|
|
|
|
|
|
|
|
if (!address.has_value()) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Invalid IPv4 mask: '{}'", value_mask);
|
2020-03-11 20:30:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-14 13:32:26 +00:00
|
|
|
auto fd = TRY(Core::System::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP));
|
2020-03-11 20:30:41 +00:00
|
|
|
|
|
|
|
struct ifreq ifr;
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2019-06-16 05:06:49 +00:00
|
|
|
|
2020-08-25 14:32:01 +00:00
|
|
|
bool fits = ifname.copy_characters_to_buffer(ifr.ifr_name, IFNAMSIZ);
|
|
|
|
if (!fits) {
|
2021-05-31 14:43:25 +00:00
|
|
|
warnln("Interface name '{}' is too long", ifname);
|
2020-08-25 14:32:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2020-03-11 20:30:41 +00:00
|
|
|
ifr.ifr_netmask.sa_family = AF_INET;
|
|
|
|
((sockaddr_in&)ifr.ifr_netmask).sin_addr.s_addr = address.value().to_in_addr_t();
|
2019-06-16 05:06:49 +00:00
|
|
|
|
2022-09-14 13:32:26 +00:00
|
|
|
TRY(Core::System::ioctl(fd, SIOCSIFNETMASK, &ifr));
|
2020-03-11 20:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-16 05:06:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|