From nobody Mon Jun 9 10:58:49 2025 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4bG86x74jHz5xxYC; Mon, 09 Jun 2025 10:58:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bG86x5c6nz4Hcb; Mon, 09 Jun 2025 10:58:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1749466729; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=v0zxmuBWJ2eKBv0FsDT4G2Ead0Uf2NbAeIJs//SBNck=; b=I9J5jqcrYu+NAwY+lCJF5XCbwCPAByCjBBuYhQ/OUOjUySCcyONo7pV970OO7CNnCz/NF6 BMTlGhvQWx/0q1hky/kNtrIl/m5g+5brZnGvAKlnb59t8Qd5OyZFJvaNZGXAVNdYjOFAUy 0QOWZfUVnWotlex6o+Fcw5tx3XwqBkCJIFTHsLKfAdiNeGEv/LxPQCYY3qGUZ4Jw4lEtMz uT1HVPh0SqRikDT84FoV7KnYW4BAb16W5G020lFFEAB0NVjvf6Dyo0wqZoak+F0sWhys9B h5aNE7d+FUd+XzgYiTuArD4BlUMxsJUBQg+51tJzVoVi6WVkOe2hMGxTMt5EIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1749466729; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=v0zxmuBWJ2eKBv0FsDT4G2Ead0Uf2NbAeIJs//SBNck=; b=Sf4OBjK9Cddnc68V9fkRQ9lHyJy+V4P0A+MCZ21Ec/D3krs8KN4AR9iUrxuo0HT7G4yp7V XMnSmcqZ1g4ch16QDi4IozasefbZ/PQraEgEXkxoZPxFYX/qtwNqoqAbZ51+ALR1PnnLW7 xiOBuEI4hhc3MOO/44eMKGIWv9vnP7qv55zu+TGihRJFgKp/NwI33TgC9WhZUtkMP3E0+g q7W9iYpsHpoZd0TqhjYDgWKvKR42THanoNmVaQ6AjMEHTFZ4zt48U7S2dk2tqIbCCNChCd WvG1apA8qbPJ5GYidrDIUueC3znGqI8peks04ztb1fvPfpMt/eObixHYqjIzwA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1749466729; a=rsa-sha256; cv=none; b=YS0TpgA6StJhrbckJwbKiK9+E4Hw70DnNTjk8x6eTOFserX9F58OaqhSU3Q82VtviFyEyw XXED2XNsA92BowBQ038KXglkjy2teZnSwVxRbtdXGFFU2mNrAI4rpGmXPtFUqJOfVZFhWd FqIOZqDQEE8ezBYfwEhed2f6M8vRVnFFYap4+8dbIbiKlSgyWzBw8zXJtk1pifYjp1GxMS GWZfTu7g7SfRzggtT7OAVg/inVdKm1oEN7mjx+/4rGBAeExLpHX+/graaPCp4DMoG2Hw+V D3Xu/x/mnLIc/a3uBOkY+DY7eHagha4etS3XNPrpQKGYocgueaKQH8AEPK7IMw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4bG86x58WPzslZ; Mon, 09 Jun 2025 10:58:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 559Awn3Y044948; Mon, 9 Jun 2025 10:58:49 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 559Awn2R044945; Mon, 9 Jun 2025 10:58:49 GMT (envelope-from git) Date: Mon, 9 Jun 2025 10:58:49 GMT Message-Id: <202506091058.559Awn2R044945@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 0fe58344e829 - stable/14 - netcat: Allow service names to be used. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 0fe58344e8290637c91d70142578e92bd3d1e8f7 Auto-Submitted: auto-generated 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 AuthorDate: 2025-05-31 18:07:46 +0000 Commit: Dag-Erling Smørgrav 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); } }