From 98f982d8b9fce9463dd4862237b574893cf6d6f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Sun, 28 Jan 2024 05:49:06 +0100 Subject: [PATCH] netcat: Implement zero I/O mode --- toys/net/netcat.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/toys/net/netcat.c b/toys/net/netcat.c index 3783b821..4e76fa7f 100644 --- a/toys/net/netcat.c +++ b/toys/net/netcat.c @@ -4,7 +4,7 @@ * * TODO: genericize for telnet/microcom/tail-f, fix -t -USE_NETCAT(NEWTOY(netcat, "^tElLw#<1W#<1p#<1>65535q#<1s:f:46uUn[!tlL][!Lw][!Lu][!46U]", TOYFLAG_BIN)) +USE_NETCAT(NEWTOY(netcat, "^tElLw#<1W#<1p#<1>65535q#<1s:f:46uUnvz[!tlL][!Lw][!Lu][!46U]", TOYFLAG_BIN)) USE_NETCAT(OLDTOY(nc, netcat, TOYFLAG_USR|TOYFLAG_BIN)) config NETCAT @@ -26,10 +26,12 @@ config NETCAT -q Quit SECONDS after EOF on stdin, even if stdout hasn't closed yet -s Local source address -t Allocate tty + -v Verbose -u Use UDP -U Use a UNIX domain socket -W SECONDS timeout for more data on an idle connection -w SECONDS timeout to establish connection + -z zero-I/O mode [used for scanning] When listening the COMMAND line is executed as a child process to handle an incoming connection. With no COMMAND -l forwards the connection @@ -106,9 +108,15 @@ void netcat_main(void) else { // Setup socket if (!FLAG(l) && !FLAG(L)) { - if (FLAG(U)) sockfd = usock(toys.optargs[0], type, 1); - else sockfd = xconnectany(xgetaddrinfo(toys.optargs[0], - toys.optargs[1], family, type, 0, AI_NUMERICHOST*FLAG(n))); + char *host = toys.optargs[0]; + char *port = toys.optargs[1]; + if (FLAG(U)) sockfd = usock(host, type, 1); + else sockfd = xconnectany(xgetaddrinfo(host, port, family, type, 0, AI_NUMERICHOST*FLAG(n))); + + if (FLAG(v)) printf("%s:%s [open]\n", host, port); + + // Do not perform any I/O in zero mode + if (FLAG(z)) goto cleanup; // We have a connection. Disarm timeout and start poll/send loop. alarm(0); -- 2.39.2