diff options
author | brapru <brapru@pm.me> | 2022-04-17 10:06:44 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-04-21 13:17:29 +0200 |
commit | a4d84a76e1225d32beea5056ce7d1d65d6141681 (patch) | |
tree | 1672425533bd019a2efca73c89fab9875460d995 | |
parent | 71f2c342c41c9b4771d8accaa644922303634882 (diff) | |
download | serenity-a4d84a76e1225d32beea5056ce7d1d65d6141681.zip |
arp: Add hostname resolution
-rw-r--r-- | Userland/Utilities/arp.cpp | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/Userland/Utilities/arp.cpp b/Userland/Utilities/arp.cpp index e4d913fad1..dff57c279c 100644 --- a/Userland/Utilities/arp.cpp +++ b/Userland/Utilities/arp.cpp @@ -15,8 +15,10 @@ #include <LibCore/File.h> #include <LibCore/System.h> #include <LibMain/Main.h> +#include <arpa/inet.h> #include <net/if_arp.h> #include <net/route.h> +#include <netdb.h> #include <netinet/in.h> #include <string.h> #include <sys/ioctl.h> @@ -25,12 +27,14 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) { - TRY(Core::System::pledge("stdio rpath tty inet")); + TRY(Core::System::pledge("stdio rpath tty inet unix")); TRY(Core::System::unveil("/proc/net/arp", "r")); + TRY(Core::System::unveil("/tmp/portal/lookup", "rw")); TRY(Core::System::unveil(nullptr, nullptr)); static bool flag_set; static bool flag_delete; + static bool flag_numeric; StringView value_ipv4_address; StringView value_hw_address; @@ -38,6 +42,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) 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_option(flag_numeric, "Display numerical addresses. Don't resolve hostnames", "numeric", 'n'); 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); args_parser.parse(arguments); @@ -107,6 +112,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) auto& if_object = value.as_object(); auto ip_address = if_object.get("ip_address").to_string(); + + if (!flag_numeric) { + auto from_string = IPv4Address::from_string(ip_address); + auto addr = from_string.value().to_in_addr_t(); + auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET); + if (hostent != nullptr) { + auto host_name = StringView(hostent->h_name); + if (!host_name.is_empty()) + ip_address = host_name; + } + } + auto mac_address = if_object.get("mac_address").to_string(); if (proto_address_column != -1) |