1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:04:59 +00:00

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.
This commit is contained in:
brapru 2022-04-16 16:46:28 -04:00 committed by Brian Gianforcaro
parent 9191829a39
commit 8b370f988b

View file

@ -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;