Evening!
This diff allows users to use the name of a service in /etc/services
instead of a port number when using netcat. Hopefully, this will make
using netcat easier for some users.
? nc
Index: nc.1
===================================================================
RCS file: /cvs/src/usr.bin/nc/nc.1,v
retrieving revision 1.71
diff -u -p -r1.71 nc.1
--- nc.1 25 Sep 2015 14:56:33 -0000 1.71
+++ nc.1 1 Jun 2016 02:56:09 -0000
@@ -51,7 +51,7 @@
.Op Fl X Ar proxy_protocol
.Op Fl x Ar proxy_address Ns Op : Ns Ar port
.Op Ar destination
-.Op Ar port
+.Op Ar port | service
.Sh DESCRIPTION
The
.Nm
@@ -344,6 +344,10 @@ a destination port must be specified,
unless the
.Fl U
option is given.
+.Pp
+.Ar service
+is the name of a service in
+.Pa /etc/services
.Sh CLIENT/SERVER MODEL
It is quite simple to build a very basic client/server model using
.Nm .
Index: netcat.c
===================================================================
RCS file: /cvs/src/usr.bin/nc/netcat.c,v
retrieving revision 1.152
diff -u -p -r1.152 netcat.c
--- netcat.c 28 May 2016 20:14:58 -0000 1.152
+++ netcat.c 1 Jun 2016 02:56:09 -0000
@@ -1291,22 +1291,43 @@ atelnet(int nfd, unsigned char *buf, uns
void
build_ports(char *p)
{
+ struct servent *entry;
const char *errstr;
char *n;
+ char *proto;
int hi, lo, cp;
int x = 0;
+ if (uflag == 1)
+ proto = "udp";
+ else
+ proto = "tcp";
+
if ((n = strchr(p, '-')) != NULL) {
*n = '\0';
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);
+ if (errstr) {
+ if (errno == EINVAL) {
+ entry = getservbyname(n, proto);
+ if (entry == NULL)
+ errx(1, "service \"%s\" unknown", n);
+ hi = ntohs(entry->s_port);
+ } else
+ 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);
+ if (errstr) {
+ if (errno == EINVAL) {
+ entry = getservbyname(p, proto);
+ if (entry == NULL)
+ errx(1, "service \"%s\" unknown", p);
+ hi = ntohs(entry->s_port);
+ } else
+ errx(1, "port number %s: %s", errstr, p);
+ }
if (lo > hi) {
cp = hi;
@@ -1334,8 +1355,15 @@ build_ports(char *p)
}
} else {
hi = strtonum(p, 1, PORT_MAX, &errstr);
- if (errstr)
- errx(1, "port number %s: %s", errstr, p);
+ if (errstr) {
+ if (errno == EINVAL) {
+ entry = getservbyname(p, proto);
+ if (entry == NULL)
+ errx(1, "service \"%s\" unknown", p);
+ hi = ntohs(entry->s_port);
+ } else
+ errx(1, "port number %s: %s", errstr, p);
+ }
portlist[0] = strdup(p);
if (portlist[0] == NULL)
err(1, NULL);