diff options
author | Christiano F. Haesbaert <haesbaert@cvs.openbsd.org> | 2012-07-07 09:36:31 +0000 |
---|---|---|
committer | Christiano F. Haesbaert <haesbaert@cvs.openbsd.org> | 2012-07-07 09:36:31 +0000 |
commit | 7dc5d0a40458db5084b2d033e8ccec9b23a31629 (patch) | |
tree | b5fbf765354a53816d9eeae32eac5fd2072c66f8 /usr.bin | |
parent | bb7eedfc8f3b896d4bdd56be67116d7a7045b22c (diff) |
Report incoming connections when -l is specified with -v.
From Ricky Zhou with a few tweaks by me.
ok henning@ haesbaert@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/nc/netcat.c | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/usr.bin/nc/netcat.c b/usr.bin/nc/netcat.c index 6b372033e75..4bef71a7ca4 100644 --- a/usr.bin/nc/netcat.c +++ b/usr.bin/nc/netcat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: netcat.c,v 1.107 2012/04/01 02:58:57 deraadt Exp $ */ +/* $OpenBSD: netcat.c,v 1.108 2012/07/07 09:36:30 haesbaert Exp $ */ /* * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> * @@ -106,6 +106,7 @@ int unix_connect(char *); int unix_listen(char *); void set_common_sockopts(int); int map_tos(char *, int *); +void report_connect(const struct sockaddr *, socklen_t); void usage(int); int @@ -364,6 +365,9 @@ main(int argc, char *argv[]) if (rv < 0) err(1, "connect"); + if (vflag) + report_connect((struct sockaddr *)&z, len); + readwrite(s); } else { len = sizeof(cliaddr); @@ -371,6 +375,10 @@ main(int argc, char *argv[]) &len); if (connfd == -1) err(1, "accept"); + + if (vflag) + report_connect((struct sockaddr *)&cliaddr, len); + readwrite(connfd); close(connfd); } @@ -960,6 +968,32 @@ map_tos(char *s, int *val) } void +report_connect(const struct sockaddr *sa, socklen_t salen) +{ + char remote_host[NI_MAXHOST]; + char remote_port[NI_MAXSERV]; + int herr; + int flags = NI_NUMERICSERV; + + if (nflag) + flags |= NI_NUMERICHOST; + + if ((herr = getnameinfo(sa, salen, + remote_host, sizeof(remote_host), + remote_port, sizeof(remote_port), + flags)) != 0) { + if (herr == EAI_SYSTEM) + err(1, "getnameinfo"); + else + errx(1, "getnameinfo: %s", gai_strerror(herr)); + } + + fprintf(stderr, + "Connection from %s %s " + "received!\n", remote_host, remote_port); +} + +void help(void) { usage(0); |