From 0f1fbd28fc3dcb2d7c4fadb5bcb0c31f24f099f3 Mon Sep 17 00:00:00 2001 From: beck <> Date: Thu, 2 Jun 2016 04:26:32 +0000 Subject: Let netcat support the use of service names instead of port numbers. based on a diff from Andras Farkas ok deraadt@ --- src/usr.bin/nc/nc.1 | 8 ++++---- src/usr.bin/nc/netcat.c | 44 ++++++++++++++++++++++++++++++-------------- 2 files changed, 34 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/usr.bin/nc/nc.1 b/src/usr.bin/nc/nc.1 index 6b14122f3d..ee57a64a6b 100644 --- a/src/usr.bin/nc/nc.1 +++ b/src/usr.bin/nc/nc.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: nc.1,v 1.71 2015/09/25 14:56:33 schwarze Exp $ +.\" $OpenBSD: nc.1,v 1.72 2016/06/02 04:26:32 beck Exp $ .\" .\" Copyright (c) 1996 David Sacerdote .\" All rights reserved. @@ -25,7 +25,7 @@ .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.Dd $Mdocdate: September 25 2015 $ +.Dd $Mdocdate: June 2 2016 $ .Dt NC 1 .Os .Sh NAME @@ -337,8 +337,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/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index 95f276bc89..cf8a87d883 100644 --- a/src/usr.bin/nc/netcat.c +++ b/src/usr.bin/nc/netcat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: netcat.c,v 1.152 2016/05/28 20:14:58 beck Exp $ */ +/* $OpenBSD: netcat.c,v 1.153 2016/06/02 04:26:32 beck Exp $ */ /* * Copyright (c) 2001 Eric Jackson * Copyright (c) 2015 Bob Beck. All rights reserved. @@ -1283,6 +1283,27 @@ 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 @@ -1291,7 +1312,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; @@ -1301,13 +1321,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; @@ -1333,11 +1348,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); } } -- cgit v1.2.3-55-g6feb