1 /* $OpenBSD: netcat.c,v 1.187 2017/07/15 17:27:39 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_ALL (1 << 1)
72 #define TLS_NOVERIFY (1 << 2)
73 #define TLS_NONAME (1 << 3)
74 #define TLS_CCERT (1 << 4)
75 #define TLS_MUSTSTAPLE (1 << 5)
76 #define TLS_COMPAT (1 << 6)
78 /* Command Line Options */
79 int dflag
; /* detached, no stdin */
80 int Fflag
; /* fdpass sock to stdout */
81 unsigned int iflag
; /* Interval Flag */
82 int kflag
; /* More than one connect */
83 int lflag
; /* Bind to local port */
84 int Nflag
; /* shutdown() network socket */
85 int nflag
; /* Don't do name look up */
86 char *Pflag
; /* Proxy username */
87 char *pflag
; /* Localport flag */
88 int rflag
; /* Random ports flag */
89 char *sflag
; /* Source Address */
90 int tflag
; /* Telnet Emulation */
91 int uflag
; /* UDP - Default to TCP */
92 int vflag
; /* Verbosity */
93 int xflag
; /* Socks proxy */
94 int zflag
; /* Port Scan Flag */
95 int Dflag
; /* sodebug */
96 int Iflag
; /* TCP receive buffer size */
97 int Oflag
; /* TCP send buffer size */
99 int Sflag
; /* TCP MD5 signature option */
101 int Tflag
= -1; /* IP Type of Service */
106 int usetls
; /* use TLS */
107 char *Cflag
; /* Public cert file */
108 char *Kflag
; /* Private key file */
109 char *oflag
; /* OCSP stapling file */
110 char *Rflag
= DEFAULT_CA_FILE
; /* Root CA file */
111 int tls_cachanged
; /* Using non-default CA file */
112 int TLSopt
; /* TLS options */
113 char *tls_expectname
; /* required name in peer cert */
114 char *tls_expecthash
; /* required hash of peer cert */
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 map_tos(char *, int *);
143 int map_tls(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
;
175 signal(SIGPIPE
, SIG_IGN
);
177 while ((ch
= getopt(argc
, argv
,
178 "46C:cDde:FH:hI:i:K:klM:m:NnO:o:P:p:R:rSs:T:tUuV:vW:w:X:x:Z:z"))
191 if (strcasecmp(optarg
, "connect") == 0)
192 socksv
= -1; /* HTTP proxy CONNECT */
193 else if (strcmp(optarg
, "4") == 0)
194 socksv
= 4; /* SOCKS v.4 */
195 else if (strcmp(optarg
, "5") == 0)
196 socksv
= 5; /* SOCKS v.5 */
198 errx(1, "unsupported proxy protocol");
210 tls_expectname
= optarg
;
216 tls_expecthash
= optarg
;
222 iflag
= strtonum(optarg
, 0, UINT_MAX
, &errstr
);
224 errx(1, "interval %s: %s", errstr
, optarg
);
236 ttl
= strtonum(optarg
, 0, 255, &errstr
);
238 errx(1, "ttl is %s", errstr
);
241 minttl
= strtonum(optarg
, 0, 255, &errstr
);
243 errx(1, "minttl is %s", errstr
);
275 rtableid
= (int)strtonum(optarg
, 0,
276 RT_TABLEID_MAX
, &errstr
);
278 errx(1, "rtable %s: %s", errstr
, optarg
);
285 recvlimit
= strtonum(optarg
, 1, INT_MAX
, &errstr
);
287 errx(1, "receive limit %s: %s", errstr
, optarg
);
290 timeout
= strtonum(optarg
, 0, INT_MAX
/ 1000, &errstr
);
292 errx(1, "timeout %s: %s", errstr
, optarg
);
297 if ((proxy
= strdup(optarg
)) == NULL
)
301 if (strcmp(optarg
, "-") == 0)
303 else if ((Zflag
= fopen(optarg
, "w")) == NULL
)
304 err(1, "can't open %s", optarg
);
313 Iflag
= strtonum(optarg
, 1, 65536 << 14, &errstr
);
315 errx(1, "TCP receive window %s: %s",
319 Oflag
= strtonum(optarg
, 1, 65536 << 14, &errstr
);
321 errx(1, "TCP send window %s: %s",
335 if (map_tos(optarg
, &Tflag
))
337 if (map_tls(optarg
, &TLSopt
))
339 if (strlen(optarg
) > 1 && optarg
[0] == '0' &&
341 Tflag
= (int)strtol(optarg
, NULL
, 16);
343 Tflag
= (int)strtonum(optarg
, 0, 255,
345 if (Tflag
< 0 || Tflag
> 255 || errstr
|| errno
)
346 errx(1, "illegal tos/tls value %s", optarg
);
357 if (setrtable(rtableid
) == -1)
361 if (family
== AF_UNIX
) {
362 if (pledge("stdio rpath wpath cpath tmppath unix", NULL
) == -1)
364 } else if (Fflag
&& Pflag
) {
365 if (pledge("stdio inet dns sendfd tty", NULL
) == -1)
368 if (pledge("stdio inet dns sendfd", NULL
) == -1)
370 } else if (Pflag
&& usetls
) {
371 if (pledge("stdio rpath inet dns tty", NULL
) == -1)
374 if (pledge("stdio inet dns tty", NULL
) == -1)
377 if (pledge("stdio rpath inet dns", NULL
) == -1)
379 } else if (pledge("stdio inet dns", NULL
) == -1)
382 /* Cruft to make sure options are clean, and used properly. */
383 if (argv
[0] && !argv
[1] && family
== AF_UNIX
) {
386 } else if (argv
[0] && !argv
[1]) {
391 } else if (argv
[0] && argv
[1]) {
398 errx(1, "cannot use -s and -l");
400 errx(1, "cannot use -p and -l");
402 errx(1, "cannot use -z and -l");
404 errx(1, "must use -l with -k");
406 errx(1, "cannot use -c and -u");
407 if ((family
== AF_UNIX
) && usetls
)
408 errx(1, "cannot use -c and -U");
409 if ((family
== AF_UNIX
) && Fflag
)
410 errx(1, "cannot use -F and -U");
412 errx(1, "cannot use -c and -F");
413 if (TLSopt
&& !usetls
)
414 errx(1, "you must specify -c to use TLS options");
415 if ((TLSopt
& (TLS_ALL
|TLS_COMPAT
)) == (TLS_ALL
|TLS_COMPAT
))
416 errx(1, "cannot use -T tlsall and -T tlscompat");
417 if (Cflag
&& !usetls
)
418 errx(1, "you must specify -c to use -C");
419 if (Kflag
&& !usetls
)
420 errx(1, "you must specify -c to use -K");
421 if (Zflag
&& !usetls
)
422 errx(1, "you must specify -c to use -Z");
424 errx(1, "you must specify -C to use -o");
425 if (tls_cachanged
&& !usetls
)
426 errx(1, "you must specify -c to use -R");
427 if (tls_expecthash
&& !usetls
)
428 errx(1, "you must specify -c to use -H");
429 if (tls_expectname
&& !usetls
)
430 errx(1, "you must specify -c to use -e");
432 /* Get name of temporary socket for unix datagram client */
433 if ((family
== AF_UNIX
) && uflag
&& !lflag
) {
435 unix_dg_tmp_socket
= sflag
;
437 strlcpy(unix_dg_tmp_socket_buf
, "/tmp/nc.XXXXXXXXXX",
438 UNIX_DG_TMP_SOCKET_SIZE
);
439 if (mktemp(unix_dg_tmp_socket_buf
) == NULL
)
441 unix_dg_tmp_socket
= unix_dg_tmp_socket_buf
;
445 /* Initialize addrinfo structure. */
446 if (family
!= AF_UNIX
) {
447 memset(&hints
, 0, sizeof(struct addrinfo
));
448 hints
.ai_family
= family
;
449 hints
.ai_socktype
= uflag
? SOCK_DGRAM
: SOCK_STREAM
;
450 hints
.ai_protocol
= uflag
? IPPROTO_UDP
: IPPROTO_TCP
;
452 hints
.ai_flags
|= AI_NUMERICHOST
;
457 errx(1, "no proxy support for UDP mode");
460 errx(1, "no proxy support for listen");
462 if (family
== AF_UNIX
)
463 errx(1, "no proxy support for unix sockets");
466 errx(1, "no proxy support for local source address");
470 proxyport
= strchr(proxy
, ']');
471 if (proxyport
== NULL
)
472 errx(1, "missing closing bracket in proxy");
474 if (*proxyport
== '\0')
475 /* Use default proxy port. */
478 if (*proxyport
== ':')
481 errx(1, "garbage proxy port delimiter");
484 proxyport
= strrchr(proxy
, ':');
485 if (proxyport
!= NULL
)
489 memset(&proxyhints
, 0, sizeof(struct addrinfo
));
490 proxyhints
.ai_family
= family
;
491 proxyhints
.ai_socktype
= SOCK_STREAM
;
492 proxyhints
.ai_protocol
= IPPROTO_TCP
;
494 proxyhints
.ai_flags
|= AI_NUMERICHOST
;
498 if (tls_init() == -1)
499 errx(1, "unable to initialize TLS");
500 if ((tls_cfg
= tls_config_new()) == NULL
)
501 errx(1, "unable to allocate TLS config");
502 if (Rflag
&& tls_config_set_ca_file(tls_cfg
, Rflag
) == -1)
503 errx(1, "%s", tls_config_error(tls_cfg
));
504 if (Cflag
&& tls_config_set_cert_file(tls_cfg
, Cflag
) == -1)
505 errx(1, "%s", tls_config_error(tls_cfg
));
506 if (Kflag
&& tls_config_set_key_file(tls_cfg
, Kflag
) == -1)
507 errx(1, "%s", tls_config_error(tls_cfg
));
508 if (oflag
&& tls_config_set_ocsp_staple_file(tls_cfg
, oflag
) == -1)
509 errx(1, "%s", tls_config_error(tls_cfg
));
510 if (TLSopt
& (TLS_ALL
|TLS_COMPAT
)) {
511 if (tls_config_set_protocols(tls_cfg
,
512 TLS_PROTOCOLS_ALL
) != 0)
513 errx(1, "%s", tls_config_error(tls_cfg
));
514 if (tls_config_set_ciphers(tls_cfg
,
515 (TLSopt
& TLS_ALL
) ? "all" : "compat") != 0)
516 errx(1, "%s", tls_config_error(tls_cfg
));
518 if (!lflag
&& (TLSopt
& TLS_CCERT
))
519 errx(1, "clientcert is only valid with -l");
520 if (TLSopt
& TLS_NONAME
)
521 tls_config_insecure_noverifyname(tls_cfg
);
522 if (TLSopt
& TLS_NOVERIFY
) {
523 if (tls_expecthash
!= NULL
)
524 errx(1, "-H and -T noverify may not be used "
526 tls_config_insecure_noverifycert(tls_cfg
);
528 if (TLSopt
& TLS_MUSTSTAPLE
)
529 tls_config_ocsp_require_stapling(tls_cfg
);
532 if (pledge("stdio inet dns tty", NULL
) == -1)
534 } else if (pledge("stdio inet dns", NULL
) == -1)
538 struct tls
*tls_cctx
= NULL
;
542 if (family
== AF_UNIX
) {
544 s
= unix_bind(host
, 0);
546 s
= unix_listen(host
);
550 tls_config_verify_client_optional(tls_cfg
);
551 if ((tls_ctx
= tls_server()) == NULL
)
552 errx(1, "tls server creation failed");
553 if (tls_configure(tls_ctx
, tls_cfg
) == -1)
554 errx(1, "tls configuration failed (%s)",
557 /* Allow only one connection at a time, but stay alive. */
559 if (family
!= AF_UNIX
)
560 s
= local_listen(host
, uport
, hints
);
563 if (uflag
&& kflag
) {
565 * For UDP and -k, don't connect the socket,
566 * let it receive datagrams from multiple
570 } else if (uflag
&& !kflag
) {
572 * For UDP and not -k, we will use recvfrom()
573 * initially to wait for a caller, then use
574 * the regular functions to talk to the caller.
578 struct sockaddr_storage z
;
582 rv
= recvfrom(s
, buf
, plen
, MSG_PEEK
,
583 (struct sockaddr
*)&z
, &len
);
587 rv
= connect(s
, (struct sockaddr
*)&z
, len
);
592 report_connect((struct sockaddr
*)&z
, len
, NULL
);
596 len
= sizeof(cliaddr
);
597 connfd
= accept4(s
, (struct sockaddr
*)&cliaddr
,
598 &len
, SOCK_NONBLOCK
);
600 /* For now, all errnos are fatal */
604 report_connect((struct sockaddr
*)&cliaddr
, len
,
605 family
== AF_UNIX
? host
: NULL
);
607 (tls_cctx
= tls_setup_server(tls_ctx
, connfd
, host
)))
608 readwrite(connfd
, tls_cctx
);
610 readwrite(connfd
, NULL
);
612 timeout_tls(s
, tls_cctx
, tls_close
);
618 if (family
!= AF_UNIX
)
621 if (connect(s
, NULL
, 0) < 0)
628 } else if (family
== AF_UNIX
) {
631 if ((s
= unix_connect(host
)) > 0) {
639 unlink(unix_dg_tmp_socket
);
645 /* Construct the portlist[] array. */
648 /* Cycle through portlist, connecting to each port. */
649 for (s
= -1, i
= 0; portlist
[i
] != NULL
; i
++) {
654 if ((tls_ctx
= tls_client()) == NULL
)
655 errx(1, "tls client creation failed");
656 if (tls_configure(tls_ctx
, tls_cfg
) == -1)
657 errx(1, "tls configuration failed (%s)",
661 s
= socks_connect(host
, portlist
[i
], hints
,
662 proxy
, proxyport
, proxyhints
, socksv
,
665 s
= remote_connect(host
, portlist
[i
], hints
);
671 if (vflag
|| zflag
) {
672 /* For UDP, make sure we are connected. */
674 if (udptest(s
) == -1) {
680 /* Don't look up port if -n. */
685 ntohs(atoi(portlist
[i
])),
686 uflag
? "udp" : "tcp");
690 "Connection to %s %s port [%s/%s] "
691 "succeeded!\n", host
, portlist
[i
],
692 uflag
? "udp" : "tcp",
693 sv
? sv
->s_name
: "*");
699 tls_setup_client(tls_ctx
, s
, host
);
701 readwrite(s
, tls_ctx
);
703 timeout_tls(s
, tls_ctx
, tls_close
);
714 tls_config_free(tls_cfg
);
721 * Returns a unix socket bound to the given path
724 unix_bind(char *path
, int flags
)
726 struct sockaddr_un s_un
;
729 /* Create unix domain socket. */
730 if ((s
= socket(AF_UNIX
, flags
| (uflag
? SOCK_DGRAM
: SOCK_STREAM
),
734 memset(&s_un
, 0, sizeof(struct sockaddr_un
));
735 s_un
.sun_family
= AF_UNIX
;
737 if (strlcpy(s_un
.sun_path
, path
, sizeof(s_un
.sun_path
)) >=
738 sizeof(s_un
.sun_path
)) {
740 errno
= ENAMETOOLONG
;
744 if (bind(s
, (struct sockaddr
*)&s_un
, sizeof(s_un
)) < 0) {
755 timeout_tls(int s
, struct tls
*tls_ctx
, int (*func
)(struct tls
*))
760 while ((ret
= (*func
)(tls_ctx
)) != 0) {
761 if (ret
== TLS_WANT_POLLIN
)
763 else if (ret
== TLS_WANT_POLLOUT
)
764 pfd
.events
= POLLOUT
;
768 if ((ret
= poll(&pfd
, 1, timeout
)) == 1)
775 err(1, "poll failed");
782 tls_setup_client(struct tls
*tls_ctx
, int s
, char *host
)
786 if (tls_connect_socket(tls_ctx
, s
,
787 tls_expectname
? tls_expectname
: host
) == -1) {
788 errx(1, "tls connection failed (%s)",
791 if (timeout_tls(s
, tls_ctx
, tls_handshake
) == -1) {
792 if ((errstr
= tls_error(tls_ctx
)) == NULL
)
793 errstr
= strerror(errno
);
794 errx(1, "tls handshake failed (%s)", errstr
);
797 report_tls(tls_ctx
, host
);
798 if (tls_expecthash
&& tls_peer_cert_hash(tls_ctx
) &&
799 strcmp(tls_expecthash
, tls_peer_cert_hash(tls_ctx
)) != 0)
800 errx(1, "peer certificate is not %s", tls_expecthash
);
802 save_peer_cert(tls_ctx
, Zflag
);
803 if (Zflag
!= stderr
&& (fclose(Zflag
) != 0))
804 err(1, "fclose failed saving peer cert");
809 tls_setup_server(struct tls
*tls_ctx
, int connfd
, char *host
)
811 struct tls
*tls_cctx
;
814 if (tls_accept_socket(tls_ctx
, &tls_cctx
, connfd
) == -1) {
815 warnx("tls accept failed (%s)", tls_error(tls_ctx
));
816 } else if (timeout_tls(connfd
, tls_cctx
, tls_handshake
) == -1) {
817 if ((errstr
= tls_error(tls_cctx
)) == NULL
)
818 errstr
= strerror(errno
);
819 warnx("tls handshake failed (%s)", errstr
);
821 int gotcert
= tls_peer_cert_provided(tls_cctx
);
823 if (vflag
&& gotcert
)
824 report_tls(tls_cctx
, host
);
825 if ((TLSopt
& TLS_CCERT
) && !gotcert
)
826 warnx("No client certificate provided");
827 else if (gotcert
&& tls_peer_cert_hash(tls_ctx
) && tls_expecthash
&&
828 strcmp(tls_expecthash
, tls_peer_cert_hash(tls_ctx
)) != 0)
829 warnx("peer certificate is not %s", tls_expecthash
);
830 else if (gotcert
&& tls_expectname
&&
831 (!tls_peer_cert_contains_name(tls_cctx
, tls_expectname
)))
832 warnx("name (%s) not found in client cert",
843 * Returns a socket connected to a local unix socket. Returns -1 on failure.
846 unix_connect(char *path
)
848 struct sockaddr_un s_un
;
852 if ((s
= unix_bind(unix_dg_tmp_socket
, SOCK_CLOEXEC
)) < 0)
855 if ((s
= socket(AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
, 0)) < 0)
859 memset(&s_un
, 0, sizeof(struct sockaddr_un
));
860 s_un
.sun_family
= AF_UNIX
;
862 if (strlcpy(s_un
.sun_path
, path
, sizeof(s_un
.sun_path
)) >=
863 sizeof(s_un
.sun_path
)) {
865 errno
= ENAMETOOLONG
;
868 if (connect(s
, (struct sockaddr
*)&s_un
, sizeof(s_un
)) < 0) {
880 * Create a unix domain socket, and listen on it.
883 unix_listen(char *path
)
886 if ((s
= unix_bind(path
, 0)) < 0)
889 if (listen(s
, 5) < 0) {
898 * Returns a socket connected to a remote host. Properly binds to a local
899 * port or source address if needed. Returns -1 on failure.
902 remote_connect(const char *host
, const char *port
, struct addrinfo hints
)
904 struct addrinfo
*res
, *res0
;
905 int s
= -1, error
, save_errno
;
910 if ((error
= getaddrinfo(host
, port
, &hints
, &res0
)))
911 errx(1, "getaddrinfo for host \"%s\" port %s: %s", host
,
912 port
, gai_strerror(error
));
914 for (res
= res0
; res
; res
= res
->ai_next
) {
915 if ((s
= socket(res
->ai_family
, res
->ai_socktype
|
916 SOCK_NONBLOCK
, res
->ai_protocol
)) < 0)
919 /* Bind to a local port or source address if specified. */
920 if (sflag
|| pflag
) {
921 struct addrinfo ahints
, *ares
;
924 /* try SO_BINDANY, but don't insist */
925 setsockopt(s
, SOL_SOCKET
, SO_BINDANY
, &on
, sizeof(on
));
927 memset(&ahints
, 0, sizeof(struct addrinfo
));
928 ahints
.ai_family
= res
->ai_family
;
929 ahints
.ai_socktype
= uflag
? SOCK_DGRAM
: SOCK_STREAM
;
930 ahints
.ai_protocol
= uflag
? IPPROTO_UDP
: IPPROTO_TCP
;
931 ahints
.ai_flags
= AI_PASSIVE
;
932 if ((error
= getaddrinfo(sflag
, pflag
, &ahints
, &ares
)))
933 errx(1, "getaddrinfo: %s", gai_strerror(error
));
935 if (bind(s
, (struct sockaddr
*)ares
->ai_addr
,
936 ares
->ai_addrlen
) < 0)
937 err(1, "bind failed");
941 set_common_sockopts(s
, res
->ai_family
);
943 if (timeout_connect(s
, res
->ai_addr
, res
->ai_addrlen
) == 0)
946 warn("connect to %s port %s (%s) failed", host
, port
,
947 uflag
? "udp" : "tcp");
961 timeout_connect(int s
, const struct sockaddr
*name
, socklen_t namelen
)
968 if ((ret
= connect(s
, name
, namelen
)) != 0 && errno
== EINPROGRESS
) {
970 pfd
.events
= POLLOUT
;
971 if ((ret
= poll(&pfd
, 1, timeout
)) == 1) {
972 optlen
= sizeof(optval
);
973 if ((ret
= getsockopt(s
, SOL_SOCKET
, SO_ERROR
,
974 &optval
, &optlen
)) == 0) {
976 ret
= optval
== 0 ? 0 : -1;
978 } else if (ret
== 0) {
982 err(1, "poll failed");
990 * Returns a socket listening on a local port, binds to specified source
991 * address. Returns -1 on failure.
994 local_listen(char *host
, char *port
, struct addrinfo hints
)
996 struct addrinfo
*res
, *res0
;
997 int s
= -1, save_errno
;
1003 /* Allow nodename to be null. */
1004 hints
.ai_flags
|= AI_PASSIVE
;
1007 * In the case of binding to a wildcard address
1008 * default to binding to an ipv4 address.
1010 if (host
== NULL
&& hints
.ai_family
== AF_UNSPEC
)
1011 hints
.ai_family
= AF_INET
;
1013 if ((error
= getaddrinfo(host
, port
, &hints
, &res0
)))
1014 errx(1, "getaddrinfo: %s", gai_strerror(error
));
1016 for (res
= res0
; res
; res
= res
->ai_next
) {
1017 if ((s
= socket(res
->ai_family
, res
->ai_socktype
,
1018 res
->ai_protocol
)) < 0)
1022 ret
= setsockopt(s
, SOL_SOCKET
, SO_REUSEPORT
, &x
, sizeof(x
));
1027 set_common_sockopts(s
, res
->ai_family
);
1029 if (bind(s
, (struct sockaddr
*)res
->ai_addr
,
1030 res
->ai_addrlen
) == 0)
1039 if (!uflag
&& s
!= -1) {
1040 if (listen(s
, 1) < 0)
1051 * Loop that polls on the network file descriptor and stdin.
1054 readwrite(int net_fd
, struct tls
*tls_ctx
)
1056 struct pollfd pfd
[4];
1057 int stdin_fd
= STDIN_FILENO
;
1058 int stdout_fd
= STDOUT_FILENO
;
1059 unsigned char netinbuf
[BUFSIZE
];
1060 size_t netinbufpos
= 0;
1061 unsigned char stdinbuf
[BUFSIZE
];
1062 size_t stdinbufpos
= 0;
1066 /* don't read from stdin if requested */
1071 pfd
[POLL_STDIN
].fd
= stdin_fd
;
1072 pfd
[POLL_STDIN
].events
= POLLIN
;
1075 pfd
[POLL_NETOUT
].fd
= net_fd
;
1076 pfd
[POLL_NETOUT
].events
= 0;
1079 pfd
[POLL_NETIN
].fd
= net_fd
;
1080 pfd
[POLL_NETIN
].events
= POLLIN
;
1083 pfd
[POLL_STDOUT
].fd
= stdout_fd
;
1084 pfd
[POLL_STDOUT
].events
= 0;
1087 /* both inputs are gone, buffers are empty, we are done */
1088 if (pfd
[POLL_STDIN
].fd
== -1 && pfd
[POLL_NETIN
].fd
== -1 &&
1089 stdinbufpos
== 0 && netinbufpos
== 0)
1091 /* both outputs are gone, we can't continue */
1092 if (pfd
[POLL_NETOUT
].fd
== -1 && pfd
[POLL_STDOUT
].fd
== -1)
1094 /* listen and net in gone, queues empty, done */
1095 if (lflag
&& pfd
[POLL_NETIN
].fd
== -1 &&
1096 stdinbufpos
== 0 && netinbufpos
== 0)
1099 /* help says -i is for "wait between lines sent". We read and
1100 * write arbitrary amounts of data, and we don't want to start
1101 * scanning for newlines, so this is as good as it gets */
1106 num_fds
= poll(pfd
, 4, timeout
);
1108 /* treat poll errors */
1110 err(1, "polling error");
1112 /* timeout happened */
1116 /* treat socket error conditions */
1117 for (n
= 0; n
< 4; n
++) {
1118 if (pfd
[n
].revents
& (POLLERR
|POLLNVAL
)) {
1122 /* reading is possible after HUP */
1123 if (pfd
[POLL_STDIN
].events
& POLLIN
&&
1124 pfd
[POLL_STDIN
].revents
& POLLHUP
&&
1125 !(pfd
[POLL_STDIN
].revents
& POLLIN
))
1126 pfd
[POLL_STDIN
].fd
= -1;
1128 if (pfd
[POLL_NETIN
].events
& POLLIN
&&
1129 pfd
[POLL_NETIN
].revents
& POLLHUP
&&
1130 !(pfd
[POLL_NETIN
].revents
& POLLIN
))
1131 pfd
[POLL_NETIN
].fd
= -1;
1133 if (pfd
[POLL_NETOUT
].revents
& POLLHUP
) {
1135 shutdown(pfd
[POLL_NETOUT
].fd
, SHUT_WR
);
1136 pfd
[POLL_NETOUT
].fd
= -1;
1138 /* if HUP, stop watching stdout */
1139 if (pfd
[POLL_STDOUT
].revents
& POLLHUP
)
1140 pfd
[POLL_STDOUT
].fd
= -1;
1141 /* if no net out, stop watching stdin */
1142 if (pfd
[POLL_NETOUT
].fd
== -1)
1143 pfd
[POLL_STDIN
].fd
= -1;
1144 /* if no stdout, stop watching net in */
1145 if (pfd
[POLL_STDOUT
].fd
== -1) {
1146 if (pfd
[POLL_NETIN
].fd
!= -1)
1147 shutdown(pfd
[POLL_NETIN
].fd
, SHUT_RD
);
1148 pfd
[POLL_NETIN
].fd
= -1;
1151 /* try to read from stdin */
1152 if (pfd
[POLL_STDIN
].revents
& POLLIN
&& stdinbufpos
< BUFSIZE
) {
1153 ret
= fillbuf(pfd
[POLL_STDIN
].fd
, stdinbuf
,
1154 &stdinbufpos
, NULL
);
1155 if (ret
== TLS_WANT_POLLIN
)
1156 pfd
[POLL_STDIN
].events
= POLLIN
;
1157 else if (ret
== TLS_WANT_POLLOUT
)
1158 pfd
[POLL_STDIN
].events
= POLLOUT
;
1159 else if (ret
== 0 || ret
== -1)
1160 pfd
[POLL_STDIN
].fd
= -1;
1161 /* read something - poll net out */
1162 if (stdinbufpos
> 0)
1163 pfd
[POLL_NETOUT
].events
= POLLOUT
;
1164 /* filled buffer - remove self from polling */
1165 if (stdinbufpos
== BUFSIZE
)
1166 pfd
[POLL_STDIN
].events
= 0;
1168 /* try to write to network */
1169 if (pfd
[POLL_NETOUT
].revents
& POLLOUT
&& stdinbufpos
> 0) {
1170 ret
= drainbuf(pfd
[POLL_NETOUT
].fd
, stdinbuf
,
1171 &stdinbufpos
, tls_ctx
);
1172 if (ret
== TLS_WANT_POLLIN
)
1173 pfd
[POLL_NETOUT
].events
= POLLIN
;
1174 else if (ret
== TLS_WANT_POLLOUT
)
1175 pfd
[POLL_NETOUT
].events
= POLLOUT
;
1177 pfd
[POLL_NETOUT
].fd
= -1;
1178 /* buffer empty - remove self from polling */
1179 if (stdinbufpos
== 0)
1180 pfd
[POLL_NETOUT
].events
= 0;
1181 /* buffer no longer full - poll stdin again */
1182 if (stdinbufpos
< BUFSIZE
)
1183 pfd
[POLL_STDIN
].events
= POLLIN
;
1185 /* try to read from network */
1186 if (pfd
[POLL_NETIN
].revents
& POLLIN
&& netinbufpos
< BUFSIZE
) {
1187 ret
= fillbuf(pfd
[POLL_NETIN
].fd
, netinbuf
,
1188 &netinbufpos
, tls_ctx
);
1189 if (ret
== TLS_WANT_POLLIN
)
1190 pfd
[POLL_NETIN
].events
= POLLIN
;
1191 else if (ret
== TLS_WANT_POLLOUT
)
1192 pfd
[POLL_NETIN
].events
= POLLOUT
;
1194 pfd
[POLL_NETIN
].fd
= -1;
1195 /* eof on net in - remove from pfd */
1197 shutdown(pfd
[POLL_NETIN
].fd
, SHUT_RD
);
1198 pfd
[POLL_NETIN
].fd
= -1;
1200 if (recvlimit
> 0 && ++recvcount
>= recvlimit
) {
1201 if (pfd
[POLL_NETIN
].fd
!= -1)
1202 shutdown(pfd
[POLL_NETIN
].fd
, SHUT_RD
);
1203 pfd
[POLL_NETIN
].fd
= -1;
1204 pfd
[POLL_STDIN
].fd
= -1;
1206 /* read something - poll stdout */
1207 if (netinbufpos
> 0)
1208 pfd
[POLL_STDOUT
].events
= POLLOUT
;
1209 /* filled buffer - remove self from polling */
1210 if (netinbufpos
== BUFSIZE
)
1211 pfd
[POLL_NETIN
].events
= 0;
1214 atelnet(pfd
[POLL_NETIN
].fd
, netinbuf
,
1217 /* try to write to stdout */
1218 if (pfd
[POLL_STDOUT
].revents
& POLLOUT
&& netinbufpos
> 0) {
1219 ret
= drainbuf(pfd
[POLL_STDOUT
].fd
, netinbuf
,
1220 &netinbufpos
, NULL
);
1221 if (ret
== TLS_WANT_POLLIN
)
1222 pfd
[POLL_STDOUT
].events
= POLLIN
;
1223 else if (ret
== TLS_WANT_POLLOUT
)
1224 pfd
[POLL_STDOUT
].events
= POLLOUT
;
1226 pfd
[POLL_STDOUT
].fd
= -1;
1227 /* buffer empty - remove self from polling */
1228 if (netinbufpos
== 0)
1229 pfd
[POLL_STDOUT
].events
= 0;
1230 /* buffer no longer full - poll net in again */
1231 if (netinbufpos
< BUFSIZE
)
1232 pfd
[POLL_NETIN
].events
= POLLIN
;
1235 /* stdin gone and queue empty? */
1236 if (pfd
[POLL_STDIN
].fd
== -1 && stdinbufpos
== 0) {
1237 if (pfd
[POLL_NETOUT
].fd
!= -1 && Nflag
)
1238 shutdown(pfd
[POLL_NETOUT
].fd
, SHUT_WR
);
1239 pfd
[POLL_NETOUT
].fd
= -1;
1241 /* net in gone and queue empty? */
1242 if (pfd
[POLL_NETIN
].fd
== -1 && netinbufpos
== 0) {
1243 pfd
[POLL_STDOUT
].fd
= -1;
1249 drainbuf(int fd
, unsigned char *buf
, size_t *bufpos
, struct tls
*tls
)
1255 n
= tls_write(tls
, buf
, *bufpos
);
1257 n
= write(fd
, buf
, *bufpos
);
1258 /* don't treat EAGAIN, EINTR as error */
1259 if (n
== -1 && (errno
== EAGAIN
|| errno
== EINTR
))
1260 n
= TLS_WANT_POLLOUT
;
1265 adjust
= *bufpos
- n
;
1267 memmove(buf
, buf
+ n
, adjust
);
1273 fillbuf(int fd
, unsigned char *buf
, size_t *bufpos
, struct tls
*tls
)
1275 size_t num
= BUFSIZE
- *bufpos
;
1279 n
= tls_read(tls
, buf
+ *bufpos
, num
);
1281 n
= read(fd
, buf
+ *bufpos
, num
);
1282 /* don't treat EAGAIN, EINTR as error */
1283 if (n
== -1 && (errno
== EAGAIN
|| errno
== EINTR
))
1284 n
= TLS_WANT_POLLIN
;
1294 * Pass the connected file descriptor to stdout and exit.
1302 char buf
[CMSG_SPACE(sizeof(int))];
1304 struct cmsghdr
*cmsg
;
1310 /* Avoid obvious stupidity */
1311 if (isatty(STDOUT_FILENO
))
1312 errx(1, "Cannot pass file descriptor to tty");
1314 bzero(&mh
, sizeof(mh
));
1315 bzero(&cmsgbuf
, sizeof(cmsgbuf
));
1316 bzero(&iov
, sizeof(iov
));
1318 mh
.msg_control
= (caddr_t
)&cmsgbuf
.buf
;
1319 mh
.msg_controllen
= sizeof(cmsgbuf
.buf
);
1320 cmsg
= CMSG_FIRSTHDR(&mh
);
1321 cmsg
->cmsg_len
= CMSG_LEN(sizeof(int));
1322 cmsg
->cmsg_level
= SOL_SOCKET
;
1323 cmsg
->cmsg_type
= SCM_RIGHTS
;
1324 *(int *)CMSG_DATA(cmsg
) = nfd
;
1331 bzero(&pfd
, sizeof(pfd
));
1332 pfd
.fd
= STDOUT_FILENO
;
1333 pfd
.events
= POLLOUT
;
1335 r
= sendmsg(STDOUT_FILENO
, &mh
, 0);
1337 if (errno
== EAGAIN
|| errno
== EINTR
) {
1338 if (poll(&pfd
, 1, -1) == -1)
1344 errx(1, "sendmsg: unexpected return value %zd", r
);
1351 /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
1353 atelnet(int nfd
, unsigned char *buf
, unsigned int size
)
1355 unsigned char *p
, *end
;
1356 unsigned char obuf
[4];
1360 end
= buf
+ size
- 2;
1362 for (p
= buf
; p
< end
; p
++) {
1368 if ((*p
== WILL
) || (*p
== WONT
))
1370 else if ((*p
== DO
) || (*p
== DONT
))
1377 if (atomicio(vwrite
, nfd
, obuf
, 3) != 3)
1378 warn("Write Error!");
1384 strtoport(char *portstr
, int udp
)
1386 struct servent
*entry
;
1391 proto
= udp
? "udp" : "tcp";
1393 port
= strtonum(portstr
, 1, PORT_MAX
, &errstr
);
1396 if (errno
!= EINVAL
)
1397 errx(1, "port number %s: %s", errstr
, portstr
);
1398 if ((entry
= getservbyname(portstr
, proto
)) == NULL
)
1399 errx(1, "service \"%s\" unknown", portstr
);
1400 return ntohs(entry
->s_port
);
1405 * Build an array of ports in portlist[], listing each port
1406 * that we should try to connect to.
1409 build_ports(char *p
)
1415 if ((n
= strchr(p
, '-')) != NULL
) {
1419 /* Make sure the ports are in order: lowest->highest. */
1420 hi
= strtoport(n
, uflag
);
1421 lo
= strtoport(p
, uflag
);
1429 * Initialize portlist with a random permutation. Based on
1430 * Knuth, as in ip_randomid() in sys/netinet/ip_id.c.
1433 for (x
= 0; x
<= hi
- lo
; x
++) {
1434 cp
= arc4random_uniform(x
+ 1);
1435 portlist
[x
] = portlist
[cp
];
1436 if (asprintf(&portlist
[cp
], "%d", x
+ lo
) < 0)
1439 } else { /* Load ports sequentially. */
1440 for (cp
= lo
; cp
<= hi
; cp
++) {
1441 if (asprintf(&portlist
[x
], "%d", cp
) < 0)
1449 hi
= strtoport(p
, uflag
);
1450 if (asprintf(&tmp
, "%d", hi
) != -1)
1459 * Do a few writes to see if the UDP port is there.
1460 * Fails once PF state table is full.
1467 for (i
= 0; i
<= 3; i
++) {
1468 if (write(s
, "X", 1) == 1)
1477 set_common_sockopts(int s
, int af
)
1483 if (setsockopt(s
, IPPROTO_TCP
, TCP_MD5SIG
,
1484 &x
, sizeof(x
)) == -1)
1489 if (setsockopt(s
, SOL_SOCKET
, SO_DEBUG
,
1490 &x
, sizeof(x
)) == -1)
1494 if (af
== AF_INET
&& setsockopt(s
, IPPROTO_IP
,
1495 IP_TOS
, &Tflag
, sizeof(Tflag
)) == -1)
1496 err(1, "set IP ToS");
1498 else if (af
== AF_INET6
&& setsockopt(s
, IPPROTO_IPV6
,
1499 IPV6_TCLASS
, &Tflag
, sizeof(Tflag
)) == -1)
1500 err(1, "set IPv6 traffic class");
1503 if (setsockopt(s
, SOL_SOCKET
, SO_RCVBUF
,
1504 &Iflag
, sizeof(Iflag
)) == -1)
1505 err(1, "set TCP receive buffer size");
1508 if (setsockopt(s
, SOL_SOCKET
, SO_SNDBUF
,
1509 &Oflag
, sizeof(Oflag
)) == -1)
1510 err(1, "set TCP send buffer size");
1514 if (af
== AF_INET
&& setsockopt(s
, IPPROTO_IP
,
1515 IP_TTL
, &ttl
, sizeof(ttl
)))
1516 err(1, "set IP TTL");
1518 else if (af
== AF_INET6
&& setsockopt(s
, IPPROTO_IPV6
,
1519 IPV6_UNICAST_HOPS
, &ttl
, sizeof(ttl
)))
1520 err(1, "set IPv6 unicast hops");
1525 if (af
== AF_INET
&& setsockopt(s
, IPPROTO_IP
,
1526 IP_MINTTL
, &minttl
, sizeof(minttl
)))
1527 err(1, "set IP min TTL");
1530 #ifdef IPV6_MINHOPCOUNT
1531 else if (af
== AF_INET6
&& setsockopt(s
, IPPROTO_IPV6
,
1532 IPV6_MINHOPCOUNT
, &minttl
, sizeof(minttl
)))
1533 err(1, "set IPv6 min hop count");
1539 map_tos(char *s
, int *val
)
1541 /* DiffServ Codepoints and other TOS mappings */
1542 const struct toskeywords
{
1543 const char *keyword
;
1545 } *t
, toskeywords
[] = {
1546 { "af11", IPTOS_DSCP_AF11
},
1547 { "af12", IPTOS_DSCP_AF12
},
1548 { "af13", IPTOS_DSCP_AF13
},
1549 { "af21", IPTOS_DSCP_AF21
},
1550 { "af22", IPTOS_DSCP_AF22
},
1551 { "af23", IPTOS_DSCP_AF23
},
1552 { "af31", IPTOS_DSCP_AF31
},
1553 { "af32", IPTOS_DSCP_AF32
},
1554 { "af33", IPTOS_DSCP_AF33
},
1555 { "af41", IPTOS_DSCP_AF41
},
1556 { "af42", IPTOS_DSCP_AF42
},
1557 { "af43", IPTOS_DSCP_AF43
},
1558 { "critical", IPTOS_PREC_CRITIC_ECP
},
1559 { "cs0", IPTOS_DSCP_CS0
},
1560 { "cs1", IPTOS_DSCP_CS1
},
1561 { "cs2", IPTOS_DSCP_CS2
},
1562 { "cs3", IPTOS_DSCP_CS3
},
1563 { "cs4", IPTOS_DSCP_CS4
},
1564 { "cs5", IPTOS_DSCP_CS5
},
1565 { "cs6", IPTOS_DSCP_CS6
},
1566 { "cs7", IPTOS_DSCP_CS7
},
1567 { "ef", IPTOS_DSCP_EF
},
1568 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL
},
1569 { "lowdelay", IPTOS_LOWDELAY
},
1570 { "netcontrol", IPTOS_PREC_NETCONTROL
},
1571 { "reliability", IPTOS_RELIABILITY
},
1572 { "throughput", IPTOS_THROUGHPUT
},
1576 for (t
= toskeywords
; t
->keyword
!= NULL
; t
++) {
1577 if (strcmp(s
, t
->keyword
) == 0) {
1587 map_tls(char *s
, int *val
)
1589 const struct tlskeywords
{
1590 const char *keyword
;
1592 } *t
, tlskeywords
[] = {
1593 { "tlsall", TLS_ALL
},
1594 { "noverify", TLS_NOVERIFY
},
1595 { "noname", TLS_NONAME
},
1596 { "clientcert", TLS_CCERT
},
1597 { "muststaple", TLS_MUSTSTAPLE
},
1598 { "tlscompat", TLS_COMPAT
},
1602 for (t
= tlskeywords
; t
->keyword
!= NULL
; t
++) {
1603 if (strcmp(s
, t
->keyword
) == 0) {
1612 save_peer_cert(struct tls
*tls_ctx
, FILE *fp
)
1617 if ((pem
= tls_peer_cert_chain_pem(tls_ctx
, &plen
)) == NULL
)
1618 errx(1, "Can't get peer certificate");
1619 if (fprintf(fp
, "%.*s", (int)plen
, pem
) < 0)
1620 err(1, "unable to save peer cert");
1621 if (fflush(fp
) != 0)
1622 err(1, "unable to flush peer cert");
1626 report_tls(struct tls
* tls_ctx
, char * host
)
1629 const char *ocsp_url
;
1631 fprintf(stderr
, "TLS handshake negotiated %s/%s with host %s\n",
1632 tls_conn_version(tls_ctx
), tls_conn_cipher(tls_ctx
), host
);
1633 fprintf(stderr
, "Peer name: %s\n",
1634 tls_expectname
? tls_expectname
: host
);
1635 if (tls_peer_cert_subject(tls_ctx
))
1636 fprintf(stderr
, "Subject: %s\n",
1637 tls_peer_cert_subject(tls_ctx
));
1638 if (tls_peer_cert_issuer(tls_ctx
))
1639 fprintf(stderr
, "Issuer: %s\n",
1640 tls_peer_cert_issuer(tls_ctx
));
1641 if ((t
= tls_peer_cert_notbefore(tls_ctx
)) != -1)
1642 fprintf(stderr
, "Valid From: %s", ctime(&t
));
1643 if ((t
= tls_peer_cert_notafter(tls_ctx
)) != -1)
1644 fprintf(stderr
, "Valid Until: %s", ctime(&t
));
1645 if (tls_peer_cert_hash(tls_ctx
))
1646 fprintf(stderr
, "Cert Hash: %s\n",
1647 tls_peer_cert_hash(tls_ctx
));
1648 ocsp_url
= tls_peer_ocsp_url(tls_ctx
);
1649 if (ocsp_url
!= NULL
)
1650 fprintf(stderr
, "OCSP URL: %s\n", ocsp_url
);
1651 switch (tls_peer_ocsp_response_status(tls_ctx
)) {
1652 case TLS_OCSP_RESPONSE_SUCCESSFUL
:
1653 fprintf(stderr
, "OCSP Stapling: %s\n",
1654 tls_peer_ocsp_result(tls_ctx
) == NULL
? "" :
1655 tls_peer_ocsp_result(tls_ctx
));
1657 " response_status=%d cert_status=%d crl_reason=%d\n",
1658 tls_peer_ocsp_response_status(tls_ctx
),
1659 tls_peer_ocsp_cert_status(tls_ctx
),
1660 tls_peer_ocsp_crl_reason(tls_ctx
));
1661 t
= tls_peer_ocsp_this_update(tls_ctx
);
1662 fprintf(stderr
, " this update: %s",
1663 t
!= -1 ? ctime(&t
) : "\n");
1664 t
= tls_peer_ocsp_next_update(tls_ctx
);
1665 fprintf(stderr
, " next update: %s",
1666 t
!= -1 ? ctime(&t
) : "\n");
1667 t
= tls_peer_ocsp_revocation_time(tls_ctx
);
1668 fprintf(stderr
, " revocation: %s",
1669 t
!= -1 ? ctime(&t
) : "\n");
1674 fprintf(stderr
, "OCSP Stapling: failure - response_status %d (%s)\n",
1675 tls_peer_ocsp_response_status(tls_ctx
),
1676 tls_peer_ocsp_result(tls_ctx
) == NULL
? "" :
1677 tls_peer_ocsp_result(tls_ctx
));
1684 report_connect(const struct sockaddr
*sa
, socklen_t salen
, char *path
)
1686 char remote_host
[NI_MAXHOST
];
1687 char remote_port
[NI_MAXSERV
];
1689 int flags
= NI_NUMERICSERV
;
1692 fprintf(stderr
, "Connection on %s received!\n", path
);
1697 flags
|= NI_NUMERICHOST
;
1699 if ((herr
= getnameinfo(sa
, salen
,
1700 remote_host
, sizeof(remote_host
),
1701 remote_port
, sizeof(remote_port
),
1703 if (herr
== EAI_SYSTEM
)
1704 err(1, "getnameinfo");
1706 errx(1, "getnameinfo: %s", gai_strerror(herr
));
1710 "Connection from %s %s "
1711 "received!\n", remote_host
, remote_port
);
1718 fprintf(stderr
, "\tCommand Summary:\n\
1721 \t-C certfile Public key file\n\
1723 \t-D Enable the debug socket option\n\
1724 \t-d Detach from stdin\n\
1725 \t-e name\t Required name in peer certificate\n\
1726 \t-F Pass socket fd\n\
1727 \t-H hash\t Hash string of peer certificate\n\
1728 \t-h This help text\n\
1729 \t-I length TCP receive buffer length\n\
1730 \t-i interval Delay interval for lines sent, ports scanned\n\
1731 \t-K keyfile Private key file\n\
1732 \t-k Keep inbound sockets open for multiple connects\n\
1733 \t-l Listen mode, for inbound connects\n\
1734 \t-M ttl Outgoing TTL / Hop Limit\n\
1735 \t-m minttl Minimum incoming TTL / Hop Limit\n\
1736 \t-N Shutdown the network socket after EOF on stdin\n\
1737 \t-n Suppress name/port resolutions\n\
1738 \t-O length TCP send buffer length\n\
1739 \t-o staplefile Staple file\n\
1740 \t-P proxyuser\tUsername for proxy authentication\n\
1741 \t-p port\t Specify local port for remote connects\n\
1742 \t-R CAfile CA bundle\n\
1743 \t-r Randomize remote ports\n"
1746 \t-S Enable the TCP MD5 signature option\n"
1749 \t-s source Local source address\n\
1750 \t-T keyword TOS value or TLS options\n\
1751 \t-t Answer TELNET negotiation\n\
1752 \t-U Use UNIX domain socket\n\
1756 \t-V rtable Specify alternate routing table\n"
1760 \t-W recvlimit Terminate after receiving a number of packets\n\
1761 \t-w timeout Timeout for connects and final net reads\n\
1762 \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
1763 \t-x addr[:port]\tSpecify proxy address and port\n\
1764 \t-Z Peer certificate file\n\
1765 \t-z Zero-I/O mode [used for scanning]\n\
1766 Port numbers can be individual or ranges: lo-hi [inclusive]\n");
1774 "usage: nc [-46cDdFhklNnrStUuvz] [-C certfile] [-e name] "
1775 "[-H hash] [-I length]\n"
1776 "\t [-i interval] [-K keyfile] [-M ttl] [-m minttl] [-O length]\n"
1777 "\t [-o staplefile] [-P proxy_username] [-p source_port] "
1779 "\t [-s source] [-T keyword] [-V rtable] [-W recvlimit] "
1781 "\t [-X proxy_protocol] [-x proxy_address[:port]] "
1782 "[-Z peercertfile]\n"
1783 "\t [destination] [port]\n");