This patch adds a message on incoming connections when netcat is run
with -l and -v.  Does this look like a reasonable addition?

One thought is that report_connect might not need to err() when
getnameinfo fails thoughts?

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    15 Jun 2012 12:39:49 -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,27 @@
        }

        return (0);
+}
+
+void
+report_connect(const struct sockaddr *sa, socklen_t salen)
+{
+       char remote_host[NI_MAXHOST];
+       char remote_port[NI_MAXSERV];
+       int flags = NI_NUMERICSERV;
+
+       if (nflag)
+               flags |= NI_NUMERICHOST;
+
+       if (getnameinfo(sa, salen,
+           remote_host, sizeof(remote_host),
+           remote_port, sizeof(remote_port),
+           flags) != 0)
+               err(1, "getnameinfo");
+
+       fprintf(stderr,
+           "Connection from %s %s "
+           "receieved!\n", remote_host, remote_port);
 }

 void

Reply via email to