The other day I was trying to send a syslog UDP packet
via netcat. Out of habit I was running it with the -v flag
and noticed that it corrupted my syslog packet with three 'X'
characters.

Turns out verbose mode enables udptest() which is meant
to get feedback if the "connection" is successful and thus sends
three 'X' as probes.

I suggest we only enable udptest, if nc(1) is run with stdin as input.
We might also want to document that behavior.

In addition, I wanted to send a huge UDP packet (~64k) for testing.
This wasn't possible, because the write(2) happens with only 16k.
Bumping BUFSIZE fixes that.

Cheers,
  Marco

Index: netcat.c
===================================================================
RCS file: /cvs/src/usr.bin/nc/netcat.c,v
retrieving revision 1.219
diff -u -p -p -u -r1.219 netcat.c
--- netcat.c    8 Jun 2022 20:07:31 -0000       1.219
+++ netcat.c    1 Dec 2022 20:34:40 -0000
@@ -66,7 +66,7 @@
 #define POLL_NETOUT    1
 #define POLL_NETIN     2
 #define POLL_STDOUT    3
-#define BUFSIZE                16384
+#define BUFSIZE                65536
 
 #define TLS_NOVERIFY   (1 << 1)
 #define TLS_NONAME     (1 << 2)
@@ -702,7 +702,7 @@ main(int argc, char *argv[])
                        ret = 0;
                        if (vflag || zflag) {
                                /* For UDP, make sure we are connected. */
-                               if (uflag) {
+                               if (uflag && isatty(STDIN_FILENO)) {
                                        if (udptest(s) == -1) {
                                                ret = 1;
                                                continue;

Reply via email to