1 /* $OpenBSD: netcat.c,v 1.89 2007/02/20 14:11:17 jmc 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.
30 * Re-written nc(1) for OpenBSD. Original implementation by
31 * *Hobbit* <hobbit@avian.org>.
35 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
36 * Use is subject to license terms.
40 * Portions Copyright 2008 Erik Trauschke
43 #include <sys/types.h>
44 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <netinet/in_systm.h>
50 #include <netinet/tcp.h>
51 #include <netinet/ip.h>
52 #include <arpa/telnet.h>
71 (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
75 #define PORT_MAX 65535
76 #define PORT_MAX_LEN 6
77 #define PLIST_SZ 32 /* initial capacity of the portlist */
79 /* Command Line Options */
80 int dflag
; /* detached, no stdin */
81 unsigned int iflag
; /* Interval Flag */
82 int kflag
; /* More than one connect */
83 int lflag
; /* Bind to local port */
84 int nflag
; /* Don't do name lookup */
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 Xflag
; /* indicator of Socks version set */
94 int zflag
; /* Port Scan Flag */
95 int Dflag
; /* sodebug */
96 int Tflag
= -1; /* IP Type of Service */
99 int family
= AF_UNSPEC
;
103 * Used to store a list of ports given by the user and maintaining
104 * information about the number of ports stored.
107 uint16_t *list
; /* list containing the ports */
108 uint_t listsize
; /* capacity of the list (number of entries) */
109 uint_t numports
; /* number of ports in the list */
112 void atelnet(int, unsigned char *, unsigned int);
113 void build_ports(char *);
115 int local_listen(char *, char *, struct addrinfo
);
117 int remote_connect(const char *, const char *, struct addrinfo
);
118 int socks_connect(const char *, const char *,
119 const char *, const char *, struct addrinfo
, int, const char *);
121 int unix_connect(char *);
122 int unix_listen(char *);
123 void set_common_sockopts(int);
124 int parse_iptos(char *);
126 char *print_addr(char *, size_t, struct sockaddr
*, int, int);
129 main(int argc
, char *argv
[])
131 int ch
, s
, ret
, socksv
;
132 char *host
, *uport
, *proxy
;
133 struct addrinfo hints
;
136 struct sockaddr_storage cliaddr
;
137 const char *errstr
, *proxyhost
= "", *proxyport
= NULL
;
138 struct addrinfo proxyhints
;
139 char port
[PORT_MAX_LEN
];
148 while ((ch
= getopt(argc
, argv
,
149 "46Ddhi:klnP:p:rs:T:tUuvw:X:x:z")) != -1) {
162 if (strcasecmp(optarg
, "connect") == 0)
163 socksv
= -1; /* HTTP proxy CONNECT */
164 else if (strcmp(optarg
, "4") == 0)
165 socksv
= 4; /* SOCKS v.4 */
166 else if (strcmp(optarg
, "5") == 0)
167 socksv
= 5; /* SOCKS v.5 */
169 errx(1, "unsupported proxy protocol");
178 iflag
= strtonum(optarg
, 0, UINT_MAX
, &errstr
);
180 errx(1, "interval %s: %s", errstr
, optarg
);
213 timeout
= strtonum(optarg
, 0, INT_MAX
/ 1000, &errstr
);
215 errx(1, "timeout %s: %s", errstr
, optarg
);
220 if ((proxy
= strdup(optarg
)) == NULL
)
230 Tflag
= parse_iptos(optarg
);
239 /* Cruft to make sure options are clean, and used properly. */
240 if (argv
[0] && !argv
[1] && family
== AF_UNIX
) {
242 errx(1, "cannot use -u and -U");
245 } else if (argv
[0] && !argv
[1]) {
250 } else if (argv
[0] && argv
[1]) {
251 if (family
== AF_UNIX
)
256 if (!(lflag
&& pflag
))
264 errx(1, "cannot use -s and -l");
266 errx(1, "cannot use -r and -l");
267 if (lflag
&& (timeout
>= 0))
268 warnx("-w has no effect with -l");
269 if (lflag
&& pflag
) {
275 errx(1, "cannot use -z and -l");
277 errx(1, "must use -l with -k");
278 if (lflag
&& (Pflag
|| xflag
|| Xflag
))
279 errx(1, "cannot use -l with -P, -X or -x");
281 /* Initialize addrinfo structure. */
282 if (family
!= AF_UNIX
) {
283 (void) memset(&hints
, 0, sizeof (struct addrinfo
));
284 hints
.ai_family
= family
;
285 hints
.ai_socktype
= uflag
? SOCK_DGRAM
: SOCK_STREAM
;
286 hints
.ai_protocol
= uflag
? IPPROTO_UDP
: IPPROTO_TCP
;
288 hints
.ai_flags
|= AI_NUMERICHOST
;
293 errx(1, "no proxy support for UDP mode");
296 errx(1, "no proxy support for listen");
298 if (family
== AF_UNIX
)
299 errx(1, "no proxy support for unix sockets");
301 if (family
== AF_INET6
)
302 errx(1, "no proxy support for IPv6");
305 errx(1, "no proxy support for local source address");
307 if ((proxyhost
= strtok(proxy
, ":")) == NULL
)
308 errx(1, "missing port specification");
309 proxyport
= strtok(NULL
, ":");
311 (void) memset(&proxyhints
, 0, sizeof (struct addrinfo
));
312 proxyhints
.ai_family
= family
;
313 proxyhints
.ai_socktype
= SOCK_STREAM
;
314 proxyhints
.ai_protocol
= IPPROTO_TCP
;
316 proxyhints
.ai_flags
|= AI_NUMERICHOST
;
323 if (family
== AF_UNIX
) {
326 s
= unix_listen(host
);
329 /* Allow only one connection at a time, but stay alive. */
331 if (family
!= AF_UNIX
) {
332 /* check if uport is valid */
333 if (strtonum(uport
, PORT_MIN
, PORT_MAX
,
335 errx(1, "port number %s: %s",
337 s
= local_listen(host
, uport
, hints
);
342 * For UDP, we will use recvfrom() initially
343 * to wait for a caller, then use the regular
344 * functions to talk to the caller.
349 struct sockaddr_storage z
;
353 rv
= recvfrom(s
, buf
, plen
, MSG_PEEK
,
354 (struct sockaddr
*)&z
, &len
);
358 rv
= connect(s
, (struct sockaddr
*)&z
, len
);
364 len
= sizeof (cliaddr
);
365 connfd
= accept(s
, (struct sockaddr
*)&cliaddr
,
367 if ((connfd
!= -1) && vflag
) {
368 char ntop
[NI_MAXHOST
+ NI_MAXSERV
];
369 (void) fprintf(stderr
,
370 "Received connection from %s\n",
371 print_addr(ntop
, sizeof (ntop
),
372 (struct sockaddr
*)&cliaddr
, len
,
373 nflag
? NI_NUMERICHOST
: 0));
378 (void) close(connfd
);
379 if (family
!= AF_UNIX
)
385 } else if (family
== AF_UNIX
) {
388 if ((s
= unix_connect(host
)) > 0 && !zflag
) {
396 } else { /* AF_INET or AF_INET6 */
399 /* Construct the portlist. */
402 /* Cycle through portlist, connecting to each port. */
403 for (i
= 0; i
< ports
.numports
; i
++) {
404 (void) snprintf(port
, sizeof (port
), "%u",
411 s
= socks_connect(host
, port
,
412 proxyhost
, proxyport
, proxyhints
, socksv
,
415 s
= remote_connect(host
, port
, hints
);
421 if (vflag
|| zflag
) {
422 /* For UDP, make sure we are connected. */
424 if (udptest(s
) == -1) {
430 /* Don't look up port if -n. */
435 ntohs(ports
.list
[i
]),
436 uflag
? "udp" : "tcp");
439 (void) fprintf(stderr
, "Connection to %s %s "
440 "port [%s/%s] succeeded!\n",
441 host
, port
, uflag
? "udp" : "tcp",
442 sv
? sv
->s_name
: "*");
457 * print IP address and (optionally) a port
460 print_addr(char *ntop
, size_t ntlen
, struct sockaddr
*addr
, int len
, int flags
)
462 char port
[NI_MAXSERV
];
465 /* print port always as number */
466 if ((e
= getnameinfo(addr
, len
, ntop
, ntlen
,
467 port
, sizeof (port
), flags
|NI_NUMERICSERV
)) != 0) {
468 return ((char *)gai_strerror(e
));
471 (void) snprintf(ntop
, ntlen
, "%s port %s", ntop
, port
);
478 * Returns a socket connected to a local unix socket. Returns -1 on failure.
481 unix_connect(char *path
)
483 struct sockaddr_un sunaddr
;
486 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
489 (void) memset(&sunaddr
, 0, sizeof (struct sockaddr_un
));
490 sunaddr
.sun_family
= AF_UNIX
;
492 if (strlcpy(sunaddr
.sun_path
, path
, sizeof (sunaddr
.sun_path
)) >=
493 sizeof (sunaddr
.sun_path
)) {
495 errno
= ENAMETOOLONG
;
498 if (connect(s
, (struct sockaddr
*)&sunaddr
, SUN_LEN(&sunaddr
)) < 0) {
507 * Create a unix domain socket, and listen on it.
510 unix_listen(char *path
)
512 struct sockaddr_un sunaddr
;
515 /* Create unix domain socket. */
516 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0)
519 (void) memset(&sunaddr
, 0, sizeof (struct sockaddr_un
));
520 sunaddr
.sun_family
= AF_UNIX
;
522 if (strlcpy(sunaddr
.sun_path
, path
, sizeof (sunaddr
.sun_path
)) >=
523 sizeof (sunaddr
.sun_path
)) {
525 errno
= ENAMETOOLONG
;
529 if (bind(s
, (struct sockaddr
*)&sunaddr
, SUN_LEN(&sunaddr
)) < 0) {
534 if (listen(s
, 5) < 0) {
543 * Returns a socket connected to a remote host. Properly binds to a local
544 * port or source address if needed. Returns -1 on failure.
547 remote_connect(const char *host
, const char *port
, struct addrinfo hints
)
549 struct addrinfo
*res
, *res0
;
552 if ((error
= getaddrinfo(host
, port
, &hints
, &res
)))
553 errx(1, "getaddrinfo: %s", gai_strerror(error
));
557 if ((s
= socket(res0
->ai_family
, res0
->ai_socktype
,
558 res0
->ai_protocol
)) < 0) {
559 warn("failed to create socket");
563 /* Bind to a local port or source address if specified. */
564 if (sflag
|| pflag
) {
565 struct addrinfo ahints
, *ares
;
567 (void) memset(&ahints
, 0, sizeof (struct addrinfo
));
568 ahints
.ai_family
= res0
->ai_family
;
569 ahints
.ai_socktype
= uflag
? SOCK_DGRAM
: SOCK_STREAM
;
570 ahints
.ai_protocol
= uflag
? IPPROTO_UDP
: IPPROTO_TCP
;
571 ahints
.ai_flags
= AI_PASSIVE
;
572 if ((error
= getaddrinfo(sflag
, pflag
, &ahints
, &ares
)))
573 errx(1, "getaddrinfo: %s", gai_strerror(error
));
575 if (bind(s
, (struct sockaddr
*)ares
->ai_addr
,
576 ares
->ai_addrlen
) < 0)
577 errx(1, "bind failed: %s", strerror(errno
));
580 if (vflag
&& !lflag
) {
582 (void) fprintf(stderr
,
583 "Using source address: %s\n",
586 (void) fprintf(stderr
,
587 "Using source port: %s\n", pflag
);
591 set_common_sockopts(s
);
593 if (connect(s
, res0
->ai_addr
, res0
->ai_addrlen
) == 0)
596 char ntop
[NI_MAXHOST
+ NI_MAXSERV
];
597 warn("connect to %s [host %s] (%s) failed",
598 print_addr(ntop
, sizeof (ntop
),
599 res0
->ai_addr
, res0
->ai_addrlen
, NI_NUMERICHOST
),
600 host
, uflag
? "udp" : "tcp");
605 } while ((res0
= res0
->ai_next
) != NULL
);
614 * Returns a socket listening on a local port, binds to specified source
615 * address. Returns -1 on failure.
618 local_listen(char *host
, char *port
, struct addrinfo hints
)
620 struct addrinfo
*res
, *res0
;
624 /* Allow nodename to be null. */
625 hints
.ai_flags
|= AI_PASSIVE
;
627 if ((error
= getaddrinfo(host
, port
, &hints
, &res
)))
628 errx(1, "getaddrinfo: %s", gai_strerror(error
));
632 if ((s
= socket(res0
->ai_family
, res0
->ai_socktype
,
633 res0
->ai_protocol
)) < 0) {
634 warn("failed to create socket");
638 ret
= setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &x
, sizeof (x
));
642 set_common_sockopts(s
);
644 if (bind(s
, (struct sockaddr
*)res0
->ai_addr
,
645 res0
->ai_addrlen
) == 0)
650 } while ((res0
= res0
->ai_next
) != NULL
);
652 if (!uflag
&& s
!= -1) {
653 if (listen(s
, 1) < 0)
664 * Loop that polls on the network file descriptor and stdin.
669 struct pollfd pfd
[2];
670 unsigned char buf
[8192];
671 int n
, wfd
= fileno(stdin
);
672 int lfd
= fileno(stdout
);
677 /* Setup Network FD */
679 pfd
[0].events
= POLLIN
;
681 /* Set up STDIN FD. */
683 pfd
[1].events
= POLLIN
;
685 while (pfd
[0].fd
!= -1) {
689 if ((n
= poll(pfd
, 2 - dflag
, timeout
)) < 0) {
691 err(1, "Polling Error");
697 if (pfd
[0].revents
& (POLLIN
|POLLHUP
)) {
698 if ((n
= read(nfd
, buf
, plen
)) < 0)
701 (void) shutdown(nfd
, SHUT_RD
);
706 atelnet(nfd
, buf
, n
);
707 if (atomicio(vwrite
, lfd
, buf
, n
) != n
)
713 * handle the case of disconnected pipe: after pipe
714 * is closed (indicated by POLLHUP) there may still
715 * be some data lingering (POLLIN). After we read
716 * the data, only POLLHUP remains, read() returns 0
717 * and we are finished.
719 if (!dflag
&& (pfd
[1].revents
& (POLLIN
|POLLHUP
))) {
720 if ((n
= read(wfd
, buf
, plen
)) < 0)
723 (void) shutdown(nfd
, SHUT_WR
);
727 if (atomicio(vwrite
, nfd
, buf
, n
) != n
)
734 /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
736 atelnet(int nfd
, unsigned char *buf
, unsigned int size
)
738 unsigned char *p
, *end
;
739 unsigned char obuf
[4];
744 for (p
= buf
; p
< end
; p
++) {
751 /* refuse all options */
752 if ((*p
== WILL
) || (*p
== WONT
))
754 if ((*p
== DO
) || (*p
== DONT
))
760 if (atomicio(vwrite
, nfd
, obuf
, 3) != 3)
761 warn("Write Error!");
769 * Build an array of ports in ports.list[], listing each port
770 * that we should try to connect to.
781 /* Set up initial portlist. */
782 ports
.list
= malloc(PLIST_SZ
* sizeof (uint16_t));
783 if (ports
.list
== NULL
)
785 ports
.listsize
= PLIST_SZ
;
788 /* Cycle through list of given ports sep. by "," */
789 while ((token
= strsep(&p
, ",")) != NULL
) {
791 errx(1, "Invalid port/portlist format: "
794 /* check if it is a range */
795 if ((n
= strchr(token
, '-')) != NULL
)
798 lo
= strtonum(token
, PORT_MIN
, PORT_MAX
, &errstr
);
800 errx(1, "port number %s: %s", errstr
, token
);
805 hi
= strtonum(n
, PORT_MIN
, PORT_MAX
, &errstr
);
807 errx(1, "port number %s: %s", errstr
, n
);
816 * Grow the portlist if needed.
817 * We double the size and add size of current range
818 * to make sure we don't have to resize that often.
820 if (hi
- lo
+ ports
.numports
+ 1 >= ports
.listsize
) {
821 ports
.listsize
= ports
.listsize
* 2 + hi
- lo
;
822 ports
.list
= realloc(ports
.list
,
823 ports
.listsize
* sizeof (uint16_t));
824 if (ports
.list
== NULL
)
828 /* Load ports sequentially. */
829 for (i
= lo
; i
<= hi
; i
++)
830 ports
.list
[ports
.numports
++] = i
;
833 /* Randomly swap ports. */
838 if (ports
.numports
< 2) {
839 warnx("can not swap %d port randomly",
844 for (i
= 0; i
< ports
.numports
; i
++) {
845 y
= random() % (ports
.numports
- 1);
847 ports
.list
[i
] = ports
.list
[y
];
855 * Do a few writes to see if the UDP port is there.
856 * XXX - Better way of doing this? Doesn't work for IPv6.
857 * Also fails after around 100 ports checked.
864 for (i
= 0; i
<= 3; i
++) {
865 if (write(s
, "X", 1) == 1)
874 set_common_sockopts(int s
)
879 if (setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, &x
, sizeof (x
)) == -1)
883 if (setsockopt(s
, IPPROTO_IP
, IP_TOS
, &Tflag
,
884 sizeof (Tflag
)) == -1)
885 err(1, "set IP ToS");
894 if (strcmp(s
, "lowdelay") == 0)
895 return (IPTOS_LOWDELAY
);
896 if (strcmp(s
, "throughput") == 0)
897 return (IPTOS_THROUGHPUT
);
898 if (strcmp(s
, "reliability") == 0)
899 return (IPTOS_RELIABILITY
);
901 if (sscanf(s
, "0x%x", (unsigned int *) &tos
) != 1 ||
902 tos
< 0 || tos
> 0xff)
903 errx(1, "invalid IP Type of Service");
911 (void) fprintf(stderr
, "\tCommand Summary:\n\
914 \t-D Enable the debug socket option\n\
915 \t-d Detach from stdin\n\
916 \t-h This help text\n\
917 \t-i secs\t Delay interval for lines sent, ports scanned\n\
918 \t-k Keep inbound sockets open for multiple connects\n\
919 \t-l Listen mode, for inbound connects\n\
920 \t-n Suppress name/port resolutions\n\
921 \t-P proxyuser\tUsername for proxy authentication\n\
922 \t-p port\t Specify local port or listen port\n\
923 \t-r Randomize remote ports\n\
924 \t-s addr\t Local source address\n\
925 \t-T ToS\t Set IP Type of Service\n\
926 \t-t Answer TELNET negotiation\n\
927 \t-U Use UNIX domain socket\n\
930 \t-w secs\t Timeout for connects and final net reads\n\
931 \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
932 \t-x addr[:port]\tSpecify proxy address and port\n\
933 \t-z Zero-I/O mode [used for scanning]\n\
934 Port numbers can be individuals, ranges (lo-hi; inclusive) and\n\
935 combinations of both separated by comma (e.g. 10,22-25,80)\n");
942 (void) fprintf(stderr
,
943 "usage: nc [-46DdhklnrtUuvz] [-i interval] [-P proxy_username]"
945 (void) fprintf(stderr
,
946 "\t [-s source_ip_address] [-T ToS] [-w timeout]"
947 " [-X proxy_protocol]\n");
948 (void) fprintf(stderr
,
949 "\t [-x proxy_address[:port]] [hostname]"