From 8c1dacecbafd15d36de3a201d29438af5245cd73 Mon Sep 17 00:00:00 2001 From: Fabian Dellwing Date: Fri, 14 Apr 2023 13:18:56 +0200 Subject: [PATCH] nc: Add -n option to suppress name resolution --- Base/usr/share/man/man1/nc.md | 3 ++- Userland/Utilities/nc.cpp | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Base/usr/share/man/man1/nc.md b/Base/usr/share/man/man1/nc.md index 1a31d5b6c35..3d94cc40de2 100644 --- a/Base/usr/share/man/man1/nc.md +++ b/Base/usr/share/man/man1/nc.md @@ -5,7 +5,7 @@ nc ## Synopsis ```sh -$ nc [--length ] [--listen] [-N] [--udp] [-p port] [--verbose] [target] [port] +$ nc [--length ] [--listen] [-N] [-n] [--udp] [-p port] [--verbose] [target] [port] ``` ## Description @@ -17,6 +17,7 @@ Network cat: Connect to network sockets as if it were a file. * `-I`, `--length`: Set maximum tcp receive buffer size * `-l`, `--listen`: Listen instead of connecting * `-N`: Close connection after reading stdin to the end +* `-n`: Suppress name resolution * `-u`, `--udp`: UDP mode * `-p port`: Local port for remote connections * `-v`, `--verbose`: Log everything that's happening diff --git a/Userland/Utilities/nc.cpp b/Userland/Utilities/nc.cpp index 7ab51b5f9c6..808468a31d5 100644 --- a/Userland/Utilities/nc.cpp +++ b/Userland/Utilities/nc.cpp @@ -50,6 +50,7 @@ ErrorOr serenity_main(Main::Arguments arguments) bool verbose = false; bool should_close = false; bool udp_mode = false; + bool numeric_mode = false; DeprecatedString target; int port = 0; int local_port = 0; @@ -60,6 +61,7 @@ ErrorOr serenity_main(Main::Arguments arguments) args_parser.add_option(maximum_tcp_receive_buffer_size_input, "Set maximum tcp receive buffer size", "length", 'I', nullptr); args_parser.add_option(should_listen, "Listen instead of connecting", "listen", 'l'); args_parser.add_option(should_close, "Close connection after reading stdin to the end", nullptr, 'N'); + args_parser.add_option(numeric_mode, "Suppress name resolution", nullptr, 'n'); args_parser.add_option(udp_mode, "UDP mode", "udp", 'u'); args_parser.add_option(local_port, "Local port for remote connections", nullptr, 'p', "port"); args_parser.add_option(verbose, "Log everything that's happening", "verbose", 'v'); @@ -144,16 +146,23 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Core::System::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout))); TRY(Core::System::setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout))); - auto* hostent = gethostbyname(target.characters()); - if (!hostent) { - warnln("Socket::connect: Unable to resolve '{}'", target); - return 1; - } - sockaddr_in dst_addr {}; dst_addr.sin_family = AF_INET; dst_addr.sin_port = htons(port); - dst_addr.sin_addr.s_addr = *(in_addr_t const*)hostent->h_addr_list[0]; + + if (!numeric_mode) { + auto* hostent = gethostbyname(target.characters()); + if (!hostent) { + warnln("nc: Unable to resolve '{}'", target); + return 1; + } + dst_addr.sin_addr.s_addr = *(in_addr_t const*)hostent->h_addr_list[0]; + } else { + if (inet_pton(AF_INET, target.characters(), &dst_addr.sin_addr) <= 0) { + perror("inet_pton"); + return 1; + } + } // FIXME: Actually use the local_port for the outgoing connection once we have a working implementation of bind and connect