On Fri, Jun 15, 2012 at 10:43:13AM -0400, Ricky Zhou wrote:
> This patch adds a message on incoming connections when netcat is run
> with -l and -v.  Does this look like a reasonable addition?

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. 

> 
> 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");

Your getnameinfo() call is not correct, you want something like this:

        if ((herr = getnameinfo(addr, alen, hbuf, sizeof(hbuf),
            pbuf, sizeof(pbuf), NI_NUMERICHOST|NI_NUMERICSERV)) != 0) {
                if (herr == EAI_SYSTEM)
                        err(1, "getnameinfo");
                else
                        errx(1, "getnameinfo: %s", gai_strerror(herr));

You can only trust errno if getnameinfo returned EAI_SYSTEM.


> +
> +     fprintf(stderr,
> +         "Connection from %s %s "
> +         "receieved!\n", remote_host, remote_port);

Typo here: receieved.

>  }
> 
>  void

Reply via email to