Serenity Operating System
1/*
2 * Copyright (c) 2020, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/Assertions.h>
8#include <AK/DeprecatedString.h>
9#include <AK/IPv4Address.h>
10#include <AK/JsonObject.h>
11#include <AK/MACAddress.h>
12#include <AK/QuickSort.h>
13#include <AK/Types.h>
14#include <LibCore/ArgsParser.h>
15#include <LibCore/File.h>
16#include <LibCore/System.h>
17#include <LibMain/Main.h>
18#include <arpa/inet.h>
19#include <net/if_arp.h>
20#include <net/route.h>
21#include <netdb.h>
22#include <netinet/in.h>
23#include <string.h>
24#include <sys/ioctl.h>
25#include <sys/socket.h>
26#include <unistd.h>
27
28ErrorOr<int> serenity_main(Main::Arguments arguments)
29{
30 TRY(Core::System::pledge("stdio rpath tty inet unix"));
31
32 static bool flag_set;
33 static bool flag_delete;
34 static bool flag_numeric;
35 StringView value_ipv4_address;
36 StringView value_hw_address;
37
38 Core::ArgsParser args_parser;
39 args_parser.set_general_help("Display or modify the system ARP cache");
40 args_parser.add_option(flag_set, "Set an ARP table entry", "set", 's');
41 args_parser.add_option(flag_delete, "Delete an ARP table entry", "delete", 'd');
42 args_parser.add_option(flag_numeric, "Display numerical addresses. Don't resolve hostnames", "numeric", 'n');
43 args_parser.add_positional_argument(value_ipv4_address, "IPv4 protocol address", "address", Core::ArgsParser::Required::No);
44 args_parser.add_positional_argument(value_hw_address, "Hardware address", "hwaddress", Core::ArgsParser::Required::No);
45 args_parser.parse(arguments);
46
47 TRY(Core::System::unveil("/sys/kernel/net/arp", "r"));
48 if (!flag_numeric)
49 TRY(Core::System::unveil("/tmp/portal/lookup", "rw"));
50
51 TRY(Core::System::unveil(nullptr, nullptr));
52
53 enum class Alignment {
54 Left,
55 Right
56 };
57
58 struct Column {
59 DeprecatedString title;
60 Alignment alignment { Alignment::Left };
61 int width { 0 };
62 DeprecatedString buffer;
63 };
64
65 Vector<Column> columns;
66
67 int proto_address_column = -1;
68 int hw_address_column = -1;
69
70 auto add_column = [&](auto title, auto alignment, auto width) {
71 columns.append({ title, alignment, width, {} });
72 return columns.size() - 1;
73 };
74
75 proto_address_column = add_column("Address", Alignment::Left, 15);
76 hw_address_column = add_column("HWaddress", Alignment::Left, 15);
77
78 auto print_column = [](auto& column, auto& string) {
79 if (!column.width) {
80 out("{}", string);
81 return;
82 }
83 if (column.alignment == Alignment::Right) {
84 out("{:>{1}} "sv, string, column.width);
85 } else {
86 out("{:<{1}} "sv, string, column.width);
87 }
88 };
89
90 for (auto& column : columns)
91 print_column(column, column.title);
92 outln();
93
94 if (!flag_set && !flag_delete) {
95 auto file = TRY(Core::File::open("/sys/kernel/net/arp"sv, Core::File::OpenMode::Read));
96 auto file_contents = TRY(file->read_until_eof());
97 auto json = TRY(JsonValue::from_string(file_contents));
98
99 Vector<JsonValue> sorted_regions = json.as_array().values();
100 quick_sort(sorted_regions, [](auto& a, auto& b) {
101 return a.as_object().get_deprecated_string("ip_address"sv).value_or({}) < b.as_object().get_deprecated_string("ip_address"sv).value_or({});
102 });
103
104 for (auto& value : sorted_regions) {
105 auto& if_object = value.as_object();
106
107 auto ip_address = if_object.get_deprecated_string("ip_address"sv).value_or({});
108
109 if (!flag_numeric) {
110 auto from_string = IPv4Address::from_string(ip_address);
111 auto addr = from_string.value().to_in_addr_t();
112 auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET);
113 if (hostent != nullptr) {
114 StringView host_name { hostent->h_name, strlen(hostent->h_name) };
115 if (!host_name.is_empty())
116 ip_address = host_name;
117 }
118 }
119
120 auto mac_address = if_object.get_deprecated_string("mac_address"sv).value_or({});
121
122 if (proto_address_column != -1)
123 columns[proto_address_column].buffer = ip_address;
124 if (hw_address_column != -1)
125 columns[hw_address_column].buffer = mac_address;
126
127 for (auto& column : columns)
128 print_column(column, column.buffer);
129 outln();
130 };
131 }
132
133 if (flag_set || flag_delete) {
134 if (value_ipv4_address.is_empty() || value_hw_address.is_empty()) {
135 warnln("No protocol address or hardware address specified.");
136 return 1;
137 }
138
139 auto address = IPv4Address::from_string(value_ipv4_address);
140 if (!address.has_value()) {
141 warnln("Invalid IPv4 protocol address: '{}'", value_ipv4_address);
142 return 1;
143 }
144
145 auto hw_address = MACAddress::from_string(value_hw_address);
146 if (!hw_address.has_value()) {
147 warnln("Invalid MACAddress: '{}'", value_hw_address);
148 return 1;
149 }
150
151 int fd = TRY(Core::System::socket(AF_INET, SOCK_DGRAM, IPPROTO_IP));
152
153 struct arpreq arp_req;
154 memset(&arp_req, 0, sizeof(arp_req));
155
156 arp_req.arp_pa.sa_family = AF_INET;
157 ((sockaddr_in&)arp_req.arp_pa).sin_addr.s_addr = address.value().to_in_addr_t();
158
159 *(MACAddress*)&arp_req.arp_ha.sa_data[0] = hw_address.value();
160
161 if (flag_set)
162 TRY(Core::System::ioctl(fd, SIOCSARP, &arp_req));
163 if (flag_delete)
164 TRY(Core::System::ioctl(fd, SIOCDARP, &arp_req));
165 }
166
167 return 0;
168}