mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
nc: Add -n option to suppress name resolution
This commit is contained in:
parent
7cfa108fad
commit
8c1dacecba
Notes:
sideshowbarker
2024-07-17 01:46:00 +09:00
Author: https://github.com/fdellwing Commit: https://github.com/SerenityOS/serenity/commit/8c1dacecba Pull-request: https://github.com/SerenityOS/serenity/pull/18364 Reviewed-by: https://github.com/ADKaster ✅ Reviewed-by: https://github.com/timschumi
2 changed files with 18 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue