2012-05-04 18:29:32 +00:00
|
|
|
/* $OpenBSD: netcat.c,v 1.105 2012/02/09 06:25:35 lum Exp $ */
|
2005-02-04 08:41:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2005-02-06 14:44:27 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
2005-02-04 08:41:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Re-written nc(1) for OpenBSD. Original implementation by
|
|
|
|
* *Hobbit* <hobbit@avian.org>.
|
|
|
|
*/
|
|
|
|
|
2005-02-06 14:44:27 +00:00
|
|
|
#include <sys/limits.h>
|
2005-02-04 08:41:44 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2010-01-19 18:45:29 +00:00
|
|
|
#include <sys/sysctl.h>
|
2005-02-04 08:41:44 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2006-05-21 15:52:24 +00:00
|
|
|
#include <netinet/in_systm.h>
|
2005-02-06 14:44:27 +00:00
|
|
|
#ifdef IPSEC
|
2007-07-01 12:08:08 +00:00
|
|
|
#include <netipsec/ipsec.h>
|
2005-02-06 14:44:27 +00:00
|
|
|
#endif
|
2005-02-04 08:41:44 +00:00
|
|
|
#include <netinet/tcp.h>
|
2006-05-21 15:52:24 +00:00
|
|
|
#include <netinet/ip.h>
|
2005-02-04 08:41:44 +00:00
|
|
|
#include <arpa/telnet.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2008-12-19 23:00:23 +00:00
|
|
|
#include <getopt.h>
|
2005-02-04 08:41:44 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2006-05-21 15:52:24 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include "atomicio.h"
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
#ifndef SUN_LEN
|
|
|
|
#define SUN_LEN(su) \
|
|
|
|
(sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PORT_MAX 65535
|
|
|
|
#define PORT_MAX_LEN 6
|
2011-05-11 18:57:17 +00:00
|
|
|
#define UNIX_DG_TMP_SOCKET_SIZE 19
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
/* Command Line Options */
|
|
|
|
int dflag; /* detached, no stdin */
|
2007-03-28 01:57:03 +00:00
|
|
|
unsigned int iflag; /* Interval Flag */
|
2006-05-21 15:52:24 +00:00
|
|
|
int jflag; /* use jumbo frames if we can */
|
2005-02-04 08:41:44 +00:00
|
|
|
int kflag; /* More than one connect */
|
|
|
|
int lflag; /* Bind to local port */
|
|
|
|
int nflag; /* Don't do name look up */
|
2008-12-19 23:00:23 +00:00
|
|
|
int FreeBSD_Oflag; /* Do not use TCP options */
|
2006-05-21 15:52:24 +00:00
|
|
|
char *Pflag; /* Proxy username */
|
2005-02-04 08:41:44 +00:00
|
|
|
char *pflag; /* Localport flag */
|
|
|
|
int rflag; /* Random ports flag */
|
|
|
|
char *sflag; /* Source Address */
|
|
|
|
int tflag; /* Telnet Emulation */
|
|
|
|
int uflag; /* UDP - Default to TCP */
|
|
|
|
int vflag; /* Verbosity */
|
|
|
|
int xflag; /* Socks proxy */
|
|
|
|
int zflag; /* Port Scan Flag */
|
|
|
|
int Dflag; /* sodebug */
|
2008-09-27 02:29:03 +00:00
|
|
|
int Iflag; /* TCP receive buffer size */
|
|
|
|
int Oflag; /* TCP send buffer size */
|
2005-02-04 08:41:44 +00:00
|
|
|
int Sflag; /* TCP MD5 signature option */
|
2006-05-21 15:52:24 +00:00
|
|
|
int Tflag = -1; /* IP Type of Service */
|
2010-10-18 19:44:45 +00:00
|
|
|
u_int rtableid;
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
int timeout = -1;
|
|
|
|
int family = AF_UNSPEC;
|
|
|
|
char *portlist[PORT_MAX+1];
|
2011-05-11 18:57:17 +00:00
|
|
|
char *unix_dg_tmp_socket;
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
void atelnet(int, unsigned char *, unsigned int);
|
|
|
|
void build_ports(char *);
|
|
|
|
void help(void);
|
|
|
|
int local_listen(char *, char *, struct addrinfo);
|
|
|
|
void readwrite(int);
|
2006-05-21 15:52:24 +00:00
|
|
|
int remote_connect(const char *, const char *, struct addrinfo);
|
2012-05-04 18:29:32 +00:00
|
|
|
int timeout_connect(int, const struct sockaddr *, socklen_t);
|
2006-05-21 15:52:24 +00:00
|
|
|
int socks_connect(const char *, const char *, struct addrinfo,
|
|
|
|
const char *, const char *, struct addrinfo, int, const char *);
|
2005-02-04 08:41:44 +00:00
|
|
|
int udptest(int);
|
2011-05-11 18:57:17 +00:00
|
|
|
int unix_bind(char *);
|
2005-02-04 08:41:44 +00:00
|
|
|
int unix_connect(char *);
|
|
|
|
int unix_listen(char *);
|
2006-05-21 15:52:24 +00:00
|
|
|
void set_common_sockopts(int);
|
2012-05-04 18:29:32 +00:00
|
|
|
int map_tos(char *, int *);
|
2005-02-04 08:41:44 +00:00
|
|
|
void usage(int);
|
|
|
|
|
2005-02-06 14:44:27 +00:00
|
|
|
#ifdef IPSEC
|
|
|
|
void add_ipsec_policy(int, char *);
|
|
|
|
|
|
|
|
char *ipsec_policy[2];
|
|
|
|
#endif
|
|
|
|
|
2005-02-04 08:41:44 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2005-02-06 14:44:27 +00:00
|
|
|
int ch, s, ret, socksv, ipsec_count;
|
2010-01-19 18:45:29 +00:00
|
|
|
int numfibs;
|
|
|
|
size_t intsize = sizeof(int);
|
2007-03-28 01:57:03 +00:00
|
|
|
char *host, *uport;
|
2005-02-04 08:41:44 +00:00
|
|
|
struct addrinfo hints;
|
|
|
|
struct servent *sv;
|
|
|
|
socklen_t len;
|
|
|
|
struct sockaddr_storage cliaddr;
|
|
|
|
char *proxy;
|
2007-03-28 01:57:03 +00:00
|
|
|
const char *errstr, *proxyhost = "", *proxyport = NULL;
|
2005-02-04 08:41:44 +00:00
|
|
|
struct addrinfo proxyhints;
|
2011-05-11 18:57:17 +00:00
|
|
|
char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE];
|
2008-12-19 23:00:23 +00:00
|
|
|
struct option longopts[] = {
|
|
|
|
{ "no-tcpopt", no_argument, &FreeBSD_Oflag, 1 },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
ret = 1;
|
2005-02-06 14:44:27 +00:00
|
|
|
ipsec_count = 0;
|
2005-02-04 08:41:44 +00:00
|
|
|
s = 0;
|
|
|
|
socksv = 5;
|
|
|
|
host = NULL;
|
|
|
|
uport = NULL;
|
|
|
|
sv = NULL;
|
|
|
|
|
2008-12-19 23:00:23 +00:00
|
|
|
while ((ch = getopt_long(argc, argv,
|
2010-04-15 19:15:05 +00:00
|
|
|
"46DdEe:hI:i:jklnoO:P:p:rSs:tT:UuV:vw:X:x:z",
|
2008-12-19 23:00:23 +00:00
|
|
|
longopts, NULL)) != -1) {
|
2005-02-04 08:41:44 +00:00
|
|
|
switch (ch) {
|
|
|
|
case '4':
|
|
|
|
family = AF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
family = AF_INET6;
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
family = AF_UNIX;
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
if (strcasecmp(optarg, "connect") == 0)
|
|
|
|
socksv = -1; /* HTTP proxy CONNECT */
|
|
|
|
else if (strcmp(optarg, "4") == 0)
|
|
|
|
socksv = 4; /* SOCKS v.4 */
|
|
|
|
else if (strcmp(optarg, "5") == 0)
|
|
|
|
socksv = 5; /* SOCKS v.5 */
|
|
|
|
else
|
|
|
|
errx(1, "unsupported proxy protocol");
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
break;
|
2005-02-06 14:44:27 +00:00
|
|
|
case 'e':
|
|
|
|
#ifdef IPSEC
|
|
|
|
ipsec_policy[ipsec_count++ % 2] = optarg;
|
|
|
|
#else
|
|
|
|
errx(1, "IPsec support unavailable.");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
#ifdef IPSEC
|
|
|
|
ipsec_policy[0] = "in ipsec esp/transport//require";
|
|
|
|
ipsec_policy[1] = "out ipsec esp/transport//require";
|
|
|
|
#else
|
|
|
|
errx(1, "IPsec support unavailable.");
|
|
|
|
#endif
|
|
|
|
break;
|
2005-02-04 08:41:44 +00:00
|
|
|
case 'h':
|
|
|
|
help();
|
|
|
|
break;
|
|
|
|
case 'i':
|
2007-03-28 01:57:03 +00:00
|
|
|
iflag = strtonum(optarg, 0, UINT_MAX, &errstr);
|
|
|
|
if (errstr)
|
|
|
|
errx(1, "interval %s: %s", errstr, optarg);
|
2005-02-04 08:41:44 +00:00
|
|
|
break;
|
2006-05-21 15:52:24 +00:00
|
|
|
#ifdef SO_JUMBO
|
|
|
|
case 'j':
|
|
|
|
jflag = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
2005-02-04 08:41:44 +00:00
|
|
|
case 'k':
|
|
|
|
kflag = 1;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
2005-02-06 14:44:27 +00:00
|
|
|
case 'o':
|
2010-04-15 19:15:05 +00:00
|
|
|
fprintf(stderr, "option -o is deprecated.\n");
|
2005-02-06 14:44:27 +00:00
|
|
|
break;
|
2006-05-21 15:52:24 +00:00
|
|
|
case 'P':
|
|
|
|
Pflag = optarg;
|
|
|
|
break;
|
2005-02-04 08:41:44 +00:00
|
|
|
case 'p':
|
|
|
|
pflag = optarg;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sflag = optarg;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
tflag = 1;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
2010-01-19 18:25:10 +00:00
|
|
|
case 'V':
|
2010-01-19 18:45:29 +00:00
|
|
|
if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
|
|
|
|
errx(1, "Multiple FIBS not supported");
|
2010-10-18 19:44:45 +00:00
|
|
|
rtableid = (unsigned int)strtonum(optarg, 0,
|
2010-01-19 18:45:29 +00:00
|
|
|
numfibs - 1, &errstr);
|
2010-01-19 18:25:10 +00:00
|
|
|
if (errstr)
|
2010-10-18 19:44:45 +00:00
|
|
|
errx(1, "rtable %s: %s", errstr, optarg);
|
2010-01-19 18:25:10 +00:00
|
|
|
break;
|
2005-02-04 08:41:44 +00:00
|
|
|
case 'v':
|
|
|
|
vflag = 1;
|
|
|
|
break;
|
|
|
|
case 'w':
|
2007-03-28 01:57:03 +00:00
|
|
|
timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr);
|
|
|
|
if (errstr)
|
|
|
|
errx(1, "timeout %s: %s", errstr, optarg);
|
2005-02-04 08:41:44 +00:00
|
|
|
timeout *= 1000;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
xflag = 1;
|
|
|
|
if ((proxy = strdup(optarg)) == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
zflag = 1;
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
Dflag = 1;
|
|
|
|
break;
|
2008-09-27 02:29:03 +00:00
|
|
|
case 'I':
|
|
|
|
Iflag = strtonum(optarg, 1, 65536 << 14, &errstr);
|
|
|
|
if (errstr != NULL)
|
|
|
|
errx(1, "TCP receive window %s: %s",
|
|
|
|
errstr, optarg);
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
Oflag = strtonum(optarg, 1, 65536 << 14, &errstr);
|
2008-12-19 23:00:23 +00:00
|
|
|
if (errstr != NULL) {
|
|
|
|
if (strcmp(errstr, "invalid") != 0)
|
2008-09-27 02:29:03 +00:00
|
|
|
errx(1, "TCP send window %s: %s",
|
|
|
|
errstr, optarg);
|
2008-12-19 23:00:23 +00:00
|
|
|
}
|
2008-09-27 02:29:03 +00:00
|
|
|
break;
|
2005-02-04 08:41:44 +00:00
|
|
|
case 'S':
|
|
|
|
Sflag = 1;
|
|
|
|
break;
|
2006-05-21 15:52:24 +00:00
|
|
|
case 'T':
|
2012-05-04 18:29:32 +00:00
|
|
|
errstr = NULL;
|
|
|
|
errno = 0;
|
|
|
|
if (map_tos(optarg, &Tflag))
|
|
|
|
break;
|
|
|
|
if (strlen(optarg) > 1 && optarg[0] == '0' &&
|
|
|
|
optarg[1] == 'x')
|
|
|
|
Tflag = (int)strtol(optarg, NULL, 16);
|
|
|
|
else
|
|
|
|
Tflag = (int)strtonum(optarg, 0, 255,
|
|
|
|
&errstr);
|
|
|
|
if (Tflag < 0 || Tflag > 255 || errstr || errno)
|
|
|
|
errx(1, "illegal tos value %s", optarg);
|
2006-05-21 15:52:24 +00:00
|
|
|
break;
|
2005-02-04 08:41:44 +00:00
|
|
|
default:
|
|
|
|
usage(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* Cruft to make sure options are clean, and used properly. */
|
|
|
|
if (argv[0] && !argv[1] && family == AF_UNIX) {
|
|
|
|
host = argv[0];
|
|
|
|
uport = NULL;
|
|
|
|
} else if (argv[0] && !argv[1]) {
|
|
|
|
if (!lflag)
|
|
|
|
usage(1);
|
|
|
|
uport = argv[0];
|
|
|
|
host = NULL;
|
|
|
|
} else if (argv[0] && argv[1]) {
|
|
|
|
host = argv[0];
|
|
|
|
uport = argv[1];
|
|
|
|
} else
|
|
|
|
usage(1);
|
|
|
|
|
|
|
|
if (lflag && sflag)
|
|
|
|
errx(1, "cannot use -s and -l");
|
|
|
|
if (lflag && pflag)
|
|
|
|
errx(1, "cannot use -p and -l");
|
|
|
|
if (lflag && zflag)
|
|
|
|
errx(1, "cannot use -z and -l");
|
|
|
|
if (!lflag && kflag)
|
|
|
|
errx(1, "must use -l with -k");
|
|
|
|
|
2011-05-11 18:57:17 +00:00
|
|
|
/* Get name of temporary socket for unix datagram client */
|
|
|
|
if ((family == AF_UNIX) && uflag && !lflag) {
|
|
|
|
if (sflag) {
|
|
|
|
unix_dg_tmp_socket = sflag;
|
|
|
|
} else {
|
|
|
|
strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
|
|
|
|
UNIX_DG_TMP_SOCKET_SIZE);
|
|
|
|
if (mktemp(unix_dg_tmp_socket_buf) == NULL)
|
|
|
|
err(1, "mktemp");
|
|
|
|
unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-04 08:41:44 +00:00
|
|
|
/* Initialize addrinfo structure. */
|
|
|
|
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 (nflag)
|
|
|
|
hints.ai_flags |= AI_NUMERICHOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xflag) {
|
|
|
|
if (uflag)
|
|
|
|
errx(1, "no proxy support for UDP mode");
|
|
|
|
|
|
|
|
if (lflag)
|
|
|
|
errx(1, "no proxy support for listen");
|
|
|
|
|
|
|
|
if (family == AF_UNIX)
|
|
|
|
errx(1, "no proxy support for unix sockets");
|
|
|
|
|
|
|
|
/* XXX IPv6 transport to proxy would probably work */
|
|
|
|
if (family == AF_INET6)
|
|
|
|
errx(1, "no proxy support for IPv6");
|
|
|
|
|
|
|
|
if (sflag)
|
|
|
|
errx(1, "no proxy support for local source address");
|
|
|
|
|
|
|
|
proxyhost = strsep(&proxy, ":");
|
|
|
|
proxyport = proxy;
|
|
|
|
|
|
|
|
memset(&proxyhints, 0, sizeof(struct addrinfo));
|
|
|
|
proxyhints.ai_family = family;
|
|
|
|
proxyhints.ai_socktype = SOCK_STREAM;
|
|
|
|
proxyhints.ai_protocol = IPPROTO_TCP;
|
|
|
|
if (nflag)
|
|
|
|
proxyhints.ai_flags |= AI_NUMERICHOST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lflag) {
|
|
|
|
int connfd;
|
|
|
|
ret = 0;
|
|
|
|
|
2011-05-11 18:57:17 +00:00
|
|
|
if (family == AF_UNIX) {
|
|
|
|
if (uflag)
|
|
|
|
s = unix_bind(host);
|
|
|
|
else
|
|
|
|
s = unix_listen(host);
|
|
|
|
}
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
/* Allow only one connection at a time, but stay alive. */
|
|
|
|
for (;;) {
|
|
|
|
if (family != AF_UNIX)
|
|
|
|
s = local_listen(host, uport, hints);
|
|
|
|
if (s < 0)
|
|
|
|
err(1, NULL);
|
|
|
|
/*
|
|
|
|
* For UDP, we will use recvfrom() initially
|
|
|
|
* to wait for a caller, then use the regular
|
|
|
|
* functions to talk to the caller.
|
|
|
|
*/
|
|
|
|
if (uflag) {
|
2006-05-21 15:52:24 +00:00
|
|
|
int rv, plen;
|
2010-10-18 19:44:45 +00:00
|
|
|
char buf[16384];
|
2005-02-04 08:41:44 +00:00
|
|
|
struct sockaddr_storage z;
|
|
|
|
|
|
|
|
len = sizeof(z);
|
2010-10-18 19:44:45 +00:00
|
|
|
plen = jflag ? 16384 : 2048;
|
2006-05-21 15:52:24 +00:00
|
|
|
rv = recvfrom(s, buf, plen, MSG_PEEK,
|
2005-02-04 08:41:44 +00:00
|
|
|
(struct sockaddr *)&z, &len);
|
|
|
|
if (rv < 0)
|
|
|
|
err(1, "recvfrom");
|
|
|
|
|
|
|
|
rv = connect(s, (struct sockaddr *)&z, len);
|
|
|
|
if (rv < 0)
|
|
|
|
err(1, "connect");
|
|
|
|
|
2011-05-11 18:57:17 +00:00
|
|
|
readwrite(s);
|
2005-02-04 08:41:44 +00:00
|
|
|
} else {
|
2006-05-21 15:52:24 +00:00
|
|
|
len = sizeof(cliaddr);
|
2005-02-04 08:41:44 +00:00
|
|
|
connfd = accept(s, (struct sockaddr *)&cliaddr,
|
|
|
|
&len);
|
2011-05-11 18:57:17 +00:00
|
|
|
readwrite(connfd);
|
|
|
|
close(connfd);
|
2005-02-04 08:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (family != AF_UNIX)
|
|
|
|
close(s);
|
2011-05-11 18:57:17 +00:00
|
|
|
else if (uflag) {
|
|
|
|
if (connect(s, NULL, 0) < 0)
|
|
|
|
err(1, "connect");
|
|
|
|
}
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
if (!kflag)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (family == AF_UNIX) {
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
if ((s = unix_connect(host)) > 0 && !zflag) {
|
|
|
|
readwrite(s);
|
|
|
|
close(s);
|
|
|
|
} else
|
|
|
|
ret = 1;
|
|
|
|
|
2011-05-11 18:57:17 +00:00
|
|
|
if (uflag)
|
|
|
|
unlink(unix_dg_tmp_socket);
|
2005-02-04 08:41:44 +00:00
|
|
|
exit(ret);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
/* Construct the portlist[] array. */
|
|
|
|
build_ports(uport);
|
|
|
|
|
|
|
|
/* Cycle through portlist, connecting to each port. */
|
|
|
|
for (i = 0; portlist[i] != NULL; i++) {
|
|
|
|
if (s)
|
|
|
|
close(s);
|
|
|
|
|
|
|
|
if (xflag)
|
|
|
|
s = socks_connect(host, portlist[i], hints,
|
2006-05-21 15:52:24 +00:00
|
|
|
proxyhost, proxyport, proxyhints, socksv,
|
|
|
|
Pflag);
|
2005-02-04 08:41:44 +00:00
|
|
|
else
|
|
|
|
s = remote_connect(host, portlist[i], hints);
|
|
|
|
|
|
|
|
if (s < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
if (vflag || zflag) {
|
|
|
|
/* For UDP, make sure we are connected. */
|
|
|
|
if (uflag) {
|
|
|
|
if (udptest(s) == -1) {
|
|
|
|
ret = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't look up port if -n. */
|
|
|
|
if (nflag)
|
|
|
|
sv = NULL;
|
|
|
|
else {
|
|
|
|
sv = getservbyport(
|
|
|
|
ntohs(atoi(portlist[i])),
|
|
|
|
uflag ? "udp" : "tcp");
|
|
|
|
}
|
|
|
|
|
2010-03-23 22:56:19 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Connection to %s %s port [%s/%s] "
|
|
|
|
"succeeded!\n", host, portlist[i],
|
|
|
|
uflag ? "udp" : "tcp",
|
2005-02-04 08:41:44 +00:00
|
|
|
sv ? sv->s_name : "*");
|
|
|
|
}
|
|
|
|
if (!zflag)
|
|
|
|
readwrite(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
close(s);
|
|
|
|
|
|
|
|
exit(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-05-11 18:57:17 +00:00
|
|
|
* unix_bind()
|
|
|
|
* Returns a unix socket bound to the given path
|
2005-02-04 08:41:44 +00:00
|
|
|
*/
|
|
|
|
int
|
2011-05-11 18:57:17 +00:00
|
|
|
unix_bind(char *path)
|
2005-02-04 08:41:44 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_un sun;
|
|
|
|
int s;
|
|
|
|
|
2011-05-11 18:57:17 +00:00
|
|
|
/* Create unix domain socket. */
|
|
|
|
if ((s = socket(AF_UNIX, uflag ? SOCK_DGRAM : SOCK_STREAM,
|
|
|
|
0)) < 0)
|
2005-02-04 08:41:44 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
memset(&sun, 0, sizeof(struct sockaddr_un));
|
|
|
|
sun.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
|
|
|
|
sizeof(sun.sun_path)) {
|
|
|
|
close(s);
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return (-1);
|
|
|
|
}
|
2011-05-11 18:57:17 +00:00
|
|
|
|
|
|
|
if (bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
|
2005-02-04 08:41:44 +00:00
|
|
|
close(s);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-05-11 18:57:17 +00:00
|
|
|
* unix_connect()
|
|
|
|
* Returns a socket connected to a local unix socket. Returns -1 on failure.
|
2005-02-04 08:41:44 +00:00
|
|
|
*/
|
|
|
|
int
|
2011-05-11 18:57:17 +00:00
|
|
|
unix_connect(char *path)
|
2005-02-04 08:41:44 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_un sun;
|
|
|
|
int s;
|
|
|
|
|
2011-05-11 18:57:17 +00:00
|
|
|
if (uflag) {
|
|
|
|
if ((s = unix_bind(unix_dg_tmp_socket)) < 0)
|
|
|
|
return (-1);
|
|
|
|
} else {
|
|
|
|
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
(void)fcntl(s, F_SETFD, 1);
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
memset(&sun, 0, sizeof(struct sockaddr_un));
|
|
|
|
sun.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
|
|
|
|
sizeof(sun.sun_path)) {
|
|
|
|
close(s);
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
return (-1);
|
|
|
|
}
|
2011-05-11 18:57:17 +00:00
|
|
|
if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
|
2005-02-04 08:41:44 +00:00
|
|
|
close(s);
|
|
|
|
return (-1);
|
|
|
|
}
|
2011-05-11 18:57:17 +00:00
|
|
|
return (s);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* unix_listen()
|
|
|
|
* Create a unix domain socket, and listen on it.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
unix_listen(char *path)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
if ((s = unix_bind(path)) < 0)
|
|
|
|
return (-1);
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
if (listen(s, 5) < 0) {
|
|
|
|
close(s);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remote_connect()
|
|
|
|
* Returns a socket connected to a remote host. Properly binds to a local
|
|
|
|
* port or source address if needed. Returns -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
2006-05-21 15:52:24 +00:00
|
|
|
remote_connect(const char *host, const char *port, struct addrinfo hints)
|
2005-02-04 08:41:44 +00:00
|
|
|
{
|
|
|
|
struct addrinfo *res, *res0;
|
2008-09-27 02:29:03 +00:00
|
|
|
int s, error, on = 1;
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
if ((error = getaddrinfo(host, port, &hints, &res)))
|
|
|
|
errx(1, "getaddrinfo: %s", gai_strerror(error));
|
|
|
|
|
|
|
|
res0 = res;
|
|
|
|
do {
|
|
|
|
if ((s = socket(res0->ai_family, res0->ai_socktype,
|
|
|
|
res0->ai_protocol)) < 0)
|
|
|
|
continue;
|
2005-02-06 14:44:27 +00:00
|
|
|
#ifdef IPSEC
|
|
|
|
if (ipsec_policy[0] != NULL)
|
|
|
|
add_ipsec_policy(s, ipsec_policy[0]);
|
|
|
|
if (ipsec_policy[1] != NULL)
|
|
|
|
add_ipsec_policy(s, ipsec_policy[1]);
|
|
|
|
#endif
|
2005-02-04 08:41:44 +00:00
|
|
|
|
2010-10-18 19:44:45 +00:00
|
|
|
if (rtableid) {
|
2012-02-14 11:37:04 +00:00
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_SETFIB, &rtableid,
|
|
|
|
sizeof(rtableid)) == -1)
|
|
|
|
err(1, "setsockopt(.., SO_SETFIB, %u, ..)",
|
|
|
|
rtableid);
|
2010-01-19 18:25:10 +00:00
|
|
|
}
|
|
|
|
|
2005-02-04 08:41:44 +00:00
|
|
|
/* Bind to a local port or source address if specified. */
|
|
|
|
if (sflag || pflag) {
|
|
|
|
struct addrinfo ahints, *ares;
|
|
|
|
|
2010-04-15 23:21:24 +00:00
|
|
|
/* try IP_BINDANY, but don't insist */
|
|
|
|
setsockopt(s, IPPROTO_IP, IP_BINDANY, &on, sizeof(on));
|
2005-02-04 08:41:44 +00:00
|
|
|
memset(&ahints, 0, sizeof(struct addrinfo));
|
|
|
|
ahints.ai_family = res0->ai_family;
|
|
|
|
ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
|
|
|
|
ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
|
|
|
|
ahints.ai_flags = AI_PASSIVE;
|
|
|
|
if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
|
|
|
|
errx(1, "getaddrinfo: %s", gai_strerror(error));
|
|
|
|
|
|
|
|
if (bind(s, (struct sockaddr *)ares->ai_addr,
|
|
|
|
ares->ai_addrlen) < 0)
|
|
|
|
errx(1, "bind failed: %s", strerror(errno));
|
|
|
|
freeaddrinfo(ares);
|
|
|
|
}
|
2006-05-21 15:52:24 +00:00
|
|
|
|
|
|
|
set_common_sockopts(s);
|
2005-02-04 08:41:44 +00:00
|
|
|
|
2012-05-04 18:29:32 +00:00
|
|
|
if (timeout_connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
|
2005-02-04 08:41:44 +00:00
|
|
|
break;
|
|
|
|
else if (vflag)
|
|
|
|
warn("connect to %s port %s (%s) failed", host, port,
|
|
|
|
uflag ? "udp" : "tcp");
|
|
|
|
|
|
|
|
close(s);
|
|
|
|
s = -1;
|
|
|
|
} while ((res0 = res0->ai_next) != NULL);
|
|
|
|
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2012-05-04 18:29:32 +00:00
|
|
|
int
|
|
|
|
timeout_connect(int s, const struct sockaddr *name, socklen_t namelen)
|
|
|
|
{
|
|
|
|
struct pollfd pfd;
|
|
|
|
socklen_t optlen;
|
|
|
|
int flags, optval;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (timeout != -1) {
|
|
|
|
flags = fcntl(s, F_GETFL, 0);
|
|
|
|
if (fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1)
|
|
|
|
err(1, "set non-blocking mode");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = connect(s, name, namelen)) != 0 && errno == EINPROGRESS) {
|
|
|
|
pfd.fd = s;
|
|
|
|
pfd.events = POLLOUT;
|
|
|
|
if ((ret = poll(&pfd, 1, timeout)) == 1) {
|
|
|
|
optlen = sizeof(optval);
|
|
|
|
if ((ret = getsockopt(s, SOL_SOCKET, SO_ERROR,
|
|
|
|
&optval, &optlen)) == 0) {
|
|
|
|
errno = optval;
|
|
|
|
ret = optval == 0 ? 0 : -1;
|
|
|
|
}
|
|
|
|
} else if (ret == 0) {
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
ret = -1;
|
|
|
|
} else
|
|
|
|
err(1, "poll failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout != -1 && fcntl(s, F_SETFL, flags) == -1)
|
|
|
|
err(1, "restoring flags");
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2005-02-04 08:41:44 +00:00
|
|
|
/*
|
|
|
|
* local_listen()
|
|
|
|
* Returns a socket listening on a local port, binds to specified source
|
|
|
|
* address. Returns -1 on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
local_listen(char *host, char *port, struct addrinfo hints)
|
|
|
|
{
|
|
|
|
struct addrinfo *res, *res0;
|
|
|
|
int s, ret, x = 1;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* Allow nodename to be null. */
|
|
|
|
hints.ai_flags |= AI_PASSIVE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In the case of binding to a wildcard address
|
|
|
|
* default to binding to an ipv4 address.
|
|
|
|
*/
|
|
|
|
if (host == NULL && hints.ai_family == AF_UNSPEC)
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
|
|
|
|
if ((error = getaddrinfo(host, port, &hints, &res)))
|
|
|
|
errx(1, "getaddrinfo: %s", gai_strerror(error));
|
|
|
|
|
|
|
|
res0 = res;
|
|
|
|
do {
|
|
|
|
if ((s = socket(res0->ai_family, res0->ai_socktype,
|
2006-05-21 15:52:24 +00:00
|
|
|
res0->ai_protocol)) < 0)
|
2005-02-04 08:41:44 +00:00
|
|
|
continue;
|
|
|
|
|
2010-10-18 19:44:45 +00:00
|
|
|
if (rtableid) {
|
2012-02-14 11:37:04 +00:00
|
|
|
ret = setsockopt(s, SOL_SOCKET, SO_SETFIB, &rtableid,
|
|
|
|
sizeof(rtableid));
|
|
|
|
if (ret == -1)
|
|
|
|
err(1, "setsockopt(.., SO_SETFIB, %u, ..)",
|
|
|
|
rtableid);
|
2010-01-19 18:25:10 +00:00
|
|
|
}
|
|
|
|
|
2005-02-04 08:41:44 +00:00
|
|
|
ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
|
|
|
|
if (ret == -1)
|
|
|
|
err(1, NULL);
|
2005-02-06 14:44:27 +00:00
|
|
|
#ifdef IPSEC
|
|
|
|
if (ipsec_policy[0] != NULL)
|
|
|
|
add_ipsec_policy(s, ipsec_policy[0]);
|
|
|
|
if (ipsec_policy[1] != NULL)
|
|
|
|
add_ipsec_policy(s, ipsec_policy[1]);
|
|
|
|
#endif
|
2008-12-19 23:00:23 +00:00
|
|
|
if (FreeBSD_Oflag) {
|
2008-04-01 13:54:24 +00:00
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_NOOPT,
|
2008-12-19 23:00:23 +00:00
|
|
|
&FreeBSD_Oflag, sizeof(FreeBSD_Oflag)) == -1)
|
2008-04-01 13:54:24 +00:00
|
|
|
err(1, "disable TCP options");
|
|
|
|
}
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
if (bind(s, (struct sockaddr *)res0->ai_addr,
|
|
|
|
res0->ai_addrlen) == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
close(s);
|
|
|
|
s = -1;
|
|
|
|
} while ((res0 = res0->ai_next) != NULL);
|
|
|
|
|
|
|
|
if (!uflag && s != -1) {
|
|
|
|
if (listen(s, 1) < 0)
|
|
|
|
err(1, "listen");
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* readwrite()
|
|
|
|
* Loop that polls on the network file descriptor and stdin.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
readwrite(int nfd)
|
|
|
|
{
|
|
|
|
struct pollfd pfd[2];
|
2010-10-18 19:44:45 +00:00
|
|
|
unsigned char buf[16384];
|
2006-05-21 15:52:24 +00:00
|
|
|
int n, wfd = fileno(stdin);
|
2005-02-04 08:41:44 +00:00
|
|
|
int lfd = fileno(stdout);
|
2006-05-21 15:52:24 +00:00
|
|
|
int plen;
|
|
|
|
|
2010-10-18 19:44:45 +00:00
|
|
|
plen = jflag ? 16384 : 2048;
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
/* Setup Network FD */
|
|
|
|
pfd[0].fd = nfd;
|
|
|
|
pfd[0].events = POLLIN;
|
|
|
|
|
|
|
|
/* Set up STDIN FD. */
|
|
|
|
pfd[1].fd = wfd;
|
|
|
|
pfd[1].events = POLLIN;
|
|
|
|
|
|
|
|
while (pfd[0].fd != -1) {
|
|
|
|
if (iflag)
|
|
|
|
sleep(iflag);
|
|
|
|
|
|
|
|
if ((n = poll(pfd, 2 - dflag, timeout)) < 0) {
|
|
|
|
close(nfd);
|
|
|
|
err(1, "Polling Error");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pfd[0].revents & POLLIN) {
|
2006-05-21 15:52:24 +00:00
|
|
|
if ((n = read(nfd, buf, plen)) < 0)
|
2005-02-04 08:41:44 +00:00
|
|
|
return;
|
|
|
|
else if (n == 0) {
|
|
|
|
shutdown(nfd, SHUT_RD);
|
|
|
|
pfd[0].fd = -1;
|
|
|
|
pfd[0].events = 0;
|
|
|
|
} else {
|
|
|
|
if (tflag)
|
|
|
|
atelnet(nfd, buf, n);
|
2006-05-21 15:52:24 +00:00
|
|
|
if (atomicio(vwrite, lfd, buf, n) != n)
|
2005-02-04 08:41:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dflag && pfd[1].revents & POLLIN) {
|
2010-04-15 19:15:05 +00:00
|
|
|
if ((n = read(wfd, buf, plen)) < 0)
|
2005-02-04 08:41:44 +00:00
|
|
|
return;
|
2010-04-15 19:15:05 +00:00
|
|
|
else if (n == 0) {
|
2005-02-04 08:41:44 +00:00
|
|
|
shutdown(nfd, SHUT_WR);
|
|
|
|
pfd[1].fd = -1;
|
|
|
|
pfd[1].events = 0;
|
|
|
|
} else {
|
2006-05-21 15:52:24 +00:00
|
|
|
if (atomicio(vwrite, nfd, buf, n) != n)
|
2005-02-04 08:41:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
|
|
|
|
void
|
|
|
|
atelnet(int nfd, unsigned char *buf, unsigned int size)
|
|
|
|
{
|
|
|
|
unsigned char *p, *end;
|
|
|
|
unsigned char obuf[4];
|
|
|
|
|
2010-03-23 22:56:19 +00:00
|
|
|
if (size < 3)
|
|
|
|
return;
|
|
|
|
end = buf + size - 2;
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
for (p = buf; p < end; p++) {
|
|
|
|
if (*p != IAC)
|
2010-03-23 22:56:19 +00:00
|
|
|
continue;
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
obuf[0] = IAC;
|
|
|
|
p++;
|
|
|
|
if ((*p == WILL) || (*p == WONT))
|
|
|
|
obuf[1] = DONT;
|
2010-03-23 22:56:19 +00:00
|
|
|
else if ((*p == DO) || (*p == DONT))
|
2005-02-04 08:41:44 +00:00
|
|
|
obuf[1] = WONT;
|
2010-03-23 22:56:19 +00:00
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
obuf[2] = *p;
|
|
|
|
if (atomicio(vwrite, nfd, obuf, 3) != 3)
|
|
|
|
warn("Write Error!");
|
2005-02-04 08:41:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* build_ports()
|
2012-05-04 18:29:32 +00:00
|
|
|
* Build an array of ports in portlist[], listing each port
|
2005-02-04 08:41:44 +00:00
|
|
|
* that we should try to connect to.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
build_ports(char *p)
|
|
|
|
{
|
2007-03-28 01:57:03 +00:00
|
|
|
const char *errstr;
|
|
|
|
char *n;
|
2005-02-04 08:41:44 +00:00
|
|
|
int hi, lo, cp;
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
if ((n = strchr(p, '-')) != NULL) {
|
|
|
|
*n = '\0';
|
|
|
|
n++;
|
|
|
|
|
|
|
|
/* Make sure the ports are in order: lowest->highest. */
|
2007-03-28 01:57:03 +00:00
|
|
|
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);
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
if (lo > hi) {
|
|
|
|
cp = hi;
|
|
|
|
hi = lo;
|
|
|
|
lo = cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load ports sequentially. */
|
|
|
|
for (cp = lo; cp <= hi; cp++) {
|
|
|
|
portlist[x] = calloc(1, PORT_MAX_LEN);
|
|
|
|
if (portlist[x] == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
snprintf(portlist[x], PORT_MAX_LEN, "%d", cp);
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Randomly swap ports. */
|
|
|
|
if (rflag) {
|
|
|
|
int y;
|
|
|
|
char *c;
|
|
|
|
|
|
|
|
for (x = 0; x <= (hi - lo); x++) {
|
|
|
|
y = (arc4random() & 0xFFFF) % (hi - lo);
|
|
|
|
c = portlist[x];
|
|
|
|
portlist[x] = portlist[y];
|
|
|
|
portlist[y] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2007-03-28 01:57:03 +00:00
|
|
|
hi = strtonum(p, 1, PORT_MAX, &errstr);
|
|
|
|
if (errstr)
|
|
|
|
errx(1, "port number %s: %s", errstr, p);
|
2010-10-18 19:44:45 +00:00
|
|
|
portlist[0] = strdup(p);
|
2005-02-04 08:41:44 +00:00
|
|
|
if (portlist[0] == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* udptest()
|
|
|
|
* Do a few writes to see if the UDP port is there.
|
2012-05-04 18:29:32 +00:00
|
|
|
* Fails once PF state table is full.
|
2005-02-04 08:41:44 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
udptest(int s)
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
for (i = 0; i <= 3; i++) {
|
|
|
|
if (write(s, "X", 1) == 1)
|
|
|
|
ret = 1;
|
|
|
|
else
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2006-05-21 15:52:24 +00:00
|
|
|
void
|
|
|
|
set_common_sockopts(int s)
|
|
|
|
{
|
|
|
|
int x = 1;
|
|
|
|
|
|
|
|
if (Sflag) {
|
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
|
|
|
|
&x, sizeof(x)) == -1)
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
|
|
|
if (Dflag) {
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
|
|
|
|
&x, sizeof(x)) == -1)
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
|
|
|
#ifdef SO_JUMBO
|
|
|
|
if (jflag) {
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_JUMBO,
|
|
|
|
&x, sizeof(x)) == -1)
|
|
|
|
err(1, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (Tflag != -1) {
|
|
|
|
if (setsockopt(s, IPPROTO_IP, IP_TOS,
|
|
|
|
&Tflag, sizeof(Tflag)) == -1)
|
|
|
|
err(1, "set IP ToS");
|
|
|
|
}
|
2008-09-27 02:29:03 +00:00
|
|
|
if (Iflag) {
|
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
|
|
|
|
&Iflag, sizeof(Iflag)) == -1)
|
|
|
|
err(1, "set TCP receive buffer size");
|
|
|
|
}
|
2008-04-01 13:54:24 +00:00
|
|
|
if (Oflag) {
|
2008-09-27 02:29:03 +00:00
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
|
2008-04-01 13:54:24 +00:00
|
|
|
&Oflag, sizeof(Oflag)) == -1)
|
2008-09-27 02:29:03 +00:00
|
|
|
err(1, "set TCP send buffer size");
|
|
|
|
}
|
2008-12-19 23:00:23 +00:00
|
|
|
if (FreeBSD_Oflag) {
|
|
|
|
if (setsockopt(s, IPPROTO_TCP, TCP_NOOPT,
|
|
|
|
&FreeBSD_Oflag, sizeof(FreeBSD_Oflag)) == -1)
|
2008-04-01 13:54:24 +00:00
|
|
|
err(1, "disable TCP options");
|
|
|
|
}
|
2006-05-21 15:52:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-05-04 18:29:32 +00:00
|
|
|
map_tos(char *s, int *val)
|
2006-05-21 15:52:24 +00:00
|
|
|
{
|
2012-05-04 18:29:32 +00:00
|
|
|
/* DiffServ Codepoints and other TOS mappings */
|
|
|
|
const struct toskeywords {
|
|
|
|
const char *keyword;
|
|
|
|
int val;
|
|
|
|
} *t, toskeywords[] = {
|
|
|
|
{ "af11", IPTOS_DSCP_AF11 },
|
|
|
|
{ "af12", IPTOS_DSCP_AF12 },
|
|
|
|
{ "af13", IPTOS_DSCP_AF13 },
|
|
|
|
{ "af21", IPTOS_DSCP_AF21 },
|
|
|
|
{ "af22", IPTOS_DSCP_AF22 },
|
|
|
|
{ "af23", IPTOS_DSCP_AF23 },
|
|
|
|
{ "af31", IPTOS_DSCP_AF31 },
|
|
|
|
{ "af32", IPTOS_DSCP_AF32 },
|
|
|
|
{ "af33", IPTOS_DSCP_AF33 },
|
|
|
|
{ "af41", IPTOS_DSCP_AF41 },
|
|
|
|
{ "af42", IPTOS_DSCP_AF42 },
|
|
|
|
{ "af43", IPTOS_DSCP_AF43 },
|
|
|
|
{ "critical", IPTOS_PREC_CRITIC_ECP },
|
|
|
|
{ "cs0", IPTOS_DSCP_CS0 },
|
|
|
|
{ "cs1", IPTOS_DSCP_CS1 },
|
|
|
|
{ "cs2", IPTOS_DSCP_CS2 },
|
|
|
|
{ "cs3", IPTOS_DSCP_CS3 },
|
|
|
|
{ "cs4", IPTOS_DSCP_CS4 },
|
|
|
|
{ "cs5", IPTOS_DSCP_CS5 },
|
|
|
|
{ "cs6", IPTOS_DSCP_CS6 },
|
|
|
|
{ "cs7", IPTOS_DSCP_CS7 },
|
|
|
|
{ "ef", IPTOS_DSCP_EF },
|
|
|
|
{ "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
|
|
|
|
{ "lowdelay", IPTOS_LOWDELAY },
|
|
|
|
{ "netcontrol", IPTOS_PREC_NETCONTROL },
|
|
|
|
{ "reliability", IPTOS_RELIABILITY },
|
|
|
|
{ "throughput", IPTOS_THROUGHPUT },
|
|
|
|
{ NULL, -1 },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (t = toskeywords; t->keyword != NULL; t++) {
|
|
|
|
if (strcmp(s, t->keyword) == 0) {
|
|
|
|
*val = t->val;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
2006-05-21 15:52:24 +00:00
|
|
|
}
|
|
|
|
|
2005-02-04 08:41:44 +00:00
|
|
|
void
|
|
|
|
help(void)
|
|
|
|
{
|
|
|
|
usage(0);
|
|
|
|
fprintf(stderr, "\tCommand Summary:\n\
|
|
|
|
\t-4 Use IPv4\n\
|
2008-05-10 18:50:45 +00:00
|
|
|
\t-6 Use IPv6\n\
|
|
|
|
\t-D Enable the debug socket option\n\
|
|
|
|
\t-d Detach from stdin\n");
|
2005-02-06 14:44:27 +00:00
|
|
|
#ifdef IPSEC
|
|
|
|
fprintf(stderr, "\
|
2008-05-10 18:50:45 +00:00
|
|
|
\t-E Use IPsec ESP\n\
|
|
|
|
\t-e policy Use specified IPsec policy\n");
|
2005-02-06 14:44:27 +00:00
|
|
|
#endif
|
|
|
|
fprintf(stderr, "\
|
2005-02-04 08:41:44 +00:00
|
|
|
\t-h This help text\n\
|
2008-09-27 02:29:03 +00:00
|
|
|
\t-I length TCP receive buffer length\n\
|
2005-02-04 08:41:44 +00:00
|
|
|
\t-i secs\t Delay interval for lines sent, ports scanned\n\
|
|
|
|
\t-k Keep inbound sockets open for multiple connects\n\
|
|
|
|
\t-l Listen mode, for inbound connects\n\
|
|
|
|
\t-n Suppress name/port resolutions\n\
|
2008-12-19 23:00:23 +00:00
|
|
|
\t--no-tcpopt Disable TCP options\n\
|
2008-09-27 02:29:03 +00:00
|
|
|
\t-O length TCP send buffer length\n\
|
2006-05-21 15:52:24 +00:00
|
|
|
\t-P proxyuser\tUsername for proxy authentication\n\
|
2005-02-04 08:41:44 +00:00
|
|
|
\t-p port\t Specify local port for remote connects\n\
|
|
|
|
\t-r Randomize remote ports\n\
|
|
|
|
\t-S Enable the TCP MD5 signature option\n\
|
|
|
|
\t-s addr\t Local source address\n\
|
2012-05-04 18:29:32 +00:00
|
|
|
\t-T toskeyword\tSet IP Type of Service\n\
|
2005-02-04 08:41:44 +00:00
|
|
|
\t-t Answer TELNET negotiation\n\
|
|
|
|
\t-U Use UNIX domain socket\n\
|
|
|
|
\t-u UDP mode\n\
|
2010-10-18 19:44:45 +00:00
|
|
|
\t-V rtable Specify alternate routing table\n\
|
2005-02-04 08:41:44 +00:00
|
|
|
\t-v Verbose\n\
|
|
|
|
\t-w secs\t 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 Zero-I/O mode [used for scanning]\n\
|
|
|
|
Port numbers can be individual or ranges: lo-hi [inclusive]\n");
|
2005-02-06 14:44:27 +00:00
|
|
|
#ifdef IPSEC
|
|
|
|
fprintf(stderr, "See ipsec_set_policy(3) for -e argument format\n");
|
|
|
|
#endif
|
2005-02-04 08:41:44 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-02-06 14:44:27 +00:00
|
|
|
#ifdef IPSEC
|
|
|
|
void
|
|
|
|
add_ipsec_policy(int s, char *policy)
|
|
|
|
{
|
|
|
|
char *raw;
|
|
|
|
int e;
|
|
|
|
|
|
|
|
raw = ipsec_set_policy(policy, strlen(policy));
|
|
|
|
if (raw == NULL)
|
|
|
|
errx(1, "ipsec_set_policy `%s': %s", policy,
|
|
|
|
ipsec_strerror());
|
|
|
|
e = setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, raw,
|
|
|
|
ipsec_get_policylen(raw));
|
|
|
|
if (e < 0)
|
|
|
|
err(1, "ipsec policy cannot be configured");
|
|
|
|
free(raw);
|
|
|
|
if (vflag)
|
|
|
|
fprintf(stderr, "ipsec policy configured: `%s'\n", policy);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif /* IPSEC */
|
|
|
|
|
2005-02-04 08:41:44 +00:00
|
|
|
void
|
|
|
|
usage(int ret)
|
|
|
|
{
|
2009-05-28 23:16:17 +00:00
|
|
|
fprintf(stderr,
|
2005-02-06 14:44:27 +00:00
|
|
|
#ifdef IPSEC
|
2010-04-15 19:15:05 +00:00
|
|
|
"usage: nc [-46DdEhklnrStUuvz] [-e policy] [-I length] [-i interval] [-O length]\n"
|
2005-02-06 14:44:27 +00:00
|
|
|
#else
|
2010-04-15 19:15:05 +00:00
|
|
|
"usage: nc [-46DdhklnrStUuvz] [-I length] [-i interval] [-O length]\n"
|
2005-02-06 14:44:27 +00:00
|
|
|
#endif
|
2011-05-11 18:57:17 +00:00
|
|
|
"\t [-P proxy_username] [-p source_port] [-s source] [-T ToS]\n"
|
2010-10-18 19:44:45 +00:00
|
|
|
"\t [-V rtable] [-w timeout] [-X proxy_protocol]\n"
|
2011-05-11 18:57:17 +00:00
|
|
|
"\t [-x proxy_address[:port]] [destination] [port]\n");
|
2005-02-04 08:41:44 +00:00
|
|
|
if (ret)
|
|
|
|
exit(1);
|
|
|
|
}
|