summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbrapru <brapru@pm.me>2022-04-16 16:46:28 -0400
committerBrian Gianforcaro <b.gianfo@gmail.com>2022-04-16 22:16:29 -0700
commit8b370f988b79568243be7e3f9c8250f1036f7aa4 (patch)
treec097ec89c15a14b786f6e04d4d9ce47ed8e34574
parent9191829a39c7774d7cb8dfaa5675ff31a49a4a8f (diff)
downloadserenity-8b370f988b79568243be7e3f9c8250f1036f7aa4.zip
host: Use AK/IPv4Address to determine if argument is host/ip
It's a bit cleaner to just rely on AK/IPv4Address' ability to determine the validity of the given input. If a valid IP address is not returned, then input will be processed as a hostname.
-rw-r--r--Userland/Utilities/host.cpp12
1 files changed, 5 insertions, 7 deletions
diff --git a/Userland/Utilities/host.cpp b/Userland/Utilities/host.cpp
index 993f273721..dd6783ffb4 100644
--- a/Userland/Utilities/host.cpp
+++ b/Userland/Utilities/host.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/IPv4Address.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
@@ -23,14 +24,11 @@ ErrorOr<int> serenity_main(Main::Arguments args)
args_parser.parse(args);
// If input looks like an IPv4 address, we should do a reverse lookup.
- struct sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(53);
- int rc = inet_pton(AF_INET, name_or_ip, &addr.sin_addr);
- if (rc == 1) {
+ auto ip_address = IPv4Address::from_string(name_or_ip);
+ if (ip_address.has_value()) {
// Okay, let's do a reverse lookup.
- auto* hostent = gethostbyaddr(&addr.sin_addr, sizeof(in_addr), AF_INET);
+ auto addr = ip_address.value().to_in_addr_t();
+ auto* hostent = gethostbyaddr(&addr, sizeof(in_addr), AF_INET);
if (!hostent) {
warnln("Reverse lookup failed for '{}'", name_or_ip);
return 1;