1 /* $OpenBSD: netcat.c,v 1.190 2018/03/19 16:35:29 jsing Exp $ */
3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
4 * Copyright (c) 2015 Bob Beck. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Re-written nc(1) for OpenBSD. Original implementation by
32 * *Hobbit* <hobbit@avian.org>.
35 #include <sys/types.h>
36 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
42 #include <netinet/ip.h>
43 #include <arpa/telnet.h>
61 #define PORT_MAX 65535
62 #define UNIX_DG_TMP_SOCKET_SIZE 19
69 #define DEFAULT_CA_FILE "/etc/ssl/cert.pem"
71 #define TLS_NOVERIFY (1 << 1)
72 #define TLS_NONAME (1 << 2)
73 #define TLS_CCERT (1 << 3)
74 #define TLS_MUSTSTAPLE (1 << 4)
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 char *Pflag
; /* Proxy username */
85 char *pflag
; /* Localport flag */
86 int rflag
; /* Random ports flag */
87 char *sflag
; /* Source Address */
88 int tflag
; /* Telnet Emulation */
89 int uflag
; /* UDP - Default to TCP */
90 int vflag
; /* Verbosity */
91 int xflag
; /* Socks proxy */
92 int zflag
; /* Port Scan Flag */
93 int Dflag
; /* sodebug */
94 int Iflag
; /* TCP receive buffer size */
95 int Oflag
; /* TCP send buffer size */
97 int Sflag
; /* TCP MD5 signature option */
99 int Tflag
= -1; /* IP Type of Service */
104 int usetls
; /* use TLS */
105 char *Cflag
; /* Public cert file */
106 char *Kflag
; /* Private key file */
107 char *oflag
; /* OCSP stapling file */
108 char *Rflag
= DEFAULT_CA_FILE
; /* Root CA file */
109 int tls_cachanged
; /* Using non-default CA file */
110 int TLSopt
; /* TLS options */
111 char *tls_expectname
; /* required name in peer cert */
112 char *tls_expecthash
; /* required hash of peer cert */
113 char *tls_ciphers
; /* TLS ciphers */
114 char *tls_protocols
; /* TLS protocols */
115 FILE *Zflag
; /* file to save peer cert */
117 int recvcount
, recvlimit
;
119 int family
= AF_UNSPEC
;
120 char *portlist
[PORT_MAX
+1];
121 char *unix_dg_tmp_socket
;
125 void atelnet(int, unsigned char *, unsigned int);
126 int strtoport(char *portstr
, int udp
);
127 void build_ports(char *);
128 void help(void) __attribute__((noreturn
));
129 int local_listen(char *, char *, struct addrinfo
);
130 void readwrite(int, struct tls
*);
131 void fdpass(int nfd
) __attribute__((noreturn
));
132 int remote_connect(const char *, const char *, struct addrinfo
);
133 int timeout_tls(int, struct tls
*, int (*)(struct tls
*));
134 int timeout_connect(int, const struct sockaddr
*, socklen_t
);
135 int socks_connect(const char *, const char *, struct addrinfo
,
136 const char *, const char *, struct addrinfo
, int, const char *);
138 int unix_bind(char *, int);
139 int unix_connect(char *);
140 int unix_listen(char *);
141 void set_common_sockopts(int, int);
142 int process_tos_opt(char *, int *);
143 int process_tls_opt(char *, int *);
144 void save_peer_cert(struct tls
*_tls_ctx
, FILE *_fp
);
145 void report_connect(const struct sockaddr
*, socklen_t
, char *);
146 void report_tls(struct tls
*tls_ctx
, char * host
);
148 ssize_t
drainbuf(int, unsigned char *, size_t *, struct tls
*);
149 ssize_t
fillbuf(int, unsigned char *, size_t *, struct tls
*);
150 void tls_setup_client(struct tls
*, int, char *);
151 struct tls
*tls_setup_server(struct tls
*, int, char *);
154 main(int argc
, char *argv
[])
156 int ch
, s
= -1, ret
, socksv
;
158 struct addrinfo hints
;
161 struct sockaddr_storage cliaddr
;
162 char *proxy
= NULL
, *proxyport
= NULL
;
164 struct addrinfo proxyhints
;
165 char unix_dg_tmp_socket_buf
[UNIX_DG_TMP_SOCKET_SIZE
];
166 struct tls_config
*tls_cfg
= NULL
;
167 struct tls
*tls_ctx
= NULL
;
176 signal(SIGPIPE
, SIG_IGN
);
178 while ((ch
= getopt(argc
, argv
,
179 "46C:cDde:FH:hI:i:K:klM:m:NnO:o:P:p:R:rSs:T:tUuV:vW:w:X:x:Z:z"))
192 if (strcasecmp(optarg
, "connect") == 0)
193 socksv
= -1; /* HTTP proxy CONNECT */
194 else if (strcmp(optarg
, "4") == 0)
195 socksv
= 4; /* SOCKS v.4 */
196 else if (strcmp(optarg
, "5") == 0)
197 socksv
= 5; /* SOCKS v.5 */
199 errx(1, "unsupported proxy protocol");
211 tls_expectname
= optarg
;
217 tls_expecthash
= optarg
;
223 iflag
= strtonum(optarg
, 0, UINT_MAX
, &errstr
);
225 errx(1, "interval %s: %s", errstr
, optarg
);
237 ttl
= strtonum(optarg
, 0, 255, &errstr
);
239 errx(1, "ttl is %s", errstr
);
242 minttl
= strtonum(optarg
, 0, 255, &errstr
);
244 errx(1, "minttl is %s", errstr
);
276 rtableid
= (int)strtonum(optarg
, 0,
277 RT_TABLEID_MAX
, &errstr
);
279 errx(1, "rtable %s: %s", errstr
, optarg
);
286 recvlimit
= strtonum(optarg
, 1, INT_MAX
, &errstr
);
288 errx(1, "receive limit %s: %s", errstr
, optarg
);
291 timeout
= strtonum(optarg
, 0, INT_MAX
/ 1000, &errstr
);
293 errx(1, "timeout %s: %s", errstr
, optarg
);
298 if ((proxy
= strdup(optarg
)) == NULL
)
302 if (strcmp(optarg
, "-") == 0)
304 else if ((Zflag
= fopen(optarg
, "w")) == NULL
)
305 err(1, "can't open %s", optarg
);
314 Iflag
= strtonum(optarg
, 1, 65536 << 14, &errstr
);
316 errx(1, "TCP receive window %s: %s",
320 Oflag
= strtonum(optarg
, 1, 65536 << 14, &errstr
);
322 errx(1, "TCP send window %s: %s",
336 if (process_tls_opt(optarg
, &TLSopt
))
338 if (process_tos_opt(optarg
, &Tflag
))
340 if (strlen(optarg
) > 1 && optarg
[0] == '0' &&
342 Tflag
= (int)strtol(optarg
, NULL
, 16);
344 Tflag
= (int)strtonum(optarg
, 0, 255,
346 if (Tflag
< 0 || Tflag
> 255 || errstr
|| errno
)
347 errx(1, "illegal tos/tls value %s", optarg
);
358 if (setrtable(rtableid
) == -1)
362 if (family
== AF_UNIX
) {
363 if (pledge("stdio rpath wpath cpath tmppath unix", NULL
) == -1)
365 } else if (Fflag
&& Pflag
) {
366 if (pledge("stdio inet dns sendfd tty", NULL
) == -1)
369 if (pledge("stdio inet dns sendfd", NULL
) == -1)
371 } else if (Pflag
&& usetls
) {
372 if (pledge("stdio rpath inet dns tty", NULL
) == -1)
375 if (pledge("stdio inet dns tty", NULL
) == -1)
378 if (pledge("stdio rpath inet dns", NULL
) == -1)
380 } else if (pledge("stdio inet dns", NULL
) == -1)
383 /* Cruft to make sure options are clean, and used properly. */
384 if (argv
[0] && !argv
[1] && family
== AF_UNIX
) {
387 } else if (argv
[0] && !argv
[1]) {
392 } else if (argv
[0] && argv
[1]) {
399 errx(1, "cannot use -s and -l");
401 errx(1, "cannot use -p and -l");
403 errx(1, "cannot use -z and -l");
405 errx(1, "must use -l with -k");
407 errx(1, "cannot use -c and -u");
408 if ((family
== AF_UNIX
) && usetls
)
409 errx(1, "cannot use -c and -U");
410 if ((family
== AF_UNIX
) && Fflag
)
411 errx(1, "cannot use -F and -U");
413 errx(1, "cannot use -c and -F");
414 if (TLSopt
&& !usetls
)
415 errx(1, "you must specify -c to use TLS options");
416 if (Cflag
&& !usetls
)
417 errx(1, "you must specify -c to use -C");
418 if (Kflag
&& !usetls
)
419 errx(1, "you must specify -c to use -K");
420 if (Zflag
&& !usetls
)
421 errx(1, "you must specify -c to use -Z");
423 errx(1, "you must specify -C to use -o");
424 if (tls_cachanged
&& !usetls
)
425 errx(1, "you must specify -c to use -R");
426 if (tls_expecthash
&& !usetls
)
427 errx(1, "you must specify -c to use -H");
428 if (tls_expectname
&& !usetls
)
429 errx(1, "you must specify -c to use -e");
431 /* Get name of temporary socket for unix datagram client */
432 if ((family
== AF_UNIX
) && uflag
&& !lflag
) {
434 unix_dg_tmp_socket
= sflag
;
436 strlcpy(unix_dg_tmp_socket_buf
, "/tmp/nc.XXXXXXXXXX",
437 UNIX_DG_TMP_SOCKET_SIZE
);
438 if (mktemp(unix_dg_tmp_socket_buf
) == NULL
)
440 unix_dg_tmp_socket
= unix_dg_tmp_socket_buf
;
444 /* Initialize addrinfo structure. */
445 if (family
!= AF_UNIX
) {
446 memset(&hints
, 0, sizeof(struct addrinfo
));
447 hints
.ai_family
= family
;
448 hints
.ai_socktype
= uflag
? SOCK_DGRAM
: SOCK_STREAM
;
449 hints
.ai_protocol
= uflag
? IPPROTO_UDP
: IPPROTO_TCP
;
451 hints
.ai_flags
|= AI_NUMERICHOST
;
456 errx(1, "no proxy support for UDP mode");
459 errx(1, "no proxy support for listen");
461 if (family
== AF_UNIX
)
462 errx(1, "no proxy support for unix sockets");
465 errx(1, "no proxy support for local source address");
469 proxyport
= strchr(proxy
, ']');
470 if (proxyport
== NULL
)
471 errx(1, "missing closing bracket in proxy");
473 if (*proxyport
== '\0')
474 /* Use default proxy port. */
477 if (*proxyport
== ':')
480 errx(1, "garbage proxy port delimiter");
483 proxyport
= strrchr(proxy
, ':');
484 if (proxyport
!= NULL
)
488 memset(&proxyhints
, 0, sizeof(struct addrinfo
));
489 proxyhints
.ai_family
= family
;
490 proxyhints
.ai_socktype
= SOCK_STREAM
;
491 proxyhints
.ai_protocol
= IPPROTO_TCP
;
493 proxyhints
.ai_flags
|= AI_NUMERICHOST
;
497 if ((tls_cfg
= tls_config_new()) == NULL
)
498 errx(1, "unable to allocate TLS config");
499 if (Rflag
&& tls_config_set_ca_file(tls_cfg
, Rflag
) == -1)
500 errx(1, "%s", tls_config_error(tls_cfg
));
501 if (Cflag
&& tls_config_set_cert_file(tls_cfg
, Cflag
) == -1)
502 errx(1, "%s", tls_config_error(tls_cfg
));
503 if (Kflag
&& tls_config_set_key_file(tls_cfg
, Kflag
) == -1)
504 errx(1, "%s", tls_config_error(tls_cfg
));
505 if (oflag
&& tls_config_set_ocsp_staple_file(tls_cfg
, oflag
) == -1)
506 errx(1, "%s", tls_config_error(tls_cfg
));
507 if (tls_config_parse_protocols(&protocols
, tls_protocols
) == -1)
508 errx(1, "invalid TLS protocols `%s'", tls_protocols
);
509 if (tls_config_set_protocols(tls_cfg
, protocols
) == -1)
510 errx(1, "%s", tls_config_error(tls_cfg
));
511 if (tls_config_set_ciphers(tls_cfg
, tls_ciphers
) == -1)
512 errx(1, "%s", tls_config_error(tls_cfg
));
513 if (!lflag
&& (TLSopt
& TLS_CCERT
))
514 errx(1, "clientcert is only valid with -l");
515 if (TLSopt
& TLS_NONAME
)
516 tls_config_insecure_noverifyname(tls_cfg
);
517 if (TLSopt
& TLS_NOVERIFY
) {
518 if (tls_expecthash
!= NULL
)
519 errx(1, "-H and -T noverify may not be used "
521 tls_config_insecure_noverifycert(tls_cfg
);
523 if (TLSopt
& TLS_MUSTSTAPLE
)
524 tls_config_ocsp_require_stapling(tls_cfg
);
527 if (pledge("stdio inet dns tty", NULL
) == -1)
529 } else if (pledge("stdio inet dns", NULL
) == -1)
533 struct tls
*tls_cctx
= NULL
;
537 if (family
== AF_UNIX
) {
539 s
= unix_bind(host
, 0);
541 s
= unix_listen(host
);
545 tls_config_verify_client_optional(tls_cfg
);
546 if ((tls_ctx
= tls_server()) == NULL
)
547 errx(1, "tls server creation failed");
548 if (tls_configure(tls_ctx
, tls_cfg
) == -1)
549 errx(1, "tls configuration failed (%s)",
552 /* Allow only one connection at a time, but stay alive. */
554 if (family
!= AF_UNIX
)
555 s
= local_listen(host
, uport
, hints
);
558 if (uflag
&& kflag
) {
560 * For UDP and -k, don't connect the socket,
561 * let it receive datagrams from multiple
565 } else if (uflag
&& !kflag
) {
567 * For UDP and not -k, we will use recvfrom()
568 * initially to wait for a caller, then use
569 * the regular functions to talk to the caller.
573 struct sockaddr_storage z
;
576 rv
= recvfrom(s
, buf
, sizeof(buf
), MSG_PEEK
,
577 (struct sockaddr
*)&z
, &len
);
581 rv
= connect(s
, (struct sockaddr
*)&z
, len
);
586 report_connect((struct sockaddr
*)&z
, len
, NULL
);
590 len
= sizeof(cliaddr
);
591 connfd
= accept4(s
, (struct sockaddr
*)&cliaddr
,
592 &len
, SOCK_NONBLOCK
);
594 /* For now, all errnos are fatal */
598 report_connect((struct sockaddr
*)&cliaddr
, len
,
599 family
== AF_UNIX
? host
: NULL
);
601 (tls_cctx
= tls_setup_server(tls_ctx
, connfd
, host
)))
602 readwrite(connfd
, tls_cctx
);
604 readwrite(connfd
, NULL
);
606 timeout_tls(s
, tls_cctx
, tls_close
);
612 if (family
!= AF_UNIX
)
615 if (connect(s
, NULL
, 0) < 0)
622 } else if (family
== AF_UNIX
) {
625 if ((s
= unix_connect(host
)) > 0) {
633 unlink(unix_dg_tmp_socket
);
639 /* Construct the portlist[] array. */
642 /* Cycle through portlist, connecting to each port. */
643 for (s
= -1, i
= 0; portlist
[i
] != NULL
; i
++) {
648 if ((tls_ctx
= tls_client()) == NULL
)
649 errx(1, "tls client creation failed");
650 if (tls_configure(tls_ctx
, tls_cfg
) == -1)
651 errx(1, "tls configuration failed (%s)",
655 s
= socks_connect(host
, portlist
[i
], hints
,
656 proxy
, proxyport
, proxyhints
, socksv
,
659 s
= remote_connect(host
, portlist
[i
], hints
);
665 if (vflag
|| zflag
) {
666 /* For UDP, make sure we are connected. */
668 if (udptest(s
) == -1) {
674 /* Don't look up port if -n. */
679 ntohs(atoi(portlist
[i
])),
680 uflag
? "udp" : "tcp");
684 "Connection to %s %s port [%s/%s] "
685 "succeeded!\n", host
, portlist
[i
],
686 uflag
? "udp" : "tcp",
687 sv
? sv
->s_name
: "*");
693 tls_setup_client(tls_ctx
, s
, host
);
695 readwrite(s
, tls_ctx
);
697 timeout_tls(s
, tls_ctx
, tls_close
);
708 tls_config_free(tls_cfg
);
715 * Returns a unix socket bound to the given path
718 unix_bind(char *path
, int flags
)
720 struct sockaddr_un s_un
;
723 /* Create unix domain socket. */
724 if ((s
= socket(AF_UNIX
, flags
| (uflag
? SOCK_DGRAM
: SOCK_STREAM
),
728 memset(&s_un
, 0, sizeof(struct sockaddr_un
));
729 s_un
.sun_family
= AF_UNIX
;
731 if (strlcpy(s_un
.sun_path
, path
, sizeof(s_un
.sun_path
)) >=
732 sizeof(s_un
.sun_path
)) {
734 errno
= ENAMETOOLONG
;
738 if (bind(s
, (struct sockaddr
*)&s_un
, sizeof(s_un
)) < 0) {
749 timeout_tls(int s
, struct tls
*tls_ctx
, int (*func
)(struct tls
*))
754 while ((ret
= (*func
)(tls_ctx
)) != 0) {
755 if (ret
== TLS_WANT_POLLIN
)
757 else if (ret
== TLS_WANT_POLLOUT
)
758 pfd
.events
= POLLOUT
;
762 if ((ret
= poll(&pfd
, 1, timeout
)) == 1)
769 err(1, "poll failed");
776 tls_setup_client(struct tls
*tls_ctx
, int s
, char *host
)
780 if (tls_connect_socket(tls_ctx
, s
,
781 tls_expectname
? tls_expectname
: host
) == -1) {
782 errx(1, "tls connection failed (%s)",
785 if (timeout_tls(s
, tls_ctx
, tls_handshake
) == -1) {
786 if ((errstr
= tls_error(tls_ctx
)) == NULL
)
787 errstr
= strerror(errno
);
788 errx(1, "tls handshake failed (%s)", errstr
);
791 report_tls(tls_ctx
, host
);
792 if (tls_expecthash
&& tls_peer_cert_hash(tls_ctx
) &&
793 strcmp(tls_expecthash
, tls_peer_cert_hash(tls_ctx
)) != 0)
794 errx(1, "peer certificate is not %s", tls_expecthash
);
796 save_peer_cert(tls_ctx
, Zflag
);
797 if (Zflag
!= stderr
&& (fclose(Zflag
) != 0))
798 err(1, "fclose failed saving peer cert");
803 tls_setup_server(struct tls
*tls_ctx
, int connfd
, char *host
)
805 struct tls
*tls_cctx
;
808 if (tls_accept_socket(tls_ctx
, &tls_cctx
, connfd
) == -1) {
809 warnx("tls accept failed (%s)", tls_error(tls_ctx
));
810 } else if (timeout_tls(connfd
, tls_cctx
, tls_handshake
) == -1) {
811 if ((errstr
= tls_error(tls_cctx
)) == NULL
)
812 errstr
= strerror(errno
);
813 warnx("tls handshake failed (%s)", errstr
);
815 int gotcert
= tls_peer_cert_provided(tls_cctx
);
817 if (vflag
&& gotcert
)
818 report_tls(tls_cctx
, host
);
819 if ((TLSopt
& TLS_CCERT
) && !gotcert
)
820 warnx("No client certificate provided");
821 else if (gotcert
&& tls_peer_cert_hash(tls_ctx
) && tls_expecthash
&&
822 strcmp(tls_expecthash
, tls_peer_cert_hash(tls_ctx
)) != 0)
823 warnx("peer certificate is not %s", tls_expecthash
);
824 else if (gotcert
&& tls_expectname
&&
825 (!tls_peer_cert_contains_name(tls_cctx
, tls_expectname
)))
826 warnx("name (%s) not found in client cert",
837 * Returns a socket connected to a local unix socket. Returns -1 on failure.
840 unix_connect(char *path
)
842 struct sockaddr_un s_un
;
846 if ((s
= unix_bind(unix_dg_tmp_socket
, SOCK_CLOEXEC
)) < 0)
849 if ((s
= socket(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0)) < 0)
853 memset(&s_un
, 0, sizeof(struct sockaddr_un
));
854 s_un
.sun_family
= AF_UNIX
;
856 if (strlcpy(s_un
.sun_path
, path
, sizeof(s_un
.sun_path
)) >=
857 sizeof(s_un
.sun_path
)) {
859 errno
= ENAMETOOLONG
;
862 if (connect(s
, (struct sockaddr
*)&s_un
, sizeof(s_un
)) < 0) {
874 * Create a unix domain socket, and listen on it.
877 unix_listen(char *path
)
880 if ((s
= unix_bind(path
, 0)) < 0)
883 if (listen(s
, 5) < 0) {
892 * Returns a socket connected to a remote host. Properly binds to a local
893 * port or source address if needed. Returns -1 on failure.
896 remote_connect(const char *host
, const char *port
, struct addrinfo hints
)
898 struct addrinfo
*res
, *res0
;
899 int s
= -1, error
, save_errno
;
904 if ((error
= getaddrinfo(host
, port
, &hints
, &res0
)))
905 errx(1, "getaddrinfo for host \"%s\" port %s: %s", host
,
906 port
, gai_strerror(error
));
908 for (res
= res0
; res
; res
= res
->ai_next
) {
909 if ((s
= socket(res
->ai_family
, res
->ai_socktype
|
910 SOCK_NONBLOCK
, res
->ai_protocol
)) < 0)
913 /* Bind to a local port or source address if specified. */
914 if (sflag
|| pflag
) {
915 struct addrinfo ahints
, *ares
;
918 /* try SO_BINDANY, but don't insist */
919 setsockopt(s
, SOL_SOCKET
, SO_BINDANY
, &on
, sizeof(on
));
921 memset(&ahints
, 0, sizeof(struct addrinfo
));
922 ahints
.ai_family
= res
->ai_family
;
923 ahints
.ai_socktype
= uflag
? SOCK_DGRAM
: SOCK_STREAM
;
924 ahints
.ai_protocol
= uflag
? IPPROTO_UDP
: IPPROTO_TCP
;
925 ahints
.ai_flags
= AI_PASSIVE
;
926 if ((error
= getaddrinfo(sflag
, pflag
, &ahints
, &ares
)))
927 errx(1, "getaddrinfo: %s", gai_strerror(error
));
929 if (bind(s
, (struct sockaddr
*)ares
->ai_addr
,
930 ares
->ai_addrlen
) < 0)
931 err(1, "bind failed");
935 set_common_sockopts(s
, res
->ai_family
);
937 if (timeout_connect(s
, res
->ai_addr
, res
->ai_addrlen
) == 0)
940 warn("connect to %s port %s (%s) failed", host
, port
,
941 uflag
? "udp" : "tcp");
955 timeout_connect(int s
, const struct sockaddr
*name
, socklen_t namelen
)
962 if ((ret
= connect(s
, name
, namelen
)) != 0 && errno
== EINPROGRESS
) {
964 pfd
.events
= POLLOUT
;
965 if ((ret
= poll(&pfd
, 1, timeout
)) == 1) {
966 optlen
= sizeof(optval
);
967 if ((ret
= getsockopt(s
, SOL_SOCKET
, SO_ERROR
,
968 &optval
, &optlen
)) == 0) {
970 ret
= optval
== 0 ? 0 : -1;
972 } else if (ret
== 0) {
976 err(1, "poll failed");
984 * Returns a socket listening on a local port, binds to specified source
985 * address. Returns -1 on failure.
988 local_listen(char *host
, char *port
, struct addrinfo hints
)
990 struct addrinfo
*res
, *res0
;
991 int s
= -1, save_errno
;
997 /* Allow nodename to be null. */
998 hints
.ai_flags
|= AI_PASSIVE
;
1001 * In the case of binding to a wildcard address
1002 * default to binding to an ipv4 address.
1004 if (host
== NULL
&& hints
.ai_family
== AF_UNSPEC
)
1005 hints
.ai_family
= AF_INET
;
1007 if ((error
= getaddrinfo(host
, port
, &hints
, &res0
)))
1008 errx(1, "getaddrinfo: %s", gai_strerror(error
));
1010 for (res
= res0
; res
; res
= res
->ai_next
) {
1011 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
1012 res
->ai_protocol
)) < 0)
1016 ret
= setsockopt(s
, SOL_SOCKET
, SO_REUSEPORT
, &x
, sizeof(x
));
1021 set_common_sockopts(s
, res
->ai_family
);
1023 if (bind(s
, (struct sockaddr
*)res
->ai_addr
,
1024 res
->ai_addrlen
) == 0)
1033 if (!uflag
&& s
!= -1) {
1034 if (listen(s
, 1) < 0)
1045 * Loop that polls on the network file descriptor and stdin.
1048 readwrite(int net_fd
, struct tls
*tls_ctx
)
1050 struct pollfd pfd
[4];
1051 int stdin_fd
= STDIN_FILENO
;
1052 int stdout_fd
= STDOUT_FILENO
;
1053 unsigned char netinbuf
[BUFSIZE
];
1054 size_t netinbufpos
= 0;
1055 unsigned char stdinbuf
[BUFSIZE
];
1056 size_t stdinbufpos
= 0;
1060 /* don't read from stdin if requested */
1065 pfd
[POLL_STDIN
].fd
= stdin_fd
;
1066 pfd
[POLL_STDIN
].events
= POLLIN
;
1069 pfd
[POLL_NETOUT
].fd
= net_fd
;
1070 pfd
[POLL_NETOUT
].events
= 0;
1073 pfd
[POLL_NETIN
].fd
= net_fd
;
1074 pfd
[POLL_NETIN
].events
= POLLIN
;
1077 pfd
[POLL_STDOUT
].fd
= stdout_fd
;
1078 pfd
[POLL_STDOUT
].events
= 0;
1081 /* both inputs are gone, buffers are empty, we are done */
1082 if (pfd
[POLL_STDIN
].fd
== -1 && pfd
[POLL_NETIN
].fd
== -1 &&
1083 stdinbufpos
== 0 && netinbufpos
== 0)
1085 /* both outputs are gone, we can't continue */
1086 if (pfd
[POLL_NETOUT
].fd
== -1 && pfd
[POLL_STDOUT
].fd
== -1)
1088 /* listen and net in gone, queues empty, done */
1089 if (lflag
&& pfd
[POLL_NETIN
].fd
== -1 &&
1090 stdinbufpos
== 0 && netinbufpos
== 0)
1093 /* help says -i is for "wait between lines sent". We read and
1094 * write arbitrary amounts of data, and we don't want to start
1095 * scanning for newlines, so this is as good as it gets */
1100 num_fds
= poll(pfd
, 4, timeout
);
1102 /* treat poll errors */
1104 err(1, "polling error");
1106 /* timeout happened */
1110 /* treat socket error conditions */
1111 for (n
= 0; n
< 4; n
++) {
1112 if (pfd
[n
].revents
& (POLLERR
|POLLNVAL
)) {
1116 /* reading is possible after HUP */
1117 if (pfd
[POLL_STDIN
].events
& POLLIN
&&
1118 pfd
[POLL_STDIN
].revents
& POLLHUP
&&
1119 !(pfd
[POLL_STDIN
].revents
& POLLIN
))
1120 pfd
[POLL_STDIN
].fd
= -1;
1122 if (pfd
[POLL_NETIN
].events
& POLLIN
&&
1123 pfd
[POLL_NETIN
].revents
& POLLHUP
&&
1124 !(pfd
[POLL_NETIN
].revents
& POLLIN
))
1125 pfd
[POLL_NETIN
].fd
= -1;
1127 if (pfd
[POLL_NETOUT
].revents
& POLLHUP
) {
1129 shutdown(pfd
[POLL_NETOUT
].fd
, SHUT_WR
);
1130 pfd
[POLL_NETOUT
].fd
= -1;
1132 /* if HUP, stop watching stdout */
1133 if (pfd
[POLL_STDOUT
].revents
& POLLHUP
)
1134 pfd
[POLL_STDOUT
].fd
= -1;
1135 /* if no net out, stop watching stdin */
1136 if (pfd
[POLL_NETOUT
].fd
== -1)
1137 pfd
[POLL_STDIN
].fd
= -1;
1138 /* if no stdout, stop watching net in */
1139 if (pfd
[POLL_STDOUT
].fd
== -1) {
1140 if (pfd
[POLL_NETIN
].fd
!= -1)
1141 shutdown(pfd
[POLL_NETIN
].fd
, SHUT_RD
);
1142 pfd
[POLL_NETIN
].fd
= -1;
1145 /* try to read from stdin */
1146 if (pfd
[POLL_STDIN
].revents
& POLLIN
&& stdinbufpos
< BUFSIZE
) {
1147 ret
= fillbuf(pfd
[POLL_STDIN
].fd
, stdinbuf
,
1148 &stdinbufpos
, NULL
);
1149 if (ret
== TLS_WANT_POLLIN
)
1150 pfd
[POLL_STDIN
].events
= POLLIN
;
1151 else if (ret
== TLS_WANT_POLLOUT
)
1152 pfd
[POLL_STDIN
].events
= POLLOUT
;
1153 else if (ret
== 0 || ret
== -1)
1154 pfd
[POLL_STDIN
].fd
= -1;
1155 /* read something - poll net out */
1156 if (stdinbufpos
> 0)
1157 pfd
[POLL_NETOUT
].events
= POLLOUT
;
1158 /* filled buffer - remove self from polling */
1159 if (stdinbufpos
== BUFSIZE
)
1160 pfd
[POLL_STDIN
].events
= 0;
1162 /* try to write to network */
1163 if (pfd
[POLL_NETOUT
].revents
& POLLOUT
&& stdinbufpos
> 0) {
1164 ret
= drainbuf(pfd
[POLL_NETOUT
].fd
, stdinbuf
,
1165 &stdinbufpos
, tls_ctx
);
1166 if (ret
== TLS_WANT_POLLIN
)
1167 pfd
[POLL_NETOUT
].events
= POLLIN
;
1168 else if (ret
== TLS_WANT_POLLOUT
)
1169 pfd
[POLL_NETOUT
].events
= POLLOUT
;
1171 pfd
[POLL_NETOUT
].fd
= -1;
1172 /* buffer empty - remove self from polling */
1173 if (stdinbufpos
== 0)
1174 pfd
[POLL_NETOUT
].events
= 0;
1175 /* buffer no longer full - poll stdin again */
1176 if (stdinbufpos
< BUFSIZE
)
1177 pfd
[POLL_STDIN
].events
= POLLIN
;
1179 /* try to read from network */
1180 if (pfd
[POLL_NETIN
].revents
& POLLIN
&& netinbufpos
< BUFSIZE
) {
1181 ret
= fillbuf(pfd
[POLL_NETIN
].fd
, netinbuf
,
1182 &netinbufpos
, tls_ctx
);
1183 if (ret
== TLS_WANT_POLLIN
)
1184 pfd
[POLL_NETIN
].events
= POLLIN
;
1185 else if (ret
== TLS_WANT_POLLOUT
)
1186 pfd
[POLL_NETIN
].events
= POLLOUT
;
1188 pfd
[POLL_NETIN
].fd
= -1;
1189 /* eof on net in - remove from pfd */
1191 shutdown(pfd
[POLL_NETIN
].fd
, SHUT_RD
);
1192 pfd
[POLL_NETIN
].fd
= -1;
1194 if (recvlimit
> 0 && ++recvcount
>= recvlimit
) {
1195 if (pfd
[POLL_NETIN
].fd
!= -1)
1196 shutdown(pfd
[POLL_NETIN
].fd
, SHUT_RD
);
1197 pfd
[POLL_NETIN
].fd
= -1;
1198 pfd
[POLL_STDIN
].fd
= -1;
1200 /* read something - poll stdout */
1201 if (netinbufpos
> 0)
1202 pfd
[POLL_STDOUT
].events
= POLLOUT
;
1203 /* filled buffer - remove self from polling */
1204 if (netinbufpos
== BUFSIZE
)
1205 pfd
[POLL_NETIN
].events
= 0;
1208 atelnet(pfd
[POLL_NETIN
].fd
, netinbuf
,
1211 /* try to write to stdout */
1212 if (pfd
[POLL_STDOUT
].revents
& POLLOUT
&& netinbufpos
> 0) {
1213 ret
= drainbuf(pfd
[POLL_STDOUT
].fd
, netinbuf
,
1214 &netinbufpos
, NULL
);
1215 if (ret
== TLS_WANT_POLLIN
)
1216 pfd
[POLL_STDOUT
].events
= POLLIN
;
1217 else if (ret
== TLS_WANT_POLLOUT
)
1218 pfd
[POLL_STDOUT
].events
= POLLOUT
;
1220 pfd
[POLL_STDOUT
].fd
= -1;
1221 /* buffer empty - remove self from polling */
1222 if (netinbufpos
== 0)
1223 pfd
[POLL_STDOUT
].events
= 0;
1224 /* buffer no longer full - poll net in again */
1225 if (netinbufpos
< BUFSIZE
)
1226 pfd
[POLL_NETIN
].events
= POLLIN
;
1229 /* stdin gone and queue empty? */
1230 if (pfd
[POLL_STDIN
].fd
== -1 && stdinbufpos
== 0) {
1231 if (pfd
[POLL_NETOUT
].fd
!= -1 && Nflag
)
1232 shutdown(pfd
[POLL_NETOUT
].fd
, SHUT_WR
);
1233 pfd
[POLL_NETOUT
].fd
= -1;
1235 /* net in gone and queue empty? */
1236 if (pfd
[POLL_NETIN
].fd
== -1 && netinbufpos
== 0) {
1237 pfd
[POLL_STDOUT
].fd
= -1;
1243 drainbuf(int fd
, unsigned char *buf
, size_t *bufpos
, struct tls
*tls
)
1249 n
= tls_write(tls
, buf
, *bufpos
);
1251 n
= write(fd
, buf
, *bufpos
);
1252 /* don't treat EAGAIN, EINTR as error */
1253 if (n
== -1 && (errno
== EAGAIN
|| errno
== EINTR
))
1254 n
= TLS_WANT_POLLOUT
;
1259 adjust
= *bufpos
- n
;
1261 memmove(buf
, buf
+ n
, adjust
);
1267 fillbuf(int fd
, unsigned char *buf
, size_t *bufpos
, struct tls
*tls
)
1269 size_t num
= BUFSIZE
- *bufpos
;
1273 n
= tls_read(tls
, buf
+ *bufpos
, num
);
1275 n
= read(fd
, buf
+ *bufpos
, num
);
1276 /* don't treat EAGAIN, EINTR as error */
1277 if (n
== -1 && (errno
== EAGAIN
|| errno
== EINTR
))
1278 n
= TLS_WANT_POLLIN
;
1288 * Pass the connected file descriptor to stdout and exit.
1296 char buf
[CMSG_SPACE(sizeof(int))];
1298 struct cmsghdr
*cmsg
;
1304 /* Avoid obvious stupidity */
1305 if (isatty(STDOUT_FILENO
))
1306 errx(1, "Cannot pass file descriptor to tty");
1308 bzero(&mh
, sizeof(mh
));
1309 bzero(&cmsgbuf
, sizeof(cmsgbuf
));
1310 bzero(&iov
, sizeof(iov
));
1312 mh
.msg_control
= (caddr_t
)&cmsgbuf
.buf
;
1313 mh
.msg_controllen
= sizeof(cmsgbuf
.buf
);
1314 cmsg
= CMSG_FIRSTHDR(&mh
);
1315 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
1316 cmsg
->cmsg_level
= SOL_SOCKET
;
1317 cmsg
->cmsg_type
= SCM_RIGHTS
;
1318 *(int *)CMSG_DATA(cmsg
) = nfd
;
1325 bzero(&pfd
, sizeof(pfd
));
1326 pfd
.fd
= STDOUT_FILENO
;
1327 pfd
.events
= POLLOUT
;
1329 r
= sendmsg(STDOUT_FILENO
, &mh
, 0);
1331 if (errno
== EAGAIN
|| errno
== EINTR
) {
1332 if (poll(&pfd
, 1, -1) == -1)
1338 errx(1, "sendmsg: unexpected return value %zd", r
);
1345 /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
1347 atelnet(int nfd
, unsigned char *buf
, unsigned int size
)
1349 unsigned char *p
, *end
;
1350 unsigned char obuf
[4];
1354 end
= buf
+ size
- 2;
1356 for (p
= buf
; p
< end
; p
++) {
1362 if ((*p
== WILL
) || (*p
== WONT
))
1364 else if ((*p
== DO
) || (*p
== DONT
))
1371 if (atomicio(vwrite
, nfd
, obuf
, 3) != 3)
1372 warn("Write Error!");
1378 strtoport(char *portstr
, int udp
)
1380 struct servent
*entry
;
1385 proto
= udp
? "udp" : "tcp";
1387 port
= strtonum(portstr
, 1, PORT_MAX
, &errstr
);
1390 if (errno
!= EINVAL
)
1391 errx(1, "port number %s: %s", errstr
, portstr
);
1392 if ((entry
= getservbyname(portstr
, proto
)) == NULL
)
1393 errx(1, "service \"%s\" unknown", portstr
);
1394 return ntohs(entry
->s_port
);
1399 * Build an array of ports in portlist[], listing each port
1400 * that we should try to connect to.
1403 build_ports(char *p
)
1409 if ((n
= strchr(p
, '-')) != NULL
) {
1413 /* Make sure the ports are in order: lowest->highest. */
1414 hi
= strtoport(n
, uflag
);
1415 lo
= strtoport(p
, uflag
);
1423 * Initialize portlist with a random permutation. Based on
1424 * Knuth, as in ip_randomid() in sys/netinet/ip_id.c.
1427 for (x
= 0; x
<= hi
- lo
; x
++) {
1428 cp
= arc4random_uniform(x
+ 1);
1429 portlist
[x
] = portlist
[cp
];
1430 if (asprintf(&portlist
[cp
], "%d", x
+ lo
) < 0)
1433 } else { /* Load ports sequentially. */
1434 for (cp
= lo
; cp
<= hi
; cp
++) {
1435 if (asprintf(&portlist
[x
], "%d", cp
) < 0)
1443 hi
= strtoport(p
, uflag
);
1444 if (asprintf(&tmp
, "%d", hi
) != -1)
1453 * Do a few writes to see if the UDP port is there.
1454 * Fails once PF state table is full.
1461 for (i
= 0; i
<= 3; i
++) {
1462 if (write(s
, "X", 1) == 1)
1471 set_common_sockopts(int s
, int af
)
1477 if (setsockopt(s
, IPPROTO_TCP
, TCP_MD5SIG
,
1478 &x
, sizeof(x
)) == -1)
1483 if (setsockopt(s
, SOL_SOCKET
, SO_DEBUG
,
1484 &x
, sizeof(x
)) == -1)
1488 if (af
== AF_INET
&& setsockopt(s
, IPPROTO_IP
,
1489 IP_TOS
, &Tflag
, sizeof(Tflag
)) == -1)
1490 err(1, "set IP ToS");
1492 else if (af
== AF_INET6
&& setsockopt(s
, IPPROTO_IPV6
,
1493 IPV6_TCLASS
, &Tflag
, sizeof(Tflag
)) == -1)
1494 err(1, "set IPv6 traffic class");
1497 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
,
1498 &Iflag
, sizeof(Iflag
)) == -1)
1499 err(1, "set TCP receive buffer size");
1502 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
,
1503 &Oflag
, sizeof(Oflag
)) == -1)
1504 err(1, "set TCP send buffer size");
1508 if (af
== AF_INET
&& setsockopt(s
, IPPROTO_IP
,
1509 IP_TTL
, &ttl
, sizeof(ttl
)))
1510 err(1, "set IP TTL");
1512 else if (af
== AF_INET6
&& setsockopt(s
, IPPROTO_IPV6
,
1513 IPV6_UNICAST_HOPS
, &ttl
, sizeof(ttl
)))
1514 err(1, "set IPv6 unicast hops");
1519 if (af
== AF_INET
&& setsockopt(s
, IPPROTO_IP
,
1520 IP_MINTTL
, &minttl
, sizeof(minttl
)))
1521 err(1, "set IP min TTL");
1524 #ifdef IPV6_MINHOPCOUNT
1525 else if (af
== AF_INET6
&& setsockopt(s
, IPPROTO_IPV6
,
1526 IPV6_MINHOPCOUNT
, &minttl
, sizeof(minttl
)))
1527 err(1, "set IPv6 min hop count");
1533 process_tos_opt(char *s
, int *val
)
1535 /* DiffServ Codepoints and other TOS mappings */
1536 const struct toskeywords
{
1537 const char *keyword
;
1539 } *t
, toskeywords
[] = {
1540 { "af11", IPTOS_DSCP_AF11
},
1541 { "af12", IPTOS_DSCP_AF12
},
1542 { "af13", IPTOS_DSCP_AF13
},
1543 { "af21", IPTOS_DSCP_AF21
},
1544 { "af22", IPTOS_DSCP_AF22
},
1545 { "af23", IPTOS_DSCP_AF23
},
1546 { "af31", IPTOS_DSCP_AF31
},
1547 { "af32", IPTOS_DSCP_AF32
},
1548 { "af33", IPTOS_DSCP_AF33
},
1549 { "af41", IPTOS_DSCP_AF41
},
1550 { "af42", IPTOS_DSCP_AF42
},
1551 { "af43", IPTOS_DSCP_AF43
},
1552 { "critical", IPTOS_PREC_CRITIC_ECP
},
1553 { "cs0", IPTOS_DSCP_CS0
},
1554 { "cs1", IPTOS_DSCP_CS1
},
1555 { "cs2", IPTOS_DSCP_CS2
},
1556 { "cs3", IPTOS_DSCP_CS3
},
1557 { "cs4", IPTOS_DSCP_CS4
},
1558 { "cs5", IPTOS_DSCP_CS5
},
1559 { "cs6", IPTOS_DSCP_CS6
},
1560 { "cs7", IPTOS_DSCP_CS7
},
1561 { "ef", IPTOS_DSCP_EF
},
1562 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL
},
1563 { "lowdelay", IPTOS_LOWDELAY
},
1564 { "netcontrol", IPTOS_PREC_NETCONTROL
},
1565 { "reliability", IPTOS_RELIABILITY
},
1566 { "throughput", IPTOS_THROUGHPUT
},
1570 for (t
= toskeywords
; t
->keyword
!= NULL
; t
++) {
1571 if (strcmp(s
, t
->keyword
) == 0) {
1581 process_tls_opt(char *s
, int *flags
)
1586 const struct tlskeywords
{
1587 const char *keyword
;
1590 } *t
, tlskeywords
[] = {
1591 { "ciphers", -1, &tls_ciphers
},
1592 { "clientcert", TLS_CCERT
, NULL
},
1593 { "muststaple", TLS_MUSTSTAPLE
, NULL
},
1594 { "noverify", TLS_NOVERIFY
, NULL
},
1595 { "noname", TLS_NONAME
, NULL
},
1596 { "protocols", -1, &tls_protocols
},
1601 if ((v
= strchr(s
, '=')) != NULL
) {
1606 for (t
= tlskeywords
; t
->keyword
!= NULL
; t
++) {
1607 if (strlen(t
->keyword
) == len
&&
1608 strncmp(s
, t
->keyword
, len
) == 0) {
1609 if (t
->value
!= NULL
) {
1611 errx(1, "invalid tls value `%s'", s
);
1623 save_peer_cert(struct tls
*tls_ctx
, FILE *fp
)
1628 if ((pem
= tls_peer_cert_chain_pem(tls_ctx
, &plen
)) == NULL
)
1629 errx(1, "Can't get peer certificate");
1630 if (fprintf(fp
, "%.*s", (int)plen
, pem
) < 0)
1631 err(1, "unable to save peer cert");
1632 if (fflush(fp
) != 0)
1633 err(1, "unable to flush peer cert");
1637 report_tls(struct tls
* tls_ctx
, char * host
)
1640 const char *ocsp_url
;
1642 fprintf(stderr
, "TLS handshake negotiated %s/%s with host %s\n",
1643 tls_conn_version(tls_ctx
), tls_conn_cipher(tls_ctx
), host
);
1644 fprintf(stderr
, "Peer name: %s\n",
1645 tls_expectname
? tls_expectname
: host
);
1646 if (tls_peer_cert_subject(tls_ctx
))
1647 fprintf(stderr
, "Subject: %s\n",
1648 tls_peer_cert_subject(tls_ctx
));
1649 if (tls_peer_cert_issuer(tls_ctx
))
1650 fprintf(stderr
, "Issuer: %s\n",
1651 tls_peer_cert_issuer(tls_ctx
));
1652 if ((t
= tls_peer_cert_notbefore(tls_ctx
)) != -1)
1653 fprintf(stderr
, "Valid From: %s", ctime(&t
));
1654 if ((t
= tls_peer_cert_notafter(tls_ctx
)) != -1)
1655 fprintf(stderr
, "Valid Until: %s", ctime(&t
));
1656 if (tls_peer_cert_hash(tls_ctx
))
1657 fprintf(stderr
, "Cert Hash: %s\n",
1658 tls_peer_cert_hash(tls_ctx
));
1659 ocsp_url
= tls_peer_ocsp_url(tls_ctx
);
1660 if (ocsp_url
!= NULL
)
1661 fprintf(stderr
, "OCSP URL: %s\n", ocsp_url
);
1662 switch (tls_peer_ocsp_response_status(tls_ctx
)) {
1663 case TLS_OCSP_RESPONSE_SUCCESSFUL
:
1664 fprintf(stderr
, "OCSP Stapling: %s\n",
1665 tls_peer_ocsp_result(tls_ctx
) == NULL
? "" :
1666 tls_peer_ocsp_result(tls_ctx
));
1668 " response_status=%d cert_status=%d crl_reason=%d\n",
1669 tls_peer_ocsp_response_status(tls_ctx
),
1670 tls_peer_ocsp_cert_status(tls_ctx
),
1671 tls_peer_ocsp_crl_reason(tls_ctx
));
1672 t
= tls_peer_ocsp_this_update(tls_ctx
);
1673 fprintf(stderr
, " this update: %s",
1674 t
!= -1 ? ctime(&t
) : "\n");
1675 t
= tls_peer_ocsp_next_update(tls_ctx
);
1676 fprintf(stderr
, " next update: %s",
1677 t
!= -1 ? ctime(&t
) : "\n");
1678 t
= tls_peer_ocsp_revocation_time(tls_ctx
);
1679 fprintf(stderr
, " revocation: %s",
1680 t
!= -1 ? ctime(&t
) : "\n");
1685 fprintf(stderr
, "OCSP Stapling: failure - response_status %d (%s)\n",
1686 tls_peer_ocsp_response_status(tls_ctx
),
1687 tls_peer_ocsp_result(tls_ctx
) == NULL
? "" :
1688 tls_peer_ocsp_result(tls_ctx
));
1695 report_connect(const struct sockaddr
*sa
, socklen_t salen
, char *path
)
1697 char remote_host
[NI_MAXHOST
];
1698 char remote_port
[NI_MAXSERV
];
1700 int flags
= NI_NUMERICSERV
;
1703 fprintf(stderr
, "Connection on %s received!\n", path
);
1708 flags
|= NI_NUMERICHOST
;
1710 if ((herr
= getnameinfo(sa
, salen
,
1711 remote_host
, sizeof(remote_host
),
1712 remote_port
, sizeof(remote_port
),
1714 if (herr
== EAI_SYSTEM
)
1715 err(1, "getnameinfo");
1717 errx(1, "getnameinfo: %s", gai_strerror(herr
));
1721 "Connection from %s %s "
1722 "received!\n", remote_host
, remote_port
);
1729 fprintf(stderr
, "\tCommand Summary:\n\
1732 \t-C certfile Public key file\n\
1734 \t-D Enable the debug socket option\n\
1735 \t-d Detach from stdin\n\
1736 \t-e name\t Required name in peer certificate\n\
1737 \t-F Pass socket fd\n\
1738 \t-H hash\t Hash string of peer certificate\n\
1739 \t-h This help text\n\
1740 \t-I length TCP receive buffer length\n\
1741 \t-i interval Delay interval for lines sent, ports scanned\n\
1742 \t-K keyfile Private key file\n\
1743 \t-k Keep inbound sockets open for multiple connects\n\
1744 \t-l Listen mode, for inbound connects\n\
1745 \t-M ttl Outgoing TTL / Hop Limit\n\
1746 \t-m minttl Minimum incoming TTL / Hop Limit\n\
1747 \t-N Shutdown the network socket after EOF on stdin\n\
1748 \t-n Suppress name/port resolutions\n\
1749 \t-O length TCP send buffer length\n\
1750 \t-o staplefile Staple file\n\
1751 \t-P proxyuser\tUsername for proxy authentication\n\
1752 \t-p port\t Specify local port for remote connects\n\
1753 \t-R CAfile CA bundle\n\
1754 \t-r Randomize remote ports\n"
1757 \t-S Enable the TCP MD5 signature option\n"
1760 \t-s source Local source address\n\
1761 \t-T keyword TOS value or TLS options\n\
1762 \t-t Answer TELNET negotiation\n\
1763 \t-U Use UNIX domain socket\n\
1767 \t-V rtable Specify alternate routing table\n"
1771 \t-W recvlimit Terminate after receiving a number of packets\n\
1772 \t-w timeout Timeout for connects and final net reads\n\
1773 \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
1774 \t-x addr[:port]\tSpecify proxy address and port\n\
1775 \t-Z Peer certificate file\n\
1776 \t-z Zero-I/O mode [used for scanning]\n\
1777 Port numbers can be individual or ranges: lo-hi [inclusive]\n");
1785 "usage: nc [-46cDdFhklNnrStUuvz] [-C certfile] [-e name] "
1786 "[-H hash] [-I length]\n"
1787 "\t [-i interval] [-K keyfile] [-M ttl] [-m minttl] [-O length]\n"
1788 "\t [-o staplefile] [-P proxy_username] [-p source_port] "
1790 "\t [-s source] [-T keyword] [-V rtable] [-W recvlimit] "
1792 "\t [-X proxy_protocol] [-x proxy_address[:port]] "
1793 "[-Z peercertfile]\n"
1794 "\t [destination] [port]\n");