From: Guilhem Moulin Date: Mon, 22 Oct 2018 04:50:54 +0200 Subject: use -s/-p flags to specify listen address --- nc.1 | 18 ++++++++++++++---- netcat.c | 49 +++++++++++++++++++++++++++++-------------------- 2 files changed, 43 insertions(+), 24 deletions(-) --- a/nc.1 +++ b/nc.1 @@ -143,8 +143,20 @@ multiple hosts. .It Fl l Listen for an incoming connection rather than initiating a connection to a remote host. -Cannot be used together with any of the options -.Fl psxz . +The +.Ar destination +and +.Ar port +to listen on can be specified either as non-optional arguments, or with +options +.Fl s +and +.Fl p +respectively. +Cannot be used together with +.Fl x +or +.Fl z . Additionally, any timeouts specified with the .Fl w option are ignored. @@ -194,8 +206,6 @@ For datagram sockets, specifies the local temporary socket file to create and use so that datagrams can be received. Cannot be used together with -.Fl l -or .Fl x . .It Fl T Ar keyword Change the IPv4 TOS/IPv6 traffic class value. --- a/netcat.c +++ b/netcat.c @@ -509,31 +509,40 @@ main(int argc, char *argv[]) # endif /* Cruft to make sure options are clean, and used properly. */ - if (argv[0] && !argv[1] && family == AF_UNIX) { -# if defined(IPPROTO_DCCP) && defined(SOCK_DCCP) - if (dccpflag) - errx(1, "cannot use -Z and -U"); -# endif - host = argv[0]; - uport = NULL; - } else if (!argv[0] && lflag) { - if (sflag) - errx(1, "cannot use -s and -l"); - if (pflag) - errx(1, "cannot use -p and -l"); - if (zflag) - errx(1, "cannot use -z and -l"); - } else if (argv[0] && !argv[1]) { - if (!lflag) - usage(1); - uport = &argv[0]; - host = NULL; - } else if (argv[0] && argv[1]) { + if (argc == 0 && lflag) { + uport = &pflag; + host = sflag; + } else if (argc == 1 && !pflag && !sflag) { + if (family == AF_UNIX) { + host = argv[0]; + uport = NULL; + } else if (lflag) { + host = NULL; + uport = argv; + } + } else if (argc >= 2) { + if (lflag && (pflag || sflag || argc > 2)) + usage(1); /* conflict */ host = argv[0]; uport = &argv[1]; } else usage(1); + if (family == AF_UNIX) { +# if defined(IPPROTO_DCCP) && defined(SOCK_DCCP) + if (dccpflag) + errx(1, "cannot use -Z and -U"); +# endif + if (uport && *uport) + errx(1, "cannot use port with -U"); + if (!host) + errx(1, "missing socket pathname"); + } else if (!uport || !*uport) + errx(1, "missing port number"); + + if (lflag && zflag) + errx(1, "cannot use -z and -l"); + # if defined(TLS) if (usetls) { if (Cflag && unveil(Cflag, "r") == -1)