Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
PATCH: Add some more ztcp options
- X-seq: zsh-workers 55261
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: PATCH: Add some more ztcp options
- Date: Fri, 13 Nov 2026 08:19:21 +0100
- Archived-at: <https://zsh.org/workers/55261>
- List-id: <zsh-workers.zsh.org>
-r for setting SO_REUSEADDR
-b for binding to a specific ip
-i for binding to a specific device (linux only)
Technically an empty argument for -i *is* allowed, but it has the same
effect as not passing -i at all, and disallowing it catches things like
-i "$iface" where $iface is mistakenly empty.
While adding them to the bintab entry, I noticed the maxargs was
accidentally 3 instead of 2 (host and port for outgoing connections) so
that's also fixed here.
Also convert all the tcp_close+return to goto to make it harder to
forget to close the session on these error paths.
---
I'm reasonably sure including net/if.h inside the SO_BINDTODEVICE ifdef
should be completely safe without adding any autoconf checks, but if
someone feels differently, let me know. It would probably be equally
safe to just hardcode 16. We could also not check it, the kernel will
silently truncate the name to 15 bytes, but that's a bit confusing.
And yes, the name can contain fairly arbitrary non-ascii bytes, so the
unmeta is necessary :).
Doc/Zsh/mod_tcp.yo | 17 +++++++++++-
Src/Modules/tcp.c | 65 ++++++++++++++++++++++++++++++++++++----------
Src/Modules/tcp.h | 4 +++
3 files changed, 72 insertions(+), 14 deletions(-)
diff --git a/Doc/Zsh/mod_tcp.yo b/Doc/Zsh/mod_tcp.yo
index 5dd1d5da1d..f72cce1d1b 100644
--- a/Doc/Zsh/mod_tcp.yo
+++ b/Doc/Zsh/mod_tcp.yo
@@ -89,7 +89,8 @@ subsect(Inbound Connections)
cindex(sockets, inbound TCP)
startitem()
-item(tt(ztcp) tt(-l) [ tt(-v) ] [ tt(-d) var(fd) ] var(port))(
+xitem(tt(ztcp) tt(-l) [ tt(-v) ] [ tt(-d) var(fd) ] [ tt(-b) var(address) ])
+item(SPACES()[ tt(-r) ] [ tt(-i) var(interface) ] var(port))(
tt(ztcp -l) will open a socket listening on TCP
var(port). The socket will be added to the
session table and the shell parameter tt(REPLY)
@@ -99,6 +100,20 @@ with that listener.
If tt(-d) is specified, its argument will be taken as the target file
descriptor for the connection.
+If tt(-b) is specified, its argument will be used instead of "0.0.0.0"
+as the host to bind to. var(address) has to be in numeric form.
+
+If tt(-i) is specified, SO_BINDTODEVICE is used to bind to the specified
+var(interface). Note that this socket option is Linux-only and may require root
+or CAP_NET_RAW on kernels before v5.7. It can be used in combination with -b
+which can be useful if, for example, an interface has multiple IP addresses.
+Note also that older tools like netstat will not show the bound device in their
+listings, but ss will.
+
+If tt(-r) is specified, the SO_REUSEADDR socket option is used, allowing
+binding to ports in the TIME_WAIT state when possible. If applying the option
+fails, or it is not supported, no error is reported.
+
In order to elicit more verbose output, use tt(-v).
)
item(tt(ztcp) tt(-a) [ tt(-tv) ] [ tt(-d) var(targetfd) ] var(listenfd))(
diff --git a/Src/Modules/tcp.c b/Src/Modules/tcp.c
index 36c3dca373..6855b134fa 100644
--- a/Src/Modules/tcp.c
+++ b/Src/Modules/tcp.c
@@ -367,7 +367,7 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
int herrno, err=1, destport, force=0, verbose=0, test=0, targetfd=0;
ZSOCKLEN_T len;
char **addrp, *desthost;
- const char *localname, *remotename;
+ const char *localname, *remotename, *opt;
struct hostent *zthost = NULL, *ztpeer = NULL;
struct servent *srv;
Tcp_session sess = NULL;
@@ -389,6 +389,13 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
}
}
+ for (opt = "rbi"; *opt; opt++) {
+ if (OPT_ISSET(ops,*opt) && !OPT_ISSET(ops,'l')) {
+ zwarnnam(nam, "-%c is only valid with -l", *opt);
+ return 1;
+ }
+ }
+
if (OPT_ISSET(ops,'s')) {
targetfd = atoi(args[0]);
if(!targetfd) {
@@ -437,6 +444,7 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
}
else if (OPT_ISSET(ops,'l')) {
int lport = 0;
+ char *address = OPT_ISSET(ops,'b') ? OPT_ARG(ops,'b') : "0.0.0.0";
if (!args[0]) {
zwarnnam(nam, "-l requires an argument");
@@ -461,10 +469,42 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
len = 1;
setsockopt(sess->fd, SOL_SOCKET, SO_OOBINLINE, (char *)&len, sizeof(len));
#endif
- if (!zsh_inet_aton("0.0.0.0", &(sess->sock.in.sin_addr)))
+ if (OPT_ISSET(ops,'r')) {
+#ifdef SO_REUSEADDR
+ len = 1;
+ setsockopt(sess->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&len, sizeof(len));
+#else
+ /* probably just annoying to warn about this */
+ /* zwarnnam(nam, "SO_REUSEADDR not available"); */
+#endif
+ }
+ if (OPT_ISSET(ops,'i')) {
+ char *iface = OPT_ARG(ops,'i');
+ if (!*iface) {
+ zwarnnam(nam, "argument can't be empty: -i");
+ goto l_opt_close_err;
+ }
+#ifdef SO_BINDTODEVICE
+ char *iface_unmeta = unmeta(iface);
+ if (strlen(iface_unmeta) >= IFNAMSIZ) {
+ zwarnnam(nam, "interface name too long (max %d bytes): %s",
+ IFNAMSIZ-1, iface);
+ goto l_opt_close_err;
+ }
+ if (setsockopt(sess->fd, SOL_SOCKET, SO_BINDTODEVICE,
+ iface_unmeta, strlen(iface_unmeta))) {
+ zwarnnam(nam, "could not bind to interface %s: %e", iface, errno);
+ goto l_opt_close_err;
+ }
+#else
+ zwarnnam(nam, "SO_BINDTODEVICE not available");
+ goto l_opt_close_err;
+#endif
+ }
+ if (!zsh_inet_aton(address, &(sess->sock.in.sin_addr)))
{
- zwarnnam(nam, "bad address: %s", "0.0.0.0");
- return 1;
+ zwarnnam(nam, "bad address: %s", address);
+ goto l_opt_close_err;
}
sess->sock.in.sin_family = AF_INET;
@@ -476,23 +516,20 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
char buf[DIGBUFSIZE];
convbase(buf, (zlong)ntohs(lport), 10);
zwarnnam(nam, "could not bind to port %s: %e", buf, errno);
- tcp_close(sess);
- return 1;
+ goto l_opt_close_err;
}
if (listen(sess->fd, 1))
{
zwarnnam(nam, "could not listen on socket: %e", errno);
- tcp_close(sess);
- return 1;
+ goto l_opt_close_err;
}
if (targetfd) {
sess->fd = redup(sess->fd, targetfd);
if (sess->fd < 0) {
zerrnam(nam, "could not duplicate socket fd to %d: %e", targetfd, errno);
- tcp_close(sess);
- return 1;
+ goto l_opt_close_err;
}
}
else {
@@ -502,8 +539,7 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
if (sess->fd == -1) {
zwarnnam(nam, "cannot duplicate fd %d: %e", targetfd, errno);
- tcp_close(sess);
- return 1;
+ goto l_opt_close_err;
}
/* disallow to be closed explicitly, after movefd set it to INTERNAL */
@@ -516,6 +552,9 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
return 0;
+l_opt_close_err:
+ tcp_close(sess);
+ return 1;
}
else if (OPT_ISSET(ops,'a'))
{
@@ -751,7 +790,7 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
}
static struct builtin bintab[] = {
- BUILTIN("ztcp", 0, bin_ztcp, 0, 3, 0, "acd:flLstv", NULL),
+ BUILTIN("ztcp", 0, bin_ztcp, 0, 2, 0, "ab:cd:fi:lLrstv", NULL),
};
static struct features module_features = {
diff --git a/Src/Modules/tcp.h b/Src/Modules/tcp.h
index a9ea7df2d1..1b0c95a602 100644
--- a/Src/Modules/tcp.h
+++ b/Src/Modules/tcp.h
@@ -38,6 +38,10 @@
#include <sys/types.h>
#include <sys/socket.h>
+#ifdef SO_BINDTODEVICE
+#include <net/if.h>
+#endif
+
#ifdef HAVE_BIND_NETDB_H
/*
* On systems where we're using -lbind, this has more definitions
--
2.38.1
Messages sorted by:
Reverse Date,
Date,
Thread,
Author