Ver código fonte

nc: Add -n option to suppress name resolution

Fabian Dellwing 2 anos atrás
pai
commit
8c1dacecba
2 arquivos alterados com 18 adições e 8 exclusões
  1. 2 1
      Base/usr/share/man/man1/nc.md
  2. 16 7
      Userland/Utilities/nc.cpp

+ 2 - 1
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

+ 16 - 7
Userland/Utilities/nc.cpp

@@ -50,6 +50,7 @@ ErrorOr<int> 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<int> 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<int> 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