2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-12 23:42:43 -05:00
|
|
|
* Copyright (c) 2022, Alex Major
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2021-05-15 12:34:40 +02:00
|
|
|
#include <AK/Assertions.h>
|
2021-06-03 12:56:32 +02:00
|
|
|
#include <AK/IPv4Address.h>
|
2019-08-08 12:32:35 +10:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
2020-08-15 14:05:46 -04:00
|
|
|
#include <AK/NumberFormat.h>
|
2019-09-23 19:06:53 +02:00
|
|
|
#include <AK/String.h>
|
2019-08-08 12:32:35 +10:00
|
|
|
#include <AK/Types.h>
|
2020-03-11 22:30:41 +02:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/File.h>
|
2022-01-12 23:42:43 -05:00
|
|
|
#include <LibMain/Main.h>
|
2019-09-23 19:06:53 +02:00
|
|
|
#include <net/if.h>
|
2020-03-14 21:00:49 +02:00
|
|
|
#include <net/route.h>
|
2020-01-01 20:52:47 -06:00
|
|
|
#include <netinet/in.h>
|
2019-06-16 07:06:49 +02:00
|
|
|
#include <stdio.h>
|
2020-03-08 12:05:14 +01:00
|
|
|
#include <string.h>
|
2019-09-23 19:06:53 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
2019-06-16 07:06:49 +02:00
|
|
|
|
2022-01-12 23:42:43 -05:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-06-16 07:06:49 +02:00
|
|
|
{
|
2022-07-11 20:42:03 +00:00
|
|
|
StringView value_ipv4 {};
|
|
|
|
StringView value_adapter {};
|
|
|
|
StringView value_mask {};
|
2019-09-23 19:06:53 +02:00
|
|
|
|
2020-03-11 22:30:41 +02:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 16:22:58 +01:00
|
|
|
args_parser.set_general_help("Display or modify the configuration of each network interface.");
|
2021-06-04 18:15:33 +02: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-12 23:42:43 -05:00
|
|
|
args_parser.parse(arguments);
|
2020-03-11 22:30:41 +02:00
|
|
|
|
2022-07-11 20:42:03 +00:00
|
|
|
if (value_ipv4.is_empty() && value_adapter.is_empty() && value_mask.is_empty()) {
|
2022-10-14 21:56:19 +03:00
|
|
|
auto file = TRY(Core::File::open("/sys/kernel/net/adapters", Core::OpenMode::ReadOnly));
|
2022-01-12 23:42:43 -05:00
|
|
|
auto json = TRY(JsonValue::from_string(file->read_all()));
|
2020-03-11 22:30:41 +02:00
|
|
|
|
2021-11-15 01:46:51 +01:00
|
|
|
json.as_array().for_each([](auto& value) {
|
2021-05-31 17:59:02 +01:00
|
|
|
auto& if_object = value.as_object();
|
2020-03-11 22:30:41 +02:00
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
auto name = if_object.get("name"sv).to_string();
|
|
|
|
auto class_name = if_object.get("class_name"sv).to_string();
|
|
|
|
auto mac_address = if_object.get("mac_address"sv).to_string();
|
|
|
|
auto ipv4_address = if_object.get("ipv4_address"sv).to_string();
|
|
|
|
auto netmask = if_object.get("ipv4_netmask"sv).to_string();
|
|
|
|
auto packets_in = if_object.get("packets_in"sv).to_u32();
|
|
|
|
auto bytes_in = if_object.get("bytes_in"sv).to_u32();
|
|
|
|
auto packets_out = if_object.get("packets_out"sv).to_u32();
|
|
|
|
auto bytes_out = if_object.get("bytes_out"sv).to_u32();
|
|
|
|
auto mtu = if_object.get("mtu"sv).to_u32();
|
2020-03-11 22:30:41 +02:00
|
|
|
|
2021-05-31 15:43:25 +01: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 22:30:41 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
|
2022-07-11 20:42:03 +00:00
|
|
|
if (value_adapter.is_empty()) {
|
2021-05-31 15:43:25 +01:00
|
|
|
warnln("No network adapter was specified.");
|
2019-09-23 19:06:53 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-03-11 22:30:41 +02:00
|
|
|
String ifname = value_adapter;
|
2019-09-23 19:06:53 +02:00
|
|
|
|
2022-07-11 20:42:03 +00:00
|
|
|
if (!value_ipv4.is_empty()) {
|
2020-03-11 22:30:41 +02:00
|
|
|
auto address = IPv4Address::from_string(value_ipv4);
|
2019-09-23 19:06:53 +02:00
|
|
|
|
2020-03-11 22:30:41 +02:00
|
|
|
if (!address.has_value()) {
|
2021-05-31 15:43:25 +01:00
|
|
|
warnln("Invalid IPv4 address: '{}'", value_ipv4);
|
2020-03-11 22:30:41 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("socket");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ifreq ifr;
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
|
|
|
|
2020-08-25 17:32:01 +03:00
|
|
|
bool fits = ifname.copy_characters_to_buffer(ifr.ifr_name, IFNAMSIZ);
|
|
|
|
if (!fits) {
|
2021-05-31 15:43:25 +01:00
|
|
|
warnln("Interface name '{}' is too long", ifname);
|
2020-08-25 17:32:01 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2020-03-11 22:30:41 +02:00
|
|
|
ifr.ifr_addr.sa_family = AF_INET;
|
|
|
|
((sockaddr_in&)ifr.ifr_addr).sin_addr.s_addr = address.value().to_in_addr_t();
|
|
|
|
|
|
|
|
int rc = ioctl(fd, SIOCSIFADDR, &ifr);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("ioctl(SIOCSIFADDR)");
|
|
|
|
return 1;
|
|
|
|
}
|
2019-09-23 19:06:53 +02:00
|
|
|
}
|
2019-06-16 07:06:49 +02:00
|
|
|
|
2022-07-11 20:42:03 +00:00
|
|
|
if (!value_mask.is_empty()) {
|
2020-03-11 22:30:41 +02:00
|
|
|
auto address = IPv4Address::from_string(value_mask);
|
|
|
|
|
|
|
|
if (!address.has_value()) {
|
2021-05-31 15:43:25 +01:00
|
|
|
warnln("Invalid IPv4 mask: '{}'", value_mask);
|
2020-03-11 22:30:41 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("socket");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ifreq ifr;
|
|
|
|
memset(&ifr, 0, sizeof(ifr));
|
2019-06-16 07:06:49 +02:00
|
|
|
|
2020-08-25 17:32:01 +03:00
|
|
|
bool fits = ifname.copy_characters_to_buffer(ifr.ifr_name, IFNAMSIZ);
|
|
|
|
if (!fits) {
|
2021-05-31 15:43:25 +01:00
|
|
|
warnln("Interface name '{}' is too long", ifname);
|
2020-08-25 17:32:01 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2020-03-11 22:30:41 +02: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 07:06:49 +02:00
|
|
|
|
2020-03-11 22:30:41 +02:00
|
|
|
int rc = ioctl(fd, SIOCSIFNETMASK, &ifr);
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("ioctl(SIOCSIFNETMASK)");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-16 07:06:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|