1 /* $OpenBSD: netcat.c,v 1.117 2013/10/26 21:33:29 sthen 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>.
36 #include <sys/limits.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
47 #include <netipsec/ipsec.h>
49 #include <netinet/tcp.h>
50 #include <netinet/ip.h>
51 #include <arpa/telnet.h>
69 (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
72 #define PORT_MAX 65535
73 #define PORT_MAX_LEN 6
74 #define UNIX_DG_TMP_SOCKET_SIZE 19
76 /* Command Line Options */
77 int dflag; /* detached, no stdin */
78 int Fflag; /* fdpass sock to stdout */
79 unsigned int iflag; /* Interval Flag */
80 int kflag; /* More than one connect */
81 int lflag; /* Bind to local port */
82 int Nflag; /* shutdown() network socket */
83 int nflag; /* Don't do name look up */
84 int FreeBSD_Oflag; /* Do not use TCP options */
85 char *Pflag; /* Proxy username */
86 char *pflag; /* Localport flag */
87 int rflag; /* Random ports flag */
88 char *sflag; /* Source Address */
89 int tflag; /* Telnet Emulation */
90 int uflag; /* UDP - Default to TCP */
91 int vflag; /* Verbosity */
92 int xflag; /* Socks proxy */
93 int zflag; /* Port Scan Flag */
94 int Dflag; /* sodebug */
95 int Iflag; /* TCP receive buffer size */
96 int Oflag; /* TCP send buffer size */
97 int Sflag; /* TCP MD5 signature option */
98 int Tflag = -1; /* IP Type of Service */
102 int family = AF_UNSPEC;
103 char *portlist[PORT_MAX+1];
104 char *unix_dg_tmp_socket;
106 void atelnet(int, unsigned char *, unsigned int);
107 void build_ports(char *);
109 int local_listen(char *, char *, struct addrinfo);
111 void fdpass(int nfd) __attribute__((noreturn));
112 int remote_connect(const char *, const char *, struct addrinfo);
113 int timeout_connect(int, const struct sockaddr *, socklen_t);
114 int socks_connect(const char *, const char *, struct addrinfo,
115 const char *, const char *, struct addrinfo, int, const char *);
117 int unix_bind(char *);
118 int unix_connect(char *);
119 int unix_listen(char *);
120 void set_common_sockopts(int);
121 int map_tos(char *, int *);
122 void report_connect(const struct sockaddr *, socklen_t);
126 void add_ipsec_policy(int, char *);
128 char *ipsec_policy[2];
132 main(int argc, char *argv[])
134 int ch, s, ret, socksv, ipsec_count;
136 size_t intsize = sizeof(int);
138 struct addrinfo hints;
141 struct sockaddr_storage cliaddr;
143 const char *errstr, *proxyhost = "", *proxyport = NULL;
144 struct addrinfo proxyhints;
145 char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE];
146 struct option longopts[] = {
147 { "no-tcpopt", no_argument, &FreeBSD_Oflag, 1 },
159 rtableid = getrtable();
162 while ((ch = getopt_long(argc, argv,
163 "46DdEe:FhI:i:klNnoO:P:p:rSs:tT:UuV:vw:X:x:z",
164 longopts, NULL)) != -1) {
176 if (strcasecmp(optarg, "connect") == 0)
177 socksv = -1; /* HTTP proxy CONNECT */
178 else if (strcmp(optarg, "4") == 0)
179 socksv = 4; /* SOCKS v.4 */
180 else if (strcmp(optarg, "5") == 0)
181 socksv = 5; /* SOCKS v.5 */
183 errx(1, "unsupported proxy protocol");
190 ipsec_policy[ipsec_count++ % 2] = optarg;
192 errx(1, "IPsec support unavailable.");
197 ipsec_policy[0] = "in ipsec esp/transport//require";
198 ipsec_policy[1] = "out ipsec esp/transport//require";
200 errx(1, "IPsec support unavailable.");
210 iflag = strtonum(optarg, 0, UINT_MAX, &errstr);
212 errx(1, "interval %s: %s", errstr, optarg);
227 fprintf(stderr, "option -o is deprecated.\n");
248 if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
249 errx(1, "Multiple FIBS not supported");
250 rtableid = (int)strtonum(optarg, 0,
251 numfibs - 1, &errstr);
253 errx(1, "rtable %s: %s", errstr, optarg);
259 timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr);
261 errx(1, "timeout %s: %s", errstr, optarg);
266 if ((proxy = strdup(optarg)) == NULL)
276 Iflag = strtonum(optarg, 1, 65536 << 14, &errstr);
278 errx(1, "TCP receive window %s: %s",
282 Oflag = strtonum(optarg, 1, 65536 << 14, &errstr);
283 if (errstr != NULL) {
284 if (strcmp(errstr, "invalid") != 0)
285 errx(1, "TCP send window %s: %s",
295 if (map_tos(optarg, &Tflag))
297 if (strlen(optarg) > 1 && optarg[0] == '0' &&
299 Tflag = (int)strtol(optarg, NULL, 16);
301 Tflag = (int)strtonum(optarg, 0, 255,
303 if (Tflag < 0 || Tflag > 255 || errstr || errno)
304 errx(1, "illegal tos value %s", optarg);
313 /* Cruft to make sure options are clean, and used properly. */
314 if (argv[0] && !argv[1] && family == AF_UNIX) {
317 } else if (argv[0] && !argv[1]) {
322 } else if (argv[0] && argv[1]) {
329 errx(1, "cannot use -s and -l");
331 errx(1, "cannot use -p and -l");
333 errx(1, "cannot use -z and -l");
335 errx(1, "must use -l with -k");
337 /* Get name of temporary socket for unix datagram client */
338 if ((family == AF_UNIX) && uflag && !lflag) {
340 unix_dg_tmp_socket = sflag;
342 strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
343 UNIX_DG_TMP_SOCKET_SIZE);
344 if (mktemp(unix_dg_tmp_socket_buf) == NULL)
346 unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
350 /* Initialize addrinfo structure. */
351 if (family != AF_UNIX) {
352 memset(&hints, 0, sizeof(struct addrinfo));
353 hints.ai_family = family;
354 hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
355 hints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
357 hints.ai_flags |= AI_NUMERICHOST;
362 errx(1, "no proxy support for UDP mode");
365 errx(1, "no proxy support for listen");
367 if (family == AF_UNIX)
368 errx(1, "no proxy support for unix sockets");
370 /* XXX IPv6 transport to proxy would probably work */
371 if (family == AF_INET6)
372 errx(1, "no proxy support for IPv6");
375 errx(1, "no proxy support for local source address");
377 proxyhost = strsep(&proxy, ":");
380 memset(&proxyhints, 0, sizeof(struct addrinfo));
381 proxyhints.ai_family = family;
382 proxyhints.ai_socktype = SOCK_STREAM;
383 proxyhints.ai_protocol = IPPROTO_TCP;
385 proxyhints.ai_flags |= AI_NUMERICHOST;
392 if (family == AF_UNIX) {
396 s = unix_listen(host);
399 /* Allow only one connection at a time, but stay alive. */
401 if (family != AF_UNIX)
402 s = local_listen(host, uport, hints);
406 * For UDP and -k, don't connect the socket, let it
407 * receive datagrams from multiple socket pairs.
412 * For UDP and not -k, we will use recvfrom() initially
413 * to wait for a caller, then use the regular functions
414 * to talk to the caller.
416 else if (uflag && !kflag) {
419 struct sockaddr_storage z;
423 rv = recvfrom(s, buf, plen, MSG_PEEK,
424 (struct sockaddr *)&z, &len);
428 rv = connect(s, (struct sockaddr *)&z, len);
433 report_connect((struct sockaddr *)&z, len);
437 len = sizeof(cliaddr);
438 connfd = accept(s, (struct sockaddr *)&cliaddr,
441 /* For now, all errnos are fatal */
445 report_connect((struct sockaddr *)&cliaddr, len);
451 if (family != AF_UNIX)
454 if (connect(s, NULL, 0) < 0)
461 } else if (family == AF_UNIX) {
464 if ((s = unix_connect(host)) > 0 && !zflag) {
471 unlink(unix_dg_tmp_socket);
477 /* Construct the portlist[] array. */
480 /* Cycle through portlist, connecting to each port. */
481 for (i = 0; portlist[i] != NULL; i++) {
486 s = socks_connect(host, portlist[i], hints,
487 proxyhost, proxyport, proxyhints, socksv,
490 s = remote_connect(host, portlist[i], hints);
496 if (vflag || zflag) {
497 /* For UDP, make sure we are connected. */
499 if (udptest(s) == -1) {
505 /* Don't look up port if -n. */
510 ntohs(atoi(portlist[i])),
511 uflag ? "udp" : "tcp");
515 "Connection to %s %s port [%s/%s] "
516 "succeeded!\n", host, portlist[i],
517 uflag ? "udp" : "tcp",
518 sv ? sv->s_name : "*");
535 * Returns a unix socket bound to the given path
538 unix_bind(char *path)
540 struct sockaddr_un sun;
543 /* Create unix domain socket. */
544 if ((s = socket(AF_UNIX, uflag ? SOCK_DGRAM : SOCK_STREAM,
548 memset(&sun, 0, sizeof(struct sockaddr_un));
549 sun.sun_family = AF_UNIX;
551 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
552 sizeof(sun.sun_path)) {
554 errno = ENAMETOOLONG;
558 if (bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
567 * Returns a socket connected to a local unix socket. Returns -1 on failure.
570 unix_connect(char *path)
572 struct sockaddr_un sun;
576 if ((s = unix_bind(unix_dg_tmp_socket)) < 0)
579 if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
582 (void)fcntl(s, F_SETFD, FD_CLOEXEC);
584 memset(&sun, 0, sizeof(struct sockaddr_un));
585 sun.sun_family = AF_UNIX;
587 if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
588 sizeof(sun.sun_path)) {
590 errno = ENAMETOOLONG;
593 if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
603 * Create a unix domain socket, and listen on it.
606 unix_listen(char *path)
609 if ((s = unix_bind(path)) < 0)
612 if (listen(s, 5) < 0) {
621 * Returns a socket connected to a remote host. Properly binds to a local
622 * port or source address if needed. Returns -1 on failure.
625 remote_connect(const char *host, const char *port, struct addrinfo hints)
627 struct addrinfo *res, *res0;
628 int s, error, on = 1;
630 if ((error = getaddrinfo(host, port, &hints, &res)))
631 errx(1, "getaddrinfo: %s", gai_strerror(error));
635 if ((s = socket(res0->ai_family, res0->ai_socktype,
636 res0->ai_protocol)) < 0)
639 if (ipsec_policy[0] != NULL)
640 add_ipsec_policy(s, ipsec_policy[0]);
641 if (ipsec_policy[1] != NULL)
642 add_ipsec_policy(s, ipsec_policy[1]);
645 if (rtableid >= 0 && (setsockopt(s, SOL_SOCKET, SO_SETFIB,
646 &rtableid, sizeof(rtableid)) == -1))
647 err(1, "setsockopt SO_SETFIB");
649 /* Bind to a local port or source address if specified. */
650 if (sflag || pflag) {
651 struct addrinfo ahints, *ares;
653 /* try IP_BINDANY, but don't insist */
654 setsockopt(s, IPPROTO_IP, IP_BINDANY, &on, sizeof(on));
655 memset(&ahints, 0, sizeof(struct addrinfo));
656 ahints.ai_family = res0->ai_family;
657 ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
658 ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
659 ahints.ai_flags = AI_PASSIVE;
660 if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
661 errx(1, "getaddrinfo: %s", gai_strerror(error));
663 if (bind(s, (struct sockaddr *)ares->ai_addr,
664 ares->ai_addrlen) < 0)
665 errx(1, "bind failed: %s", strerror(errno));
669 set_common_sockopts(s);
671 if (timeout_connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
674 warn("connect to %s port %s (%s) failed", host, port,
675 uflag ? "udp" : "tcp");
679 } while ((res0 = res0->ai_next) != NULL);
687 timeout_connect(int s, const struct sockaddr *name, socklen_t namelen)
695 flags = fcntl(s, F_GETFL, 0);
696 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1)
697 err(1, "set non-blocking mode");
700 if ((ret = connect(s, name, namelen)) != 0 && errno == EINPROGRESS) {
702 pfd.events = POLLOUT;
703 if ((ret = poll(&pfd, 1, timeout)) == 1) {
704 optlen = sizeof(optval);
705 if ((ret = getsockopt(s, SOL_SOCKET, SO_ERROR,
706 &optval, &optlen)) == 0) {
708 ret = optval == 0 ? 0 : -1;
710 } else if (ret == 0) {
714 err(1, "poll failed");
717 if (timeout != -1 && fcntl(s, F_SETFL, flags) == -1)
718 err(1, "restoring flags");
725 * Returns a socket listening on a local port, binds to specified source
726 * address. Returns -1 on failure.
729 local_listen(char *host, char *port, struct addrinfo hints)
731 struct addrinfo *res, *res0;
735 /* Allow nodename to be null. */
736 hints.ai_flags |= AI_PASSIVE;
739 * In the case of binding to a wildcard address
740 * default to binding to an ipv4 address.
742 if (host == NULL && hints.ai_family == AF_UNSPEC)
743 hints.ai_family = AF_INET;
745 if ((error = getaddrinfo(host, port, &hints, &res)))
746 errx(1, "getaddrinfo: %s", gai_strerror(error));
750 if ((s = socket(res0->ai_family, res0->ai_socktype,
751 res0->ai_protocol)) < 0)
754 if (rtableid >= 0 && (setsockopt(s, IPPROTO_IP, SO_SETFIB,
755 &rtableid, sizeof(rtableid)) == -1))
756 err(1, "setsockopt SO_SETFIB");
758 ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
762 if (ipsec_policy[0] != NULL)
763 add_ipsec_policy(s, ipsec_policy[0]);
764 if (ipsec_policy[1] != NULL)
765 add_ipsec_policy(s, ipsec_policy[1]);
768 if (setsockopt(s, IPPROTO_TCP, TCP_NOOPT,
769 &FreeBSD_Oflag, sizeof(FreeBSD_Oflag)) == -1)
770 err(1, "disable TCP options");
773 if (bind(s, (struct sockaddr *)res0->ai_addr,
774 res0->ai_addrlen) == 0)
779 } while ((res0 = res0->ai_next) != NULL);
781 if (!uflag && s != -1) {
782 if (listen(s, 1) < 0)
793 * Loop that polls on the network file descriptor and stdin.
798 struct pollfd pfd[2];
799 unsigned char buf[16384];
800 int n, wfd = fileno(stdin);
801 int lfd = fileno(stdout);
806 /* Setup Network FD */
808 pfd[0].events = POLLIN;
810 /* Set up STDIN FD. */
812 pfd[1].events = POLLIN;
814 while (pfd[0].fd != -1) {
818 if ((n = poll(pfd, 2 - dflag, timeout)) < 0) {
820 err(1, "Polling Error");
826 if (pfd[0].revents & POLLIN) {
827 if ((n = read(nfd, buf, plen)) < 0)
830 shutdown(nfd, SHUT_RD);
835 atelnet(nfd, buf, n);
836 if (atomicio(vwrite, lfd, buf, n) != n)
841 if (!dflag && pfd[1].revents & POLLIN) {
842 if ((n = read(wfd, buf, plen)) < 0)
846 shutdown(nfd, SHUT_WR);
850 if (atomicio(vwrite, nfd, buf, n) != n)
859 * Pass the connected file descriptor to stdout and exit.
867 char buf[CMSG_SPACE(sizeof(int))];
869 struct cmsghdr *cmsg;
875 /* Avoid obvious stupidity */
876 if (isatty(STDOUT_FILENO))
877 errx(1, "Cannot pass file descriptor to tty");
879 bzero(&mh, sizeof(mh));
880 bzero(&cmsgbuf, sizeof(cmsgbuf));
881 bzero(&iov, sizeof(iov));
882 bzero(&pfd, sizeof(pfd));
884 mh.msg_control = (caddr_t)&cmsgbuf.buf;
885 mh.msg_controllen = sizeof(cmsgbuf.buf);
886 cmsg = CMSG_FIRSTHDR(&mh);
887 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
888 cmsg->cmsg_level = SOL_SOCKET;
889 cmsg->cmsg_type = SCM_RIGHTS;
890 *(int *)CMSG_DATA(cmsg) = nfd;
897 bzero(&pfd, sizeof(pfd));
898 pfd.fd = STDOUT_FILENO;
900 r = sendmsg(STDOUT_FILENO, &mh, 0);
902 if (errno == EAGAIN || errno == EINTR) {
903 pfd.events = POLLOUT;
904 if (poll(&pfd, 1, -1) == -1)
910 errx(1, "sendmsg: unexpected return value %zd", r);
917 /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
919 atelnet(int nfd, unsigned char *buf, unsigned int size)
921 unsigned char *p, *end;
922 unsigned char obuf[4];
926 end = buf + size - 2;
928 for (p = buf; p < end; p++) {
934 if ((*p == WILL) || (*p == WONT))
936 else if ((*p == DO) || (*p == DONT))
943 if (atomicio(vwrite, nfd, obuf, 3) != 3)
944 warn("Write Error!");
950 * Build an array of ports in portlist[], listing each port
951 * that we should try to connect to.
961 if ((n = strchr(p, '-')) != NULL) {
965 /* Make sure the ports are in order: lowest->highest. */
966 hi = strtonum(n, 1, PORT_MAX, &errstr);
968 errx(1, "port number %s: %s", errstr, n);
969 lo = strtonum(p, 1, PORT_MAX, &errstr);
971 errx(1, "port number %s: %s", errstr, p);
979 /* Load ports sequentially. */
980 for (cp = lo; cp <= hi; cp++) {
981 portlist[x] = calloc(1, PORT_MAX_LEN);
982 if (portlist[x] == NULL)
984 snprintf(portlist[x], PORT_MAX_LEN, "%d", cp);
988 /* Randomly swap ports. */
993 for (x = 0; x <= (hi - lo); x++) {
994 y = (arc4random() & 0xFFFF) % (hi - lo);
996 portlist[x] = portlist[y];
1001 hi = strtonum(p, 1, PORT_MAX, &errstr);
1003 errx(1, "port number %s: %s", errstr, p);
1004 portlist[0] = strdup(p);
1005 if (portlist[0] == NULL)
1012 * Do a few writes to see if the UDP port is there.
1013 * Fails once PF state table is full.
1020 for (i = 0; i <= 3; i++) {
1021 if (write(s, "X", 1) == 1)
1030 set_common_sockopts(int s)
1035 if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
1036 &x, sizeof(x)) == -1)
1040 if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
1041 &x, sizeof(x)) == -1)
1045 if (setsockopt(s, IPPROTO_IP, IP_TOS,
1046 &Tflag, sizeof(Tflag)) == -1)
1047 err(1, "set IP ToS");
1050 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
1051 &Iflag, sizeof(Iflag)) == -1)
1052 err(1, "set TCP receive buffer size");
1055 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
1056 &Oflag, sizeof(Oflag)) == -1)
1057 err(1, "set TCP send buffer size");
1059 if (FreeBSD_Oflag) {
1060 if (setsockopt(s, IPPROTO_TCP, TCP_NOOPT,
1061 &FreeBSD_Oflag, sizeof(FreeBSD_Oflag)) == -1)
1062 err(1, "disable TCP options");
1067 map_tos(char *s, int *val)
1069 /* DiffServ Codepoints and other TOS mappings */
1070 const struct toskeywords {
1071 const char *keyword;
1073 } *t, toskeywords[] = {
1074 { "af11", IPTOS_DSCP_AF11 },
1075 { "af12", IPTOS_DSCP_AF12 },
1076 { "af13", IPTOS_DSCP_AF13 },
1077 { "af21", IPTOS_DSCP_AF21 },
1078 { "af22", IPTOS_DSCP_AF22 },
1079 { "af23", IPTOS_DSCP_AF23 },
1080 { "af31", IPTOS_DSCP_AF31 },
1081 { "af32", IPTOS_DSCP_AF32 },
1082 { "af33", IPTOS_DSCP_AF33 },
1083 { "af41", IPTOS_DSCP_AF41 },
1084 { "af42", IPTOS_DSCP_AF42 },
1085 { "af43", IPTOS_DSCP_AF43 },
1086 { "critical", IPTOS_PREC_CRITIC_ECP },
1087 { "cs0", IPTOS_DSCP_CS0 },
1088 { "cs1", IPTOS_DSCP_CS1 },
1089 { "cs2", IPTOS_DSCP_CS2 },
1090 { "cs3", IPTOS_DSCP_CS3 },
1091 { "cs4", IPTOS_DSCP_CS4 },
1092 { "cs5", IPTOS_DSCP_CS5 },
1093 { "cs6", IPTOS_DSCP_CS6 },
1094 { "cs7", IPTOS_DSCP_CS7 },
1095 { "ef", IPTOS_DSCP_EF },
1096 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL },
1097 { "lowdelay", IPTOS_LOWDELAY },
1098 { "netcontrol", IPTOS_PREC_NETCONTROL },
1099 { "reliability", IPTOS_RELIABILITY },
1100 { "throughput", IPTOS_THROUGHPUT },
1104 for (t = toskeywords; t->keyword != NULL; t++) {
1105 if (strcmp(s, t->keyword) == 0) {
1115 report_connect(const struct sockaddr *sa, socklen_t salen)
1117 char remote_host[NI_MAXHOST];
1118 char remote_port[NI_MAXSERV];
1120 int flags = NI_NUMERICSERV;
1123 flags |= NI_NUMERICHOST;
1125 if ((herr = getnameinfo(sa, salen,
1126 remote_host, sizeof(remote_host),
1127 remote_port, sizeof(remote_port),
1129 if (herr == EAI_SYSTEM)
1130 err(1, "getnameinfo");
1132 errx(1, "getnameinfo: %s", gai_strerror(herr));
1136 "Connection from %s %s "
1137 "received!\n", remote_host, remote_port);
1144 fprintf(stderr, "\tCommand Summary:\n\
1147 \t-D Enable the debug socket option\n\
1148 \t-d Detach from stdin\n");
1151 \t-E Use IPsec ESP\n\
1152 \t-e policy Use specified IPsec policy\n");
1155 \t-F Pass socket fd\n\
1156 \t-h This help text\n\
1157 \t-I length TCP receive buffer length\n\
1158 \t-i secs\t Delay interval for lines sent, ports scanned\n\
1159 \t-k Keep inbound sockets open for multiple connects\n\
1160 \t-l Listen mode, for inbound connects\n\
1161 \t-N Shutdown the network socket after EOF on stdin\n\
1162 \t-n Suppress name/port resolutions\n\
1163 \t--no-tcpopt Disable TCP options\n\
1164 \t-O length TCP send buffer length\n\
1165 \t-P proxyuser\tUsername for proxy authentication\n\
1166 \t-p port\t Specify local port for remote connects\n\
1167 \t-r Randomize remote ports\n\
1168 \t-S Enable the TCP MD5 signature option\n\
1169 \t-s addr\t Local source address\n\
1170 \t-T toskeyword\tSet IP Type of Service\n\
1171 \t-t Answer TELNET negotiation\n\
1172 \t-U Use UNIX domain socket\n\
1174 \t-V rtable Specify alternate routing table\n\
1176 \t-w secs\t Timeout for connects and final net reads\n\
1177 \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
1178 \t-x addr[:port]\tSpecify proxy address and port\n\
1179 \t-z Zero-I/O mode [used for scanning]\n\
1180 Port numbers can be individual or ranges: lo-hi [inclusive]\n");
1182 fprintf(stderr, "See ipsec_set_policy(3) for -e argument format\n");
1189 add_ipsec_policy(int s, char *policy)
1194 raw = ipsec_set_policy(policy, strlen(policy));
1196 errx(1, "ipsec_set_policy `%s': %s", policy,
1198 e = setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, raw,
1199 ipsec_get_policylen(raw));
1201 err(1, "ipsec policy cannot be configured");
1204 fprintf(stderr, "ipsec policy configured: `%s'\n", policy);
1214 "usage: nc [-46DdEFhklNnrStUuvz] [-e policy] [-I length] [-i interval] [-O length]\n"
1216 "usage: nc [-46DdFhklNnrStUuvz] [-I length] [-i interval] [-O length]\n"
1218 "\t [-P proxy_username] [-p source_port] [-s source] [-T ToS]\n"
1219 "\t [-V rtable] [-w timeout] [-X proxy_protocol]\n"
1220 "\t [-x proxy_address[:port]] [destination] [port]\n");