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
This commit is contained in:
+3
-3
@@ -27,7 +27,7 @@
|
|||||||
.\"
|
.\"
|
||||||
.\" $FreeBSD$
|
.\" $FreeBSD$
|
||||||
.\"
|
.\"
|
||||||
.Dd January 20, 2025
|
.Dd May 14, 2025
|
||||||
.Dt NC 1
|
.Dt NC 1
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
@@ -351,8 +351,8 @@ sockets, a destination is required and is the socket path to connect to
|
|||||||
option is given).
|
option is given).
|
||||||
.Pp
|
.Pp
|
||||||
.Ar port
|
.Ar port
|
||||||
can be a single integer or a range of ports.
|
can be a specified as a numeric port number, or as a service name.
|
||||||
Ranges are in the form nn-mm.
|
Ports may be specified in a range of the form nn-mm.
|
||||||
In general,
|
In general,
|
||||||
a destination port must be specified,
|
a destination port must be specified,
|
||||||
unless the
|
unless the
|
||||||
|
|||||||
+29
-13
@@ -119,6 +119,7 @@ char *portlist[PORT_MAX+1];
|
|||||||
char *unix_dg_tmp_socket;
|
char *unix_dg_tmp_socket;
|
||||||
|
|
||||||
void atelnet(int, unsigned char *, unsigned int);
|
void atelnet(int, unsigned char *, unsigned int);
|
||||||
|
int strtoport(char *portstr, int udp);
|
||||||
void build_ports(char *);
|
void build_ports(char *);
|
||||||
void help(void);
|
void help(void);
|
||||||
int local_listen(char *, char *, struct addrinfo);
|
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_ports()
|
||||||
* Build an array of ports in portlist[], listing each port
|
* Build an array of ports in portlist[], listing each port
|
||||||
@@ -1205,7 +1226,6 @@ atelnet(int nfd, unsigned char *buf, unsigned int size)
|
|||||||
void
|
void
|
||||||
build_ports(char *p)
|
build_ports(char *p)
|
||||||
{
|
{
|
||||||
const char *errstr;
|
|
||||||
char *n;
|
char *n;
|
||||||
int hi, lo, cp;
|
int hi, lo, cp;
|
||||||
int x = 0;
|
int x = 0;
|
||||||
@@ -1215,13 +1235,8 @@ build_ports(char *p)
|
|||||||
n++;
|
n++;
|
||||||
|
|
||||||
/* Make sure the ports are in order: lowest->highest. */
|
/* Make sure the ports are in order: lowest->highest. */
|
||||||
hi = strtonum(n, 1, PORT_MAX, &errstr);
|
hi = strtoport(n, uflag);
|
||||||
if (errstr)
|
lo = strtoport(p, uflag);
|
||||||
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 (lo > hi) {
|
if (lo > hi) {
|
||||||
cp = hi;
|
cp = hi;
|
||||||
hi = lo;
|
hi = lo;
|
||||||
@@ -1250,11 +1265,12 @@ build_ports(char *p)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
hi = strtonum(p, 1, PORT_MAX, &errstr);
|
char *tmp;
|
||||||
if (errstr)
|
|
||||||
errx(1, "port number %s: %s", errstr, p);
|
hi = strtoport(p, uflag);
|
||||||
portlist[0] = strdup(p);
|
if (asprintf(&tmp, "%d", hi) != -1)
|
||||||
if (portlist[0] == NULL)
|
portlist[0] = tmp;
|
||||||
|
else
|
||||||
err(1, NULL);
|
err(1, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user