Patch to nc
Dave Edwards
busybox at dpe.lusars.net
Mon Nov 5 03:53:20 UTC 2007
I've written a small patch for nc. It adds a switch that causes nc to
return after the first read from network (instead of establishing a
connection).
Why this is useful for me (and potentially others): Some network protocols
(such as Tivo's home networking protocols) use a periodic broadcast UDP
message as a service locator (a "beacon"). The existing 'nc' will block
after the first such packet, expecting a UDP session to be established.
This patch adds a switch to tell it to write out the contents of the
buffer and return immediately.
I used the '-O' ('Oh', not 'zero') flag, for 'Only one packet', which is
(as far as I can tell) currently unused by both the BusyBox 'nc' and the
BSD 'nc'. It's enabled as part of the "USE_NC_EXTRA" ifdef.
Example:
$ ./busybox nc -l -u -v -p 2190
listening on [::]:2190 ...
connect to 192.168.1.255:2190 from TiVo:2190 ([::ffff:192.168.1.31]:2190)
tivoconnect=1
swversion=9.1-01-2-140
method=broadcast
identity=240000080XXXXXX
machine=Living Room
platform=tcd/Series2
services=TiVoMediaServer:80/http
[ ... time passes ... ]
punt! [ I ^C busybox ]
$ ./busybox nc -l -u -O -v -p 2190
listening on [::]:2190 ...
connect to 192.168.1.255:2190 from TiVo:2190 ([::ffff:192.168.1.31]:2190)
tivoconnect=1
swversion=9.1-01-2-140
method=broadcast
identity=240000080XXXXXX
machine=Living Room
platform=tcd/Series2
services=TiVoMediaServer:80/http
$
-------------- next part --------------
Index: networking/nc_bloaty.c
===================================================================
--- networking/nc_bloaty.c (revision 20365)
+++ networking/nc_bloaty.c (working copy)
@@ -1,3 +1,4 @@
+/* vi: set sw=4 ts=4: */
/* Based on netcat 1.10 RELEASE 960320 written by hobbit at avian.org.
* Released into public domain by the author.
*
@@ -129,6 +130,7 @@
OPT_i = (1 << (7+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
OPT_o = (1 << (8+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
OPT_z = (1 << (9+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
+ OPT_O = (1 << (10+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA,
};
#define o_nflag (option_mask32 & OPT_n)
@@ -141,9 +143,11 @@
#if ENABLE_NC_EXTRA
#define o_ofile (option_mask32 & OPT_o)
#define o_zero (option_mask32 & OPT_z)
+#define o_onlyone (option_mask32 & OPT_O)
#else
#define o_ofile 0
#define o_zero 0
+#define o_onlyone 0
#endif
/* Debug: squirt whatever message and sleep a bit so we can see it go by. */
@@ -584,6 +588,16 @@
} else {
rnleft = rr;
np = bigbuf_net;
+#if ENABLE_NC_EXTRA
+ /* The following is a special case intended mostly for UDP listeners,
+ * where the contents of the first packet get printed and then the
+ * connection gets closed. Useful for tracking broadcast UDP beacons. */
+ if(o_onlyone) { /* Special case where we just want the first packet */
+ rr = write(1, np, rnleft); /* don't care what write returns */
+ close(netfd);
+ return 0;
+ }
+#endif
}
Debug("got %d from the net, errno %d", rr, errno);
} /* net:ding */
@@ -707,7 +721,7 @@
// -g -G -t -r deleted, unimplemented -a deleted too
opt_complementary = "?2:vv"; /* max 2 params, -v is a counter */
getopt32(argv, "hnp:s:uvw:" USE_NC_SERVER("l")
- USE_NC_EXTRA("i:o:z"),
+ USE_NC_EXTRA("i:o:zO"),
&str_p, &str_s, &str_w
USE_NC_EXTRA(, &str_i, &str_o, &o_verbose));
argv += optind;
Index: include/usage.h
===================================================================
--- include/usage.h (revision 20365)
+++ include/usage.h (working copy)
@@ -2494,6 +2494,7 @@
USE_NC_EXTRA( \
"\n (use -l twice with -e for persistent server)") \
"\n -p PORT Local port number" \
+ "\n -O Only print the first packet, then exit (use with -l)"\
)
#define nc_notes_usage "" \
More information about the busybox
mailing list