The branch stable/14 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=0fe58344e8290637c91d70142578e92bd3d1e8f7
commit 0fe58344e8290637c91d70142578e92bd3d1e8f7 Author: Dag-Erling Smørgrav <d...@freebsd.org> AuthorDate: 2025-05-31 18:07:46 +0000 Commit: Dag-Erling Smørgrav <d...@freebsd.org> CommitDate: 2025-06-09 10:58:40 +0000 netcat: Allow service names to be used. Someone should really do a vendor import, but it's non-trivial, as we have local modifications. In the meantime, here's a nine-year-old upstream patch which allows service names to be used instead of port numbers. MFC after: 1 week Obtained from: OpenBSD Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D50348 (cherry picked from commit 6d3d1fc3a30453be19831f79399bcba0ae822ad1) --- contrib/netcat/nc.1 | 6 +++--- contrib/netcat/netcat.c | 42 +++++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/contrib/netcat/nc.1 b/contrib/netcat/nc.1 index 9f8696135f35..8db8dd4e6521 100644 --- a/contrib/netcat/nc.1 +++ b/contrib/netcat/nc.1 @@ -27,7 +27,7 @@ .\" .\" $FreeBSD$ .\" -.Dd January 17, 2023 +.Dd May 14, 2025 .Dt NC 1 .Os .Sh NAME @@ -341,8 +341,8 @@ sockets, a destination is required and is the socket path to connect to option is given). .Pp .Ar port -can be a single integer or a range of ports. -Ranges are in the form nn-mm. +can be a specified as a numeric port number, or as a service name. +Ports may be specified in a range of the form nn-mm. In general, a destination port must be specified, unless the diff --git a/contrib/netcat/netcat.c b/contrib/netcat/netcat.c index 65266b99c28e..63571975d233 100644 --- a/contrib/netcat/netcat.c +++ b/contrib/netcat/netcat.c @@ -117,6 +117,7 @@ char *portlist[PORT_MAX+1]; char *unix_dg_tmp_socket; void atelnet(int, unsigned char *, unsigned int); +int strtoport(char *portstr, int udp); void build_ports(char *); void help(void); int local_listen(char *, char *, struct addrinfo); @@ -1163,6 +1164,26 @@ atelnet(int nfd, unsigned char *buf, unsigned int size) } } +int +strtoport(char *portstr, int udp) +{ + struct servent *entry; + const char *errstr; + char *proto; + int port = -1; + + proto = udp ? "udp" : "tcp"; + + port = strtonum(portstr, 1, PORT_MAX, &errstr); + if (errstr == NULL) + return port; + if (errno != EINVAL) + errx(1, "port number %s: %s", errstr, portstr); + if ((entry = getservbyname(portstr, proto)) == NULL) + errx(1, "service \"%s\" unknown", portstr); + return ntohs(entry->s_port); +} + /* * build_ports() * Build an array of ports in portlist[], listing each port @@ -1171,7 +1192,6 @@ atelnet(int nfd, unsigned char *buf, unsigned int size) void build_ports(char *p) { - const char *errstr; char *n; int hi, lo, cp; int x = 0; @@ -1181,13 +1201,8 @@ build_ports(char *p) n++; /* Make sure the ports are in order: lowest->highest. */ - hi = strtonum(n, 1, PORT_MAX, &errstr); - if (errstr) - errx(1, "port number %s: %s", errstr, n); - lo = strtonum(p, 1, PORT_MAX, &errstr); - if (errstr) - errx(1, "port number %s: %s", errstr, p); - + hi = strtoport(n, uflag); + lo = strtoport(p, uflag); if (lo > hi) { cp = hi; hi = lo; @@ -1216,11 +1231,12 @@ build_ports(char *p) } } } else { - hi = strtonum(p, 1, PORT_MAX, &errstr); - if (errstr) - errx(1, "port number %s: %s", errstr, p); - portlist[0] = strdup(p); - if (portlist[0] == NULL) + char *tmp; + + hi = strtoport(p, uflag); + if (asprintf(&tmp, "%d", hi) != -1) + portlist[0] = tmp; + else err(1, NULL); } }