2020-04-26 10:54:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-26 10:54:22 +00:00
|
|
|
*/
|
|
|
|
|
2021-05-15 10:34:40 +00:00
|
|
|
#include <AK/Assertions.h>
|
2021-07-25 00:29:48 +00:00
|
|
|
#include <AK/IPv4Address.h>
|
2020-04-26 10:54:22 +00:00
|
|
|
#include <AK/JsonObject.h>
|
2021-07-25 00:29:48 +00:00
|
|
|
#include <AK/MACAddress.h>
|
2021-07-31 03:33:47 +00:00
|
|
|
#include <AK/QuickSort.h>
|
2021-07-25 00:29:48 +00:00
|
|
|
#include <AK/String.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-04-26 10:54:22 +00:00
|
|
|
#include <LibCore/File.h>
|
2021-11-23 16:14:56 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-08-14 16:06:44 +00:00
|
|
|
#include <net/if_arp.h>
|
2021-07-25 00:29:48 +00:00
|
|
|
#include <net/route.h>
|
|
|
|
#include <netinet/in.h>
|
2021-11-07 01:15:10 +00:00
|
|
|
#include <string.h>
|
2021-07-25 00:29:48 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
2021-07-31 03:19:48 +00:00
|
|
|
#include <unistd.h>
|
2020-04-26 10:54:22 +00:00
|
|
|
|
2021-11-23 16:14:56 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-04-26 10:54:22 +00:00
|
|
|
{
|
2021-11-27 22:26:34 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath tty"));
|
2021-11-23 16:14:56 +00:00
|
|
|
TRY(Core::System::unveil("/proc/net/arp", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-07-31 03:19:48 +00:00
|
|
|
|
2021-07-25 00:29:48 +00:00
|
|
|
static bool flag_set;
|
|
|
|
static bool flag_delete;
|
|
|
|
const char* value_ipv4_address = nullptr;
|
|
|
|
const char* value_hw_address = nullptr;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("Display or modify the system ARP cache");
|
|
|
|
args_parser.add_option(flag_set, "Set an ARP table entry", "set", 's');
|
|
|
|
args_parser.add_option(flag_delete, "Delete an ARP table entry", "delete", 'd');
|
|
|
|
args_parser.add_positional_argument(value_ipv4_address, "IPv4 protocol address", "address", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.add_positional_argument(value_hw_address, "Hardware address", "hwaddress", Core::ArgsParser::Required::No);
|
2021-11-23 16:14:56 +00:00
|
|
|
args_parser.parse(arguments);
|
2021-07-25 00:29:48 +00:00
|
|
|
|
2021-07-31 03:33:47 +00:00
|
|
|
enum class Alignment {
|
|
|
|
Left,
|
|
|
|
Right
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Column {
|
|
|
|
String title;
|
|
|
|
Alignment alignment { Alignment::Left };
|
|
|
|
int width { 0 };
|
|
|
|
String buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Column> columns;
|
|
|
|
|
|
|
|
int proto_address_column = -1;
|
|
|
|
int hw_address_column = -1;
|
|
|
|
|
|
|
|
auto add_column = [&](auto title, auto alignment, auto width) {
|
|
|
|
columns.append({ title, alignment, width, {} });
|
|
|
|
return columns.size() - 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
proto_address_column = add_column("Address", Alignment::Left, 15);
|
|
|
|
hw_address_column = add_column("HWaddress", Alignment::Left, 15);
|
|
|
|
|
|
|
|
auto print_column = [](auto& column, auto& string) {
|
|
|
|
if (!column.width) {
|
|
|
|
out("{}", string);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (column.alignment == Alignment::Right) {
|
|
|
|
out("{:>{1}} "sv, string, column.width);
|
|
|
|
} else {
|
|
|
|
out("{:<{1}} "sv, string, column.width);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto& column : columns)
|
|
|
|
print_column(column, column.title);
|
|
|
|
outln();
|
2020-04-26 10:54:22 +00:00
|
|
|
|
2021-07-25 00:29:48 +00:00
|
|
|
if (!flag_set && !flag_delete) {
|
2021-07-31 03:33:47 +00:00
|
|
|
auto file = Core::File::construct("/proc/net/arp");
|
|
|
|
if (!file->open(Core::OpenMode::ReadOnly)) {
|
|
|
|
warnln("Failed to open {}: {}", file->name(), file->error_string());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-07-25 00:29:48 +00:00
|
|
|
auto file_contents = file->read_all();
|
2021-12-25 15:43:34 +00:00
|
|
|
auto json_or_error = JsonValue::from_string(file_contents);
|
|
|
|
if (json_or_error.is_error()) {
|
|
|
|
warnln("Failed to decode JSON: {}", json_or_error.error());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
auto json = json_or_error.release_value();
|
2021-07-31 03:33:47 +00:00
|
|
|
|
2021-11-15 00:46:51 +00:00
|
|
|
Vector<JsonValue> sorted_regions = json.as_array().values();
|
2021-07-31 03:33:47 +00:00
|
|
|
quick_sort(sorted_regions, [](auto& a, auto& b) {
|
|
|
|
return a.as_object().get("ip_address").to_string() < b.as_object().get("ip_address").to_string();
|
|
|
|
});
|
|
|
|
|
|
|
|
for (auto& value : sorted_regions) {
|
2021-07-25 00:29:48 +00:00
|
|
|
auto& if_object = value.as_object();
|
|
|
|
|
|
|
|
auto ip_address = if_object.get("ip_address").to_string();
|
|
|
|
auto mac_address = if_object.get("mac_address").to_string();
|
|
|
|
|
2021-07-31 03:33:47 +00:00
|
|
|
if (proto_address_column != -1)
|
|
|
|
columns[proto_address_column].buffer = ip_address;
|
|
|
|
if (hw_address_column != -1)
|
|
|
|
columns[hw_address_column].buffer = mac_address;
|
|
|
|
|
|
|
|
for (auto& column : columns)
|
|
|
|
print_column(column, column.buffer);
|
|
|
|
outln();
|
|
|
|
};
|
2021-07-25 00:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flag_set || flag_delete) {
|
|
|
|
if (!value_ipv4_address || !value_hw_address) {
|
|
|
|
warnln("No protocol address or hardware address specified.");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto address = IPv4Address::from_string(value_ipv4_address);
|
|
|
|
if (!address.has_value()) {
|
|
|
|
warnln("Invalid IPv4 protocol address: '{}'", value_ipv4_address);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-04-26 10:54:22 +00:00
|
|
|
|
2021-07-25 00:29:48 +00:00
|
|
|
auto hw_address = MACAddress::from_string(value_hw_address);
|
|
|
|
if (!hw_address.has_value()) {
|
|
|
|
warnln("Invalid MACAddress: '{}'", value_hw_address);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-04-26 10:54:22 +00:00
|
|
|
|
2021-07-25 00:29:48 +00:00
|
|
|
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("socket");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct arpreq arp_req;
|
|
|
|
memset(&arp_req, 0, sizeof(arp_req));
|
|
|
|
|
|
|
|
arp_req.arp_pa.sa_family = AF_INET;
|
|
|
|
((sockaddr_in&)arp_req.arp_pa).sin_addr.s_addr = address.value().to_in_addr_t();
|
|
|
|
|
|
|
|
*(MACAddress*)&arp_req.arp_ha.sa_data[0] = hw_address.value();
|
|
|
|
|
2021-10-10 22:02:03 +00:00
|
|
|
int rc = 0;
|
2021-07-25 00:29:48 +00:00
|
|
|
if (flag_set)
|
|
|
|
rc = ioctl(fd, SIOCSARP, &arp_req);
|
2021-10-10 22:02:03 +00:00
|
|
|
if (flag_delete) {
|
|
|
|
int rc2 = ioctl(fd, SIOCDARP, &arp_req);
|
|
|
|
if (!rc2)
|
|
|
|
rc = rc2;
|
|
|
|
}
|
2021-07-25 00:29:48 +00:00
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
perror("ioctl");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 10:54:22 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|