From: Aron Xu Date: Mon, 13 Feb 2012 15:38:15 +0800 Subject: verbose numeric port --- netcat.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) --- a/netcat.c +++ b/netcat.c @@ -43,6 +43,7 @@ #include #include #include +#include #ifdef __linux__ # include #endif @@ -689,6 +690,21 @@ main(int argc, char *argv[]) } if (s < 0) err(1, NULL); + + if (vflag && (family != AF_UNIX)) { + char* local; + if (family == AF_INET) + local = "0.0.0.0"; + else if (family == AF_INET6) + local = "::"; + else + local = "unknown"; + fprintf(stderr, "Listening on [%s] (family %d, port %d)\n", + host ?: local, + family, + *uport); + } + if (uflag && kflag) { /* * For UDP and -k, don't connect the socket, @@ -708,20 +724,19 @@ main(int argc, char *argv[]) */ int rv; char buf[2048]; - struct sockaddr_storage z; - len = sizeof(z); + len = sizeof(cliaddr); rv = recvfrom(s, buf, sizeof(buf), MSG_PEEK, - (struct sockaddr *)&z, &len); + (struct sockaddr *)&cliaddr, &len); if (rv < 0) err(1, "recvfrom"); - rv = connect(s, (struct sockaddr *)&z, len); + rv = connect(s, (struct sockaddr *)&cliaddr, len); if (rv < 0) err(1, "connect"); if (vflag) - report_connect((struct sockaddr *)&z, len, NULL); + report_connect((struct sockaddr *)&cliaddr, len, NULL); # if defined(TLS) readwrite(s, NULL);