Skip to content

Commit

Permalink
Fix support for ncat --vsock -l <port>
Browse files Browse the repository at this point in the history
Commit 04c4c70 ("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 <[email protected]>
Signed-off-by: Stefano Garzarella <[email protected]>
  • Loading branch information
stefano-garzarella committed Mar 21, 2024
1 parent 00d71a8 commit 6cd7b7d
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions ncat/ncat_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,16 @@ int main(int argc, char *argv[])
break;
}
#endif
/* Support ncat -l <port>, but otherwise assume ncat <target> */
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;
Expand All @@ -875,16 +885,6 @@ int main(int argc, char *argv[])
break;
}
#endif
/* Support ncat -l <port>, but otherwise assume ncat <target> */
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
Expand Down

0 comments on commit 6cd7b7d

Please sign in to comment.