1 /* $OpenBSD: netcat.c,v 1.130 2015/07/26 19:12:28 chl Exp $ */
3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
32 * Re-written nc(1) for OpenBSD. Original implementation by
33 * *Hobbit* <hobbit@avian.org>.
39 #include <sys/limits.h>
40 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
44 #include <sys/qmath.h>
45 #include <sys/stats.h>
50 #include <netinet/in.h>
52 #include <netipsec/ipsec.h>
54 #include <netinet/tcp.h>
55 #include <netinet/ip.h>
56 #include <arpa/telnet.h>
73 (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
76 #define PORT_MAX 65535
77 #define PORT_MAX_LEN 6
78 #define UNIX_DG_TMP_SOCKET_SIZE 19
86 /* Command Line Options */
87 int dflag; /* detached, no stdin */
88 int Fflag; /* fdpass sock to stdout */
89 unsigned int iflag; /* Interval Flag */
90 int kflag; /* More than one connect */
91 int lflag; /* Bind to local port */
92 int FreeBSD_Mflag; /* Measure using stats(3) */
93 int Nflag; /* shutdown() network socket */
94 int nflag; /* Don't do name look up */
95 int FreeBSD_Oflag; /* Do not use TCP options */
96 int FreeBSD_sctp; /* Use SCTP */
97 char *Pflag; /* Proxy username */
98 char *pflag; /* Localport flag */
99 int rflag; /* Random ports flag */
100 char *sflag; /* Source Address */
101 int tflag; /* Telnet Emulation */
102 int uflag; /* UDP - Default to TCP */
103 int vflag; /* Verbosity */
104 int xflag; /* Socks proxy */
105 int zflag; /* Port Scan Flag */
106 int Dflag; /* sodebug */
107 int Iflag; /* TCP receive buffer size */
108 int Oflag; /* TCP send buffer size */
109 int Sflag; /* TCP MD5 signature option */
110 int Tflag = -1; /* IP Type of Service */
114 int family = AF_UNSPEC;
116 char *portlist[PORT_MAX+1];
117 char *unix_dg_tmp_socket;
119 void atelnet(int, unsigned char *, unsigned int);
120 void build_ports(char *);
122 int local_listen(char *, char *, struct addrinfo);
124 void fdpass(int nfd) __attribute__((noreturn));
125 int remote_connect(const char *, const char *, struct addrinfo);
126 int timeout_connect(int, const struct sockaddr *, socklen_t);
127 int socks_connect(const char *, const char *, struct addrinfo,
128 const char *, const char *, struct addrinfo, int, const char *);
130 int unix_bind(char *);
131 int unix_connect(char *);
132 int unix_listen(char *);
133 void FreeBSD_stats_setup(int);
134 void FreeBSD_stats_print(int);
135 void set_common_sockopts(int, int);
136 int map_tos(char *, int *);
137 void report_connect(const struct sockaddr *, socklen_t);
139 ssize_t drainbuf(int, unsigned char *, size_t *);
140 ssize_t fillbuf(int, unsigned char *, size_t *);
143 void add_ipsec_policy(int, int, char *);
145 char *ipsec_policy[2];
149 FREEBSD_TUN = CHAR_MAX, /* avoid collision with return values from getopt */
153 main(int argc, char *argv[])
155 int ch, s, ret, socksv, ipsec_count;
157 size_t intsize = sizeof(int);
159 struct addrinfo hints;
162 struct sockaddr_storage cliaddr;
164 const char *errstr, *proxyhost = "", *proxyport = NULL, *tundev = NULL;
165 struct addrinfo proxyhints;
166 char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE];
167 struct option longopts[] = {
168 { "no-tcpopt", no_argument, &FreeBSD_Oflag, 1 },
169 { "sctp", no_argument, &FreeBSD_sctp, 1 },
170 { "tun", required_argument, NULL, FREEBSD_TUN },
182 signal(SIGPIPE, SIG_IGN);
184 while ((ch = getopt_long(argc, argv,
185 "46DdEe:FhI:i:klMNnoO:P:p:rSs:tT:UuV:vw:X:x:z",
186 longopts, NULL)) != -1) {
198 if (strcasecmp(optarg, "connect") == 0)
199 socksv = -1; /* HTTP proxy CONNECT */
200 else if (strcmp(optarg, "4") == 0)
201 socksv = 4; /* SOCKS v.4 */
202 else if (strcmp(optarg, "5") == 0)
203 socksv = 5; /* SOCKS v.5 */
205 errx(1, "unsupported proxy protocol");
212 ipsec_policy[ipsec_count++ % 2] = optarg;
214 errx(1, "IPsec support unavailable.");
219 ipsec_policy[0] = "in ipsec esp/transport//require";
220 ipsec_policy[1] = "out ipsec esp/transport//require";
222 errx(1, "IPsec support unavailable.");
232 iflag = strtonum(optarg, 0, UINT_MAX, &errstr);
234 errx(1, "interval %s: %s", errstr, optarg);
244 errx(1, "-M requires stats(3) support");
256 fprintf(stderr, "option -o is deprecated.\n");
277 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
278 errx(1, "Multiple FIBS not supported");
279 rtableid = (int)strtonum(optarg, 0,
280 numfibs - 1, &errstr);
282 errx(1, "rtable %s: %s", errstr, optarg);
288 timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr);
290 errx(1, "timeout %s: %s", errstr, optarg);
295 if ((proxy = strdup(optarg)) == NULL)
305 Iflag = strtonum(optarg, 1, 65536 << 14, &errstr);
307 errx(1, "TCP receive window %s: %s",
311 Oflag = strtonum(optarg, 1, 65536 << 14, &errstr);
312 if (errstr != NULL) {
313 if (strcmp(errstr, "invalid") != 0)
314 errx(1, "TCP send window %s: %s",
324 if (map_tos(optarg, &Tflag))
326 if (strlen(optarg) > 1 && optarg[0] == '0' &&
328 Tflag = (int)strtol(optarg, NULL, 16);
330 Tflag = (int)strtonum(optarg, 0, 255,
332 if (Tflag < 0 || Tflag > 255 || errstr || errno)
333 errx(1, "illegal tos value %s", optarg);
348 /* Cruft to make sure options are clean, and used properly. */
349 if (argv[0] && !argv[1] && family == AF_UNIX) {
352 } else if (argv[0] && !argv[1]) {
357 } else if (argv[0] && argv[1]) {
364 errx(1, "cannot use -s and -l");
366 errx(1, "cannot use -p and -l");
368 errx(1, "cannot use -z and -l");
370 errx(1, "must use -l with -k");
373 errx(1, "cannot use -u and --sctp");
374 if (family == AF_UNIX)
375 errx(1, "cannot use -U and --sctp");
377 if (tundev != NULL) {
379 errx(1, "must use --tun with -u");
380 tun_fd = open(tundev, O_RDWR);
382 errx(1, "unable to open tun device %s", tundev);
385 /* Get name of temporary socket for unix datagram client */
386 if ((family == AF_UNIX) && uflag && !lflag) {
388 unix_dg_tmp_socket = sflag;
390 strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
391 UNIX_DG_TMP_SOCKET_SIZE);
392 if (mktemp(unix_dg_tmp_socket_buf) == NULL)
394 unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
398 /* Initialize addrinfo structure. */
399 if (family != AF_UNIX) {
400 memset(&hints, 0, sizeof(struct addrinfo));
401 hints.ai_family = family;
402 hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
403 hints.ai_protocol = uflag ? IPPROTO_UDP :
404 FreeBSD_sctp ? IPPROTO_SCTP : IPPROTO_TCP;
406 hints.ai_flags |= AI_NUMERICHOST;
411 errx(1, "no proxy support for UDP mode");
414 errx(1, "no proxy support for SCTP mode");
417 errx(1, "no proxy support for listen");
419 if (family == AF_UNIX)
420 errx(1, "no proxy support for unix sockets");
422 /* XXX IPv6 transport to proxy would probably work */
423 if (family == AF_INET6)
424 errx(1, "no proxy support for IPv6");
427 errx(1, "no proxy support for local source address");
429 proxyhost = strsep(&proxy, ":");
432 memset(&proxyhints, 0, sizeof(struct addrinfo));
433 proxyhints.ai_family = family;
434 proxyhints.ai_socktype = SOCK_STREAM;
435 proxyhints.ai_protocol = IPPROTO_TCP;
437 proxyhints.ai_flags |= AI_NUMERICHOST;
444 if (family == AF_UNIX) {
448 s = unix_listen(host);
451 /* Allow only one connection at a time, but stay alive. */
453 if (family != AF_UNIX)
454 s = local_listen(host, uport, hints);
458 * For UDP and -k, don't connect the socket, let it
459 * receive datagrams from multiple socket pairs.
464 * For UDP and not -k, we will use recvfrom() initially
465 * to wait for a caller, then use the regular functions
466 * to talk to the caller.
468 else if (uflag && !kflag) {
471 struct sockaddr_storage z;
475 rv = recvfrom(s, buf, plen, MSG_PEEK,
476 (struct sockaddr *)&z, &len);
480 rv = connect(s, (struct sockaddr *)&z, len);
485 report_connect((struct sockaddr *)&z, len);
489 len = sizeof(cliaddr);
490 connfd = accept(s, (struct sockaddr *)&cliaddr,
493 /* For now, all errnos are fatal */
497 report_connect((struct sockaddr *)&cliaddr, len);
500 FreeBSD_stats_setup(connfd);
505 if (family != AF_UNIX)
508 if (connect(s, NULL, 0) < 0)
515 } else if (family == AF_UNIX) {
518 if ((s = unix_connect(host)) > 0 && !zflag) {
525 unlink(unix_dg_tmp_socket);
531 /* Construct the portlist[] array. */
534 /* Cycle through portlist, connecting to each port. */
535 for (i = 0; portlist[i] != NULL; i++) {
540 s = socks_connect(host, portlist[i], hints,
541 proxyhost, proxyport, proxyhints, socksv,
544 s = remote_connect(host, portlist[i], hints);
550 if (vflag || zflag) {
551 /* For UDP, make sure we are connected. */
553 if (udptest(s) == -1) {
559 /* Don't look up port if -n. */
564 ntohs(atoi(portlist[i])),
565 uflag ? "udp" : "tcp");
569 "Connection to %s %s port [%s/%s] "
570 "succeeded!\n", host, portlist[i],
571 uflag ? "udp" : "tcp",
572 sv ? sv->s_name : "*");
591 * Returns a unix socket bound to the given path
594 unix_bind(char *path)
596 struct sockaddr_un sun;
599 /* Create unix domain socket. */
600 if ((s = socket(AF_UNIX, uflag ? SOCK_DGRAM : SOCK_STREAM,
604 memset(&sun, 0, sizeof(struct sockaddr_un));
605 sun.sun_family = AF_UNIX;
607 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
608 sizeof(sun.sun_path)) {
610 errno = ENAMETOOLONG;
614 if (bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
623 * Returns a socket connected to a local unix socket. Returns -1 on failure.
626 unix_connect(char *path)
628 struct sockaddr_un sun;
632 if ((s = unix_bind(unix_dg_tmp_socket)) < 0)
635 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
638 (void)fcntl(s, F_SETFD, FD_CLOEXEC);
640 memset(&sun, 0, sizeof(struct sockaddr_un));
641 sun.sun_family = AF_UNIX;
643 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
644 sizeof(sun.sun_path)) {
646 errno = ENAMETOOLONG;
649 if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
659 * Create a unix domain socket, and listen on it.
662 unix_listen(char *path)
665 if ((s = unix_bind(path)) < 0)
668 if (listen(s, 5) < 0) {
677 * Returns a socket connected to a remote host. Properly binds to a local
678 * port or source address if needed. Returns -1 on failure.
681 remote_connect(const char *host, const char *port, struct addrinfo hints)
683 struct addrinfo *res, *res0;
684 int s, error, on = 1;
686 if ((error = getaddrinfo(host, port, &hints, &res)))
687 errx(1, "getaddrinfo: %s", gai_strerror(error));
691 if ((s = socket(res0->ai_family, res0->ai_socktype,
692 res0->ai_protocol)) < 0)
695 if (rtableid >= 0 && (setsockopt(s, SOL_SOCKET, SO_SETFIB,
696 &rtableid, sizeof(rtableid)) == -1))
697 err(1, "setsockopt SO_SETFIB");
699 /* Bind to a local port or source address if specified. */
700 if (sflag || pflag) {
701 struct addrinfo ahints, *ares;
703 /* try IP_BINDANY, but don't insist */
704 setsockopt(s, IPPROTO_IP, IP_BINDANY, &on, sizeof(on));
705 memset(&ahints, 0, sizeof(struct addrinfo));
706 ahints.ai_family = res0->ai_family;
707 ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
708 ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
709 ahints.ai_flags = AI_PASSIVE;
710 if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
711 errx(1, "getaddrinfo: %s", gai_strerror(error));
713 if (bind(s, (struct sockaddr *)ares->ai_addr,
714 ares->ai_addrlen) < 0)
715 err(1, "bind failed");
719 set_common_sockopts(s, res0->ai_family);
721 if (timeout_connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
724 warn("connect to %s port %s (%s) failed", host, port,
725 uflag ? "udp" : "tcp");
729 } while ((res0 = res0->ai_next) != NULL);
737 timeout_connect(int s, const struct sockaddr *name, socklen_t namelen)
745 flags = fcntl(s, F_GETFL, 0);
746 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1)
747 err(1, "set non-blocking mode");
750 if ((ret = connect(s, name, namelen)) != 0 && errno == EINPROGRESS) {
752 pfd.events = POLLOUT;
753 if ((ret = poll(&pfd, 1, timeout)) == 1) {
754 optlen = sizeof(optval);
755 if ((ret = getsockopt(s, SOL_SOCKET, SO_ERROR,
756 &optval, &optlen)) == 0) {
758 ret = optval == 0 ? 0 : -1;
760 } else if (ret == 0) {
764 err(1, "poll failed");
767 if (timeout != -1 && fcntl(s, F_SETFL, flags) == -1)
768 err(1, "restoring flags");
775 * Returns a socket listening on a local port, binds to specified source
776 * address. Returns -1 on failure.
779 local_listen(char *host, char *port, struct addrinfo hints)
781 struct addrinfo *res, *res0;
785 /* Allow nodename to be null. */
786 hints.ai_flags |= AI_PASSIVE;
789 * In the case of binding to a wildcard address
790 * default to binding to an ipv4 address.
792 if (host == NULL && hints.ai_family == AF_UNSPEC)
793 hints.ai_family = AF_INET;
795 if ((error = getaddrinfo(host, port, &hints, &res)))
796 errx(1, "getaddrinfo: %s", gai_strerror(error));
800 if ((s = socket(res0->ai_family, res0->ai_socktype,
801 res0->ai_protocol)) < 0)
804 if (rtableid >= 0 && (setsockopt(s, SOL_SOCKET, SO_SETFIB,
805 &rtableid, sizeof(rtableid)) == -1))
806 err(1, "setsockopt SO_SETFIB");
808 ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
813 if (setsockopt(s, IPPROTO_TCP, TCP_NOOPT,
814 &FreeBSD_Oflag, sizeof(FreeBSD_Oflag)) == -1)
815 err(1, "disable TCP options");
818 set_common_sockopts(s, res0->ai_family);
820 if (bind(s, (struct sockaddr *)res0->ai_addr,
821 res0->ai_addrlen) == 0)
826 } while ((res0 = res0->ai_next) != NULL);
828 if (!uflag && s != -1) {
829 if (listen(s, 1) < 0)
840 * Loop that polls on the network file descriptor and stdin.
843 readwrite(int net_fd)
845 struct pollfd pfd[4];
846 int stdin_fd = STDIN_FILENO;
847 int stdout_fd = STDOUT_FILENO;
848 unsigned char netinbuf[BUFSIZE];
849 size_t netinbufpos = 0;
850 unsigned char stdinbuf[BUFSIZE];
851 size_t stdinbufpos = 0;
853 int stats_printed = 0;
856 /* don't read from stdin if requested */
861 pfd[POLL_STDIN].fd = (tun_fd != -1) ? tun_fd : stdin_fd;
862 pfd[POLL_STDIN].events = POLLIN;
865 pfd[POLL_NETOUT].fd = net_fd;
866 pfd[POLL_NETOUT].events = 0;
869 pfd[POLL_NETIN].fd = net_fd;
870 pfd[POLL_NETIN].events = POLLIN;
873 pfd[POLL_STDOUT].fd = (tun_fd != -1) ? tun_fd : stdout_fd;
874 pfd[POLL_STDOUT].events = 0;
877 /* both inputs are gone, buffers are empty, we are done */
878 if (pfd[POLL_STDIN].fd == -1 && pfd[POLL_NETIN].fd == -1
879 && stdinbufpos == 0 && netinbufpos == 0) {
880 if (FreeBSD_Mflag && !stats_printed)
881 FreeBSD_stats_print(net_fd);
885 /* both outputs are gone, we can't continue */
886 if (pfd[POLL_NETOUT].fd == -1 && pfd[POLL_STDOUT].fd == -1) {
887 if (FreeBSD_Mflag && !stats_printed)
888 FreeBSD_stats_print(net_fd);
892 /* listen and net in gone, queues empty, done */
893 if (lflag && pfd[POLL_NETIN].fd == -1
894 && stdinbufpos == 0 && netinbufpos == 0) {
895 if (FreeBSD_Mflag && !stats_printed)
896 FreeBSD_stats_print(net_fd);
901 /* help says -i is for "wait between lines sent". We read and
902 * write arbitrary amounts of data, and we don't want to start
903 * scanning for newlines, so this is as good as it gets */
908 num_fds = poll(pfd, 4, timeout);
910 /* treat poll errors */
913 err(1, "polling error");
916 /* timeout happened */
919 FreeBSD_stats_print(net_fd);
923 /* treat socket error conditions */
924 for (n = 0; n < 4; n++) {
925 if (pfd[n].revents & (POLLERR|POLLNVAL)) {
929 /* reading is possible after HUP */
930 if (pfd[POLL_STDIN].events & POLLIN &&
931 pfd[POLL_STDIN].revents & POLLHUP &&
932 ! (pfd[POLL_STDIN].revents & POLLIN))
933 pfd[POLL_STDIN].fd = -1;
935 if (pfd[POLL_NETIN].events & POLLIN &&
936 pfd[POLL_NETIN].revents & POLLHUP &&
937 ! (pfd[POLL_NETIN].revents & POLLIN))
938 pfd[POLL_NETIN].fd = -1;
940 if (pfd[POLL_NETOUT].revents & POLLHUP) {
942 shutdown(pfd[POLL_NETOUT].fd, SHUT_WR);
943 pfd[POLL_NETOUT].fd = -1;
945 /* if HUP, stop watching stdout */
946 if (pfd[POLL_STDOUT].revents & POLLHUP)
947 pfd[POLL_STDOUT].fd = -1;
948 /* if no net out, stop watching stdin */
949 if (pfd[POLL_NETOUT].fd == -1)
950 pfd[POLL_STDIN].fd = -1;
951 /* if no stdout, stop watching net in */
952 if (pfd[POLL_STDOUT].fd == -1) {
953 if (pfd[POLL_NETIN].fd != -1)
954 shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
955 pfd[POLL_NETIN].fd = -1;
958 /* try to read from stdin */
959 if (pfd[POLL_STDIN].revents & POLLIN && stdinbufpos < BUFSIZE) {
960 ret = fillbuf(pfd[POLL_STDIN].fd, stdinbuf,
962 /* error or eof on stdin - remove from pfd */
963 if (ret == 0 || ret == -1)
964 pfd[POLL_STDIN].fd = -1;
965 /* read something - poll net out */
967 pfd[POLL_NETOUT].events = POLLOUT;
968 /* filled buffer - remove self from polling */
969 if (stdinbufpos == BUFSIZE)
970 pfd[POLL_STDIN].events = 0;
972 /* try to write to network */
973 if (pfd[POLL_NETOUT].revents & POLLOUT && stdinbufpos > 0) {
974 ret = drainbuf(pfd[POLL_NETOUT].fd, stdinbuf,
977 pfd[POLL_NETOUT].fd = -1;
978 /* buffer empty - remove self from polling */
979 if (stdinbufpos == 0)
980 pfd[POLL_NETOUT].events = 0;
981 /* buffer no longer full - poll stdin again */
982 if (stdinbufpos < BUFSIZE)
983 pfd[POLL_STDIN].events = POLLIN;
985 /* try to read from network */
986 if (pfd[POLL_NETIN].revents & POLLIN && netinbufpos < BUFSIZE) {
987 ret = fillbuf(pfd[POLL_NETIN].fd, netinbuf,
990 pfd[POLL_NETIN].fd = -1;
991 /* eof on net in - remove from pfd */
993 shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
994 pfd[POLL_NETIN].fd = -1;
996 /* read something - poll stdout */
998 pfd[POLL_STDOUT].events = POLLOUT;
999 /* filled buffer - remove self from polling */
1000 if (netinbufpos == BUFSIZE)
1001 pfd[POLL_NETIN].events = 0;
1004 atelnet(pfd[POLL_NETIN].fd, netinbuf,
1007 /* try to write to stdout */
1008 if (pfd[POLL_STDOUT].revents & POLLOUT && netinbufpos > 0) {
1009 ret = drainbuf(pfd[POLL_STDOUT].fd, netinbuf,
1012 pfd[POLL_STDOUT].fd = -1;
1013 /* buffer empty - remove self from polling */
1014 if (netinbufpos == 0)
1015 pfd[POLL_STDOUT].events = 0;
1016 /* buffer no longer full - poll net in again */
1017 if (netinbufpos < BUFSIZE)
1018 pfd[POLL_NETIN].events = POLLIN;
1021 /* stdin gone and queue empty? */
1022 if (pfd[POLL_STDIN].fd == -1 && stdinbufpos == 0) {
1023 if (pfd[POLL_NETOUT].fd != -1 && Nflag) {
1024 if (FreeBSD_Mflag) {
1025 FreeBSD_stats_print(net_fd);
1028 shutdown(pfd[POLL_NETOUT].fd, SHUT_WR);
1030 pfd[POLL_NETOUT].fd = -1;
1032 /* net in gone and queue empty? */
1033 if (pfd[POLL_NETIN].fd == -1 && netinbufpos == 0) {
1034 pfd[POLL_STDOUT].fd = -1;
1040 drainbuf(int fd, unsigned char *buf, size_t *bufpos)
1045 n = write(fd, buf, *bufpos);
1046 /* don't treat EAGAIN, EINTR as error */
1047 if (n == -1 && (errno == EAGAIN || errno == EINTR))
1052 adjust = *bufpos - n;
1054 memmove(buf, buf + n, adjust);
1061 fillbuf(int fd, unsigned char *buf, size_t *bufpos)
1063 size_t num = BUFSIZE - *bufpos;
1066 n = read(fd, buf + *bufpos, num);
1067 /* don't treat EAGAIN, EINTR as error */
1068 if (n == -1 && (errno == EAGAIN || errno == EINTR))
1078 * Pass the connected file descriptor to stdout and exit.
1086 char buf[CMSG_SPACE(sizeof(int))];
1088 struct cmsghdr *cmsg;
1094 /* Avoid obvious stupidity */
1095 if (isatty(STDOUT_FILENO))
1096 errx(1, "Cannot pass file descriptor to tty");
1098 bzero(&mh, sizeof(mh));
1099 bzero(&cmsgbuf, sizeof(cmsgbuf));
1100 bzero(&iov, sizeof(iov));
1102 mh.msg_control = (caddr_t)&cmsgbuf.buf;
1103 mh.msg_controllen = sizeof(cmsgbuf.buf);
1104 cmsg = CMSG_FIRSTHDR(&mh);
1105 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
1106 cmsg->cmsg_level = SOL_SOCKET;
1107 cmsg->cmsg_type = SCM_RIGHTS;
1108 *(int *)CMSG_DATA(cmsg) = nfd;
1115 bzero(&pfd, sizeof(pfd));
1116 pfd.fd = STDOUT_FILENO;
1117 pfd.events = POLLOUT;
1119 r = sendmsg(STDOUT_FILENO, &mh, 0);
1121 if (errno == EAGAIN || errno == EINTR) {
1122 if (poll(&pfd, 1, -1) == -1)
1128 errx(1, "sendmsg: unexpected return value %zd", r);
1135 /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
1137 atelnet(int nfd, unsigned char *buf, unsigned int size)
1139 unsigned char *p, *end;
1140 unsigned char obuf[4];
1144 end = buf + size - 2;
1146 for (p = buf; p < end; p++) {
1152 if ((*p == WILL) || (*p == WONT))
1154 else if ((*p == DO) || (*p == DONT))
1161 if (atomicio(vwrite, nfd, obuf, 3) != 3)
1162 warn("Write Error!");
1168 * Build an array of ports in portlist[], listing each port
1169 * that we should try to connect to.
1172 build_ports(char *p)
1179 if ((n = strchr(p, '-')) != NULL) {
1183 /* Make sure the ports are in order: lowest->highest. */
1184 hi = strtonum(n, 1, PORT_MAX, &errstr);
1186 errx(1, "port number %s: %s", errstr, n);
1187 lo = strtonum(p, 1, PORT_MAX, &errstr);
1189 errx(1, "port number %s: %s", errstr, p);
1197 /* Load ports sequentially. */
1198 for (cp = lo; cp <= hi; cp++) {
1199 portlist[x] = calloc(1, PORT_MAX_LEN);
1200 if (portlist[x] == NULL)
1202 snprintf(portlist[x], PORT_MAX_LEN, "%d", cp);
1206 /* Randomly swap ports. */
1211 for (x = 0; x <= (hi - lo); x++) {
1212 y = (arc4random() & 0xFFFF) % (hi - lo);
1214 portlist[x] = portlist[y];
1219 hi = strtonum(p, 1, PORT_MAX, &errstr);
1221 errx(1, "port number %s: %s", errstr, p);
1222 portlist[0] = strdup(p);
1223 if (portlist[0] == NULL)
1230 * Do a few writes to see if the UDP port is there.
1231 * Fails once PF state table is full.
1238 for (i = 0; i <= 3; i++) {
1239 if (write(s, "X", 1) == 1)
1248 FreeBSD_stats_setup(int s)
1251 if (setsockopt(s, IPPROTO_TCP, TCP_STATS,
1252 &FreeBSD_Mflag, sizeof(FreeBSD_Mflag)) == -1) {
1253 if (errno == EOPNOTSUPP) {
1254 warnx("getsockopt(TCP_STATS) failed; "
1255 "kernel built without \"options STATS\"?");
1257 err(1, "enable TCP_STATS gathering");
1262 FreeBSD_stats_print(int s)
1265 struct statsblob *statsb;
1267 socklen_t sockoptlen;
1271 * This usleep is a workaround for TCP_STATS reporting
1272 * incorrect values for TXPB.
1277 statsb = malloc(sockoptlen);
1280 error = getsockopt(s, IPPROTO_TCP, TCP_STATS, statsb, &sockoptlen);
1282 if (errno == EOVERFLOW && statsb->cursz > sockoptlen) {
1283 /* Retry with a larger size. */
1284 sockoptlen = statsb->cursz;
1285 statsb = realloc(statsb, sockoptlen);
1288 error = getsockopt(s, IPPROTO_TCP, TCP_STATS,
1289 statsb, &sockoptlen);
1292 err(1, "getsockopt");
1295 sb = sbuf_new_auto();
1296 error = stats_blob_tostr(statsb, sb, SB_STRFMT_JSON, SB_TOSTR_META);
1298 errc(1, error, "stats_blob_tostr");
1300 error = sbuf_finish(sb);
1302 err(1, "sbuf_finish");
1304 fprintf(stderr, "%s\n", sbuf_data(sb));
1309 set_common_sockopts(int s, int af)
1314 if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
1315 &x, sizeof(x)) == -1)
1319 if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
1320 &x, sizeof(x)) == -1)
1326 if (af == AF_INET6) {
1327 proto = IPPROTO_IPV6;
1328 option = IPV6_TCLASS;
1334 if (setsockopt(s, proto, option, &Tflag, sizeof(Tflag)) == -1)
1335 err(1, "set IP ToS");
1338 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
1339 &Iflag, sizeof(Iflag)) == -1)
1340 err(1, "set TCP receive buffer size");
1343 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
1344 &Oflag, sizeof(Oflag)) == -1)
1345 err(1, "set TCP send buffer size");
1347 if (FreeBSD_Oflag) {
1348 if (setsockopt(s, IPPROTO_TCP, TCP_NOOPT,
1349 &FreeBSD_Oflag, sizeof(FreeBSD_Oflag)) == -1)
1350 err(1, "disable TCP options");
1353 FreeBSD_stats_setup(s);
1355 if (ipsec_policy[0] != NULL)
1356 add_ipsec_policy(s, af, ipsec_policy[0]);
1357 if (ipsec_policy[1] != NULL)
1358 add_ipsec_policy(s, af, ipsec_policy[1]);
1363 map_tos(char *s, int *val)
1365 /* DiffServ Codepoints and other TOS mappings */
1366 const struct toskeywords {
1367 const char *keyword;
1369 } *t, toskeywords[] = {
1370 { "af11", IPTOS_DSCP_AF11 },
1371 { "af12", IPTOS_DSCP_AF12 },
1372 { "af13", IPTOS_DSCP_AF13 },
1373 { "af21", IPTOS_DSCP_AF21 },
1374 { "af22", IPTOS_DSCP_AF22 },
1375 { "af23", IPTOS_DSCP_AF23 },
1376 { "af31", IPTOS_DSCP_AF31 },
1377 { "af32", IPTOS_DSCP_AF32 },
1378 { "af33", IPTOS_DSCP_AF33 },
1379 { "af41", IPTOS_DSCP_AF41 },
1380 { "af42", IPTOS_DSCP_AF42 },
1381 { "af43", IPTOS_DSCP_AF43 },
1382 { "critical", IPTOS_PREC_CRITIC_ECP },
1383 { "cs0", IPTOS_DSCP_CS0 },
1384 { "cs1", IPTOS_DSCP_CS1 },
1385 { "cs2", IPTOS_DSCP_CS2 },
1386 { "cs3", IPTOS_DSCP_CS3 },
1387 { "cs4", IPTOS_DSCP_CS4 },
1388 { "cs5", IPTOS_DSCP_CS5 },
1389 { "cs6", IPTOS_DSCP_CS6 },
1390 { "cs7", IPTOS_DSCP_CS7 },
1391 { "ef", IPTOS_DSCP_EF },
1392 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
1393 { "lowdelay", IPTOS_LOWDELAY },
1394 { "netcontrol", IPTOS_PREC_NETCONTROL },
1395 { "reliability", IPTOS_RELIABILITY },
1396 { "throughput", IPTOS_THROUGHPUT },
1400 for (t = toskeywords; t->keyword != NULL; t++) {
1401 if (strcmp(s, t->keyword) == 0) {
1411 report_connect(const struct sockaddr *sa, socklen_t salen)
1413 char remote_host[NI_MAXHOST];
1414 char remote_port[NI_MAXSERV];
1416 int flags = NI_NUMERICSERV;
1419 flags |= NI_NUMERICHOST;
1421 if ((herr = getnameinfo(sa, salen,
1422 remote_host, sizeof(remote_host),
1423 remote_port, sizeof(remote_port),
1425 if (herr == EAI_SYSTEM)
1426 err(1, "getnameinfo");
1428 errx(1, "getnameinfo: %s", gai_strerror(herr));
1432 "Connection from %s %s "
1433 "received!\n", remote_host, remote_port);
1440 fprintf(stderr, "\tCommand Summary:\n\
1443 \t-D Enable the debug socket option\n\
1444 \t-d Detach from stdin\n");
1447 \t-E Use IPsec ESP\n\
1448 \t-e policy Use specified IPsec policy\n");
1451 \t-F Pass socket fd\n\
1452 \t-h This help text\n\
1453 \t-I length TCP receive buffer length\n\
1454 \t-i secs\t Delay interval for lines sent, ports scanned\n\
1455 \t-k Keep inbound sockets open for multiple connects\n\
1456 \t-l Listen mode, for inbound connects\n\
1457 \t-N Shutdown the network socket after EOF on stdin\n\
1458 \t-n Suppress name/port resolutions\n\
1459 \t--no-tcpopt Disable TCP options\n\
1460 \t--sctp\t SCTP mode\n\
1461 \t--tun tundev Use tun device rather than stdio\n\
1462 \t-O length TCP send buffer length\n\
1463 \t-P proxyuser\tUsername for proxy authentication\n\
1464 \t-p port\t Specify local port for remote connects\n\
1465 \t-r Randomize remote ports\n\
1466 \t-S Enable the TCP MD5 signature option\n\
1467 \t-s addr\t Local source address\n\
1468 \t-T toskeyword\tSet IP Type of Service\n\
1469 \t-t Answer TELNET negotiation\n\
1470 \t-U Use UNIX domain socket\n\
1472 \t-V rtable Specify alternate routing table\n\
1474 \t-w secs\t Timeout for connects and final net reads\n\
1475 \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
1476 \t-x addr[:port]\tSpecify proxy address and port\n\
1477 \t-z Zero-I/O mode [used for scanning]\n\
1478 Port numbers can be individual or ranges: lo-hi [inclusive]\n");
1480 fprintf(stderr, "\tSee ipsec_set_policy(3) for -e argument format\n");
1487 add_ipsec_policy(int s, int af, char *policy)
1492 raw = ipsec_set_policy(policy, strlen(policy));
1494 errx(1, "ipsec_set_policy `%s': %s", policy,
1497 e = setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, raw,
1498 ipsec_get_policylen(raw));
1500 e = setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, raw,
1501 ipsec_get_policylen(raw));
1503 err(1, "ipsec policy cannot be configured");
1506 fprintf(stderr, "ipsec policy configured: `%s'\n", policy);
1516 "usage: nc [-46DdEFhklNnrStUuvz] [-e policy] [-I length] [-i interval] [-O length]\n"
1518 "usage: nc [-46DdFhklNnrStUuvz] [-I length] [-i interval] [-O length]\n"
1520 "\t [--no-tcpopt] [--sctp]\n"
1521 "\t [-P proxy_username] [-p source_port] [-s source] [-T ToS]\n"
1522 "\t [--tun tundev] [-V rtable] [-w timeout] [-X proxy_protocol]\n"
1523 "\t [-x proxy_address[:port]] [destination] [port]\n");