On 2012-06-16 02:37:27 PM, Christiano F. Haesbaert wrote:
> I guess so, I don't use nc too often but it sounds reasonable to me,
> your code has a few notes though, please check inline.
(Sorry for the slow response, was travelling last week)
Thanks for looking at the patch - here's a new one with your fixes
included.
Thanks,
Ricky
Index: netcat.c
===================================================================
RCS file: /cvs/src/usr.bin/nc/netcat.c,v
retrieving revision 1.107
diff -u netcat.c
--- netcat.c 1 Apr 2012 02:58:57 -0000 1.107
+++ netcat.c 24 Jun 2012 09:51:19 -0000
@@ -106,6 +106,7 @@
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 @@
if (rv < 0)
err(1, "connect");
+ if (vflag)
+ report_connect((struct sockaddr *)&z,
len);
+
readwrite(s);
} else {
len = sizeof(cliaddr);
@@ -371,6 +375,10 @@
&len);
if (connfd == -1)
err(1, "accept");
+
+ if (vflag)
+ report_connect((struct sockaddr
*)&cliaddr, len);
+
readwrite(connfd);
close(connfd);
}
@@ -957,6 +965,32 @@
}
return (0);
+}
+
+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