File use-flags-to-specify-listen-address.patch of Package netcat-openbsd

From: Guilhem Moulin <guilhem@debian.org>
Date: Mon, 22 Oct 2018 04:50:54 +0200
Subject: Add ability to use -s/-p flags to specify listening address

---
 nc.1     | 18 ++++++++++++++----
 netcat.c | 47 +++++++++++++++++++++++++++++++----------------
 2 files changed, 45 insertions(+), 20 deletions(-)

diff --git a/nc.1 b/nc.1
index 80c654c..d30389a 100644
--- 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.
@@ -193,8 +205,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.
diff --git a/netcat.c b/netcat.c
index bef27a4..0caba28 100644
--- a/netcat.c
+++ b/netcat.c
@@ -507,27 +507,42 @@ main(int argc, char *argv[])
 #endif
 
 	/* Cruft to make sure options are clean, and used properly. */
-	if (argc == 1 && family == AF_UNIX) {
-#if defined(IPPROTO_DCCP) && defined(SOCK_DCCP)
-		if (dccpflag)
-			errx(1, "cannot use -Z and -U");
-#endif
-		host = argv[0];
-	} else if (argc == 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 (argc == 1 && lflag) {
-		uport = &argv[0];
-	} else if (argc == 2) {
+	if (argc == 0 && lflag) {
+		uport = &pflag;
+		host = sflag;
+	} else if (argc == 1 && !pflag &&
+			/* `nc -l 12345` or `nc -U bar` or `nc -uU -s foo bar` */
+			(!sflag || (family == AF_UNIX && uflag && !lflag))) {
+		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");
+
 #ifdef HAVE_TLS
 	if (usetls) {
 		if (Cflag && unveil(Cflag, "r") == -1)
openSUSE Build Service is sponsored by