2017-07-17 15:26:12 +02:00
|
|
|
From: Aron Xu <aron@debian.org>
|
|
|
|
Date: Mon, 13 Feb 2012 15:56:51 +0800
|
|
|
|
Subject: dccp support
|
|
|
|
|
|
|
|
---
|
|
|
|
nc.1 | 4 ++
|
2018-11-14 14:18:05 +01:00
|
|
|
netcat.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++----------
|
|
|
|
2 files changed, 79 insertions(+), 14 deletions(-)
|
2017-07-17 15:26:12 +02:00
|
|
|
|
|
|
|
--- a/nc.1
|
|
|
|
+++ b/nc.1
|
|
|
|
@@ -33,7 +33,7 @@
|
|
|
|
.Nd arbitrary TCP and UDP connections and listens
|
|
|
|
.Sh SYNOPSIS
|
|
|
|
.Nm nc
|
|
|
|
-.Op Fl 46CDdFhklNnrStUuvz
|
|
|
|
+.Op Fl 46CDdFhklNnrStUuvZz
|
|
|
|
.Op Fl I Ar length
|
|
|
|
.Op Fl i Ar interval
|
|
|
|
.Op Fl M Ar ttl
|
2018-11-14 14:18:05 +01:00
|
|
|
@@ -289,6 +289,8 @@ An IPv6 address can be specified unambig
|
2017-07-17 15:26:12 +02:00
|
|
|
in square brackets.
|
2018-11-14 14:18:05 +01:00
|
|
|
A proxy cannot be used with any of the options
|
|
|
|
.Fl lsuU .
|
2017-07-17 15:26:12 +02:00
|
|
|
+.It Fl Z
|
|
|
|
+DCCP mode.
|
|
|
|
.It Fl z
|
2018-11-14 14:18:05 +01:00
|
|
|
Only scan for listening daemons, without sending any data to them.
|
|
|
|
Cannot be used together with
|
2017-07-17 15:26:12 +02:00
|
|
|
--- a/netcat.c
|
|
|
|
+++ b/netcat.c
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -146,6 +146,7 @@ int rflag; /* Random ports flag */
|
2017-07-17 15:26:12 +02:00
|
|
|
char *sflag; /* Source Address */
|
|
|
|
int tflag; /* Telnet Emulation */
|
|
|
|
int uflag; /* UDP - Default to TCP */
|
|
|
|
+int dccpflag; /* DCCP - Default to TCP */
|
|
|
|
int vflag; /* Verbosity */
|
|
|
|
int xflag; /* Socks proxy */
|
|
|
|
int zflag; /* Port Scan Flag */
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -224,6 +225,7 @@ ssize_t drainbuf(int, unsigned char *, s
|
2017-07-17 15:26:12 +02:00
|
|
|
ssize_t fillbuf(int, unsigned char *, size_t *);
|
|
|
|
# endif
|
|
|
|
|
|
|
|
+char *proto_name(int uflag, int dccpflag);
|
|
|
|
static int connect_with_timeout(int fd, const struct sockaddr *sa,
|
|
|
|
socklen_t salen, int ctimeout);
|
|
|
|
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -263,7 +265,7 @@ main(int argc, char *argv[])
|
2017-07-17 15:26:12 +02:00
|
|
|
# if defined(TLS)
|
2018-11-14 14:18:05 +01:00
|
|
|
"46C:cDde:FH:hI:i:K:klM:m:NnO:o:P:p:q:R:rSs:T:tUuV:vW:w:X:x:Z:z"))
|
2017-07-17 15:26:12 +02:00
|
|
|
# else
|
2018-11-14 14:18:05 +01:00
|
|
|
- "46CDdFhI:i:klM:m:NnO:P:p:q:rSs:T:tUuV:vW:w:X:x:z"))
|
|
|
|
+ "46CDdFhI:i:klM:m:NnO:P:p:q:rSs:T:tUuV:vW:w:X:x:Zz"))
|
2017-07-17 15:26:12 +02:00
|
|
|
# endif
|
2018-11-14 14:18:05 +01:00
|
|
|
!= -1) {
|
2017-07-17 15:26:12 +02:00
|
|
|
switch (ch) {
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -380,6 +382,13 @@ main(int argc, char *argv[])
|
2017-07-17 15:26:12 +02:00
|
|
|
case 'u':
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
|
|
|
+ case 'Z':
|
|
|
|
+# if defined(IPPROTO_DCCP) && defined(SOCK_DCCP)
|
|
|
|
+ dccpflag = 1;
|
|
|
|
+# else
|
|
|
|
+ errx(1, "no DCCP support available");
|
|
|
|
+# endif
|
|
|
|
+ break;
|
|
|
|
case 'V':
|
|
|
|
# if defined(RT_TABLEID_MAX)
|
|
|
|
rtableid = (int)strtonum(optarg, 0,
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -484,6 +493,10 @@ main(int argc, char *argv[])
|
2017-07-17 15:26:12 +02:00
|
|
|
|
|
|
|
/* Cruft to make sure options are clean, and used properly. */
|
|
|
|
if (argv[0] && !argv[1] && family == AF_UNIX) {
|
|
|
|
+# if defined(IPPROTO_DCCP) && defined(SOCK_DCCP)
|
|
|
|
+ if (dccpflag)
|
|
|
|
+ errx(1, "cannot use -Z and -U");
|
|
|
|
+# endif
|
|
|
|
host = argv[0];
|
|
|
|
uport = NULL;
|
|
|
|
} else if (!argv[0] && lflag) {
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -577,8 +590,20 @@ main(int argc, char *argv[])
|
2017-07-17 15:26:12 +02:00
|
|
|
if (family != AF_UNIX) {
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = family;
|
|
|
|
- hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
|
|
|
|
- hints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
|
|
|
|
+ if (uflag) {
|
|
|
|
+ hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
+ hints.ai_protocol = IPPROTO_UDP;
|
|
|
|
+ }
|
|
|
|
+# if defined(IPPROTO_DCCP) && defined(SOCK_DCCP)
|
|
|
|
+ else if (dccpflag) {
|
|
|
|
+ hints.ai_socktype = SOCK_DCCP;
|
|
|
|
+ hints.ai_protocol = IPPROTO_DCCP;
|
|
|
|
+ }
|
|
|
|
+# endif
|
|
|
|
+ else {
|
|
|
|
+ hints.ai_socktype = SOCK_STREAM;
|
|
|
|
+ hints.ai_protocol = IPPROTO_TCP;
|
|
|
|
+ }
|
|
|
|
if (nflag)
|
|
|
|
hints.ai_flags |= AI_NUMERICHOST;
|
|
|
|
}
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -586,7 +611,10 @@ main(int argc, char *argv[])
|
2017-07-17 15:26:12 +02:00
|
|
|
if (xflag) {
|
|
|
|
if (uflag)
|
|
|
|
errx(1, "no proxy support for UDP mode");
|
|
|
|
-
|
|
|
|
+# if defined(IPPROTO_DCCP) && defined(SOCK_DCCP)
|
|
|
|
+ if (dccpflag)
|
|
|
|
+ errx(1, "no proxy support for DCCP mode");
|
|
|
|
+# endif
|
|
|
|
if (lflag)
|
|
|
|
errx(1, "no proxy support for listen");
|
|
|
|
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -833,19 +861,20 @@ main(int argc, char *argv[])
|
2017-07-17 15:26:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ char *proto = proto_name(uflag, dccpflag);
|
|
|
|
/* Don't look up port if -n. */
|
|
|
|
if (nflag)
|
|
|
|
sv = NULL;
|
|
|
|
else {
|
|
|
|
sv = getservbyport(
|
|
|
|
ntohs(atoi(portlist[i])),
|
|
|
|
- uflag ? "udp" : "tcp");
|
|
|
|
+ proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,
|
|
|
|
"Connection to %s %s port [%s/%s] "
|
|
|
|
"succeeded!\n", host, portlist[i],
|
|
|
|
- uflag ? "udp" : "tcp",
|
|
|
|
+ proto,
|
|
|
|
sv ? sv->s_name : "*");
|
|
|
|
}
|
|
|
|
if (Fflag)
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -1060,6 +1089,24 @@ unix_listen(char *path)
|
2018-11-14 14:18:05 +01:00
|
|
|
return s;
|
2017-07-17 15:26:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
+char *proto_name(int uflag, int dccpflag) {
|
|
|
|
+
|
|
|
|
+ char *proto = NULL;
|
|
|
|
+ if (uflag) {
|
|
|
|
+ proto = "udp";
|
|
|
|
+ }
|
|
|
|
+# if defined(IPPROTO_DCCP) && defined(SOCK_DCCP)
|
|
|
|
+ else if (dccpflag) {
|
|
|
|
+ proto = "dccp";
|
|
|
|
+ }
|
|
|
|
+# endif
|
|
|
|
+ else {
|
|
|
|
+ proto = "tcp";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return proto;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
/*
|
|
|
|
* remote_connect()
|
|
|
|
* Returns a socket connected to a remote host. Properly binds to a local
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -1090,8 +1137,21 @@ remote_connect(const char *host, const c
|
2017-07-17 15:26:12 +02:00
|
|
|
# endif
|
|
|
|
memset(&ahints, 0, sizeof(struct addrinfo));
|
|
|
|
ahints.ai_family = res->ai_family;
|
|
|
|
- ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
|
|
|
|
- ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
|
|
|
|
+ if (uflag) {
|
|
|
|
+ ahints.ai_socktype = SOCK_DGRAM;
|
|
|
|
+ ahints.ai_protocol = IPPROTO_UDP;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+# if defined(IPPROTO_DCCP) && defined(SOCK_DCCP)
|
|
|
|
+ else if (dccpflag) {
|
|
|
|
+ hints.ai_socktype = SOCK_DCCP;
|
|
|
|
+ hints.ai_protocol = IPPROTO_DCCP;
|
|
|
|
+ }
|
|
|
|
+# endif
|
|
|
|
+ else {
|
|
|
|
+ ahints.ai_socktype = SOCK_STREAM;
|
|
|
|
+ ahints.ai_protocol = IPPROTO_TCP;
|
|
|
|
+ }
|
|
|
|
ahints.ai_flags = AI_PASSIVE;
|
|
|
|
if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
|
|
|
|
errx(1, "getaddrinfo: %s", gai_strerror(error));
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -1103,15 +1163,16 @@ remote_connect(const char *host, const c
|
2017-07-17 15:26:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
set_common_sockopts(s, res->ai_family);
|
|
|
|
+ char *proto = proto_name(uflag, dccpflag);
|
|
|
|
|
|
|
|
if ((error = connect_with_timeout(s, res->ai_addr, res->ai_addrlen, timeout)) == CONNECTION_SUCCESS)
|
|
|
|
break;
|
|
|
|
if (vflag && error == CONNECTION_FAILED)
|
|
|
|
warn("connect to %s port %s (%s) failed", host, port,
|
|
|
|
- uflag ? "udp" : "tcp");
|
|
|
|
- else if (vflag && error == CONNECTION_TIMEOUT)
|
|
|
|
+ proto);
|
|
|
|
+ else if (vflag && error == CONNECTION_TIMEOUT)
|
|
|
|
warn("connect to %s port %s (%s) timed out", host, port,
|
|
|
|
- uflag ? "udp" : "tcp");
|
|
|
|
+ proto);
|
|
|
|
|
|
|
|
save_errno = errno;
|
|
|
|
close(s);
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -1715,7 +1776,8 @@ build_ports(char *p)
|
2017-07-17 15:26:12 +02:00
|
|
|
int hi, lo, cp;
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
- sv = getservbyname(p, uflag ? "udp" : "tcp");
|
|
|
|
+ char *proto = proto_name(uflag, dccpflag);
|
|
|
|
+ sv = getservbyname(p, proto);
|
|
|
|
if (sv) {
|
|
|
|
if (asprintf(&portlist[0], "%d", ntohs(sv->s_port)) < 0)
|
|
|
|
err(1, "asprintf");
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -2095,6 +2157,7 @@ help(void)
|
2017-07-17 15:26:12 +02:00
|
|
|
\t-w timeout Timeout for connects and final net reads\n\
|
|
|
|
\t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
|
|
|
|
\t-x addr[:port]\tSpecify proxy address and port\n\
|
|
|
|
+ \t-Z DCCP mode\n\
|
|
|
|
\t-z Zero-I/O mode [used for scanning]\n\
|
|
|
|
Port numbers can be individual or ranges: lo-hi [inclusive]\n");
|
|
|
|
exit(0);
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -2104,7 +2167,7 @@ void
|
2017-07-17 15:26:12 +02:00
|
|
|
usage(int ret)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
- "usage: nc [-46CDdFhklNnrStUuvz] [-I length] [-i interval] [-M ttl]\n"
|
|
|
|
+ "usage: nc [-46CDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl]\n"
|
|
|
|
"\t [-m minttl] [-O length] [-P proxy_username] [-p source_port]\n"
|
2018-11-14 14:18:05 +01:00
|
|
|
"\t [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] "
|
|
|
|
"[-w timeout]\n"
|