summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbeck <>2016-06-02 04:26:32 +0000
committerbeck <>2016-06-02 04:26:32 +0000
commit0f1fbd28fc3dcb2d7c4fadb5bcb0c31f24f099f3 (patch)
tree611f67f0e2433c08b97a4c8fa4049fb875733a56 /src
parentb5e812d88519f28137ac990c141e23bb5d90d6b5 (diff)
downloadopenbsd-0f1fbd28fc3dcb2d7c4fadb5bcb0c31f24f099f3.tar.gz
openbsd-0f1fbd28fc3dcb2d7c4fadb5bcb0c31f24f099f3.tar.bz2
openbsd-0f1fbd28fc3dcb2d7c4fadb5bcb0c31f24f099f3.zip
Let netcat support the use of service names instead of port numbers.
based on a diff from Andras Farkas <deepbluemistake@gmail.com> ok deraadt@
Diffstat (limited to 'src')
-rw-r--r--src/usr.bin/nc/nc.18
-rw-r--r--src/usr.bin/nc/netcat.c44
2 files changed, 34 insertions, 18 deletions
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 @@
1.\" $OpenBSD: nc.1,v 1.71 2015/09/25 14:56:33 schwarze Exp $ 1.\" $OpenBSD: nc.1,v 1.72 2016/06/02 04:26:32 beck Exp $
2.\" 2.\"
3.\" Copyright (c) 1996 David Sacerdote 3.\" Copyright (c) 1996 David Sacerdote
4.\" All rights reserved. 4.\" All rights reserved.
@@ -25,7 +25,7 @@
25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27.\" 27.\"
28.Dd $Mdocdate: September 25 2015 $ 28.Dd $Mdocdate: June 2 2016 $
29.Dt NC 1 29.Dt NC 1
30.Os 30.Os
31.Sh NAME 31.Sh NAME
@@ -337,8 +337,8 @@ sockets, a destination is required and is the socket path to connect to
337option is given). 337option is given).
338.Pp 338.Pp
339.Ar port 339.Ar port
340can be a single integer or a range of ports. 340can be a specified as a numeric port number, or as a service name.
341Ranges are in the form nn-mm. 341Ports may be specified in a range of the form nn-mm.
342In general, 342In general,
343a destination port must be specified, 343a destination port must be specified,
344unless the 344unless 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 @@
1/* $OpenBSD: netcat.c,v 1.152 2016/05/28 20:14:58 beck Exp $ */ 1/* $OpenBSD: netcat.c,v 1.153 2016/06/02 04:26:32 beck Exp $ */
2/* 2/*
3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> 3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
4 * Copyright (c) 2015 Bob Beck. All rights reserved. 4 * Copyright (c) 2015 Bob Beck. All rights reserved.
@@ -1283,6 +1283,27 @@ atelnet(int nfd, unsigned char *buf, unsigned int size)
1283 } 1283 }
1284} 1284}
1285 1285
1286
1287int
1288strtoport(char *portstr, int udp)
1289{
1290 struct servent *entry;
1291 const char *errstr;
1292 char *proto;
1293 int port = -1;
1294
1295 proto = udp ? "udp" : "tcp";
1296
1297 port = strtonum(portstr, 1, PORT_MAX, &errstr);
1298 if (errstr == NULL)
1299 return port;
1300 if (errno != EINVAL)
1301 errx(1, "port number %s: %s", errstr, portstr);
1302 if ((entry = getservbyname(portstr, proto)) == NULL)
1303 errx(1, "service \"%s\" unknown", portstr);
1304 return ntohs(entry->s_port);
1305}
1306
1286/* 1307/*
1287 * build_ports() 1308 * build_ports()
1288 * Build an array of ports in portlist[], listing each port 1309 * Build an array of ports in portlist[], listing each port
@@ -1291,7 +1312,6 @@ atelnet(int nfd, unsigned char *buf, unsigned int size)
1291void 1312void
1292build_ports(char *p) 1313build_ports(char *p)
1293{ 1314{
1294 const char *errstr;
1295 char *n; 1315 char *n;
1296 int hi, lo, cp; 1316 int hi, lo, cp;
1297 int x = 0; 1317 int x = 0;
@@ -1301,13 +1321,8 @@ build_ports(char *p)
1301 n++; 1321 n++;
1302 1322
1303 /* Make sure the ports are in order: lowest->highest. */ 1323 /* Make sure the ports are in order: lowest->highest. */
1304 hi = strtonum(n, 1, PORT_MAX, &errstr); 1324 hi = strtoport(n, uflag);
1305 if (errstr) 1325 lo = strtoport(p, uflag);
1306 errx(1, "port number %s: %s", errstr, n);
1307 lo = strtonum(p, 1, PORT_MAX, &errstr);
1308 if (errstr)
1309 errx(1, "port number %s: %s", errstr, p);
1310
1311 if (lo > hi) { 1326 if (lo > hi) {
1312 cp = hi; 1327 cp = hi;
1313 hi = lo; 1328 hi = lo;
@@ -1333,11 +1348,12 @@ build_ports(char *p)
1333 } 1348 }
1334 } 1349 }
1335 } else { 1350 } else {
1336 hi = strtonum(p, 1, PORT_MAX, &errstr); 1351 char *tmp;
1337 if (errstr) 1352
1338 errx(1, "port number %s: %s", errstr, p); 1353 hi = strtoport(p, uflag);
1339 portlist[0] = strdup(p); 1354 if (asprintf(&tmp, "%d", hi) != -1)
1340 if (portlist[0] == NULL) 1355 portlist[0] = tmp;
1356 else
1341 err(1, NULL); 1357 err(1, NULL);
1342 } 1358 }
1343} 1359}