From nobody Sat May 31 18:08:38 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 4b8p5244zfz5xNYs; Sat, 31 May 2025 18:08:38 +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 4b8p52310Mz3sTf; Sat, 31 May 2025 18:08:38 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1748714918; 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=D2jd6AReDu4ELQ/LY+4HBvDIolRKdLH2eKGeU3T6gSg=; b=X7VhQIydHAC4/SQ0Ch+5iwXBIPeAQl82077Qj55oT9DrMePUJzjSdSAvRb1T0lr9eeRVID vTBsOKdBkp8rgJGwC9xBZZdKEcr7P1Rdh0eDKgqvwuCuPHsu/B+oQcn8vfJqM6M2+jD5w+ jSks9PDNSMKlk4Hff/PgPWRUomW7qmRVM2gN9GaZrazpKinFR3isHYlVaw6eWWBk6aks53 C5KM/MkU3UL4FC86r5YFCv/HQn1Fgc0wQ5HY31Ni6e/c1SIxoSB+FtoEnm0X8/aomaFga8 VyDvXmtyojJpnMd7OoZyiq/WVz3iLdiwmV3PL1DHBfzRbWqoA6GVPggPIxj+mw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1748714918; 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=D2jd6AReDu4ELQ/LY+4HBvDIolRKdLH2eKGeU3T6gSg=; b=BZ8AQHD591h6iJpvjRmNCaJ3o9zOXT1m4M1WEAZ8qPUTFoZDcaiC3LpPYdT9jE0rrfvA0I 2tTjTFGkzzaXMOfFYYln6nkeVBTE6/VZOe9L7mDyYMGOKfAgMhM1WzZi2ciGpm5/yR4rt7 jq2E4Uwfw+BJ+1696VAmAoAwikwC6Al8SSRSXM229EZYCRJ2fXI6seXmkPAb/4lHhf6lyE E17lQABVP4J4Gv7WBnzsXhkkCRKbulDA2voKyPvU8GLdHkxZ0s3kTczhhedGG3NTwLaMPX kt4azFWFf1hDs5/ZGwArjXB2VUrXaiTqIyCNxGRuATyLUeUlzVUS8Pte7irn6g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1748714918; a=rsa-sha256; cv=none; b=s3aQNKkrVLdUDAe6PsKayvWlHIc9BExPCRA4qSc3N29WgFGCh0xHkk0os1Tjob1oJJuWnm HB5vMVV2M5GWKBeYhfcxXN4K/sMzt1aYnqPWOj3I7SonP3OcSNorb+ox5W32nPWvZ9VezE VGSQIsNr7V3zMJDanBjk4gviNqn+FeQVej8VuH+L/Rdo8d2lXLEGyiK56+jdZfbeGdBNh2 x037/iV4201gEavYlTEVCPJRvIsGedxIgaRjH8NR1J9OTmR67ZAy1LW8iOqAS9KsEfSSwN 5XEMxEK/99+oWTPL1QUwt88W1a/AcaMc8qd8iZZbvGfePkgCKzriwq5pd/CGfA== 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 4b8p522VPjzpX8; Sat, 31 May 2025 18:08:38 +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 54VI8cu6025580; Sat, 31 May 2025 18:08:38 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 54VI8cdn025577; Sat, 31 May 2025 18:08:38 GMT (envelope-from git) Date: Sat, 31 May 2025 18:08:38 GMT Message-Id: <202505311808.54VI8cdn025577@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 6d3d1fc3a304 - main - 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/main X-Git-Reftype: branch X-Git-Commit: 6d3d1fc3a30453be19831f79399bcba0ae822ad1 Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=6d3d1fc3a30453be19831f79399bcba0ae822ad1 commit 6d3d1fc3a30453be19831f79399bcba0ae822ad1 Author: Dag-Erling Smørgrav AuthorDate: 2025-05-31 18:07:46 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2025-05-31 18:08:12 +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 --- 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 5801c6fa00e3..1519ed7b64d2 100644 --- a/contrib/netcat/nc.1 +++ b/contrib/netcat/nc.1 @@ -27,7 +27,7 @@ .\" .\" $FreeBSD$ .\" -.Dd January 20, 2025 +.Dd May 14, 2025 .Dt NC 1 .Os .Sh NAME @@ -351,8 +351,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 7035fdf68961..ff4031fc2bd0 100644 --- a/contrib/netcat/netcat.c +++ b/contrib/netcat/netcat.c @@ -119,6 +119,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); @@ -1197,6 +1198,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 @@ -1205,7 +1226,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; @@ -1215,13 +1235,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; @@ -1250,11 +1265,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); } }