From 6cd7b7dbd9ddacf7713c17cc6dcea70755becf39 Mon Sep 17 00:00:00 2001 From: Stefano Garzarella Date: Wed, 20 Mar 2024 18:57:50 +0100 Subject: [PATCH] Fix support for `ncat --vsock -l ` Commit 04c4c70c7 ("Support decimal integer representation of target addresses. Fixes #375") introduced a regression when the user specifies only the port using AF_VSOCK in listening (e.g. `ncat --vsock -l 1234`). Before that commit, we supported this invocation, but currently we interpret the parameter as CID. Alternatively, the version with -p is still working (e.g. `ncat --vsock -l -p 1234`). Having documented this usage in the manpage as well, it's better to fix it and support it again. Tested locally (without VMs involved), using CID = 1 (loopback) in this way: shell1$ ncat --vsock -l 1234 shell2$ ncat --vsock 1 1234 # Note: If CID 1 is not supported and the previous command fails, # it means that you have to manually load `vsock_loopback` kernel # module (usually it is autoamtically loaded when AF_VSOCK is used # if the `vsock` kernel module is not already loaded in the system) Reported-by: Luigi Leonardi Signed-off-by: Stefano Garzarella --- ncat/ncat_main.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ncat/ncat_main.c b/ncat/ncat_main.c index 45507d14c6..595e453811 100644 --- a/ncat/ncat_main.c +++ b/ncat/ncat_main.c @@ -857,6 +857,16 @@ int main(int argc, char *argv[]) break; } #endif + /* Support ncat -l , but otherwise assume ncat */ + if (num_ports == 0 && o.listen) { + rc = strspn(argv[optind], "1234567890"); + /* If the last arg is 5 or fewer digits, assume it's a port number */ + if (argv[optind][rc] == '\0' && rc <= 5) { + o.portno = parseport(argv[optind], max_port, "port"); + num_ports++; + break; + } + } #if HAVE_LINUX_VM_SOCKETS_H if (o.af == AF_VSOCK) { long long_cid; @@ -875,16 +885,6 @@ int main(int argc, char *argv[]) break; } #endif - /* Support ncat -l , but otherwise assume ncat */ - if (num_ports == 0 && o.listen) { - rc = strspn(argv[optind], "1234567890"); - /* If the last arg is 5 or fewer digits, assume it's a port number */ - if (argv[optind][rc] == '\0' && rc <= 5) { - o.portno = parseport(argv[optind], max_port, "port"); - num_ports++; - break; - } - } o.target = argv[optind]; /* resolve hostname only if o.proxytype == NULL * targetss contains data already and you don't want remove them